* qatar/master:
Add an HDS live fragmenting muxer
Conflicts:
Changelog
libavformat/version.h
Merged-by: Michael Niedermayer <michaelni@gmx.at>
... | ... |
@@ -2079,6 +2079,7 @@ dxa_demuxer_select="riffdec" |
2079 | 2079 |
eac3_demuxer_select="ac3_parser" |
2080 | 2080 |
f4v_muxer_select="mov_muxer" |
2081 | 2081 |
flac_demuxer_select="flac_parser" |
2082 |
+hds_muxer_select="flv_muxer" |
|
2082 | 2083 |
hls_muxer_select="mpegts_muxer" |
2083 | 2084 |
ipod_muxer_select="mov_muxer" |
2084 | 2085 |
ismv_muxer_select="mov_muxer" |
... | ... |
@@ -166,6 +166,7 @@ OBJS-$(CONFIG_H263_DEMUXER) += h263dec.o rawdec.o |
166 | 166 |
OBJS-$(CONFIG_H263_MUXER) += rawenc.o |
167 | 167 |
OBJS-$(CONFIG_H264_DEMUXER) += h264dec.o rawdec.o |
168 | 168 |
OBJS-$(CONFIG_H264_MUXER) += rawenc.o |
169 |
+OBJS-$(CONFIG_HDS_MUXER) += hdsenc.o |
|
169 | 170 |
OBJS-$(CONFIG_HEVC_DEMUXER) += hevcdec.o rawdec.o |
170 | 171 |
OBJS-$(CONFIG_HLS_DEMUXER) += hls.o |
171 | 172 |
OBJS-$(CONFIG_HLS_MUXER) += hlsenc.o |
... | ... |
@@ -134,6 +134,7 @@ void av_register_all(void) |
134 | 134 |
REGISTER_MUXDEMUX(H261, h261); |
135 | 135 |
REGISTER_MUXDEMUX(H263, h263); |
136 | 136 |
REGISTER_MUXDEMUX(H264, h264); |
137 |
+ REGISTER_MUXER (HDS, hds); |
|
137 | 138 |
REGISTER_DEMUXER (HEVC, hevc); |
138 | 139 |
REGISTER_MUXDEMUX(HLS, hls); |
139 | 140 |
REGISTER_DEMUXER (HNM, hnm); |
140 | 141 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,589 @@ |
0 |
+/* |
|
1 |
+ * Live HDS fragmenter |
|
2 |
+ * Copyright (c) 2013 Martin Storsjo |
|
3 |
+ * |
|
4 |
+ * This file is part of FFmpeg. |
|
5 |
+ * |
|
6 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
7 |
+ * modify it under the terms of the GNU Lesser General Public |
|
8 |
+ * License as published by the Free Software Foundation; either |
|
9 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
10 |
+ * |
|
11 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
12 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
+ * Lesser General Public License for more details. |
|
15 |
+ * |
|
16 |
+ * You should have received a copy of the GNU Lesser General Public |
|
17 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
18 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
19 |
+ */ |
|
20 |
+ |
|
21 |
+#include "config.h" |
|
22 |
+#include <float.h> |
|
23 |
+#if HAVE_UNISTD_H |
|
24 |
+#include <unistd.h> |
|
25 |
+#endif |
|
26 |
+ |
|
27 |
+#include "avformat.h" |
|
28 |
+#include "internal.h" |
|
29 |
+#include "os_support.h" |
|
30 |
+ |
|
31 |
+#include "libavutil/avstring.h" |
|
32 |
+#include "libavutil/base64.h" |
|
33 |
+#include "libavutil/intreadwrite.h" |
|
34 |
+#include "libavutil/mathematics.h" |
|
35 |
+#include "libavutil/opt.h" |
|
36 |
+ |
|
37 |
+typedef struct Fragment { |
|
38 |
+ char file[1024]; |
|
39 |
+ int64_t start_time, duration; |
|
40 |
+ int n; |
|
41 |
+} Fragment; |
|
42 |
+ |
|
43 |
+typedef struct OutputStream { |
|
44 |
+ int bitrate; |
|
45 |
+ int first_stream; |
|
46 |
+ AVFormatContext *ctx; |
|
47 |
+ int ctx_inited; |
|
48 |
+ uint8_t iobuf[32768]; |
|
49 |
+ char temp_filename[1024]; |
|
50 |
+ int64_t frag_start_ts, last_ts; |
|
51 |
+ AVIOContext *out; |
|
52 |
+ int packets_written; |
|
53 |
+ int nb_fragments, fragments_size, fragment_index; |
|
54 |
+ Fragment **fragments; |
|
55 |
+ |
|
56 |
+ int has_audio, has_video; |
|
57 |
+ |
|
58 |
+ uint8_t *metadata; |
|
59 |
+ int metadata_size; |
|
60 |
+ |
|
61 |
+ uint8_t *extra_packets[2]; |
|
62 |
+ int extra_packet_sizes[2]; |
|
63 |
+ int nb_extra_packets; |
|
64 |
+} OutputStream; |
|
65 |
+ |
|
66 |
+typedef struct HDSContext { |
|
67 |
+ const AVClass *class; /* Class for private options. */ |
|
68 |
+ int window_size; |
|
69 |
+ int extra_window_size; |
|
70 |
+ int min_frag_duration; |
|
71 |
+ int remove_at_exit; |
|
72 |
+ |
|
73 |
+ OutputStream *streams; |
|
74 |
+ int nb_streams; |
|
75 |
+} HDSContext; |
|
76 |
+ |
|
77 |
+static int parse_header(OutputStream *os, const uint8_t *buf, int buf_size) |
|
78 |
+{ |
|
79 |
+ if (buf_size < 13) |
|
80 |
+ return AVERROR_INVALIDDATA; |
|
81 |
+ if (memcmp(buf, "FLV", 3)) |
|
82 |
+ return AVERROR_INVALIDDATA; |
|
83 |
+ buf += 13; |
|
84 |
+ buf_size -= 13; |
|
85 |
+ while (buf_size >= 11 + 4) { |
|
86 |
+ int type = buf[0]; |
|
87 |
+ int size = AV_RB24(&buf[1]) + 11 + 4; |
|
88 |
+ if (size > buf_size) |
|
89 |
+ return AVERROR_INVALIDDATA; |
|
90 |
+ if (type == 8 || type == 9) { |
|
91 |
+ if (os->nb_extra_packets > FF_ARRAY_ELEMS(os->extra_packets)) |
|
92 |
+ return AVERROR_INVALIDDATA; |
|
93 |
+ os->extra_packet_sizes[os->nb_extra_packets] = size; |
|
94 |
+ os->extra_packets[os->nb_extra_packets] = av_malloc(size); |
|
95 |
+ if (!os->extra_packets[os->nb_extra_packets]) |
|
96 |
+ return AVERROR(ENOMEM); |
|
97 |
+ memcpy(os->extra_packets[os->nb_extra_packets], buf, size); |
|
98 |
+ os->nb_extra_packets++; |
|
99 |
+ } else if (type == 0x12) { |
|
100 |
+ if (os->metadata) |
|
101 |
+ return AVERROR_INVALIDDATA; |
|
102 |
+ os->metadata_size = size - 11 - 4; |
|
103 |
+ os->metadata = av_malloc(os->metadata_size); |
|
104 |
+ if (!os->metadata) |
|
105 |
+ return AVERROR(ENOMEM); |
|
106 |
+ memcpy(os->metadata, buf + 11, os->metadata_size); |
|
107 |
+ } |
|
108 |
+ buf += size; |
|
109 |
+ buf_size -= size; |
|
110 |
+ } |
|
111 |
+ if (!os->metadata) |
|
112 |
+ return AVERROR_INVALIDDATA; |
|
113 |
+ return 0; |
|
114 |
+} |
|
115 |
+ |
|
116 |
+static int hds_write(void *opaque, uint8_t *buf, int buf_size) |
|
117 |
+{ |
|
118 |
+ OutputStream *os = opaque; |
|
119 |
+ if (os->out) { |
|
120 |
+ avio_write(os->out, buf, buf_size); |
|
121 |
+ } else { |
|
122 |
+ if (!os->metadata_size) { |
|
123 |
+ int ret; |
|
124 |
+ // Assuming the IO buffer is large enough to fit the |
|
125 |
+ // FLV header and all metadata and extradata packets |
|
126 |
+ if ((ret = parse_header(os, buf, buf_size)) < 0) |
|
127 |
+ return ret; |
|
128 |
+ } |
|
129 |
+ } |
|
130 |
+ return buf_size; |
|
131 |
+} |
|
132 |
+ |
|
133 |
+static void hds_free(AVFormatContext *s) |
|
134 |
+{ |
|
135 |
+ HDSContext *c = s->priv_data; |
|
136 |
+ int i, j; |
|
137 |
+ if (!c->streams) |
|
138 |
+ return; |
|
139 |
+ for (i = 0; i < s->nb_streams; i++) { |
|
140 |
+ OutputStream *os = &c->streams[i]; |
|
141 |
+ if (os->out) |
|
142 |
+ avio_close(os->out); |
|
143 |
+ os->out = NULL; |
|
144 |
+ if (os->ctx && os->ctx_inited) |
|
145 |
+ av_write_trailer(os->ctx); |
|
146 |
+ if (os->ctx && os->ctx->pb) |
|
147 |
+ av_free(os->ctx->pb); |
|
148 |
+ if (os->ctx) |
|
149 |
+ avformat_free_context(os->ctx); |
|
150 |
+ av_free(os->metadata); |
|
151 |
+ for (j = 0; j < os->nb_extra_packets; j++) |
|
152 |
+ av_free(os->extra_packets[j]); |
|
153 |
+ for (j = 0; j < os->nb_fragments; j++) |
|
154 |
+ av_free(os->fragments[j]); |
|
155 |
+ av_free(os->fragments); |
|
156 |
+ } |
|
157 |
+ av_freep(&c->streams); |
|
158 |
+} |
|
159 |
+ |
|
160 |
+static int write_manifest(AVFormatContext *s, int final) |
|
161 |
+{ |
|
162 |
+ HDSContext *c = s->priv_data; |
|
163 |
+ AVIOContext *out; |
|
164 |
+ char filename[1024], temp_filename[1024]; |
|
165 |
+ int ret, i; |
|
166 |
+ float duration = 0; |
|
167 |
+ |
|
168 |
+ if (c->nb_streams > 0) |
|
169 |
+ duration = c->streams[0].last_ts * av_q2d(s->streams[0]->time_base); |
|
170 |
+ |
|
171 |
+ snprintf(filename, sizeof(filename), "%s/index.f4m", s->filename); |
|
172 |
+ snprintf(temp_filename, sizeof(temp_filename), "%s/index.f4m.tmp", s->filename); |
|
173 |
+ ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE, |
|
174 |
+ &s->interrupt_callback, NULL); |
|
175 |
+ if (ret < 0) { |
|
176 |
+ av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", filename); |
|
177 |
+ return ret; |
|
178 |
+ } |
|
179 |
+ avio_printf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); |
|
180 |
+ avio_printf(out, "<manifest xmlns=\"http://ns.adobe.com/f4m/1.0\">\n"); |
|
181 |
+ avio_printf(out, "\t<id>%s</id>\n", av_basename(s->filename)); |
|
182 |
+ avio_printf(out, "\t<streamType>%s</streamType>\n", |
|
183 |
+ final ? "recorded" : "live"); |
|
184 |
+ avio_printf(out, "\t<deliveryType>streaming</deliveryType>\n"); |
|
185 |
+ if (final) |
|
186 |
+ avio_printf(out, "\t<duration>%f</duration>\n", duration); |
|
187 |
+ for (i = 0; i < c->nb_streams; i++) { |
|
188 |
+ OutputStream *os = &c->streams[i]; |
|
189 |
+ int b64_size = AV_BASE64_SIZE(os->metadata_size); |
|
190 |
+ char *base64 = av_malloc(b64_size); |
|
191 |
+ if (!base64) { |
|
192 |
+ avio_close(out); |
|
193 |
+ return AVERROR(ENOMEM); |
|
194 |
+ } |
|
195 |
+ av_base64_encode(base64, b64_size, os->metadata, os->metadata_size); |
|
196 |
+ |
|
197 |
+ avio_printf(out, "\t<bootstrapInfo profile=\"named\" url=\"stream%d.abst\" id=\"bootstrap%d\" />\n", i, i); |
|
198 |
+ avio_printf(out, "\t<media bitrate=\"%d\" url=\"stream%d\" bootstrapInfoId=\"bootstrap%d\">\n", os->bitrate/1000, i, i); |
|
199 |
+ avio_printf(out, "\t\t<metadata>%s</metadata>\n", base64); |
|
200 |
+ avio_printf(out, "\t</media>\n"); |
|
201 |
+ av_free(base64); |
|
202 |
+ } |
|
203 |
+ avio_printf(out, "</manifest>\n"); |
|
204 |
+ avio_flush(out); |
|
205 |
+ avio_close(out); |
|
206 |
+ rename(temp_filename, filename); |
|
207 |
+ return 0; |
|
208 |
+} |
|
209 |
+ |
|
210 |
+static void update_size(AVIOContext *out, int64_t pos) |
|
211 |
+{ |
|
212 |
+ int64_t end = avio_tell(out); |
|
213 |
+ avio_seek(out, pos, SEEK_SET); |
|
214 |
+ avio_wb32(out, end - pos); |
|
215 |
+ avio_seek(out, end, SEEK_SET); |
|
216 |
+} |
|
217 |
+ |
|
218 |
+/* Note, the .abst files need to be served with the "binary/octet" |
|
219 |
+ * mime type, otherwise at least the OSMF player can easily fail |
|
220 |
+ * with "stream not found" when polling for the next fragment. */ |
|
221 |
+static int write_abst(AVFormatContext *s, OutputStream *os, int final) |
|
222 |
+{ |
|
223 |
+ HDSContext *c = s->priv_data; |
|
224 |
+ AVIOContext *out; |
|
225 |
+ char filename[1024], temp_filename[1024]; |
|
226 |
+ int i, ret; |
|
227 |
+ int64_t asrt_pos, afrt_pos; |
|
228 |
+ int start = 0, fragments; |
|
229 |
+ int index = s->streams[os->first_stream]->id; |
|
230 |
+ int64_t cur_media_time = 0; |
|
231 |
+ if (c->window_size) |
|
232 |
+ start = FFMAX(os->nb_fragments - c->window_size, 0); |
|
233 |
+ fragments = os->nb_fragments - start; |
|
234 |
+ if (final) |
|
235 |
+ cur_media_time = os->last_ts; |
|
236 |
+ else if (os->nb_fragments) |
|
237 |
+ cur_media_time = os->fragments[os->nb_fragments - 1]->start_time; |
|
238 |
+ |
|
239 |
+ snprintf(filename, sizeof(filename), |
|
240 |
+ "%s/stream%d.abst", s->filename, index); |
|
241 |
+ snprintf(temp_filename, sizeof(temp_filename), |
|
242 |
+ "%s/stream%d.abst.tmp", s->filename, index); |
|
243 |
+ ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE, |
|
244 |
+ &s->interrupt_callback, NULL); |
|
245 |
+ if (ret < 0) { |
|
246 |
+ av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename); |
|
247 |
+ return ret; |
|
248 |
+ } |
|
249 |
+ avio_wb32(out, 0); // abst size |
|
250 |
+ avio_wl32(out, MKTAG('a','b','s','t')); |
|
251 |
+ avio_wb32(out, 0); // version + flags |
|
252 |
+ avio_wb32(out, os->fragment_index - 1); // BootstrapinfoVersion |
|
253 |
+ avio_w8(out, final ? 0 : 0x20); // profile, live, update |
|
254 |
+ avio_wb32(out, 1000); // timescale |
|
255 |
+ avio_wb64(out, cur_media_time); |
|
256 |
+ avio_wb64(out, 0); // SmpteTimeCodeOffset |
|
257 |
+ avio_w8(out, 0); // MovieIdentifer (null string) |
|
258 |
+ avio_w8(out, 0); // ServerEntryCount |
|
259 |
+ avio_w8(out, 0); // QualityEntryCount |
|
260 |
+ avio_w8(out, 0); // DrmData (null string) |
|
261 |
+ avio_w8(out, 0); // MetaData (null string) |
|
262 |
+ avio_w8(out, 1); // SegmentRunTableCount |
|
263 |
+ asrt_pos = avio_tell(out); |
|
264 |
+ avio_wb32(out, 0); // asrt size |
|
265 |
+ avio_wl32(out, MKTAG('a','s','r','t')); |
|
266 |
+ avio_wb32(out, 0); // version + flags |
|
267 |
+ avio_w8(out, 0); // QualityEntryCount |
|
268 |
+ avio_wb32(out, 1); // SegmentRunEntryCount |
|
269 |
+ avio_wb32(out, 1); // FirstSegment |
|
270 |
+ avio_wb32(out, final ? (os->fragment_index - 1) : 0xffffffff); // FragmentsPerSegment |
|
271 |
+ update_size(out, asrt_pos); |
|
272 |
+ avio_w8(out, 1); // FragmentRunTableCount |
|
273 |
+ afrt_pos = avio_tell(out); |
|
274 |
+ avio_wb32(out, 0); // afrt size |
|
275 |
+ avio_wl32(out, MKTAG('a','f','r','t')); |
|
276 |
+ avio_wb32(out, 0); // version + flags |
|
277 |
+ avio_wb32(out, 1000); // timescale |
|
278 |
+ avio_w8(out, 0); // QualityEntryCount |
|
279 |
+ avio_wb32(out, fragments); // FragmentRunEntryCount |
|
280 |
+ for (i = start; i < os->nb_fragments; i++) { |
|
281 |
+ avio_wb32(out, os->fragments[i]->n); |
|
282 |
+ avio_wb64(out, os->fragments[i]->start_time); |
|
283 |
+ avio_wb32(out, os->fragments[i]->duration); |
|
284 |
+ } |
|
285 |
+ update_size(out, afrt_pos); |
|
286 |
+ update_size(out, 0); |
|
287 |
+ avio_close(out); |
|
288 |
+ rename(temp_filename, filename); |
|
289 |
+ return 0; |
|
290 |
+} |
|
291 |
+ |
|
292 |
+static int init_file(AVFormatContext *s, OutputStream *os, int64_t start_ts) |
|
293 |
+{ |
|
294 |
+ int ret, i; |
|
295 |
+ ret = avio_open2(&os->out, os->temp_filename, AVIO_FLAG_WRITE, |
|
296 |
+ &s->interrupt_callback, NULL); |
|
297 |
+ if (ret < 0) |
|
298 |
+ return ret; |
|
299 |
+ avio_wb32(os->out, 0); |
|
300 |
+ avio_wl32(os->out, MKTAG('m','d','a','t')); |
|
301 |
+ for (i = 0; i < os->nb_extra_packets; i++) { |
|
302 |
+ AV_WB24(os->extra_packets[i] + 4, start_ts); |
|
303 |
+ os->extra_packets[i][7] = (start_ts >> 24) & 0x7f; |
|
304 |
+ avio_write(os->out, os->extra_packets[i], os->extra_packet_sizes[i]); |
|
305 |
+ } |
|
306 |
+ return 0; |
|
307 |
+} |
|
308 |
+ |
|
309 |
+static void close_file(OutputStream *os) |
|
310 |
+{ |
|
311 |
+ int64_t pos = avio_tell(os->out); |
|
312 |
+ avio_seek(os->out, 0, SEEK_SET); |
|
313 |
+ avio_wb32(os->out, pos); |
|
314 |
+ avio_flush(os->out); |
|
315 |
+ avio_close(os->out); |
|
316 |
+ os->out = NULL; |
|
317 |
+} |
|
318 |
+ |
|
319 |
+static int hds_write_header(AVFormatContext *s) |
|
320 |
+{ |
|
321 |
+ HDSContext *c = s->priv_data; |
|
322 |
+ int ret = 0, i; |
|
323 |
+ AVOutputFormat *oformat; |
|
324 |
+ |
|
325 |
+ mkdir(s->filename, 0777); |
|
326 |
+ |
|
327 |
+ oformat = av_guess_format("flv", NULL, NULL); |
|
328 |
+ if (!oformat) { |
|
329 |
+ ret = AVERROR_MUXER_NOT_FOUND; |
|
330 |
+ goto fail; |
|
331 |
+ } |
|
332 |
+ |
|
333 |
+ c->streams = av_mallocz(sizeof(*c->streams) * s->nb_streams); |
|
334 |
+ if (!c->streams) { |
|
335 |
+ ret = AVERROR(ENOMEM); |
|
336 |
+ goto fail; |
|
337 |
+ } |
|
338 |
+ |
|
339 |
+ for (i = 0; i < s->nb_streams; i++) { |
|
340 |
+ OutputStream *os = &c->streams[c->nb_streams]; |
|
341 |
+ AVFormatContext *ctx; |
|
342 |
+ AVStream *st = s->streams[i]; |
|
343 |
+ |
|
344 |
+ if (!st->codec->bit_rate) { |
|
345 |
+ av_log(s, AV_LOG_ERROR, "No bit rate set for stream %d\n", i); |
|
346 |
+ ret = AVERROR(EINVAL); |
|
347 |
+ goto fail; |
|
348 |
+ } |
|
349 |
+ if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) { |
|
350 |
+ if (os->has_video) { |
|
351 |
+ c->nb_streams++; |
|
352 |
+ os++; |
|
353 |
+ } |
|
354 |
+ os->has_video = 1; |
|
355 |
+ } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) { |
|
356 |
+ if (os->has_audio) { |
|
357 |
+ c->nb_streams++; |
|
358 |
+ os++; |
|
359 |
+ } |
|
360 |
+ os->has_audio = 1; |
|
361 |
+ } else { |
|
362 |
+ av_log(s, AV_LOG_ERROR, "Unsupported stream type in stream %d\n", i); |
|
363 |
+ ret = AVERROR(EINVAL); |
|
364 |
+ goto fail; |
|
365 |
+ } |
|
366 |
+ os->bitrate += s->streams[i]->codec->bit_rate; |
|
367 |
+ |
|
368 |
+ if (!os->ctx) { |
|
369 |
+ os->first_stream = i; |
|
370 |
+ ctx = avformat_alloc_context(); |
|
371 |
+ if (!ctx) { |
|
372 |
+ ret = AVERROR(ENOMEM); |
|
373 |
+ goto fail; |
|
374 |
+ } |
|
375 |
+ os->ctx = ctx; |
|
376 |
+ ctx->oformat = oformat; |
|
377 |
+ ctx->interrupt_callback = s->interrupt_callback; |
|
378 |
+ |
|
379 |
+ ctx->pb = avio_alloc_context(os->iobuf, sizeof(os->iobuf), |
|
380 |
+ AVIO_FLAG_WRITE, os, |
|
381 |
+ NULL, hds_write, NULL); |
|
382 |
+ if (!ctx->pb) { |
|
383 |
+ ret = AVERROR(ENOMEM); |
|
384 |
+ goto fail; |
|
385 |
+ } |
|
386 |
+ } else { |
|
387 |
+ ctx = os->ctx; |
|
388 |
+ } |
|
389 |
+ s->streams[i]->id = c->nb_streams; |
|
390 |
+ |
|
391 |
+ if (!(st = avformat_new_stream(ctx, NULL))) { |
|
392 |
+ ret = AVERROR(ENOMEM); |
|
393 |
+ goto fail; |
|
394 |
+ } |
|
395 |
+ avcodec_copy_context(st->codec, s->streams[i]->codec); |
|
396 |
+ st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio; |
|
397 |
+ } |
|
398 |
+ if (c->streams[c->nb_streams].ctx) |
|
399 |
+ c->nb_streams++; |
|
400 |
+ |
|
401 |
+ for (i = 0; i < c->nb_streams; i++) { |
|
402 |
+ OutputStream *os = &c->streams[i]; |
|
403 |
+ int j; |
|
404 |
+ if ((ret = avformat_write_header(os->ctx, NULL)) < 0) { |
|
405 |
+ goto fail; |
|
406 |
+ } |
|
407 |
+ os->ctx_inited = 1; |
|
408 |
+ avio_flush(os->ctx->pb); |
|
409 |
+ for (j = 0; j < os->ctx->nb_streams; j++) |
|
410 |
+ s->streams[os->first_stream + j]->time_base = os->ctx->streams[j]->time_base; |
|
411 |
+ |
|
412 |
+ snprintf(os->temp_filename, sizeof(os->temp_filename), |
|
413 |
+ "%s/stream%d_temp", s->filename, i); |
|
414 |
+ init_file(s, os, 0); |
|
415 |
+ |
|
416 |
+ if (!os->has_video && c->min_frag_duration <= 0) { |
|
417 |
+ av_log(s, AV_LOG_WARNING, |
|
418 |
+ "No video stream in output stream %d and no min frag duration set\n", i); |
|
419 |
+ ret = AVERROR(EINVAL); |
|
420 |
+ } |
|
421 |
+ os->fragment_index = 1; |
|
422 |
+ write_abst(s, os, 0); |
|
423 |
+ } |
|
424 |
+ ret = write_manifest(s, 0); |
|
425 |
+ |
|
426 |
+fail: |
|
427 |
+ if (ret) |
|
428 |
+ hds_free(s); |
|
429 |
+ return ret; |
|
430 |
+} |
|
431 |
+ |
|
432 |
+static int add_fragment(OutputStream *os, const char *file, |
|
433 |
+ int64_t start_time, int64_t duration) |
|
434 |
+{ |
|
435 |
+ Fragment *frag; |
|
436 |
+ if (duration == 0) |
|
437 |
+ duration = 1; |
|
438 |
+ if (os->nb_fragments >= os->fragments_size) { |
|
439 |
+ int ret; |
|
440 |
+ os->fragments_size = (os->fragments_size + 1) * 2; |
|
441 |
+ if ((ret = av_reallocp_array(&os->fragments, os->fragments_size, |
|
442 |
+ sizeof(*os->fragments))) < 0) { |
|
443 |
+ os->fragments_size = 0; |
|
444 |
+ os->nb_fragments = 0; |
|
445 |
+ return ret; |
|
446 |
+ } |
|
447 |
+ } |
|
448 |
+ frag = av_mallocz(sizeof(*frag)); |
|
449 |
+ if (!frag) |
|
450 |
+ return AVERROR(ENOMEM); |
|
451 |
+ av_strlcpy(frag->file, file, sizeof(frag->file)); |
|
452 |
+ frag->start_time = start_time; |
|
453 |
+ frag->duration = duration; |
|
454 |
+ frag->n = os->fragment_index; |
|
455 |
+ os->fragments[os->nb_fragments++] = frag; |
|
456 |
+ os->fragment_index++; |
|
457 |
+ return 0; |
|
458 |
+} |
|
459 |
+ |
|
460 |
+static int hds_flush(AVFormatContext *s, OutputStream *os, int final, |
|
461 |
+ int64_t end_ts) |
|
462 |
+{ |
|
463 |
+ HDSContext *c = s->priv_data; |
|
464 |
+ int i, ret = 0; |
|
465 |
+ char target_filename[1024]; |
|
466 |
+ int index = s->streams[os->first_stream]->id; |
|
467 |
+ |
|
468 |
+ if (!os->packets_written) |
|
469 |
+ return 0; |
|
470 |
+ |
|
471 |
+ avio_flush(os->ctx->pb); |
|
472 |
+ os->packets_written = 0; |
|
473 |
+ close_file(os); |
|
474 |
+ |
|
475 |
+ snprintf(target_filename, sizeof(target_filename), |
|
476 |
+ "%s/stream%dSeg1-Frag%d", s->filename, index, os->fragment_index); |
|
477 |
+ rename(os->temp_filename, target_filename); |
|
478 |
+ add_fragment(os, target_filename, os->frag_start_ts, end_ts - os->frag_start_ts); |
|
479 |
+ |
|
480 |
+ if (!final) { |
|
481 |
+ ret = init_file(s, os, end_ts); |
|
482 |
+ if (ret < 0) |
|
483 |
+ return ret; |
|
484 |
+ } |
|
485 |
+ |
|
486 |
+ if (c->window_size || (final && c->remove_at_exit)) { |
|
487 |
+ int remove = os->nb_fragments - c->window_size - c->extra_window_size; |
|
488 |
+ if (final && c->remove_at_exit) |
|
489 |
+ remove = os->nb_fragments; |
|
490 |
+ if (remove > 0) { |
|
491 |
+ for (i = 0; i < remove; i++) { |
|
492 |
+ unlink(os->fragments[i]->file); |
|
493 |
+ av_free(os->fragments[i]); |
|
494 |
+ } |
|
495 |
+ os->nb_fragments -= remove; |
|
496 |
+ memmove(os->fragments, os->fragments + remove, |
|
497 |
+ os->nb_fragments * sizeof(*os->fragments)); |
|
498 |
+ } |
|
499 |
+ } |
|
500 |
+ |
|
501 |
+ if (ret >= 0) |
|
502 |
+ ret = write_abst(s, os, final); |
|
503 |
+ return ret; |
|
504 |
+} |
|
505 |
+ |
|
506 |
+static int hds_write_packet(AVFormatContext *s, AVPacket *pkt) |
|
507 |
+{ |
|
508 |
+ HDSContext *c = s->priv_data; |
|
509 |
+ AVStream *st = s->streams[pkt->stream_index]; |
|
510 |
+ OutputStream *os = &c->streams[s->streams[pkt->stream_index]->id]; |
|
511 |
+ int64_t end_dts = (os->fragment_index) * c->min_frag_duration; |
|
512 |
+ int ret; |
|
513 |
+ |
|
514 |
+ if (st->first_dts == AV_NOPTS_VALUE) |
|
515 |
+ st->first_dts = pkt->dts; |
|
516 |
+ |
|
517 |
+ if ((!os->has_video || st->codec->codec_type == AVMEDIA_TYPE_VIDEO) && |
|
518 |
+ av_compare_ts(pkt->dts - st->first_dts, st->time_base, |
|
519 |
+ end_dts, AV_TIME_BASE_Q) >= 0 && |
|
520 |
+ pkt->flags & AV_PKT_FLAG_KEY && os->packets_written) { |
|
521 |
+ |
|
522 |
+ if ((ret = hds_flush(s, os, 0, pkt->dts)) < 0) |
|
523 |
+ return ret; |
|
524 |
+ } |
|
525 |
+ |
|
526 |
+ // Note, these fragment start timestamps, that represent a whole |
|
527 |
+ // OutputStream, assume all streams in it have the same time base. |
|
528 |
+ if (!os->packets_written) |
|
529 |
+ os->frag_start_ts = pkt->dts; |
|
530 |
+ os->last_ts = pkt->dts; |
|
531 |
+ |
|
532 |
+ os->packets_written++; |
|
533 |
+ return ff_write_chained(os->ctx, pkt->stream_index - os->first_stream, pkt, s); |
|
534 |
+} |
|
535 |
+ |
|
536 |
+static int hds_write_trailer(AVFormatContext *s) |
|
537 |
+{ |
|
538 |
+ HDSContext *c = s->priv_data; |
|
539 |
+ int i; |
|
540 |
+ |
|
541 |
+ for (i = 0; i < c->nb_streams; i++) |
|
542 |
+ hds_flush(s, &c->streams[i], 1, c->streams[i].last_ts); |
|
543 |
+ write_manifest(s, 1); |
|
544 |
+ |
|
545 |
+ if (c->remove_at_exit) { |
|
546 |
+ char filename[1024]; |
|
547 |
+ snprintf(filename, sizeof(filename), "%s/index.f4m", s->filename); |
|
548 |
+ unlink(filename); |
|
549 |
+ for (i = 0; i < c->nb_streams; i++) { |
|
550 |
+ snprintf(filename, sizeof(filename), "%s/stream%d.abst", s->filename, i); |
|
551 |
+ unlink(filename); |
|
552 |
+ } |
|
553 |
+ rmdir(s->filename); |
|
554 |
+ } |
|
555 |
+ |
|
556 |
+ hds_free(s); |
|
557 |
+ return 0; |
|
558 |
+} |
|
559 |
+ |
|
560 |
+#define OFFSET(x) offsetof(HDSContext, x) |
|
561 |
+#define E AV_OPT_FLAG_ENCODING_PARAM |
|
562 |
+static const AVOption options[] = { |
|
563 |
+ { "window_size", "number of fragments kept in the manifest", OFFSET(window_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, E }, |
|
564 |
+ { "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 }, |
|
565 |
+ { "min_frag_duration", "minimum fragment duration (in microseconds)", OFFSET(min_frag_duration), AV_OPT_TYPE_INT64, { .i64 = 10000000 }, 0, INT_MAX, E }, |
|
566 |
+ { "remove_at_exit", "remove all fragments when finished", OFFSET(remove_at_exit), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E }, |
|
567 |
+ { NULL }, |
|
568 |
+}; |
|
569 |
+ |
|
570 |
+static const AVClass hds_class = { |
|
571 |
+ .class_name = "HDS muxer", |
|
572 |
+ .item_name = av_default_item_name, |
|
573 |
+ .option = options, |
|
574 |
+ .version = LIBAVUTIL_VERSION_INT, |
|
575 |
+}; |
|
576 |
+ |
|
577 |
+AVOutputFormat ff_hds_muxer = { |
|
578 |
+ .name = "hds", |
|
579 |
+ .long_name = NULL_IF_CONFIG_SMALL("HDS Muxer"), |
|
580 |
+ .priv_data_size = sizeof(HDSContext), |
|
581 |
+ .audio_codec = AV_CODEC_ID_AAC, |
|
582 |
+ .video_codec = AV_CODEC_ID_H264, |
|
583 |
+ .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE, |
|
584 |
+ .write_header = hds_write_header, |
|
585 |
+ .write_packet = hds_write_packet, |
|
586 |
+ .write_trailer = hds_write_trailer, |
|
587 |
+ .priv_class = &hds_class, |
|
588 |
+}; |
... | ... |
@@ -30,8 +30,8 @@ |
30 | 30 |
#include "libavutil/avutil.h" |
31 | 31 |
|
32 | 32 |
#define LIBAVFORMAT_VERSION_MAJOR 55 |
33 |
-#define LIBAVFORMAT_VERSION_MINOR 20 |
|
34 |
-#define LIBAVFORMAT_VERSION_MICRO 105 |
|
33 |
+#define LIBAVFORMAT_VERSION_MINOR 21 |
|
34 |
+#define LIBAVFORMAT_VERSION_MICRO 100 |
|
35 | 35 |
|
36 | 36 |
#define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ |
37 | 37 |
LIBAVFORMAT_VERSION_MINOR, \ |