yading@11
|
1 /*
|
yading@11
|
2 * Live smooth streaming fragmenter
|
yading@11
|
3 * Copyright (c) 2012 Martin Storsjo
|
yading@11
|
4 *
|
yading@11
|
5 * This file is part of FFmpeg.
|
yading@11
|
6 *
|
yading@11
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@11
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@11
|
9 * License as published by the Free Software Foundation; either
|
yading@11
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@11
|
11 *
|
yading@11
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@11
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@11
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@11
|
15 * Lesser General Public License for more details.
|
yading@11
|
16 *
|
yading@11
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@11
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@11
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@11
|
20 */
|
yading@11
|
21
|
yading@11
|
22 #include "config.h"
|
yading@11
|
23 #include <float.h>
|
yading@11
|
24 #if HAVE_UNISTD_H
|
yading@11
|
25 #include <unistd.h>
|
yading@11
|
26 #endif
|
yading@11
|
27
|
yading@11
|
28 #include "avformat.h"
|
yading@11
|
29 #include "internal.h"
|
yading@11
|
30 #include "os_support.h"
|
yading@11
|
31 #include "avc.h"
|
yading@11
|
32 #include "url.h"
|
yading@11
|
33 #include "isom.h"
|
yading@11
|
34
|
yading@11
|
35 #include "libavutil/opt.h"
|
yading@11
|
36 #include "libavutil/avstring.h"
|
yading@11
|
37 #include "libavutil/mathematics.h"
|
yading@11
|
38 #include "libavutil/intreadwrite.h"
|
yading@11
|
39
|
yading@11
|
40 typedef struct {
|
yading@11
|
41 char file[1024];
|
yading@11
|
42 char infofile[1024];
|
yading@11
|
43 int64_t start_time, duration;
|
yading@11
|
44 int n;
|
yading@11
|
45 int64_t start_pos, size;
|
yading@11
|
46 } Fragment;
|
yading@11
|
47
|
yading@11
|
48 typedef struct {
|
yading@11
|
49 AVFormatContext *ctx;
|
yading@11
|
50 int ctx_inited;
|
yading@11
|
51 char dirname[1024];
|
yading@11
|
52 uint8_t iobuf[32768];
|
yading@11
|
53 URLContext *out; // Current output stream where all output is written
|
yading@11
|
54 URLContext *out2; // Auxiliary output stream where all output is also written
|
yading@11
|
55 URLContext *tail_out; // The actual main output stream, if we're currently seeked back to write elsewhere
|
yading@11
|
56 int64_t tail_pos, cur_pos, cur_start_pos;
|
yading@11
|
57 int packets_written;
|
yading@11
|
58 const char *stream_type_tag;
|
yading@11
|
59 int nb_fragments, fragments_size, fragment_index;
|
yading@11
|
60 Fragment **fragments;
|
yading@11
|
61
|
yading@11
|
62 const char *fourcc;
|
yading@11
|
63 char *private_str;
|
yading@11
|
64 int packet_size;
|
yading@11
|
65 int audio_tag;
|
yading@11
|
66 } OutputStream;
|
yading@11
|
67
|
yading@11
|
68 typedef struct {
|
yading@11
|
69 const AVClass *class; /* Class for private options. */
|
yading@11
|
70 int window_size;
|
yading@11
|
71 int extra_window_size;
|
yading@11
|
72 int lookahead_count;
|
yading@11
|
73 int min_frag_duration;
|
yading@11
|
74 int remove_at_exit;
|
yading@11
|
75 OutputStream *streams;
|
yading@11
|
76 int has_video, has_audio;
|
yading@11
|
77 int nb_fragments;
|
yading@11
|
78 } SmoothStreamingContext;
|
yading@11
|
79
|
yading@11
|
80 static int ism_write(void *opaque, uint8_t *buf, int buf_size)
|
yading@11
|
81 {
|
yading@11
|
82 OutputStream *os = opaque;
|
yading@11
|
83 if (os->out)
|
yading@11
|
84 ffurl_write(os->out, buf, buf_size);
|
yading@11
|
85 if (os->out2)
|
yading@11
|
86 ffurl_write(os->out2, buf, buf_size);
|
yading@11
|
87 os->cur_pos += buf_size;
|
yading@11
|
88 if (os->cur_pos >= os->tail_pos)
|
yading@11
|
89 os->tail_pos = os->cur_pos;
|
yading@11
|
90 return buf_size;
|
yading@11
|
91 }
|
yading@11
|
92
|
yading@11
|
93 static int64_t ism_seek(void *opaque, int64_t offset, int whence)
|
yading@11
|
94 {
|
yading@11
|
95 OutputStream *os = opaque;
|
yading@11
|
96 int i;
|
yading@11
|
97 if (whence != SEEK_SET)
|
yading@11
|
98 return AVERROR(ENOSYS);
|
yading@11
|
99 if (os->tail_out) {
|
yading@11
|
100 if (os->out) {
|
yading@11
|
101 ffurl_close(os->out);
|
yading@11
|
102 }
|
yading@11
|
103 if (os->out2) {
|
yading@11
|
104 ffurl_close(os->out2);
|
yading@11
|
105 }
|
yading@11
|
106 os->out = os->tail_out;
|
yading@11
|
107 os->out2 = NULL;
|
yading@11
|
108 os->tail_out = NULL;
|
yading@11
|
109 }
|
yading@11
|
110 if (offset >= os->cur_start_pos) {
|
yading@11
|
111 if (os->out)
|
yading@11
|
112 ffurl_seek(os->out, offset - os->cur_start_pos, SEEK_SET);
|
yading@11
|
113 os->cur_pos = offset;
|
yading@11
|
114 return offset;
|
yading@11
|
115 }
|
yading@11
|
116 for (i = os->nb_fragments - 1; i >= 0; i--) {
|
yading@11
|
117 Fragment *frag = os->fragments[i];
|
yading@11
|
118 if (offset >= frag->start_pos && offset < frag->start_pos + frag->size) {
|
yading@11
|
119 int ret;
|
yading@11
|
120 AVDictionary *opts = NULL;
|
yading@11
|
121 os->tail_out = os->out;
|
yading@11
|
122 av_dict_set(&opts, "truncate", "0", 0);
|
yading@11
|
123 ret = ffurl_open(&os->out, frag->file, AVIO_FLAG_READ_WRITE, &os->ctx->interrupt_callback, &opts);
|
yading@11
|
124 av_dict_free(&opts);
|
yading@11
|
125 if (ret < 0) {
|
yading@11
|
126 os->out = os->tail_out;
|
yading@11
|
127 os->tail_out = NULL;
|
yading@11
|
128 return ret;
|
yading@11
|
129 }
|
yading@11
|
130 av_dict_set(&opts, "truncate", "0", 0);
|
yading@11
|
131 ffurl_open(&os->out2, frag->infofile, AVIO_FLAG_READ_WRITE, &os->ctx->interrupt_callback, &opts);
|
yading@11
|
132 av_dict_free(&opts);
|
yading@11
|
133 ffurl_seek(os->out, offset - frag->start_pos, SEEK_SET);
|
yading@11
|
134 if (os->out2)
|
yading@11
|
135 ffurl_seek(os->out2, offset - frag->start_pos, SEEK_SET);
|
yading@11
|
136 os->cur_pos = offset;
|
yading@11
|
137 return offset;
|
yading@11
|
138 }
|
yading@11
|
139 }
|
yading@11
|
140 return AVERROR(EIO);
|
yading@11
|
141 }
|
yading@11
|
142
|
yading@11
|
143 static void get_private_data(OutputStream *os)
|
yading@11
|
144 {
|
yading@11
|
145 AVCodecContext *codec = os->ctx->streams[0]->codec;
|
yading@11
|
146 uint8_t *ptr = codec->extradata;
|
yading@11
|
147 int size = codec->extradata_size;
|
yading@11
|
148 int i;
|
yading@11
|
149 if (codec->codec_id == AV_CODEC_ID_H264) {
|
yading@11
|
150 ff_avc_write_annexb_extradata(ptr, &ptr, &size);
|
yading@11
|
151 if (!ptr)
|
yading@11
|
152 ptr = codec->extradata;
|
yading@11
|
153 }
|
yading@11
|
154 if (!ptr)
|
yading@11
|
155 return;
|
yading@11
|
156 os->private_str = av_mallocz(2*size + 1);
|
yading@11
|
157 for (i = 0; i < size; i++)
|
yading@11
|
158 snprintf(&os->private_str[2*i], 3, "%02x", ptr[i]);
|
yading@11
|
159 if (ptr != codec->extradata)
|
yading@11
|
160 av_free(ptr);
|
yading@11
|
161 }
|
yading@11
|
162
|
yading@11
|
163 static void ism_free(AVFormatContext *s)
|
yading@11
|
164 {
|
yading@11
|
165 SmoothStreamingContext *c = s->priv_data;
|
yading@11
|
166 int i, j;
|
yading@11
|
167 if (!c->streams)
|
yading@11
|
168 return;
|
yading@11
|
169 for (i = 0; i < s->nb_streams; i++) {
|
yading@11
|
170 OutputStream *os = &c->streams[i];
|
yading@11
|
171 ffurl_close(os->out);
|
yading@11
|
172 ffurl_close(os->out2);
|
yading@11
|
173 ffurl_close(os->tail_out);
|
yading@11
|
174 os->out = os->out2 = os->tail_out = NULL;
|
yading@11
|
175 if (os->ctx && os->ctx_inited)
|
yading@11
|
176 av_write_trailer(os->ctx);
|
yading@11
|
177 if (os->ctx && os->ctx->pb)
|
yading@11
|
178 av_free(os->ctx->pb);
|
yading@11
|
179 if (os->ctx)
|
yading@11
|
180 avformat_free_context(os->ctx);
|
yading@11
|
181 av_free(os->private_str);
|
yading@11
|
182 for (j = 0; j < os->nb_fragments; j++)
|
yading@11
|
183 av_free(os->fragments[j]);
|
yading@11
|
184 av_free(os->fragments);
|
yading@11
|
185 }
|
yading@11
|
186 av_freep(&c->streams);
|
yading@11
|
187 }
|
yading@11
|
188
|
yading@11
|
189 static void output_chunk_list(OutputStream *os, AVIOContext *out, int final, int skip, int window_size)
|
yading@11
|
190 {
|
yading@11
|
191 int removed = 0, i, start = 0;
|
yading@11
|
192 if (os->nb_fragments <= 0)
|
yading@11
|
193 return;
|
yading@11
|
194 if (os->fragments[0]->n > 0)
|
yading@11
|
195 removed = 1;
|
yading@11
|
196 if (final)
|
yading@11
|
197 skip = 0;
|
yading@11
|
198 if (window_size)
|
yading@11
|
199 start = FFMAX(os->nb_fragments - skip - window_size, 0);
|
yading@11
|
200 for (i = start; i < os->nb_fragments - skip; i++) {
|
yading@11
|
201 Fragment *frag = os->fragments[i];
|
yading@11
|
202 if (!final || removed)
|
yading@11
|
203 avio_printf(out, "<c t=\"%"PRIu64"\" d=\"%"PRIu64"\" />\n", frag->start_time, frag->duration);
|
yading@11
|
204 else
|
yading@11
|
205 avio_printf(out, "<c n=\"%d\" d=\"%"PRIu64"\" />\n", frag->n, frag->duration);
|
yading@11
|
206 }
|
yading@11
|
207 }
|
yading@11
|
208
|
yading@11
|
209 static int write_manifest(AVFormatContext *s, int final)
|
yading@11
|
210 {
|
yading@11
|
211 SmoothStreamingContext *c = s->priv_data;
|
yading@11
|
212 AVIOContext *out;
|
yading@11
|
213 char filename[1024];
|
yading@11
|
214 int ret, i, video_chunks = 0, audio_chunks = 0, video_streams = 0, audio_streams = 0;
|
yading@11
|
215 int64_t duration = 0;
|
yading@11
|
216
|
yading@11
|
217 snprintf(filename, sizeof(filename), "%s/Manifest", s->filename);
|
yading@11
|
218 ret = avio_open2(&out, filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
|
yading@11
|
219 if (ret < 0) {
|
yading@11
|
220 av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", filename);
|
yading@11
|
221 return ret;
|
yading@11
|
222 }
|
yading@11
|
223 avio_printf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
|
yading@11
|
224 for (i = 0; i < s->nb_streams; i++) {
|
yading@11
|
225 OutputStream *os = &c->streams[i];
|
yading@11
|
226 if (os->nb_fragments > 0) {
|
yading@11
|
227 Fragment *last = os->fragments[os->nb_fragments - 1];
|
yading@11
|
228 duration = last->start_time + last->duration;
|
yading@11
|
229 }
|
yading@11
|
230 if (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
|
yading@11
|
231 video_chunks = os->nb_fragments;
|
yading@11
|
232 video_streams++;
|
yading@11
|
233 } else {
|
yading@11
|
234 audio_chunks = os->nb_fragments;
|
yading@11
|
235 audio_streams++;
|
yading@11
|
236 }
|
yading@11
|
237 }
|
yading@11
|
238 if (!final) {
|
yading@11
|
239 duration = 0;
|
yading@11
|
240 video_chunks = audio_chunks = 0;
|
yading@11
|
241 }
|
yading@11
|
242 if (c->window_size) {
|
yading@11
|
243 video_chunks = FFMIN(video_chunks, c->window_size);
|
yading@11
|
244 audio_chunks = FFMIN(audio_chunks, c->window_size);
|
yading@11
|
245 }
|
yading@11
|
246 avio_printf(out, "<SmoothStreamingMedia MajorVersion=\"2\" MinorVersion=\"0\" Duration=\"%"PRIu64"\"", duration);
|
yading@11
|
247 if (!final)
|
yading@11
|
248 avio_printf(out, " IsLive=\"true\" LookAheadFragmentCount=\"%d\" DVRWindowLength=\"0\"", c->lookahead_count);
|
yading@11
|
249 avio_printf(out, ">\n");
|
yading@11
|
250 if (c->has_video) {
|
yading@11
|
251 int last = -1, index = 0;
|
yading@11
|
252 avio_printf(out, "<StreamIndex Type=\"video\" QualityLevels=\"%d\" Chunks=\"%d\" Url=\"QualityLevels({bitrate})/Fragments(video={start time})\">\n", video_streams, video_chunks);
|
yading@11
|
253 for (i = 0; i < s->nb_streams; i++) {
|
yading@11
|
254 OutputStream *os = &c->streams[i];
|
yading@11
|
255 if (s->streams[i]->codec->codec_type != AVMEDIA_TYPE_VIDEO)
|
yading@11
|
256 continue;
|
yading@11
|
257 last = i;
|
yading@11
|
258 avio_printf(out, "<QualityLevel Index=\"%d\" Bitrate=\"%d\" FourCC=\"%s\" MaxWidth=\"%d\" MaxHeight=\"%d\" CodecPrivateData=\"%s\" />\n", index, s->streams[i]->codec->bit_rate, os->fourcc, s->streams[i]->codec->width, s->streams[i]->codec->height, os->private_str);
|
yading@11
|
259 index++;
|
yading@11
|
260 }
|
yading@11
|
261 output_chunk_list(&c->streams[last], out, final, c->lookahead_count, c->window_size);
|
yading@11
|
262 avio_printf(out, "</StreamIndex>\n");
|
yading@11
|
263 }
|
yading@11
|
264 if (c->has_audio) {
|
yading@11
|
265 int last = -1, index = 0;
|
yading@11
|
266 avio_printf(out, "<StreamIndex Type=\"audio\" QualityLevels=\"%d\" Chunks=\"%d\" Url=\"QualityLevels({bitrate})/Fragments(audio={start time})\">\n", audio_streams, audio_chunks);
|
yading@11
|
267 for (i = 0; i < s->nb_streams; i++) {
|
yading@11
|
268 OutputStream *os = &c->streams[i];
|
yading@11
|
269 if (s->streams[i]->codec->codec_type != AVMEDIA_TYPE_AUDIO)
|
yading@11
|
270 continue;
|
yading@11
|
271 last = i;
|
yading@11
|
272 avio_printf(out, "<QualityLevel Index=\"%d\" Bitrate=\"%d\" FourCC=\"%s\" SamplingRate=\"%d\" Channels=\"%d\" BitsPerSample=\"16\" PacketSize=\"%d\" AudioTag=\"%d\" CodecPrivateData=\"%s\" />\n", index, s->streams[i]->codec->bit_rate, os->fourcc, s->streams[i]->codec->sample_rate, s->streams[i]->codec->channels, os->packet_size, os->audio_tag, os->private_str);
|
yading@11
|
273 index++;
|
yading@11
|
274 }
|
yading@11
|
275 output_chunk_list(&c->streams[last], out, final, c->lookahead_count, c->window_size);
|
yading@11
|
276 avio_printf(out, "</StreamIndex>\n");
|
yading@11
|
277 }
|
yading@11
|
278 avio_printf(out, "</SmoothStreamingMedia>\n");
|
yading@11
|
279 avio_flush(out);
|
yading@11
|
280 avio_close(out);
|
yading@11
|
281 return 0;
|
yading@11
|
282 }
|
yading@11
|
283
|
yading@11
|
284 static int ism_write_header(AVFormatContext *s)
|
yading@11
|
285 {
|
yading@11
|
286 SmoothStreamingContext *c = s->priv_data;
|
yading@11
|
287 int ret = 0, i;
|
yading@11
|
288 AVOutputFormat *oformat;
|
yading@11
|
289
|
yading@11
|
290 if (mkdir(s->filename, 0777) < 0) {
|
yading@11
|
291 av_log(s, AV_LOG_ERROR, "mkdir failed\n");
|
yading@11
|
292 ret = AVERROR(errno);
|
yading@11
|
293 goto fail;
|
yading@11
|
294 }
|
yading@11
|
295
|
yading@11
|
296 oformat = av_guess_format("ismv", NULL, NULL);
|
yading@11
|
297 if (!oformat) {
|
yading@11
|
298 ret = AVERROR_MUXER_NOT_FOUND;
|
yading@11
|
299 goto fail;
|
yading@11
|
300 }
|
yading@11
|
301
|
yading@11
|
302 c->streams = av_mallocz(sizeof(*c->streams) * s->nb_streams);
|
yading@11
|
303 if (!c->streams) {
|
yading@11
|
304 ret = AVERROR(ENOMEM);
|
yading@11
|
305 goto fail;
|
yading@11
|
306 }
|
yading@11
|
307
|
yading@11
|
308 for (i = 0; i < s->nb_streams; i++) {
|
yading@11
|
309 OutputStream *os = &c->streams[i];
|
yading@11
|
310 AVFormatContext *ctx;
|
yading@11
|
311 AVStream *st;
|
yading@11
|
312 AVDictionary *opts = NULL;
|
yading@11
|
313 char buf[10];
|
yading@11
|
314
|
yading@11
|
315 if (!s->streams[i]->codec->bit_rate) {
|
yading@11
|
316 av_log(s, AV_LOG_ERROR, "No bit rate set for stream %d\n", i);
|
yading@11
|
317 ret = AVERROR(EINVAL);
|
yading@11
|
318 goto fail;
|
yading@11
|
319 }
|
yading@11
|
320 snprintf(os->dirname, sizeof(os->dirname), "%s/QualityLevels(%d)", s->filename, s->streams[i]->codec->bit_rate);
|
yading@11
|
321 if (mkdir(os->dirname, 0777) < 0) {
|
yading@11
|
322 ret = AVERROR(errno);
|
yading@11
|
323 av_log(s, AV_LOG_ERROR, "mkdir failed\n");
|
yading@11
|
324 goto fail;
|
yading@11
|
325 }
|
yading@11
|
326
|
yading@11
|
327 ctx = avformat_alloc_context();
|
yading@11
|
328 if (!ctx) {
|
yading@11
|
329 ret = AVERROR(ENOMEM);
|
yading@11
|
330 goto fail;
|
yading@11
|
331 }
|
yading@11
|
332 os->ctx = ctx;
|
yading@11
|
333 ctx->oformat = oformat;
|
yading@11
|
334 ctx->interrupt_callback = s->interrupt_callback;
|
yading@11
|
335
|
yading@11
|
336 if (!(st = avformat_new_stream(ctx, NULL))) {
|
yading@11
|
337 ret = AVERROR(ENOMEM);
|
yading@11
|
338 goto fail;
|
yading@11
|
339 }
|
yading@11
|
340 avcodec_copy_context(st->codec, s->streams[i]->codec);
|
yading@11
|
341 st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
|
yading@11
|
342
|
yading@11
|
343 ctx->pb = avio_alloc_context(os->iobuf, sizeof(os->iobuf), AVIO_FLAG_WRITE, os, NULL, ism_write, ism_seek);
|
yading@11
|
344 if (!ctx->pb) {
|
yading@11
|
345 ret = AVERROR(ENOMEM);
|
yading@11
|
346 goto fail;
|
yading@11
|
347 }
|
yading@11
|
348
|
yading@11
|
349 snprintf(buf, sizeof(buf), "%d", c->lookahead_count);
|
yading@11
|
350 av_dict_set(&opts, "ism_lookahead", buf, 0);
|
yading@11
|
351 av_dict_set(&opts, "movflags", "frag_custom", 0);
|
yading@11
|
352 if ((ret = avformat_write_header(ctx, &opts)) < 0) {
|
yading@11
|
353 goto fail;
|
yading@11
|
354 }
|
yading@11
|
355 os->ctx_inited = 1;
|
yading@11
|
356 avio_flush(ctx->pb);
|
yading@11
|
357 av_dict_free(&opts);
|
yading@11
|
358 s->streams[i]->time_base = st->time_base;
|
yading@11
|
359 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
|
yading@11
|
360 c->has_video = 1;
|
yading@11
|
361 os->stream_type_tag = "video";
|
yading@11
|
362 if (st->codec->codec_id == AV_CODEC_ID_H264) {
|
yading@11
|
363 os->fourcc = "H264";
|
yading@11
|
364 } else if (st->codec->codec_id == AV_CODEC_ID_VC1) {
|
yading@11
|
365 os->fourcc = "WVC1";
|
yading@11
|
366 } else {
|
yading@11
|
367 av_log(s, AV_LOG_ERROR, "Unsupported video codec\n");
|
yading@11
|
368 ret = AVERROR(EINVAL);
|
yading@11
|
369 goto fail;
|
yading@11
|
370 }
|
yading@11
|
371 } else {
|
yading@11
|
372 c->has_audio = 1;
|
yading@11
|
373 os->stream_type_tag = "audio";
|
yading@11
|
374 if (st->codec->codec_id == AV_CODEC_ID_AAC) {
|
yading@11
|
375 os->fourcc = "AACL";
|
yading@11
|
376 os->audio_tag = 0xff;
|
yading@11
|
377 } else if (st->codec->codec_id == AV_CODEC_ID_WMAPRO) {
|
yading@11
|
378 os->fourcc = "WMAP";
|
yading@11
|
379 os->audio_tag = 0x0162;
|
yading@11
|
380 } else {
|
yading@11
|
381 av_log(s, AV_LOG_ERROR, "Unsupported audio codec\n");
|
yading@11
|
382 ret = AVERROR(EINVAL);
|
yading@11
|
383 goto fail;
|
yading@11
|
384 }
|
yading@11
|
385 os->packet_size = st->codec->block_align ? st->codec->block_align : 4;
|
yading@11
|
386 }
|
yading@11
|
387 get_private_data(os);
|
yading@11
|
388 }
|
yading@11
|
389
|
yading@11
|
390 if (!c->has_video && c->min_frag_duration <= 0) {
|
yading@11
|
391 av_log(s, AV_LOG_WARNING, "no video stream and no min frag duration set\n");
|
yading@11
|
392 ret = AVERROR(EINVAL);
|
yading@11
|
393 }
|
yading@11
|
394 ret = write_manifest(s, 0);
|
yading@11
|
395
|
yading@11
|
396 fail:
|
yading@11
|
397 if (ret)
|
yading@11
|
398 ism_free(s);
|
yading@11
|
399 return ret;
|
yading@11
|
400 }
|
yading@11
|
401
|
yading@11
|
402 static int parse_fragment(AVFormatContext *s, const char *filename, int64_t *start_ts, int64_t *duration, int64_t *moof_size, int64_t size)
|
yading@11
|
403 {
|
yading@11
|
404 AVIOContext *in;
|
yading@11
|
405 int ret;
|
yading@11
|
406 uint32_t len;
|
yading@11
|
407 if ((ret = avio_open2(&in, filename, AVIO_FLAG_READ, &s->interrupt_callback, NULL)) < 0)
|
yading@11
|
408 return ret;
|
yading@11
|
409 ret = AVERROR(EIO);
|
yading@11
|
410 *moof_size = avio_rb32(in);
|
yading@11
|
411 if (*moof_size < 8 || *moof_size > size)
|
yading@11
|
412 goto fail;
|
yading@11
|
413 if (avio_rl32(in) != MKTAG('m','o','o','f'))
|
yading@11
|
414 goto fail;
|
yading@11
|
415 len = avio_rb32(in);
|
yading@11
|
416 if (len > *moof_size)
|
yading@11
|
417 goto fail;
|
yading@11
|
418 if (avio_rl32(in) != MKTAG('m','f','h','d'))
|
yading@11
|
419 goto fail;
|
yading@11
|
420 avio_seek(in, len - 8, SEEK_CUR);
|
yading@11
|
421 avio_rb32(in); /* traf size */
|
yading@11
|
422 if (avio_rl32(in) != MKTAG('t','r','a','f'))
|
yading@11
|
423 goto fail;
|
yading@11
|
424 while (avio_tell(in) < *moof_size) {
|
yading@11
|
425 uint32_t len = avio_rb32(in);
|
yading@11
|
426 uint32_t tag = avio_rl32(in);
|
yading@11
|
427 int64_t end = avio_tell(in) + len - 8;
|
yading@11
|
428 if (len < 8 || len >= *moof_size)
|
yading@11
|
429 goto fail;
|
yading@11
|
430 if (tag == MKTAG('u','u','i','d')) {
|
yading@11
|
431 const uint8_t tfxd[] = {
|
yading@11
|
432 0x6d, 0x1d, 0x9b, 0x05, 0x42, 0xd5, 0x44, 0xe6,
|
yading@11
|
433 0x80, 0xe2, 0x14, 0x1d, 0xaf, 0xf7, 0x57, 0xb2
|
yading@11
|
434 };
|
yading@11
|
435 uint8_t uuid[16];
|
yading@11
|
436 avio_read(in, uuid, 16);
|
yading@11
|
437 if (!memcmp(uuid, tfxd, 16) && len >= 8 + 16 + 4 + 16) {
|
yading@11
|
438 avio_seek(in, 4, SEEK_CUR);
|
yading@11
|
439 *start_ts = avio_rb64(in);
|
yading@11
|
440 *duration = avio_rb64(in);
|
yading@11
|
441 ret = 0;
|
yading@11
|
442 break;
|
yading@11
|
443 }
|
yading@11
|
444 }
|
yading@11
|
445 avio_seek(in, end, SEEK_SET);
|
yading@11
|
446 }
|
yading@11
|
447 fail:
|
yading@11
|
448 avio_close(in);
|
yading@11
|
449 return ret;
|
yading@11
|
450 }
|
yading@11
|
451
|
yading@11
|
452 static int add_fragment(OutputStream *os, const char *file, const char *infofile, int64_t start_time, int64_t duration, int64_t start_pos, int64_t size)
|
yading@11
|
453 {
|
yading@11
|
454 Fragment *frag;
|
yading@11
|
455 if (os->nb_fragments >= os->fragments_size) {
|
yading@11
|
456 os->fragments_size = (os->fragments_size + 1) * 2;
|
yading@11
|
457 os->fragments = av_realloc(os->fragments, sizeof(*os->fragments)*os->fragments_size);
|
yading@11
|
458 if (!os->fragments)
|
yading@11
|
459 return AVERROR(ENOMEM);
|
yading@11
|
460 }
|
yading@11
|
461 frag = av_mallocz(sizeof(*frag));
|
yading@11
|
462 if (!frag)
|
yading@11
|
463 return AVERROR(ENOMEM);
|
yading@11
|
464 av_strlcpy(frag->file, file, sizeof(frag->file));
|
yading@11
|
465 av_strlcpy(frag->infofile, infofile, sizeof(frag->infofile));
|
yading@11
|
466 frag->start_time = start_time;
|
yading@11
|
467 frag->duration = duration;
|
yading@11
|
468 frag->start_pos = start_pos;
|
yading@11
|
469 frag->size = size;
|
yading@11
|
470 frag->n = os->fragment_index;
|
yading@11
|
471 os->fragments[os->nb_fragments++] = frag;
|
yading@11
|
472 os->fragment_index++;
|
yading@11
|
473 return 0;
|
yading@11
|
474 }
|
yading@11
|
475
|
yading@11
|
476 static int copy_moof(AVFormatContext *s, const char* infile, const char *outfile, int64_t size)
|
yading@11
|
477 {
|
yading@11
|
478 AVIOContext *in, *out;
|
yading@11
|
479 int ret = 0;
|
yading@11
|
480 if ((ret = avio_open2(&in, infile, AVIO_FLAG_READ, &s->interrupt_callback, NULL)) < 0)
|
yading@11
|
481 return ret;
|
yading@11
|
482 if ((ret = avio_open2(&out, outfile, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL)) < 0) {
|
yading@11
|
483 avio_close(in);
|
yading@11
|
484 return ret;
|
yading@11
|
485 }
|
yading@11
|
486 while (size > 0) {
|
yading@11
|
487 uint8_t buf[8192];
|
yading@11
|
488 int n = FFMIN(size, sizeof(buf));
|
yading@11
|
489 n = avio_read(in, buf, n);
|
yading@11
|
490 if (n <= 0) {
|
yading@11
|
491 ret = AVERROR(EIO);
|
yading@11
|
492 break;
|
yading@11
|
493 }
|
yading@11
|
494 avio_write(out, buf, n);
|
yading@11
|
495 size -= n;
|
yading@11
|
496 }
|
yading@11
|
497 avio_flush(out);
|
yading@11
|
498 avio_close(out);
|
yading@11
|
499 avio_close(in);
|
yading@11
|
500 return ret;
|
yading@11
|
501 }
|
yading@11
|
502
|
yading@11
|
503 static int ism_flush(AVFormatContext *s, int final)
|
yading@11
|
504 {
|
yading@11
|
505 SmoothStreamingContext *c = s->priv_data;
|
yading@11
|
506 int i, ret = 0;
|
yading@11
|
507
|
yading@11
|
508 for (i = 0; i < s->nb_streams; i++) {
|
yading@11
|
509 OutputStream *os = &c->streams[i];
|
yading@11
|
510 char filename[1024], target_filename[1024], header_filename[1024];
|
yading@11
|
511 int64_t start_pos = os->tail_pos, size;
|
yading@11
|
512 int64_t start_ts, duration, moof_size;
|
yading@11
|
513 if (!os->packets_written)
|
yading@11
|
514 continue;
|
yading@11
|
515
|
yading@11
|
516 snprintf(filename, sizeof(filename), "%s/temp", os->dirname);
|
yading@11
|
517 ret = ffurl_open(&os->out, filename, AVIO_FLAG_WRITE, &s->interrupt_callback, NULL);
|
yading@11
|
518 if (ret < 0)
|
yading@11
|
519 break;
|
yading@11
|
520 os->cur_start_pos = os->tail_pos;
|
yading@11
|
521 av_write_frame(os->ctx, NULL);
|
yading@11
|
522 avio_flush(os->ctx->pb);
|
yading@11
|
523 os->packets_written = 0;
|
yading@11
|
524 if (!os->out || os->tail_out)
|
yading@11
|
525 return AVERROR(EIO);
|
yading@11
|
526
|
yading@11
|
527 ffurl_close(os->out);
|
yading@11
|
528 os->out = NULL;
|
yading@11
|
529 size = os->tail_pos - start_pos;
|
yading@11
|
530 if ((ret = parse_fragment(s, filename, &start_ts, &duration, &moof_size, size)) < 0)
|
yading@11
|
531 break;
|
yading@11
|
532 snprintf(header_filename, sizeof(header_filename), "%s/FragmentInfo(%s=%"PRIu64")", os->dirname, os->stream_type_tag, start_ts);
|
yading@11
|
533 snprintf(target_filename, sizeof(target_filename), "%s/Fragments(%s=%"PRIu64")", os->dirname, os->stream_type_tag, start_ts);
|
yading@11
|
534 copy_moof(s, filename, header_filename, moof_size);
|
yading@11
|
535 rename(filename, target_filename);
|
yading@11
|
536 add_fragment(os, target_filename, header_filename, start_ts, duration, start_pos, size);
|
yading@11
|
537 }
|
yading@11
|
538
|
yading@11
|
539 if (c->window_size || (final && c->remove_at_exit)) {
|
yading@11
|
540 for (i = 0; i < s->nb_streams; i++) {
|
yading@11
|
541 OutputStream *os = &c->streams[i];
|
yading@11
|
542 int j;
|
yading@11
|
543 int remove = os->nb_fragments - c->window_size - c->extra_window_size - c->lookahead_count;
|
yading@11
|
544 if (final && c->remove_at_exit)
|
yading@11
|
545 remove = os->nb_fragments;
|
yading@11
|
546 if (remove > 0) {
|
yading@11
|
547 for (j = 0; j < remove; j++) {
|
yading@11
|
548 unlink(os->fragments[j]->file);
|
yading@11
|
549 unlink(os->fragments[j]->infofile);
|
yading@11
|
550 av_free(os->fragments[j]);
|
yading@11
|
551 }
|
yading@11
|
552 os->nb_fragments -= remove;
|
yading@11
|
553 memmove(os->fragments, os->fragments + remove, os->nb_fragments * sizeof(*os->fragments));
|
yading@11
|
554 }
|
yading@11
|
555 if (final && c->remove_at_exit)
|
yading@11
|
556 rmdir(os->dirname);
|
yading@11
|
557 }
|
yading@11
|
558 }
|
yading@11
|
559
|
yading@11
|
560 if (ret >= 0)
|
yading@11
|
561 ret = write_manifest(s, final);
|
yading@11
|
562 return ret;
|
yading@11
|
563 }
|
yading@11
|
564
|
yading@11
|
565 static int ism_write_packet(AVFormatContext *s, AVPacket *pkt)
|
yading@11
|
566 {
|
yading@11
|
567 SmoothStreamingContext *c = s->priv_data;
|
yading@11
|
568 AVStream *st = s->streams[pkt->stream_index];
|
yading@11
|
569 OutputStream *os = &c->streams[pkt->stream_index];
|
yading@11
|
570 int64_t end_dts = (c->nb_fragments + 1LL) * c->min_frag_duration;
|
yading@11
|
571 int ret;
|
yading@11
|
572
|
yading@11
|
573 if (st->first_dts == AV_NOPTS_VALUE)
|
yading@11
|
574 st->first_dts = pkt->dts;
|
yading@11
|
575
|
yading@11
|
576 if ((!c->has_video || st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
|
yading@11
|
577 av_compare_ts(pkt->dts - st->first_dts, st->time_base,
|
yading@11
|
578 end_dts, AV_TIME_BASE_Q) >= 0 &&
|
yading@11
|
579 pkt->flags & AV_PKT_FLAG_KEY && os->packets_written) {
|
yading@11
|
580
|
yading@11
|
581 if ((ret = ism_flush(s, 0)) < 0)
|
yading@11
|
582 return ret;
|
yading@11
|
583 c->nb_fragments++;
|
yading@11
|
584 }
|
yading@11
|
585
|
yading@11
|
586 os->packets_written++;
|
yading@11
|
587 return ff_write_chained(os->ctx, 0, pkt, s);
|
yading@11
|
588 }
|
yading@11
|
589
|
yading@11
|
590 static int ism_write_trailer(AVFormatContext *s)
|
yading@11
|
591 {
|
yading@11
|
592 SmoothStreamingContext *c = s->priv_data;
|
yading@11
|
593 ism_flush(s, 1);
|
yading@11
|
594
|
yading@11
|
595 if (c->remove_at_exit) {
|
yading@11
|
596 char filename[1024];
|
yading@11
|
597 snprintf(filename, sizeof(filename), "%s/Manifest", s->filename);
|
yading@11
|
598 unlink(filename);
|
yading@11
|
599 rmdir(s->filename);
|
yading@11
|
600 }
|
yading@11
|
601
|
yading@11
|
602 ism_free(s);
|
yading@11
|
603 return 0;
|
yading@11
|
604 }
|
yading@11
|
605
|
yading@11
|
606 #define OFFSET(x) offsetof(SmoothStreamingContext, x)
|
yading@11
|
607 #define E AV_OPT_FLAG_ENCODING_PARAM
|
yading@11
|
608 static const AVOption options[] = {
|
yading@11
|
609 { "window_size", "number of fragments kept in the manifest", OFFSET(window_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, E },
|
yading@11
|
610 { "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 },
|
yading@11
|
611 { "lookahead_count", "number of lookahead fragments", OFFSET(lookahead_count), AV_OPT_TYPE_INT, { .i64 = 2 }, 0, INT_MAX, E },
|
yading@11
|
612 { "min_frag_duration", "minimum fragment duration (in microseconds)", OFFSET(min_frag_duration), AV_OPT_TYPE_INT64, { .i64 = 5000000 }, 0, INT_MAX, E },
|
yading@11
|
613 { "remove_at_exit", "remove all fragments when finished", OFFSET(remove_at_exit), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
|
yading@11
|
614 { NULL },
|
yading@11
|
615 };
|
yading@11
|
616
|
yading@11
|
617 static const AVClass ism_class = {
|
yading@11
|
618 .class_name = "smooth streaming muxer",
|
yading@11
|
619 .item_name = av_default_item_name,
|
yading@11
|
620 .option = options,
|
yading@11
|
621 .version = LIBAVUTIL_VERSION_INT,
|
yading@11
|
622 };
|
yading@11
|
623
|
yading@11
|
624
|
yading@11
|
625 AVOutputFormat ff_smoothstreaming_muxer = {
|
yading@11
|
626 .name = "smoothstreaming",
|
yading@11
|
627 .long_name = NULL_IF_CONFIG_SMALL("Smooth Streaming Muxer"),
|
yading@11
|
628 .priv_data_size = sizeof(SmoothStreamingContext),
|
yading@11
|
629 .audio_codec = AV_CODEC_ID_AAC,
|
yading@11
|
630 .video_codec = AV_CODEC_ID_H264,
|
yading@11
|
631 .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
|
yading@11
|
632 .write_header = ism_write_header,
|
yading@11
|
633 .write_packet = ism_write_packet,
|
yading@11
|
634 .write_trailer = ism_write_trailer,
|
yading@11
|
635 .codec_tag = (const AVCodecTag* const []){ ff_mp4_obj_type, 0 },
|
yading@11
|
636 .priv_class = &ism_class,
|
yading@11
|
637 };
|