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.

624 lines
21KB

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