Browse code

hlsenc: stand alone hls segmenter

Simplifies usage but has higher latency.

Luca Barbato authored on 2012/10/26 23:36:56
Showing 4 changed files
... ...
@@ -90,6 +90,27 @@ avconv -i INPUT -c:a pcm_u8 -c:v mpeg2video -f framecrc -
90 90
 
91 91
 See also the @ref{crc} muxer.
92 92
 
93
+@anchor{hls}
94
+@section hls
95
+
96
+Apple HTTP Live Streaming muxer that segments MPEG-TS according to
97
+the HTTP Live Streaming specification.
98
+
99
+It creates a playlist file and numbered segment files. The output
100
+filename specifies the playlist filename; the segment filenames
101
+receive the same basename as the playlist, a sequential number and
102
+a .ts extension.
103
+
104
+@example
105
+avconv -i in.nut out.m3u8
106
+@end example
107
+
108
+@table @option
109
+@item -hls_time segment length in seconds
110
+@item -hls_list_size maximum number of playlist entries
111
+@item -hls_wrap number after which index wraps
112
+@end table
113
+
93 114
 @anchor{image2}
94 115
 @section image2
95 116
 
... ...
@@ -111,6 +111,7 @@ OBJS-$(CONFIG_H263_MUXER)                += rawenc.o
111 111
 OBJS-$(CONFIG_H264_DEMUXER)              += h264dec.o rawdec.o
112 112
 OBJS-$(CONFIG_H264_MUXER)                += rawenc.o
113 113
 OBJS-$(CONFIG_HLS_DEMUXER)               += hls.o
114
+OBJS-$(CONFIG_HLS_MUXER)                 += hlsenc.o mpegtsenc.o
114 115
 OBJS-$(CONFIG_IDCIN_DEMUXER)             += idcin.o
115 116
 OBJS-$(CONFIG_IFF_DEMUXER)               += iff.o
116 117
 OBJS-$(CONFIG_ILBC_DEMUXER)              += ilbc.o
... ...
@@ -106,7 +106,7 @@ void av_register_all(void)
106 106
     REGISTER_MUXDEMUX (H261, h261);
107 107
     REGISTER_MUXDEMUX (H263, h263);
108 108
     REGISTER_MUXDEMUX (H264, h264);
109
-    REGISTER_DEMUXER  (HLS, hls);
109
+    REGISTER_MUXDEMUX (HLS, hls);
110 110
     REGISTER_DEMUXER  (IDCIN, idcin);
111 111
     REGISTER_DEMUXER  (IFF, iff);
112 112
     REGISTER_MUXDEMUX (ILBC, ilbc);
113 113
new file mode 100644
... ...
@@ -0,0 +1,324 @@
0
+/*
1
+ * Apple HTTP Live Streaming segmenter
2
+ * Copyright (c) 2012, Luca Barbato
3
+ *
4
+ * This file is part of Libav.
5
+ *
6
+ * Libav 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
+ * Libav 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 Libav; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include <float.h>
22
+
23
+#include "libavutil/mathematics.h"
24
+#include "libavutil/parseutils.h"
25
+#include "libavutil/avstring.h"
26
+#include "libavutil/opt.h"
27
+#include "libavutil/log.h"
28
+
29
+#include "avformat.h"
30
+#include "internal.h"
31
+
32
+typedef struct ListEntry {
33
+    char  name[1024];
34
+    int   duration;
35
+    struct ListEntry *next;
36
+} ListEntry;
37
+
38
+typedef struct HLSContext {
39
+    const AVClass *class;  // Class for private options.
40
+    int number;
41
+    AVOutputFormat *oformat;
42
+    AVFormatContext *avf;
43
+    float time;            // Set by a private option.
44
+    int  size;             // Set by a private option.
45
+    int  wrap;             // Set by a private option.
46
+    int64_t recording_time;
47
+    int has_video;
48
+    int64_t start_pts;
49
+    int64_t end_pts;
50
+    ListEntry *list;
51
+    ListEntry *end_list;
52
+    char *basename;
53
+    AVIOContext *pb;
54
+} HLSContext;
55
+
56
+static int hls_mux_init(AVFormatContext *s)
57
+{
58
+    HLSContext *hls = s->priv_data;
59
+    AVFormatContext *oc;
60
+    int i;
61
+
62
+    hls->avf = oc = avformat_alloc_context();
63
+    if (!oc)
64
+        return AVERROR(ENOMEM);
65
+
66
+    oc->oformat            = hls->oformat;
67
+    oc->interrupt_callback = s->interrupt_callback;
68
+
69
+    for (i = 0; i < s->nb_streams; i++) {
70
+        AVStream *st;
71
+        if (!(st = avformat_new_stream(oc, NULL)))
72
+            return AVERROR(ENOMEM);
73
+        avcodec_copy_context(st->codec, s->streams[i]->codec);
74
+        st->sample_aspect_ratio = s->streams[i]->sample_aspect_ratio;
75
+    }
76
+
77
+    return 0;
78
+}
79
+
80
+static int append_entry(HLSContext *hls, uint64_t duration)
81
+{
82
+    ListEntry *en = av_malloc(sizeof(*en));
83
+
84
+    if (!en)
85
+        return AVERROR(ENOMEM);
86
+
87
+    av_get_frame_filename(en->name, sizeof(en->name), hls->basename,
88
+                          hls->number -1);
89
+
90
+    en->duration = duration;
91
+    en->next     = NULL;
92
+
93
+    if (!hls->list)
94
+        hls->list = en;
95
+    else
96
+        hls->end_list->next = en;
97
+
98
+    hls->end_list = en;
99
+
100
+    if (hls->number >= hls->size) {
101
+        en = hls->list;
102
+        hls->list = en->next;
103
+        av_free(en);
104
+    }
105
+
106
+    return 0;
107
+}
108
+
109
+static void free_entries(HLSContext *hls)
110
+{
111
+    ListEntry *p = hls->list, *en;
112
+
113
+    while(p) {
114
+        en = p;
115
+        p = p->next;
116
+        av_free(en);
117
+    }
118
+}
119
+
120
+static int hls_window(AVFormatContext *s, int last)
121
+{
122
+    HLSContext *hls = s->priv_data;
123
+    ListEntry *en;
124
+    int ret = 0;
125
+
126
+    if ((ret = avio_open2(&hls->pb, s->filename, AVIO_FLAG_WRITE,
127
+                          &s->interrupt_callback, NULL)) < 0)
128
+        goto fail;
129
+
130
+    avio_printf(hls->pb, "#EXTM3U\n");
131
+    avio_printf(hls->pb, "#EXT-X-VERSION:3\n");
132
+    avio_printf(hls->pb, "#EXT-X-TARGETDURATION:%d\n", (int)hls->time);
133
+    avio_printf(hls->pb, "#EXT-X-MEDIA-SEQUENCE:%d\n",
134
+                FFMAX(0, hls->number - hls->size));
135
+
136
+    for (en = hls->list; en; en = en->next) {
137
+        avio_printf(hls->pb, "#EXTINF:%d,\n", en->duration);
138
+        avio_printf(hls->pb, "%s\n", en->name);
139
+    }
140
+
141
+    if (last)
142
+        avio_printf(hls->pb, "#EXT-X-ENDLIST\n");
143
+
144
+fail:
145
+    avio_closep(&hls->pb);
146
+    return ret;
147
+}
148
+
149
+static int hls_start(AVFormatContext *s)
150
+{
151
+    HLSContext *c = s->priv_data;
152
+    AVFormatContext *oc = c->avf;
153
+    int err = 0;
154
+
155
+    if (c->wrap)
156
+        c->number %= c->wrap;
157
+
158
+    if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
159
+                              c->basename, c->number++) < 0)
160
+        return AVERROR(EINVAL);
161
+
162
+    if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
163
+                          &s->interrupt_callback, NULL)) < 0)
164
+        return err;
165
+
166
+    if (oc->oformat->priv_class && oc->priv_data)
167
+        av_opt_set(oc->priv_data, "mpegts_flags", "resend_headers", 0);
168
+
169
+    return 0;
170
+}
171
+
172
+static int hls_write_header(AVFormatContext *s)
173
+{
174
+    HLSContext *hls = s->priv_data;
175
+    int ret, i;
176
+    char *p;
177
+    const char *pattern = "%d.ts";
178
+    int basename_size = strlen(s->filename) + strlen(pattern);
179
+
180
+    hls->number      = 0;
181
+
182
+    hls->recording_time = hls->time * 1000000;
183
+    hls->start_pts      = AV_NOPTS_VALUE;
184
+
185
+    for (i = 0; i < s->nb_streams; i++)
186
+        hls->has_video +=
187
+            s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO;
188
+
189
+    if (hls->has_video > 1)
190
+        av_log(s, AV_LOG_WARNING,
191
+               "More than a single video stream present, "
192
+               "expect issues decoding it.\n");
193
+
194
+    hls->oformat = av_guess_format("mpegts", NULL, NULL);
195
+
196
+    if (!hls->oformat) {
197
+        ret = AVERROR_MUXER_NOT_FOUND;
198
+        goto fail;
199
+    }
200
+
201
+    hls->basename = av_malloc(basename_size);
202
+
203
+    if (!hls->basename) {
204
+        ret = AVERROR(ENOMEM);
205
+        goto fail;
206
+    }
207
+
208
+    strcpy(hls->basename, s->filename);
209
+
210
+    p = strrchr(hls->basename, '.');
211
+
212
+    if (p)
213
+        *p = '\0';
214
+
215
+    av_strlcat(hls->basename, "%d.ts", basename_size);
216
+
217
+    if ((ret = hls_mux_init(s)) < 0)
218
+        goto fail;
219
+
220
+    if ((ret = hls_start(s)) < 0)
221
+        goto fail;
222
+
223
+    if ((ret = avformat_write_header(hls->avf, NULL)) < 0)
224
+        return ret;
225
+
226
+
227
+fail:
228
+    if (ret) {
229
+        av_free(hls->basename);
230
+        if (hls->avf)
231
+            avformat_free_context(hls->avf);
232
+    }
233
+    return ret;
234
+}
235
+
236
+static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
237
+{
238
+    HLSContext *hls = s->priv_data;
239
+    AVFormatContext *oc = hls->avf;
240
+    AVStream *st = s->streams[pkt->stream_index];
241
+    int64_t end_pts = hls->recording_time * hls->number;
242
+    int ret;
243
+
244
+    if (hls->start_pts == AV_NOPTS_VALUE) {
245
+        hls->start_pts = pkt->pts;
246
+        hls->end_pts   = pkt->pts;
247
+    }
248
+    end_pts += hls->start_pts;
249
+
250
+    if ((hls->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO)      &&
251
+        av_compare_ts(pkt->pts, st->time_base, end_pts, AV_TIME_BASE_Q) >= 0 &&
252
+        pkt->flags & AV_PKT_FLAG_KEY) {
253
+
254
+        append_entry(hls, av_rescale(pkt->pts - hls->end_pts,
255
+                                     st->time_base.num,
256
+                                     st->time_base.den));
257
+        hls->end_pts = pkt->pts;
258
+
259
+        av_write_frame(oc, NULL); /* Flush any buffered data */
260
+        avio_close(oc->pb);
261
+
262
+        ret = hls_start(s);
263
+
264
+        if (ret)
265
+            return ret;
266
+
267
+        oc = hls->avf;
268
+
269
+        if ((ret = hls_window(s, 0)) < 0)
270
+            return ret;
271
+    }
272
+
273
+    ret = ff_write_chained(oc, pkt->stream_index, pkt, s);
274
+
275
+    return ret;
276
+}
277
+
278
+static int hls_write_trailer(struct AVFormatContext *s)
279
+{
280
+    HLSContext *hls = s->priv_data;
281
+    AVFormatContext *oc = hls->avf;
282
+
283
+    av_write_trailer(oc);
284
+    avio_closep(&oc->pb);
285
+    avformat_free_context(oc);
286
+    av_free(hls->basename);
287
+    hls_window(s, 1);
288
+
289
+    free_entries(hls);
290
+    avio_close(hls->pb);
291
+    return 0;
292
+}
293
+
294
+#define OFFSET(x) offsetof(HLSContext, x)
295
+#define E AV_OPT_FLAG_ENCODING_PARAM
296
+static const AVOption options[] = {
297
+    {"hls_time",      "segment length in seconds",               OFFSET(time),    AV_OPT_TYPE_FLOAT,  {.dbl = 2},     0, FLT_MAX, E},
298
+    {"hls_list_size", "maximum number of playlist entries",      OFFSET(size),    AV_OPT_TYPE_INT,    {.i64 = 5},     0, INT_MAX, E},
299
+    {"hls_wrap",      "number after which the index wraps",      OFFSET(wrap),    AV_OPT_TYPE_INT,    {.i64 = 0},     0, INT_MAX, E},
300
+    { NULL },
301
+};
302
+
303
+static const AVClass hls_class = {
304
+    .class_name = "hls muxer",
305
+    .item_name  = av_default_item_name,
306
+    .option     = options,
307
+    .version    = LIBAVUTIL_VERSION_INT,
308
+};
309
+
310
+
311
+AVOutputFormat ff_hls_muxer = {
312
+    .name           = "hls",
313
+    .long_name      = NULL_IF_CONFIG_SMALL("hls"),
314
+    .extensions     = "m3u8",
315
+    .priv_data_size = sizeof(HLSContext),
316
+    .audio_codec    = AV_CODEC_ID_MP2,
317
+    .video_codec    = AV_CODEC_ID_MPEG2VIDEO,
318
+    .flags          = AVFMT_NOFILE | AVFMT_ALLOW_FLUSH,
319
+    .write_header   = hls_write_header,
320
+    .write_packet   = hls_write_packet,
321
+    .write_trailer  = hls_write_trailer,
322
+    .priv_class     = &hls_class,
323
+};