obs-ffmpeg-output.c 25 KB

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