| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2015, Vignesh Venkatasubramanian | ||
| 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 | /** | ||
| 22 | * @file WebM Chunk Muxer | ||
| 23 | * The chunk muxer enables writing WebM Live chunks where there is a header | ||
| 24 | * chunk, followed by data chunks where each Cluster is written out as a Chunk. | ||
| 25 | */ | ||
| 26 | |||
| 27 | #include "avformat.h" | ||
| 28 | #include "avio.h" | ||
| 29 | #include "avio_internal.h" | ||
| 30 | #include "internal.h" | ||
| 31 | #include "mux.h" | ||
| 32 | |||
| 33 | #include "libavutil/attributes_internal.h" | ||
| 34 | #include "libavutil/bprint.h" | ||
| 35 | #include "libavutil/log.h" | ||
| 36 | #include "libavutil/mem.h" | ||
| 37 | #include "libavutil/opt.h" | ||
| 38 | #include "libavutil/mathematics.h" | ||
| 39 | |||
| 40 | typedef struct WebMChunkContext { | ||
| 41 | const AVClass *class; | ||
| 42 | char *header_filename; | ||
| 43 | int chunk_duration; | ||
| 44 | int chunk_index; | ||
| 45 | char *http_method; | ||
| 46 | uint64_t duration_written; | ||
| 47 | int64_t prev_pts; | ||
| 48 | AVFormatContext *avf; | ||
| 49 | int header_written; | ||
| 50 | } WebMChunkContext; | ||
| 51 | |||
| 52 | ✗ | static int webm_chunk_init(AVFormatContext *s) | |
| 53 | { | ||
| 54 | ✗ | WebMChunkContext *wc = s->priv_data; | |
| 55 | AVFormatContext *oc; | ||
| 56 | ✗ | AVStream *st, *ost = s->streams[0]; | |
| 57 | ✗ | AVDictionary *dict = NULL; | |
| 58 | int ret; | ||
| 59 | |||
| 60 | // DASH Streams can only have one track per file. | ||
| 61 | ✗ | if (s->nb_streams != 1) | |
| 62 | ✗ | return AVERROR(EINVAL); | |
| 63 | |||
| 64 | ✗ | if (!wc->header_filename) { | |
| 65 | ✗ | av_log(s, AV_LOG_ERROR, "No header filename provided\n"); | |
| 66 | ✗ | return AVERROR(EINVAL); | |
| 67 | } | ||
| 68 | |||
| 69 | ✗ | wc->prev_pts = AV_NOPTS_VALUE; | |
| 70 | |||
| 71 | EXTERN const FFOutputFormat ff_webm_muxer; | ||
| 72 | |||
| 73 | ✗ | ret = avformat_alloc_output_context2(&wc->avf, &ff_webm_muxer.p, NULL, NULL); | |
| 74 | ✗ | if (ret < 0) | |
| 75 | ✗ | return ret; | |
| 76 | ✗ | oc = wc->avf; | |
| 77 | |||
| 78 | ✗ | ff_format_set_url(oc, wc->header_filename); | |
| 79 | ✗ | wc->header_filename = NULL; | |
| 80 | |||
| 81 | ✗ | oc->interrupt_callback = s->interrupt_callback; | |
| 82 | ✗ | oc->max_delay = s->max_delay; | |
| 83 | ✗ | oc->flags = s->flags & ~AVFMT_FLAG_FLUSH_PACKETS; | |
| 84 | ✗ | oc->strict_std_compliance = s->strict_std_compliance; | |
| 85 | ✗ | oc->avoid_negative_ts = s->avoid_negative_ts; | |
| 86 | |||
| 87 | ✗ | oc->flush_packets = 0; | |
| 88 | |||
| 89 | ✗ | if ((ret = av_dict_copy(&oc->metadata, s->metadata, 0)) < 0) | |
| 90 | ✗ | return ret; | |
| 91 | |||
| 92 | ✗ | st = ff_stream_clone(oc, ost); | |
| 93 | ✗ | if (!st) | |
| 94 | ✗ | return AVERROR(ENOMEM); | |
| 95 | |||
| 96 | ✗ | if (wc->http_method) | |
| 97 | ✗ | if ((ret = av_dict_set(&dict, "method", wc->http_method, 0)) < 0) | |
| 98 | ✗ | return ret; | |
| 99 | ✗ | ret = s->io_open(s, &oc->pb, oc->url, AVIO_FLAG_WRITE, &dict); | |
| 100 | ✗ | av_dict_free(&dict); | |
| 101 | ✗ | if (ret < 0) | |
| 102 | ✗ | return ret; | |
| 103 | ✗ | oc->pb->seekable = 0; | |
| 104 | |||
| 105 | ✗ | if ((ret = av_dict_set_int(&dict, "dash", 1, 0)) < 0 || | |
| 106 | ✗ | (ret = av_dict_set_int(&dict, "cluster_time_limit", | |
| 107 | ✗ | wc->chunk_duration, 0)) < 0 || | |
| 108 | ✗ | (ret = av_dict_set_int(&dict, "live", 1, 0)) < 0) | |
| 109 | ✗ | goto fail; | |
| 110 | |||
| 111 | ✗ | ret = avformat_init_output(oc, &dict); | |
| 112 | ✗ | fail: | |
| 113 | ✗ | av_dict_free(&dict); | |
| 114 | ✗ | if (ret < 0) | |
| 115 | ✗ | return ret; | |
| 116 | |||
| 117 | // Copy the timing info back to the original stream | ||
| 118 | // so that the timestamps of the packets are directly usable | ||
| 119 | ✗ | avpriv_set_pts_info(ost, st->pts_wrap_bits, st->time_base.num, | |
| 120 | ✗ | st->time_base.den); | |
| 121 | |||
| 122 | // This ensures that the timestamps will already be properly shifted | ||
| 123 | // when the packets arrive here, so we don't need to shift again. | ||
| 124 | ✗ | s->avoid_negative_ts = oc->avoid_negative_ts; | |
| 125 | ✗ | ffformatcontext(s)->avoid_negative_ts_use_pts = | |
| 126 | ✗ | ffformatcontext(oc)->avoid_negative_ts_use_pts; | |
| 127 | ✗ | oc->avoid_negative_ts = AVFMT_AVOID_NEG_TS_DISABLED; | |
| 128 | ✗ | ffformatcontext(oc)->avoid_negative_ts_status = AVOID_NEGATIVE_TS_DISABLED; | |
| 129 | |||
| 130 | ✗ | return 0; | |
| 131 | } | ||
| 132 | |||
| 133 | ✗ | static int get_chunk_filename(AVFormatContext *s, AVBPrint *filename) | |
| 134 | { | ||
| 135 | ✗ | WebMChunkContext *wc = s->priv_data; | |
| 136 | ✗ | int ret = ff_bprint_get_frame_filename(filename, s->url, wc->chunk_index - 1, 0); | |
| 137 | ✗ | if (ret < 0) { | |
| 138 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid chunk filename template '%s'\n", s->url); | |
| 139 | ✗ | return ret; | |
| 140 | } | ||
| 141 | ✗ | return 0; | |
| 142 | } | ||
| 143 | |||
| 144 | ✗ | static int webm_chunk_write_header(AVFormatContext *s) | |
| 145 | { | ||
| 146 | ✗ | WebMChunkContext *wc = s->priv_data; | |
| 147 | ✗ | AVFormatContext *oc = wc->avf; | |
| 148 | ✗ | AVStream *st = s->streams[0], *ost = oc->streams[0]; | |
| 149 | int ret; | ||
| 150 | |||
| 151 | ✗ | ret = avformat_write_header(oc, NULL); | |
| 152 | ✗ | ff_format_io_close(s, &oc->pb); | |
| 153 | ✗ | ffstream(st)->lowest_ts_allowed = ffstream(ost)->lowest_ts_allowed; | |
| 154 | ✗ | ffstream(ost)->lowest_ts_allowed = 0; | |
| 155 | ✗ | wc->header_written = 1; | |
| 156 | ✗ | if (ret < 0) | |
| 157 | ✗ | return ret; | |
| 158 | ✗ | return 0; | |
| 159 | } | ||
| 160 | |||
| 161 | ✗ | static int chunk_start(AVFormatContext *s) | |
| 162 | { | ||
| 163 | ✗ | WebMChunkContext *wc = s->priv_data; | |
| 164 | ✗ | AVFormatContext *oc = wc->avf; | |
| 165 | int ret; | ||
| 166 | |||
| 167 | ✗ | ret = avio_open_dyn_buf(&oc->pb); | |
| 168 | ✗ | if (ret < 0) | |
| 169 | ✗ | return ret; | |
| 170 | ✗ | wc->chunk_index++; | |
| 171 | ✗ | return 0; | |
| 172 | } | ||
| 173 | |||
| 174 | ✗ | static int chunk_end(AVFormatContext *s, int flush) | |
| 175 | { | ||
| 176 | ✗ | WebMChunkContext *wc = s->priv_data; | |
| 177 | ✗ | AVFormatContext *oc = wc->avf; | |
| 178 | int ret; | ||
| 179 | int buffer_size; | ||
| 180 | uint8_t *buffer; | ||
| 181 | AVIOContext *pb; | ||
| 182 | AVBPrint filename; | ||
| 183 | ✗ | AVDictionary *options = NULL; | |
| 184 | |||
| 185 | ✗ | if (!oc->pb) | |
| 186 | ✗ | return 0; | |
| 187 | |||
| 188 | ✗ | if (flush) | |
| 189 | // Flush the cluster in WebM muxer. | ||
| 190 | ✗ | av_write_frame(oc, NULL); | |
| 191 | ✗ | buffer_size = avio_close_dyn_buf(oc->pb, &buffer); | |
| 192 | ✗ | oc->pb = NULL; | |
| 193 | ✗ | av_bprint_init(&filename, 0, AV_BPRINT_SIZE_UNLIMITED); | |
| 194 | ✗ | ret = get_chunk_filename(s, &filename); | |
| 195 | ✗ | if (ret < 0) | |
| 196 | ✗ | goto fail; | |
| 197 | ✗ | if (wc->http_method) | |
| 198 | ✗ | if ((ret = av_dict_set(&options, "method", wc->http_method, 0)) < 0) | |
| 199 | ✗ | goto fail; | |
| 200 | ✗ | ret = s->io_open(s, &pb, filename.str, AVIO_FLAG_WRITE, &options); | |
| 201 | ✗ | av_dict_free(&options); | |
| 202 | ✗ | if (ret < 0) | |
| 203 | ✗ | goto fail; | |
| 204 | ✗ | avio_write(pb, buffer, buffer_size); | |
| 205 | ✗ | ff_format_io_close(s, &pb); | |
| 206 | ✗ | fail: | |
| 207 | ✗ | av_bprint_finalize(&filename, NULL); | |
| 208 | ✗ | av_free(buffer); | |
| 209 | ✗ | return (ret < 0) ? ret : 0; | |
| 210 | } | ||
| 211 | |||
| 212 | ✗ | static int webm_chunk_write_packet(AVFormatContext *s, AVPacket *pkt) | |
| 213 | { | ||
| 214 | ✗ | WebMChunkContext *wc = s->priv_data; | |
| 215 | ✗ | AVFormatContext *oc = wc->avf; | |
| 216 | ✗ | AVStream *st = s->streams[pkt->stream_index]; | |
| 217 | int ret; | ||
| 218 | |||
| 219 | ✗ | if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { | |
| 220 | ✗ | if (wc->prev_pts != AV_NOPTS_VALUE) | |
| 221 | ✗ | wc->duration_written += av_rescale_q(pkt->pts - wc->prev_pts, | |
| 222 | st->time_base, | ||
| 223 | ✗ | (AVRational) {1, 1000}); | |
| 224 | ✗ | wc->prev_pts = pkt->pts; | |
| 225 | } | ||
| 226 | |||
| 227 | // For video, a new chunk is started only on key frames. For audio, a new | ||
| 228 | // chunk is started based on chunk_duration. Also, a new chunk is started | ||
| 229 | // unconditionally if there is no currently open chunk. | ||
| 230 | ✗ | if (!oc->pb || (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && | |
| 231 | ✗ | (pkt->flags & AV_PKT_FLAG_KEY)) || | |
| 232 | ✗ | (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && | |
| 233 | ✗ | wc->duration_written >= wc->chunk_duration)) { | |
| 234 | ✗ | wc->duration_written = 0; | |
| 235 | ✗ | if ((ret = chunk_end(s, 1)) < 0 || (ret = chunk_start(s)) < 0) { | |
| 236 | ✗ | return ret; | |
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | // We only have one stream, so use the non-interleaving av_write_frame. | ||
| 241 | ✗ | return av_write_frame(oc, pkt); | |
| 242 | } | ||
| 243 | |||
| 244 | ✗ | static int webm_chunk_write_trailer(AVFormatContext *s) | |
| 245 | { | ||
| 246 | ✗ | WebMChunkContext *wc = s->priv_data; | |
| 247 | ✗ | AVFormatContext *oc = wc->avf; | |
| 248 | int ret; | ||
| 249 | |||
| 250 | ✗ | if (!oc->pb) { | |
| 251 | ✗ | ret = chunk_start(s); | |
| 252 | ✗ | if (ret < 0) | |
| 253 | ✗ | return ret; | |
| 254 | } | ||
| 255 | ✗ | ret = av_write_trailer(oc); | |
| 256 | ✗ | if (ret < 0) | |
| 257 | ✗ | return ret; | |
| 258 | ✗ | return chunk_end(s, 0); | |
| 259 | } | ||
| 260 | |||
| 261 | ✗ | static void webm_chunk_deinit(AVFormatContext *s) | |
| 262 | { | ||
| 263 | ✗ | WebMChunkContext *wc = s->priv_data; | |
| 264 | |||
| 265 | ✗ | if (!wc->avf) | |
| 266 | ✗ | return; | |
| 267 | |||
| 268 | ✗ | if (wc->header_written) | |
| 269 | ✗ | ffio_free_dyn_buf(&wc->avf->pb); | |
| 270 | else | ||
| 271 | ✗ | ff_format_io_close(s, &wc->avf->pb); | |
| 272 | ✗ | avformat_free_context(wc->avf); | |
| 273 | ✗ | wc->avf = NULL; | |
| 274 | } | ||
| 275 | |||
| 276 | #define OFFSET(x) offsetof(WebMChunkContext, x) | ||
| 277 | static const AVOption options[] = { | ||
| 278 | { "chunk_start_index", "start index of the chunk", OFFSET(chunk_index), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM }, | ||
| 279 | { "header", "filename of the header where the initialization data will be written", OFFSET(header_filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM }, | ||
| 280 | { "audio_chunk_duration", "duration of each chunk in milliseconds", OFFSET(chunk_duration), AV_OPT_TYPE_INT, {.i64 = 5000}, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM }, | ||
| 281 | { "method", "set the HTTP method", OFFSET(http_method), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, AV_OPT_FLAG_ENCODING_PARAM }, | ||
| 282 | { NULL }, | ||
| 283 | }; | ||
| 284 | |||
| 285 | static const AVClass webm_chunk_class = { | ||
| 286 | .class_name = "WebM Chunk Muxer", | ||
| 287 | .item_name = av_default_item_name, | ||
| 288 | .option = options, | ||
| 289 | .version = LIBAVUTIL_VERSION_INT, | ||
| 290 | }; | ||
| 291 | |||
| 292 | const FFOutputFormat ff_webm_chunk_muxer = { | ||
| 293 | .p.name = "webm_chunk", | ||
| 294 | .p.long_name = NULL_IF_CONFIG_SMALL("WebM Chunk Muxer"), | ||
| 295 | .p.mime_type = "video/webm", | ||
| 296 | .p.extensions = "chk", | ||
| 297 | .p.flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER | AVFMT_NEEDNUMBER | | ||
| 298 | AVFMT_TS_NONSTRICT, | ||
| 299 | .p.priv_class = &webm_chunk_class, | ||
| 300 | .priv_data_size = sizeof(WebMChunkContext), | ||
| 301 | .init = webm_chunk_init, | ||
| 302 | .write_header = webm_chunk_write_header, | ||
| 303 | .write_packet = webm_chunk_write_packet, | ||
| 304 | .write_trailer = webm_chunk_write_trailer, | ||
| 305 | .deinit = webm_chunk_deinit, | ||
| 306 | }; | ||
| 307 |