
Recherche avancée
Médias (1)
-
Richard Stallman et le logiciel libre
19 octobre 2011, par
Mis à jour : Mai 2013
Langue : français
Type : Texte
Autres articles (71)
-
MediaSPIP version 0.1 Beta
16 avril 2011, parMediaSPIP 0.1 beta est la première version de MediaSPIP décrétée comme "utilisable".
Le fichier zip ici présent contient uniquement les sources de MediaSPIP en version standalone.
Pour avoir une installation fonctionnelle, il est nécessaire d’installer manuellement l’ensemble des dépendances logicielles sur le serveur.
Si vous souhaitez utiliser cette archive pour une installation en mode ferme, il vous faudra également procéder à d’autres modifications (...) -
MediaSPIP 0.1 Beta version
25 avril 2011, parMediaSPIP 0.1 beta is the first version of MediaSPIP proclaimed as "usable".
The zip file provided here only contains the sources of MediaSPIP in its standalone version.
To get a working installation, you must manually install all-software dependencies on the server.
If you want to use this archive for an installation in "farm mode", you will also need to proceed to other manual (...) -
Amélioration de la version de base
13 septembre 2013Jolie sélection multiple
Le plugin Chosen permet d’améliorer l’ergonomie des champs de sélection multiple. Voir les deux images suivantes pour comparer.
Il suffit pour cela d’activer le plugin Chosen (Configuration générale du site > Gestion des plugins), puis de configurer le plugin (Les squelettes > Chosen) en activant l’utilisation de Chosen dans le site public et en spécifiant les éléments de formulaires à améliorer, par exemple select[multiple] pour les listes à sélection multiple (...)
Sur d’autres sites (8410)
-
How can I fix a segmentation fault in a C program ? [duplicate]
31 mars 2023, par ipegasus

Possible Duplicate :

Segmentation fault



Currently I am upgrading an open source program used for HTTP streaming. It needs to support the latest FFmpeg.
The code compiles fine without any warnings, although I am getting a segmentation fault error.


How can I fix the issue ? And / or, what is the best way to debug ? Please find attached a portion of the code due to size. I will try to add the project to GitHub :)


Sample Usage


# segmenter --i out.ts --l 10 --o stream.m3u8 --d segments --f stream



Makefile


FFLIBS=`pkg-config --libs libavformat libavcodec libavutil`
FFFLAGS=`pkg-config --cflags libavformat libavcodec libavutil`

all:
 gcc -Wall -g segmenter.c -o segmenter ${FFFLAGS} ${FFLIBS}



segmenter.c


/*
 * Copyright (c) 2009 Chase Douglas
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#include 
#include 
#include 
#include 
#include 
#include "libavformat/avformat.h"

#include "libavformat/avio.h"

#include <sys></sys>stat.h>

#include "segmenter.h"
#include "libavformat/avformat.h"

#define IMAGE_ID3_SIZE 9171

void printUsage() {
 fprintf(stderr, "\nExample: segmenter --i infile --d baseDir --f baseFileName --o playListFile.m3u8 --l 10 \n");
 fprintf(stderr, "\nOptions: \n");
 fprintf(stderr, "--i <infile>.\n");
 fprintf(stderr, "--o <outfile>.\n");
 fprintf(stderr, "--d basedir, the base directory for files.\n");
 fprintf(stderr, "--f baseFileName, output files will be baseFileName-#.\n");
 fprintf(stderr, "--l segment length, the length of each segment.\n");
 fprintf(stderr, "--a, audio only decode for < 64k streams.\n");
 fprintf(stderr, "--v, video only decode for < 64k streams.\n");
 fprintf(stderr, "--version, print version details and exit.\n");
 fprintf(stderr, "\n\n");
}

void ffmpeg_version() {
 // output build and version numbers
 fprintf(stderr, " libavutil version: %s\n", AV_STRINGIFY(LIBAVUTIL_VERSION));
 fprintf(stderr, " libavutil build: %d\n", LIBAVUTIL_BUILD);
 fprintf(stderr, " libavcodec version: %s\n", AV_STRINGIFY(LIBAVCODEC_VERSION));
 fprintf(stdout, " libavcodec build: %d\n", LIBAVCODEC_BUILD);
 fprintf(stderr, " libavformat version: %s\n", AV_STRINGIFY(LIBAVFORMAT_VERSION));
 fprintf(stderr, " libavformat build: %d\n", LIBAVFORMAT_BUILD);
 fprintf(stderr, " built on " __DATE__ " " __TIME__);
#ifdef __GNUC__
 fprintf(stderr, ", gcc: " __VERSION__ "\n");
#else
 fprintf(stderr, ", using a non-gcc compiler\n");
#endif
}


static AVStream *add_output_stream(AVFormatContext *output_format_context, AVStream *input_stream) {
 AVCodecContext *input_codec_context;
 AVCodecContext *output_codec_context;
 AVStream *output_stream;

 output_stream = avformat_new_stream(output_format_context, 0);
 if (!output_stream) {
 fprintf(stderr, "Segmenter error: Could not allocate stream\n");
 exit(1);
 }

 input_codec_context = input_stream->codec;
 output_codec_context = output_stream->codec;

 output_codec_context->codec_id = input_codec_context->codec_id;
 output_codec_context->codec_type = input_codec_context->codec_type;
 output_codec_context->codec_tag = input_codec_context->codec_tag;
 output_codec_context->bit_rate = input_codec_context->bit_rate;
 output_codec_context->extradata = input_codec_context->extradata;
 output_codec_context->extradata_size = input_codec_context->extradata_size;

 if (av_q2d(input_codec_context->time_base) * input_codec_context->ticks_per_frame > av_q2d(input_stream->time_base) && av_q2d(input_stream->time_base) < 1.0 / 1000) {
 output_codec_context->time_base = input_codec_context->time_base;
 output_codec_context->time_base.num *= input_codec_context->ticks_per_frame;
 } else {
 output_codec_context->time_base = input_stream->time_base;
 }

 switch (input_codec_context->codec_type) {
#ifdef USE_OLD_FFMPEG
 case CODEC_TYPE_AUDIO:
#else
 case AVMEDIA_TYPE_AUDIO:
#endif
 output_codec_context->channel_layout = input_codec_context->channel_layout;
 output_codec_context->sample_rate = input_codec_context->sample_rate;
 output_codec_context->channels = input_codec_context->channels;
 output_codec_context->frame_size = input_codec_context->frame_size;
 if ((input_codec_context->block_align == 1 && input_codec_context->codec_id == CODEC_ID_MP3) || input_codec_context->codec_id == CODEC_ID_AC3) {
 output_codec_context->block_align = 0;
 } else {
 output_codec_context->block_align = input_codec_context->block_align;
 }
 break;
#ifdef USE_OLD_FFMPEG
 case CODEC_TYPE_VIDEO:
#else
 case AVMEDIA_TYPE_VIDEO:
#endif
 output_codec_context->pix_fmt = input_codec_context->pix_fmt;
 output_codec_context->width = input_codec_context->width;
 output_codec_context->height = input_codec_context->height;
 output_codec_context->has_b_frames = input_codec_context->has_b_frames;

 if (output_format_context->oformat->flags & AVFMT_GLOBALHEADER) {
 output_codec_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
 }
 break;
 default:
 break;
 }

 return output_stream;
}

int write_index_file(const char index[], const char tmp_index[], const unsigned int planned_segment_duration, const unsigned int actual_segment_duration[],
 const char output_directory[], const char output_prefix[], const char output_file_extension[],
 const unsigned int first_segment, const unsigned int last_segment) {
 FILE *index_fp;
 char *write_buf;
 unsigned int i;

 index_fp = fopen(tmp_index, "w");
 if (!index_fp) {
 fprintf(stderr, "Could not open temporary m3u8 index file (%s), no index file will be created\n", tmp_index);
 return -1;
 }

 write_buf = malloc(sizeof (char) * 1024);
 if (!write_buf) {
 fprintf(stderr, "Could not allocate write buffer for index file, index file will be invalid\n");
 fclose(index_fp);
 return -1;
 }

 unsigned int maxDuration = planned_segment_duration;

 for (i = first_segment; i <= last_segment; i++)
 if (actual_segment_duration[i] > maxDuration)
 maxDuration = actual_segment_duration[i];



 snprintf(write_buf, 1024, "#EXTM3U\n#EXT-X-TARGETDURATION:%u\n", maxDuration);

 if (fwrite(write_buf, strlen(write_buf), 1, index_fp) != 1) {
 fprintf(stderr, "Could not write to m3u8 index file, will not continue writing to index file\n");
 free(write_buf);
 fclose(index_fp);
 return -1;
 }

 for (i = first_segment; i <= last_segment; i++) {
 snprintf(write_buf, 1024, "#EXTINF:%u,\n%s-%u%s\n", actual_segment_duration[i], output_prefix, i, output_file_extension);
 if (fwrite(write_buf, strlen(write_buf), 1, index_fp) != 1) {
 fprintf(stderr, "Could not write to m3u8 index file, will not continue writing to index file\n");
 free(write_buf);
 fclose(index_fp);
 return -1;
 }
 }

 snprintf(write_buf, 1024, "#EXT-X-ENDLIST\n");
 if (fwrite(write_buf, strlen(write_buf), 1, index_fp) != 1) {
 fprintf(stderr, "Could not write last file and endlist tag to m3u8 index file\n");
 free(write_buf);
 fclose(index_fp);
 return -1;
 }

 free(write_buf);
 fclose(index_fp);

 return rename(tmp_index, index);
}

int main(int argc, const char *argv[]) {
 //input parameters
 char inputFilename[MAX_FILENAME_LENGTH], playlistFilename[MAX_FILENAME_LENGTH], baseDirName[MAX_FILENAME_LENGTH], baseFileName[MAX_FILENAME_LENGTH];
 char baseFileExtension[5]; //either "ts", "aac" or "mp3"
 int segmentLength, outputStreams, verbosity, version;



 char currentOutputFileName[MAX_FILENAME_LENGTH];
 char tempPlaylistName[MAX_FILENAME_LENGTH];


 //these are used to determine the exact length of the current segment
 double prev_segment_time = 0;
 double segment_time;
 unsigned int actual_segment_durations[2048];
 double packet_time = 0;

 //new variables to keep track of output size
 double output_bytes = 0;

 unsigned int output_index = 1;
 AVOutputFormat *ofmt;
 AVFormatContext *ic = NULL;
 AVFormatContext *oc;
 AVStream *video_st = NULL;
 AVStream *audio_st = NULL;
 AVCodec *codec;
 int video_index;
 int audio_index;
 unsigned int first_segment = 1;
 unsigned int last_segment = 0;
 int write_index = 1;
 int decode_done;
 int ret;
 int i;

 unsigned char id3_tag[128];
 unsigned char * image_id3_tag;

 size_t id3_tag_size = 73;
 int newFile = 1; //a boolean value to flag when a new file needs id3 tag info in it

 if (parseCommandLine(inputFilename, playlistFilename, baseDirName, baseFileName, baseFileExtension, &outputStreams, &segmentLength, &verbosity, &version, argc, argv) != 0)
 return 0;

 if (version) {
 ffmpeg_version();
 return 0;
 }


 fprintf(stderr, "%s %s\n", playlistFilename, tempPlaylistName);


 image_id3_tag = malloc(IMAGE_ID3_SIZE);
 if (outputStreams == OUTPUT_STREAM_AUDIO)
 build_image_id3_tag(image_id3_tag);
 build_id3_tag((char *) id3_tag, id3_tag_size);

 snprintf(tempPlaylistName, strlen(playlistFilename) + strlen(baseDirName) + 1, "%s%s", baseDirName, playlistFilename);
 strncpy(playlistFilename, tempPlaylistName, strlen(tempPlaylistName));
 strncpy(tempPlaylistName, playlistFilename, MAX_FILENAME_LENGTH);
 strncat(tempPlaylistName, ".", 1);

 //decide if this is an aac file or a mpegts file.
 //postpone deciding format until later
 /* ifmt = av_find_input_format("mpegts");
 if (!ifmt)
 {
 fprintf(stderr, "Could not find MPEG-TS demuxer.\n");
 exit(1);
 } */

 av_log_set_level(AV_LOG_DEBUG);

 av_register_all();
 ret = avformat_open_input(&ic, inputFilename, NULL, NULL);
 if (ret != 0) {
 fprintf(stderr, "Could not open input file %s. Error %d.\n", inputFilename, ret);
 exit(1);
 }

 if (avformat_find_stream_info(ic, NULL) < 0) {
 fprintf(stderr, "Could not read stream information.\n");
 exit(1);
 }

 oc = avformat_alloc_context();
 if (!oc) {
 fprintf(stderr, "Could not allocate output context.");
 exit(1);
 }

 video_index = -1;
 audio_index = -1;

 for (i = 0; i < ic->nb_streams && (video_index < 0 || audio_index < 0); i++) {
 switch (ic->streams[i]->codec->codec_type) {

#ifdef USE_OLD_FFMPEG
 case CODEC_TYPE_VIDEO:
#else
 case AVMEDIA_TYPE_VIDEO:
#endif
 video_index = i;
 ic->streams[i]->discard = AVDISCARD_NONE;
 if (outputStreams & OUTPUT_STREAM_VIDEO)
 video_st = add_output_stream(oc, ic->streams[i]);
 break;
#ifdef USE_OLD_FFMPEG
 case CODEC_TYPE_AUDIO:
#else
 case AVMEDIA_TYPE_AUDIO:
#endif
 audio_index = i;
 ic->streams[i]->discard = AVDISCARD_NONE;
 if (outputStreams & OUTPUT_STREAM_AUDIO)
 audio_st = add_output_stream(oc, ic->streams[i]);
 break;
 default:
 ic->streams[i]->discard = AVDISCARD_ALL;
 break;
 }
 }

 if (video_index == -1) {
 fprintf(stderr, "Stream must have video component.\n");
 exit(1);
 }

 //now that we know the audio and video output streams
 //we can decide on an output format.
 if (outputStreams == OUTPUT_STREAM_AUDIO) {
 //the audio output format should be the same as the audio input format
 switch (ic->streams[audio_index]->codec->codec_id) {
 case CODEC_ID_MP3:
 fprintf(stderr, "Setting output audio to mp3.");
 strncpy(baseFileExtension, ".mp3", strlen(".mp3"));
 ofmt = av_guess_format("mp3", NULL, NULL);
 break;
 case CODEC_ID_AAC:
 fprintf(stderr, "Setting output audio to aac.");
 ofmt = av_guess_format("adts", NULL, NULL);
 break;
 default:
 fprintf(stderr, "Codec id %d not supported.\n", ic->streams[audio_index]->id);
 }
 if (!ofmt) {
 fprintf(stderr, "Could not find audio muxer.\n");
 exit(1);
 }
 } else {
 ofmt = av_guess_format("mpegts", NULL, NULL);
 if (!ofmt) {
 fprintf(stderr, "Could not find MPEG-TS muxer.\n");
 exit(1);
 }
 }
 oc->oformat = ofmt;

 if (outputStreams & OUTPUT_STREAM_VIDEO && oc->oformat->flags & AVFMT_GLOBALHEADER) {
 oc->flags |= CODEC_FLAG_GLOBAL_HEADER;
 }


 /* Deprecated: pass the options to avformat_write_header directly.
 if (av_set_parameters(oc, NULL) < 0) {
 fprintf(stderr, "Invalid output format parameters.\n");
 exit(1);
 }
 */

 av_dump_format(oc, 0, baseFileName, 1);


 //open the video codec only if there is video data
 if (video_index != -1) {
 if (outputStreams & OUTPUT_STREAM_VIDEO)
 codec = avcodec_find_decoder(video_st->codec->codec_id);
 else
 codec = avcodec_find_decoder(ic->streams[video_index]->codec->codec_id);
 if (!codec) {
 fprintf(stderr, "Could not find video decoder, key frames will not be honored.\n");
 }

 if (outputStreams & OUTPUT_STREAM_VIDEO)
 ret = avcodec_open2(video_st->codec, codec, NULL);
 else
 avcodec_open2(ic->streams[video_index]->codec, codec, NULL);
 if (ret < 0) {
 fprintf(stderr, "Could not open video decoder, key frames will not be honored.\n");
 }
 }

 snprintf(currentOutputFileName, strlen(baseDirName) + strlen(baseFileName) + strlen(baseFileExtension) + 10, "%s%s-%u%s", baseDirName, baseFileName, output_index++, baseFileExtension);

 if (avio_open(&oc->pb, currentOutputFileName, URL_WRONLY) < 0) {
 fprintf(stderr, "Could not open '%s'.\n", currentOutputFileName);
 exit(1);
 }
 newFile = 1;

 int r = avformat_write_header(oc,NULL);
 if (r) {
 fprintf(stderr, "Could not write mpegts header to first output file.\n");
 debugReturnCode(r);
 exit(1);
 }

 //no segment info is written here. This just creates the shell of the playlist file
 write_index = !write_index_file(playlistFilename, tempPlaylistName, segmentLength, actual_segment_durations, baseDirName, baseFileName, baseFileExtension, first_segment, last_segment);

 do {
 AVPacket packet;

 decode_done = av_read_frame(ic, &packet);

 if (decode_done < 0) {
 break;
 }

 if (av_dup_packet(&packet) < 0) {
 fprintf(stderr, "Could not duplicate packet.");
 av_free_packet(&packet);
 break;
 }

 //this time is used to check for a break in the segments
 // if (packet.stream_index == video_index && (packet.flags & PKT_FLAG_KEY))
 // {
 // segment_time = (double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;
 // }
#if USE_OLD_FFMPEG
 if (packet.stream_index == video_index && (packet.flags & PKT_FLAG_KEY))
#else
 if (packet.stream_index == video_index && (packet.flags & AV_PKT_FLAG_KEY))
#endif
 {
 segment_time = (double) packet.pts * ic->streams[video_index]->time_base.num / ic->streams[video_index]->time_base.den;
 }
 // else if (video_index < 0)
 // {
 // segment_time = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
 // }

 //get the most recent packet time
 //this time is used when the time for the final segment is printed. It may not be on the edge of
 //of a keyframe!
 if (packet.stream_index == video_index)
 packet_time = (double) packet.pts * ic->streams[video_index]->time_base.num / ic->streams[video_index]->time_base.den; //(double)video_st->pts.val * video_st->time_base.num / video_st->time_base.den;
 else if (outputStreams & OUTPUT_STREAM_AUDIO)
 packet_time = (double) audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
 else
 continue;
 //start looking for segment splits for videos one half second before segment duration expires. This is because the
 //segments are split on key frames so we cannot expect all segments to be split exactly equally.
 if (segment_time - prev_segment_time >= segmentLength - 0.5) {
 fprintf(stderr, "looking to print index file at time %lf\n", segment_time);
 avio_flush(oc->pb);
 avio_close(oc->pb);

 if (write_index) {
 actual_segment_durations[++last_segment] = (unsigned int) rint(segment_time - prev_segment_time);
 write_index = !write_index_file(playlistFilename, tempPlaylistName, segmentLength, actual_segment_durations, baseDirName, baseFileName, baseFileExtension, first_segment, last_segment);
 fprintf(stderr, "Writing index file at time %lf\n", packet_time);
 }

 struct stat st;
 stat(currentOutputFileName, &st);
 output_bytes += st.st_size;

 snprintf(currentOutputFileName, strlen(baseDirName) + strlen(baseFileName) + strlen(baseFileExtension) + 10, "%s%s-%u%s", baseDirName, baseFileName, output_index++, baseFileExtension);
 if (avio_open(&oc->pb, currentOutputFileName, URL_WRONLY) < 0) {
 fprintf(stderr, "Could not open '%s'\n", currentOutputFileName);
 break;
 }

 newFile = 1;
 prev_segment_time = segment_time;
 }

 if (outputStreams == OUTPUT_STREAM_AUDIO && packet.stream_index == audio_index) {
 if (newFile && outputStreams == OUTPUT_STREAM_AUDIO) {
 //add id3 tag info
 //fprintf(stderr, "adding id3tag to file %s\n", currentOutputFileName);
 //printf("%lf %lld %lld %lld %lld %lld %lf\n", segment_time, audio_st->pts.val, audio_st->cur_dts, audio_st->cur_pkt.pts, packet.pts, packet.dts, packet.dts * av_q2d(ic->streams[audio_index]->time_base) );
 fill_id3_tag((char*) id3_tag, id3_tag_size, packet.dts);
 avio_write(oc->pb, id3_tag, id3_tag_size);
 avio_write(oc->pb, image_id3_tag, IMAGE_ID3_SIZE);
 avio_flush(oc->pb);
 newFile = 0;
 }

 packet.stream_index = 0; //only one stream in audio only segments
 ret = av_interleaved_write_frame(oc, &packet);
 } else if (outputStreams & OUTPUT_STREAM_VIDEO) {
 if (newFile) {
 //fprintf(stderr, "New File: %lld %lld %lld\n", packet.pts, video_st->pts.val, audio_st->pts.val);
 //printf("%lf %lld %lld %lld %lld %lld %lf\n", segment_time, audio_st->pts.val, audio_st->cur_dts, audio_st->cur_pkt.pts, packet.pts, packet.dts, packet.dts * av_q2d(ic->streams[audio_index]->time_base) );
 newFile = 0;
 }
 if (outputStreams == OUTPUT_STREAM_VIDEO)
 ret = av_write_frame(oc, &packet);
 else
 ret = av_interleaved_write_frame(oc, &packet);
 }

 if (ret < 0) {
 fprintf(stderr, "Warning: Could not write frame of stream.\n");
 } else if (ret > 0) {
 fprintf(stderr, "End of stream requested.\n");
 av_free_packet(&packet);
 break;
 }

 av_free_packet(&packet);
 } while (!decode_done);

 //make sure all packets are written and then close the last file.
 avio_flush(oc->pb);
 av_write_trailer(oc);

 if (video_st && video_st->codec)
 avcodec_close(video_st->codec);

 if (audio_st && audio_st->codec)
 avcodec_close(audio_st->codec);

 for (i = 0; i < oc->nb_streams; i++) {
 av_freep(&oc->streams[i]->codec);
 av_freep(&oc->streams[i]);
 }

 avio_close(oc->pb);
 av_free(oc);

 struct stat st;
 stat(currentOutputFileName, &st);
 output_bytes += st.st_size;


 if (write_index) {
 actual_segment_durations[++last_segment] = (unsigned int) rint(packet_time - prev_segment_time);

 //make sure that the last segment length is not zero
 if (actual_segment_durations[last_segment] == 0)
 actual_segment_durations[last_segment] = 1;

 write_index_file(playlistFilename, tempPlaylistName, segmentLength, actual_segment_durations, baseDirName, baseFileName, baseFileExtension, first_segment, last_segment);

 }

 write_stream_size_file(baseDirName, baseFileName, output_bytes * 8 / segment_time);

 return 0;
}
</outfile></infile>


-
dyld[16458] : Library not loaded : @rpath/libavcodec.framework/libavcodec while running my flutter app on iOS
31 janvier 2023, par Stéphane de LucaCompilation is successful. But When I run the code, I get this.


Here is the full trace.


Any idea ?


dyld[16458]: Library not loaded: @rpath/libavcodec.framework/libavcodec
 Referenced from: <3500F5CF-B1D2-30EC-8D7F-1C29BD45D05E> /private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Runner
 Reason: tried: '/usr/lib/swift/libavcodec.framework/libavcodec' (errno=2, not in dyld cache), '/private/preboot/Cryptexes/OS/usr/lib/swift/libavcodec.framework/libavcodec' (errno=2), '/usr/lib/swift/libavcodec.framework/libavcodec' (errno=2, not in dyld cache), '/private/preboot/Cryptexes/OS/usr/lib/swift/libavcodec.framework/libavcodec' (errno=2), '/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec' (errno=2), '/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec' (errno=2), '/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec' (errno=2), '/usr/lib/swift/libavcodec.framework/libavcodec' (errno=2, not in dyld cache), '/private/preboot/Cryptexes/OS/usr/lib/swift/libavcodec.framework/libavcodec' (errno=2), '/usr/lib/swift/libavcodec.framework/libavcodec' (errno=2, not in dyld cache), '/private/preboot/Cryptexes/OS/usr/lib/swift/libavcodec.framework/libavcodec' (errno=2), '/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec' (errno=2), '/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec' (errno=2), '/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec' (errno=2), '/private/preboot/Cryptexes/OS@rpath/libavcodec.framework/libavcodec' (errno=2), '/usr/lib/swift/libavcodec.framework/libavcodec' (errno=2, not in dyld cache), '/private/preboot/Cryptexes/OS/usr/lib/swift/libavcodec.framework/libavcodec' (errno=2), '/usr/lib/swift/libavcodec.framework/libavcodec' (errno=2, not in dyld cache), '/private/preboot/Cryptexes/OS/usr/lib/swift/libavcodec.framework/libavcodec' (errno=2), '/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec' (errno=2), '/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec' (errno=2), '/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec' (errno=2), '/usr/lib/swift/libavcodec.framework/libavcodec' (errno=2, not in dyld cache), '/private/preboot/Cryptexes/OS/usr/lib/swift/libavcodec.framework/libavcodec' (errno=2), '/usr/lib/swift/libavcodec.framework/libavcodec' (errno=2, not in dyld cache), '/private/preboot/Cryptexes/OS/usr/lib/swift/libavcodec.framework/libavcodec' (errno=2), '/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec' (errno=2), '/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec' (errno=2), '/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec' (errno=2), '/System/Library/Frameworks/libavcodec.framework/libavcodec' (errno=2, not in dyld cache)
Library not loaded: @rpath/libavcodec.framework/libavcodec
 Referenced from: <3500F5CF-B1D2-30EC-8D7F-1C29BD45D05E> /private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Runner
 Reason: tried: '/usr/lib/swift/libavcodec.framework/libavcodec' (errno=2, not in dyld cache), '/private/preboot/Cryptexes/OS/usr/lib/swift/libavcodec.framework/libavcodec' (errno=2), '/usr/lib/swift/libavcodec.framework/libavcodec' (errno=2, not in dyld cache), '/private/preboot/Cryptexes/OS/usr/lib/swift/libavcodec.framework/libavcodec' (errno=2), '/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec' (errno=2), '/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec' (errno=2), '/private/var/containers/Bundle/Application/984D87E5-818C-49A9-9CB5-F0CC3160D2FF/Runner.app/Frameworks/libavcodec.framework/libavcodec' (errno=2), '/usr/lib/swif
dyld config: DYLD_LIBRARY_PATH=/usr/lib/system/introspection DYLD_INSERT_LIBRARIES=/usr/lib/libBacktraceRecording.dylib:/usr/lib/libMainThreadChecker.dylib:/usr/lib/libRPAC.dylib:/Developer/Library/PrivateFrameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib
(lldb) 



The pod is as follows :


# Uncomment this line to define a global platform for your project
platform :ios, '14.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'

project 'Runner', {
 'Debug' => :debug,
 'Profile' => :release,
 'Release' => :release,
}

def flutter_root
 generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
 unless File.exist?(generated_xcode_build_settings_path)
 raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
 end

 File.foreach(generated_xcode_build_settings_path) do |line|
 matches = line.match(/FLUTTER_ROOT\=(.*)/)
 return matches[1].strip if matches
 end
 raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
end

require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)

flutter_ios_podfile_setup

target 'Runner' do
 use_frameworks!
 use_modular_headers!

 flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end

post_install do |installer|
 installer.pods_project.targets.each do |target|
 flutter_additional_ios_build_settings(target)
 end
end



The yaml :



environment:
 sdk: '>=2.18.2 <3.0.0'

# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
# consider running `flutter pub upgrade --major-versions`. Alternatively,
# dependencies can be manually updated by changing the version numbers below to
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
 flutter:
 sdk: flutter
 flutter_localizations:
 sdk: flutter
 # The following adds the Cupertino Icons font to your application.
 # Use with the CupertinoIcons class for iOS style icons.
 cupertino_icons: ^1.0.2
 video_editor: ^1.5.2
 image_picker: ^0.8.6
 helpers: ^1.2.0
 intl: ^0.17.0
 i18n_extension: ^5.0.1
 optimized_cached_image: ^3.0.1
 youtube_player_flutter: ^8.1.1
 flutter_launcher_icons: ^0.11.0
 flutter_lorem: ^2.0.0
 advance_image_picker: ^0.1.7+1
 wechat_assets_picker: ^8.1.4
 lecle_flutter_absolute_path: ^0.0.2+1
 #ffmpeg_kit_flutter: 5.1.0-LTS
 path_provider: ^2.0.11
 video_thumbnail: ^0.5.3
 flutter_document_picker: ^5.1.0
 flutter_login: ^4.1.1
 #flutter_absolute_path: ^1.0.6
 # flutter_absolute_path:
 # git:
 # url: https://github.com/ValeriusGC/flutter_absolute_path.git
 uuid: ^3.0.6
 flutter_form_builder: ^7.7.0
 form_builder_validators: ^8.4.0
 state_persistence: ^0.1.0
 shared_preferences: ^2.0.15
 firebase_core: ^2.4.0
 firebase_storage: ^11.0.8
 video_compress: ^3.1.2
 connectivity_plus: ^3.0.2
 internet_connection_checker: ^1.0.0+1
 cached_video_player: ^2.0.3
 visibility_detector: ^0.3.3
 firebase_database: ^10.0.7
 firebase_auth: ^4.2.1
 firebase_dynamic_links: ^5.0.9
 cloud_firestore: ^4.2.0
 cloud_functions: ^4.0.6
 cached_network_image: ^3.2.3
 ffmpeg_kit_flutter_min_gpl: ^5.1.0
 video_player: ^2.4.10
 provider: ^6.0.5
 camera: ^0.9.8+1
 share_plus: ^6.3.0
 package_info_plus: ^3.0.2

dependency_overrides:
 ffmpeg_kit_flutter_min_gpl: ^5.1.0-LTS

dev_dependencies:
 flutter_test:
 sdk: flutter


 # The "flutter_lints" package below contains a set of recommended lints to
 # encourage good coding practices. The lint set provided by the package is
 # activated in the `analysis_options.yaml` file located at the root of your
 # package. See that file for information about deactivating specific lint
 # rules and activating additional ones.
 flutter_lints: ^2.0.0

``



-
Node.js readable maximize throughput/performance for compute intense readable - Writable doesn't pull data fast enough
31 décembre 2022, par flohallGeneral setup


I developed an application using AWS Lambda node.js 14.
I use a custom
Readable
implementationFrameCreationStream
that uses node-canvas to draw images, svgs and more on a canvas. This result is then extracted as a raw image buffer in BGRA. A single image buffer contains 1920 * 1080 * 4 Bytes = 8294400 Bytes 8 MB.
This is then piped tostdin
of achild_process
runningffmpeg
.
ThehighWaterMark
of myReadable
inobjectMode:true
is set to 25 so that the internal buffer can use up to 8 MB * 25 = 200 MB.

All this works fine and also doesn't contain too much RAM. But I noticed after some time, that the performance is not ideally.


Performance not optimal


I have an example input that generates a video of 315 frames. If I set
highWaterMark
to a value above 25 the performance increases to the point, when I set to a value of 315 or above.

For some reason
ffmpeg
doesn't start to pull any data untilhighWaterMark
is reached. Obviously thats not what I want.ffmpeg
should always consume data if minimum 1 frame is cached in theReadable
and if it has finished processing the frame before. And theReadable
should produce more frames as longhighWaterMark
isn't reached or the last frame has been reached. So ideally theReadable
and theWriteable
are busy all the time.

I found another way to improve the speed. If I add a timeout in the
_read()
method of theReadable
after let's say every tenth frame for 100 ms. Then theffmpeg
-Writable
will use this timeout to write some frames toffmpeg
.

It seems like frames aren't passed to
ffmpeg
during frame creation because some node.js main thread is busy ?

The fastest result I have if I increase
highWaterMark
above the amount of frames - which doesn't work for longer videos as this would make the AWS Lambda RAM explode. And this makes the whole streaming idea useless. Using timeouts always gives me stomach pain. Also depending on the execution on different environments a good fitting timeout might differ. Any ideas ?

FrameCreationStream


import canvas from 'canvas';
import {Readable} from 'stream';
import {IMAGE_STREAM_BUFFER_SIZE, PerformanceUtil, RenderingLibraryError, VideoRendererInput} from 'vm-rendering-backend-commons';
import {AnimationAssets, BufferType, DrawingService, FullAnimationData} from 'vm-rendering-library';

/**
 * This is a proper back pressure compatible implementation of readable for a having a stream to read single frames from.
 * Whenever read() is called a new frame is created and added to the stream.
 * read() will be called internally until options.highWaterMark has been reached.
 * then calling read will be paused until one frame is read from the stream.
 */
export class FrameCreationStream extends Readable {

 drawingService: DrawingService;
 endFrameIndex: number;
 currentFrameIndex: number = 0;
 startFrameIndex: number;
 frameTimer: [number, number];
 readTimer: [number, number];
 fullAnimationData: FullAnimationData;

 constructor(animationAssets: AnimationAssets, fullAnimationData: FullAnimationData, videoRenderingInput: VideoRendererInput, frameTimer: [number, number]) {
 super({highWaterMark: IMAGE_STREAM_BUFFER_SIZE, objectMode: true});

 this.frameTimer = frameTimer;
 this.readTimer = PerformanceUtil.startTimer();

 this.fullAnimationData = fullAnimationData;

 this.startFrameIndex = Math.floor(videoRenderingInput.startFrameId);
 this.currentFrameIndex = this.startFrameIndex;
 this.endFrameIndex = Math.floor(videoRenderingInput.endFrameId);

 this.drawingService = new DrawingService(animationAssets, fullAnimationData, videoRenderingInput, canvas);
 console.time("read");
 }

 /**
 * this method is only overwritten for debugging
 * @param size
 */
 read(size?: number): string | Buffer {

 console.log("read("+size+")");
 const buffer = super.read(size);
 console.log(buffer);
 console.log(buffer?.length);
 if(buffer) {
 console.timeLog("read");
 }
 return buffer;
 }

 // _read() will be called when the stream wants to pull more data in.
 // _read() will be called again after each call to this.push(dataChunk) once the stream is ready to accept more data. https://nodejs.org/api/stream.html#readable_readsize
 // this way it is ensured, that even though this.createImageBuffer() is async, only one frame is created at a time and the order is kept
 _read(): void {
 // as frame numbers are consecutive and unique, we have to draw each frame number (also the first and the last one)
 if (this.currentFrameIndex <= this.endFrameIndex) {
 PerformanceUtil.logTimer(this.readTimer, 'WAIT -> READ\t');
 this.createImageBuffer()
 .then(buffer => this.optionalTimeout(buffer))
 // push means adding a buffered raw frame to the stream
 .then((buffer: Buffer) => {
 this.readTimer = PerformanceUtil.startTimer();
 // the following two frame numbers start with 1 as first value
 const processedFrameNumberOfScene = 1 + this.currentFrameIndex - this.startFrameIndex;
 const totalFrameNumberOfScene = 1 + this.endFrameIndex - this.startFrameIndex;
 // the overall frameId or frameIndex starts with frameId 0
 const processedFrameIndex = this.currentFrameIndex;
 this.currentFrameIndex++;
 this.push(buffer); // nothing besides logging should happen after calling this.push(buffer)
 console.log(processedFrameNumberOfScene + ' of ' + totalFrameNumberOfScene + ' processed - full video frameId: ' + processedFrameIndex + ' - buffered frames: ' + this.readableLength);
 })
 .catch(err => {
 // errors will be finally handled, when subscribing to frameCreation stream in ffmpeg service
 // this log is just generated for tracing errors and if for some reason the handling in ffmpeg service doesn't work
 console.log("createImageBuffer: ", err);
 this.emit("error", err);
 });
 } else {
 // push(null) makes clear that this stream has ended
 this.push(null);
 PerformanceUtil.logTimer(this.frameTimer, 'FRAME_STREAM');
 }
 }

 private optionalTimeout(buffer: Buffer): Promise<buffer> {
 if(this.currentFrameIndex % 10 === 0) {
 return new Promise(resolve => setTimeout(() => resolve(buffer), 140));
 }
 return Promise.resolve(buffer);
 }

 // prevent memory leaks - without this lambda memory will increase with every call
 _destroy(): void {
 this.drawingService.destroyStage();
 }

 /**
 * This creates a raw pixel buffer that contains a single frame of the video drawn by the rendering library
 *
 */
 public async createImageBuffer(): Promise<buffer> {

 const drawTimer = PerformanceUtil.startTimer();
 try {
 await this.drawingService.drawForFrame(this.currentFrameIndex);
 } catch (err: any) {
 throw new RenderingLibraryError(err);
 }

 PerformanceUtil.logTimer(drawTimer, 'DRAW -> FRAME\t');

 const bufferTimer = PerformanceUtil.startTimer();
 // Creates a raw pixel buffer, containing simple binary data
 // the exact same information (BGRA/screen ratio) has to be provided to ffmpeg, because ffmpeg cannot detect format for raw input
 const buffer = await this.drawingService.toBuffer(BufferType.RAW);
 PerformanceUtil.logTimer(bufferTimer, 'CANVAS -> BUFFER');

 return buffer;
 }
}
</buffer></buffer>


FfmpegService


import {ChildProcess, execFile} from 'child_process';
import {Readable} from 'stream';
import {FPS, StageSize} from 'vm-rendering-library';
import {
 FfmpegError,
 LOCAL_MERGE_VIDEOS_TEXT_FILE, LOCAL_SOUND_FILE_PATH,
 LOCAL_VIDEO_FILE_PATH,
 LOCAL_VIDEO_SOUNDLESS_MERGE_FILE_PATH
} from "vm-rendering-backend-commons";

/**
 * This class bundles all ffmpeg usages for rendering one scene.
 * FFmpeg is a console program which can transcode nearly all types of sounds, images and videos from one to another.
 */
export class FfmpegService {

 ffmpegPath: string = null;


 constructor(ffmpegPath: string) {
 this.ffmpegPath = ffmpegPath;
 }

 /**
 * Convert a stream of raw images into an .mp4 video using the command line program ffmpeg.
 *
 * @param inputStream an input stream containing images in raw format BGRA
 * @param stageSize the size of a single frame in pixels (minimum is 2*2)
 * @param outputPath the filepath to write the resulting video to
 */
 public imageToVideo(inputStream: Readable, stageSize: StageSize, outputPath: string): Promise<void> {
 const args: string[] = [
 '-f',
 'rawvideo',
 '-r',
 `${FPS}`,
 '-pix_fmt',
 'bgra',
 '-s',
 `${stageSize.width}x${stageSize.height}`,
 '-i',
 // input "-" means input will be passed via pipe (streamed)
 '-',
 // codec that also QuickTime player can understand
 '-vcodec',
 'libx264',
 '-pix_fmt',
 'yuv420p',
 /*
 * "-movflags faststart":
 * metadata at beginning of file
 * needs more RAM
 * file will be broken, if not finished properly
 * higher application compatibility
 * better for browser streaming
 */
 '-movflags',
 'faststart',
 // "-preset ultrafast", //use this to speed up compression, but quality/compression ratio gets worse
 // don't overwrite an existing file here,
 // but delete file in the beginning of execution index.ts
 // (this is better for local testing believe me)
 outputPath
 ];

 return this.execFfmpegPromise(args, inputStream);
 }

 private execFfmpegPromise(args: string[], inputStream?: Readable): Promise<void> {
 const ffmpegServiceSelf = this;
 return new Promise(function (resolve, reject) {
 const executionProcess: ChildProcess = execFile(ffmpegServiceSelf.ffmpegPath, args, (err) => {
 if (err) {
 reject(new FfmpegError(err));
 } else {
 console.log('ffmpeg finished');
 resolve();
 }
 });
 if (inputStream) {
 // it's important to listen on errors of input stream before piping it into the write stream
 // if we don't do this here, we get an unhandled promise exception for every issue in the input stream
 inputStream.on("error", err => {
 reject(err);
 });
 // don't reject promise here as the error will also be thrown inside execFile and will contain more debugging info
 // this log is just generated for tracing errors and if for some reason the handling in execFile doesn't work
 inputStream.pipe(executionProcess.stdin).on("error", err => console.log("pipe stream: " , err));
 }
 });
 }
}
</void></void>