You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

638 lines
22KB

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