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.

630 lines
22KB

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