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.

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