X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=ffmpeg_capture.cpp;h=9ae35042d1ce998701e34b9072ca26b2700d047f;hb=4a0187ffb4075b4d217b8d9e9c96cac548b199d8;hp=b8010d08219b52e30479b299822550ac8a850519;hpb=affb3ca6e247a1193287c20e5ebb2574289375ae;p=nageru diff --git a/ffmpeg_capture.cpp b/ffmpeg_capture.cpp index b8010d0..9ae3504 100644 --- a/ffmpeg_capture.cpp +++ b/ffmpeg_capture.cpp @@ -211,6 +211,8 @@ FFmpegCapture::FFmpegCapture(const string &filename, unsigned width, unsigned he // Not really used for anything. description = "Video: " + filename; + last_frame = steady_clock::now(); + avformat_network_init(); // In case someone wants this. } @@ -288,6 +290,7 @@ void FFmpegCapture::producer_thread_func() producer_thread_should_quit.sleep_for(seconds(1)); continue; } + should_interrupt = false; if (!play_video(pathname)) { // Error. fprintf(stderr, "Error when playing %s, sleeping one second and trying again...\n", pathname.c_str()); @@ -321,10 +324,15 @@ void FFmpegCapture::send_disconnected_frame() video_frame.len = width * height * 4; } else { video_format.stride = width; + current_frame_ycbcr_format.luma_coefficients = YCBCR_REC_709; current_frame_ycbcr_format.full_range = true; current_frame_ycbcr_format.num_levels = 256; current_frame_ycbcr_format.chroma_subsampling_x = 2; current_frame_ycbcr_format.chroma_subsampling_y = 2; + current_frame_ycbcr_format.cb_x_position = 0.0f; + current_frame_ycbcr_format.cb_y_position = 0.0f; + current_frame_ycbcr_format.cr_x_position = 0.0f; + current_frame_ycbcr_format.cr_y_position = 0.0f; video_frame.len = width * height * 2; } memset(video_frame.data, 0, video_frame.len); @@ -332,6 +340,7 @@ void FFmpegCapture::send_disconnected_frame() frame_callback(-1, AVRational{1, TIMEBASE}, -1, AVRational{1, TIMEBASE}, timecode++, video_frame, /*video_offset=*/0, video_format, FrameAllocator::Frame(), /*audio_offset=*/0, AudioFormat()); + last_frame_was_connected = false; } } @@ -349,7 +358,10 @@ bool FFmpegCapture::play_video(const string &pathname) last_modified = buf.st_mtim; } - auto format_ctx = avformat_open_input_unique(pathname.c_str(), nullptr, nullptr); + AVDictionary *opts = nullptr; + av_dict_set(&opts, "fflags", "nobuffer", 0); + + auto format_ctx = avformat_open_input_unique(pathname.c_str(), nullptr, &opts, AVIOInterruptCB{ &FFmpegCapture::interrupt_cb_thunk, this }); if (format_ctx == nullptr) { fprintf(stderr, "%s: Error opening file\n", pathname.c_str()); return false; @@ -414,6 +426,7 @@ bool FFmpegCapture::play_video(const string &pathname) internal_rewind(); // Main loop. + bool first_frame = true; while (!producer_thread_should_quit.should_quit()) { if (process_queued_commands(format_ctx.get(), pathname, last_modified, /*rewound=*/nullptr)) { return true; @@ -462,15 +475,34 @@ bool FFmpegCapture::play_video(const string &pathname) pts_origin = frame->pts; } next_frame_start = compute_frame_start(frame->pts, pts_origin, video_timebase, start, rate); + if (first_frame && last_frame_was_connected) { + // If reconnect took more than one second, this is probably a live feed, + // and we should reset the resampler. (Or the rate is really, really low, + // in which case a reset on the first frame is fine anyway.) + if (duration(next_frame_start - last_frame).count() >= 1.0) { + last_frame_was_connected = false; + } + } video_frame->received_timestamp = next_frame_start; + audio_frame->received_timestamp = next_frame_start; bool finished_wakeup = producer_thread_should_quit.sleep_until(next_frame_start); if (finished_wakeup) { if (audio_frame->len > 0) { assert(audio_pts != -1); } + if (!last_frame_was_connected) { + // We're recovering from an error (or really slow load, see above). + // Make sure to get the audio resampler reset. (This is a hack; + // ideally, the frame callback should just accept a way to signal + // audio discontinuity.) + timecode += MAX_FPS * 2 + 1; + } frame_callback(frame->pts, video_timebase, audio_pts, audio_timebase, timecode++, video_frame.get_and_release(), 0, video_format, audio_frame.get_and_release(), 0, audio_format); + first_frame = false; + last_frame = steady_clock::now(); + last_frame_was_connected = true; break; } else { if (producer_thread_should_quit.should_quit()) break; @@ -797,3 +829,13 @@ UniqueFrame FFmpegCapture::make_video_frame(const AVFrame *frame, const string & return video_frame; } + +int FFmpegCapture::interrupt_cb_thunk(void *unique) +{ + return reinterpret_cast(unique)->interrupt_cb(); +} + +int FFmpegCapture::interrupt_cb() +{ + return should_interrupt.load(); +}