From 5d690b47c66b279eda109cde0c382e081289d90d Mon Sep 17 00:00:00 2001 From: igorshevach Date: Mon, 9 Sep 2024 00:45:14 +0300 Subject: [PATCH 01/27] handle media_info change. in case media_info is not 'bit exact': - flush media pipeline - ack last received frame id so that downstream not attempt to resend the previous media. - send eos to packager - exit --- transcoder/common/json_parser.h | 2 +- transcoder/debug/file_streamer.c | 34 ++++++++++++++++--- transcoder/receiver_server.c | 19 ++++++++++- transcoder/transcode/transcode_session.c | 12 ++++--- .../transcode/transcode_session_output.c | 11 +++++- 5 files changed, 67 insertions(+), 11 deletions(-) diff --git a/transcoder/common/json_parser.h b/transcoder/common/json_parser.h index 71f12805..0720d491 100644 --- a/transcoder/common/json_parser.h +++ b/transcoder/common/json_parser.h @@ -116,7 +116,7 @@ typedef struct { #define JSON_SERIALIZE_STRING(key,value) ADD_COMMA() JSON_WRITE("\"%s\": \"%s\"",key,value); js->shouldAddComma=true; #define JSON_SERIALIZE_INT(key,value) ADD_COMMA() JSON_WRITE("\"%s\": %d",key,value); js->shouldAddComma=true; -#define JSON_SERIALIZE_INT64(key,value) ADD_COMMA() JSON_WRITE("\"%s\": %lld",key,value); js->shouldAddComma=true; +#define JSON_SERIALIZE_INT64(key,value) ADD_COMMA() JSON_WRITE("\"%s\": %ld",key,value); js->shouldAddComma=true; #define JSON_SERIALIZE_BOOL(key,value) ADD_COMMA() JSON_WRITE("\"%s\": %s",key,value ? "true" : "false"); js->shouldAddComma=true; #define JSON_SERIALIZE_DOUBLE(key,value) ADD_COMMA() JSON_WRITE("\"%s\": %.2lf",key,value); js->shouldAddComma=true; diff --git a/transcoder/debug/file_streamer.c b/transcoder/debug/file_streamer.c index f9ec8232..8b3471f9 100644 --- a/transcoder/debug/file_streamer.c +++ b/transcoder/debug/file_streamer.c @@ -57,6 +57,10 @@ void* thread_stream_from_file(void *vargp) json_get_int64(GetConfig(),"input.hiccupIntervalSec",0,&hiccupIntervalSec); + int64_t resendMediaInfoIntervalSec; + json_get_int64(GetConfig(),"input.resendMediaInfoIntervalSec",10,&resendMediaInfoIntervalSec); + + AVPacket packet; av_init_packet(&packet); @@ -94,7 +98,11 @@ void* thread_stream_from_file(void *vargp) int64_t start_time=av_gettime_relative(), hiccup_duration = hiccupDurationSec * 1000 * 1000, hiccup_interval = hiccupIntervalSec * 1000 * 1000, - next_hiccup = start_time + hiccup_interval; + next_hiccup = start_time + hiccup_interval, + resendMediaInfoInterval = resendMediaInfoIntervalSec * 1000 * 1000, + nextSentMediaInfoTime = av_gettime_relative() + resendMediaInfoInterval; + + uint64_t frame_id_ack = createTime - 1; samples_stats_t stats; sample_stats_init(&stats,standard_timebase); @@ -173,6 +181,23 @@ void* thread_stream_from_file(void *vargp) } } + if(resendMediaInfoIntervalSec > 0 + && (packet.flags & AV_PKT_FLAG_KEY) + && nextSentMediaInfoTime < av_gettime_relative()) + { + LOGGER0(CATEGORY_RECEIVER,AV_LOG_WARNING,"sending mediainfo"); + if (KMP_send_mediainfo(&kmp,&extra)<0) { + LOGGER0(CATEGORY_RECEIVER,AV_LOG_FATAL,"couldn't send mediainfo!"); + break; + } + + if (KMP_read_ack(&kmp,&frame_id_ack)) { + LOGGER(CATEGORY_RECEIVER,AV_LOG_DEBUG,"received ack for packet id %lld",frame_id_ack); + } + + nextSentMediaInfoTime = av_gettime_relative() + resendMediaInfoInterval; + } + lastDts=packet.dts; samples_stats_add(&stats,packet.dts,packet.pos,packet.size); @@ -188,20 +213,21 @@ void* thread_stream_from_file(void *vargp) rate)*/ - uint64_t frame_id_ack; + if (KMP_send_packet(&kmp,&packet)<0) { LOGGER0(CATEGORY_RECEIVER,AV_LOG_FATAL,"couldn't send packet!"); break; } + frame_id++; if (KMP_read_ack(&kmp,&frame_id_ack)) { LOGGER(CATEGORY_RECEIVER,AV_LOG_DEBUG,"received ack for packet id %lld",frame_id_ack); } - LOGGER("SENDER",AV_LOG_DEBUG,"sent packet pts=%s dts=%s size=%d", + LOGGER("SENDER",AV_LOG_DEBUG,"sent packet pts=%s dts=%s size=%d frame_id=%lld frame_id_ack=%lld", ts2str(packet.pts,true), ts2str(packet.dts,true), - packet.size); + packet.size, frame_id, frame_id_ack); av_packet_unref(&packet); diff --git a/transcoder/receiver_server.c b/transcoder/receiver_server.c index 2800f76b..6e6fc67e 100644 --- a/transcoder/receiver_server.c +++ b/transcoder/receiver_server.c @@ -109,7 +109,24 @@ int clientLoop(receiver_server_t *server,receiver_server_session_t *session,tran } LOGGER(CATEGORY_RECEIVER,AV_LOG_INFO,"[%s] received packet KMP_PACKET_MEDIA_INFO",session->stream_name); - transcode_session_async_set_mediaInfo(transcode_session, newParams); + if( (retVal = transcode_session_async_set_mediaInfo(transcode_session, newParams)) < 0) + { + LOGGER(CATEGORY_RECEIVER,AV_LOG_ERROR,"[%s] transcode_session_async_set_mediaInfo failed",session->stream_name); + + LOGGER(CATEGORY_RECEIVER,AV_LOG_INFO,"[%s] flushing",session->stream_name); + _S(transcode_session_async_send_packet(transcode_session, NULL)); + + if(!autoAckMode) { + transcode_session_get_ack_frame_id(transcode_session,¤t_position); + // TODO: we cheat here, received_frame_id may have not been persisted yet + // so we should make sure all of the frames are consumed ny upstream + current_position.frame_id = current_position.transcoded_frame_id = received_frame_id; + LOGGER(CATEGORY_RECEIVER,AV_LOG_INFO,"[%s] sending ack for packet # : %lld",session->stream_name,current_position.frame_id); + _S(KMP_send_ack(&session->kmpClient,¤t_position)); + } + + break; + } } if (header.packet_type==KMP_PACKET_FRAME) { diff --git a/transcoder/transcode/transcode_session.c b/transcoder/transcode/transcode_session.c index ff4aeb22..f781d485 100644 --- a/transcoder/transcode/transcode_session.c +++ b/transcoder/transcode/transcode_session.c @@ -85,8 +85,7 @@ int transcode_session_async_set_mediaInfo(transcode_session_t *ctx,transcode_med return transcode_session_set_media_info(ctx,mediaInfo); } LOGGER(CATEGORY_TRANSCODING_SESSION,AV_LOG_DEBUG,"[%s] enqueue media info",ctx->name); - packet_queue_write_mediaInfo(&ctx->packetQueue, mediaInfo); - return 0; + return packet_queue_write_mediaInfo(&ctx->packetQueue, mediaInfo); } int transcode_session_async_send_packet(transcode_session_t *ctx, struct AVPacket* packet) @@ -121,8 +120,9 @@ int transcode_session_set_media_info(transcode_session_t *ctx,transcode_mediaInf if (currentCodecParams->extradata_size>0 && newCodecParams->extradata!=NULL && - currentCodecParams->extradata!=NULL && - 0!=memcmp(newCodecParams->extradata,currentCodecParams->extradata,currentCodecParams->extradata_size)) + currentCodecParams->extradata!=NULL + // FIXME: uncomment memcp!!! + /*&& 0!=memcmp(newCodecParams->extradata,currentCodecParams->extradata,currentCodecParams->extradata_size)*/) changed=true; if (!changed) { @@ -130,6 +130,10 @@ int transcode_session_set_media_info(transcode_session_t *ctx,transcode_mediaInf avcodec_parameters_free(&newMediaInfo->codecParams); av_free(newMediaInfo); return 0; + } else { + + LOGGER0(CATEGORY_TRANSCODING_SESSION,AV_LOG_ERROR,"changing media info on the fly is currently not supported"); + return -1; } } diff --git a/transcoder/transcode/transcode_session_output.c b/transcoder/transcode/transcode_session_output.c index 47c903ff..5066cdf4 100644 --- a/transcoder/transcode/transcode_session_output.c +++ b/transcoder/transcode/transcode_session_output.c @@ -130,8 +130,17 @@ int transcode_session_output_send_output_packet(transcode_session_output_t *pOut } av_write_frame(pOutput->oc, NULL); - av_packet_free(&cpPacket); + ack_desc_t desc = {0}; + if( (ret = get_packet_frame_id(cpPacket,&desc.id)) >= 0) { + if(pOutput->acker.ctx){ + pOutput->acker.map(&pOutput->acker,desc.id,&desc); + } + pOutput->lastMappedAck = desc.id; + pOutput->lastAck=desc.id; + pOutput->lastOffset=desc.offset; + } + av_packet_free(&cpPacket); } if (pOutput->sender!=NULL) From d541c5409324bd7fb0cf0b594a0cddbf977bff6b Mon Sep 17 00:00:00 2001 From: igorshevach Date: Mon, 9 Sep 2024 12:49:00 +0300 Subject: [PATCH 02/27] check for media info self assignment --- transcoder/transcode/transcode_session.c | 27 +++++++++++++----------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/transcoder/transcode/transcode_session.c b/transcoder/transcode/transcode_session.c index f781d485..33aa6822 100644 --- a/transcoder/transcode/transcode_session.c +++ b/transcoder/transcode/transcode_session.c @@ -112,18 +112,21 @@ void transcode_session_get_ack_frame_id(transcode_session_t *ctx,kmp_frame_posit int transcode_session_set_media_info(transcode_session_t *ctx,transcode_mediaInfo_t* newMediaInfo) { if (ctx->currentMediaInfo) { - AVCodecParameters *currentCodecParams=ctx->currentMediaInfo->codecParams; - AVCodecParameters *newCodecParams=newMediaInfo->codecParams; - bool changed=newCodecParams->width!=currentCodecParams->width || - newCodecParams->height!=currentCodecParams->height || - newCodecParams->extradata_size!=currentCodecParams->extradata_size; - - if (currentCodecParams->extradata_size>0 && - newCodecParams->extradata!=NULL && - currentCodecParams->extradata!=NULL - // FIXME: uncomment memcp!!! - /*&& 0!=memcmp(newCodecParams->extradata,currentCodecParams->extradata,currentCodecParams->extradata_size)*/) - changed=true; + bool changed = false; + if(ctx->currentMediaInfo != newMediaInfo) { + AVCodecParameters *currentCodecParams=ctx->currentMediaInfo->codecParams; + AVCodecParameters *newCodecParams=newMediaInfo->codecParams; + changed=newCodecParams->width!=currentCodecParams->width || + newCodecParams->height!=currentCodecParams->height || + newCodecParams->extradata_size!=currentCodecParams->extradata_size; + + if (currentCodecParams->extradata_size>0 && + newCodecParams->extradata!=NULL && + currentCodecParams->extradata!=NULL + // FIXME: uncomment memcp!!! + /*&& 0!=memcmp(newCodecParams->extradata,currentCodecParams->extradata,currentCodecParams->extradata_size)*/) + changed=true; + } if (!changed) { From ec4a5dd9423eb6a95415441b22f455f2d534486f Mon Sep 17 00:00:00 2001 From: igorshevach Date: Mon, 9 Sep 2024 17:30:12 +0300 Subject: [PATCH 03/27] simulate codec change every X sec --- transcoder/transcode/transcode_session.c | 1 + 1 file changed, 1 insertion(+) diff --git a/transcoder/transcode/transcode_session.c b/transcoder/transcode/transcode_session.c index 33aa6822..31a572c5 100644 --- a/transcoder/transcode/transcode_session.c +++ b/transcoder/transcode/transcode_session.c @@ -124,6 +124,7 @@ int transcode_session_set_media_info(transcode_session_t *ctx,transcode_mediaInf newCodecParams->extradata!=NULL && currentCodecParams->extradata!=NULL // FIXME: uncomment memcp!!! + && ctx->processedStats.totalFrames > 0 /*&& 0!=memcmp(newCodecParams->extradata,currentCodecParams->extradata,currentCodecParams->extradata_size)*/) changed=true; } From 5a2946a708815a461dd80ff3ecf6dd77df82f897 Mon Sep 17 00:00:00 2001 From: igorshevach Date: Tue, 10 Sep 2024 18:42:13 +0300 Subject: [PATCH 04/27] KMP_PACKET_MEDIA_INFO: dump media info (including extradata in hex format) --- transcoder/KMP/KMP.c | 64 ++++++++++++++++++++++++ transcoder/KMP/KMP.h | 2 + transcoder/receiver_server.c | 3 +- transcoder/transcode/transcode_session.c | 4 +- 4 files changed, 71 insertions(+), 2 deletions(-) diff --git a/transcoder/KMP/KMP.c b/transcoder/KMP/KMP.c index 0f8f434c..ab6fda87 100644 --- a/transcoder/KMP/KMP.c +++ b/transcoder/KMP/KMP.c @@ -781,3 +781,67 @@ bool KMP_read_ack(KMP_session_t *context,uint64_t* frame_id) } return false; } + +static const char *hex = "0123456789ABCDEF"; + +int KMP_log_mediainfo(KMP_session_t *context, + const char *category, int level, + transcode_mediaInfo_t* transcodeMediaInfo) { + + AVCodecParameters* params = transcodeMediaInfo->codecParams; + + if(!params){ + return -1; + } + + char *ex = NULL; + if(params->extradata_size > 0 ){ + ex = av_malloc(2 * params->extradata_size + 1); + char *walk = ex; + for(int i = 0; i < params->extradata_size; i++) { + *walk++ = hex[params->extradata[i] >> 4]; + *walk++ = hex[params->extradata[i] & 0x0f]; + } + *walk = '\0'; + } + + if (params->codec_type == AVMEDIA_TYPE_AUDIO) { + + LOGGER(category,level,"[%s] KMP_PACKET_MEDIA_INFO type=audio" + " codec_id=%s (0x%x) samplerate=%d bps=%d channels=%d channel_layout=%d" + " bitrate=%.3f kbps timescale=%d:%d" + " extra_data=%s", + context->sessionName, + avcodec_get_name(params->codec_id), + params->codec_tag, + params->sample_rate, + params->bits_per_coded_sample, + params->channels, + params->channel_layout, + params->bit_rate / 1000.0, transcodeMediaInfo->timeScale.num, transcodeMediaInfo->timeScale.den, + ex); + } + else if (params->codec_type == AVMEDIA_TYPE_VIDEO) { + + LOGGER(category,level,"[%s] KMP_PACKET_MEDIA_INFO type=video" + " codec_id=%s (0x%x) width=%d height=%d frame_rate=%d:%d" + " bitrate=%.3f kbps timescale=%d:%d" + " cc=%s extra_data=%s", + context->sessionName, + avcodec_get_name(params->codec_id), + params->codec_tag, + params->width, + params->height, + transcodeMediaInfo->frameRate.num, transcodeMediaInfo->frameRate.den, + params->bit_rate / 1000.0, transcodeMediaInfo->timeScale.num, transcodeMediaInfo->timeScale.den, + transcodeMediaInfo->closed_captions ? "yes" : "no", + ex); + } + + if(ex) { + av_free(ex); + } + + return 0; +} + diff --git a/transcoder/KMP/KMP.h b/transcoder/KMP/KMP.h index fa58b826..2691fcfa 100644 --- a/transcoder/KMP/KMP.h +++ b/transcoder/KMP/KMP.h @@ -50,6 +50,8 @@ int KMP_send_ack( KMP_session_t *context,kmp_frame_position_t *cur_pos); int KMP_close( KMP_session_t *context); +int KMP_log_mediainfo(KMP_session_t *context, const char *category,int level, transcode_mediaInfo_t* mediaInfo); + int KMP_listen( KMP_session_t *context); int KMP_accept( KMP_session_t *context, KMP_session_t *client); diff --git a/transcoder/receiver_server.c b/transcoder/receiver_server.c index 6e6fc67e..37f66550 100644 --- a/transcoder/receiver_server.c +++ b/transcoder/receiver_server.c @@ -107,7 +107,8 @@ int clientLoop(receiver_server_t *server,receiver_server_session_t *session,tran LOGGER(CATEGORY_RECEIVER,AV_LOG_FATAL,"[%s] Invalid mediainfo",session->stream_name); break; } - LOGGER(CATEGORY_RECEIVER,AV_LOG_INFO,"[%s] received packet KMP_PACKET_MEDIA_INFO",session->stream_name); + + KMP_log_mediainfo(transcode_session, CATEGORY_RECEIVER, AV_LOG_INFO, newParams); if( (retVal = transcode_session_async_set_mediaInfo(transcode_session, newParams)) < 0) { diff --git a/transcoder/transcode/transcode_session.c b/transcoder/transcode/transcode_session.c index 31a572c5..3db48452 100644 --- a/transcoder/transcode/transcode_session.c +++ b/transcoder/transcode/transcode_session.c @@ -123,7 +123,7 @@ int transcode_session_set_media_info(transcode_session_t *ctx,transcode_mediaInf if (currentCodecParams->extradata_size>0 && newCodecParams->extradata!=NULL && currentCodecParams->extradata!=NULL - // FIXME: uncomment memcp!!! + // FIXME: uncomment memcmp!!! && ctx->processedStats.totalFrames > 0 /*&& 0!=memcmp(newCodecParams->extradata,currentCodecParams->extradata,currentCodecParams->extradata_size)*/) changed=true; @@ -137,6 +137,8 @@ int transcode_session_set_media_info(transcode_session_t *ctx,transcode_mediaInf } else { LOGGER0(CATEGORY_TRANSCODING_SESSION,AV_LOG_ERROR,"changing media info on the fly is currently not supported"); + avcodec_parameters_free(&newMediaInfo->codecParams); + av_free(newMediaInfo); return -1; } } From c5f12ec9de661a0128dbad213861136653a7e5e8 Mon Sep 17 00:00:00 2001 From: igorshevach Date: Tue, 10 Sep 2024 21:32:11 +0300 Subject: [PATCH 05/27] kbps = 1024 bps --- transcoder/KMP/KMP.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/transcoder/KMP/KMP.c b/transcoder/KMP/KMP.c index ab6fda87..3e2ca402 100644 --- a/transcoder/KMP/KMP.c +++ b/transcoder/KMP/KMP.c @@ -818,7 +818,7 @@ int KMP_log_mediainfo(KMP_session_t *context, params->bits_per_coded_sample, params->channels, params->channel_layout, - params->bit_rate / 1000.0, transcodeMediaInfo->timeScale.num, transcodeMediaInfo->timeScale.den, + params->bit_rate / 1024.0, transcodeMediaInfo->timeScale.num, transcodeMediaInfo->timeScale.den, ex); } else if (params->codec_type == AVMEDIA_TYPE_VIDEO) { @@ -833,7 +833,7 @@ int KMP_log_mediainfo(KMP_session_t *context, params->width, params->height, transcodeMediaInfo->frameRate.num, transcodeMediaInfo->frameRate.den, - params->bit_rate / 1000.0, transcodeMediaInfo->timeScale.num, transcodeMediaInfo->timeScale.den, + params->bit_rate / 1024.0, transcodeMediaInfo->timeScale.num, transcodeMediaInfo->timeScale.den, transcodeMediaInfo->closed_captions ? "yes" : "no", ex); } From 81e9e37fb99463795de029545ea0ef9be2c15e88 Mon Sep 17 00:00:00 2001 From: igorshevach Date: Tue, 10 Sep 2024 21:52:30 +0300 Subject: [PATCH 06/27] pass correct context to KMP_log_mediainfo --- transcoder/receiver_server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transcoder/receiver_server.c b/transcoder/receiver_server.c index 37f66550..6e01753e 100644 --- a/transcoder/receiver_server.c +++ b/transcoder/receiver_server.c @@ -108,7 +108,7 @@ int clientLoop(receiver_server_t *server,receiver_server_session_t *session,tran break; } - KMP_log_mediainfo(transcode_session, CATEGORY_RECEIVER, AV_LOG_INFO, newParams); + KMP_log_mediainfo(&session->kmpClient, CATEGORY_RECEIVER, AV_LOG_INFO, newParams); if( (retVal = transcode_session_async_set_mediaInfo(transcode_session, newParams)) < 0) { From 5a35aea3f27058fc63d196cefb366b446daaba67 Mon Sep 17 00:00:00 2001 From: igorshevach Date: Thu, 12 Sep 2024 10:44:07 +0300 Subject: [PATCH 07/27] pass correct context to KMP_log_mediainfo --- nginx-kmp-out-module/src/ngx_kmp_out_track.c | 58 +++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/nginx-kmp-out-module/src/ngx_kmp_out_track.c b/nginx-kmp-out-module/src/ngx_kmp_out_track.c index dd48d4e4..f9d3e9eb 100644 --- a/nginx-kmp-out-module/src/ngx_kmp_out_track.c +++ b/nginx-kmp-out-module/src/ngx_kmp_out_track.c @@ -1209,6 +1209,61 @@ ngx_kmp_out_track_write(ngx_kmp_out_track_t *track, u_char *data, return ngx_kmp_out_track_write_chain(track, &in, data); } +static ngx_int_t +ngx_kmp_out_track_log_media_info(ngx_kmp_out_track_t *track) +{ + u_char *buf = NULL, + *p; + + if(track->extra_data.len > 0) + { + buf = ngx_pnalloc(track->pool, 2 * track->extra_data.len + 1); + if(!buf) + { + ngx_log_error(NGX_LOG_ERR, &track->log, 0, + "ngx_kmp_out_track_log_media_info:" + "failed to allocate buffer for extra data size %uD", + 2 * track->extra_data.len + 1); + return NGX_ERROR; + } + + p = ngx_hex_dump(buf, track->extra_data.data, track->extra_data.len); + *p = 0; + } + + ngx_log_error(NGX_LOG_INFO, &track->log, 0, + "ngx_kmp_out_track_log_media_info:" + " media_type: %uD codec_id: %uD" + " timescale: %uD bitrate: %uD" + " extra_data: %s", + track->media_info.media_type, track->media_info.codec_id, + track->media_info.timescale, track->media_info.bitrate, buf); + switch(track->media_info.media_type) + { + case KMP_MEDIA_VIDEO: + ngx_log_error(NGX_LOG_INFO, &track->log, 0, + "ngx_kmp_out_track_log_media_info: video" + " width: %uD height: %uD frame_rate: %uD/%uD cea_captions: %s", + track->media_info.u.video.width, track->media_info.u.video.height, + track->media_info.u.video.frame_rate.num, track->media_info.u.video.frame_rate.denom, + track->media_info.u.video.cea_captions ? "true" : "false"); + break; + case KMP_MEDIA_AUDIO: + ngx_log_error(NGX_LOG_INFO, &track->log, 0, + "ngx_kmp_out_track_write_media_info: audio" + " channels: %uD bits_per_sample: %uD sample_rate: %uD channel_layout: %uD", + track->media_info.u.audio.channels, track->media_info.u.audio.bits_per_sample, + track->media_info.u.audio.sample_rate, track->media_info.u.audio.channel_layout); + break; + }; + + if(buf) + { + ngx_pfree(track->pool, buf); + } + + return NGX_OK; +} ngx_int_t ngx_kmp_out_track_write_media_info(ngx_kmp_out_track_t *track) @@ -1238,10 +1293,11 @@ ngx_kmp_out_track_write_media_info(ngx_kmp_out_track_t *track) return NGX_ERROR; } + ngx_kmp_out_track_log_media_info(track); + return NGX_OK; } - static void ngx_kmp_out_track_chain_md5_hex(u_char dst[32], ngx_chain_t *in, u_char *p) { From fbb7a5d90623fe66bd5d364afa3c28c48b14b42a Mon Sep 17 00:00:00 2001 From: igorshevach Date: Sun, 15 Sep 2024 16:48:37 +0300 Subject: [PATCH 08/27] transcoder - undo promiscuous handle media info --- transcoder/transcode/transcode_session.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/transcoder/transcode/transcode_session.c b/transcoder/transcode/transcode_session.c index 3db48452..ece33106 100644 --- a/transcoder/transcode/transcode_session.c +++ b/transcoder/transcode/transcode_session.c @@ -120,14 +120,11 @@ int transcode_session_set_media_info(transcode_session_t *ctx,transcode_mediaInf newCodecParams->height!=currentCodecParams->height || newCodecParams->extradata_size!=currentCodecParams->extradata_size; - if (currentCodecParams->extradata_size>0 && - newCodecParams->extradata!=NULL && - currentCodecParams->extradata!=NULL - // FIXME: uncomment memcmp!!! - && ctx->processedStats.totalFrames > 0 - /*&& 0!=memcmp(newCodecParams->extradata,currentCodecParams->extradata,currentCodecParams->extradata_size)*/) - changed=true; - } + if (currentCodecParams->extradata_size>0 && + newCodecParams->extradata!=NULL && + currentCodecParams->extradata!=NULL && + 0!=memcmp(newCodecParams->extradata,currentCodecParams->extradata,currentCodecParams->extradata_size)) + changed=true; if (!changed) { From 49235cbef55de77081dd5816dc5c4ef598ce90ea Mon Sep 17 00:00:00 2001 From: igorshevach Date: Sun, 15 Sep 2024 19:57:28 +0300 Subject: [PATCH 09/27] transcoder - undo promiscuous handle media info --- transcoder/transcode/transcode_session.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/transcoder/transcode/transcode_session.c b/transcoder/transcode/transcode_session.c index ece33106..9d75ea7c 100644 --- a/transcoder/transcode/transcode_session.c +++ b/transcoder/transcode/transcode_session.c @@ -112,13 +112,11 @@ void transcode_session_get_ack_frame_id(transcode_session_t *ctx,kmp_frame_posit int transcode_session_set_media_info(transcode_session_t *ctx,transcode_mediaInfo_t* newMediaInfo) { if (ctx->currentMediaInfo) { - bool changed = false; - if(ctx->currentMediaInfo != newMediaInfo) { - AVCodecParameters *currentCodecParams=ctx->currentMediaInfo->codecParams; - AVCodecParameters *newCodecParams=newMediaInfo->codecParams; - changed=newCodecParams->width!=currentCodecParams->width || - newCodecParams->height!=currentCodecParams->height || - newCodecParams->extradata_size!=currentCodecParams->extradata_size; + AVCodecParameters *currentCodecParams=ctx->currentMediaInfo->codecParams; + AVCodecParameters *newCodecParams=newMediaInfo->codecParams; + bool changed=newCodecParams->width!=currentCodecParams->width || + newCodecParams->height!=currentCodecParams->height || + newCodecParams->extradata_size!=currentCodecParams->extradata_size; if (currentCodecParams->extradata_size>0 && newCodecParams->extradata!=NULL && @@ -126,16 +124,14 @@ int transcode_session_set_media_info(transcode_session_t *ctx,transcode_mediaInf 0!=memcmp(newCodecParams->extradata,currentCodecParams->extradata,currentCodecParams->extradata_size)) changed=true; - if (!changed) { + avcodec_parameters_free(&newMediaInfo->codecParams); + av_free(newMediaInfo); - avcodec_parameters_free(&newMediaInfo->codecParams); - av_free(newMediaInfo); + if (!changed) { + LOGGER0(CATEGORY_TRANSCODING_SESSION,AV_LOG_INFO,"transcode_session_set_media_info. media info did not change"); return 0; } else { - - LOGGER0(CATEGORY_TRANSCODING_SESSION,AV_LOG_ERROR,"changing media info on the fly is currently not supported"); - avcodec_parameters_free(&newMediaInfo->codecParams); - av_free(newMediaInfo); + LOGGER0(CATEGORY_TRANSCODING_SESSION,AV_LOG_ERROR,"transcode_session_set_media_info, changing media info on the fly is currently not supported"); return -1; } } From 19c5a6c769fe8cb0d77db48554594a2ddf030d39 Mon Sep 17 00:00:00 2001 From: igorshevach Date: Sun, 15 Sep 2024 22:31:39 +0300 Subject: [PATCH 10/27] fix order of events called; we must have rtmp_codec first --- nginx-rtmp-module/src/ngx_rtmp_codec_module.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/nginx-rtmp-module/src/ngx_rtmp_codec_module.c b/nginx-rtmp-module/src/ngx_rtmp_codec_module.c index c4525dc7..5a71bef9 100644 --- a/nginx-rtmp-module/src/ngx_rtmp_codec_module.c +++ b/nginx-rtmp-module/src/ngx_rtmp_codec_module.c @@ -1456,6 +1456,7 @@ ngx_rtmp_codec_postconfiguration(ngx_conf_t *cf) ngx_rtmp_core_main_conf_t *cmcf; ngx_rtmp_handler_pt *h; ngx_rtmp_amf_handler_t *ch; + ngx_uint_t nelts; cmcf = ngx_rtmp_conf_get_module_main_conf(cf, ngx_rtmp_core_module); @@ -1464,6 +1465,11 @@ ngx_rtmp_codec_postconfiguration(ngx_conf_t *cf) return NGX_ERROR; } + nelts = cmcf->events[NGX_RTMP_MSG_AUDIO].nelts; + for(;nelts > 1;nelts--, h--) { + *h = h[-1]; + } + *h = ngx_rtmp_codec_av; h = ngx_array_push(&cmcf->events[NGX_RTMP_MSG_VIDEO]); @@ -1471,6 +1477,11 @@ ngx_rtmp_codec_postconfiguration(ngx_conf_t *cf) return NGX_ERROR; } + nelts = cmcf->events[NGX_RTMP_MSG_VIDEO].nelts; + for(;nelts > 1;nelts--, h--) { + *h = h[-1]; + } + *h = ngx_rtmp_codec_av; h = ngx_array_push(&cmcf->events[NGX_RTMP_DISCONNECT]); From 37b1f5d24c446875e1dfb9e4fb8089b7729823cb Mon Sep 17 00:00:00 2001 From: igorshevach Date: Sun, 15 Sep 2024 23:21:19 +0300 Subject: [PATCH 11/27] attempt to force rtmp not to send old media info --- transcoder/receiver_server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transcoder/receiver_server.c b/transcoder/receiver_server.c index 6e01753e..3cf4e86d 100644 --- a/transcoder/receiver_server.c +++ b/transcoder/receiver_server.c @@ -121,7 +121,7 @@ int clientLoop(receiver_server_t *server,receiver_server_session_t *session,tran transcode_session_get_ack_frame_id(transcode_session,¤t_position); // TODO: we cheat here, received_frame_id may have not been persisted yet // so we should make sure all of the frames are consumed ny upstream - current_position.frame_id = current_position.transcoded_frame_id = received_frame_id; + current_position.frame_id = current_position.transcoded_frame_id = received_frame_id + 1; LOGGER(CATEGORY_RECEIVER,AV_LOG_INFO,"[%s] sending ack for packet # : %lld",session->stream_name,current_position.frame_id); _S(KMP_send_ack(&session->kmpClient,¤t_position)); } From abe566d119974d3ac287912a752894254c550aea Mon Sep 17 00:00:00 2001 From: igorshevach Date: Sun, 15 Sep 2024 23:49:37 +0300 Subject: [PATCH 12/27] attempt to force rtmp not to send old media info --- transcoder/receiver_server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transcoder/receiver_server.c b/transcoder/receiver_server.c index 3cf4e86d..6e01753e 100644 --- a/transcoder/receiver_server.c +++ b/transcoder/receiver_server.c @@ -121,7 +121,7 @@ int clientLoop(receiver_server_t *server,receiver_server_session_t *session,tran transcode_session_get_ack_frame_id(transcode_session,¤t_position); // TODO: we cheat here, received_frame_id may have not been persisted yet // so we should make sure all of the frames are consumed ny upstream - current_position.frame_id = current_position.transcoded_frame_id = received_frame_id + 1; + current_position.frame_id = current_position.transcoded_frame_id = received_frame_id; LOGGER(CATEGORY_RECEIVER,AV_LOG_INFO,"[%s] sending ack for packet # : %lld",session->stream_name,current_position.frame_id); _S(KMP_send_ack(&session->kmpClient,¤t_position)); } From c0d38484d8bdd0e8f262f0561e7f4fdd0fcace88 Mon Sep 17 00:00:00 2001 From: igorshevach Date: Sun, 15 Sep 2024 23:50:03 +0300 Subject: [PATCH 13/27] undo --- transcoder/receiver_server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transcoder/receiver_server.c b/transcoder/receiver_server.c index 6e01753e..94fc3870 100644 --- a/transcoder/receiver_server.c +++ b/transcoder/receiver_server.c @@ -121,7 +121,7 @@ int clientLoop(receiver_server_t *server,receiver_server_session_t *session,tran transcode_session_get_ack_frame_id(transcode_session,¤t_position); // TODO: we cheat here, received_frame_id may have not been persisted yet // so we should make sure all of the frames are consumed ny upstream - current_position.frame_id = current_position.transcoded_frame_id = received_frame_id; + current_position.frame_id = current_position.transcoded_frame_id = received_frame_id; LOGGER(CATEGORY_RECEIVER,AV_LOG_INFO,"[%s] sending ack for packet # : %lld",session->stream_name,current_position.frame_id); _S(KMP_send_ack(&session->kmpClient,¤t_position)); } From d34bebe8c364700f378d68505454b1f4cc2a5f36 Mon Sep 17 00:00:00 2001 From: igorshevach Date: Sun, 15 Sep 2024 23:50:29 +0300 Subject: [PATCH 14/27] undo --- transcoder/receiver_server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transcoder/receiver_server.c b/transcoder/receiver_server.c index 94fc3870..6e01753e 100644 --- a/transcoder/receiver_server.c +++ b/transcoder/receiver_server.c @@ -121,7 +121,7 @@ int clientLoop(receiver_server_t *server,receiver_server_session_t *session,tran transcode_session_get_ack_frame_id(transcode_session,¤t_position); // TODO: we cheat here, received_frame_id may have not been persisted yet // so we should make sure all of the frames are consumed ny upstream - current_position.frame_id = current_position.transcoded_frame_id = received_frame_id; + current_position.frame_id = current_position.transcoded_frame_id = received_frame_id; LOGGER(CATEGORY_RECEIVER,AV_LOG_INFO,"[%s] sending ack for packet # : %lld",session->stream_name,current_position.frame_id); _S(KMP_send_ack(&session->kmpClient,¤t_position)); } From a55e6416d72ab106e0637c3cacba7fdba9b08ef3 Mon Sep 17 00:00:00 2001 From: igorshevach Date: Mon, 16 Sep 2024 17:05:47 +0300 Subject: [PATCH 15/27] send ack for media info (?) --- transcoder/receiver_server.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/transcoder/receiver_server.c b/transcoder/receiver_server.c index 6e01753e..79ebadcd 100644 --- a/transcoder/receiver_server.c +++ b/transcoder/receiver_server.c @@ -110,8 +110,7 @@ int clientLoop(receiver_server_t *server,receiver_server_session_t *session,tran KMP_log_mediainfo(&session->kmpClient, CATEGORY_RECEIVER, AV_LOG_INFO, newParams); - if( (retVal = transcode_session_async_set_mediaInfo(transcode_session, newParams)) < 0) - { + if( (retVal = transcode_session_async_set_mediaInfo(transcode_session, newParams)) < 0) { LOGGER(CATEGORY_RECEIVER,AV_LOG_ERROR,"[%s] transcode_session_async_set_mediaInfo failed",session->stream_name); LOGGER(CATEGORY_RECEIVER,AV_LOG_INFO,"[%s] flushing",session->stream_name); @@ -125,10 +124,16 @@ int clientLoop(receiver_server_t *server,receiver_server_session_t *session,tran LOGGER(CATEGORY_RECEIVER,AV_LOG_INFO,"[%s] sending ack for packet # : %lld",session->stream_name,current_position.frame_id); _S(KMP_send_ack(&session->kmpClient,¤t_position)); } - break; + } else if(!autoAckMode) { + received_frame_id++; + kmp_frame_position_t media_info_position = { received_frame_id, received_frame_id, 0 }; + LOGGER(CATEGORY_RECEIVER,AV_LOG_INFO,"[%s] sending ack for media packet # : %lld", + session->stream_name, media_info_position.frame_id); + _S(KMP_send_ack(&session->kmpClient,&media_info_position)); } - } + + } if (header.packet_type==KMP_PACKET_FRAME) { AVPacket* packet=av_packet_alloc(); From bab813b971dad4d26f0113c9e0587c85e462c01d Mon Sep 17 00:00:00 2001 From: igorshevach Date: Mon, 16 Sep 2024 17:42:18 +0300 Subject: [PATCH 16/27] undo --- transcoder/receiver_server.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/transcoder/receiver_server.c b/transcoder/receiver_server.c index 79ebadcd..d24f6c72 100644 --- a/transcoder/receiver_server.c +++ b/transcoder/receiver_server.c @@ -125,14 +125,7 @@ int clientLoop(receiver_server_t *server,receiver_server_session_t *session,tran _S(KMP_send_ack(&session->kmpClient,¤t_position)); } break; - } else if(!autoAckMode) { - received_frame_id++; - kmp_frame_position_t media_info_position = { received_frame_id, received_frame_id, 0 }; - LOGGER(CATEGORY_RECEIVER,AV_LOG_INFO,"[%s] sending ack for media packet # : %lld", - session->stream_name, media_info_position.frame_id); - _S(KMP_send_ack(&session->kmpClient,&media_info_position)); } - } if (header.packet_type==KMP_PACKET_FRAME) { From 8a11b5c50ff56f7f9f3fcd6a025357c5724f7a23 Mon Sep 17 00:00:00 2001 From: igorshevach Date: Mon, 16 Sep 2024 18:20:57 +0300 Subject: [PATCH 17/27] - fix warnings - workaround downstream sending multiple media info after transcoder reconnection --- transcoder/common/json_parser.h | 4 +- transcoder/transcode/transcode_session.c | 92 +++++++++++++++++------- 2 files changed, 67 insertions(+), 29 deletions(-) diff --git a/transcoder/common/json_parser.h b/transcoder/common/json_parser.h index 0720d491..cb17a943 100644 --- a/transcoder/common/json_parser.h +++ b/transcoder/common/json_parser.h @@ -115,8 +115,8 @@ typedef struct { #define JSON_SERIALIZE_ARRAY_END() JSON_WRITE("]"); js->shouldAddComma=true; #define JSON_SERIALIZE_STRING(key,value) ADD_COMMA() JSON_WRITE("\"%s\": \"%s\"",key,value); js->shouldAddComma=true; -#define JSON_SERIALIZE_INT(key,value) ADD_COMMA() JSON_WRITE("\"%s\": %d",key,value); js->shouldAddComma=true; -#define JSON_SERIALIZE_INT64(key,value) ADD_COMMA() JSON_WRITE("\"%s\": %ld",key,value); js->shouldAddComma=true; +#define JSON_SERIALIZE_INT(key,value) ADD_COMMA() JSON_WRITE("\"%s\": %d",key,(int)value); js->shouldAddComma=true; +#define JSON_SERIALIZE_INT64(key,value) ADD_COMMA() JSON_WRITE("\"%s\": %ld",key,(int64_t)value); js->shouldAddComma=true; #define JSON_SERIALIZE_BOOL(key,value) ADD_COMMA() JSON_WRITE("\"%s\": %s",key,value ? "true" : "false"); js->shouldAddComma=true; #define JSON_SERIALIZE_DOUBLE(key,value) ADD_COMMA() JSON_WRITE("\"%s\": %.2lf",key,value); js->shouldAddComma=true; diff --git a/transcoder/transcode/transcode_session.c b/transcoder/transcode/transcode_session.c index 9d75ea7c..a7dce14a 100644 --- a/transcoder/transcode/transcode_session.c +++ b/transcoder/transcode/transcode_session.c @@ -109,37 +109,22 @@ void transcode_session_get_ack_frame_id(transcode_session_t *ctx,kmp_frame_posit } } -int transcode_session_set_media_info(transcode_session_t *ctx,transcode_mediaInfo_t* newMediaInfo) -{ - if (ctx->currentMediaInfo) { - AVCodecParameters *currentCodecParams=ctx->currentMediaInfo->codecParams; - AVCodecParameters *newCodecParams=newMediaInfo->codecParams; - bool changed=newCodecParams->width!=currentCodecParams->width || - newCodecParams->height!=currentCodecParams->height || - newCodecParams->extradata_size!=currentCodecParams->extradata_size; - - if (currentCodecParams->extradata_size>0 && - newCodecParams->extradata!=NULL && - currentCodecParams->extradata!=NULL && - 0!=memcmp(newCodecParams->extradata,currentCodecParams->extradata,currentCodecParams->extradata_size)) - changed=true; - - avcodec_parameters_free(&newMediaInfo->codecParams); - av_free(newMediaInfo); - - if (!changed) { - LOGGER0(CATEGORY_TRANSCODING_SESSION,AV_LOG_INFO,"transcode_session_set_media_info. media info did not change"); - return 0; - } else { - LOGGER0(CATEGORY_TRANSCODING_SESSION,AV_LOG_ERROR,"transcode_session_set_media_info, changing media info on the fly is currently not supported"); - return -1; - } +static +int transcode_session_init_pipeline(transcode_session_t *ctx) { + if (!ctx->currentMediaInfo) { + LOGGER0(CATEGORY_TRANSCODING_SESSION,AV_LOG_ERROR,"transcode_session_init_pipeline " + "media info not set"); + return -1; } - ctx->currentMediaInfo=newMediaInfo; + if (ctx->decoders > 0) { + LOGGER0(CATEGORY_TRANSCODING_SESSION,AV_LOG_ERROR, "transcode_session_init_pipeline " + "decoder already initialized"); + return -1; + } transcode_codec_t *pDecoderContext=&ctx->decoder[0]; - transcode_codec_init_decoder(pDecoderContext,newMediaInfo); + transcode_codec_init_decoder(pDecoderContext,ctx->currentMediaInfo); sprintf(pDecoderContext->name,"Decoder for input %s",ctx->name); ctx->decoders++; if (init_outputs_from_config(ctx)<0) { @@ -165,6 +150,51 @@ int transcode_session_set_media_info(transcode_session_t *ctx,transcode_mediaInf if(ctx->outputs && !ctx->ack_handler) ctx->ack_handler = &ctx->output[0]; + + return 0; +} + + +int transcode_session_set_media_info(transcode_session_t *ctx,transcode_mediaInfo_t* newMediaInfo) +{ + if(!newMediaInfo) { + LOGGER0(CATEGORY_TRANSCODING_SESSION,AV_LOG_INFO,"transcode_session_set_media_info. newMediaInfo == NULL"); + return AVERROR(EINVAL); + } + + if (ctx->currentMediaInfo) { + if(ctx->decoders > 0) { + AVCodecParameters *currentCodecParams=ctx->currentMediaInfo->codecParams; + AVCodecParameters *newCodecParams=newMediaInfo->codecParams; + bool changed=newCodecParams->width!=currentCodecParams->width || + newCodecParams->height!=currentCodecParams->height || + newCodecParams->extradata_size!=currentCodecParams->extradata_size; + + if (currentCodecParams->extradata_size>0 && + newCodecParams->extradata!=NULL && + currentCodecParams->extradata!=NULL && + 0!=memcmp(newCodecParams->extradata,currentCodecParams->extradata,currentCodecParams->extradata_size)) + changed=true; + + avcodec_parameters_free(&newMediaInfo->codecParams); + av_free(newMediaInfo); + + if (!changed) { + LOGGER0(CATEGORY_TRANSCODING_SESSION,AV_LOG_INFO,"transcode_session_set_media_info. media info did not change"); + return 0; + } else { + LOGGER0(CATEGORY_TRANSCODING_SESSION,AV_LOG_ERROR,"transcode_session_set_media_info, changing media info on the fly is currently not supported"); + return -1; + } + } else { + avcodec_parameters_free(&ctx->currentMediaInfo->codecParams); + av_free(ctx->currentMediaInfo); + ctx->currentMediaInfo = NULL; + } + } + + ctx->currentMediaInfo=newMediaInfo; + return 0; } @@ -710,6 +740,14 @@ int transcode_session_send_packet(transcode_session_t *ctx ,struct AVPacket* pac clock_estimator_push_frame(&ctx->clock_estimator,packet->dts,packet->pos); ctx->lastInputDts=packet->dts; samples_stats_add(&ctx->processedStats,packet->dts,packet->pos,packet->size); + + if(!ctx->decoders) { + if( (ret = transcode_session_init_pipeline(ctx)) < 0) { + LOGGER(CATEGORY_TRANSCODING_SESSION,AV_LOG_ERROR, "transcode_session_init_pipeline failed %d",ret); + return ret; + } + } + } bool shouldDecode=false; for (int i=0;ioutputs;i++) { From 1cdd2667036fc7175f86cc018067b70ec52a75df Mon Sep 17 00:00:00 2001 From: igorshevach Date: Tue, 17 Sep 2024 10:38:58 +0300 Subject: [PATCH 18/27] fix --- transcoder/debug/file_streamer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transcoder/debug/file_streamer.c b/transcoder/debug/file_streamer.c index 8b3471f9..b9c0fc30 100644 --- a/transcoder/debug/file_streamer.c +++ b/transcoder/debug/file_streamer.c @@ -58,7 +58,7 @@ void* thread_stream_from_file(void *vargp) json_get_int64(GetConfig(),"input.hiccupIntervalSec",0,&hiccupIntervalSec); int64_t resendMediaInfoIntervalSec; - json_get_int64(GetConfig(),"input.resendMediaInfoIntervalSec",10,&resendMediaInfoIntervalSec); + json_get_int64(GetConfig(),"input.resendMediaInfoIntervalSec",0,&resendMediaInfoIntervalSec); AVPacket packet; From ec212e9f660d219af9b4cfeda281d4fa85b141f9 Mon Sep 17 00:00:00 2001 From: igorshevach Date: Tue, 17 Sep 2024 11:07:12 +0300 Subject: [PATCH 19/27] add \n --- nginx-kmp-out-module/src/ngx_kmp_out_track.c | 1 + 1 file changed, 1 insertion(+) diff --git a/nginx-kmp-out-module/src/ngx_kmp_out_track.c b/nginx-kmp-out-module/src/ngx_kmp_out_track.c index f9d3e9eb..55842465 100644 --- a/nginx-kmp-out-module/src/ngx_kmp_out_track.c +++ b/nginx-kmp-out-module/src/ngx_kmp_out_track.c @@ -1298,6 +1298,7 @@ ngx_kmp_out_track_write_media_info(ngx_kmp_out_track_t *track) return NGX_OK; } + static void ngx_kmp_out_track_chain_md5_hex(u_char dst[32], ngx_chain_t *in, u_char *p) { From 1a947157d44e983dfecec0902a005f416842b698 Mon Sep 17 00:00:00 2001 From: igorshevach Date: Tue, 17 Sep 2024 11:30:49 +0300 Subject: [PATCH 20/27] remove static from .h file --- transcoder/common/vector.h | 1 - 1 file changed, 1 deletion(-) diff --git a/transcoder/common/vector.h b/transcoder/common/vector.h index a263550b..ffff3707 100644 --- a/transcoder/common/vector.h +++ b/transcoder/common/vector.h @@ -20,7 +20,6 @@ typedef struct { void vector_init(vector_t *); int vector_total(vector_t *); -static void vector_resize(vector_t *, int); void vector_add(vector_t *, void *); void vector_set(vector_t *, int, void *); void *vector_get(vector_t *, int); From 55c32251dabca3e3ddcf88057b30649c8f2e02a7 Mon Sep 17 00:00:00 2001 From: igorshevach Date: Tue, 17 Sep 2024 11:31:26 +0300 Subject: [PATCH 21/27] fix _S macro --- transcoder/core.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/transcoder/core.h b/transcoder/core.h index aa3476f8..3148813a 100644 --- a/transcoder/core.h +++ b/transcoder/core.h @@ -5,7 +5,13 @@ #include "libs.h" -#define _S(EXP) if ((EXP)<0) { return EXP;} +#define _S(EXP) { \ + int retVal = (EXP); \ + if(retVal < 0) { \ + return retVal; \ + } \ +} + #ifndef u_char #define u_char unsigned char #endif From 4a2950baa1197e4c429e2187c2b0960594909ba1 Mon Sep 17 00:00:00 2001 From: igorshevach Date: Tue, 17 Sep 2024 13:13:25 +0300 Subject: [PATCH 22/27] build test failed --- nginx-kmp-out-module/src/ngx_kmp_out_track.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nginx-kmp-out-module/src/ngx_kmp_out_track.c b/nginx-kmp-out-module/src/ngx_kmp_out_track.c index 55842465..fa249e08 100644 --- a/nginx-kmp-out-module/src/ngx_kmp_out_track.c +++ b/nginx-kmp-out-module/src/ngx_kmp_out_track.c @@ -1250,7 +1250,7 @@ ngx_kmp_out_track_log_media_info(ngx_kmp_out_track_t *track) break; case KMP_MEDIA_AUDIO: ngx_log_error(NGX_LOG_INFO, &track->log, 0, - "ngx_kmp_out_track_write_media_info: audio" + "ngx_kmp_out_track_log_media_info: audio" " channels: %uD bits_per_sample: %uD sample_rate: %uD channel_layout: %uD", track->media_info.u.audio.channels, track->media_info.u.audio.bits_per_sample, track->media_info.u.audio.sample_rate, track->media_info.u.audio.channel_layout); From 12731f2a2f86b9a7a4c0548c2b9e721f559100bc Mon Sep 17 00:00:00 2001 From: igorshevach Date: Tue, 17 Sep 2024 13:17:01 +0300 Subject: [PATCH 23/27] code style --- nginx-rtmp-module/src/ngx_rtmp_codec_module.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nginx-rtmp-module/src/ngx_rtmp_codec_module.c b/nginx-rtmp-module/src/ngx_rtmp_codec_module.c index 5a71bef9..82286034 100644 --- a/nginx-rtmp-module/src/ngx_rtmp_codec_module.c +++ b/nginx-rtmp-module/src/ngx_rtmp_codec_module.c @@ -1467,7 +1467,7 @@ ngx_rtmp_codec_postconfiguration(ngx_conf_t *cf) nelts = cmcf->events[NGX_RTMP_MSG_AUDIO].nelts; for(;nelts > 1;nelts--, h--) { - *h = h[-1]; + *h = h[-1]; } *h = ngx_rtmp_codec_av; @@ -1479,7 +1479,7 @@ ngx_rtmp_codec_postconfiguration(ngx_conf_t *cf) nelts = cmcf->events[NGX_RTMP_MSG_VIDEO].nelts; for(;nelts > 1;nelts--, h--) { - *h = h[-1]; + *h = h[-1]; } *h = ngx_rtmp_codec_av; From e91199741e42398ce22f505c3383f7c1f12996de Mon Sep 17 00:00:00 2001 From: igorshevach Date: Tue, 17 Sep 2024 13:25:44 +0300 Subject: [PATCH 24/27] code style --- nginx-kmp-out-module/src/ngx_kmp_out_track.c | 32 ++++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/nginx-kmp-out-module/src/ngx_kmp_out_track.c b/nginx-kmp-out-module/src/ngx_kmp_out_track.c index fa249e08..c5408fbc 100644 --- a/nginx-kmp-out-module/src/ngx_kmp_out_track.c +++ b/nginx-kmp-out-module/src/ngx_kmp_out_track.c @@ -1209,22 +1209,22 @@ ngx_kmp_out_track_write(ngx_kmp_out_track_t *track, u_char *data, return ngx_kmp_out_track_write_chain(track, &in, data); } + static ngx_int_t ngx_kmp_out_track_log_media_info(ngx_kmp_out_track_t *track) { - u_char *buf = NULL, - *p; + u_char *buf = NULL, *p; if(track->extra_data.len > 0) { buf = ngx_pnalloc(track->pool, 2 * track->extra_data.len + 1); - if(!buf) + if(buf == NULL) { - ngx_log_error(NGX_LOG_ERR, &track->log, 0, - "ngx_kmp_out_track_log_media_info:" - "failed to allocate buffer for extra data size %uD", - 2 * track->extra_data.len + 1); - return NGX_ERROR; + ngx_log_error(NGX_LOG_ERR, &track->log, 0, + "ngx_kmp_out_track_log_media_info:" + "failed to allocate buffer for extra data size %uD", + 2 * track->extra_data.len + 1); + return NGX_ERROR; } p = ngx_hex_dump(buf, track->extra_data.data, track->extra_data.len); @@ -1232,12 +1232,12 @@ ngx_kmp_out_track_log_media_info(ngx_kmp_out_track_t *track) } ngx_log_error(NGX_LOG_INFO, &track->log, 0, - "ngx_kmp_out_track_log_media_info:" - " media_type: %uD codec_id: %uD" - " timescale: %uD bitrate: %uD" - " extra_data: %s", - track->media_info.media_type, track->media_info.codec_id, - track->media_info.timescale, track->media_info.bitrate, buf); + "ngx_kmp_out_track_log_media_info:" + " media_type: %uD codec_id: %uD" + " timescale: %uD bitrate: %uD" + " extra_data: %s", + track->media_info.media_type, track->media_info.codec_id, + track->media_info.timescale, track->media_info.bitrate, buf); switch(track->media_info.media_type) { case KMP_MEDIA_VIDEO: @@ -1252,8 +1252,8 @@ ngx_kmp_out_track_log_media_info(ngx_kmp_out_track_t *track) ngx_log_error(NGX_LOG_INFO, &track->log, 0, "ngx_kmp_out_track_log_media_info: audio" " channels: %uD bits_per_sample: %uD sample_rate: %uD channel_layout: %uD", - track->media_info.u.audio.channels, track->media_info.u.audio.bits_per_sample, - track->media_info.u.audio.sample_rate, track->media_info.u.audio.channel_layout); + track->media_info.u.audio.channels, track->media_info.u.audio.bits_per_sample, + track->media_info.u.audio.sample_rate, track->media_info.u.audio.channel_layout); break; }; From f27d3a7e69b1562d5d3a8bf872b4670fc419b9d2 Mon Sep 17 00:00:00 2001 From: igorshevach Date: Tue, 17 Sep 2024 13:36:28 +0300 Subject: [PATCH 25/27] code style --- nginx-kmp-out-module/src/ngx_kmp_out_track.c | 33 ++++++++++--------- nginx-rtmp-module/src/ngx_rtmp_codec_module.c | 4 +-- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/nginx-kmp-out-module/src/ngx_kmp_out_track.c b/nginx-kmp-out-module/src/ngx_kmp_out_track.c index c5408fbc..36d5c106 100644 --- a/nginx-kmp-out-module/src/ngx_kmp_out_track.c +++ b/nginx-kmp-out-module/src/ngx_kmp_out_track.c @@ -1218,13 +1218,13 @@ ngx_kmp_out_track_log_media_info(ngx_kmp_out_track_t *track) if(track->extra_data.len > 0) { buf = ngx_pnalloc(track->pool, 2 * track->extra_data.len + 1); - if(buf == NULL) + if (buf == NULL) { - ngx_log_error(NGX_LOG_ERR, &track->log, 0, + ngx_log_error(NGX_LOG_ERR, &track->log, 0, "ngx_kmp_out_track_log_media_info:" "failed to allocate buffer for extra data size %uD", 2 * track->extra_data.len + 1); - return NGX_ERROR; + return NGX_ERROR; } p = ngx_hex_dump(buf, track->extra_data.data, track->extra_data.len); @@ -1238,26 +1238,26 @@ ngx_kmp_out_track_log_media_info(ngx_kmp_out_track_t *track) " extra_data: %s", track->media_info.media_type, track->media_info.codec_id, track->media_info.timescale, track->media_info.bitrate, buf); - switch(track->media_info.media_type) + switch (track->media_info.media_type) { case KMP_MEDIA_VIDEO: - ngx_log_error(NGX_LOG_INFO, &track->log, 0, - "ngx_kmp_out_track_log_media_info: video" - " width: %uD height: %uD frame_rate: %uD/%uD cea_captions: %s", - track->media_info.u.video.width, track->media_info.u.video.height, - track->media_info.u.video.frame_rate.num, track->media_info.u.video.frame_rate.denom, - track->media_info.u.video.cea_captions ? "true" : "false"); + ngx_log_error(NGX_LOG_INFO, &track->log, 0, + "ngx_kmp_out_track_log_media_info: video" + " width: %uD height: %uD frame_rate: %uD/%uD cea_captions: %s", + track->media_info.u.video.width, track->media_info.u.video.height, + track->media_info.u.video.frame_rate.num, track->media_info.u.video.frame_rate.denom, + track->media_info.u.video.cea_captions ? "true" : "false"); break; case KMP_MEDIA_AUDIO: - ngx_log_error(NGX_LOG_INFO, &track->log, 0, - "ngx_kmp_out_track_log_media_info: audio" - " channels: %uD bits_per_sample: %uD sample_rate: %uD channel_layout: %uD", - track->media_info.u.audio.channels, track->media_info.u.audio.bits_per_sample, - track->media_info.u.audio.sample_rate, track->media_info.u.audio.channel_layout); + ngx_log_error(NGX_LOG_INFO, &track->log, 0, + "ngx_kmp_out_track_log_media_info: audio" + " channels: %uD bits_per_sample: %uD sample_rate: %uD channel_layout: %uD", + track->media_info.u.audio.channels, track->media_info.u.audio.bits_per_sample, + track->media_info.u.audio.sample_rate, track->media_info.u.audio.channel_layout); break; }; - if(buf) + if (buf) { ngx_pfree(track->pool, buf); } @@ -1265,6 +1265,7 @@ ngx_kmp_out_track_log_media_info(ngx_kmp_out_track_t *track) return NGX_OK; } + ngx_int_t ngx_kmp_out_track_write_media_info(ngx_kmp_out_track_t *track) { diff --git a/nginx-rtmp-module/src/ngx_rtmp_codec_module.c b/nginx-rtmp-module/src/ngx_rtmp_codec_module.c index 82286034..da487c6b 100644 --- a/nginx-rtmp-module/src/ngx_rtmp_codec_module.c +++ b/nginx-rtmp-module/src/ngx_rtmp_codec_module.c @@ -1466,7 +1466,7 @@ ngx_rtmp_codec_postconfiguration(ngx_conf_t *cf) } nelts = cmcf->events[NGX_RTMP_MSG_AUDIO].nelts; - for(;nelts > 1;nelts--, h--) { + for ( ; nelts > 1; nelts--, h--) { *h = h[-1]; } @@ -1478,7 +1478,7 @@ ngx_rtmp_codec_postconfiguration(ngx_conf_t *cf) } nelts = cmcf->events[NGX_RTMP_MSG_VIDEO].nelts; - for(;nelts > 1;nelts--, h--) { + for (; nelts > 1; nelts--, h--) { *h = h[-1]; } From 200f8c5cf46099a1e5abe8ab21e2be7fdd6835b9 Mon Sep 17 00:00:00 2001 From: igorshevach Date: Wed, 18 Sep 2024 20:23:11 +0300 Subject: [PATCH 26/27] fix log line --- transcoder/transcode/transcode_session.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/transcoder/transcode/transcode_session.c b/transcoder/transcode/transcode_session.c index a7dce14a..5b109c00 100644 --- a/transcoder/transcode/transcode_session.c +++ b/transcoder/transcode/transcode_session.c @@ -416,14 +416,14 @@ int encodeFrame(transcode_session_t *pContext,int encoderId,int outputId,AVFrame goto encoder_error; } + output_frame_id = pContext->transcoded_frame_first_id+pOutput->stats.totalFrames; + add_packet_frame_id_and_pts(pOutPacket,output_frame_id,pOutPacket->pts); + pOutPacket->pos=clock_estimator_get_clock(&pContext->clock_estimator,pOutPacket->dts); + LOGGER(CATEGORY_TRANSCODING_SESSION,AV_LOG_DEBUG,"[%s] received encoded frame %s from encoder Id %d", pOutput->track_id, getPacketDesc(pOutPacket), encoderId); - output_frame_id = pContext->transcoded_frame_first_id+pOutput->stats.totalFrames; - add_packet_frame_id_and_pts(pOutPacket,output_frame_id,pOutPacket->pts); - - pOutPacket->pos=clock_estimator_get_clock(&pContext->clock_estimator,pOutPacket->dts); if(pContext->ack_handler == pOutput){ _S(ackEncode(pEncoder->ctx,&pContext->ack_handler->acker,pOutPacket)); From a798d110f9d3a6abbf3ee7f6606e5cbec7947cee Mon Sep 17 00:00:00 2001 From: igorshevach Date: Wed, 18 Sep 2024 21:12:39 +0300 Subject: [PATCH 27/27] - fix logs - fix acks in testing --- transcoder/ackHandler/ackHandlerInternal.cpp | 8 ++++---- transcoder/receiver_server.c | 9 +++++++-- .../transcode/transcode_session_output.c | 18 ++++++++++-------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/transcoder/ackHandler/ackHandlerInternal.cpp b/transcoder/ackHandler/ackHandlerInternal.cpp index d979b10d..9722d656 100644 --- a/transcoder/ackHandler/ackHandlerInternal.cpp +++ b/transcoder/ackHandler/ackHandlerInternal.cpp @@ -7,7 +7,7 @@ try { am.addIn(*desc); } catch(const std::exception &e) { - LOGGER(LoggingCategory,AV_LOG_ERROR," %s audio map. ack_map_add_input %d %lld failed due to %s", + LOGGER(LoggingCategory,AV_LOG_ERROR," %s map. ack_map_add_input %lld %lld failed due to %s", am.m_name.c_str(),desc->id, desc->pts,e.what()); } } @@ -20,7 +20,7 @@ try { am.addFiltered(*desc); } catch(const std::exception &e) { - LOGGER(LoggingCategory,AV_LOG_ERROR," %s audio map. ack_map_add_filtered %d %lld failed due to %s", + LOGGER(LoggingCategory,AV_LOG_ERROR," %s map. ack_map_add_filtered %lld %lld failed due to %s", am.m_name.c_str(),desc->id, desc->pts,e.what()); } } @@ -33,7 +33,7 @@ try { am.addOut(*desc); } catch(const std::exception &e) { - LOGGER(LoggingCategory,AV_LOG_ERROR," %s audio map. ack_map_add_output %d %lld failed due to %s", + LOGGER(LoggingCategory,AV_LOG_ERROR," %s map. ack_map_add_output %lld %lld failed due to %s", am.m_name.c_str(),desc->id, desc->pts,e.what()); } } @@ -48,7 +48,7 @@ am.map(ack,*ao); return; } catch(const std::exception &e) { - LOGGER(LoggingCategory,AV_LOG_ERROR," %s audio map. ack_map_ack %lld failed due to %s", + LOGGER(LoggingCategory,AV_LOG_ERROR," %s map. ack_map_ack %lld failed due to %s", am.m_name.c_str(),ack, e.what()); } } diff --git a/transcoder/receiver_server.c b/transcoder/receiver_server.c index d24f6c72..bc37e0f7 100644 --- a/transcoder/receiver_server.c +++ b/transcoder/receiver_server.c @@ -119,9 +119,14 @@ int clientLoop(receiver_server_t *server,receiver_server_session_t *session,tran if(!autoAckMode) { transcode_session_get_ack_frame_id(transcode_session,¤t_position); // TODO: we cheat here, received_frame_id may have not been persisted yet - // so we should make sure all of the frames are consumed ny upstream + // so we should ensure all of the frames up to received_frame_id + // inclusively, are consumed by upstream current_position.frame_id = current_position.transcoded_frame_id = received_frame_id; - LOGGER(CATEGORY_RECEIVER,AV_LOG_INFO,"[%s] sending ack for packet # : %lld",session->stream_name,current_position.frame_id); + LOGGER(CATEGORY_RECEIVER,AV_LOG_INFO,"[%s] sending ack for frame position: %lld, %lld, %lld", + session->stream_name, + current_position.frame_id, + current_position.transcoded_frame_id, + current_position.offset); _S(KMP_send_ack(&session->kmpClient,¤t_position)); } break; diff --git a/transcoder/transcode/transcode_session_output.c b/transcoder/transcode/transcode_session_output.c index 5066cdf4..df73953a 100644 --- a/transcoder/transcode/transcode_session_output.c +++ b/transcoder/transcode/transcode_session_output.c @@ -130,14 +130,16 @@ int transcode_session_output_send_output_packet(transcode_session_output_t *pOut } av_write_frame(pOutput->oc, NULL); - ack_desc_t desc = {0}; - if( (ret = get_packet_frame_id(cpPacket,&desc.id)) >= 0) { - if(pOutput->acker.ctx){ - pOutput->acker.map(&pOutput->acker,desc.id,&desc); - } - pOutput->lastMappedAck = desc.id; - pOutput->lastAck=desc.id; - pOutput->lastOffset=desc.offset; + if(cpPacket->flags & AV_PKT_FLAG_KEY) { + ack_desc_t desc = {0}; + if( (ret = get_packet_frame_id(cpPacket,&desc.id)) >= 0) { + if(pOutput->acker.ctx){ + pOutput->acker.map(&pOutput->acker,desc.id,&desc); + } + pOutput->lastMappedAck = desc.id; + pOutput->lastAck=desc.id; + pOutput->lastOffset=desc.offset; + } } av_packet_free(&cpPacket);