]> git.sesse.net Git - nageru/blobdiff - httpd.cpp
Make it possible for file and HTTP streams to use different audio codecs.
[nageru] / httpd.cpp
index 7375e8a09db1d597f0a2988975019d709b7276a9..487d429c5d5b1e49df0a4e019f954823ffb2ceee 100644 (file)
--- a/httpd.cpp
+++ b/httpd.cpp
@@ -42,40 +42,12 @@ void HTTPD::start(int port)
                         MHD_OPTION_END);
 }
 
-void HTTPD::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts, PacketDestination destination)
+void HTTPD::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
 {
        unique_lock<mutex> lock(streams_mutex);
-       if (destination != DESTINATION_FILE_ONLY) {
-               for (Stream *stream : streams) {
-                       stream->add_packet(pkt, pts, dts);
-               }
-       }
-       if (file_mux && destination != DESTINATION_HTTP_ONLY) {
-               file_mux->add_packet(pkt, pts, dts);
-       }
-}
-
-void HTTPD::open_output_file(const string &filename)
-{
-       AVFormatContext *avctx = avformat_alloc_context();
-       avctx->oformat = av_guess_format(NULL, filename.c_str(), NULL);
-       assert(filename.size() < sizeof(avctx->filename) - 1);
-       strcpy(avctx->filename, filename.c_str());
-
-       string url = "file:" + filename;
-       int ret = avio_open2(&avctx->pb, url.c_str(), AVIO_FLAG_WRITE, &avctx->interrupt_callback, NULL);
-       if (ret < 0) {
-               char tmp[AV_ERROR_MAX_STRING_SIZE];
-               fprintf(stderr, "%s: avio_open2() failed: %s\n", filename.c_str(), av_make_error_string(tmp, sizeof(tmp), ret));
-               exit(1);
+       for (Stream *stream : streams) {
+               stream->add_packet(pkt, pts, dts);
        }
-
-       file_mux.reset(new Mux(avctx, width, height, Mux::CODEC_H264, TIMEBASE));
-}
-
-void HTTPD::close_output_file()
-{
-       file_mux.reset();
 }
 
 int HTTPD::answer_to_connection_thunk(void *cls, MHD_Connection *connection,
@@ -95,8 +67,13 @@ int HTTPD::answer_to_connection(MHD_Connection *connection,
        AVOutputFormat *oformat = av_guess_format(global_flags.stream_mux_name.c_str(), nullptr, nullptr);
        assert(oformat != nullptr);
 
+       // TODO: This is an ugly place to have this logic.
+       const int bit_rate = global_flags.stream_audio_codec_name.empty() ?
+               DEFAULT_AUDIO_OUTPUT_BIT_RATE :
+               global_flags.stream_audio_codec_bitrate;
+
        int time_base = global_flags.stream_coarse_timebase ? COARSE_TIMEBASE : TIMEBASE;
-       HTTPD::Stream *stream = new HTTPD::Stream(oformat, width, height, time_base);
+       HTTPD::Stream *stream = new HTTPD::Stream(oformat, width, height, time_base, bit_rate);
        {
                unique_lock<mutex> lock(streams_mutex);
                streams.insert(stream);
@@ -140,7 +117,7 @@ void HTTPD::request_completed(struct MHD_Connection *connection, void **con_cls,
        }
 }
 
-HTTPD::Stream::Stream(AVOutputFormat *oformat, int width, int height, int time_base)
+HTTPD::Stream::Stream(AVOutputFormat *oformat, int width, int height, int time_base, int bit_rate)
 {
        AVFormatContext *avctx = avformat_alloc_context();
        avctx->oformat = oformat;
@@ -156,7 +133,7 @@ HTTPD::Stream::Stream(AVOutputFormat *oformat, int width, int height, int time_b
 
        avctx->flags = AVFMT_FLAG_CUSTOM_IO;
 
-       mux.reset(new Mux(avctx, width, height, video_codec, time_base));
+       mux.reset(new Mux(avctx, width, height, video_codec, time_base, bit_rate));
 }
 
 ssize_t HTTPD::Stream::reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max)