obs-source.c 23 KB

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