obs-source.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /******************************************************************************
  2. Copyright (C) 2013 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 3 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 "media-io/format-conversion.h"
  15. #include "util/platform.h"
  16. #include "obs.h"
  17. #include "obs-data.h"
  18. static void obs_source_destroy(obs_source_t source);
  19. bool get_source_info(void *module, const char *module_name,
  20. const char *source_name, struct source_info *info)
  21. {
  22. info->getname = load_module_subfunc(module, module_name,
  23. source_name, "getname", true);
  24. info->create = load_module_subfunc(module, module_name,
  25. source_name,"create", true);
  26. info->destroy = load_module_subfunc(module, module_name,
  27. source_name, "destroy", true);
  28. info->get_output_flags = load_module_subfunc(module, module_name,
  29. source_name, "get_output_flags", true);
  30. if (!info->getname || !info->create || !info->destroy ||
  31. !info->get_output_flags)
  32. return false;
  33. info->config = load_module_subfunc(module, module_name,
  34. source_name, "config", false);
  35. info->activate = load_module_subfunc(module, module_name,
  36. source_name, "activate", false);
  37. info->deactivate = load_module_subfunc(module, module_name,
  38. source_name, "deactivate", false);
  39. info->video_tick = load_module_subfunc(module, module_name,
  40. source_name, "video_tick", false);
  41. info->video_render = load_module_subfunc(module, module_name,
  42. source_name, "video_render", false);
  43. info->getwidth = load_module_subfunc(module, module_name,
  44. source_name, "getwidth", false);
  45. info->getheight = load_module_subfunc(module, module_name,
  46. source_name, "getheight", false);
  47. info->getparam = load_module_subfunc(module, module_name,
  48. source_name, "getparam", false);
  49. info->setparam = load_module_subfunc(module, module_name,
  50. source_name, "setparam", false);
  51. info->enum_children = load_module_subfunc(module, module_name,
  52. source_name, "enum_children", false);
  53. info->filter_video = load_module_subfunc(module, module_name,
  54. source_name, "filter_video", false);
  55. info->filter_audio = load_module_subfunc(module, module_name,
  56. source_name, "filter_audio", false);
  57. info->name = source_name;
  58. return true;
  59. }
  60. static inline const struct source_info *find_source(struct darray *list,
  61. const char *name)
  62. {
  63. size_t i;
  64. struct source_info *array = list->array;
  65. for (i = 0; i < list->num; i++) {
  66. struct source_info *info = array+i;
  67. if (strcmp(info->name, name) == 0)
  68. return info;
  69. }
  70. return NULL;
  71. }
  72. bool obs_source_init(struct obs_source *source, const char *settings,
  73. const struct source_info *info)
  74. {
  75. uint32_t flags = info->get_output_flags(source->data);
  76. source->refs = 1;
  77. pthread_mutex_init_value(&source->filter_mutex);
  78. pthread_mutex_init_value(&source->video_mutex);
  79. pthread_mutex_init_value(&source->audio_mutex);
  80. dstr_copy(&source->settings, settings);
  81. memcpy(&source->callbacks, info, sizeof(struct source_info));
  82. if (pthread_mutex_init(&source->filter_mutex, NULL) != 0)
  83. return false;
  84. if (pthread_mutex_init(&source->audio_mutex, NULL) != 0)
  85. return false;
  86. if (pthread_mutex_init(&source->video_mutex, NULL) != 0)
  87. return false;
  88. if (flags & SOURCE_AUDIO) {
  89. source->audio_line = audio_output_createline(obs->audio.audio);
  90. if (!source->audio_line) {
  91. blog(LOG_ERROR, "Failed to create audio line for "
  92. "source");
  93. return false;
  94. }
  95. }
  96. return true;
  97. }
  98. obs_source_t obs_source_create(enum obs_source_type type, const char *name,
  99. const char *settings)
  100. {
  101. const struct source_info *info = NULL;
  102. struct darray *list = NULL;
  103. struct obs_source *source;
  104. switch (type) {
  105. case SOURCE_INPUT: list = &obs->input_types.da; break;
  106. case SOURCE_FILTER: list = &obs->filter_types.da; break;
  107. case SOURCE_TRANSITION: list = &obs->transition_types.da; break;
  108. case SOURCE_SCENE:
  109. default:
  110. return NULL;
  111. }
  112. info = find_source(list, name);
  113. if (!info) {
  114. blog(LOG_WARNING, "Source '%s' not found", name);
  115. return NULL;
  116. }
  117. source = bmalloc(sizeof(struct obs_source));
  118. memset(source, 0, sizeof(struct obs_source));
  119. source->data = info->create(settings, source);
  120. if (!source->data)
  121. goto fail;
  122. if (!obs_source_init(source, settings, info))
  123. goto fail;
  124. return source;
  125. fail:
  126. blog(LOG_ERROR, "obs_source_create failed");
  127. obs_source_destroy(source);
  128. return NULL;
  129. }
  130. static void obs_source_destroy(obs_source_t source)
  131. {
  132. size_t i;
  133. if (source->filter_parent)
  134. obs_source_filter_remove(source->filter_parent, source);
  135. for (i = 0; i < source->filters.num; i++)
  136. obs_source_release(source->filters.array[i]);
  137. for (i = 0; i < source->audio_wait_buffer.num; i++)
  138. audiobuf_free(source->audio_wait_buffer.array+i);
  139. for (i = 0; i < source->video_frames.num; i++)
  140. source_frame_destroy(source->video_frames.array[i]);
  141. gs_entercontext(obs->video.graphics);
  142. texture_destroy(source->output_texture);
  143. gs_leavecontext();
  144. if (source->data)
  145. source->callbacks.destroy(source->data);
  146. bfree(source->audio_data.data);
  147. audio_line_destroy(source->audio_line);
  148. audio_resampler_destroy(source->resampler);
  149. da_free(source->video_frames);
  150. da_free(source->audio_wait_buffer);
  151. da_free(source->filters);
  152. pthread_mutex_destroy(&source->filter_mutex);
  153. pthread_mutex_destroy(&source->audio_mutex);
  154. pthread_mutex_destroy(&source->video_mutex);
  155. dstr_free(&source->settings);
  156. bfree(source);
  157. }
  158. void obs_source_addref(obs_source_t source)
  159. {
  160. assert(source != NULL);
  161. if (!source)
  162. return;
  163. ++source->refs;
  164. }
  165. void obs_source_release(obs_source_t source)
  166. {
  167. assert(source != NULL);
  168. if (!source)
  169. return;
  170. if (--source->refs == 0)
  171. obs_source_destroy(source);
  172. }
  173. bool obs_source_removed(obs_source_t source)
  174. {
  175. return source->removed;
  176. }
  177. uint32_t obs_source_get_output_flags(obs_source_t source)
  178. {
  179. return source->callbacks.get_output_flags(source->data);
  180. }
  181. bool obs_source_hasconfig(obs_source_t source)
  182. {
  183. return source->callbacks.config != NULL;
  184. }
  185. void obs_source_config(obs_source_t source, void *parent)
  186. {
  187. if (source->callbacks.config)
  188. source->callbacks.config(source->data, parent);
  189. }
  190. void obs_source_activate(obs_source_t source)
  191. {
  192. if (source->callbacks.activate)
  193. source->callbacks.activate(source->data);
  194. }
  195. void obs_source_deactivate(obs_source_t source)
  196. {
  197. if (source->callbacks.deactivate)
  198. source->callbacks.deactivate(source->data);
  199. }
  200. void obs_source_video_tick(obs_source_t source, float seconds)
  201. {
  202. if (source->callbacks.video_tick)
  203. source->callbacks.video_tick(source->data, seconds);
  204. }
  205. #define MAX_VARIANCE 2000000000ULL
  206. static void source_output_audio_line(obs_source_t source,
  207. const struct audio_data *data)
  208. {
  209. struct audio_data in = *data;
  210. if (!in.timestamp) {
  211. in.timestamp = os_gettime_ns();
  212. if (!source->timing_set) {
  213. source->timing_set = true;
  214. source->timing_adjust = 0;
  215. }
  216. }
  217. if (!source->timing_set) {
  218. source->timing_set = true;
  219. source->timing_adjust = in.timestamp - os_gettime_ns();
  220. /* detects 'directly' set timestamps as long as they're within
  221. * a certain threashold */
  222. if ((source->timing_adjust+MAX_VARIANCE) < MAX_VARIANCE*2)
  223. source->timing_adjust = 0;
  224. }
  225. in.timestamp += source->timing_adjust;
  226. audio_line_output(source->audio_line, &in);
  227. }
  228. static void obs_source_flush_audio_wait_buffer(obs_source_t source)
  229. {
  230. size_t i;
  231. pthread_mutex_lock(&source->audio_mutex);
  232. source->timing_set = true;
  233. for (i = 0; i < source->audio_wait_buffer.num; i++) {
  234. struct audiobuf *buf = source->audio_wait_buffer.array+i;
  235. struct audio_data data;
  236. data.data = buf->data;
  237. data.frames = buf->frames;
  238. data.timestamp = buf->timestamp + source->timing_adjust;
  239. audio_line_output(source->audio_line, &data);
  240. audiobuf_free(buf);
  241. }
  242. da_free(source->audio_wait_buffer);
  243. pthread_mutex_unlock(&source->audio_mutex);
  244. }
  245. static bool set_texture_size(obs_source_t source, struct source_frame *frame)
  246. {
  247. if (source->output_texture) {
  248. uint32_t width = texture_getwidth(source->output_texture);
  249. uint32_t height = texture_getheight(source->output_texture);
  250. if (width == frame->width && height == frame->height)
  251. return true;
  252. }
  253. texture_destroy(source->output_texture);
  254. source->output_texture = gs_create_texture(frame->width, frame->height,
  255. GS_RGBA, 1, NULL, GS_DYNAMIC);
  256. return source->output_texture != NULL;
  257. }
  258. enum convert_type {
  259. CONVERT_NONE,
  260. CONVERT_NV12,
  261. CONVERT_420,
  262. CONVERT_422_U,
  263. CONVERT_422_Y,
  264. };
  265. static inline enum convert_type get_convert_type(enum video_format format)
  266. {
  267. switch (format) {
  268. case VIDEO_FORMAT_I420:
  269. return CONVERT_420;
  270. case VIDEO_FORMAT_NV12:
  271. return CONVERT_NV12;
  272. case VIDEO_FORMAT_YVYU:
  273. case VIDEO_FORMAT_YUY2:
  274. return CONVERT_422_Y;
  275. case VIDEO_FORMAT_UYVY:
  276. return CONVERT_422_U;
  277. case VIDEO_FORMAT_UNKNOWN:
  278. case VIDEO_FORMAT_YUVX:
  279. case VIDEO_FORMAT_UYVX:
  280. case VIDEO_FORMAT_RGBA:
  281. case VIDEO_FORMAT_BGRA:
  282. case VIDEO_FORMAT_BGRX:
  283. return CONVERT_NONE;
  284. }
  285. return CONVERT_NONE;
  286. }
  287. static inline bool is_yuv(enum video_format format)
  288. {
  289. switch (format) {
  290. case VIDEO_FORMAT_I420:
  291. case VIDEO_FORMAT_NV12:
  292. case VIDEO_FORMAT_YVYU:
  293. case VIDEO_FORMAT_YUY2:
  294. case VIDEO_FORMAT_UYVY:
  295. case VIDEO_FORMAT_YUVX:
  296. case VIDEO_FORMAT_UYVX:
  297. return true;
  298. case VIDEO_FORMAT_UNKNOWN:
  299. case VIDEO_FORMAT_RGBA:
  300. case VIDEO_FORMAT_BGRA:
  301. case VIDEO_FORMAT_BGRX:
  302. return false;
  303. }
  304. return false;
  305. }
  306. static bool upload_frame(texture_t tex, const struct source_frame *frame)
  307. {
  308. void *ptr;
  309. uint32_t row_bytes;
  310. enum convert_type type = get_convert_type(frame->format);
  311. if (type == CONVERT_NONE) {
  312. texture_setimage(tex, frame->data, frame->row_bytes, false);
  313. return true;
  314. }
  315. if (!texture_map(tex, &ptr, &row_bytes))
  316. return false;
  317. if (type == CONVERT_420)
  318. decompress_420(frame->data, frame->width, frame->height,
  319. frame->row_bytes, 0, frame->height, ptr);
  320. else if (type == CONVERT_NV12)
  321. decompress_nv12(frame->data, frame->width, frame->height,
  322. frame->row_bytes, 0, frame->height, ptr);
  323. else if (type == CONVERT_422_Y)
  324. decompress_422(frame->data, frame->width, frame->height,
  325. frame->row_bytes, 0, frame->height, ptr, true);
  326. else if (type == CONVERT_422_U)
  327. decompress_422(frame->data, frame->width, frame->height,
  328. frame->row_bytes, 0, frame->height, ptr, false);
  329. texture_unmap(tex);
  330. return true;
  331. }
  332. static void obs_source_draw_texture(texture_t tex, struct source_frame *frame)
  333. {
  334. effect_t effect = obs->video.default_effect;
  335. bool yuv = is_yuv(frame->format);
  336. const char *type = yuv ? "DrawYUVToRGB" : "DrawRGB";
  337. technique_t tech;
  338. eparam_t param;
  339. if (!upload_frame(tex, frame))
  340. return;
  341. tech = effect_gettechnique(effect, type);
  342. technique_begin(tech);
  343. technique_beginpass(tech, 0);
  344. if (yuv) {
  345. param = effect_getparambyname(effect, "yuv_matrix");
  346. effect_setval(effect, param, frame->yuv_matrix,
  347. sizeof(float) * 16);
  348. }
  349. param = effect_getparambyname(effect, "diffuse");
  350. effect_settexture(effect, param, tex);
  351. gs_draw_sprite(tex, frame->flip ? GS_FLIP_V : 0);
  352. technique_endpass(tech);
  353. technique_end(tech);
  354. }
  355. static void obs_source_render_async_video(obs_source_t source)
  356. {
  357. struct source_frame *frame = obs_source_getframe(source);
  358. if (!frame)
  359. return;
  360. source->timing_adjust = frame->timestamp - os_gettime_ns();
  361. if (!source->timing_set && source->audio_wait_buffer.num)
  362. obs_source_flush_audio_wait_buffer(source);
  363. if (set_texture_size(source, frame))
  364. obs_source_draw_texture(source->output_texture, frame);
  365. obs_source_releaseframe(source, frame);
  366. }
  367. void obs_source_video_render(obs_source_t source)
  368. {
  369. if (source->callbacks.video_render) {
  370. if (source->filters.num && !source->rendering_filter) {
  371. source->rendering_filter = true;
  372. obs_source_video_render(source->filters.array[0]);
  373. source->rendering_filter = false;
  374. } else {
  375. source->callbacks.video_render(source->data);
  376. }
  377. } else if (source->filter_target) {
  378. obs_source_video_render(source->filter_target);
  379. } else {
  380. obs_source_render_async_video(source);
  381. }
  382. }
  383. uint32_t obs_source_getwidth(obs_source_t source)
  384. {
  385. if (source->callbacks.getwidth)
  386. return source->callbacks.getwidth(source->data);
  387. return 0;
  388. }
  389. uint32_t obs_source_getheight(obs_source_t source)
  390. {
  391. if (source->callbacks.getheight)
  392. return source->callbacks.getheight(source->data);
  393. return 0;
  394. }
  395. size_t obs_source_getparam(obs_source_t source, const char *param, void *buf,
  396. size_t buf_size)
  397. {
  398. if (source->callbacks.getparam)
  399. return source->callbacks.getparam(source->data, param, buf,
  400. buf_size);
  401. return 0;
  402. }
  403. void obs_source_setparam(obs_source_t source, const char *param,
  404. const void *data, size_t size)
  405. {
  406. if (source->callbacks.setparam)
  407. source->callbacks.setparam(source->data, param, data, size);
  408. }
  409. bool obs_source_enum_children(obs_source_t source, size_t idx,
  410. obs_source_t *child)
  411. {
  412. if (source->callbacks.enum_children)
  413. return source->callbacks.enum_children(source, idx, child);
  414. return false;
  415. }
  416. obs_source_t obs_filter_gettarget(obs_source_t filter)
  417. {
  418. return filter->filter_target;
  419. }
  420. void obs_source_filter_add(obs_source_t source, obs_source_t filter)
  421. {
  422. pthread_mutex_lock(&source->filter_mutex);
  423. if (da_find(source->filters, &filter, 0) != DARRAY_INVALID) {
  424. blog(LOG_WARNING, "Tried to add a filter that was already "
  425. "present on the source");
  426. return;
  427. }
  428. if (source->filters.num) {
  429. obs_source_t *back = da_end(source->filters);
  430. (*back)->filter_target = filter;
  431. }
  432. da_push_back(source->filters, &filter);
  433. pthread_mutex_unlock(&source->filter_mutex);
  434. filter->filter_parent = source;
  435. filter->filter_target = source;
  436. }
  437. void obs_source_filter_remove(obs_source_t source, obs_source_t filter)
  438. {
  439. size_t idx;
  440. pthread_mutex_lock(&source->filter_mutex);
  441. idx = da_find(source->filters, &filter, 0);
  442. if (idx == DARRAY_INVALID)
  443. return;
  444. if (idx > 0) {
  445. obs_source_t prev = source->filters.array[idx-1];
  446. prev->filter_target = filter->filter_target;
  447. }
  448. da_erase(source->filters, idx);
  449. pthread_mutex_unlock(&source->filter_mutex);
  450. filter->filter_parent = NULL;
  451. filter->filter_target = NULL;
  452. }
  453. void obs_source_filter_setorder(obs_source_t source, obs_source_t filter,
  454. enum order_movement movement)
  455. {
  456. size_t idx = da_find(source->filters, &filter, 0);
  457. size_t i;
  458. if (idx == DARRAY_INVALID)
  459. return;
  460. if (movement == ORDER_MOVE_UP) {
  461. if (idx == source->filters.num-1)
  462. return;
  463. da_move_item(source->filters, idx, idx+1);
  464. } else if (movement == ORDER_MOVE_DOWN) {
  465. if (idx == 0)
  466. return;
  467. da_move_item(source->filters, idx, idx-1);
  468. } else if (movement == ORDER_MOVE_TOP) {
  469. if (idx == source->filters.num-1)
  470. return;
  471. da_move_item(source->filters, idx, source->filters.num-1);
  472. } else if (movement == ORDER_MOVE_BOTTOM) {
  473. if (idx == 0)
  474. return;
  475. da_move_item(source->filters, idx, 0);
  476. }
  477. /* reorder filter targets */
  478. for (i = 0; i < source->filters.num; i++) {
  479. obs_source_t next_filter = (i == source->filters.num-1) ?
  480. source : source->filters.array[idx+1];
  481. source->filters.array[i]->filter_target = next_filter;
  482. }
  483. }
  484. const char *obs_source_get_settings(obs_source_t source)
  485. {
  486. return source->settings.array;
  487. }
  488. void obs_source_save_settings(obs_source_t source, const char *settings)
  489. {
  490. dstr_copy(&source->settings, settings);
  491. }
  492. static inline struct source_frame *filter_async_video(obs_source_t source,
  493. struct source_frame *in)
  494. {
  495. size_t i;
  496. for (i = source->filters.num; i > 0; i--) {
  497. struct obs_source *filter = source->filters.array[i-1];
  498. if (filter->callbacks.filter_video) {
  499. in = filter->callbacks.filter_video(filter->data, in);
  500. if (!in)
  501. return NULL;
  502. }
  503. }
  504. return in;
  505. }
  506. static inline struct source_frame *cache_video(obs_source_t source,
  507. const struct source_frame *frame)
  508. {
  509. /* TODO: use an actual cache */
  510. struct source_frame *new_frame = bmalloc(sizeof(struct source_frame));
  511. memcpy(new_frame, frame, sizeof(struct source_frame));
  512. new_frame->data = bmalloc(frame->row_bytes * frame->height);
  513. return new_frame;
  514. }
  515. void obs_source_output_video(obs_source_t source,
  516. const struct source_frame *frame)
  517. {
  518. struct source_frame *output = cache_video(source, frame);
  519. pthread_mutex_lock(&source->filter_mutex);
  520. output = filter_async_video(source, output);
  521. pthread_mutex_unlock(&source->filter_mutex);
  522. if (output) {
  523. pthread_mutex_lock(&source->video_mutex);
  524. da_push_back(source->video_frames, &output);
  525. pthread_mutex_unlock(&source->video_mutex);
  526. }
  527. }
  528. static inline struct filtered_audio *filter_async_audio(obs_source_t source,
  529. struct filtered_audio *in)
  530. {
  531. size_t i;
  532. for (i = source->filters.num; i > 0; i--) {
  533. struct obs_source *filter = source->filters.array[i-1];
  534. if (filter->callbacks.filter_audio) {
  535. in = filter->callbacks.filter_audio(filter->data, in);
  536. if (!in)
  537. return NULL;
  538. }
  539. }
  540. return in;
  541. }
  542. static inline void reset_resampler(obs_source_t source,
  543. const struct source_audio *audio)
  544. {
  545. const struct audio_info *obs_info;
  546. struct resample_info output_info;
  547. obs_info = audio_output_getinfo(obs->audio.audio);
  548. output_info.format = obs_info->format;
  549. output_info.samples_per_sec = obs_info->samples_per_sec;
  550. output_info.speakers = obs_info->speakers;
  551. source->sample_info.format = audio->format;
  552. source->sample_info.samples_per_sec = audio->samples_per_sec;
  553. source->sample_info.speakers = audio->speakers;
  554. if (source->sample_info.samples_per_sec == obs_info->samples_per_sec &&
  555. source->sample_info.format == obs_info->format &&
  556. source->sample_info.speakers == obs_info->speakers) {
  557. source->audio_failed = false;
  558. return;
  559. }
  560. audio_resampler_destroy(source->resampler);
  561. source->resampler = audio_resampler_create(&output_info,
  562. &source->sample_info);
  563. source->audio_failed = source->resampler == NULL;
  564. if (source->resampler == NULL)
  565. blog(LOG_ERROR, "creation of resampler failed");
  566. }
  567. static inline void copy_audio_data(obs_source_t source,
  568. const void *data, uint32_t frames, uint64_t timestamp)
  569. {
  570. size_t blocksize = audio_output_blocksize(obs->audio.audio);
  571. size_t size = (size_t)frames * blocksize;
  572. /* ensure audio storage capacity */
  573. if (source->audio_storage_size < size) {
  574. bfree(source->audio_data.data);
  575. source->audio_data.data = bmalloc(size);
  576. source->audio_storage_size = size;
  577. }
  578. source->audio_data.frames = frames;
  579. source->audio_data.timestamp = timestamp;
  580. memcpy(source->audio_data.data, data, size);
  581. }
  582. /* resamples/remixes new audio to the designated main audio output format */
  583. static void process_audio(obs_source_t source, const struct source_audio *audio)
  584. {
  585. if (source->sample_info.samples_per_sec != audio->samples_per_sec ||
  586. source->sample_info.format != audio->format ||
  587. source->sample_info.speakers != audio->speakers)
  588. reset_resampler(source, audio);
  589. if (source->audio_failed)
  590. return;
  591. if (source->resampler) {
  592. void *output;
  593. uint32_t frames;
  594. uint64_t offset;
  595. audio_resampler_resample(source->resampler, &output, &frames,
  596. audio->data, audio->frames, &offset);
  597. copy_audio_data(source, output, frames,
  598. audio->timestamp - offset);
  599. } else {
  600. copy_audio_data(source, audio->data, audio->frames,
  601. audio->timestamp);
  602. }
  603. }
  604. void obs_source_output_audio(obs_source_t source,
  605. const struct source_audio *audio)
  606. {
  607. uint32_t flags = obs_source_get_output_flags(source);
  608. size_t blocksize = audio_output_blocksize(obs->audio.audio);
  609. struct filtered_audio *output;
  610. process_audio(source, audio);
  611. pthread_mutex_lock(&source->filter_mutex);
  612. output = filter_async_audio(source, &source->audio_data);
  613. if (output) {
  614. pthread_mutex_lock(&source->audio_mutex);
  615. if (!source->timing_set && flags & SOURCE_ASYNC_VIDEO) {
  616. struct audiobuf newbuf;
  617. size_t audio_size = blocksize * output->frames;
  618. newbuf.data = bmalloc(audio_size);
  619. newbuf.frames = output->frames;
  620. newbuf.timestamp = output->timestamp;
  621. memcpy(newbuf.data, output->data, audio_size);
  622. da_push_back(source->audio_wait_buffer, &newbuf);
  623. } else {
  624. struct audio_data data;
  625. data.data = output->data;
  626. data.frames = output->frames;
  627. data.timestamp = output->timestamp;
  628. source_output_audio_line(source, &data);
  629. }
  630. pthread_mutex_unlock(&source->audio_mutex);
  631. }
  632. pthread_mutex_unlock(&source->filter_mutex);
  633. }
  634. /*
  635. * Ensures that cached frames are displayed on time. If multiple frames
  636. * were cached between renders, then releases the unnecessary frames and uses
  637. * the frame with the closest timing.
  638. */
  639. struct source_frame *obs_source_getframe(obs_source_t source)
  640. {
  641. uint64_t last_frame_time = source->last_frame_timestamp;
  642. struct source_frame *frame = NULL;
  643. struct source_frame *next_frame;
  644. uint64_t sys_time, frame_time;
  645. pthread_mutex_lock(&source->video_mutex);
  646. if (!source->video_frames.num)
  647. goto unlock;
  648. next_frame = source->video_frames.array[0];
  649. sys_time = os_gettime_ns();
  650. frame_time = next_frame->timestamp;
  651. if (!source->last_frame_timestamp) {
  652. frame = next_frame;
  653. da_erase(source->video_frames, 0);
  654. source->last_frame_timestamp = frame_time;
  655. } else {
  656. uint64_t sys_offset, frame_offset;
  657. sys_offset = sys_time - source->last_sys_timestamp;
  658. frame_offset = frame_time - last_frame_time;
  659. source->last_frame_timestamp += sys_offset;
  660. while (frame_offset <= sys_offset) {
  661. if (frame)
  662. source_frame_destroy(frame);
  663. frame = next_frame;
  664. da_erase(source->video_frames, 0);
  665. if (!source->video_frames.num)
  666. break;
  667. next_frame = source->video_frames.array[0];
  668. frame_time = next_frame->timestamp;
  669. frame_offset = frame_time - last_frame_time;
  670. }
  671. }
  672. source->last_sys_timestamp = sys_time;
  673. unlock:
  674. pthread_mutex_unlock(&source->video_mutex);
  675. return frame;
  676. }
  677. void obs_source_releaseframe(obs_source_t source, struct source_frame *frame)
  678. {
  679. source_frame_destroy(frame);
  680. }