obs-ffmpeg-output.c 26 KB

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