obs-ffmpeg-output.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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 <libavutil/opt.h>
  21. #include <libavformat/avformat.h>
  22. #include <libswscale/swscale.h>
  23. #include "obs-ffmpeg-formats.h"
  24. #include "closest-pixel-format.h"
  25. #include "obs-ffmpeg-compat.h"
  26. struct ffmpeg_cfg {
  27. const char *url;
  28. const char *format_name;
  29. const char *format_mime_type;
  30. const char *muxer_settings;
  31. int video_bitrate;
  32. int audio_bitrate;
  33. const char *video_encoder;
  34. int video_encoder_id;
  35. const char *audio_encoder;
  36. int audio_encoder_id;
  37. const char *video_settings;
  38. const char *audio_settings;
  39. enum AVPixelFormat format;
  40. enum AVColorRange color_range;
  41. enum AVColorSpace color_space;
  42. int scale_width;
  43. int scale_height;
  44. int width;
  45. int height;
  46. };
  47. struct ffmpeg_data {
  48. AVStream *video;
  49. AVStream *audio;
  50. AVCodec *acodec;
  51. AVCodec *vcodec;
  52. AVFormatContext *output;
  53. struct SwsContext *swscale;
  54. int64_t total_frames;
  55. AVPicture dst_picture;
  56. AVFrame *vframe;
  57. int frame_size;
  58. uint64_t start_timestamp;
  59. int64_t total_samples;
  60. uint32_t audio_samplerate;
  61. enum audio_format audio_format;
  62. size_t audio_planes;
  63. size_t audio_size;
  64. struct circlebuf excess_frames[MAX_AV_PLANES];
  65. uint8_t *samples[MAX_AV_PLANES];
  66. AVFrame *aframe;
  67. struct ffmpeg_cfg config;
  68. bool initialized;
  69. };
  70. struct ffmpeg_output {
  71. obs_output_t *output;
  72. volatile bool active;
  73. struct ffmpeg_data ff_data;
  74. bool connecting;
  75. pthread_t start_thread;
  76. bool write_thread_active;
  77. pthread_mutex_t write_mutex;
  78. pthread_t write_thread;
  79. os_sem_t *write_sem;
  80. os_event_t *stop_event;
  81. DARRAY(AVPacket) packets;
  82. };
  83. /* ------------------------------------------------------------------------- */
  84. static bool new_stream(struct ffmpeg_data *data, AVStream **stream,
  85. AVCodec **codec, enum AVCodecID id, const char *name)
  86. {
  87. *codec = (!!name && *name) ?
  88. avcodec_find_encoder_by_name(name) :
  89. avcodec_find_encoder(id);
  90. if (!*codec) {
  91. blog(LOG_WARNING, "Couldn't find encoder '%s'",
  92. avcodec_get_name(id));
  93. return false;
  94. }
  95. *stream = avformat_new_stream(data->output, *codec);
  96. if (!*stream) {
  97. blog(LOG_WARNING, "Couldn't create stream for encoder '%s'",
  98. avcodec_get_name(id));
  99. return false;
  100. }
  101. (*stream)->id = data->output->nb_streams-1;
  102. return true;
  103. }
  104. static void parse_params(AVCodecContext *context, char **opts)
  105. {
  106. if (!context || !context->priv_data)
  107. return;
  108. while (*opts) {
  109. char *opt = *opts;
  110. char *assign = strchr(opt, '=');
  111. if (assign) {
  112. char *name = opt;
  113. char *value;
  114. *assign = 0;
  115. value = assign+1;
  116. av_opt_set(context->priv_data, name, value, 0);
  117. }
  118. opts++;
  119. }
  120. }
  121. static bool open_video_codec(struct ffmpeg_data *data)
  122. {
  123. AVCodecContext *context = data->video->codec;
  124. char **opts = strlist_split(data->config.video_settings, ' ', false);
  125. int ret;
  126. if (strcmp(data->vcodec->name, "libx264") == 0)
  127. av_opt_set(context->priv_data, "preset", "veryfast", 0);
  128. if (opts) {
  129. parse_params(context, opts);
  130. strlist_free(opts);
  131. }
  132. ret = avcodec_open2(context, data->vcodec, NULL);
  133. if (ret < 0) {
  134. blog(LOG_WARNING, "Failed to open video codec: %s",
  135. av_err2str(ret));
  136. return false;
  137. }
  138. data->vframe = av_frame_alloc();
  139. if (!data->vframe) {
  140. blog(LOG_WARNING, "Failed to allocate video frame");
  141. return false;
  142. }
  143. data->vframe->format = context->pix_fmt;
  144. data->vframe->width = context->width;
  145. data->vframe->height = context->height;
  146. data->vframe->colorspace = data->config.color_space;
  147. data->vframe->color_range = data->config.color_range;
  148. ret = avpicture_alloc(&data->dst_picture, context->pix_fmt,
  149. context->width, context->height);
  150. if (ret < 0) {
  151. blog(LOG_WARNING, "Failed to allocate dst_picture: %s",
  152. av_err2str(ret));
  153. return false;
  154. }
  155. *((AVPicture*)data->vframe) = data->dst_picture;
  156. return true;
  157. }
  158. static bool init_swscale(struct ffmpeg_data *data, AVCodecContext *context)
  159. {
  160. data->swscale = sws_getContext(
  161. data->config.width, data->config.height,
  162. data->config.format,
  163. data->config.scale_width, data->config.scale_height,
  164. context->pix_fmt,
  165. SWS_BICUBIC, NULL, NULL, NULL);
  166. if (!data->swscale) {
  167. blog(LOG_WARNING, "Could not initialize swscale");
  168. return false;
  169. }
  170. return true;
  171. }
  172. static bool create_video_stream(struct ffmpeg_data *data)
  173. {
  174. enum AVPixelFormat closest_format;
  175. AVCodecContext *context;
  176. struct obs_video_info ovi;
  177. if (!obs_get_video_info(&ovi)) {
  178. blog(LOG_WARNING, "No active video");
  179. return false;
  180. }
  181. if (!new_stream(data, &data->video, &data->vcodec,
  182. data->output->oformat->video_codec,
  183. data->config.video_encoder))
  184. return false;
  185. closest_format = get_closest_format(data->config.format,
  186. data->vcodec->pix_fmts);
  187. context = data->video->codec;
  188. context->bit_rate = data->config.video_bitrate * 1000;
  189. context->width = data->config.scale_width;
  190. context->height = data->config.scale_height;
  191. context->time_base = (AVRational){ ovi.fps_den, ovi.fps_num };
  192. context->gop_size = 120;
  193. context->pix_fmt = closest_format;
  194. context->colorspace = data->config.color_space;
  195. context->color_range = data->config.color_range;
  196. context->thread_count = 0;
  197. data->video->time_base = context->time_base;
  198. if (data->output->oformat->flags & AVFMT_GLOBALHEADER)
  199. context->flags |= CODEC_FLAG_GLOBAL_HEADER;
  200. if (!open_video_codec(data))
  201. return false;
  202. if (context->pix_fmt != data->config.format ||
  203. data->config.width != data->config.scale_width ||
  204. data->config.height != data->config.scale_height) {
  205. if (!init_swscale(data, context))
  206. return false;
  207. }
  208. return true;
  209. }
  210. static bool open_audio_codec(struct ffmpeg_data *data)
  211. {
  212. AVCodecContext *context = data->audio->codec;
  213. char **opts = strlist_split(data->config.video_settings, ' ', false);
  214. int ret;
  215. if (opts) {
  216. parse_params(context, opts);
  217. strlist_free(opts);
  218. }
  219. data->aframe = av_frame_alloc();
  220. if (!data->aframe) {
  221. blog(LOG_WARNING, "Failed to allocate audio frame");
  222. return false;
  223. }
  224. context->strict_std_compliance = -2;
  225. ret = avcodec_open2(context, data->acodec, NULL);
  226. if (ret < 0) {
  227. blog(LOG_WARNING, "Failed to open audio codec: %s",
  228. av_err2str(ret));
  229. return false;
  230. }
  231. data->frame_size = context->frame_size ? context->frame_size : 1024;
  232. ret = av_samples_alloc(data->samples, NULL, context->channels,
  233. data->frame_size, context->sample_fmt, 0);
  234. if (ret < 0) {
  235. blog(LOG_WARNING, "Failed to create audio buffer: %s",
  236. av_err2str(ret));
  237. return false;
  238. }
  239. return true;
  240. }
  241. static bool create_audio_stream(struct ffmpeg_data *data)
  242. {
  243. AVCodecContext *context;
  244. struct obs_audio_info aoi;
  245. if (!obs_get_audio_info(&aoi)) {
  246. blog(LOG_WARNING, "No active audio");
  247. return false;
  248. }
  249. if (!new_stream(data, &data->audio, &data->acodec,
  250. data->output->oformat->audio_codec,
  251. data->config.audio_encoder))
  252. return false;
  253. context = data->audio->codec;
  254. context->bit_rate = data->config.audio_bitrate * 1000;
  255. context->time_base = (AVRational){ 1, aoi.samples_per_sec };
  256. context->channels = get_audio_channels(aoi.speakers);
  257. context->sample_rate = aoi.samples_per_sec;
  258. context->channel_layout =
  259. av_get_default_channel_layout(context->channels);
  260. context->sample_fmt = data->acodec->sample_fmts ?
  261. data->acodec->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
  262. data->audio->time_base = context->time_base;
  263. data->audio_samplerate = aoi.samples_per_sec;
  264. data->audio_format = convert_ffmpeg_sample_format(context->sample_fmt);
  265. data->audio_planes = get_audio_planes(data->audio_format, aoi.speakers);
  266. data->audio_size = get_audio_size(data->audio_format, aoi.speakers, 1);
  267. if (data->output->oformat->flags & AVFMT_GLOBALHEADER)
  268. context->flags |= CODEC_FLAG_GLOBAL_HEADER;
  269. return open_audio_codec(data);
  270. }
  271. static inline bool init_streams(struct ffmpeg_data *data)
  272. {
  273. AVOutputFormat *format = data->output->oformat;
  274. if (format->video_codec != AV_CODEC_ID_NONE)
  275. if (!create_video_stream(data))
  276. return false;
  277. if (format->audio_codec != AV_CODEC_ID_NONE)
  278. if (!create_audio_stream(data))
  279. return false;
  280. return true;
  281. }
  282. static inline bool open_output_file(struct ffmpeg_data *data)
  283. {
  284. AVOutputFormat *format = data->output->oformat;
  285. int ret;
  286. if ((format->flags & AVFMT_NOFILE) == 0) {
  287. ret = avio_open(&data->output->pb, data->config.url,
  288. AVIO_FLAG_WRITE);
  289. if (ret < 0) {
  290. blog(LOG_WARNING, "Couldn't open '%s', %s",
  291. data->config.url, av_err2str(ret));
  292. return false;
  293. }
  294. }
  295. strncpy(data->output->filename, data->config.url,
  296. sizeof(data->output->filename));
  297. data->output->filename[sizeof(data->output->filename) - 1] = 0;
  298. AVDictionary *dict = NULL;
  299. if ((ret = av_dict_parse_string(&dict, data->config.muxer_settings,
  300. "=", " ", 0))) {
  301. blog(LOG_WARNING, "Failed to parse muxer settings: %s\n%s",
  302. av_err2str(ret), data->config.muxer_settings);
  303. av_dict_free(&dict);
  304. return false;
  305. }
  306. if (av_dict_count(dict) > 0) {
  307. struct dstr str = {0};
  308. AVDictionaryEntry *entry = NULL;
  309. while ((entry = av_dict_get(dict, "", entry,
  310. AV_DICT_IGNORE_SUFFIX)))
  311. dstr_catf(&str, "\n\t%s=%s", entry->key, entry->value);
  312. blog(LOG_INFO, "Using muxer settings:%s", str.array);
  313. dstr_free(&str);
  314. }
  315. ret = avformat_write_header(data->output, &dict);
  316. if (ret < 0) {
  317. blog(LOG_WARNING, "Error opening '%s': %s",
  318. data->config.url, av_err2str(ret));
  319. return false;
  320. }
  321. av_dict_free(&dict);
  322. return true;
  323. }
  324. static void close_video(struct ffmpeg_data *data)
  325. {
  326. avcodec_close(data->video->codec);
  327. avpicture_free(&data->dst_picture);
  328. // This format for some reason derefs video frame
  329. // too many times
  330. if (data->vcodec->id == AV_CODEC_ID_A64_MULTI ||
  331. data->vcodec->id == AV_CODEC_ID_A64_MULTI5)
  332. return;
  333. av_frame_free(&data->vframe);
  334. }
  335. static void close_audio(struct ffmpeg_data *data)
  336. {
  337. for (size_t i = 0; i < MAX_AV_PLANES; i++)
  338. circlebuf_free(&data->excess_frames[i]);
  339. av_freep(&data->samples[0]);
  340. avcodec_close(data->audio->codec);
  341. av_frame_free(&data->aframe);
  342. }
  343. static void ffmpeg_data_free(struct ffmpeg_data *data)
  344. {
  345. if (data->initialized)
  346. av_write_trailer(data->output);
  347. if (data->video)
  348. close_video(data);
  349. if (data->audio)
  350. close_audio(data);
  351. if (data->output) {
  352. if ((data->output->oformat->flags & AVFMT_NOFILE) == 0)
  353. avio_close(data->output->pb);
  354. avformat_free_context(data->output);
  355. }
  356. memset(data, 0, sizeof(struct ffmpeg_data));
  357. }
  358. static inline const char *safe_str(const char *s)
  359. {
  360. if (s == NULL)
  361. return "(NULL)";
  362. else
  363. return s;
  364. }
  365. static enum AVCodecID get_codec_id(const char *name, int id)
  366. {
  367. AVCodec *codec;
  368. if (id != 0)
  369. return (enum AVCodecID)id;
  370. if (!name || !*name)
  371. return AV_CODEC_ID_NONE;
  372. codec = avcodec_find_encoder_by_name(name);
  373. if (!codec)
  374. return AV_CODEC_ID_NONE;
  375. return codec->id;
  376. }
  377. static void set_encoder_ids(struct ffmpeg_data *data)
  378. {
  379. data->output->oformat->video_codec = get_codec_id(
  380. data->config.video_encoder,
  381. data->config.video_encoder_id);
  382. data->output->oformat->audio_codec = get_codec_id(
  383. data->config.audio_encoder,
  384. data->config.audio_encoder_id);
  385. }
  386. static bool ffmpeg_data_init(struct ffmpeg_data *data,
  387. struct ffmpeg_cfg *config)
  388. {
  389. bool is_rtmp = false;
  390. memset(data, 0, sizeof(struct ffmpeg_data));
  391. data->config = *config;
  392. if (!config->url || !*config->url)
  393. return false;
  394. av_register_all();
  395. avformat_network_init();
  396. is_rtmp = (astrcmpi_n(config->url, "rtmp://", 7) == 0);
  397. AVOutputFormat *output_format = av_guess_format(
  398. is_rtmp ? "flv" : data->config.format_name,
  399. data->config.url,
  400. is_rtmp ? NULL : data->config.format_mime_type);
  401. if (output_format == NULL) {
  402. blog(LOG_WARNING, "Couldn't find matching output format with "
  403. " parameters: name=%s, url=%s, mime=%s",
  404. safe_str(is_rtmp ?
  405. "flv" : data->config.format_name),
  406. safe_str(data->config.url),
  407. safe_str(is_rtmp ?
  408. NULL : data->config.format_mime_type));
  409. goto fail;
  410. }
  411. avformat_alloc_output_context2(&data->output, output_format,
  412. NULL, NULL);
  413. if (is_rtmp) {
  414. data->output->oformat->video_codec = AV_CODEC_ID_H264;
  415. data->output->oformat->audio_codec = AV_CODEC_ID_AAC;
  416. } else {
  417. if (data->config.format_name)
  418. set_encoder_ids(data);
  419. }
  420. if (!data->output) {
  421. blog(LOG_WARNING, "Couldn't create avformat context");
  422. goto fail;
  423. }
  424. if (!init_streams(data))
  425. goto fail;
  426. if (!open_output_file(data))
  427. goto fail;
  428. av_dump_format(data->output, 0, NULL, 1);
  429. data->initialized = true;
  430. return true;
  431. fail:
  432. blog(LOG_WARNING, "ffmpeg_data_init failed");
  433. ffmpeg_data_free(data);
  434. return false;
  435. }
  436. /* ------------------------------------------------------------------------- */
  437. static const char *ffmpeg_output_getname(void *unused)
  438. {
  439. UNUSED_PARAMETER(unused);
  440. return obs_module_text("FFmpegOutput");
  441. }
  442. static void ffmpeg_log_callback(void *param, int level, const char *format,
  443. va_list args)
  444. {
  445. if (level <= AV_LOG_INFO)
  446. blogva(LOG_DEBUG, format, args);
  447. UNUSED_PARAMETER(param);
  448. }
  449. static void *ffmpeg_output_create(obs_data_t *settings, obs_output_t *output)
  450. {
  451. struct ffmpeg_output *data = bzalloc(sizeof(struct ffmpeg_output));
  452. pthread_mutex_init_value(&data->write_mutex);
  453. data->output = output;
  454. if (pthread_mutex_init(&data->write_mutex, NULL) != 0)
  455. goto fail;
  456. if (os_event_init(&data->stop_event, OS_EVENT_TYPE_AUTO) != 0)
  457. goto fail;
  458. if (os_sem_init(&data->write_sem, 0) != 0)
  459. goto fail;
  460. av_log_set_callback(ffmpeg_log_callback);
  461. UNUSED_PARAMETER(settings);
  462. return data;
  463. fail:
  464. pthread_mutex_destroy(&data->write_mutex);
  465. os_event_destroy(data->stop_event);
  466. bfree(data);
  467. return NULL;
  468. }
  469. static void ffmpeg_output_stop(void *data);
  470. static void ffmpeg_deactivate(struct ffmpeg_output *output);
  471. static void ffmpeg_output_destroy(void *data)
  472. {
  473. struct ffmpeg_output *output = data;
  474. if (output) {
  475. if (output->connecting)
  476. pthread_join(output->start_thread, NULL);
  477. ffmpeg_output_stop(output);
  478. pthread_mutex_destroy(&output->write_mutex);
  479. os_sem_destroy(output->write_sem);
  480. os_event_destroy(output->stop_event);
  481. bfree(data);
  482. }
  483. }
  484. static inline void copy_data(AVPicture *pic, const struct video_data *frame,
  485. int height)
  486. {
  487. for (int plane = 0; plane < MAX_AV_PLANES; plane++) {
  488. if (!frame->data[plane])
  489. continue;
  490. int frame_rowsize = (int)frame->linesize[plane];
  491. int pic_rowsize = pic->linesize[plane];
  492. int bytes = frame_rowsize < pic_rowsize ?
  493. frame_rowsize : pic_rowsize;
  494. int plane_height = plane == 0 ? height : height/2;
  495. for (int y = 0; y < plane_height; y++) {
  496. int pos_frame = y * frame_rowsize;
  497. int pos_pic = y * pic_rowsize;
  498. memcpy(pic->data[plane] + pos_pic,
  499. frame->data[plane] + pos_frame,
  500. bytes);
  501. }
  502. }
  503. }
  504. static void receive_video(void *param, struct video_data *frame)
  505. {
  506. struct ffmpeg_output *output = param;
  507. struct ffmpeg_data *data = &output->ff_data;
  508. // codec doesn't support video or none configured
  509. if (!data->video)
  510. return;
  511. AVCodecContext *context = data->video->codec;
  512. AVPacket packet = {0};
  513. int ret = 0, got_packet;
  514. av_init_packet(&packet);
  515. if (!data->start_timestamp)
  516. data->start_timestamp = frame->timestamp;
  517. if (!!data->swscale)
  518. sws_scale(data->swscale, (const uint8_t *const *)frame->data,
  519. (const int*)frame->linesize,
  520. 0, data->config.height, data->dst_picture.data,
  521. data->dst_picture.linesize);
  522. else
  523. copy_data(&data->dst_picture, frame, context->height);
  524. if (data->output->flags & AVFMT_RAWPICTURE) {
  525. packet.flags |= AV_PKT_FLAG_KEY;
  526. packet.stream_index = data->video->index;
  527. packet.data = data->dst_picture.data[0];
  528. packet.size = sizeof(AVPicture);
  529. pthread_mutex_lock(&output->write_mutex);
  530. da_push_back(output->packets, &packet);
  531. pthread_mutex_unlock(&output->write_mutex);
  532. os_sem_post(output->write_sem);
  533. } else {
  534. data->vframe->pts = data->total_frames;
  535. ret = avcodec_encode_video2(context, &packet, data->vframe,
  536. &got_packet);
  537. if (ret < 0) {
  538. blog(LOG_WARNING, "receive_video: Error encoding "
  539. "video: %s", av_err2str(ret));
  540. return;
  541. }
  542. if (!ret && got_packet && packet.size) {
  543. packet.pts = rescale_ts(packet.pts, context,
  544. data->video->time_base);
  545. packet.dts = rescale_ts(packet.dts, context,
  546. data->video->time_base);
  547. packet.duration = (int)av_rescale_q(packet.duration,
  548. context->time_base,
  549. data->video->time_base);
  550. pthread_mutex_lock(&output->write_mutex);
  551. da_push_back(output->packets, &packet);
  552. pthread_mutex_unlock(&output->write_mutex);
  553. os_sem_post(output->write_sem);
  554. } else {
  555. ret = 0;
  556. }
  557. }
  558. if (ret != 0) {
  559. blog(LOG_WARNING, "receive_video: Error writing video: %s",
  560. av_err2str(ret));
  561. }
  562. data->total_frames++;
  563. }
  564. static void encode_audio(struct ffmpeg_output *output,
  565. struct AVCodecContext *context, size_t block_size)
  566. {
  567. struct ffmpeg_data *data = &output->ff_data;
  568. AVPacket packet = {0};
  569. int ret, got_packet;
  570. size_t total_size = data->frame_size * block_size * context->channels;
  571. data->aframe->nb_samples = data->frame_size;
  572. data->aframe->pts = av_rescale_q(data->total_samples,
  573. (AVRational){1, context->sample_rate},
  574. context->time_base);
  575. ret = avcodec_fill_audio_frame(data->aframe, context->channels,
  576. context->sample_fmt, data->samples[0],
  577. (int)total_size, 1);
  578. if (ret < 0) {
  579. blog(LOG_WARNING, "encode_audio: avcodec_fill_audio_frame "
  580. "failed: %s", av_err2str(ret));
  581. return;
  582. }
  583. data->total_samples += data->frame_size;
  584. ret = avcodec_encode_audio2(context, &packet, data->aframe,
  585. &got_packet);
  586. if (ret < 0) {
  587. blog(LOG_WARNING, "encode_audio: Error encoding audio: %s",
  588. av_err2str(ret));
  589. return;
  590. }
  591. if (!got_packet)
  592. return;
  593. packet.pts = rescale_ts(packet.pts, context, data->audio->time_base);
  594. packet.dts = rescale_ts(packet.dts, context, data->audio->time_base);
  595. packet.duration = (int)av_rescale_q(packet.duration, context->time_base,
  596. data->audio->time_base);
  597. packet.stream_index = data->audio->index;
  598. pthread_mutex_lock(&output->write_mutex);
  599. da_push_back(output->packets, &packet);
  600. pthread_mutex_unlock(&output->write_mutex);
  601. os_sem_post(output->write_sem);
  602. }
  603. static bool prepare_audio(struct ffmpeg_data *data,
  604. const struct audio_data *frame, struct audio_data *output)
  605. {
  606. *output = *frame;
  607. if (frame->timestamp < data->start_timestamp) {
  608. uint64_t duration = (uint64_t)frame->frames * 1000000000 /
  609. (uint64_t)data->audio_samplerate;
  610. uint64_t end_ts = (frame->timestamp + duration);
  611. uint64_t cutoff;
  612. if (end_ts <= data->start_timestamp)
  613. return false;
  614. cutoff = data->start_timestamp - frame->timestamp;
  615. cutoff = cutoff * (uint64_t)data->audio_samplerate /
  616. 1000000000;
  617. for (size_t i = 0; i < data->audio_planes; i++)
  618. output->data[i] += data->audio_size * (uint32_t)cutoff;
  619. output->frames -= (uint32_t)cutoff;
  620. }
  621. return true;
  622. }
  623. static void receive_audio(void *param, struct audio_data *frame)
  624. {
  625. struct ffmpeg_output *output = param;
  626. struct ffmpeg_data *data = &output->ff_data;
  627. size_t frame_size_bytes;
  628. struct audio_data in;
  629. // codec doesn't support audio or none configured
  630. if (!data->audio)
  631. return;
  632. AVCodecContext *context = data->audio->codec;
  633. if (!data->start_timestamp)
  634. return;
  635. if (!prepare_audio(data, frame, &in))
  636. return;
  637. frame_size_bytes = (size_t)data->frame_size * data->audio_size;
  638. for (size_t i = 0; i < data->audio_planes; i++)
  639. circlebuf_push_back(&data->excess_frames[i], in.data[i],
  640. in.frames * data->audio_size);
  641. while (data->excess_frames[0].size >= frame_size_bytes) {
  642. for (size_t i = 0; i < data->audio_planes; i++)
  643. circlebuf_pop_front(&data->excess_frames[i],
  644. data->samples[i], frame_size_bytes);
  645. encode_audio(output, context, data->audio_size);
  646. }
  647. }
  648. static int process_packet(struct ffmpeg_output *output)
  649. {
  650. AVPacket packet;
  651. bool new_packet = false;
  652. int ret;
  653. pthread_mutex_lock(&output->write_mutex);
  654. if (output->packets.num) {
  655. packet = output->packets.array[0];
  656. da_erase(output->packets, 0);
  657. new_packet = true;
  658. }
  659. pthread_mutex_unlock(&output->write_mutex);
  660. if (!new_packet)
  661. return 0;
  662. /*blog(LOG_DEBUG, "size = %d, flags = %lX, stream = %d, "
  663. "packets queued: %lu",
  664. packet.size, packet.flags,
  665. packet.stream_index, output->packets.num);*/
  666. ret = av_interleaved_write_frame(output->ff_data.output, &packet);
  667. if (ret < 0) {
  668. av_free_packet(&packet);
  669. blog(LOG_WARNING, "receive_audio: Error writing packet: %s",
  670. av_err2str(ret));
  671. return ret;
  672. }
  673. return 0;
  674. }
  675. static void *write_thread(void *data)
  676. {
  677. struct ffmpeg_output *output = data;
  678. while (os_sem_wait(output->write_sem) == 0) {
  679. /* check to see if shutting down */
  680. if (os_event_try(output->stop_event) == 0)
  681. break;
  682. int ret = process_packet(output);
  683. if (ret != 0) {
  684. int code = OBS_OUTPUT_ERROR;
  685. pthread_detach(output->write_thread);
  686. output->write_thread_active = false;
  687. if (ret == -ENOSPC)
  688. code = OBS_OUTPUT_NO_SPACE;
  689. obs_output_signal_stop(output->output, code);
  690. ffmpeg_deactivate(output);
  691. break;
  692. }
  693. }
  694. output->active = false;
  695. return NULL;
  696. }
  697. static inline const char *get_string_or_null(obs_data_t *settings,
  698. const char *name)
  699. {
  700. const char *value = obs_data_get_string(settings, name);
  701. if (!value || !strlen(value))
  702. return NULL;
  703. return value;
  704. }
  705. static bool try_connect(struct ffmpeg_output *output)
  706. {
  707. video_t *video = obs_output_video(output->output);
  708. const struct video_output_info *voi = video_output_get_info(video);
  709. struct ffmpeg_cfg config;
  710. obs_data_t *settings;
  711. bool success;
  712. int ret;
  713. settings = obs_output_get_settings(output->output);
  714. config.url = obs_data_get_string(settings, "url");
  715. config.format_name = get_string_or_null(settings, "format_name");
  716. config.format_mime_type = get_string_or_null(settings,
  717. "format_mime_type");
  718. config.muxer_settings = obs_data_get_string(settings, "muxer_settings");
  719. config.video_bitrate = (int)obs_data_get_int(settings, "video_bitrate");
  720. config.audio_bitrate = (int)obs_data_get_int(settings, "audio_bitrate");
  721. config.video_encoder = get_string_or_null(settings, "video_encoder");
  722. config.video_encoder_id = (int)obs_data_get_int(settings,
  723. "video_encoder_id");
  724. config.audio_encoder = get_string_or_null(settings, "audio_encoder");
  725. config.audio_encoder_id = (int)obs_data_get_int(settings,
  726. "audio_encoder_id");
  727. config.video_settings = obs_data_get_string(settings, "video_settings");
  728. config.audio_settings = obs_data_get_string(settings, "audio_settings");
  729. config.scale_width = (int)obs_data_get_int(settings, "scale_width");
  730. config.scale_height = (int)obs_data_get_int(settings, "scale_height");
  731. config.width = (int)obs_output_get_width(output->output);
  732. config.height = (int)obs_output_get_height(output->output);
  733. config.format = obs_to_ffmpeg_video_format(
  734. video_output_get_format(video));
  735. if (format_is_yuv(voi->format)) {
  736. config.color_range = voi->range == VIDEO_RANGE_FULL ?
  737. AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
  738. config.color_space = voi->colorspace == VIDEO_CS_709 ?
  739. AVCOL_SPC_BT709 : AVCOL_SPC_BT470BG;
  740. } else {
  741. config.color_range = AVCOL_RANGE_UNSPECIFIED;
  742. config.color_space = AVCOL_SPC_RGB;
  743. }
  744. if (config.format == AV_PIX_FMT_NONE) {
  745. blog(LOG_DEBUG, "invalid pixel format used for FFmpeg output");
  746. return false;
  747. }
  748. if (!config.scale_width)
  749. config.scale_width = config.width;
  750. if (!config.scale_height)
  751. config.scale_height = config.height;
  752. success = ffmpeg_data_init(&output->ff_data, &config);
  753. obs_data_release(settings);
  754. if (!success)
  755. return false;
  756. struct audio_convert_info aci = {
  757. .format = output->ff_data.audio_format
  758. };
  759. output->active = true;
  760. if (!obs_output_can_begin_data_capture(output->output, 0))
  761. return false;
  762. ret = pthread_create(&output->write_thread, NULL, write_thread, output);
  763. if (ret != 0) {
  764. blog(LOG_WARNING, "ffmpeg_output_start: failed to create write "
  765. "thread.");
  766. ffmpeg_output_stop(output);
  767. return false;
  768. }
  769. obs_output_set_video_conversion(output->output, NULL);
  770. obs_output_set_audio_conversion(output->output, &aci);
  771. obs_output_begin_data_capture(output->output, 0);
  772. output->write_thread_active = true;
  773. return true;
  774. }
  775. static void *start_thread(void *data)
  776. {
  777. struct ffmpeg_output *output = data;
  778. if (!try_connect(output))
  779. obs_output_signal_stop(output->output,
  780. OBS_OUTPUT_CONNECT_FAILED);
  781. output->connecting = false;
  782. return NULL;
  783. }
  784. static bool ffmpeg_output_start(void *data)
  785. {
  786. struct ffmpeg_output *output = data;
  787. int ret;
  788. if (output->connecting)
  789. return false;
  790. ret = pthread_create(&output->start_thread, NULL, start_thread, output);
  791. return (output->connecting = (ret == 0));
  792. }
  793. static void ffmpeg_output_stop(void *data)
  794. {
  795. struct ffmpeg_output *output = data;
  796. if (output->active) {
  797. obs_output_end_data_capture(output->output);
  798. ffmpeg_deactivate(output);
  799. }
  800. }
  801. static void ffmpeg_deactivate(struct ffmpeg_output *output)
  802. {
  803. if (output->write_thread_active) {
  804. os_event_signal(output->stop_event);
  805. os_sem_post(output->write_sem);
  806. pthread_join(output->write_thread, NULL);
  807. output->write_thread_active = false;
  808. }
  809. pthread_mutex_lock(&output->write_mutex);
  810. for (size_t i = 0; i < output->packets.num; i++)
  811. av_free_packet(output->packets.array+i);
  812. da_free(output->packets);
  813. pthread_mutex_unlock(&output->write_mutex);
  814. ffmpeg_data_free(&output->ff_data);
  815. }
  816. struct obs_output_info ffmpeg_output = {
  817. .id = "ffmpeg_output",
  818. .flags = OBS_OUTPUT_AUDIO | OBS_OUTPUT_VIDEO,
  819. .get_name = ffmpeg_output_getname,
  820. .create = ffmpeg_output_create,
  821. .destroy = ffmpeg_output_destroy,
  822. .start = ffmpeg_output_start,
  823. .stop = ffmpeg_output_stop,
  824. .raw_video = receive_video,
  825. .raw_audio = receive_audio,
  826. };