1 /*
2 * Live smooth streaming fragmenter
3 * Copyright (c) 2012 Martin Storsjo
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "config.h"
24 #if HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27
34
39
40 typedef struct {
41 char file[1024];
47
48 typedef struct {
50 int ctx_inited;
54 URLContext *
out2;
// Auxiliary output stream where all output is also written
55 URLContext *
tail_out;
// The actual main output stream, if we're currently seeked back to write elsewhere
57 int packets_written;
59 int nb_fragments, fragments_size, fragment_index;
61
67
69 const AVClass *
class;
/* Class for private options. */
79
81 {
90 return buf_size;
91 }
92
94 {
96 int i;
97 if (whence != SEEK_SET)
102 }
105 }
109 }
115 }
118 if (offset >= frag->
start_pos && offset < frag->start_pos + frag->
size) {
125 if (ret < 0) {
129 }
138 }
139 }
141 }
142
144 {
148 int i;
151 if (!ptr)
153 }
154 if (!ptr)
155 return;
158 goto fail;
159 for (i = 0; i <
size; i++)
161 fail:
164 }
165
167 {
169 int i, j;
171 return;
188 }
190 }
191
193 {
194 int removed = 0, i,
start = 0;
196 return;
198 removed = 1;
199 if (final)
200 skip = 0;
201 if (window_size)
205 if (!final || removed)
207 else
209 }
210 }
211
213 {
216 char filename[1024], temp_filename[1024];
217 int ret, i, video_chunks = 0, audio_chunks = 0, video_streams = 0, audio_streams = 0;
219
221 snprintf(temp_filename,
sizeof(temp_filename),
"%s/Manifest.tmp", s->
filename);
223 if (ret < 0) {
226 }
227 avio_printf(out,
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
233 }
236 video_streams++;
237 } else {
239 audio_streams++;
240 }
241 }
242 if (!final) {
243 duration = 0;
244 video_chunks = audio_chunks = 0;
245 }
249 }
250 avio_printf(out,
"<SmoothStreamingMedia MajorVersion=\"2\" MinorVersion=\"0\" Duration=\"%"PRIu64
"\"", duration);
251 if (!final)
255 int last = -1,
index = 0;
256 avio_printf(out,
"<StreamIndex Type=\"video\" QualityLevels=\"%d\" Chunks=\"%d\" Url=\"QualityLevels({bitrate})/Fragments(video={start time})\">\n", video_streams, video_chunks);
260 continue;
261 last = i;
264 }
267 }
269 int last = -1,
index = 0;
270 avio_printf(out,
"<StreamIndex Type=\"audio\" QualityLevels=\"%d\" Chunks=\"%d\" Url=\"QualityLevels({bitrate})/Fragments(audio={start time})\">\n", audio_streams, audio_chunks);
274 continue;
275 last = i;
278 }
281 }
285 rename(temp_filename, filename);
286 return 0;
287 }
288
290 {
294
298 goto fail;
299 }
300
302 if (!oformat) {
304 goto fail;
305 }
306
310 goto fail;
311 }
312
319
323 goto fail;
324 }
326 if (mkdir(os->
dirname, 0777) < 0) {
329 goto fail;
330 }
331
333 if (!ctx) {
335 goto fail;
336 }
340
343 goto fail;
344 }
347
351 goto fail;
352 }
353
358 goto fail;
359 }
371 } else {
374 goto fail;
375 }
376 } else {
385 } else {
388 goto fail;
389 }
391 }
393 }
394
398 }
400
401 fail:
402 if (ret)
405 }
406
408 {
416 if (*moof_size < 8 || *moof_size > size)
417 goto fail;
419 goto fail;
421 if (len > *moof_size)
422 goto fail;
424 goto fail;
428 goto fail;
433 if (len < 8 || len >= *moof_size)
434 goto fail;
435 if (tag ==
MKTAG(
'u',
'u',
'i',
'd')) {
436 static const uint8_t tfxd[] = {
437 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
438 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
439 };
442 if (!memcmp(uuid, tfxd, 16) && len >= 8 + 16 + 4 + 16) {
446 ret = 0;
447 break;
448 }
449 }
451 }
452 fail:
455 }
456
458 {
459 int err;
467 return err;
468 }
469 }
471 if (!frag)
482 return 0;
483 }
484
486 {
494 }
495 while (size > 0) {
497 int n =
FFMIN(size,
sizeof(buf));
499 if (n <= 0) {
501 break;
502 }
505 }
510 }
511
513 {
516
519 char filename[1024], target_filename[1024], header_filename[1024];
521 int64_t start_ts,
duration, moof_size;
523 continue;
524
527 if (ret < 0)
528 break;
535
540 break;
543 copy_moof(s, filename, header_filename, moof_size);
544 rename(filename, target_filename);
545 add_fragment(os, target_filename, header_filename, start_ts, duration, start_pos,
size);
546 }
547
551 int j;
555 if (remove > 0) {
556 for (j = 0; j < remove; j++) {
560 }
563 }
566 }
567 }
568
569 if (ret >= 0)
572 }
573
575 {
581
584
589
593 }
594
597 }
598
600 {
603
605 char filename[1024];
607 unlink(filename);
609 }
610
612 return 0;
613 }
614
615 #define OFFSET(x) offsetof(SmoothStreamingContext, x)
616 #define E AV_OPT_FLAG_ENCODING_PARAM
618 {
"window_size",
"number of fragments kept in the manifest",
OFFSET(window_size),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
E },
619 {
"extra_window_size",
"number of fragments kept outside of the manifest before removing from disk",
OFFSET(extra_window_size),
AV_OPT_TYPE_INT, { .i64 = 5 }, 0, INT_MAX,
E },
620 {
"lookahead_count",
"number of lookahead fragments",
OFFSET(lookahead_count),
AV_OPT_TYPE_INT, { .i64 = 2 }, 0, INT_MAX,
E },
621 {
"min_frag_duration",
"minimum fragment duration (in microseconds)",
OFFSET(min_frag_duration),
AV_OPT_TYPE_INT64, { .i64 = 5000000 }, 0, INT_MAX,
E },
622 {
"remove_at_exit",
"remove all fragments when finished",
OFFSET(remove_at_exit),
AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1,
E },
623 { NULL },
624 };
625
631 };
632
633
635 .
name =
"smoothstreaming",
646 };