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.

300 lines
8.7KB

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