FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hdsenc.c
Go to the documentation of this file.
1 /*
2  * Live HDS fragmenter
3  * Copyright (c) 2013 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"
23 #include <float.h>
24 #if HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 
28 #include "avformat.h"
29 #include "internal.h"
30 #include "os_support.h"
31 
32 #include "libavutil/avstring.h"
33 #include "libavutil/base64.h"
34 #include "libavutil/intreadwrite.h"
35 #include "libavutil/mathematics.h"
36 #include "libavutil/opt.h"
37 
38 typedef struct Fragment {
39  char file[1024];
40  int64_t start_time, duration;
41  int n;
42 } Fragment;
43 
44 typedef struct OutputStream {
45  int bitrate;
49  uint8_t iobuf[32768];
50  char temp_filename[1024];
56 
58 
61 
65 } OutputStream;
66 
67 typedef struct HDSContext {
68  const AVClass *class; /* Class for private options. */
73 
76 } HDSContext;
77 
78 static int parse_header(OutputStream *os, const uint8_t *buf, int buf_size)
79 {
80  if (buf_size < 13)
81  return AVERROR_INVALIDDATA;
82  if (memcmp(buf, "FLV", 3))
83  return AVERROR_INVALIDDATA;
84  buf += 13;
85  buf_size -= 13;
86  while (buf_size >= 11 + 4) {
87  int type = buf[0];
88  int size = AV_RB24(&buf[1]) + 11 + 4;
89  if (size > buf_size)
90  return AVERROR_INVALIDDATA;
91  if (type == 8 || type == 9) {
93  return AVERROR_INVALIDDATA;
95  os->extra_packets[os->nb_extra_packets] = av_malloc(size);
96  if (!os->extra_packets[os->nb_extra_packets])
97  return AVERROR(ENOMEM);
98  memcpy(os->extra_packets[os->nb_extra_packets], buf, size);
99  os->nb_extra_packets++;
100  } else if (type == 0x12) {
101  if (os->metadata)
102  return AVERROR_INVALIDDATA;
103  os->metadata_size = size - 11 - 4;
104  os->metadata = av_malloc(os->metadata_size);
105  if (!os->metadata)
106  return AVERROR(ENOMEM);
107  memcpy(os->metadata, buf + 11, os->metadata_size);
108  }
109  buf += size;
110  buf_size -= size;
111  }
112  if (!os->metadata)
113  return AVERROR_INVALIDDATA;
114  return 0;
115 }
116 
117 static int hds_write(void *opaque, uint8_t *buf, int buf_size)
118 {
119  OutputStream *os = opaque;
120  if (os->out) {
121  avio_write(os->out, buf, buf_size);
122  } else {
123  if (!os->metadata_size) {
124  int ret;
125  // Assuming the IO buffer is large enough to fit the
126  // FLV header and all metadata and extradata packets
127  if ((ret = parse_header(os, buf, buf_size)) < 0)
128  return ret;
129  }
130  }
131  return buf_size;
132 }
133 
135 {
136  HDSContext *c = s->priv_data;
137  int i, j;
138  if (!c->streams)
139  return;
140  for (i = 0; i < s->nb_streams; i++) {
141  OutputStream *os = &c->streams[i];
142  if (os->out)
143  avio_close(os->out);
144  os->out = NULL;
145  if (os->ctx && os->ctx_inited)
146  av_write_trailer(os->ctx);
147  if (os->ctx && os->ctx->pb)
148  av_free(os->ctx->pb);
149  if (os->ctx)
151  av_free(os->metadata);
152  for (j = 0; j < os->nb_extra_packets; j++)
153  av_free(os->extra_packets[j]);
154  for (j = 0; j < os->nb_fragments; j++)
155  av_free(os->fragments[j]);
156  av_free(os->fragments);
157  }
158  av_freep(&c->streams);
159 }
160 
161 static int write_manifest(AVFormatContext *s, int final)
162 {
163  HDSContext *c = s->priv_data;
164  AVIOContext *out;
165  char filename[1024], temp_filename[1024];
166  int ret, i;
167  float duration = 0;
168 
169  if (c->nb_streams > 0)
170  duration = c->streams[0].last_ts * av_q2d(s->streams[0]->time_base);
171 
172  snprintf(filename, sizeof(filename), "%s/index.f4m", s->filename);
173  snprintf(temp_filename, sizeof(temp_filename), "%s/index.f4m.tmp", s->filename);
174  ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE,
175  &s->interrupt_callback, NULL);
176  if (ret < 0) {
177  av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", filename);
178  return ret;
179  }
180  avio_printf(out, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
181  avio_printf(out, "<manifest xmlns=\"http://ns.adobe.com/f4m/1.0\">\n");
182  avio_printf(out, "\t<id>%s</id>\n", av_basename(s->filename));
183  avio_printf(out, "\t<streamType>%s</streamType>\n",
184  final ? "recorded" : "live");
185  avio_printf(out, "\t<deliveryType>streaming</deliveryType>\n");
186  if (final)
187  avio_printf(out, "\t<duration>%f</duration>\n", duration);
188  for (i = 0; i < c->nb_streams; i++) {
189  OutputStream *os = &c->streams[i];
190  int b64_size = AV_BASE64_SIZE(os->metadata_size);
191  char *base64 = av_malloc(b64_size);
192  if (!base64) {
193  avio_close(out);
194  return AVERROR(ENOMEM);
195  }
196  av_base64_encode(base64, b64_size, os->metadata, os->metadata_size);
197 
198  avio_printf(out, "\t<bootstrapInfo profile=\"named\" url=\"stream%d.abst\" id=\"bootstrap%d\" />\n", i, i);
199  avio_printf(out, "\t<media bitrate=\"%d\" url=\"stream%d\" bootstrapInfoId=\"bootstrap%d\">\n", os->bitrate/1000, i, i);
200  avio_printf(out, "\t\t<metadata>%s</metadata>\n", base64);
201  avio_printf(out, "\t</media>\n");
202  av_free(base64);
203  }
204  avio_printf(out, "</manifest>\n");
205  avio_flush(out);
206  avio_close(out);
207  if (rename(temp_filename, filename) == -1) {
208  av_log(s, AV_LOG_ERROR, "failed to rename file %s to %s\n", temp_filename, filename);
209  return AVERROR(errno);
210  }
211  return 0;
212 }
213 
214 static void update_size(AVIOContext *out, int64_t pos)
215 {
216  int64_t end = avio_tell(out);
217  avio_seek(out, pos, SEEK_SET);
218  avio_wb32(out, end - pos);
219  avio_seek(out, end, SEEK_SET);
220 }
221 
222 /* Note, the .abst files need to be served with the "binary/octet"
223  * mime type, otherwise at least the OSMF player can easily fail
224  * with "stream not found" when polling for the next fragment. */
225 static int write_abst(AVFormatContext *s, OutputStream *os, int final)
226 {
227  HDSContext *c = s->priv_data;
228  AVIOContext *out;
229  char filename[1024], temp_filename[1024];
230  int i, ret;
231  int64_t asrt_pos, afrt_pos;
232  int start = 0, fragments;
233  int index = s->streams[os->first_stream]->id;
234  int64_t cur_media_time = 0;
235  if (c->window_size)
236  start = FFMAX(os->nb_fragments - c->window_size, 0);
237  fragments = os->nb_fragments - start;
238  if (final)
239  cur_media_time = os->last_ts;
240  else if (os->nb_fragments)
241  cur_media_time = os->fragments[os->nb_fragments - 1]->start_time;
242 
243  snprintf(filename, sizeof(filename),
244  "%s/stream%d.abst", s->filename, index);
245  snprintf(temp_filename, sizeof(temp_filename),
246  "%s/stream%d.abst.tmp", s->filename, index);
247  ret = avio_open2(&out, temp_filename, AVIO_FLAG_WRITE,
248  &s->interrupt_callback, NULL);
249  if (ret < 0) {
250  av_log(s, AV_LOG_ERROR, "Unable to open %s for writing\n", temp_filename);
251  return ret;
252  }
253  avio_wb32(out, 0); // abst size
254  avio_wl32(out, MKTAG('a','b','s','t'));
255  avio_wb32(out, 0); // version + flags
256  avio_wb32(out, os->fragment_index - 1); // BootstrapinfoVersion
257  avio_w8(out, final ? 0 : 0x20); // profile, live, update
258  avio_wb32(out, 1000); // timescale
259  avio_wb64(out, cur_media_time);
260  avio_wb64(out, 0); // SmpteTimeCodeOffset
261  avio_w8(out, 0); // MovieIdentifer (null string)
262  avio_w8(out, 0); // ServerEntryCount
263  avio_w8(out, 0); // QualityEntryCount
264  avio_w8(out, 0); // DrmData (null string)
265  avio_w8(out, 0); // MetaData (null string)
266  avio_w8(out, 1); // SegmentRunTableCount
267  asrt_pos = avio_tell(out);
268  avio_wb32(out, 0); // asrt size
269  avio_wl32(out, MKTAG('a','s','r','t'));
270  avio_wb32(out, 0); // version + flags
271  avio_w8(out, 0); // QualityEntryCount
272  avio_wb32(out, 1); // SegmentRunEntryCount
273  avio_wb32(out, 1); // FirstSegment
274  avio_wb32(out, final ? (os->fragment_index - 1) : 0xffffffff); // FragmentsPerSegment
275  update_size(out, asrt_pos);
276  avio_w8(out, 1); // FragmentRunTableCount
277  afrt_pos = avio_tell(out);
278  avio_wb32(out, 0); // afrt size
279  avio_wl32(out, MKTAG('a','f','r','t'));
280  avio_wb32(out, 0); // version + flags
281  avio_wb32(out, 1000); // timescale
282  avio_w8(out, 0); // QualityEntryCount
283  avio_wb32(out, fragments); // FragmentRunEntryCount
284  for (i = start; i < os->nb_fragments; i++) {
285  avio_wb32(out, os->fragments[i]->n);
286  avio_wb64(out, os->fragments[i]->start_time);
287  avio_wb32(out, os->fragments[i]->duration);
288  }
289  update_size(out, afrt_pos);
290  update_size(out, 0);
291  avio_close(out);
292  if (rename(temp_filename, filename) == -1) {
293  av_log(s, AV_LOG_ERROR, "failed to rename file %s to %s\n", temp_filename, filename);
294  return AVERROR(errno);
295  }
296  return 0;
297 }
298 
299 static int init_file(AVFormatContext *s, OutputStream *os, int64_t start_ts)
300 {
301  int ret, i;
302  ret = avio_open2(&os->out, os->temp_filename, AVIO_FLAG_WRITE,
303  &s->interrupt_callback, NULL);
304  if (ret < 0)
305  return ret;
306  avio_wb32(os->out, 0);
307  avio_wl32(os->out, MKTAG('m','d','a','t'));
308  for (i = 0; i < os->nb_extra_packets; i++) {
309  AV_WB24(os->extra_packets[i] + 4, start_ts);
310  os->extra_packets[i][7] = (start_ts >> 24) & 0x7f;
311  avio_write(os->out, os->extra_packets[i], os->extra_packet_sizes[i]);
312  }
313  return 0;
314 }
315 
316 static void close_file(OutputStream *os)
317 {
318  int64_t pos = avio_tell(os->out);
319  avio_seek(os->out, 0, SEEK_SET);
320  avio_wb32(os->out, pos);
321  avio_flush(os->out);
322  avio_close(os->out);
323  os->out = NULL;
324 }
325 
327 {
328  HDSContext *c = s->priv_data;
329  int ret = 0, i;
330  AVOutputFormat *oformat;
331 
332  if (mkdir(s->filename, 0777)) {
333  int is_error = errno != EEXIST;
334  av_log(s, is_error ? AV_LOG_ERROR : AV_LOG_VERBOSE, "Failed to create directory %s\n", s->filename);
335  if (is_error) {
336  ret = AVERROR(errno);
337  goto fail;
338  }
339  }
340 
341  oformat = av_guess_format("flv", NULL, NULL);
342  if (!oformat) {
344  goto fail;
345  }
346 
347  c->streams = av_mallocz_array(s->nb_streams, sizeof(*c->streams));
348  if (!c->streams) {
349  ret = AVERROR(ENOMEM);
350  goto fail;
351  }
352 
353  for (i = 0; i < s->nb_streams; i++) {
354  OutputStream *os = &c->streams[c->nb_streams];
355  AVFormatContext *ctx;
356  AVStream *st = s->streams[i];
357 
358  if (!st->codec->bit_rate) {
359  av_log(s, AV_LOG_ERROR, "No bit rate set for stream %d\n", i);
360  ret = AVERROR(EINVAL);
361  goto fail;
362  }
363  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
364  if (os->has_video) {
365  c->nb_streams++;
366  os++;
367  }
368  os->has_video = 1;
369  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
370  if (os->has_audio) {
371  c->nb_streams++;
372  os++;
373  }
374  os->has_audio = 1;
375  } else {
376  av_log(s, AV_LOG_ERROR, "Unsupported stream type in stream %d\n", i);
377  ret = AVERROR(EINVAL);
378  goto fail;
379  }
380  os->bitrate += s->streams[i]->codec->bit_rate;
381 
382  if (!os->ctx) {
383  os->first_stream = i;
384  ctx = avformat_alloc_context();
385  if (!ctx) {
386  ret = AVERROR(ENOMEM);
387  goto fail;
388  }
389  os->ctx = ctx;
390  ctx->oformat = oformat;
392 
393  ctx->pb = avio_alloc_context(os->iobuf, sizeof(os->iobuf),
394  AVIO_FLAG_WRITE, os,
395  NULL, hds_write, NULL);
396  if (!ctx->pb) {
397  ret = AVERROR(ENOMEM);
398  goto fail;
399  }
400  } else {
401  ctx = os->ctx;
402  }
403  s->streams[i]->id = c->nb_streams;
404 
405  if (!(st = avformat_new_stream(ctx, NULL))) {
406  ret = AVERROR(ENOMEM);
407  goto fail;
408  }
411  }
412  if (c->streams[c->nb_streams].ctx)
413  c->nb_streams++;
414 
415  for (i = 0; i < c->nb_streams; i++) {
416  OutputStream *os = &c->streams[i];
417  int j;
418  if ((ret = avformat_write_header(os->ctx, NULL)) < 0) {
419  goto fail;
420  }
421  os->ctx_inited = 1;
422  avio_flush(os->ctx->pb);
423  for (j = 0; j < os->ctx->nb_streams; j++)
424  s->streams[os->first_stream + j]->time_base = os->ctx->streams[j]->time_base;
425 
426  snprintf(os->temp_filename, sizeof(os->temp_filename),
427  "%s/stream%d_temp", s->filename, i);
428  ret = init_file(s, os, 0);
429  if (ret < 0)
430  goto fail;
431 
432  if (!os->has_video && c->min_frag_duration <= 0) {
434  "No video stream in output stream %d and no min frag duration set\n", i);
435  ret = AVERROR(EINVAL);
436  }
437  os->fragment_index = 1;
438  write_abst(s, os, 0);
439  }
440  ret = write_manifest(s, 0);
441 
442 fail:
443  if (ret)
444  hds_free(s);
445  return ret;
446 }
447 
448 static int add_fragment(OutputStream *os, const char *file,
449  int64_t start_time, int64_t duration)
450 {
451  Fragment *frag;
452  if (duration == 0)
453  duration = 1;
454  if (os->nb_fragments >= os->fragments_size) {
455  int ret;
456  os->fragments_size = (os->fragments_size + 1) * 2;
457  if ((ret = av_reallocp_array(&os->fragments, os->fragments_size,
458  sizeof(*os->fragments))) < 0) {
459  os->fragments_size = 0;
460  os->nb_fragments = 0;
461  return ret;
462  }
463  }
464  frag = av_mallocz(sizeof(*frag));
465  if (!frag)
466  return AVERROR(ENOMEM);
467  av_strlcpy(frag->file, file, sizeof(frag->file));
468  frag->start_time = start_time;
469  frag->duration = duration;
470  frag->n = os->fragment_index;
471  os->fragments[os->nb_fragments++] = frag;
472  os->fragment_index++;
473  return 0;
474 }
475 
476 static int hds_flush(AVFormatContext *s, OutputStream *os, int final,
477  int64_t end_ts)
478 {
479  HDSContext *c = s->priv_data;
480  int i, ret = 0;
481  char target_filename[1024];
482  int index = s->streams[os->first_stream]->id;
483 
484  if (!os->packets_written)
485  return 0;
486 
487  avio_flush(os->ctx->pb);
488  os->packets_written = 0;
489  close_file(os);
490 
491  snprintf(target_filename, sizeof(target_filename),
492  "%s/stream%dSeg1-Frag%d", s->filename, index, os->fragment_index);
493  if (rename(os->temp_filename, target_filename) == -1) {
494  av_log(s, AV_LOG_ERROR, "failed to rename file %s to %s\n", os->temp_filename, target_filename);
495  return AVERROR(errno);
496  }
497  add_fragment(os, target_filename, os->frag_start_ts, end_ts - os->frag_start_ts);
498 
499  if (!final) {
500  ret = init_file(s, os, end_ts);
501  if (ret < 0)
502  return ret;
503  }
504 
505  if (c->window_size || (final && c->remove_at_exit)) {
506  int remove = os->nb_fragments - c->window_size - c->extra_window_size;
507  if (final && c->remove_at_exit)
508  remove = os->nb_fragments;
509  if (remove > 0) {
510  for (i = 0; i < remove; i++) {
511  unlink(os->fragments[i]->file);
512  av_free(os->fragments[i]);
513  }
514  os->nb_fragments -= remove;
515  memmove(os->fragments, os->fragments + remove,
516  os->nb_fragments * sizeof(*os->fragments));
517  }
518  }
519 
520  if (ret >= 0)
521  ret = write_abst(s, os, final);
522  return ret;
523 }
524 
526 {
527  HDSContext *c = s->priv_data;
528  AVStream *st = s->streams[pkt->stream_index];
529  OutputStream *os = &c->streams[s->streams[pkt->stream_index]->id];
530  int64_t end_dts = os->fragment_index * (int64_t)c->min_frag_duration;
531  int ret;
532 
533  if (st->first_dts == AV_NOPTS_VALUE)
534  st->first_dts = pkt->dts;
535 
536  if ((!os->has_video || st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
537  av_compare_ts(pkt->dts - st->first_dts, st->time_base,
538  end_dts, AV_TIME_BASE_Q) >= 0 &&
539  pkt->flags & AV_PKT_FLAG_KEY && os->packets_written) {
540 
541  if ((ret = hds_flush(s, os, 0, pkt->dts)) < 0)
542  return ret;
543  }
544 
545  // Note, these fragment start timestamps, that represent a whole
546  // OutputStream, assume all streams in it have the same time base.
547  if (!os->packets_written)
548  os->frag_start_ts = pkt->dts;
549  os->last_ts = pkt->dts;
550 
551  os->packets_written++;
552  return ff_write_chained(os->ctx, pkt->stream_index - os->first_stream, pkt, s);
553 }
554 
556 {
557  HDSContext *c = s->priv_data;
558  int i;
559 
560  for (i = 0; i < c->nb_streams; i++)
561  hds_flush(s, &c->streams[i], 1, c->streams[i].last_ts);
562  write_manifest(s, 1);
563 
564  if (c->remove_at_exit) {
565  char filename[1024];
566  snprintf(filename, sizeof(filename), "%s/index.f4m", s->filename);
567  unlink(filename);
568  for (i = 0; i < c->nb_streams; i++) {
569  snprintf(filename, sizeof(filename), "%s/stream%d.abst", s->filename, i);
570  unlink(filename);
571  }
572  rmdir(s->filename);
573  }
574 
575  hds_free(s);
576  return 0;
577 }
578 
579 #define OFFSET(x) offsetof(HDSContext, x)
580 #define E AV_OPT_FLAG_ENCODING_PARAM
581 static const AVOption options[] = {
582  { "window_size", "number of fragments kept in the manifest", OFFSET(window_size), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, E },
583  { "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 },
584  { "min_frag_duration", "minimum fragment duration (in microseconds)", OFFSET(min_frag_duration), AV_OPT_TYPE_INT64, { .i64 = 10000000 }, 0, INT_MAX, E },
585  { "remove_at_exit", "remove all fragments when finished", OFFSET(remove_at_exit), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, E },
586  { NULL },
587 };
588 
589 static const AVClass hds_class = {
590  .class_name = "HDS muxer",
591  .item_name = av_default_item_name,
592  .option = options,
593  .version = LIBAVUTIL_VERSION_INT,
594 };
595 
597  .name = "hds",
598  .long_name = NULL_IF_CONFIG_SMALL("HDS Muxer"),
599  .priv_data_size = sizeof(HDSContext),
600  .audio_codec = AV_CODEC_ID_AAC,
601  .video_codec = AV_CODEC_ID_H264,
606  .priv_class = &hds_class,
607 };