smoothstreamingenc.c 21 KB

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