]> git.sesse.net Git - nageru/blob - httpd.cpp
More fixes for non-PCM HTTP 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         // TODO: This is an ugly place to have this logic.
137         const string codec_name = global_flags.stream_audio_codec_name.empty() ?
138                 AUDIO_OUTPUT_CODEC_NAME :
139                 global_flags.stream_audio_codec_name;
140
141         AVCodec *codec_audio = avcodec_find_encoder_by_name(codec_name.c_str());
142         if (codec_audio == nullptr) {
143                 fprintf(stderr, "ERROR: Could not find codec '%s'\n", codec_name.c_str());
144                 exit(1);
145         }
146
147         mux.reset(new Mux(avctx, width, height, video_codec, codec_audio, time_base, bit_rate));
148 }
149
150 ssize_t HTTPD::Stream::reader_callback_thunk(void *cls, uint64_t pos, char *buf, size_t max)
151 {
152         HTTPD::Stream *stream = (HTTPD::Stream *)cls;
153         return stream->reader_callback(pos, buf, max);
154 }
155
156 ssize_t HTTPD::Stream::reader_callback(uint64_t pos, char *buf, size_t max)
157 {
158         unique_lock<mutex> lock(buffer_mutex);
159         has_buffered_data.wait(lock, [this]{ return !buffered_data.empty(); });
160
161         ssize_t ret = 0;
162         while (max > 0 && !buffered_data.empty()) {
163                 const string &s = buffered_data.front();
164                 assert(s.size() > used_of_buffered_data);
165                 size_t len = s.size() - used_of_buffered_data;
166                 if (max >= len) {
167                         // Consume the entire (rest of the) string.
168                         memcpy(buf, s.data() + used_of_buffered_data, len);
169                         buf += len;
170                         ret += len;
171                         max -= len;
172                         buffered_data.pop_front();
173                         used_of_buffered_data = 0;
174                 } else {
175                         // We don't need the entire string; just use the first part of it.
176                         memcpy(buf, s.data() + used_of_buffered_data, max);
177                         buf += max;
178                         used_of_buffered_data += max;
179                         ret += max;
180                         max = 0;
181                 }
182         }
183
184         return ret;
185 }
186
187 void HTTPD::Stream::add_packet(const AVPacket &pkt, int64_t pts, int64_t dts)
188 {
189         mux->add_packet(pkt, pts, dts);
190 }
191
192 int HTTPD::Stream::write_packet_thunk(void *opaque, uint8_t *buf, int buf_size)
193 {
194         HTTPD::Stream *stream = (HTTPD::Stream *)opaque;
195         return stream->write_packet(buf, buf_size);
196 }
197
198 int HTTPD::Stream::write_packet(uint8_t *buf, int buf_size)
199 {
200         unique_lock<mutex> lock(buffer_mutex);
201         buffered_data.emplace_back((char *)buf, buf_size);
202         has_buffered_data.notify_all(); 
203         return buf_size;
204 }
205