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.

292 lines
8.8KB

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