]> git.sesse.net Git - nageru/blob - httpd.cpp
Make it possible for file and HTTP streams to use different audio codecs.
[nageru] / httpd.cpp
1 #include <assert.h>
2 #include <microhttpd.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 extern "C" {
8 #include <libavcodec/avcodec.h>
9 #include <libavutil/channel_layout.h>
10 #include <libavutil/mathematics.h>
11 #include <libavutil/mem.h>
12 #include <libavutil/pixfmt.h>
13 #include <libavutil/rational.h>
14 #include <libavutil/samplefmt.h>
15 }
16
17 #include <vector>
18
19 #include "httpd.h"
20
21 #include "defs.h"
22 #include "flags.h"
23 #include "timebase.h"
24
25 struct MHD_Connection;
26 struct MHD_Response;
27
28 using namespace std;
29
30 HTTPD::HTTPD(int width, int height)
31         : width(width), height(height)
32 {
33 }
34
35 void HTTPD::start(int port)
36 {
37         MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL_INTERNALLY | MHD_USE_DUAL_STACK,
38                          port,
39                          nullptr, nullptr,
40                          &answer_to_connection_thunk, this,
41                          MHD_OPTION_NOTIFY_COMPLETED, &request_completed_thunk, this, 
42                          MHD_OPTION_END);
43 }
44
45 void HTTPD::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
46 {
47         unique_lock<mutex> lock(streams_mutex);
48         for (Stream *stream : streams) {
49                 stream->add_packet(pkt, pts, dts);
50         }
51 }
52
53 int HTTPD::answer_to_connection_thunk(void *cls, MHD_Connection *connection,
54                                       const char *url, const char *method,
55                                       const char *version, const char *upload_data,
56                                       size_t *upload_data_size, void **con_cls)
57 {
58         HTTPD *httpd = (HTTPD *)cls;
59         return httpd->answer_to_connection(connection, url, method, version, upload_data, upload_data_size, con_cls);
60 }
61
62 int HTTPD::answer_to_connection(MHD_Connection *connection,
63                                 const char *url, const char *method,
64                                 const char *version, const char *upload_data,
65                                 size_t *upload_data_size, void **con_cls)
66 {
67         AVOutputFormat *oformat = av_guess_format(global_flags.stream_mux_name.c_str(), nullptr, nullptr);
68         assert(oformat != nullptr);
69
70         // TODO: This is an ugly place to have this logic.
71         const int bit_rate = global_flags.stream_audio_codec_name.empty() ?
72                 DEFAULT_AUDIO_OUTPUT_BIT_RATE :
73                 global_flags.stream_audio_codec_bitrate;
74
75         int time_base = global_flags.stream_coarse_timebase ? COARSE_TIMEBASE : TIMEBASE;
76         HTTPD::Stream *stream = new HTTPD::Stream(oformat, width, height, time_base, bit_rate);
77         {
78                 unique_lock<mutex> lock(streams_mutex);
79                 streams.insert(stream);
80         }
81         *con_cls = stream;
82
83         // Does not strictly have to be equal to MUX_BUFFER_SIZE.
84         MHD_Response *response = MHD_create_response_from_callback(
85                 (size_t)-1, MUX_BUFFER_SIZE, &HTTPD::Stream::reader_callback_thunk, stream, &HTTPD::free_stream);
86         int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
87         //MHD_destroy_response(response);
88
89         return ret;
90 }
91
92 void HTTPD::free_stream(void *cls)
93 {
94         // FIXME: When is this actually called, if ever?
95         // Also, shouldn't we remove it from streams?
96         HTTPD::Stream *stream = (HTTPD::Stream *)cls;
97         delete stream;
98 }
99
100 void HTTPD::request_completed_thunk(void *cls, struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe)
101 {
102         HTTPD *httpd = (HTTPD *)cls;
103         return httpd->request_completed(connection, con_cls, toe);
104 }
105
106 void HTTPD::request_completed(struct MHD_Connection *connection, void **con_cls, enum MHD_RequestTerminationCode toe)
107 {
108         if (con_cls == nullptr) {
109                 // Request was never set up.
110                 return;
111         }
112         HTTPD::Stream *stream = (HTTPD::Stream *)*con_cls;
113         {
114                 unique_lock<mutex> lock(streams_mutex);
115                 delete stream;
116                 streams.erase(stream);
117         }
118 }
119
120 HTTPD::Stream::Stream(AVOutputFormat *oformat, int width, int height, int time_base, int bit_rate)
121 {
122         AVFormatContext *avctx = avformat_alloc_context();
123         avctx->oformat = oformat;
124         uint8_t *buf = (uint8_t *)av_malloc(MUX_BUFFER_SIZE);
125         avctx->pb = avio_alloc_context(buf, MUX_BUFFER_SIZE, 1, this, nullptr, &HTTPD::Stream::write_packet_thunk, nullptr);
126
127         Mux::Codec video_codec;
128         if (global_flags.uncompressed_video_to_http) {
129                 video_codec = Mux::CODEC_NV12;
130         } else {
131                 video_codec = Mux::CODEC_H264;
132         }
133
134         avctx->flags = AVFMT_FLAG_CUSTOM_IO;
135
136         mux.reset(new Mux(avctx, width, height, video_codec, time_base, bit_rate));
137 }
138
139 ssize_t HTTPD::Stream::reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max)
140 {
141         HTTPD::Stream *stream = (HTTPD::Stream *)cls;
142         return stream->reader_callback(pos, buf, max);
143 }
144
145 ssize_t HTTPD::Stream::reader_callback(uint64_t pos, char *buf, size_t max)
146 {
147         unique_lock<mutex> lock(buffer_mutex);
148         has_buffered_data.wait(lock, [this]{ return !buffered_data.empty(); });
149
150         ssize_t ret = 0;
151         while (max > 0 && !buffered_data.empty()) {
152                 const string &s = buffered_data.front();
153                 assert(s.size() > used_of_buffered_data);
154                 size_t len = s.size() - used_of_buffered_data;
155                 if (max >= len) {
156                         // Consume the entire (rest of the) string.
157                         memcpy(buf, s.data() + used_of_buffered_data, len);
158                         buf += len;
159                         ret += len;
160                         max -= len;
161                         buffered_data.pop_front();
162                         used_of_buffered_data = 0;
163                 } else {
164                         // We don't need the entire string; just use the first part of it.
165                         memcpy(buf, s.data() + used_of_buffered_data, max);
166                         buf += max;
167                         used_of_buffered_data += max;
168                         ret += max;
169                         max = 0;
170                 }
171         }
172
173         return ret;
174 }
175
176 void HTTPD::Stream::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
177 {
178         mux->add_packet(pkt, pts, dts);
179 }
180
181 int HTTPD::Stream::write_packet_thunk(void *opaque, uint8_t *buf, int buf_size)
182 {
183         HTTPD::Stream *stream = (HTTPD::Stream *)opaque;
184         return stream->write_packet(buf, buf_size);
185 }
186
187 int HTTPD::Stream::write_packet(uint8_t *buf, int buf_size)
188 {
189         unique_lock<mutex> lock(buffer_mutex);
190         buffered_data.emplace_back((char *)buf, buf_size);
191         has_buffered_data.notify_all(); 
192         return buf_size;
193 }
194