forked from cherokee/webserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_streaming.c
557 lines (444 loc) · 12.4 KB
/
handler_streaming.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* Cherokee
*
* Authors:
* Alvaro Lopez Ortega <alvaro@alobbs.com>
*
* Copyright (C) 2001-2011 Alvaro Lopez Ortega
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "common-internal.h"
#include "handler_streaming.h"
#include "connection.h"
#include "connection-protected.h"
#include "avl.h"
#include "util.h"
#define ENTRIES "streaming"
#define FLV_HEADER "FLV\x1\x1\0\0\0\x9\0\0\0\x9"
/* Global 'stream rate' cache
*/
static cherokee_avl_t _streaming_cache;
PLUGIN_INFO_HANDLER_EASY_INIT (streaming, http_all_methods);
ret_t
cherokee_handler_streaming_props_free (cherokee_handler_streaming_props_t *props)
{
if (props->props_file != NULL) {
cherokee_handler_file_props_free (props->props_file);
props->props_file = NULL;
}
return cherokee_handler_props_free_base (HANDLER_PROPS(props));
}
ret_t
cherokee_handler_streaming_configure (cherokee_config_node_t *conf,
cherokee_server_t *srv,
cherokee_module_props_t **_props)
{
ret_t ret;
cherokee_list_t *i;
cherokee_handler_streaming_props_t *props;
if (*_props == NULL) {
CHEROKEE_NEW_STRUCT (n, handler_streaming_props);
cherokee_handler_props_init_base (HANDLER_PROPS(n),
MODULE_PROPS_FREE(cherokee_handler_streaming_props_free));
n->props_file = NULL;
n->auto_rate = true;
n->auto_rate_factor = 0.1;
n->auto_rate_boost = 5;
*_props = MODULE_PROPS(n);
}
props = PROP_STREAMING(*_props);
/* Parse 'streaming' parameters
*/
cherokee_config_node_foreach (i, conf) {
cherokee_config_node_t *subconf = CONFIG_NODE(i);
if (equal_buf_str (&subconf->key, "rate")) {
ret = cherokee_atob (subconf->val.buf, &props->auto_rate);
if (ret != ret_ok) return ret_error;
} else if (equal_buf_str (&subconf->key, "rate_factor")) {
props->auto_rate_factor = strtof (subconf->val.buf, NULL);
} else if (equal_buf_str (&subconf->key, "rate_boost")) {
ret = cherokee_atoi (subconf->val.buf, &props->auto_rate_boost);
if (ret != ret_ok) return ret_error;
}
}
/* Parse 'file' parameters
*/
return cherokee_handler_file_configure (conf, srv, (cherokee_module_props_t **)&props->props_file);
}
ret_t
cherokee_handler_streaming_free (cherokee_handler_streaming_t *hdl)
{
if (hdl->handler_file != NULL) {
cherokee_handler_file_free (hdl->handler_file);
}
if (hdl->avformat != NULL) {
av_close_input_file (hdl->avformat);
}
cherokee_buffer_mrproper (&hdl->local_file);
return ret_ok;
}
ret_t
cherokee_handler_streaming_new (cherokee_handler_t **hdl,
void *cnt,
cherokee_module_props_t *props)
{
ret_t ret;
CHEROKEE_NEW_STRUCT (n, handler_streaming);
/* Init the base class object
*/
cherokee_handler_init_base (HANDLER(n), cnt, HANDLER_PROPS(props), PLUGIN_INFO_HANDLER_PTR(streaming));
MODULE(n)->free = (module_func_free_t) cherokee_handler_streaming_free;
MODULE(n)->init = (handler_func_init_t) cherokee_handler_streaming_init;
HANDLER(n)->step = (handler_func_step_t) cherokee_handler_streaming_step;
HANDLER(n)->add_headers = (handler_func_add_headers_t) cherokee_handler_streaming_add_headers;
/* Instance the sub-handler
*/
ret = cherokee_handler_file_new ((cherokee_handler_t **)&n->handler_file, cnt,
MODULE_PROPS(PROP_STREAMING(props)->props_file));
if (ret != ret_ok) {
return ret_ok;
}
/* Supported features
*/
HANDLER(n)->support = HANDLER(n->handler_file)->support;
/* Init props
*/
cherokee_buffer_init (&n->local_file);
n->avformat = NULL;
n->start = -1;
n->start_flv = false;
n->start_time = -1;
n->auto_rate_bps = -1;
n->boost_until = 0;
/* Return the object
*/
*hdl = HANDLER(n);
return ret_ok;
}
static ret_t
parse_time_start (cherokee_handler_streaming_t *hdl,
cherokee_buffer_t *value)
{
float start;
char *end = NULL;
cherokee_connection_t *conn = HANDLER_CONN(hdl);
start = strtof (value->buf, &end);
if (*end != '\0') {
goto error;
}
if (start < 0) {
goto error;
}
TRACE(ENTRIES, "Starting time: %f\n", start);
hdl->start_time = start;
return ret_ok;
error:
conn->error_code = http_range_not_satisfiable;
return ret_error;
}
static ret_t
parse_offset_start (cherokee_handler_streaming_t *hdl,
cherokee_buffer_t *value)
{
long start;
char *end = NULL;
cherokee_connection_t *conn = HANDLER_CONN(hdl);
start = strtol (value->buf, &end, 10);
if (*end != '\0') {
goto error;
}
if (start < 0) {
goto error;
}
if (start > hdl->handler_file->info->st_size) {
goto error;
}
TRACE(ENTRIES, "Starting point: %d\n", start);
hdl->start = start;
return ret_ok;
error:
conn->error_code = http_range_not_satisfiable;
return ret_error;
}
static ret_t
seek_mp3 (cherokee_handler_streaming_t *hdl)
{
int re;
ret_t ret;
long timestamp;
AVPacket pkt;
/* Calculate timestamp
*/
timestamp = hdl->start_time * AV_TIME_BASE;
if (hdl->avformat->start_time != AV_NOPTS_VALUE) {
timestamp += hdl->avformat->start_time;
}
/* Seek
*/
re = av_seek_frame (hdl->avformat, /* AVFormatContext */
-1, /* Stream index */
timestamp, /* target timestamp */
AVSEEK_FLAG_BACKWARD); /* flags */
if (re < 0) {
PRINT_MSG ("WARNING: Couldn't seek: %d\n", re);
return ret_error;
}
/* Read Frame
*/
av_init_packet (&pkt);
av_read_frame (hdl->avformat, &pkt);
hdl->start = pkt.pos;
av_free_packet (&pkt);
/* Seek at the beginning of the frame
*/
TRACE(ENTRIES, "Seeking: %d\n", hdl->start);
ret = cherokee_handler_file_seek (hdl->handler_file, hdl->start);
if (unlikely (ret != ret_ok)) {
return ret_error;
}
return ret_ok;
}
static ret_t
set_rate (cherokee_handler_streaming_t *hdl,
cherokee_connection_t *conn,
long rate)
{
cherokee_handler_streaming_props_t *props = HDL_STREAMING_PROP(hdl);
if (rate <= 0)
return ret_ok;
/* This will be the real limit
*/
hdl->auto_rate_bps = rate + (rate * props->auto_rate_factor);
/* Special 'initial boosting' limit
*/
conn->limit_bps = props->auto_rate_boost * hdl->auto_rate_bps;
conn->limit_rate = true;
/* How long the initial boost last..
*/
if (hdl->start > 0) {
hdl->boost_until = hdl->start + conn->limit_bps;
} else {
hdl->boost_until = conn->limit_bps;
}
TRACE(ENTRIES, "Limiting rate (initial boost): %d bytes, until=%d\n",
conn->limit_bps, hdl->boost_until);
return ret_ok;
}
static ret_t
open_media_file (cherokee_handler_streaming_t *hdl)
{
int re;
if (hdl->avformat != NULL)
return ret_ok;
/* Open the media stream
*/
re = av_open_input_file (&hdl->avformat, hdl->local_file.buf, NULL, 0, NULL);
if (re != 0) {
goto error;
}
/* Read the info
*/
re = av_find_stream_info (hdl->avformat);
if (re < 0) {
goto error;
}
return ret_ok;
error:
if (hdl->avformat != NULL) {
av_close_input_file (hdl->avformat);
hdl->avformat = NULL;
}
return ret_error;
}
static ret_t
set_auto_rate (cherokee_handler_streaming_t *hdl)
{
ret_t ret;
long rate;
long secs;
void *tmp = NULL;
cherokee_connection_t *conn = HANDLER_CONN(hdl);
/* Check the cache
*/
ret = cherokee_avl_get (&_streaming_cache, &hdl->local_file, &tmp);
if (ret == ret_ok) {
rate = POINTER_TO_INT(tmp);
if (rate <= 0)
return ret_ok;
return set_rate (hdl, conn, rate);
}
/* Open the media stream
*/
ret = open_media_file (hdl);
if (unlikely (ret != ret_ok)) {
return ret_error;
}
/* bits/s to bytes/s
*/
rate = (hdl->avformat->bit_rate / 8);
secs = (hdl->avformat->duration / AV_TIME_BASE);
TRACE(ENTRIES, "Duration: %d seconds\n", hdl->avformat->duration / AV_TIME_BASE);
TRACE(ENTRIES, "Rate: %d bps (%d bytes/s)\n", hdl->avformat->bit_rate, rate);
/* Sanity Check
*/
if ((rate < 0) || (secs < 0)) {
return ret_error;
}
if (likely (secs > 0)) {
long tmp;
tmp = (hdl->avformat->file_size / secs);
if (tmp > rate) {
rate = tmp;
TRACE(ENTRIES, "New rate: %d bytes/s\n", rate);
}
}
ret = set_rate (hdl, conn, rate);
cherokee_avl_add (&_streaming_cache,
&hdl->local_file,
INT_TO_POINTER(rate));
return ret;
}
ret_t
cherokee_handler_streaming_init (cherokee_handler_streaming_t *hdl)
{
ret_t ret;
cherokee_buffer_t *value;
cherokee_boolean_t is_flv = false;
cherokee_boolean_t is_mp3 = false;
cherokee_buffer_t *mime = NULL;
cherokee_connection_t *conn = HANDLER_CONN(hdl);
cherokee_handler_streaming_props_t *props = HDL_STREAMING_PROP(hdl);
/* Local File
*/
cherokee_buffer_add_buffer (&hdl->local_file, &conn->local_directory);
cherokee_buffer_add_buffer (&hdl->local_file, &conn->request);
/* Init sub-handler
*/
ret = cherokee_handler_file_init (hdl->handler_file);
if (unlikely (ret != ret_ok))
return ret;
/* Media type detection
*/
if (hdl->handler_file->mime) {
cherokee_mime_entry_get_type (hdl->handler_file->mime, &mime);
}
if (mime != NULL) {
if (cherokee_buffer_cmp_str (mime, "video/x-flv") == 0) {
is_flv = true;
} else if (cherokee_buffer_cmp_str (mime, "audio/mpeg") == 0) {
is_mp3 = true;
}
}
/* Parse arguments
*/
ret = cherokee_connection_parse_args (conn);
if (ret == ret_ok) {
ret = cherokee_avl_get_ptr (conn->arguments, "start", (void **) &value);
if ((ret == ret_ok) && (value != NULL) && (value->len > 0)) {
/* Set the starting point
*/
if (is_flv) {
ret = parse_offset_start (hdl, value);
if (ret != ret_ok)
return ret_error;
} else if (is_mp3) {
ret = parse_time_start (hdl, value);
if (ret != ret_ok)
return ret_error;
}
}
}
/* Seeking
*/
if ((is_flv) && (hdl->start > 0)) {
ret = cherokee_handler_file_seek (hdl->handler_file, hdl->start);
if (unlikely (ret != ret_ok)) {
return ret_error;
}
hdl->start_flv = true;
} else if ((is_mp3) && (hdl->start_time > 0)) {
ret = open_media_file (hdl);
if (ret != ret_ok) {
return ret_error;
}
ret = seek_mp3 (hdl);
if (unlikely (ret != ret_ok)) {
return ret_error;
}
}
/* Set the Bitrate limit
*/
if (props->auto_rate) {
set_auto_rate (hdl);
}
return ret_ok;
}
ret_t
cherokee_handler_streaming_add_headers (cherokee_handler_streaming_t *hdl,
cherokee_buffer_t *buffer)
{
ret_t ret;
/* Add the real headers
*/
ret = cherokee_handler_file_add_headers (hdl->handler_file, buffer);
/* The handler support flags might have changed. Re-sync.
*/
HANDLER(hdl)->support = HANDLER(hdl->handler_file)->support;
return ret;
}
ret_t
cherokee_handler_streaming_step (cherokee_handler_streaming_t *hdl,
cherokee_buffer_t *buffer)
{
cherokee_connection_t *conn = HANDLER_CONN(hdl);
/* FLV's "start" parameter
*/
if (hdl->start_flv) {
cherokee_buffer_add_str (buffer, FLV_HEADER);
hdl->start_flv = false;
return ret_ok;
}
/* Check the initial boost
*/
if ((conn->limit_bps > hdl->auto_rate_bps) &&
(hdl->handler_file->offset > hdl->boost_until))
{
conn->limit_bps = hdl->auto_rate_bps;
conn->limit_rate = true;
TRACE(ENTRIES, "Limiting rate: %d bytes/s\n", conn->limit_bps);
}
/* Call the real step
*/
return cherokee_handler_file_step (hdl->handler_file, buffer);
}
/* Library init function
*/
static cherokee_boolean_t _streaming_is_init = false;
void
PLUGIN_INIT_NAME(streaming) (cherokee_plugin_loader_t *loader)
{
if (_streaming_is_init) return;
_streaming_is_init = true;
/* Load the dependences
*/
cherokee_plugin_loader_load (loader, "file");
/* Initialize the global cache
*/
cherokee_avl_init (&_streaming_cache);
/* Initialize FFMpeg
*/
av_register_all();
}