From: Steinar H. Gunderson Date: Sun, 14 Oct 2018 14:16:45 +0000 (+0200) Subject: Support other streams than the default file. X-Git-Tag: 1.8.0~76^2~60 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=5586c82d9265073c44bc765e82815ee09b4fb758;p=nageru Support other streams than the default file. --- diff --git a/Makefile b/Makefile index 0775211..a080c6c 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ OBJS += $(OBJS_WITH_MOC:.o=.moc.o) OBJS += flow.o gpu_timers.o OBJS += ffmpeg_raii.o main.o player.o httpd.o mux.o metacube2.o video_stream.o context.o chroma_subsampler.o -OBJS += vaapi_jpeg_decoder.o memcpy_interleaved.o db.o disk_space_estimator.o ycbcr_converter.o +OBJS += vaapi_jpeg_decoder.o memcpy_interleaved.o db.o disk_space_estimator.o ycbcr_converter.o flags.o OBJS += state.pb.o %.o: %.cpp diff --git a/flags.cpp b/flags.cpp new file mode 100644 index 0000000..af1cfb9 --- /dev/null +++ b/flags.cpp @@ -0,0 +1,57 @@ +#include "flags.h" + +#include +#include +#include +#include + +#include + +using namespace std; + +Flags global_flags; + +// Long options that have no corresponding short option. +enum LongOption { + OPTION_HELP = 1000, + OPTION_SLOW_DOWN_INPUT = 1001 +}; + +void usage() +{ + fprintf(stderr, "Usage: futatabi [OPTION]... SOURCE_URL\n"); + fprintf(stderr, "\n"); + fprintf(stderr, " --help print usage information\n"); + fprintf(stderr, " --slow-down-input slow down input to realtime (default on if no\n"); + fprintf(stderr, " source URL given)"); +} + +void parse_flags(int argc, char * const argv[]) +{ + static const option long_options[] = { + { "help", no_argument, 0, OPTION_HELP }, + { "slow-down-input", no_argument, 0, OPTION_SLOW_DOWN_INPUT }, + { 0, 0, 0, 0 } + }; + for ( ;; ) { + int option_index = 0; + int c = getopt_long(argc, argv, "q:", long_options, &option_index); + + if (c == -1) { + break; + } + switch (c) { + case OPTION_SLOW_DOWN_INPUT: + global_flags.slow_down_input = true; + break; + case OPTION_HELP: + usage(); + exit(0); + default: + fprintf(stderr, "Unknown option '%s'\n", argv[option_index]); + fprintf(stderr, "\n"); + usage(); + exit(1); + } + } +} diff --git a/flags.h b/flags.h new file mode 100644 index 0000000..794e90d --- /dev/null +++ b/flags.h @@ -0,0 +1,15 @@ +#ifndef _FLAGS_H +#define _FLAGS_H + +#include + +struct Flags { + std::string stream_source; + bool slow_down_input = false; +}; +extern Flags global_flags; + +void usage(); +void parse_flags(int argc, char * const argv[]); + +#endif // !defined(_FLAGS_H) diff --git a/main.cpp b/main.cpp index da2340b..b81471d 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -21,6 +22,7 @@ extern "C" { #include "defs.h" #include "disk_space_estimator.h" #include "ffmpeg_raii.h" +#include "flags.h" #include "httpd.h" #include "mainwindow.h" #include "player.h" @@ -61,6 +63,17 @@ int record_thread_func(); int main(int argc, char **argv) { + parse_flags(argc, argv); + if (optind == argc) { + global_flags.stream_source = "multiangle.mp4"; + global_flags.slow_down_input = true; + } else if (optind + 1 == argc) { + global_flags.stream_source = argv[optind]; + } else { + usage(); + exit(1); + } + avformat_network_init(); global_httpd = new HTTPD; global_httpd->start(DEFAULT_HTTPD_PORT); @@ -161,9 +174,9 @@ void load_existing_frames() int record_thread_func() { - auto format_ctx = avformat_open_input_unique("multiangle.mp4", nullptr, nullptr); + auto format_ctx = avformat_open_input_unique(global_flags.stream_source.c_str(), nullptr, nullptr); if (format_ctx == nullptr) { - fprintf(stderr, "%s: Error opening file\n", "example.mp4"); + fprintf(stderr, "%s: Error opening file\n", global_flags.stream_source.c_str()); return 1; } @@ -222,8 +235,7 @@ int record_thread_func() assert(pkt.stream_index < MAX_STREAMS); frames[pkt.stream_index].push_back(pts); - // Hack. Remove when we're dealing with live streams. - if (last_pts != -1) { + if (last_pts != -1 && global_flags.slow_down_input) { this_thread::sleep_for(microseconds((pts - last_pts) * 1000000 / TIMEBASE)); } last_pts = pts;