obs-ffmpeg-output.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. /******************************************************************************
  2. Copyright (C) 2014 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <obs-module.h>
  15. #include <util/circlebuf.h>
  16. #include <util/threading.h>
  17. #include <util/dstr.h>
  18. #include <util/darray.h>
  19. #include <util/platform.h>
  20. #include "obs-ffmpeg-output.h"
  21. #include "obs-ffmpeg-formats.h"
  22. #include "obs-ffmpeg-compat.h"
  23. #include <libavutil/channel_layout.h>
  24. #include <libavutil/mastering_display_metadata.h>
  25. /* ------------------------------------------------------------------------- */
  26. static void ffmpeg_output_set_last_error(struct ffmpeg_data *data,
  27. const char *error)
  28. {
  29. if (data->last_error)
  30. bfree(data->last_error);
  31. data->last_error = bstrdup(error);
  32. }
  33. void ffmpeg_log_error(int log_level, struct ffmpeg_data *data,
  34. const char *format, ...)
  35. {
  36. va_list args;
  37. char out[4096];
  38. va_start(args, format);
  39. vsnprintf(out, sizeof(out), format, args);
  40. va_end(args);
  41. ffmpeg_output_set_last_error(data, out);
  42. blog(log_level, "%s", out);
  43. }
  44. static bool new_stream(struct ffmpeg_data *data, AVStream **stream,
  45. const AVCodec **codec, enum AVCodecID id,
  46. const char *name)
  47. {
  48. *codec = (!!name && *name) ? avcodec_find_encoder_by_name(name)
  49. : avcodec_find_encoder(id);
  50. if (!*codec) {
  51. ffmpeg_log_error(LOG_WARNING, data,
  52. "Couldn't find encoder '%s'",
  53. avcodec_get_name(id));
  54. return false;
  55. }
  56. *stream = avformat_new_stream(data->output, *codec);
  57. if (!*stream) {
  58. ffmpeg_log_error(LOG_WARNING, data,
  59. "Couldn't create stream for encoder '%s'",
  60. avcodec_get_name(id));
  61. return false;
  62. }
  63. (*stream)->id = data->output->nb_streams - 1;
  64. return true;
  65. }
  66. static bool parse_params(AVCodecContext *context, char **opts)
  67. {
  68. bool ret = true;
  69. if (!context || !context->priv_data)
  70. return true;
  71. while (*opts) {
  72. char *opt = *opts;
  73. char *assign = strchr(opt, '=');
  74. if (assign) {
  75. char *name = opt;
  76. char *value;
  77. *assign = 0;
  78. value = assign + 1;
  79. if (av_opt_set(context, name, value,
  80. AV_OPT_SEARCH_CHILDREN)) {
  81. blog(LOG_WARNING, "Failed to set %s=%s", name,
  82. value);
  83. ret = false;
  84. }
  85. }
  86. opts++;
  87. }
  88. return ret;
  89. }
  90. static bool open_video_codec(struct ffmpeg_data *data)
  91. {
  92. AVCodecContext *const context = data->video_ctx;
  93. char **opts = strlist_split(data->config.video_settings, ' ', false);
  94. int ret;
  95. if (strcmp(data->vcodec->name, "libx264") == 0)
  96. av_opt_set(context->priv_data, "preset", "veryfast", 0);
  97. if (opts) {
  98. // libav requires x264 parameters in a special format which may be non-obvious
  99. if (!parse_params(context, opts) &&
  100. strcmp(data->vcodec->name, "libx264") == 0)
  101. blog(LOG_WARNING,
  102. "If you're trying to set x264 parameters, use x264-params=name=value:name=value");
  103. strlist_free(opts);
  104. }
  105. ret = avcodec_open2(context, data->vcodec, NULL);
  106. if (ret < 0) {
  107. ffmpeg_log_error(LOG_WARNING, data,
  108. "Failed to open video codec: %s",
  109. av_err2str(ret));
  110. return false;
  111. }
  112. data->vframe = av_frame_alloc();
  113. if (!data->vframe) {
  114. ffmpeg_log_error(LOG_WARNING, data,
  115. "Failed to allocate video frame");
  116. return false;
  117. }
  118. data->vframe->format = context->pix_fmt;
  119. data->vframe->width = context->width;
  120. data->vframe->height = context->height;
  121. data->vframe->color_range = data->config.color_range;
  122. data->vframe->color_primaries = data->config.color_primaries;
  123. data->vframe->color_trc = data->config.color_trc;
  124. data->vframe->colorspace = data->config.colorspace;
  125. data->vframe->chroma_location = determine_chroma_location(
  126. context->pix_fmt, data->config.colorspace);
  127. ret = av_frame_get_buffer(data->vframe, base_get_alignment());
  128. if (ret < 0) {
  129. ffmpeg_log_error(LOG_WARNING, data,
  130. "Failed to allocate vframe: %s",
  131. av_err2str(ret));
  132. return false;
  133. }
  134. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  135. avcodec_parameters_from_context(data->video->codecpar, context);
  136. #endif
  137. return true;
  138. }
  139. static bool init_swscale(struct ffmpeg_data *data, AVCodecContext *context)
  140. {
  141. data->swscale = sws_getContext(
  142. data->config.width, data->config.height, data->config.format,
  143. data->config.scale_width, data->config.scale_height,
  144. context->pix_fmt, SWS_BICUBIC, NULL, NULL, NULL);
  145. if (!data->swscale) {
  146. ffmpeg_log_error(LOG_WARNING, data,
  147. "Could not initialize swscale");
  148. return false;
  149. }
  150. return true;
  151. }
  152. static bool create_video_stream(struct ffmpeg_data *data)
  153. {
  154. enum AVPixelFormat closest_format;
  155. AVCodecContext *context;
  156. struct obs_video_info ovi;
  157. if (!obs_get_video_info(&ovi)) {
  158. ffmpeg_log_error(LOG_WARNING, data, "No active video");
  159. return false;
  160. }
  161. if (!new_stream(data, &data->video, &data->vcodec,
  162. data->output->oformat->video_codec,
  163. data->config.video_encoder))
  164. return false;
  165. const enum AVColorTransferCharacteristic trc = data->config.color_trc;
  166. const bool pq = trc == AVCOL_TRC_SMPTE2084;
  167. const bool hlg = trc == AVCOL_TRC_ARIB_STD_B67;
  168. if (pq || hlg) {
  169. const int hdr_nominal_peak_level =
  170. pq ? (int)obs_get_video_hdr_nominal_peak_level()
  171. : (hlg ? 1000 : 0);
  172. size_t content_size;
  173. AVContentLightMetadata *const content =
  174. av_content_light_metadata_alloc(&content_size);
  175. content->MaxCLL = hdr_nominal_peak_level;
  176. content->MaxFALL = hdr_nominal_peak_level;
  177. av_stream_add_side_data(data->video,
  178. AV_PKT_DATA_CONTENT_LIGHT_LEVEL,
  179. (uint8_t *)content, content_size);
  180. AVMasteringDisplayMetadata *const mastering =
  181. av_mastering_display_metadata_alloc();
  182. mastering->display_primaries[0][0] = av_make_q(17, 25);
  183. mastering->display_primaries[0][1] = av_make_q(8, 25);
  184. mastering->display_primaries[1][0] = av_make_q(53, 200);
  185. mastering->display_primaries[1][1] = av_make_q(69, 100);
  186. mastering->display_primaries[2][0] = av_make_q(3, 20);
  187. mastering->display_primaries[2][1] = av_make_q(3, 50);
  188. mastering->white_point[0] = av_make_q(3127, 10000);
  189. mastering->white_point[1] = av_make_q(329, 1000);
  190. mastering->min_luminance = av_make_q(0, 1);
  191. mastering->max_luminance = av_make_q(hdr_nominal_peak_level, 1);
  192. mastering->has_primaries = 1;
  193. mastering->has_luminance = 1;
  194. av_stream_add_side_data(data->video,
  195. AV_PKT_DATA_MASTERING_DISPLAY_METADATA,
  196. (uint8_t *)mastering,
  197. sizeof(*mastering));
  198. }
  199. closest_format = data->config.format;
  200. if (data->vcodec->pix_fmts) {
  201. closest_format = avcodec_find_best_pix_fmt_of_list(
  202. data->vcodec->pix_fmts, data->config.format, 0, NULL);
  203. }
  204. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  205. context = avcodec_alloc_context3(data->vcodec);
  206. #else
  207. context = data->video->codec;
  208. #endif
  209. context->bit_rate = (int64_t)data->config.video_bitrate * 1000;
  210. context->width = data->config.scale_width;
  211. context->height = data->config.scale_height;
  212. context->time_base = (AVRational){ovi.fps_den, ovi.fps_num};
  213. context->gop_size = data->config.gop_size;
  214. context->pix_fmt = closest_format;
  215. context->color_range = data->config.color_range;
  216. context->color_primaries = data->config.color_primaries;
  217. context->color_trc = data->config.color_trc;
  218. context->colorspace = data->config.colorspace;
  219. context->chroma_sample_location = determine_chroma_location(
  220. closest_format, data->config.colorspace);
  221. context->thread_count = 0;
  222. data->video->time_base = context->time_base;
  223. if (data->output->oformat->flags & AVFMT_GLOBALHEADER)
  224. context->flags |= CODEC_FLAG_GLOBAL_H;
  225. data->video_ctx = context;
  226. if (!open_video_codec(data))
  227. return false;
  228. if (context->pix_fmt != data->config.format ||
  229. data->config.width != data->config.scale_width ||
  230. data->config.height != data->config.scale_height) {
  231. if (!init_swscale(data, context))
  232. return false;
  233. }
  234. return true;
  235. }
  236. static bool open_audio_codec(struct ffmpeg_data *data, int idx)
  237. {
  238. AVCodecContext *const context = data->audio_infos[idx].ctx;
  239. char **opts = strlist_split(data->config.audio_settings, ' ', false);
  240. int ret;
  241. int channels;
  242. if (opts) {
  243. parse_params(context, opts);
  244. strlist_free(opts);
  245. }
  246. data->aframe[idx] = av_frame_alloc();
  247. if (!data->aframe[idx]) {
  248. ffmpeg_log_error(LOG_WARNING, data,
  249. "Failed to allocate audio frame");
  250. return false;
  251. }
  252. data->aframe[idx]->format = context->sample_fmt;
  253. #if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 24, 100)
  254. data->aframe[idx]->channels = context->channels;
  255. data->aframe[idx]->channel_layout = context->channel_layout;
  256. channels = context->channels;
  257. #else
  258. data->aframe[idx]->ch_layout = context->ch_layout;
  259. channels = context->ch_layout.nb_channels;
  260. #endif
  261. data->aframe[idx]->sample_rate = context->sample_rate;
  262. context->strict_std_compliance = -2;
  263. ret = avcodec_open2(context, data->acodec, NULL);
  264. if (ret < 0) {
  265. ffmpeg_log_error(LOG_WARNING, data,
  266. "Failed to open audio codec: %s",
  267. av_err2str(ret));
  268. return false;
  269. }
  270. data->frame_size = context->frame_size ? context->frame_size : 1024;
  271. ret = av_samples_alloc(data->samples[idx], NULL, channels,
  272. data->frame_size, context->sample_fmt, 0);
  273. if (ret < 0) {
  274. ffmpeg_log_error(LOG_WARNING, data,
  275. "Failed to create audio buffer: %s",
  276. av_err2str(ret));
  277. return false;
  278. }
  279. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  280. avcodec_parameters_from_context(data->audio_infos[idx].stream->codecpar,
  281. context);
  282. #endif
  283. return true;
  284. }
  285. static bool create_audio_stream(struct ffmpeg_data *data, int idx)
  286. {
  287. AVCodecContext *context;
  288. AVStream *stream;
  289. struct obs_audio_info aoi;
  290. int channels;
  291. if (!obs_get_audio_info(&aoi)) {
  292. ffmpeg_log_error(LOG_WARNING, data, "No active audio");
  293. return false;
  294. }
  295. if (!new_stream(data, &stream, &data->acodec,
  296. data->output->oformat->audio_codec,
  297. data->config.audio_encoder))
  298. return false;
  299. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  300. context = avcodec_alloc_context3(data->acodec);
  301. #else
  302. context = stream->codec;
  303. #endif
  304. context->bit_rate = (int64_t)data->config.audio_bitrate * 1000;
  305. context->time_base = (AVRational){1, aoi.samples_per_sec};
  306. #if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 24, 100)
  307. context->channels = get_audio_channels(aoi.speakers);
  308. #endif
  309. channels = get_audio_channels(aoi.speakers);
  310. context->sample_rate = aoi.samples_per_sec;
  311. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 24, 100)
  312. context->channel_layout = av_get_default_channel_layout(channels);
  313. //avutil default channel layout for 5 channels is 5.0 ; fix for 4.1
  314. if (aoi.speakers == SPEAKERS_4POINT1)
  315. context->channel_layout = av_get_channel_layout("4.1");
  316. #else
  317. av_channel_layout_default(&context->ch_layout, channels);
  318. if (aoi.speakers == SPEAKERS_4POINT1)
  319. context->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_4POINT1;
  320. #endif
  321. context->sample_fmt = data->acodec->sample_fmts
  322. ? data->acodec->sample_fmts[0]
  323. : AV_SAMPLE_FMT_FLTP;
  324. stream->time_base = context->time_base;
  325. data->audio_samplerate = aoi.samples_per_sec;
  326. data->audio_format = convert_ffmpeg_sample_format(context->sample_fmt);
  327. data->audio_planes = get_audio_planes(data->audio_format, aoi.speakers);
  328. data->audio_size = get_audio_size(data->audio_format, aoi.speakers, 1);
  329. if (data->output->oformat->flags & AVFMT_GLOBALHEADER)
  330. context->flags |= CODEC_FLAG_GLOBAL_H;
  331. data->audio_infos[idx].stream = stream;
  332. data->audio_infos[idx].ctx = context;
  333. return open_audio_codec(data, idx);
  334. }
  335. static inline bool init_streams(struct ffmpeg_data *data)
  336. {
  337. const AVOutputFormat *format = data->output->oformat;
  338. if (format->video_codec != AV_CODEC_ID_NONE)
  339. if (!create_video_stream(data))
  340. return false;
  341. if (format->audio_codec != AV_CODEC_ID_NONE &&
  342. data->num_audio_streams) {
  343. data->audio_infos = calloc(data->num_audio_streams,
  344. sizeof(*data->audio_infos));
  345. for (int i = 0; i < data->num_audio_streams; i++) {
  346. if (!create_audio_stream(data, i))
  347. return false;
  348. }
  349. }
  350. return true;
  351. }
  352. static inline bool open_output_file(struct ffmpeg_data *data)
  353. {
  354. const AVOutputFormat *format = data->output->oformat;
  355. int ret;
  356. AVDictionary *dict = NULL;
  357. if ((ret = av_dict_parse_string(&dict, data->config.muxer_settings, "=",
  358. " ", 0))) {
  359. ffmpeg_log_error(LOG_WARNING, data,
  360. "Failed to parse muxer settings: %s\n%s",
  361. av_err2str(ret), data->config.muxer_settings);
  362. av_dict_free(&dict);
  363. return false;
  364. }
  365. if (av_dict_count(dict) > 0) {
  366. struct dstr str = {0};
  367. AVDictionaryEntry *entry = NULL;
  368. while ((entry = av_dict_get(dict, "", entry,
  369. AV_DICT_IGNORE_SUFFIX)))
  370. dstr_catf(&str, "\n\t%s=%s", entry->key, entry->value);
  371. blog(LOG_INFO, "Using muxer settings: %s", str.array);
  372. dstr_free(&str);
  373. }
  374. if ((format->flags & AVFMT_NOFILE) == 0) {
  375. ret = avio_open2(&data->output->pb, data->config.url,
  376. AVIO_FLAG_WRITE, NULL, &dict);
  377. if (ret < 0) {
  378. ffmpeg_log_error(LOG_WARNING, data,
  379. "Couldn't open '%s', %s",
  380. data->config.url, av_err2str(ret));
  381. av_dict_free(&dict);
  382. return false;
  383. }
  384. }
  385. ret = avformat_write_header(data->output, &dict);
  386. if (ret < 0) {
  387. ffmpeg_log_error(LOG_WARNING, data, "Error opening '%s': %s",
  388. data->config.url, av_err2str(ret));
  389. return false;
  390. }
  391. if (av_dict_count(dict) > 0) {
  392. struct dstr str = {0};
  393. AVDictionaryEntry *entry = NULL;
  394. while ((entry = av_dict_get(dict, "", entry,
  395. AV_DICT_IGNORE_SUFFIX)))
  396. dstr_catf(&str, "\n\t%s=%s", entry->key, entry->value);
  397. blog(LOG_INFO, "Invalid muxer settings: %s", str.array);
  398. dstr_free(&str);
  399. }
  400. av_dict_free(&dict);
  401. return true;
  402. }
  403. static void close_video(struct ffmpeg_data *data)
  404. {
  405. avcodec_free_context(&data->video_ctx);
  406. av_frame_unref(data->vframe);
  407. // This format for some reason derefs video frame
  408. // too many times
  409. if (data->vcodec->id == AV_CODEC_ID_A64_MULTI ||
  410. data->vcodec->id == AV_CODEC_ID_A64_MULTI5)
  411. return;
  412. av_frame_free(&data->vframe);
  413. }
  414. static void close_audio(struct ffmpeg_data *data)
  415. {
  416. for (int idx = 0; idx < data->num_audio_streams; idx++) {
  417. for (size_t i = 0; i < MAX_AV_PLANES; i++)
  418. circlebuf_free(&data->excess_frames[idx][i]);
  419. if (data->samples[idx][0])
  420. av_freep(&data->samples[idx][0]);
  421. if (data->audio_infos[idx].ctx)
  422. avcodec_free_context(&data->audio_infos[idx].ctx);
  423. if (data->aframe[idx])
  424. av_frame_free(&data->aframe[idx]);
  425. }
  426. }
  427. void ffmpeg_data_free(struct ffmpeg_data *data)
  428. {
  429. if (data->initialized)
  430. av_write_trailer(data->output);
  431. if (data->video)
  432. close_video(data);
  433. if (data->audio_infos) {
  434. close_audio(data);
  435. free(data->audio_infos);
  436. data->audio_infos = NULL;
  437. }
  438. if (data->output) {
  439. if ((data->output->oformat->flags & AVFMT_NOFILE) == 0)
  440. avio_close(data->output->pb);
  441. avformat_free_context(data->output);
  442. }
  443. if (data->last_error)
  444. bfree(data->last_error);
  445. memset(data, 0, sizeof(struct ffmpeg_data));
  446. }
  447. static inline const char *safe_str(const char *s)
  448. {
  449. if (s == NULL)
  450. return "(NULL)";
  451. else
  452. return s;
  453. }
  454. static enum AVCodecID get_codec_id(const char *name, int id)
  455. {
  456. const AVCodec *codec;
  457. if (id != 0)
  458. return (enum AVCodecID)id;
  459. if (!name || !*name)
  460. return AV_CODEC_ID_NONE;
  461. codec = avcodec_find_encoder_by_name(name);
  462. if (!codec)
  463. return AV_CODEC_ID_NONE;
  464. return codec->id;
  465. }
  466. #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59, 0, 100)
  467. static void set_encoder_ids(struct ffmpeg_data *data)
  468. {
  469. data->output->oformat->video_codec = get_codec_id(
  470. data->config.video_encoder, data->config.video_encoder_id);
  471. data->output->oformat->audio_codec = get_codec_id(
  472. data->config.audio_encoder, data->config.audio_encoder_id);
  473. }
  474. #endif
  475. bool ffmpeg_data_init(struct ffmpeg_data *data, struct ffmpeg_cfg *config)
  476. {
  477. bool is_rtmp = false;
  478. memset(data, 0, sizeof(struct ffmpeg_data));
  479. data->config = *config;
  480. data->num_audio_streams = config->audio_mix_count;
  481. data->audio_tracks = config->audio_tracks;
  482. if (!config->url || !*config->url)
  483. return false;
  484. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  485. av_register_all();
  486. #endif
  487. avformat_network_init();
  488. is_rtmp = (astrcmpi_n(config->url, "rtmp://", 7) == 0);
  489. #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59, 0, 100)
  490. AVOutputFormat *output_format;
  491. #else
  492. const AVOutputFormat *output_format;
  493. #endif
  494. output_format = av_guess_format(
  495. is_rtmp ? "flv" : data->config.format_name, data->config.url,
  496. is_rtmp ? NULL : data->config.format_mime_type);
  497. if (output_format == NULL) {
  498. ffmpeg_log_error(
  499. LOG_WARNING, data,
  500. "Couldn't find matching output format with "
  501. "parameters: name=%s, url=%s, mime=%s",
  502. safe_str(is_rtmp ? "flv" : data->config.format_name),
  503. safe_str(data->config.url),
  504. safe_str(is_rtmp ? NULL
  505. : data->config.format_mime_type));
  506. goto fail;
  507. }
  508. avformat_alloc_output_context2(&data->output, output_format, NULL,
  509. data->config.url);
  510. if (!data->output) {
  511. ffmpeg_log_error(LOG_WARNING, data,
  512. "Couldn't create avformat context");
  513. goto fail;
  514. }
  515. #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59, 0, 100)
  516. if (is_rtmp) {
  517. data->output->oformat->video_codec = AV_CODEC_ID_H264;
  518. data->output->oformat->audio_codec = AV_CODEC_ID_AAC;
  519. } else {
  520. if (data->config.format_name)
  521. set_encoder_ids(data);
  522. }
  523. #else
  524. if (is_rtmp) {
  525. data->config.audio_encoder = "aac";
  526. data->config.audio_encoder_id = AV_CODEC_ID_AAC;
  527. data->config.video_encoder = "libx264";
  528. data->config.video_encoder_id = AV_CODEC_ID_H264;
  529. }
  530. #endif
  531. if (!init_streams(data))
  532. goto fail;
  533. if (!open_output_file(data))
  534. goto fail;
  535. av_dump_format(data->output, 0, NULL, 1);
  536. data->initialized = true;
  537. return true;
  538. fail:
  539. blog(LOG_WARNING, "ffmpeg_data_init failed");
  540. return false;
  541. }
  542. /* ------------------------------------------------------------------------- */
  543. static inline bool stopping(struct ffmpeg_output *output)
  544. {
  545. return os_atomic_load_bool(&output->stopping);
  546. }
  547. static const char *ffmpeg_output_getname(void *unused)
  548. {
  549. UNUSED_PARAMETER(unused);
  550. return obs_module_text("FFmpegOutput");
  551. }
  552. static void ffmpeg_log_callback(void *param, int level, const char *format,
  553. va_list args)
  554. {
  555. if (level <= AV_LOG_INFO)
  556. blogva(LOG_DEBUG, format, args);
  557. UNUSED_PARAMETER(param);
  558. }
  559. static void *ffmpeg_output_create(obs_data_t *settings, obs_output_t *output)
  560. {
  561. struct ffmpeg_output *data = bzalloc(sizeof(struct ffmpeg_output));
  562. pthread_mutex_init_value(&data->write_mutex);
  563. data->output = output;
  564. if (pthread_mutex_init(&data->write_mutex, NULL) != 0)
  565. goto fail;
  566. if (os_event_init(&data->stop_event, OS_EVENT_TYPE_AUTO) != 0)
  567. goto fail;
  568. if (os_sem_init(&data->write_sem, 0) != 0)
  569. goto fail;
  570. av_log_set_callback(ffmpeg_log_callback);
  571. UNUSED_PARAMETER(settings);
  572. return data;
  573. fail:
  574. pthread_mutex_destroy(&data->write_mutex);
  575. os_event_destroy(data->stop_event);
  576. bfree(data);
  577. return NULL;
  578. }
  579. static void ffmpeg_output_full_stop(void *data);
  580. static void ffmpeg_deactivate(struct ffmpeg_output *output);
  581. static void ffmpeg_output_destroy(void *data)
  582. {
  583. struct ffmpeg_output *output = data;
  584. if (output) {
  585. if (output->connecting)
  586. pthread_join(output->start_thread, NULL);
  587. ffmpeg_output_full_stop(output);
  588. pthread_mutex_destroy(&output->write_mutex);
  589. os_sem_destroy(output->write_sem);
  590. os_event_destroy(output->stop_event);
  591. bfree(data);
  592. }
  593. }
  594. static inline void copy_data(AVFrame *pic, const struct video_data *frame,
  595. int height, enum AVPixelFormat format)
  596. {
  597. int h_chroma_shift, v_chroma_shift;
  598. av_pix_fmt_get_chroma_sub_sample(format, &h_chroma_shift,
  599. &v_chroma_shift);
  600. for (int plane = 0; plane < MAX_AV_PLANES; plane++) {
  601. if (!frame->data[plane])
  602. continue;
  603. int frame_rowsize = (int)frame->linesize[plane];
  604. int pic_rowsize = pic->linesize[plane];
  605. int bytes = frame_rowsize < pic_rowsize ? frame_rowsize
  606. : pic_rowsize;
  607. int plane_height = height >> (plane ? v_chroma_shift : 0);
  608. for (int y = 0; y < plane_height; y++) {
  609. int pos_frame = y * frame_rowsize;
  610. int pos_pic = y * pic_rowsize;
  611. memcpy(pic->data[plane] + pos_pic,
  612. frame->data[plane] + pos_frame, bytes);
  613. }
  614. }
  615. }
  616. static void receive_video(void *param, struct video_data *frame)
  617. {
  618. struct ffmpeg_output *output = param;
  619. struct ffmpeg_data *data = &output->ff_data;
  620. // codec doesn't support video or none configured
  621. if (!data->video)
  622. return;
  623. AVCodecContext *context = data->video_ctx;
  624. AVPacket *packet = NULL;
  625. int ret = 0, got_packet;
  626. if (!output->video_start_ts)
  627. output->video_start_ts = frame->timestamp;
  628. if (!data->start_timestamp)
  629. data->start_timestamp = frame->timestamp;
  630. ret = av_frame_make_writable(data->vframe);
  631. if (ret < 0) {
  632. blog(LOG_WARNING,
  633. "receive_video: Error obtaining writable "
  634. "AVFrame: %s",
  635. av_err2str(ret));
  636. //FIXME: stop the encode with an error
  637. return;
  638. }
  639. if (!!data->swscale)
  640. sws_scale(data->swscale, (const uint8_t *const *)frame->data,
  641. (const int *)frame->linesize, 0, data->config.height,
  642. data->vframe->data, data->vframe->linesize);
  643. else
  644. copy_data(data->vframe, frame, context->height,
  645. context->pix_fmt);
  646. packet = av_packet_alloc();
  647. #if LIBAVFORMAT_VERSION_MAJOR < 58
  648. if (data->output->flags & AVFMT_RAWPICTURE) {
  649. packet->flags |= AV_PKT_FLAG_KEY;
  650. packet->stream_index = data->video->index;
  651. packet->data = data->vframe->data[0];
  652. packet->size = sizeof(AVPicture);
  653. pthread_mutex_lock(&output->write_mutex);
  654. da_push_back(output->packets, &packet);
  655. packet = NULL;
  656. pthread_mutex_unlock(&output->write_mutex);
  657. os_sem_post(output->write_sem);
  658. } else {
  659. #endif
  660. data->vframe->pts = data->total_frames;
  661. #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
  662. ret = avcodec_send_frame(context, data->vframe);
  663. if (ret == 0)
  664. ret = avcodec_receive_packet(context, packet);
  665. got_packet = (ret == 0);
  666. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  667. ret = 0;
  668. #else
  669. ret = avcodec_encode_video2(context, packet, data->vframe, &got_packet);
  670. #endif
  671. if (ret < 0) {
  672. blog(LOG_WARNING,
  673. "receive_video: Error encoding "
  674. "video: %s",
  675. av_err2str(ret));
  676. //FIXME: stop the encode with an error
  677. goto fail;
  678. }
  679. if (!ret && got_packet && packet->size) {
  680. packet->pts = rescale_ts(packet->pts, context,
  681. data->video->time_base);
  682. packet->dts = rescale_ts(packet->dts, context,
  683. data->video->time_base);
  684. packet->duration = (int)av_rescale_q(
  685. packet->duration, context->time_base,
  686. data->video->time_base);
  687. pthread_mutex_lock(&output->write_mutex);
  688. da_push_back(output->packets, &packet);
  689. packet = NULL;
  690. pthread_mutex_unlock(&output->write_mutex);
  691. os_sem_post(output->write_sem);
  692. } else {
  693. ret = 0;
  694. }
  695. #if LIBAVFORMAT_VERSION_MAJOR < 58
  696. }
  697. #endif
  698. if (ret != 0) {
  699. blog(LOG_WARNING, "receive_video: Error writing video: %s",
  700. av_err2str(ret));
  701. //FIXME: stop the encode with an error
  702. }
  703. data->total_frames++;
  704. fail:
  705. av_packet_free(&packet);
  706. }
  707. static void encode_audio(struct ffmpeg_output *output, int idx,
  708. struct AVCodecContext *context, size_t block_size)
  709. {
  710. struct ffmpeg_data *data = &output->ff_data;
  711. AVPacket *packet = NULL;
  712. int ret, got_packet;
  713. int channels;
  714. #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 24, 100)
  715. channels = context->ch_layout.nb_channels;
  716. #else
  717. channels = context->channels;
  718. #endif
  719. size_t total_size = data->frame_size * block_size * channels;
  720. data->aframe[idx]->nb_samples = data->frame_size;
  721. data->aframe[idx]->pts = av_rescale_q(
  722. data->total_samples[idx], (AVRational){1, context->sample_rate},
  723. context->time_base);
  724. ret = avcodec_fill_audio_frame(data->aframe[idx], channels,
  725. context->sample_fmt,
  726. data->samples[idx][0], (int)total_size,
  727. 1);
  728. if (ret < 0) {
  729. blog(LOG_WARNING,
  730. "encode_audio: avcodec_fill_audio_frame "
  731. "failed: %s",
  732. av_err2str(ret));
  733. //FIXME: stop the encode with an error
  734. return;
  735. }
  736. data->total_samples[idx] += data->frame_size;
  737. packet = av_packet_alloc();
  738. #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
  739. ret = avcodec_send_frame(context, data->aframe[idx]);
  740. if (ret == 0)
  741. ret = avcodec_receive_packet(context, packet);
  742. got_packet = (ret == 0);
  743. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  744. ret = 0;
  745. #else
  746. ret = avcodec_encode_audio2(context, packet, data->aframe[idx],
  747. &got_packet);
  748. #endif
  749. if (ret < 0) {
  750. blog(LOG_WARNING, "encode_audio: Error encoding audio: %s",
  751. av_err2str(ret));
  752. //FIXME: stop the encode with an error
  753. goto fail;
  754. }
  755. if (!got_packet)
  756. goto fail;
  757. packet->pts = rescale_ts(packet->pts, context,
  758. data->audio_infos[idx].stream->time_base);
  759. packet->dts = rescale_ts(packet->dts, context,
  760. data->audio_infos[idx].stream->time_base);
  761. packet->duration =
  762. (int)av_rescale_q(packet->duration, context->time_base,
  763. data->audio_infos[idx].stream->time_base);
  764. packet->stream_index = data->audio_infos[idx].stream->index;
  765. pthread_mutex_lock(&output->write_mutex);
  766. da_push_back(output->packets, &packet);
  767. pthread_mutex_unlock(&output->write_mutex);
  768. os_sem_post(output->write_sem);
  769. return;
  770. fail:
  771. av_packet_free(&packet);
  772. }
  773. /* Given a bitmask for the selected tracks and the mix index,
  774. * this returns the stream index which will be passed to the muxer. */
  775. static int get_track_order(int track_config, size_t mix_index)
  776. {
  777. int position = 0;
  778. for (size_t i = 0; i < mix_index; i++) {
  779. if (track_config & 1 << i)
  780. position++;
  781. }
  782. return position;
  783. }
  784. static void receive_audio(void *param, size_t mix_idx, struct audio_data *frame)
  785. {
  786. struct ffmpeg_output *output = param;
  787. struct ffmpeg_data *data = &output->ff_data;
  788. size_t frame_size_bytes;
  789. struct audio_data in = *frame;
  790. int track_order;
  791. // codec doesn't support audio or none configured
  792. if (!data->audio_infos)
  793. return;
  794. /* check that the track was selected */
  795. if ((data->audio_tracks & (1 << mix_idx)) == 0)
  796. return;
  797. /* get track order (first selected, etc ...) */
  798. track_order = get_track_order(data->audio_tracks, mix_idx);
  799. AVCodecContext *context = data->audio_infos[track_order].ctx;
  800. if (!data->start_timestamp && data->video)
  801. return;
  802. if (!output->audio_start_ts)
  803. output->audio_start_ts = in.timestamp;
  804. frame_size_bytes = (size_t)data->frame_size * data->audio_size;
  805. for (size_t i = 0; i < data->audio_planes; i++)
  806. circlebuf_push_back(&data->excess_frames[track_order][i],
  807. in.data[i], in.frames * data->audio_size);
  808. while (data->excess_frames[track_order][0].size >= frame_size_bytes) {
  809. for (size_t i = 0; i < data->audio_planes; i++)
  810. circlebuf_pop_front(
  811. &data->excess_frames[track_order][i],
  812. data->samples[track_order][i],
  813. frame_size_bytes);
  814. encode_audio(output, track_order, context, data->audio_size);
  815. }
  816. }
  817. static uint64_t get_packet_sys_dts(struct ffmpeg_output *output,
  818. AVPacket *packet)
  819. {
  820. struct ffmpeg_data *data = &output->ff_data;
  821. uint64_t pause_offset = obs_output_get_pause_offset(output->output);
  822. uint64_t start_ts;
  823. AVRational time_base;
  824. if (data->video && data->video->index == packet->stream_index) {
  825. time_base = data->video->time_base;
  826. start_ts = output->video_start_ts;
  827. } else {
  828. time_base = data->audio_infos[0].stream->time_base;
  829. start_ts = output->audio_start_ts;
  830. }
  831. return start_ts + pause_offset +
  832. (uint64_t)av_rescale_q(packet->dts, time_base,
  833. (AVRational){1, 1000000000});
  834. }
  835. static int process_packet(struct ffmpeg_output *output)
  836. {
  837. AVPacket *packet = NULL;
  838. int ret = 0;
  839. pthread_mutex_lock(&output->write_mutex);
  840. if (output->packets.num) {
  841. packet = output->packets.array[0];
  842. da_erase(output->packets, 0);
  843. }
  844. pthread_mutex_unlock(&output->write_mutex);
  845. if (!packet)
  846. return 0;
  847. /*blog(LOG_DEBUG, "size = %d, flags = %lX, stream = %d, "
  848. "packets queued: %lu",
  849. packet.size, packet.flags,
  850. packet.stream_index, output->packets.num);*/
  851. if (stopping(output)) {
  852. uint64_t sys_ts = get_packet_sys_dts(output, packet);
  853. if (sys_ts >= output->stop_ts) {
  854. ret = 0;
  855. goto end;
  856. }
  857. }
  858. output->total_bytes += packet->size;
  859. ret = av_interleaved_write_frame(output->ff_data.output, packet);
  860. if (ret < 0) {
  861. ffmpeg_log_error(LOG_WARNING, &output->ff_data,
  862. "process_packet: Error writing packet: %s",
  863. av_err2str(ret));
  864. }
  865. end:
  866. av_packet_free(&packet);
  867. return ret;
  868. }
  869. static void *write_thread(void *data)
  870. {
  871. struct ffmpeg_output *output = data;
  872. while (os_sem_wait(output->write_sem) == 0) {
  873. /* check to see if shutting down */
  874. if (os_event_try(output->stop_event) == 0)
  875. break;
  876. int ret = process_packet(output);
  877. if (ret != 0) {
  878. int code = OBS_OUTPUT_ERROR;
  879. pthread_detach(output->write_thread);
  880. output->write_thread_active = false;
  881. if (ret == -ENOSPC)
  882. code = OBS_OUTPUT_NO_SPACE;
  883. obs_output_signal_stop(output->output, code);
  884. ffmpeg_deactivate(output);
  885. break;
  886. }
  887. }
  888. output->active = false;
  889. return NULL;
  890. }
  891. static inline const char *get_string_or_null(obs_data_t *settings,
  892. const char *name)
  893. {
  894. const char *value = obs_data_get_string(settings, name);
  895. if (!value || !strlen(value))
  896. return NULL;
  897. return value;
  898. }
  899. static int get_audio_mix_count(int audio_mix_mask)
  900. {
  901. int mix_count = 0;
  902. for (int i = 0; i < MAX_AUDIO_MIXES; i++) {
  903. if ((audio_mix_mask & (1 << i)) != 0) {
  904. mix_count++;
  905. }
  906. }
  907. return mix_count;
  908. }
  909. static bool try_connect(struct ffmpeg_output *output)
  910. {
  911. video_t *video = obs_output_video(output->output);
  912. const struct video_output_info *voi = video_output_get_info(video);
  913. struct ffmpeg_cfg config;
  914. obs_data_t *settings;
  915. bool success;
  916. int ret;
  917. settings = obs_output_get_settings(output->output);
  918. obs_data_set_default_int(settings, "gop_size", 120);
  919. config.url = obs_data_get_string(settings, "url");
  920. config.format_name = get_string_or_null(settings, "format_name");
  921. config.format_mime_type =
  922. get_string_or_null(settings, "format_mime_type");
  923. config.muxer_settings = obs_data_get_string(settings, "muxer_settings");
  924. config.video_bitrate = (int)obs_data_get_int(settings, "video_bitrate");
  925. config.audio_bitrate = (int)obs_data_get_int(settings, "audio_bitrate");
  926. config.gop_size = (int)obs_data_get_int(settings, "gop_size");
  927. config.video_encoder = get_string_or_null(settings, "video_encoder");
  928. config.video_encoder_id =
  929. (int)obs_data_get_int(settings, "video_encoder_id");
  930. config.audio_encoder = get_string_or_null(settings, "audio_encoder");
  931. config.audio_encoder_id =
  932. (int)obs_data_get_int(settings, "audio_encoder_id");
  933. config.video_settings = obs_data_get_string(settings, "video_settings");
  934. config.audio_settings = obs_data_get_string(settings, "audio_settings");
  935. config.scale_width = (int)obs_data_get_int(settings, "scale_width");
  936. config.scale_height = (int)obs_data_get_int(settings, "scale_height");
  937. config.width = (int)obs_output_get_width(output->output);
  938. config.height = (int)obs_output_get_height(output->output);
  939. config.format =
  940. obs_to_ffmpeg_video_format(video_output_get_format(video));
  941. config.audio_tracks = (int)obs_output_get_mixers(output->output);
  942. config.audio_mix_count = get_audio_mix_count(config.audio_tracks);
  943. config.color_range = voi->range == VIDEO_RANGE_FULL ? AVCOL_RANGE_JPEG
  944. : AVCOL_RANGE_MPEG;
  945. config.colorspace = format_is_yuv(voi->format) ? AVCOL_SPC_BT709
  946. : AVCOL_SPC_RGB;
  947. switch (voi->colorspace) {
  948. case VIDEO_CS_601:
  949. config.color_primaries = AVCOL_PRI_SMPTE170M;
  950. config.color_trc = AVCOL_TRC_SMPTE170M;
  951. config.colorspace = AVCOL_SPC_SMPTE170M;
  952. break;
  953. case VIDEO_CS_DEFAULT:
  954. case VIDEO_CS_709:
  955. config.color_primaries = AVCOL_PRI_BT709;
  956. config.color_trc = AVCOL_TRC_BT709;
  957. config.colorspace = AVCOL_SPC_BT709;
  958. break;
  959. case VIDEO_CS_SRGB:
  960. config.color_primaries = AVCOL_PRI_BT709;
  961. config.color_trc = AVCOL_TRC_IEC61966_2_1;
  962. config.colorspace = AVCOL_SPC_BT709;
  963. break;
  964. case VIDEO_CS_2100_PQ:
  965. config.color_primaries = AVCOL_PRI_BT2020;
  966. config.color_trc = AVCOL_TRC_SMPTE2084;
  967. config.colorspace = AVCOL_SPC_BT2020_NCL;
  968. break;
  969. case VIDEO_CS_2100_HLG:
  970. config.color_primaries = AVCOL_PRI_BT2020;
  971. config.color_trc = AVCOL_TRC_ARIB_STD_B67;
  972. config.colorspace = AVCOL_SPC_BT2020_NCL;
  973. break;
  974. }
  975. if (config.format == AV_PIX_FMT_NONE) {
  976. blog(LOG_DEBUG, "invalid pixel format used for FFmpeg output");
  977. return false;
  978. }
  979. if (!config.scale_width)
  980. config.scale_width = config.width;
  981. if (!config.scale_height)
  982. config.scale_height = config.height;
  983. success = ffmpeg_data_init(&output->ff_data, &config);
  984. obs_data_release(settings);
  985. if (!success) {
  986. if (output->ff_data.last_error) {
  987. obs_output_set_last_error(output->output,
  988. output->ff_data.last_error);
  989. }
  990. ffmpeg_data_free(&output->ff_data);
  991. return false;
  992. }
  993. struct audio_convert_info aci = {.format =
  994. output->ff_data.audio_format};
  995. output->active = true;
  996. if (!obs_output_can_begin_data_capture(output->output, 0))
  997. return false;
  998. ret = pthread_create(&output->write_thread, NULL, write_thread, output);
  999. if (ret != 0) {
  1000. ffmpeg_log_error(LOG_WARNING, &output->ff_data,
  1001. "ffmpeg_output_start: failed to create write "
  1002. "thread.");
  1003. ffmpeg_output_full_stop(output);
  1004. return false;
  1005. }
  1006. obs_output_set_video_conversion(output->output, NULL);
  1007. obs_output_set_audio_conversion(output->output, &aci);
  1008. obs_output_begin_data_capture(output->output, 0);
  1009. output->write_thread_active = true;
  1010. return true;
  1011. }
  1012. static void *start_thread(void *data)
  1013. {
  1014. struct ffmpeg_output *output = data;
  1015. if (!try_connect(output))
  1016. obs_output_signal_stop(output->output,
  1017. OBS_OUTPUT_CONNECT_FAILED);
  1018. output->connecting = false;
  1019. return NULL;
  1020. }
  1021. static bool ffmpeg_output_start(void *data)
  1022. {
  1023. struct ffmpeg_output *output = data;
  1024. int ret;
  1025. if (output->connecting)
  1026. return false;
  1027. os_atomic_set_bool(&output->stopping, false);
  1028. output->audio_start_ts = 0;
  1029. output->video_start_ts = 0;
  1030. output->total_bytes = 0;
  1031. ret = pthread_create(&output->start_thread, NULL, start_thread, output);
  1032. return (output->connecting = (ret == 0));
  1033. }
  1034. static void ffmpeg_output_full_stop(void *data)
  1035. {
  1036. struct ffmpeg_output *output = data;
  1037. if (output->active) {
  1038. obs_output_end_data_capture(output->output);
  1039. ffmpeg_deactivate(output);
  1040. }
  1041. }
  1042. static void ffmpeg_output_stop(void *data, uint64_t ts)
  1043. {
  1044. struct ffmpeg_output *output = data;
  1045. if (output->active) {
  1046. if (ts > 0) {
  1047. output->stop_ts = ts;
  1048. os_atomic_set_bool(&output->stopping, true);
  1049. }
  1050. ffmpeg_output_full_stop(output);
  1051. }
  1052. }
  1053. static void ffmpeg_deactivate(struct ffmpeg_output *output)
  1054. {
  1055. if (output->write_thread_active) {
  1056. os_event_signal(output->stop_event);
  1057. os_sem_post(output->write_sem);
  1058. pthread_join(output->write_thread, NULL);
  1059. output->write_thread_active = false;
  1060. }
  1061. pthread_mutex_lock(&output->write_mutex);
  1062. for (size_t i = 0; i < output->packets.num; i++)
  1063. av_packet_free(output->packets.array + i);
  1064. da_free(output->packets);
  1065. pthread_mutex_unlock(&output->write_mutex);
  1066. ffmpeg_data_free(&output->ff_data);
  1067. }
  1068. static uint64_t ffmpeg_output_total_bytes(void *data)
  1069. {
  1070. struct ffmpeg_output *output = data;
  1071. return output->total_bytes;
  1072. }
  1073. struct obs_output_info ffmpeg_output = {
  1074. .id = "ffmpeg_output",
  1075. .flags = OBS_OUTPUT_AUDIO | OBS_OUTPUT_VIDEO | OBS_OUTPUT_MULTI_TRACK |
  1076. OBS_OUTPUT_CAN_PAUSE,
  1077. .get_name = ffmpeg_output_getname,
  1078. .create = ffmpeg_output_create,
  1079. .destroy = ffmpeg_output_destroy,
  1080. .start = ffmpeg_output_start,
  1081. .stop = ffmpeg_output_stop,
  1082. .raw_video = receive_video,
  1083. .raw_audio2 = receive_audio,
  1084. .get_total_bytes = ffmpeg_output_total_bytes,
  1085. };