obs-ffmpeg-output.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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. const int has_alpha = closest_format == AV_PIX_FMT_BGRA;
  202. closest_format = avcodec_find_best_pix_fmt_of_list(
  203. data->vcodec->pix_fmts, closest_format, has_alpha,
  204. NULL);
  205. }
  206. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  207. context = avcodec_alloc_context3(data->vcodec);
  208. #else
  209. context = data->video->codec;
  210. #endif
  211. context->bit_rate = (int64_t)data->config.video_bitrate * 1000;
  212. context->width = data->config.scale_width;
  213. context->height = data->config.scale_height;
  214. context->time_base = (AVRational){ovi.fps_den, ovi.fps_num};
  215. context->framerate = (AVRational){ovi.fps_num, ovi.fps_den};
  216. context->gop_size = data->config.gop_size;
  217. context->pix_fmt = closest_format;
  218. context->color_range = data->config.color_range;
  219. context->color_primaries = data->config.color_primaries;
  220. context->color_trc = data->config.color_trc;
  221. context->colorspace = data->config.colorspace;
  222. context->chroma_sample_location = determine_chroma_location(
  223. closest_format, data->config.colorspace);
  224. context->thread_count = 0;
  225. data->video->time_base = context->time_base;
  226. if (data->output->oformat->flags & AVFMT_GLOBALHEADER)
  227. context->flags |= CODEC_FLAG_GLOBAL_H;
  228. data->video_ctx = context;
  229. if (!open_video_codec(data))
  230. return false;
  231. if (context->pix_fmt != data->config.format ||
  232. data->config.width != data->config.scale_width ||
  233. data->config.height != data->config.scale_height) {
  234. if (!init_swscale(data, context))
  235. return false;
  236. }
  237. return true;
  238. }
  239. static bool open_audio_codec(struct ffmpeg_data *data, int idx)
  240. {
  241. AVCodecContext *const context = data->audio_infos[idx].ctx;
  242. char **opts = strlist_split(data->config.audio_settings, ' ', false);
  243. int ret;
  244. int channels;
  245. if (opts) {
  246. parse_params(context, opts);
  247. strlist_free(opts);
  248. }
  249. data->aframe[idx] = av_frame_alloc();
  250. if (!data->aframe[idx]) {
  251. ffmpeg_log_error(LOG_WARNING, data,
  252. "Failed to allocate audio frame");
  253. return false;
  254. }
  255. data->aframe[idx]->format = context->sample_fmt;
  256. #if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 24, 100)
  257. data->aframe[idx]->channels = context->channels;
  258. data->aframe[idx]->channel_layout = context->channel_layout;
  259. channels = context->channels;
  260. #else
  261. data->aframe[idx]->ch_layout = context->ch_layout;
  262. channels = context->ch_layout.nb_channels;
  263. #endif
  264. data->aframe[idx]->sample_rate = context->sample_rate;
  265. context->strict_std_compliance = -2;
  266. ret = avcodec_open2(context, data->acodec, NULL);
  267. if (ret < 0) {
  268. ffmpeg_log_error(LOG_WARNING, data,
  269. "Failed to open audio codec: %s",
  270. av_err2str(ret));
  271. return false;
  272. }
  273. data->frame_size = context->frame_size ? context->frame_size : 1024;
  274. ret = av_samples_alloc(data->samples[idx], NULL, channels,
  275. data->frame_size, context->sample_fmt, 0);
  276. if (ret < 0) {
  277. ffmpeg_log_error(LOG_WARNING, data,
  278. "Failed to create audio buffer: %s",
  279. av_err2str(ret));
  280. return false;
  281. }
  282. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  283. avcodec_parameters_from_context(data->audio_infos[idx].stream->codecpar,
  284. context);
  285. #endif
  286. return true;
  287. }
  288. static bool create_audio_stream(struct ffmpeg_data *data, int idx)
  289. {
  290. AVCodecContext *context;
  291. AVStream *stream;
  292. struct obs_audio_info aoi;
  293. int channels;
  294. if (!obs_get_audio_info(&aoi)) {
  295. ffmpeg_log_error(LOG_WARNING, data, "No active audio");
  296. return false;
  297. }
  298. if (!new_stream(data, &stream, &data->acodec,
  299. data->output->oformat->audio_codec,
  300. data->config.audio_encoder))
  301. return false;
  302. #if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 48, 101)
  303. context = avcodec_alloc_context3(data->acodec);
  304. #else
  305. context = stream->codec;
  306. #endif
  307. context->bit_rate = (int64_t)data->config.audio_bitrate * 1000;
  308. context->time_base = (AVRational){1, aoi.samples_per_sec};
  309. #if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 24, 100)
  310. context->channels = get_audio_channels(aoi.speakers);
  311. #endif
  312. channels = get_audio_channels(aoi.speakers);
  313. context->sample_rate = aoi.samples_per_sec;
  314. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 24, 100)
  315. context->channel_layout = av_get_default_channel_layout(channels);
  316. //avutil default channel layout for 5 channels is 5.0 ; fix for 4.1
  317. if (aoi.speakers == SPEAKERS_4POINT1)
  318. context->channel_layout = av_get_channel_layout("4.1");
  319. #else
  320. av_channel_layout_default(&context->ch_layout, channels);
  321. if (aoi.speakers == SPEAKERS_4POINT1)
  322. context->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_4POINT1;
  323. #endif
  324. context->sample_fmt = data->acodec->sample_fmts
  325. ? data->acodec->sample_fmts[0]
  326. : AV_SAMPLE_FMT_FLTP;
  327. stream->time_base = context->time_base;
  328. data->audio_samplerate = aoi.samples_per_sec;
  329. data->audio_format = convert_ffmpeg_sample_format(context->sample_fmt);
  330. data->audio_planes = get_audio_planes(data->audio_format, aoi.speakers);
  331. data->audio_size = get_audio_size(data->audio_format, aoi.speakers, 1);
  332. if (data->output->oformat->flags & AVFMT_GLOBALHEADER)
  333. context->flags |= CODEC_FLAG_GLOBAL_H;
  334. data->audio_infos[idx].stream = stream;
  335. data->audio_infos[idx].ctx = context;
  336. return open_audio_codec(data, idx);
  337. }
  338. static inline bool init_streams(struct ffmpeg_data *data)
  339. {
  340. const AVOutputFormat *format = data->output->oformat;
  341. if (format->video_codec != AV_CODEC_ID_NONE)
  342. if (!create_video_stream(data))
  343. return false;
  344. if (format->audio_codec != AV_CODEC_ID_NONE &&
  345. data->num_audio_streams) {
  346. data->audio_infos = calloc(data->num_audio_streams,
  347. sizeof(*data->audio_infos));
  348. for (int i = 0; i < data->num_audio_streams; i++) {
  349. if (!create_audio_stream(data, i))
  350. return false;
  351. }
  352. }
  353. return true;
  354. }
  355. static inline bool open_output_file(struct ffmpeg_data *data)
  356. {
  357. const AVOutputFormat *format = data->output->oformat;
  358. int ret;
  359. AVDictionary *dict = NULL;
  360. if ((ret = av_dict_parse_string(&dict, data->config.muxer_settings, "=",
  361. " ", 0))) {
  362. ffmpeg_log_error(LOG_WARNING, data,
  363. "Failed to parse muxer settings: %s\n%s",
  364. av_err2str(ret), data->config.muxer_settings);
  365. av_dict_free(&dict);
  366. return false;
  367. }
  368. if (av_dict_count(dict) > 0) {
  369. struct dstr str = {0};
  370. AVDictionaryEntry *entry = NULL;
  371. while ((entry = av_dict_get(dict, "", entry,
  372. AV_DICT_IGNORE_SUFFIX)))
  373. dstr_catf(&str, "\n\t%s=%s", entry->key, entry->value);
  374. blog(LOG_INFO, "Using muxer settings: %s", str.array);
  375. dstr_free(&str);
  376. }
  377. if ((format->flags & AVFMT_NOFILE) == 0) {
  378. ret = avio_open2(&data->output->pb, data->config.url,
  379. AVIO_FLAG_WRITE, NULL, &dict);
  380. if (ret < 0) {
  381. ffmpeg_log_error(LOG_WARNING, data,
  382. "Couldn't open '%s', %s",
  383. data->config.url, av_err2str(ret));
  384. av_dict_free(&dict);
  385. return false;
  386. }
  387. }
  388. ret = avformat_write_header(data->output, &dict);
  389. if (ret < 0) {
  390. ffmpeg_log_error(LOG_WARNING, data, "Error opening '%s': %s",
  391. data->config.url, av_err2str(ret));
  392. return false;
  393. }
  394. if (av_dict_count(dict) > 0) {
  395. struct dstr str = {0};
  396. AVDictionaryEntry *entry = NULL;
  397. while ((entry = av_dict_get(dict, "", entry,
  398. AV_DICT_IGNORE_SUFFIX)))
  399. dstr_catf(&str, "\n\t%s=%s", entry->key, entry->value);
  400. blog(LOG_INFO, "Invalid muxer settings: %s", str.array);
  401. dstr_free(&str);
  402. }
  403. av_dict_free(&dict);
  404. return true;
  405. }
  406. static void close_video(struct ffmpeg_data *data)
  407. {
  408. avcodec_free_context(&data->video_ctx);
  409. av_frame_unref(data->vframe);
  410. // This format for some reason derefs video frame
  411. // too many times
  412. if (data->vcodec->id == AV_CODEC_ID_A64_MULTI ||
  413. data->vcodec->id == AV_CODEC_ID_A64_MULTI5)
  414. return;
  415. av_frame_free(&data->vframe);
  416. }
  417. static void close_audio(struct ffmpeg_data *data)
  418. {
  419. for (int idx = 0; idx < data->num_audio_streams; idx++) {
  420. for (size_t i = 0; i < MAX_AV_PLANES; i++)
  421. circlebuf_free(&data->excess_frames[idx][i]);
  422. if (data->samples[idx][0])
  423. av_freep(&data->samples[idx][0]);
  424. if (data->audio_infos[idx].ctx)
  425. avcodec_free_context(&data->audio_infos[idx].ctx);
  426. if (data->aframe[idx])
  427. av_frame_free(&data->aframe[idx]);
  428. }
  429. }
  430. void ffmpeg_data_free(struct ffmpeg_data *data)
  431. {
  432. if (data->initialized)
  433. av_write_trailer(data->output);
  434. if (data->video)
  435. close_video(data);
  436. if (data->audio_infos) {
  437. close_audio(data);
  438. free(data->audio_infos);
  439. data->audio_infos = NULL;
  440. }
  441. if (data->output) {
  442. if ((data->output->oformat->flags & AVFMT_NOFILE) == 0)
  443. avio_close(data->output->pb);
  444. avformat_free_context(data->output);
  445. }
  446. if (data->last_error)
  447. bfree(data->last_error);
  448. memset(data, 0, sizeof(struct ffmpeg_data));
  449. }
  450. static inline const char *safe_str(const char *s)
  451. {
  452. if (s == NULL)
  453. return "(NULL)";
  454. else
  455. return s;
  456. }
  457. static enum AVCodecID get_codec_id(const char *name, int id)
  458. {
  459. const AVCodec *codec;
  460. if (id != 0)
  461. return (enum AVCodecID)id;
  462. if (!name || !*name)
  463. return AV_CODEC_ID_NONE;
  464. codec = avcodec_find_encoder_by_name(name);
  465. if (!codec)
  466. return AV_CODEC_ID_NONE;
  467. return codec->id;
  468. }
  469. #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59, 0, 100)
  470. static void set_encoder_ids(struct ffmpeg_data *data)
  471. {
  472. data->output->oformat->video_codec = get_codec_id(
  473. data->config.video_encoder, data->config.video_encoder_id);
  474. data->output->oformat->audio_codec = get_codec_id(
  475. data->config.audio_encoder, data->config.audio_encoder_id);
  476. }
  477. #endif
  478. bool ffmpeg_data_init(struct ffmpeg_data *data, struct ffmpeg_cfg *config)
  479. {
  480. bool is_rtmp = false;
  481. memset(data, 0, sizeof(struct ffmpeg_data));
  482. data->config = *config;
  483. data->num_audio_streams = config->audio_mix_count;
  484. data->audio_tracks = config->audio_tracks;
  485. if (!config->url || !*config->url)
  486. return false;
  487. #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
  488. av_register_all();
  489. #endif
  490. avformat_network_init();
  491. is_rtmp = (astrcmpi_n(config->url, "rtmp://", 7) == 0);
  492. #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59, 0, 100)
  493. AVOutputFormat *output_format;
  494. #else
  495. const AVOutputFormat *output_format;
  496. #endif
  497. output_format = av_guess_format(
  498. is_rtmp ? "flv" : data->config.format_name, data->config.url,
  499. is_rtmp ? NULL : data->config.format_mime_type);
  500. if (output_format == NULL) {
  501. ffmpeg_log_error(
  502. LOG_WARNING, data,
  503. "Couldn't find matching output format with "
  504. "parameters: name=%s, url=%s, mime=%s",
  505. safe_str(is_rtmp ? "flv" : data->config.format_name),
  506. safe_str(data->config.url),
  507. safe_str(is_rtmp ? NULL
  508. : data->config.format_mime_type));
  509. goto fail;
  510. }
  511. avformat_alloc_output_context2(&data->output, output_format, NULL,
  512. data->config.url);
  513. if (!data->output) {
  514. ffmpeg_log_error(LOG_WARNING, data,
  515. "Couldn't create avformat context");
  516. goto fail;
  517. }
  518. #if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(59, 0, 100)
  519. if (is_rtmp) {
  520. data->output->oformat->video_codec = AV_CODEC_ID_H264;
  521. data->output->oformat->audio_codec = AV_CODEC_ID_AAC;
  522. } else {
  523. if (data->config.format_name)
  524. set_encoder_ids(data);
  525. }
  526. #else
  527. if (is_rtmp) {
  528. data->config.audio_encoder_id = AV_CODEC_ID_AAC;
  529. data->config.video_encoder_id = AV_CODEC_ID_H264;
  530. }
  531. #endif
  532. if (!init_streams(data))
  533. goto fail;
  534. if (!open_output_file(data))
  535. goto fail;
  536. av_dump_format(data->output, 0, NULL, 1);
  537. data->initialized = true;
  538. return true;
  539. fail:
  540. blog(LOG_WARNING, "ffmpeg_data_init failed");
  541. return false;
  542. }
  543. /* ------------------------------------------------------------------------- */
  544. static inline bool stopping(struct ffmpeg_output *output)
  545. {
  546. return os_atomic_load_bool(&output->stopping);
  547. }
  548. static const char *ffmpeg_output_getname(void *unused)
  549. {
  550. UNUSED_PARAMETER(unused);
  551. return obs_module_text("FFmpegOutput");
  552. }
  553. static void ffmpeg_log_callback(void *param, int level, const char *format,
  554. va_list args)
  555. {
  556. if (level <= AV_LOG_INFO)
  557. blogva(LOG_DEBUG, format, args);
  558. UNUSED_PARAMETER(param);
  559. }
  560. static void *ffmpeg_output_create(obs_data_t *settings, obs_output_t *output)
  561. {
  562. struct ffmpeg_output *data = bzalloc(sizeof(struct ffmpeg_output));
  563. pthread_mutex_init_value(&data->write_mutex);
  564. data->output = output;
  565. if (pthread_mutex_init(&data->write_mutex, NULL) != 0)
  566. goto fail;
  567. if (os_event_init(&data->stop_event, OS_EVENT_TYPE_AUTO) != 0)
  568. goto fail;
  569. if (os_sem_init(&data->write_sem, 0) != 0)
  570. goto fail;
  571. av_log_set_callback(ffmpeg_log_callback);
  572. UNUSED_PARAMETER(settings);
  573. return data;
  574. fail:
  575. pthread_mutex_destroy(&data->write_mutex);
  576. os_event_destroy(data->stop_event);
  577. bfree(data);
  578. return NULL;
  579. }
  580. static void ffmpeg_output_full_stop(void *data);
  581. static void ffmpeg_deactivate(struct ffmpeg_output *output);
  582. static void ffmpeg_output_destroy(void *data)
  583. {
  584. struct ffmpeg_output *output = data;
  585. if (output) {
  586. if (output->connecting)
  587. pthread_join(output->start_thread, NULL);
  588. ffmpeg_output_full_stop(output);
  589. pthread_mutex_destroy(&output->write_mutex);
  590. os_sem_destroy(output->write_sem);
  591. os_event_destroy(output->stop_event);
  592. bfree(data);
  593. }
  594. }
  595. static inline void copy_data(AVFrame *pic, const struct video_data *frame,
  596. int height, enum AVPixelFormat format)
  597. {
  598. int h_chroma_shift, v_chroma_shift;
  599. av_pix_fmt_get_chroma_sub_sample(format, &h_chroma_shift,
  600. &v_chroma_shift);
  601. for (int plane = 0; plane < MAX_AV_PLANES; plane++) {
  602. if (!frame->data[plane])
  603. continue;
  604. int frame_rowsize = (int)frame->linesize[plane];
  605. int pic_rowsize = pic->linesize[plane];
  606. int bytes = frame_rowsize < pic_rowsize ? frame_rowsize
  607. : pic_rowsize;
  608. int plane_height = height >> (plane ? v_chroma_shift : 0);
  609. for (int y = 0; y < plane_height; y++) {
  610. int pos_frame = y * frame_rowsize;
  611. int pos_pic = y * pic_rowsize;
  612. memcpy(pic->data[plane] + pos_pic,
  613. frame->data[plane] + pos_frame, bytes);
  614. }
  615. }
  616. }
  617. static void receive_video(void *param, struct video_data *frame)
  618. {
  619. struct ffmpeg_output *output = param;
  620. struct ffmpeg_data *data = &output->ff_data;
  621. // codec doesn't support video or none configured
  622. if (!data->video)
  623. return;
  624. AVCodecContext *context = data->video_ctx;
  625. AVPacket *packet = NULL;
  626. int ret = 0, got_packet;
  627. if (!output->video_start_ts)
  628. output->video_start_ts = frame->timestamp;
  629. if (!data->start_timestamp)
  630. data->start_timestamp = frame->timestamp;
  631. ret = av_frame_make_writable(data->vframe);
  632. if (ret < 0) {
  633. blog(LOG_WARNING,
  634. "receive_video: Error obtaining writable "
  635. "AVFrame: %s",
  636. av_err2str(ret));
  637. //FIXME: stop the encode with an error
  638. return;
  639. }
  640. if (!!data->swscale)
  641. sws_scale(data->swscale, (const uint8_t *const *)frame->data,
  642. (const int *)frame->linesize, 0, data->config.height,
  643. data->vframe->data, data->vframe->linesize);
  644. else
  645. copy_data(data->vframe, frame, context->height,
  646. context->pix_fmt);
  647. packet = av_packet_alloc();
  648. #if LIBAVFORMAT_VERSION_MAJOR < 58
  649. if (data->output->flags & AVFMT_RAWPICTURE) {
  650. packet->flags |= AV_PKT_FLAG_KEY;
  651. packet->stream_index = data->video->index;
  652. packet->data = data->vframe->data[0];
  653. packet->size = sizeof(AVPicture);
  654. pthread_mutex_lock(&output->write_mutex);
  655. da_push_back(output->packets, &packet);
  656. packet = NULL;
  657. pthread_mutex_unlock(&output->write_mutex);
  658. os_sem_post(output->write_sem);
  659. } else {
  660. #endif
  661. data->vframe->pts = data->total_frames;
  662. #if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 40, 101)
  663. ret = avcodec_send_frame(context, data->vframe);
  664. if (ret == 0)
  665. ret = avcodec_receive_packet(context, packet);
  666. got_packet = (ret == 0);
  667. if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN))
  668. ret = 0;
  669. #else
  670. ret = avcodec_encode_video2(context, packet, data->vframe, &got_packet);
  671. #endif
  672. if (ret < 0) {
  673. blog(LOG_WARNING,
  674. "receive_video: Error encoding "
  675. "video: %s",
  676. av_err2str(ret));
  677. //FIXME: stop the encode with an error
  678. goto fail;
  679. }
  680. if (!ret && got_packet && packet->size) {
  681. packet->pts = rescale_ts(packet->pts, context,
  682. data->video->time_base);
  683. packet->dts = rescale_ts(packet->dts, context,
  684. data->video->time_base);
  685. packet->duration = (int)av_rescale_q(
  686. packet->duration, context->time_base,
  687. data->video->time_base);
  688. pthread_mutex_lock(&output->write_mutex);
  689. da_push_back(output->packets, &packet);
  690. packet = NULL;
  691. pthread_mutex_unlock(&output->write_mutex);
  692. os_sem_post(output->write_sem);
  693. } else {
  694. ret = 0;
  695. }
  696. #if LIBAVFORMAT_VERSION_MAJOR < 58
  697. }
  698. #endif
  699. if (ret != 0) {
  700. blog(LOG_WARNING, "receive_video: Error writing video: %s",
  701. av_err2str(ret));
  702. //FIXME: stop the encode with an error
  703. }
  704. data->total_frames++;
  705. fail:
  706. av_packet_free(&packet);
  707. }
  708. static void encode_audio(struct ffmpeg_output *output, int idx,
  709. struct AVCodecContext *context, size_t block_size)
  710. {
  711. struct ffmpeg_data *data = &output->ff_data;
  712. AVPacket *packet = NULL;
  713. int ret, got_packet;
  714. int channels;
  715. #if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 24, 100)
  716. channels = context->ch_layout.nb_channels;
  717. #else
  718. channels = context->channels;
  719. #endif
  720. size_t total_size = data->frame_size * block_size * 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], 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 = NULL;
  839. int ret = 0;
  840. pthread_mutex_lock(&output->write_mutex);
  841. if (output->packets.num) {
  842. packet = output->packets.array[0];
  843. da_erase(output->packets, 0);
  844. }
  845. pthread_mutex_unlock(&output->write_mutex);
  846. if (!packet)
  847. return 0;
  848. /*blog(LOG_DEBUG, "size = %d, flags = %lX, stream = %d, "
  849. "packets queued: %lu",
  850. packet.size, packet.flags,
  851. packet.stream_index, output->packets.num);*/
  852. if (stopping(output)) {
  853. uint64_t sys_ts = get_packet_sys_dts(output, packet);
  854. if (sys_ts >= output->stop_ts) {
  855. ret = 0;
  856. goto end;
  857. }
  858. }
  859. output->total_bytes += packet->size;
  860. ret = av_interleaved_write_frame(output->ff_data.output, packet);
  861. if (ret < 0) {
  862. ffmpeg_log_error(LOG_WARNING, &output->ff_data,
  863. "process_packet: Error writing packet: %s",
  864. av_err2str(ret));
  865. }
  866. end:
  867. av_packet_free(&packet);
  868. return ret;
  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. };