-
Notifications
You must be signed in to change notification settings - Fork 17
/
ngx_http_aac_module.c
313 lines (251 loc) · 11 KB
/
ngx_http_aac_module.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include "ngx_http_aac_module.h"
static ngx_int_t ngx_http_aac_handler(ngx_http_request_t *r) {
int status_code;
ngx_int_t rc;
ngx_buf_t *b;
ngx_chain_t out;
ngx_str_t rootpath = ngx_null_string;
ngx_str_t outputformat = ngx_null_string;
ngx_str_t outputheader = ngx_null_string;
ngx_str_t source = ngx_null_string;
audio_buffer *destination;
ngx_http_aac_module_loc_conf_t *conf;
destination = ngx_pcalloc(r->pool, sizeof(audio_buffer));
destination->data = NULL;
destination->len = 0;
destination->pool = r->pool;
conf = ngx_http_get_module_loc_conf(r, ngx_http_aac_module);
ngx_http_complex_value(r, conf->videosegments_rootpath, &rootpath);
ngx_http_complex_value(r, conf->output_format, &outputformat);
ngx_http_complex_value(r, conf->output_header, &outputheader);
rc = ngx_http_discard_request_body(r);
if (rc != NGX_OK) {
return rc;
}
source = build_source_path(r->pool, rootpath, r->uri);
status_code = ngx_http_aac_extract_audio(r->pool, r->connection->log, source, outputformat, destination);
if (status_code == NGX_HTTP_AAC_MODULE_VIDEO_SEGMENT_NOT_FOUND) {
r->headers_out.status = NGX_HTTP_NOT_FOUND;
r->header_only = 1;
r->headers_out.content_length_n = 0;
} else if (status_code == NGX_HTTP_AAC_MODULE_AUDIO_STREAM_NOT_FOUND) {
r->headers_out.status = NGX_HTTP_NO_CONTENT;
r->header_only = 1;
r->headers_out.content_length_n = 0;
} else if (status_code == NGX_HTTP_AAC_MODULE_NO_DECODER) {
r->headers_out.status = NGX_HTTP_NOT_IMPLEMENTED;
r->header_only = 1;
r->headers_out.content_length_n = 0;
} else {
r->headers_out.content_type_len = outputheader.len;
r->headers_out.content_type.len = r->headers_out.content_type_len;
r->headers_out.content_type.data = outputheader.data;
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
if (b == NULL) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
out.buf = b;
out.next = NULL;
b->pos = destination->data;
b->last = destination->data + (destination->len * sizeof(unsigned char));
b->memory = 1;
b->last_buf = 1;
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = destination->len;
}
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
return ngx_http_output_filter(r, &out);
}
static void *ngx_http_aac_module_create_loc_conf(ngx_conf_t *cf) {
ngx_http_aac_module_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_aac_module_loc_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
conf->videosegments_rootpath = NULL;
conf->output_format = NULL;
conf->output_header = NULL;
conf->enabled = NGX_CONF_UNSET;
return conf;
}
static char *ngx_http_aac_module_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) {
ngx_http_aac_module_loc_conf_t *prev = parent;
ngx_http_aac_module_loc_conf_t *conf = child;
ngx_conf_merge_value(conf->enabled, prev->enabled, 0);
if (conf->videosegments_rootpath == NULL) {
conf->videosegments_rootpath = (ngx_http_complex_value_t *)prev->videosegments_rootpath;
}
if (conf->output_format == NULL) {
conf->output_format = (ngx_http_complex_value_t *)prev->output_format;
}
if (conf->output_header == NULL) {
conf->output_header = (ngx_http_complex_value_t *)prev->output_header;
}
if ((conf->videosegments_rootpath == NULL) && (conf->enabled == 1)) {
ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "aac module: videosegments rootpath must be defined");
return NGX_CONF_ERROR;
}
if ((conf->output_format == NULL) && (conf->enabled == 1)) {
ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "aac module: output format must be defined");
return NGX_CONF_ERROR;
}
if ((conf->output_header == NULL) && (conf->enabled == 1)) {
ngx_conf_log_error(NGX_LOG_ERR, cf, 0, "aac module: output header must be defined");
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
static char *ngx_http_aac(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
ngx_http_core_loc_conf_t *clcf;
ngx_http_aac_module_loc_conf_t *vtlcf = conf;
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_aac_handler;
vtlcf->enabled = 1;
return NGX_CONF_OK;
}
static int ngx_http_aac_extract_audio(ngx_pool_t *pool, ngx_log_t *log, ngx_str_t source, ngx_str_t outputfmt, audio_buffer *destination) {
int audio_stream_id;
int return_code = NGX_ERROR;
int buffer_size;
unsigned char *exchange_area;
AVFormatContext *input_format_context = NULL;
AVFormatContext *output_format_context = NULL;
const AVOutputFormat *ofmt = NULL;
const AVCodec *in_codec;
AVStream *input_audio_stream;
AVStream *output_audio_stream;
AVCodecContext *out_codec_ctx;
AVPacket packet;
AVIOContext *io_context;
int ret;
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 10, 100)
av_register_all();
#endif
av_log_set_level(AV_LOG_PANIC);
if (avformat_open_input(&input_format_context, (char *)source.data, NULL, NULL) < 0) {
ngx_log_error(NGX_LOG_ERR, log, 0, "aac module: could not open video input: '%s'", source.data);
return_code = NGX_HTTP_AAC_MODULE_VIDEO_SEGMENT_NOT_FOUND;
goto exit;
}
if (avformat_find_stream_info(input_format_context, NULL) < 0) {
goto exit;
ngx_log_error(NGX_LOG_ERR, log, 0, "aac module: could not find stream info");
}
audio_stream_id = av_find_best_stream(input_format_context, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
if (audio_stream_id == AVERROR_STREAM_NOT_FOUND) {
ngx_log_error(NGX_LOG_ERR, log, 0, "aac module: audio stream not found");
return_code = NGX_HTTP_AAC_MODULE_AUDIO_STREAM_NOT_FOUND;
goto exit;
} else if (audio_stream_id == AVERROR_DECODER_NOT_FOUND) {
ngx_log_error(NGX_LOG_ERR, log, 0, "aac module: audio stream found, but no decoder for it");
return_code = NGX_HTTP_AAC_MODULE_NO_DECODER;
goto exit;
}
input_audio_stream = input_format_context->streams[audio_stream_id];
output_format_context = avformat_alloc_context();
if (!output_format_context) {
ngx_log_error(NGX_LOG_ERR, log, 0, "aac module: could not alloc output context");
goto exit;
}
in_codec = avcodec_find_decoder(input_audio_stream->codecpar->codec_id);
output_audio_stream = avformat_new_stream(output_format_context, in_codec);
if (!output_audio_stream) {
ngx_log_error(NGX_LOG_ERR, log, 0, "aac module: could not alloc output audio stream");
goto exit;
}
buffer_size = 4096;//1024;
exchange_area = ngx_pcalloc(pool, buffer_size * sizeof(unsigned char));
io_context = avio_alloc_context(exchange_area, buffer_size, 1, (void *)destination, NULL, write_packet, NULL);
output_format_context->pb = io_context;
ofmt = av_guess_format((char *)outputfmt.data, NULL, NULL);
if (!ofmt) {
ngx_log_error(NGX_LOG_ERR, log, 0, "aac module: could not get output format '%V'", &outputfmt);
goto exit;
}
output_format_context->oformat = ofmt;
out_codec_ctx = avcodec_alloc_context3(in_codec);
ret = avcodec_parameters_to_context(out_codec_ctx, input_audio_stream->codecpar);
if (ret < 0) {
ngx_log_error(NGX_LOG_ERR, log, 0, "Failed to copy context input to output stream codec context");
goto exit;
}
out_codec_ctx->codec_tag = 0;
if (output_format_context->oformat->flags & AVFMT_GLOBALHEADER) {
out_codec_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
ret = avcodec_parameters_from_context(output_audio_stream->codecpar, out_codec_ctx);
if (ret < 0) {
ngx_log_error(NGX_LOG_ERR, log, 0, "Failed to copy context input to output stream codec context: %d(%s)", ret, av_err2str(ret));
goto exit;
}
ret = avformat_write_header(output_format_context, NULL);
if (ret < 0) {
ngx_log_error(NGX_LOG_ERR, log, 0, "Failed to avformat_write_header: %d(%s)", ret, av_err2str(ret));
goto exit;
}
//av_dump_format(output_format_context, 0, "in_memory.aac", 1);
av_init_packet(&packet);
packet.size = 0;
packet.data = NULL;
while (av_read_frame(input_format_context, &packet) >= 0) {
if (packet.stream_index == audio_stream_id) {
/* copy packet */
packet.pts = av_rescale_q_rnd(packet.pts, input_audio_stream->time_base, output_audio_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
packet.dts = av_rescale_q_rnd(packet.dts, input_audio_stream->time_base, output_audio_stream->time_base, AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX);
packet.duration = av_rescale_q(packet.duration, input_audio_stream->time_base, output_audio_stream->time_base);
packet.pos = -1;
packet.stream_index = 0; // force set to 1st stream
ret = av_interleaved_write_frame(output_format_context, &packet);
if (ret < 0) {
ngx_log_error(NGX_LOG_ERR, log, 0, "Error muxing packet, %d(%s)", ret, av_err2str(ret));
goto exit;
}
}
av_packet_unref(&packet);
}
av_write_trailer(output_format_context);
av_free(io_context);
return_code = NGX_OK;
exit:
/* do some cleanup */
if (output_format_context != NULL) avformat_free_context(output_format_context);
if (input_format_context != NULL) avformat_close_input(&input_format_context);
return return_code;
}
/*
* Remove extension from uri and paste rootpath, uri and ts extension for get source path
*/
ngx_str_t build_source_path(ngx_pool_t *pool, ngx_str_t rootpath, ngx_str_t uri) {
int len = 0;
int urilen = uri.len;
char *source;
ngx_str_t source_t;
u_char *dot = (u_char *) ngx_strchr(uri.data, '.');
u_char *slash = (u_char *) ngx_strchr(uri.data, '/');
if (slash!=NULL && dot!=NULL && dot>slash) {
// We need to remove extension
urilen = uri.len - (uri.data + uri.len - dot);
}
len = (rootpath.len + urilen + strlen(".ts"));
source = ngx_pcalloc(pool, len * sizeof(char));
strncpy(source, (char *)rootpath.data, rootpath.len);
strncat(source, (char *)uri.data, urilen );
strcat(source, ".ts");
source_t.data = (u_char *)source;
source_t.len = len;
return source_t;
}
static int write_packet(void *opaque, unsigned char *buf, int buf_size) {
int old_size;
audio_buffer *destination = (audio_buffer *)opaque;
if (destination->data == NULL) {
destination->data = ngx_pcalloc(destination->pool, NGX_AAC_AUDIO_CHUNK_MAX_SIZE * sizeof(unsigned char));
}
old_size = destination->len;
destination->len += buf_size;
memcpy(destination->data + old_size, buf, buf_size * sizeof(unsigned char));
return buf_size;
}