#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;
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) {
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.
}
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)
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;
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