obs-source.c 22 KB

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