obs-ffmpeg-output.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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.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. /* NOTE: much of this stuff is test stuff that was more or less copied from
  24. * the muxing.c ffmpeg example */
  25. struct ffmpeg_data {
  26. AVStream *video;
  27. AVStream *audio;
  28. AVCodec *acodec;
  29. AVCodec *vcodec;
  30. AVFormatContext *output;
  31. struct SwsContext *swscale;
  32. int video_bitrate;
  33. AVPicture dst_picture;
  34. AVFrame *vframe;
  35. int frame_size;
  36. int total_frames;
  37. uint64_t start_timestamp;
  38. int audio_bitrate;
  39. uint32_t audio_samplerate;
  40. enum audio_format audio_format;
  41. size_t audio_planes;
  42. size_t audio_size;
  43. struct circlebuf excess_frames[MAX_AV_PLANES];
  44. uint8_t *samples[MAX_AV_PLANES];
  45. AVFrame *aframe;
  46. int total_samples;
  47. const char *filename_test;
  48. bool initialized;
  49. };
  50. struct ffmpeg_output {
  51. obs_output_t output;
  52. volatile bool active;
  53. struct ffmpeg_data ff_data;
  54. bool connecting;
  55. pthread_t start_thread;
  56. bool write_thread_active;
  57. pthread_mutex_t write_mutex;
  58. pthread_t write_thread;
  59. os_sem_t write_sem;
  60. os_event_t stop_event;
  61. DARRAY(AVPacket) packets;
  62. };
  63. /* ------------------------------------------------------------------------- */
  64. static inline enum AVPixelFormat obs_to_ffmpeg_video_format(
  65. enum video_format format)
  66. {
  67. switch (format) {
  68. case VIDEO_FORMAT_NONE: return AV_PIX_FMT_NONE;
  69. case VIDEO_FORMAT_I420: return AV_PIX_FMT_YUV420P;
  70. case VIDEO_FORMAT_NV12: return AV_PIX_FMT_NV12;
  71. case VIDEO_FORMAT_YVYU: return AV_PIX_FMT_NONE;
  72. case VIDEO_FORMAT_YUY2: return AV_PIX_FMT_YUYV422;
  73. case VIDEO_FORMAT_UYVY: return AV_PIX_FMT_UYVY422;
  74. case VIDEO_FORMAT_RGBA: return AV_PIX_FMT_RGBA;
  75. case VIDEO_FORMAT_BGRA: return AV_PIX_FMT_BGRA;
  76. case VIDEO_FORMAT_BGRX: return AV_PIX_FMT_BGRA;
  77. }
  78. return AV_PIX_FMT_NONE;
  79. }
  80. static inline enum audio_format convert_ffmpeg_sample_format(
  81. enum AVSampleFormat format)
  82. {
  83. switch ((uint32_t)format) {
  84. case AV_SAMPLE_FMT_U8: return AUDIO_FORMAT_U8BIT;
  85. case AV_SAMPLE_FMT_S16: return AUDIO_FORMAT_16BIT;
  86. case AV_SAMPLE_FMT_S32: return AUDIO_FORMAT_32BIT;
  87. case AV_SAMPLE_FMT_FLT: return AUDIO_FORMAT_FLOAT;
  88. case AV_SAMPLE_FMT_U8P: return AUDIO_FORMAT_U8BIT_PLANAR;
  89. case AV_SAMPLE_FMT_S16P: return AUDIO_FORMAT_16BIT_PLANAR;
  90. case AV_SAMPLE_FMT_S32P: return AUDIO_FORMAT_32BIT_PLANAR;
  91. case AV_SAMPLE_FMT_FLTP: return AUDIO_FORMAT_FLOAT_PLANAR;
  92. }
  93. /* shouldn't get here */
  94. return AUDIO_FORMAT_16BIT;
  95. }
  96. static bool new_stream(struct ffmpeg_data *data, AVStream **stream,
  97. AVCodec **codec, enum AVCodecID id)
  98. {
  99. *codec = avcodec_find_encoder(id);
  100. if (!*codec) {
  101. blog(LOG_WARNING, "Couldn't find encoder '%s'",
  102. avcodec_get_name(id));
  103. return false;
  104. }
  105. *stream = avformat_new_stream(data->output, *codec);
  106. if (!*stream) {
  107. blog(LOG_WARNING, "Couldn't create stream for encoder '%s'",
  108. avcodec_get_name(id));
  109. return false;
  110. }
  111. (*stream)->id = data->output->nb_streams-1;
  112. return true;
  113. }
  114. static bool open_video_codec(struct ffmpeg_data *data)
  115. {
  116. AVCodecContext *context = data->video->codec;
  117. int ret;
  118. if (data->vcodec->id == AV_CODEC_ID_H264) {
  119. av_opt_set(context->priv_data, "preset", "veryfast", 0);
  120. av_opt_set(context->priv_data, "x264-params", "nal-hrd=cbr", 0);
  121. }
  122. ret = avcodec_open2(context, data->vcodec, NULL);
  123. if (ret < 0) {
  124. blog(LOG_WARNING, "Failed to open video codec: %s",
  125. av_err2str(ret));
  126. return false;
  127. }
  128. data->vframe = av_frame_alloc();
  129. if (!data->vframe) {
  130. blog(LOG_WARNING, "Failed to allocate video frame");
  131. return false;
  132. }
  133. data->vframe->format = context->pix_fmt;
  134. data->vframe->width = context->width;
  135. data->vframe->height = context->height;
  136. ret = avpicture_alloc(&data->dst_picture, context->pix_fmt,
  137. context->width, context->height);
  138. if (ret < 0) {
  139. blog(LOG_WARNING, "Failed to allocate dst_picture: %s",
  140. av_err2str(ret));
  141. return false;
  142. }
  143. *((AVPicture*)data->vframe) = data->dst_picture;
  144. return true;
  145. }
  146. static bool init_swscale(struct ffmpeg_data *data, AVCodecContext *context)
  147. {
  148. data->swscale = sws_getContext(
  149. context->width, context->height, AV_PIX_FMT_YUV420P,
  150. context->width, context->height, context->pix_fmt,
  151. SWS_BICUBIC, NULL, NULL, NULL);
  152. if (!data->swscale) {
  153. blog(LOG_WARNING, "Could not initialize swscale");
  154. return false;
  155. }
  156. return true;
  157. }
  158. static bool create_video_stream(struct ffmpeg_data *data)
  159. {
  160. AVCodecContext *context;
  161. struct obs_video_info ovi;
  162. if (!obs_get_video_info(&ovi)) {
  163. blog(LOG_WARNING, "No active video");
  164. return false;
  165. }
  166. if (!new_stream(data, &data->video, &data->vcodec,
  167. data->output->oformat->video_codec))
  168. return false;
  169. context = data->video->codec;
  170. context->codec_id = data->output->oformat->video_codec;
  171. context->bit_rate = data->video_bitrate * 1000;
  172. context->rc_buffer_size = data->video_bitrate * 1000;
  173. context->rc_max_rate = data->video_bitrate * 1000;
  174. context->width = ovi.output_width;
  175. context->height = ovi.output_height;
  176. context->time_base.num = ovi.fps_den;
  177. context->time_base.den = ovi.fps_num;
  178. context->gop_size = 120;
  179. context->pix_fmt = AV_PIX_FMT_YUV420P;
  180. if (data->output->oformat->flags & AVFMT_GLOBALHEADER)
  181. context->flags |= CODEC_FLAG_GLOBAL_HEADER;
  182. if (!open_video_codec(data))
  183. return false;
  184. if (context->pix_fmt != AV_PIX_FMT_YUV420P)
  185. if (!init_swscale(data, context))
  186. return false;
  187. return true;
  188. }
  189. static bool open_audio_codec(struct ffmpeg_data *data)
  190. {
  191. AVCodecContext *context = data->audio->codec;
  192. int ret;
  193. data->aframe = av_frame_alloc();
  194. if (!data->aframe) {
  195. blog(LOG_WARNING, "Failed to allocate audio frame");
  196. return false;
  197. }
  198. context->strict_std_compliance = -2;
  199. ret = avcodec_open2(context, data->acodec, NULL);
  200. if (ret < 0) {
  201. blog(LOG_WARNING, "Failed to open audio codec: %s",
  202. av_err2str(ret));
  203. return false;
  204. }
  205. data->frame_size = context->frame_size ? context->frame_size : 1024;
  206. ret = av_samples_alloc(data->samples, NULL, context->channels,
  207. data->frame_size, context->sample_fmt, 0);
  208. if (ret < 0) {
  209. blog(LOG_WARNING, "Failed to create audio buffer: %s",
  210. av_err2str(ret));
  211. return false;
  212. }
  213. return true;
  214. }
  215. static bool create_audio_stream(struct ffmpeg_data *data)
  216. {
  217. AVCodecContext *context;
  218. struct audio_output_info aoi;
  219. if (!obs_get_audio_info(&aoi)) {
  220. blog(LOG_WARNING, "No active audio");
  221. return false;
  222. }
  223. if (!new_stream(data, &data->audio, &data->acodec,
  224. data->output->oformat->audio_codec))
  225. return false;
  226. context = data->audio->codec;
  227. context->bit_rate = data->audio_bitrate * 1000;
  228. context->channels = get_audio_channels(aoi.speakers);
  229. context->sample_rate = aoi.samples_per_sec;
  230. context->sample_fmt = data->acodec->sample_fmts ?
  231. data->acodec->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
  232. data->audio_samplerate = aoi.samples_per_sec;
  233. data->audio_format = convert_ffmpeg_sample_format(context->sample_fmt);
  234. data->audio_planes = get_audio_planes(data->audio_format, aoi.speakers);
  235. data->audio_size = get_audio_size(data->audio_format, aoi.speakers, 1);
  236. if (data->output->oformat->flags & AVFMT_GLOBALHEADER)
  237. context->flags |= CODEC_FLAG_GLOBAL_HEADER;
  238. return open_audio_codec(data);
  239. }
  240. static inline bool init_streams(struct ffmpeg_data *data)
  241. {
  242. AVOutputFormat *format = data->output->oformat;
  243. if (format->video_codec != AV_CODEC_ID_NONE)
  244. if (!create_video_stream(data))
  245. return false;
  246. if (format->audio_codec != AV_CODEC_ID_NONE)
  247. if (!create_audio_stream(data))
  248. return false;
  249. return true;
  250. }
  251. static inline bool open_output_file(struct ffmpeg_data *data)
  252. {
  253. AVOutputFormat *format = data->output->oformat;
  254. int ret;
  255. if ((format->flags & AVFMT_NOFILE) == 0) {
  256. ret = avio_open(&data->output->pb, data->filename_test,
  257. AVIO_FLAG_WRITE);
  258. if (ret < 0) {
  259. blog(LOG_WARNING, "Couldn't open file '%s', %s",
  260. data->filename_test, av_err2str(ret));
  261. return false;
  262. }
  263. }
  264. ret = avformat_write_header(data->output, NULL);
  265. if (ret < 0) {
  266. blog(LOG_WARNING, "Error opening file '%s': %s",
  267. data->filename_test, av_err2str(ret));
  268. return false;
  269. }
  270. return true;
  271. }
  272. static void close_video(struct ffmpeg_data *data)
  273. {
  274. avcodec_close(data->video->codec);
  275. avpicture_free(&data->dst_picture);
  276. av_frame_free(&data->vframe);
  277. }
  278. static void close_audio(struct ffmpeg_data *data)
  279. {
  280. for (size_t i = 0; i < MAX_AV_PLANES; i++)
  281. circlebuf_free(&data->excess_frames[i]);
  282. av_freep(&data->samples[0]);
  283. avcodec_close(data->audio->codec);
  284. av_frame_free(&data->aframe);
  285. }
  286. static void ffmpeg_data_free(struct ffmpeg_data *data)
  287. {
  288. if (data->initialized)
  289. av_write_trailer(data->output);
  290. if (data->video)
  291. close_video(data);
  292. if (data->audio)
  293. close_audio(data);
  294. if (data->output) {
  295. if ((data->output->oformat->flags & AVFMT_NOFILE) == 0)
  296. avio_close(data->output->pb);
  297. avformat_free_context(data->output);
  298. }
  299. memset(data, 0, sizeof(struct ffmpeg_data));
  300. }
  301. static bool ffmpeg_data_init(struct ffmpeg_data *data, const char *filename,
  302. int vbitrate, int abitrate)
  303. {
  304. bool is_rtmp = false;
  305. memset(data, 0, sizeof(struct ffmpeg_data));
  306. data->filename_test = filename;
  307. data->video_bitrate = vbitrate;
  308. data->audio_bitrate = abitrate;
  309. if (!filename || !*filename)
  310. return false;
  311. av_register_all();
  312. avformat_network_init();
  313. is_rtmp = (astrcmp_n(filename, "rtmp://", 7) == 0);
  314. /* TODO: settings */
  315. avformat_alloc_output_context2(&data->output, NULL,
  316. is_rtmp ? "flv" : NULL, data->filename_test);
  317. if (is_rtmp) {
  318. data->output->oformat->video_codec = AV_CODEC_ID_H264;
  319. data->output->oformat->audio_codec = AV_CODEC_ID_AAC;
  320. }
  321. if (!data->output) {
  322. blog(LOG_WARNING, "Couldn't create avformat context");
  323. goto fail;
  324. }
  325. if (!init_streams(data))
  326. goto fail;
  327. if (!open_output_file(data))
  328. goto fail;
  329. av_dump_format(data->output, 0, NULL, 1);
  330. data->initialized = true;
  331. return true;
  332. fail:
  333. blog(LOG_WARNING, "ffmpeg_data_init failed");
  334. ffmpeg_data_free(data);
  335. return false;
  336. }
  337. /* ------------------------------------------------------------------------- */
  338. static const char *ffmpeg_output_getname(const char *locale)
  339. {
  340. UNUSED_PARAMETER(locale);
  341. return "FFmpeg file output";
  342. }
  343. static void ffmpeg_log_callback(void *param, int level, const char *format,
  344. va_list args)
  345. {
  346. if (level <= AV_LOG_INFO)
  347. blogva(LOG_DEBUG, format, args);
  348. UNUSED_PARAMETER(param);
  349. }
  350. static void *ffmpeg_output_create(obs_data_t settings, obs_output_t output)
  351. {
  352. struct ffmpeg_output *data = bzalloc(sizeof(struct ffmpeg_output));
  353. pthread_mutex_init_value(&data->write_mutex);
  354. data->output = output;
  355. if (pthread_mutex_init(&data->write_mutex, NULL) != 0)
  356. goto fail;
  357. if (os_event_init(&data->stop_event, OS_EVENT_TYPE_AUTO) != 0)
  358. goto fail;
  359. if (os_sem_init(&data->write_sem, 0) != 0)
  360. goto fail;
  361. signal_handler_add(obs_output_signalhandler(output),
  362. "void connect(ptr output, bool success)");
  363. av_log_set_callback(ffmpeg_log_callback);
  364. UNUSED_PARAMETER(settings);
  365. return data;
  366. fail:
  367. pthread_mutex_destroy(&data->write_mutex);
  368. os_event_destroy(data->stop_event);
  369. bfree(data);
  370. return NULL;
  371. }
  372. static void ffmpeg_output_stop(void *data);
  373. static void ffmpeg_output_destroy(void *data)
  374. {
  375. struct ffmpeg_output *output = data;
  376. if (output) {
  377. if (output->connecting)
  378. pthread_join(output->start_thread, NULL);
  379. ffmpeg_output_stop(output);
  380. pthread_mutex_destroy(&output->write_mutex);
  381. os_sem_destroy(output->write_sem);
  382. os_event_destroy(output->stop_event);
  383. bfree(data);
  384. }
  385. }
  386. static inline int64_t rescale_ts(int64_t val, AVCodecContext *context,
  387. AVStream *stream)
  388. {
  389. return av_rescale_q_rnd(val, context->time_base,
  390. stream->time_base,
  391. AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
  392. }
  393. #define YUV420_PLANES 3
  394. static inline void copy_data(AVPicture *pic, const struct video_data *frame,
  395. int height)
  396. {
  397. for (int plane = 0; plane < YUV420_PLANES; plane++) {
  398. int frame_rowsize = (int)frame->linesize[plane];
  399. int pic_rowsize = pic->linesize[plane];
  400. int bytes = frame_rowsize < pic_rowsize ?
  401. frame_rowsize : pic_rowsize;
  402. int plane_height = plane == 0 ? height : height/2;
  403. for (int y = 0; y < plane_height; y++) {
  404. int pos_frame = y * frame_rowsize;
  405. int pos_pic = y * pic_rowsize;
  406. memcpy(pic->data[plane] + pos_pic,
  407. frame->data[plane] + pos_frame,
  408. bytes);
  409. }
  410. }
  411. }
  412. static void receive_video(void *param, const struct video_data *frame)
  413. {
  414. struct ffmpeg_output *output = param;
  415. struct ffmpeg_data *data = &output->ff_data;
  416. AVCodecContext *context = data->video->codec;
  417. AVPacket packet = {0};
  418. int ret, got_packet;
  419. av_init_packet(&packet);
  420. if (!data->start_timestamp)
  421. data->start_timestamp = frame->timestamp;
  422. if (context->pix_fmt != AV_PIX_FMT_YUV420P)
  423. sws_scale(data->swscale, frame->data,
  424. (const int*)frame->linesize,
  425. 0, context->height, data->dst_picture.data,
  426. data->dst_picture.linesize);
  427. else
  428. copy_data(&data->dst_picture, frame, context->height);
  429. if (data->output->flags & AVFMT_RAWPICTURE) {
  430. packet.flags |= AV_PKT_FLAG_KEY;
  431. packet.stream_index = data->video->index;
  432. packet.data = data->dst_picture.data[0];
  433. packet.size = sizeof(AVPicture);
  434. pthread_mutex_lock(&output->write_mutex);
  435. da_push_back(output->packets, &packet);
  436. pthread_mutex_unlock(&output->write_mutex);
  437. os_sem_post(output->write_sem);
  438. } else {
  439. data->vframe->pts = data->total_frames;
  440. ret = avcodec_encode_video2(context, &packet, data->vframe,
  441. &got_packet);
  442. if (ret < 0) {
  443. blog(LOG_WARNING, "receive_video: Error encoding "
  444. "video: %s", av_err2str(ret));
  445. return;
  446. }
  447. if (!ret && got_packet && packet.size) {
  448. packet.pts = rescale_ts(packet.pts, context,
  449. data->video);
  450. packet.dts = rescale_ts(packet.dts, context,
  451. data->video);
  452. packet.duration = (int)av_rescale_q(packet.duration,
  453. context->time_base,
  454. data->video->time_base);
  455. pthread_mutex_lock(&output->write_mutex);
  456. da_push_back(output->packets, &packet);
  457. pthread_mutex_unlock(&output->write_mutex);
  458. os_sem_post(output->write_sem);
  459. } else {
  460. ret = 0;
  461. }
  462. }
  463. if (ret != 0) {
  464. blog(LOG_WARNING, "receive_video: Error writing video: %s",
  465. av_err2str(ret));
  466. }
  467. data->total_frames++;
  468. }
  469. static inline void encode_audio(struct ffmpeg_output *output,
  470. struct AVCodecContext *context, size_t block_size)
  471. {
  472. struct ffmpeg_data *data = &output->ff_data;
  473. AVPacket packet = {0};
  474. int ret, got_packet;
  475. size_t total_size = data->frame_size * block_size * context->channels;
  476. data->aframe->nb_samples = data->frame_size;
  477. data->aframe->pts = av_rescale_q(data->total_samples,
  478. (AVRational){1, context->sample_rate},
  479. context->time_base);
  480. ret = avcodec_fill_audio_frame(data->aframe, context->channels,
  481. context->sample_fmt, data->samples[0],
  482. (int)total_size, 1);
  483. if (ret < 0) {
  484. blog(LOG_WARNING, "receive_audio: avcodec_fill_audio_frame "
  485. "failed: %s", av_err2str(ret));
  486. return;
  487. }
  488. data->total_samples += data->frame_size;
  489. ret = avcodec_encode_audio2(context, &packet, data->aframe,
  490. &got_packet);
  491. if (ret < 0) {
  492. blog(LOG_WARNING, "receive_audio: Error encoding audio: %s",
  493. av_err2str(ret));
  494. return;
  495. }
  496. if (!got_packet)
  497. return;
  498. packet.pts = rescale_ts(packet.pts, context, data->audio);
  499. packet.dts = rescale_ts(packet.dts, context, data->audio);
  500. packet.duration = (int)av_rescale_q(packet.duration, context->time_base,
  501. data->audio->time_base);
  502. packet.stream_index = data->audio->index;
  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. }
  508. static bool prepare_audio(struct ffmpeg_data *data,
  509. const struct audio_data *frame, struct audio_data *output)
  510. {
  511. *output = *frame;
  512. if (frame->timestamp < data->start_timestamp) {
  513. uint64_t duration = (uint64_t)frame->frames * 1000000000 /
  514. (uint64_t)data->audio_samplerate;
  515. uint64_t end_ts = (frame->timestamp + duration);
  516. uint64_t cutoff;
  517. if (end_ts <= data->start_timestamp)
  518. return false;
  519. cutoff = data->start_timestamp - frame->timestamp;
  520. cutoff = cutoff * (uint64_t)data->audio_samplerate /
  521. 1000000000;
  522. for (size_t i = 0; i < data->audio_planes; i++)
  523. output->data[i] += data->audio_size * (uint32_t)cutoff;
  524. output->frames -= (uint32_t)cutoff;
  525. }
  526. return true;
  527. }
  528. static void receive_audio(void *param, const struct audio_data *frame)
  529. {
  530. struct ffmpeg_output *output = param;
  531. struct ffmpeg_data *data = &output->ff_data;
  532. size_t frame_size_bytes;
  533. struct audio_data in;
  534. AVCodecContext *context = data->audio->codec;
  535. if (!data->start_timestamp)
  536. return;
  537. if (!prepare_audio(data, frame, &in))
  538. return;
  539. frame_size_bytes = (size_t)data->frame_size * data->audio_size;
  540. for (size_t i = 0; i < data->audio_planes; i++)
  541. circlebuf_push_back(&data->excess_frames[i], in.data[i],
  542. in.frames * data->audio_size);
  543. while (data->excess_frames[0].size >= frame_size_bytes) {
  544. for (size_t i = 0; i < data->audio_planes; i++)
  545. circlebuf_pop_front(&data->excess_frames[i],
  546. data->samples[i], frame_size_bytes);
  547. encode_audio(output, context, data->audio_size);
  548. }
  549. }
  550. static bool process_packet(struct ffmpeg_output *output)
  551. {
  552. AVPacket packet;
  553. bool new_packet = false;
  554. int ret;
  555. pthread_mutex_lock(&output->write_mutex);
  556. if (output->packets.num) {
  557. packet = output->packets.array[0];
  558. da_erase(output->packets, 0);
  559. new_packet = true;
  560. }
  561. pthread_mutex_unlock(&output->write_mutex);
  562. if (!new_packet)
  563. return true;
  564. /*blog(LOG_DEBUG, "size = %d, flags = %lX, stream = %d, "
  565. "packets queued: %lu",
  566. packet.size, packet.flags,
  567. packet.stream_index, output->packets.num);*/
  568. ret = av_interleaved_write_frame(output->ff_data.output, &packet);
  569. if (ret < 0) {
  570. av_free_packet(&packet);
  571. blog(LOG_WARNING, "receive_audio: Error writing packet: %s",
  572. av_err2str(ret));
  573. return false;
  574. }
  575. return true;
  576. }
  577. static void *write_thread(void *data)
  578. {
  579. struct ffmpeg_output *output = data;
  580. while (os_sem_wait(output->write_sem) == 0) {
  581. /* check to see if shutting down */
  582. if (os_event_try(output->stop_event) == 0)
  583. break;
  584. if (!process_packet(output)) {
  585. pthread_detach(output->write_thread);
  586. output->write_thread_active = false;
  587. ffmpeg_output_stop(output);
  588. break;
  589. }
  590. }
  591. output->active = false;
  592. return NULL;
  593. }
  594. static bool try_connect(struct ffmpeg_output *output)
  595. {
  596. video_t video = obs_video();
  597. audio_t audio = obs_audio();
  598. const char *filename_test;
  599. obs_data_t settings;
  600. int audio_bitrate, video_bitrate;
  601. int ret;
  602. if (!video || !audio) {
  603. blog(LOG_WARNING, "ffmpeg_output_start: audio and video must "
  604. "both be active (as of this writing)");
  605. return false;
  606. }
  607. settings = obs_output_get_settings(output->output);
  608. filename_test = obs_data_getstring(settings, "filename");
  609. video_bitrate = (int)obs_data_getint(settings, "video_bitrate");
  610. audio_bitrate = (int)obs_data_getint(settings, "audio_bitrate");
  611. obs_data_release(settings);
  612. if (!filename_test || !*filename_test)
  613. return false;
  614. if (!ffmpeg_data_init(&output->ff_data, filename_test,
  615. video_bitrate, audio_bitrate))
  616. return false;
  617. struct audio_convert_info aci = {
  618. .format = output->ff_data.audio_format
  619. };
  620. struct video_scale_info vsi = {
  621. .format = VIDEO_FORMAT_I420
  622. };
  623. output->active = true;
  624. ret = pthread_create(&output->write_thread, NULL, write_thread, output);
  625. if (ret != 0) {
  626. blog(LOG_WARNING, "ffmpeg_output_start: failed to create write "
  627. "thread.");
  628. ffmpeg_output_stop(output);
  629. return false;
  630. }
  631. video_output_connect(video, &vsi, receive_video, output);
  632. audio_output_connect(audio, &aci, receive_audio, output);
  633. output->write_thread_active = true;
  634. return true;
  635. }
  636. static void *start_thread(void *data)
  637. {
  638. struct ffmpeg_output *output = data;
  639. struct calldata params = {0};
  640. bool success = try_connect(output);
  641. output->connecting = false;
  642. calldata_setbool(&params, "success", success);
  643. calldata_setptr(&params, "output", output->output);
  644. signal_handler_signal(obs_output_signalhandler(output->output),
  645. "connect", &params);
  646. calldata_free(&params);
  647. return NULL;
  648. }
  649. static bool ffmpeg_output_start(void *data)
  650. {
  651. struct ffmpeg_output *output = data;
  652. int ret;
  653. if (output->connecting)
  654. return false;
  655. ret = pthread_create(&output->start_thread, NULL, start_thread, output);
  656. return (output->connecting = (ret == 0));
  657. }
  658. static void ffmpeg_output_stop(void *data)
  659. {
  660. struct ffmpeg_output *output = data;
  661. if (output->active) {
  662. video_output_disconnect(obs_video(), receive_video, data);
  663. audio_output_disconnect(obs_audio(), receive_audio, data);
  664. if (output->write_thread_active) {
  665. os_event_signal(output->stop_event);
  666. os_sem_post(output->write_sem);
  667. pthread_join(output->write_thread, NULL);
  668. output->write_thread_active = false;
  669. }
  670. pthread_mutex_lock(&output->write_mutex);
  671. for (size_t i = 0; i < output->packets.num; i++)
  672. av_free_packet(output->packets.array+i);
  673. da_free(output->packets);
  674. pthread_mutex_unlock(&output->write_mutex);
  675. ffmpeg_data_free(&output->ff_data);
  676. }
  677. }
  678. static bool ffmpeg_output_active(void *data)
  679. {
  680. struct ffmpeg_output *output = data;
  681. return output->active;
  682. }
  683. struct obs_output_info ffmpeg_output = {
  684. .id = "ffmpeg_output",
  685. .getname = ffmpeg_output_getname,
  686. .create = ffmpeg_output_create,
  687. .destroy = ffmpeg_output_destroy,
  688. .start = ffmpeg_output_start,
  689. .stop = ffmpeg_output_stop,
  690. .active = ffmpeg_output_active
  691. };