]> git.sesse.net Git - nageru/blobdiff - nageru/ffmpeg_capture.cpp
Support SRT inputs.
[nageru] / nageru / ffmpeg_capture.cpp
index 2d1b3385b4ac0d64c137bba4c3590819f06f05f6..4bc277af61fc05405c8c264fbecc134de2ce59ef 100644 (file)
@@ -39,6 +39,10 @@ extern "C" {
 #include "ref_counted_frame.h"
 #include "shared/timebase.h"
 
+#ifdef HAVE_SRT
+#include <srt/srt.h>
+#endif
+
 #define FRAME_SIZE (8 << 20)  // 8 MB.
 
 using namespace std;
@@ -257,6 +261,25 @@ FFmpegCapture::FFmpegCapture(const string &filename, unsigned width, unsigned he
        avformat_network_init();  // In case someone wants this.
 }
 
+#ifdef HAVE_SRT
+FFmpegCapture::FFmpegCapture(int srt_sock, const string &stream_id)
+       : srt_sock(srt_sock),
+         width(global_flags.width),
+         height(global_flags.height),
+         pixel_format(bmusb::PixelFormat_8BitYCbCrPlanar),
+         video_timebase{1, 1}
+{
+       if (stream_id.empty()) {
+               description = "SRT stream";
+       } else {
+               description = stream_id;
+       }
+       play_as_fast_as_possible = true;
+       play_once = true;
+       last_frame = steady_clock::now();
+}
+#endif
+
 FFmpegCapture::~FFmpegCapture()
 {
        if (has_dequeue_callbacks) {
@@ -330,22 +353,38 @@ void FFmpegCapture::producer_thread_func()
                        filename_copy = filename;
                }
 
-               string pathname = search_for_file(filename_copy);
+               string pathname;
+               if (srt_sock == -1) {
+                       pathname = search_for_file(filename_copy);
+               } else {
+                       pathname = description;
+               }
                if (pathname.empty()) {
-                       fprintf(stderr, "%s not found, sleeping one second and trying again...\n", filename_copy.c_str());
                        send_disconnected_frame();
+                       if (play_once) {
+                               break;
+                       }
                        producer_thread_should_quit.sleep_for(seconds(1));
+                       fprintf(stderr, "%s not found, sleeping one second and trying again...\n", filename_copy.c_str());
                        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());
                        send_disconnected_frame();
+                       if (play_once) {
+                               break;
+                       }
+                       fprintf(stderr, "Error when playing %s, sleeping one second and trying again...\n", pathname.c_str());
                        producer_thread_should_quit.sleep_for(seconds(1));
                        continue;
                }
 
+               if (play_once) {
+                       send_disconnected_frame();
+                       break;
+               }
+
                // Probably just EOF, will exit the loop above on next test.
        }
 
@@ -391,6 +430,13 @@ void FFmpegCapture::send_disconnected_frame()
                        FrameAllocator::Frame(), /*audio_offset=*/0, AudioFormat());
                last_frame_was_connected = false;
        }
+
+       if (play_once) {
+               disconnected = true;
+               if (card_disconnected_callback != nullptr) {
+                       card_disconnected_callback();
+               }
+       }
 }
 
 bool FFmpegCapture::play_video(const string &pathname)
@@ -407,7 +453,23 @@ bool FFmpegCapture::play_video(const string &pathname)
                last_modified = buf.st_mtim;
        }
 
-       auto format_ctx = avformat_open_input_unique(pathname.c_str(), nullptr, nullptr, AVIOInterruptCB{ &FFmpegCapture::interrupt_cb_thunk, this });
+       AVFormatContextWithCloser format_ctx;
+       if (srt_sock == -1) {
+               // Regular file.
+               format_ctx = avformat_open_input_unique(pathname.c_str(), /*fmt=*/nullptr,
+                       /*options=*/nullptr,
+                       AVIOInterruptCB{ &FFmpegCapture::interrupt_cb_thunk, this });
+       } else {
+#ifdef HAVE_SRT
+               // SRT socket, already opened.
+               AVInputFormat *mpegts_fmt = av_find_input_format("mpegts");
+               format_ctx = avformat_open_input_unique(&FFmpegCapture::read_srt_thunk, this,
+                       mpegts_fmt, /*options=*/nullptr,
+                       AVIOInterruptCB{ &FFmpegCapture::interrupt_cb_thunk, this });
+#else
+               assert(false);
+#endif
+       }
        if (format_ctx == nullptr) {
                fprintf(stderr, "%s: Error opening file\n", pathname.c_str());
                return false;
@@ -948,12 +1010,25 @@ UniqueFrame FFmpegCapture::make_video_frame(const AVFrame *frame, const string &
        return video_frame;
 }
 
-int FFmpegCapture::interrupt_cb_thunk(void *unique)
+int FFmpegCapture::interrupt_cb_thunk(void *opaque)
 {
-       return reinterpret_cast<FFmpegCapture *>(unique)->interrupt_cb();
+       return reinterpret_cast<FFmpegCapture *>(opaque)->interrupt_cb();
 }
 
 int FFmpegCapture::interrupt_cb()
 {
        return should_interrupt.load();
 }
+
+#ifdef HAVE_SRT
+int FFmpegCapture::read_srt_thunk(void *opaque, uint8_t *buf, int buf_size)
+{
+       return reinterpret_cast<FFmpegCapture *>(opaque)->read_srt(buf, buf_size);
+}
+
+int FFmpegCapture::read_srt(uint8_t *buf, int buf_size)
+{
+       SRT_MSGCTRL mc = srt_msgctrl_default;
+       return srt_recvmsg2(srt_sock, reinterpret_cast<char *>(buf), buf_size, &mc);
+}
+#endif