obs-ffmpeg-output.c 35 KB

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