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.

279 lines
8.0KB

  1. /*
  2. * Tee pesudo-muxer
  3. * Copyright (c) 2012 Nicolas George
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public License
  9. * as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg 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
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with FFmpeg; if not, write to the Free Software * Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libavutil/avutil.h"
  22. #include "libavutil/avstring.h"
  23. #include "libavutil/opt.h"
  24. #include "avformat.h"
  25. #define MAX_SLAVES 16
  26. typedef struct TeeContext {
  27. const AVClass *class;
  28. unsigned nb_slaves;
  29. AVFormatContext *slaves[MAX_SLAVES];
  30. } TeeContext;
  31. static const char *const slave_delim = "|";
  32. static const char *const slave_opt_open = "[";
  33. static const char *const slave_opt_close = "]";
  34. static const char *const slave_opt_delim = ":]"; /* must have the close too */
  35. static const AVClass tee_muxer_class = {
  36. .class_name = "Tee muxer",
  37. .item_name = av_default_item_name,
  38. .version = LIBAVUTIL_VERSION_INT,
  39. };
  40. static int parse_slave_options(void *log, char *slave,
  41. AVDictionary **options, char **filename)
  42. {
  43. const char *p;
  44. char *key, *val;
  45. int ret;
  46. if (!strspn(slave, slave_opt_open)) {
  47. *filename = slave;
  48. return 0;
  49. }
  50. p = slave + 1;
  51. if (strspn(p, slave_opt_close)) {
  52. *filename = (char *)p + 1;
  53. return 0;
  54. }
  55. while (1) {
  56. ret = av_opt_get_key_value(&p, "=", slave_opt_delim, 0, &key, &val);
  57. if (ret < 0) {
  58. av_log(log, AV_LOG_ERROR, "No option found near \"%s\"\n", p);
  59. goto fail;
  60. }
  61. ret = av_dict_set(options, key, val,
  62. AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
  63. if (ret < 0)
  64. goto fail;
  65. if (strspn(p, slave_opt_close))
  66. break;
  67. p++;
  68. }
  69. *filename = (char *)p + 1;
  70. return 0;
  71. fail:
  72. av_dict_free(options);
  73. return ret;
  74. }
  75. static int open_slave(AVFormatContext *avf, char *slave, AVFormatContext **ravf)
  76. {
  77. int i, ret;
  78. AVDictionary *options = NULL;
  79. AVDictionaryEntry *entry;
  80. char *filename;
  81. char *format = NULL;
  82. AVFormatContext *avf2 = NULL;
  83. AVStream *st, *st2;
  84. if ((ret = parse_slave_options(avf, slave, &options, &filename)) < 0)
  85. return ret;
  86. if ((entry = av_dict_get(options, "f", NULL, 0))) {
  87. format = entry->value;
  88. entry->value = NULL; /* prevent it from being freed */
  89. av_dict_set(&options, "f", NULL, 0);
  90. }
  91. ret = avformat_alloc_output_context2(&avf2, NULL, format, filename);
  92. if (ret < 0)
  93. goto fail;
  94. av_free(format);
  95. for (i = 0; i < avf->nb_streams; i++) {
  96. st = avf->streams[i];
  97. if (!(st2 = avformat_new_stream(avf2, NULL))) {
  98. ret = AVERROR(ENOMEM);
  99. goto fail;
  100. }
  101. st2->id = st->id;
  102. st2->r_frame_rate = st->r_frame_rate;
  103. st2->time_base = st->time_base;
  104. st2->start_time = st->start_time;
  105. st2->duration = st->duration;
  106. st2->nb_frames = st->nb_frames;
  107. st2->disposition = st->disposition;
  108. st2->sample_aspect_ratio = st->sample_aspect_ratio;
  109. st2->avg_frame_rate = st->avg_frame_rate;
  110. av_dict_copy(&st2->metadata, st->metadata, 0);
  111. if ((ret = avcodec_copy_context(st2->codec, st->codec)) < 0)
  112. goto fail;
  113. }
  114. if (!(avf2->oformat->flags & AVFMT_NOFILE)) {
  115. if ((ret = avio_open(&avf2->pb, filename, AVIO_FLAG_WRITE)) < 0) {
  116. av_log(avf, AV_LOG_ERROR, "Slave '%s': error opening: %s\n",
  117. slave, av_err2str(ret));
  118. goto fail;
  119. }
  120. }
  121. if ((ret = avformat_write_header(avf2, &options)) < 0) {
  122. av_log(avf, AV_LOG_ERROR, "Slave '%s': error writing header: %s\n",
  123. slave, av_err2str(ret));
  124. goto fail;
  125. }
  126. if (options) {
  127. entry = NULL;
  128. while ((entry = av_dict_get(options, "", entry, AV_DICT_IGNORE_SUFFIX)))
  129. av_log(avf2, AV_LOG_ERROR, "Unknown option '%s'\n", entry->key);
  130. ret = AVERROR_OPTION_NOT_FOUND;
  131. goto fail;
  132. }
  133. *ravf = avf2;
  134. return 0;
  135. fail:
  136. av_dict_free(&options);
  137. return ret;
  138. }
  139. static void close_slaves(AVFormatContext *avf)
  140. {
  141. TeeContext *tee = avf->priv_data;
  142. AVFormatContext *avf2;
  143. unsigned i;
  144. for (i = 0; i < tee->nb_slaves; i++) {
  145. avf2 = tee->slaves[i];
  146. avio_close(avf2->pb);
  147. avf2->pb = NULL;
  148. avformat_free_context(avf2);
  149. tee->slaves[i] = NULL;
  150. }
  151. }
  152. static int tee_write_header(AVFormatContext *avf)
  153. {
  154. TeeContext *tee = avf->priv_data;
  155. unsigned nb_slaves = 0, i;
  156. const char *filename = avf->filename;
  157. char *slaves[MAX_SLAVES];
  158. int ret;
  159. while (*filename) {
  160. if (nb_slaves == MAX_SLAVES) {
  161. av_log(avf, AV_LOG_ERROR, "Maximum %d slave muxers reached.\n",
  162. MAX_SLAVES);
  163. ret = AVERROR_PATCHWELCOME;
  164. goto fail;
  165. }
  166. if (!(slaves[nb_slaves++] = av_get_token(&filename, slave_delim))) {
  167. ret = AVERROR(ENOMEM);
  168. goto fail;
  169. }
  170. if (strspn(filename, slave_delim))
  171. filename++;
  172. }
  173. for (i = 0; i < nb_slaves; i++) {
  174. if ((ret = open_slave(avf, slaves[i], &tee->slaves[i])) < 0)
  175. goto fail;
  176. av_freep(&slaves[i]);
  177. }
  178. tee->nb_slaves = nb_slaves;
  179. return 0;
  180. fail:
  181. for (i = 0; i < nb_slaves; i++)
  182. av_freep(&slaves[i]);
  183. close_slaves(avf);
  184. return ret;
  185. }
  186. static int tee_write_trailer(AVFormatContext *avf)
  187. {
  188. TeeContext *tee = avf->priv_data;
  189. AVFormatContext *avf2;
  190. int ret_all = 0, ret;
  191. unsigned i;
  192. for (i = 0; i < tee->nb_slaves; i++) {
  193. avf2 = tee->slaves[i];
  194. if ((ret = av_write_trailer(avf2)) < 0)
  195. if (!ret_all)
  196. ret_all = ret;
  197. if (!(avf2->oformat->flags & AVFMT_NOFILE)) {
  198. if ((ret = avio_close(avf2->pb)) < 0)
  199. if (!ret_all)
  200. ret_all = ret;
  201. avf2->pb = NULL;
  202. }
  203. }
  204. close_slaves(avf);
  205. return ret_all;
  206. }
  207. static int tee_write_packet(AVFormatContext *avf, AVPacket *pkt)
  208. {
  209. TeeContext *tee = avf->priv_data;
  210. AVFormatContext *avf2;
  211. AVPacket pkt2;
  212. int ret_all = 0, ret;
  213. unsigned i, s;
  214. AVRational tb, tb2;
  215. for (i = 0; i < tee->nb_slaves; i++) {
  216. avf2 = tee->slaves[i];
  217. s = pkt->stream_index;
  218. if (s >= avf2->nb_streams) {
  219. if (!ret_all)
  220. ret_all = AVERROR(EINVAL);
  221. continue;
  222. }
  223. if ((ret = av_copy_packet(&pkt2, pkt)) < 0 ||
  224. (ret = av_dup_packet(&pkt2))< 0)
  225. if (!ret_all) {
  226. ret = ret_all;
  227. continue;
  228. }
  229. tb = avf ->streams[s]->time_base;
  230. tb2 = avf2->streams[s]->time_base;
  231. pkt2.pts = av_rescale_q(pkt->pts, tb, tb2);
  232. pkt2.dts = av_rescale_q(pkt->dts, tb, tb2);
  233. pkt2.duration = av_rescale_q(pkt->duration, tb, tb2);
  234. if ((ret = av_interleaved_write_frame(avf2, &pkt2)) < 0)
  235. if (!ret_all)
  236. ret_all = ret;
  237. }
  238. return ret_all;
  239. }
  240. AVOutputFormat ff_tee_muxer = {
  241. .name = "tee",
  242. .long_name = NULL_IF_CONFIG_SMALL("Multiple muxer tee"),
  243. .priv_data_size = sizeof(TeeContext),
  244. .write_header = tee_write_header,
  245. .write_trailer = tee_write_trailer,
  246. .write_packet = tee_write_packet,
  247. .priv_class = &tee_muxer_class,
  248. .flags = AVFMT_NOFILE,
  249. };