obs-ffmpeg-output.c 35 KB

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