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.

294 lines
8.9KB

  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. AVIOContext *list_pb; ///< list file put-byte context
  37. float time; ///< segment duration
  38. int wrap; ///< number after which the index wraps
  39. int64_t recording_time;
  40. int has_video;
  41. } SegmentContext;
  42. static int segment_start(AVFormatContext *s)
  43. {
  44. SegmentContext *seg = s->priv_data;
  45. AVFormatContext *oc = seg->avf;
  46. int err = 0;
  47. if (seg->wrap)
  48. seg->number %= seg->wrap;
  49. if (av_get_frame_filename(oc->filename, sizeof(oc->filename),
  50. s->filename, seg->number++) < 0) {
  51. av_log(oc, AV_LOG_ERROR, "Invalid segment filename template '%s'\n", s->filename);
  52. return AVERROR(EINVAL);
  53. }
  54. if ((err = avio_open2(&oc->pb, oc->filename, AVIO_FLAG_WRITE,
  55. &s->interrupt_callback, NULL)) < 0)
  56. return err;
  57. if (!oc->priv_data && oc->oformat->priv_data_size > 0) {
  58. oc->priv_data = av_mallocz(oc->oformat->priv_data_size);
  59. if (!oc->priv_data) {
  60. avio_close(oc->pb);
  61. return AVERROR(ENOMEM);
  62. }
  63. if (oc->oformat->priv_class) {
  64. *(const AVClass**)oc->priv_data = oc->oformat->priv_class;
  65. av_opt_set_defaults(oc->priv_data);
  66. }
  67. }
  68. if ((err = oc->oformat->write_header(oc)) < 0) {
  69. goto fail;
  70. }
  71. return 0;
  72. fail:
  73. av_log(oc, AV_LOG_ERROR, "Failure occurred when starting segment '%s'\n",
  74. oc->filename);
  75. avio_close(oc->pb);
  76. av_freep(&oc->priv_data);
  77. return err;
  78. }
  79. static int segment_end(AVFormatContext *s)
  80. {
  81. SegmentContext *seg = s->priv_data;
  82. AVFormatContext *oc = seg->avf;
  83. int ret = 0;
  84. if (oc->oformat->write_trailer)
  85. ret = oc->oformat->write_trailer(oc);
  86. if (ret < 0)
  87. av_log(s, AV_LOG_ERROR, "Failure occurred when ending segment '%s'\n",
  88. oc->filename);
  89. if (seg->list) {
  90. if (seg->list_size && !(seg->number % seg->list_size)) {
  91. avio_close(seg->list_pb);
  92. if ((ret = avio_open2(&seg->list_pb, seg->list, AVIO_FLAG_WRITE,
  93. &s->interrupt_callback, NULL)) < 0)
  94. goto end;
  95. }
  96. avio_printf(seg->list_pb, "%s\n", oc->filename);
  97. avio_flush(seg->list_pb);
  98. }
  99. end:
  100. avio_close(oc->pb);
  101. if (oc->oformat->priv_class)
  102. av_opt_free(oc->priv_data);
  103. av_freep(&oc->priv_data);
  104. return ret;
  105. }
  106. static int seg_write_header(AVFormatContext *s)
  107. {
  108. SegmentContext *seg = s->priv_data;
  109. AVFormatContext *oc;
  110. int ret, i;
  111. seg->number = 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->list_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->list_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 the segment has video, start a new segment *only* with a key video frame */
  173. if ((st->codec->codec_type == AVMEDIA_TYPE_VIDEO || !seg->has_video) &&
  174. av_compare_ts(pkt->pts, st->time_base,
  175. end_pts, AV_TIME_BASE_Q) >= 0 &&
  176. pkt->flags & AV_PKT_FLAG_KEY) {
  177. av_log(s, AV_LOG_DEBUG, "Next segment starts with packet stream:%d pts:%"PRId64" pts_time:%f\n",
  178. pkt->stream_index, pkt->pts, pkt->pts * av_q2d(st->time_base));
  179. if ((ret = segment_end(s)) < 0 || (ret = segment_start(s)) < 0)
  180. goto fail;
  181. }
  182. ret = oc->oformat->write_packet(oc, pkt);
  183. fail:
  184. if (ret < 0) {
  185. oc->streams = NULL;
  186. oc->nb_streams = 0;
  187. if (seg->list)
  188. avio_close(seg->list_pb);
  189. avformat_free_context(oc);
  190. }
  191. return ret;
  192. }
  193. static int seg_write_trailer(struct AVFormatContext *s)
  194. {
  195. SegmentContext *seg = s->priv_data;
  196. AVFormatContext *oc = seg->avf;
  197. int ret = segment_end(s);
  198. if (seg->list)
  199. avio_close(seg->list_pb);
  200. oc->streams = NULL;
  201. oc->nb_streams = 0;
  202. avformat_free_context(oc);
  203. return ret;
  204. }
  205. #define OFFSET(x) offsetof(SegmentContext, x)
  206. #define E AV_OPT_FLAG_ENCODING_PARAM
  207. static const AVOption options[] = {
  208. { "segment_format", "set container format used for the segments", OFFSET(format), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  209. { "segment_time", "set segment length in seconds", OFFSET(time), AV_OPT_TYPE_FLOAT, {.dbl = 2}, 0, FLT_MAX, E },
  210. { "segment_list", "set the segment list filename", OFFSET(list), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, E },
  211. { "segment_list_size", "set the maximum number of playlist entries", OFFSET(list_size), AV_OPT_TYPE_INT, {.dbl = 5}, 0, INT_MAX, E },
  212. { "segment_wrap", "set number after which the index wraps", OFFSET(wrap), AV_OPT_TYPE_INT, {.dbl = 0}, 0, INT_MAX, E },
  213. { NULL },
  214. };
  215. static const AVClass seg_class = {
  216. .class_name = "segment muxer",
  217. .item_name = av_default_item_name,
  218. .option = options,
  219. .version = LIBAVUTIL_VERSION_INT,
  220. };
  221. AVOutputFormat ff_segment_muxer = {
  222. .name = "segment",
  223. .long_name = NULL_IF_CONFIG_SMALL("segment muxer"),
  224. .priv_data_size = sizeof(SegmentContext),
  225. .flags = AVFMT_GLOBALHEADER | AVFMT_NOFILE,
  226. .write_header = seg_write_header,
  227. .write_packet = seg_write_packet,
  228. .write_trailer = seg_write_trailer,
  229. .priv_class = &seg_class,
  230. };
  231. static const AVClass sseg_class = {
  232. .class_name = "stream_segment muxer",
  233. .item_name = av_default_item_name,
  234. .option = options,
  235. .version = LIBAVUTIL_VERSION_INT,
  236. };
  237. AVOutputFormat ff_stream_segment_muxer = {
  238. .name = "stream_segment,ssegment",
  239. .long_name = NULL_IF_CONFIG_SMALL("streaming segment muxer"),
  240. .priv_data_size = sizeof(SegmentContext),
  241. .flags = AVFMT_NOFILE,
  242. .write_header = seg_write_header,
  243. .write_packet = seg_write_packet,
  244. .write_trailer = seg_write_trailer,
  245. .priv_class = &sseg_class,
  246. };