segment.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Generic segmenter
  3. * Copyright (c) 2011, Luca Barbato
  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 <float.h>
  22. #include "avformat.h"
  23. #include "internal.h"
  24. #include "libavutil/log.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/avstring.h"
  27. #include "libavutil/parseutils.h"
  28. #include "libavutil/mathematics.h"
  29. typedef struct {
  30. const AVClass *class; /**< Class for private options. */
  31. int number;
  32. AVFormatContext *avf;
  33. char *format; /**< Set by a private option. */
  34. char *list; /**< Set by a private option. */
  35. int list_size; /**< Set by a private option. */
  36. float time; /**< Set by a private option. */
  37. int wrap; /**< Set by a private option. */
  38. int64_t offset_time;
  39. int64_t recording_time;
  40. int has_video;
  41. AVIOContext *pb;
  42. } SegmentContext;
  43. static int segment_start(AVFormatContext *s)
  44. {
  45. SegmentContext *seg = s->priv_data;
  46. AVFormatContext *oc = seg->avf;
  47. int err = 0;
  48. if (seg->wrap)
  49. seg->number %= seg->wrap;
  50. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  51. s->filename, seg->number++) < 0)
  52. return AVERROR(EINVAL);
  53. if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  54. &s->interrupt_callback, NULL)) < 0)
  55. return err;
  56. if (!oc->priv_data && oc->oformat->priv_data_size > 0) {
  57. oc->priv_data = av_mallocz(oc->oformat->priv_data_size);
  58. if (!oc->priv_data) {
  59. avio_close(oc->pb);
  60. return AVERROR(ENOMEM);
  61. }
  62. if (oc->oformat->priv_class) {
  63. *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
  64. av_opt_set_defaults(oc->priv_data);
  65. }
  66. }
  67. if ((err = oc->oformat->write_header(oc)) < 0) {
  68. goto fail;
  69. }
  70. return 0;
  71. fail:
  72. av_log(oc, AV_LOG_ERROR, "Failure occurred when starting segment '%s'\n",
  73. oc->filename);
  74. avio_close(oc->pb);
  75. av_freep(&oc->priv_data);
  76. return err;
  77. }
  78. static int segment_end(AVFormatContext *s)
  79. {
  80. SegmentContext *seg = s->priv_data;
  81. AVFormatContext *oc = seg->avf;
  82. int ret = 0;
  83. if (oc->oformat->write_trailer)
  84. ret = oc->oformat->write_trailer(oc);
  85. if (ret < 0)
  86. av_log(s, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
  87. oc->filename);
  88. avio_close(oc->pb);
  89. if (oc->oformat->priv_class)
  90. av_opt_free(oc->priv_data);
  91. av_freep(&oc->priv_data);
  92. return ret;
  93. }
  94. static int seg_write_header(AVFormatContext *s)
  95. {
  96. SegmentContext *seg = s->priv_data;
  97. AVFormatContext *oc;
  98. int ret, i;
  99. seg->number = 0;
  100. seg->offset_time = 0;
  101. seg->recording_time = seg->time * 1000000;
  102. oc = avformat_alloc_context();
  103. if (!oc)
  104. return AVERROR(ENOMEM);
  105. if (seg->list)
  106. if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
  107. &s->interrupt_callback, NULL)) < 0)
  108. goto fail;
  109. for (i = 0; i< s->nb_streams; i++)
  110. seg->has_video +=
  111. (s->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO);
  112. if (seg->has_video > 1)
  113. av_log(s, AV_LOG_WARNING,
  114. "More than a single video stream present, "
  115. "expect issues decoding it.\n");
  116. oc->oformat = av_guess_format(seg->format, s->filename, NULL);
  117. if (!oc->oformat) {
  118. ret = AVERROR_MUXER_NOT_FOUND;
  119. goto fail;
  120. }
  121. if (oc->oformat->flags & AVFMT_NOFILE) {
  122. av_log(s, AV_LOG_ERROR, "format %s not supported.\n",
  123. oc->oformat->name);
  124. ret = AVERROR(EINVAL);
  125. goto fail;
  126. }
  127. seg->avf = oc;
  128. oc->streams = s->streams;
  129. oc->nb_streams = s->nb_streams;
  130. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  131. s->filename, seg->number++) < 0) {
  132. ret = AVERROR(EINVAL);
  133. goto fail;
  134. }
  135. if ((ret = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  136. &s->interrupt_callback, NULL)) < 0)
  137. goto fail;
  138. if ((ret = avformat_write_header(oc, NULL)) < 0) {
  139. avio_close(oc->pb);
  140. goto fail;
  141. }
  142. if (seg->list) {
  143. avio_printf(seg->pb, "%s\n", oc->filename);
  144. avio_flush(seg->pb);
  145. }
  146. fail:
  147. if (ret) {
  148. if (oc) {
  149. oc->streams = NULL;
  150. oc->nb_streams = 0;
  151. avformat_free_context(oc);
  152. }
  153. if (seg->list)
  154. avio_close(seg->pb);
  155. }
  156. return ret;
  157. }
  158. static int seg_write_packet(AVFormatContext *s, AVPacket *pkt)
  159. {
  160. SegmentContext *seg = s->priv_data;
  161. AVFormatContext *oc = seg->avf;
  162. AVStream *st = oc->streams[pkt->stream_index];
  163. int64_t end_pts = seg->recording_time * seg->number;
  164. int ret;
  165. if ((seg->has_video && st->codec->codec_type == AVMEDIA_TYPE_VIDEO) &&
  166. av_compare_ts(pkt->pts, st->time_base,
  167. end_pts, AV_TIME_BASE_Q) >= 0 &&
  168. pkt->flags & AV_PKT_FLAG_KEY) {
  169. av_log(s, AV_LOG_DEBUG, "Next segment starts at %d %"PRId64"\n",
  170. pkt->stream_index, pkt->pts);
  171. ret = segment_end(s);
  172. if (!ret)
  173. ret = segment_start(s);
  174. if (ret)
  175. goto fail;
  176. if (seg->list) {
  177. avio_printf(seg->pb, "%s\n", oc->filename);
  178. avio_flush(seg->pb);
  179. if (seg->list_size && !(seg->number % seg->list_size)) {
  180. avio_close(seg->pb);
  181. if ((ret = avio_open2(&seg->pb, seg->list, AVIO_FLAG_WRITE,
  182. &s->interrupt_callback, NULL)) < 0)
  183. goto fail;
  184. }
  185. }
  186. }
  187. ret = oc->oformat->write_packet(oc, pkt);
  188. fail:
  189. if (ret < 0) {
  190. oc->streams = NULL;
  191. oc->nb_streams = 0;
  192. if (seg->list)
  193. avio_close(seg->pb);
  194. avformat_free_context(oc);
  195. }
  196. return ret;
  197. }
  198. static int seg_write_trailer(struct AVFormatContext *s)
  199. {
  200. SegmentContext *seg = s->priv_data;
  201. AVFormatContext *oc = seg->avf;
  202. int ret = segment_end(s);
  203. if (seg->list)
  204. avio_close(seg->pb);
  205. oc->streams = NULL;
  206. oc->nb_streams = 0;
  207. avformat_free_context(oc);
  208. return ret;
  209. }
  210. #define OFFSET(x) offsetof(SegmentContext, x)
  211. #define E AV_OPT_FLAG_ENCODING_PARAM
  212. static const AVOption options[] = {
  213. { "segment_format", "container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  214. { "segment_time", "segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
  215. { "segment_list", "output the segment list", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  216. { "segment_list_size", "maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT, {.dbl = 5}, 0, INT_MAX, E },
  217. { "segment_wrap", "number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, E },
  218. { NULL },
  219. };
  220. static const AVClass seg_class = {
  221. .class_name = "segment muxer",
  222. .item_name = av_default_item_name,
  223. .option = options,
  224. .version = LIBAVUTIL_VERSION_INT,
  225. };
  226. AVOutputFormat ff_segment_muxer = {
  227. .name = "segment",
  228. .long_name = NULL_IF_CONFIG_SMALL("segment muxer"),
  229. .priv_data_size = sizeof(SegmentContext),
  230. .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
  231. .write_header = seg_write_header,
  232. .write_packet = seg_write_packet,
  233. .write_trailer = seg_write_trailer,
  234. .priv_class = &seg_class,
  235. };
  236. static const AVClass sseg_class = {
  237. .class_name = "stream_segment muxer",
  238. .item_name = av_default_item_name,
  239. .option = options,
  240. .version = LIBAVUTIL_VERSION_INT,
  241. };
  242. AVOutputFormat ff_stream_segment_muxer = {
  243. .name = "stream_segment,ssegment",
  244. .long_name = NULL_IF_CONFIG_SMALL("streaming segment muxer"),
  245. .priv_data_size = sizeof(SegmentContext),
  246. .flags = AVFMT_NOFILE,
  247. .write_header = seg_write_header,
  248. .write_packet = seg_write_packet,
  249. .write_trailer = seg_write_trailer,
  250. .priv_class = &sseg_class,
  251. };