obs-source.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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 "callback/calldata.h"
  17. #include "graphics/matrix3.h"
  18. #include "graphics/vec3.h"
  19. #include "obs.h"
  20. #include "obs-internal.h"
  21. static void obs_source_destroy(obs_source_t source);
  22. bool load_source_info(void *module, const char *module_name,
  23. const char *id, struct source_info *info)
  24. {
  25. LOAD_MODULE_SUBFUNC(getname, true);
  26. LOAD_MODULE_SUBFUNC(create, true);
  27. LOAD_MODULE_SUBFUNC(destroy, true);
  28. LOAD_MODULE_SUBFUNC(get_output_flags, true);
  29. LOAD_MODULE_SUBFUNC(update, false);
  30. LOAD_MODULE_SUBFUNC(activate, false);
  31. LOAD_MODULE_SUBFUNC(deactivate, false);
  32. LOAD_MODULE_SUBFUNC(video_tick, false);
  33. LOAD_MODULE_SUBFUNC(video_render, false);
  34. LOAD_MODULE_SUBFUNC(getwidth, false);
  35. LOAD_MODULE_SUBFUNC(getheight, false);
  36. LOAD_MODULE_SUBFUNC(getparam, false);
  37. LOAD_MODULE_SUBFUNC(setparam, false);
  38. LOAD_MODULE_SUBFUNC(filter_video, false);
  39. LOAD_MODULE_SUBFUNC(filter_audio, false);
  40. info->id = id;
  41. return true;
  42. }
  43. static inline const struct source_info *find_source(struct darray *list,
  44. const char *id)
  45. {
  46. size_t i;
  47. struct source_info *array = list->array;
  48. for (i = 0; i < list->num; i++) {
  49. struct source_info *info = array+i;
  50. if (strcmp(info->id, id) == 0)
  51. return info;
  52. }
  53. return NULL;
  54. }
  55. static const struct source_info *get_source_info(enum obs_source_type type,
  56. const char *id)
  57. {
  58. struct darray *list = NULL;
  59. switch (type) {
  60. case SOURCE_INPUT: list = &obs->input_types.da; break;
  61. case SOURCE_FILTER: list = &obs->filter_types.da; break;
  62. case SOURCE_TRANSITION: list = &obs->transition_types.da; break;
  63. case SOURCE_SCENE:
  64. default:
  65. blog(LOG_WARNING, "get_source_info: invalid source type");
  66. return NULL;
  67. }
  68. return find_source(list, id);
  69. }
  70. bool obs_source_init_handlers(struct obs_source *source)
  71. {
  72. source->signals = signal_handler_create();
  73. if (!source->signals)
  74. return false;
  75. source->procs = proc_handler_create();
  76. return (source->procs != NULL);
  77. }
  78. const char *obs_source_getdisplayname(enum obs_source_type type,
  79. const char *id, const char *locale)
  80. {
  81. const struct source_info *info = get_source_info(type, id);
  82. return (info != NULL) ? info->getname(locale) : NULL;
  83. }
  84. /* internal initialization */
  85. bool obs_source_init(struct obs_source *source, const char *settings,
  86. const struct source_info *info)
  87. {
  88. uint32_t flags = info->get_output_flags(source->data);
  89. source->refs = 1;
  90. source->volume = 1.0f;
  91. pthread_mutex_init_value(&source->filter_mutex);
  92. pthread_mutex_init_value(&source->video_mutex);
  93. pthread_mutex_init_value(&source->audio_mutex);
  94. dstr_copy(&source->settings, settings);
  95. memcpy(&source->callbacks, info, sizeof(struct source_info));
  96. if (pthread_mutex_init(&source->filter_mutex, NULL) != 0)
  97. return false;
  98. if (pthread_mutex_init(&source->audio_mutex, NULL) != 0)
  99. return false;
  100. if (pthread_mutex_init(&source->video_mutex, NULL) != 0)
  101. return false;
  102. if (flags & SOURCE_AUDIO) {
  103. source->audio_line = audio_output_createline(obs->audio.audio,
  104. source->name);
  105. if (!source->audio_line) {
  106. blog(LOG_ERROR, "Failed to create audio line for "
  107. "source '%s'", source->name);
  108. return false;
  109. }
  110. }
  111. return true;
  112. }
  113. static inline void obs_source_dosignal(struct obs_source *source,
  114. const char *signal)
  115. {
  116. struct calldata data;
  117. calldata_init(&data);
  118. calldata_setptr(&data, "source", source);
  119. signal_handler_signal(obs->signals, signal, &data);
  120. calldata_free(&data);
  121. }
  122. obs_source_t obs_source_create(enum obs_source_type type, const char *id,
  123. const char *name, const char *settings)
  124. {
  125. struct obs_source *source;
  126. const struct source_info *info = get_source_info(type, id);
  127. if (!info) {
  128. blog(LOG_WARNING, "Source '%s' not found", id);
  129. return NULL;
  130. }
  131. source = bmalloc(sizeof(struct obs_source));
  132. memset(source, 0, sizeof(struct obs_source));
  133. if (!obs_source_init_handlers(source))
  134. goto fail;
  135. source->name = bstrdup(name);
  136. source->type = type;
  137. source->data = info->create(settings, source);
  138. if (!source->data)
  139. goto fail;
  140. if (!obs_source_init(source, settings, info))
  141. goto fail;
  142. obs_source_dosignal(source, "source-create");
  143. return source;
  144. fail:
  145. blog(LOG_ERROR, "obs_source_create failed");
  146. obs_source_destroy(source);
  147. return NULL;
  148. }
  149. static void obs_source_destroy(obs_source_t source)
  150. {
  151. size_t i;
  152. obs_source_dosignal(source, "source-destroy");
  153. if (source->filter_parent)
  154. obs_source_filter_remove(source->filter_parent, source);
  155. for (i = 0; i < source->filters.num; i++)
  156. obs_source_release(source->filters.array[i]);
  157. for (i = 0; i < source->video_frames.num; i++)
  158. source_frame_destroy(source->video_frames.array[i]);
  159. gs_entercontext(obs->video.graphics);
  160. texture_destroy(source->output_texture);
  161. gs_leavecontext();
  162. if (source->data)
  163. source->callbacks.destroy(source->data);
  164. bfree(source->audio_data.data);
  165. audio_line_destroy(source->audio_line);
  166. audio_resampler_destroy(source->resampler);
  167. proc_handler_destroy(source->procs);
  168. signal_handler_destroy(source->signals);
  169. da_free(source->video_frames);
  170. da_free(source->filters);
  171. pthread_mutex_destroy(&source->filter_mutex);
  172. pthread_mutex_destroy(&source->audio_mutex);
  173. pthread_mutex_destroy(&source->video_mutex);
  174. dstr_free(&source->settings);
  175. bfree(source->name);
  176. bfree(source);
  177. }
  178. int obs_source_addref(obs_source_t source)
  179. {
  180. assert(source != NULL);
  181. if (!source)
  182. return 0;
  183. return ++source->refs;
  184. }
  185. int obs_source_release(obs_source_t source)
  186. {
  187. int refs;
  188. assert(source != NULL);
  189. if (!source)
  190. return 0;
  191. refs = --source->refs;
  192. if (refs == 0)
  193. obs_source_destroy(source);
  194. return refs;
  195. }
  196. void obs_source_remove(obs_source_t source)
  197. {
  198. struct obs_data *data = &obs->data;
  199. size_t id;
  200. source->removed = true;
  201. pthread_mutex_lock(&data->sources_mutex);
  202. id = da_find(data->sources, &source, 0);
  203. if (id != DARRAY_INVALID) {
  204. da_erase_item(data->sources, &source);
  205. obs_source_release(source);
  206. }
  207. pthread_mutex_unlock(&data->sources_mutex);
  208. }
  209. bool obs_source_removed(obs_source_t source)
  210. {
  211. return source->removed;
  212. }
  213. uint32_t obs_source_get_output_flags(obs_source_t source)
  214. {
  215. return source->callbacks.get_output_flags(source->data);
  216. }
  217. void obs_source_update(obs_source_t source, const char *settings)
  218. {
  219. if (source->callbacks.update)
  220. source->callbacks.update(source->data, settings);
  221. }
  222. void obs_source_activate(obs_source_t source)
  223. {
  224. if (source->callbacks.activate)
  225. source->callbacks.activate(source->data);
  226. }
  227. void obs_source_deactivate(obs_source_t source)
  228. {
  229. if (source->callbacks.deactivate)
  230. source->callbacks.deactivate(source->data);
  231. }
  232. void obs_source_video_tick(obs_source_t source, float seconds)
  233. {
  234. if (source->callbacks.video_tick)
  235. source->callbacks.video_tick(source->data, seconds);
  236. }
  237. static inline uint64_t conv_frames_to_time(obs_source_t source, size_t frames)
  238. {
  239. const struct audio_output_info *info;
  240. double sps_to_ns;
  241. info = audio_output_getinfo(obs->audio.audio);
  242. sps_to_ns = 1000000000.0 / (double)info->samples_per_sec;
  243. return (uint64_t)((double)frames * sps_to_ns);
  244. }
  245. /* maximum "direct" timestamp variance in nanoseconds */
  246. #define MAX_TS_VAR 5000000000ULL
  247. /* maximum time that timestamp can jump in nanoseconds */
  248. #define MAX_TIMESTAMP_JUMP 2000000000ULL
  249. static inline void reset_audio_timing(obs_source_t source, uint64_t timetamp)
  250. {
  251. source->timing_set = true;
  252. source->timing_adjust = os_gettime_ns() - timetamp;
  253. }
  254. static inline void handle_ts_jump(obs_source_t source, uint64_t ts,
  255. uint64_t diff)
  256. {
  257. uint32_t flags = source->callbacks.get_output_flags(source->data);
  258. blog(LOG_DEBUG, "Timestamp for source '%s' jumped by '%lld', "
  259. "resetting audio timing", source->name, diff);
  260. /* if has video, ignore audio data until reset */
  261. if (flags & SOURCE_ASYNC_VIDEO)
  262. source->audio_reset_ref--;
  263. else
  264. reset_audio_timing(source, ts);
  265. }
  266. static void source_output_audio_line(obs_source_t source,
  267. const struct audio_data *data)
  268. {
  269. struct audio_data in = *data;
  270. uint64_t diff;
  271. if (!source->timing_set) {
  272. reset_audio_timing(source, in.timestamp);
  273. /* detects 'directly' set timestamps as long as they're within
  274. * a certain threshold */
  275. if ((source->timing_adjust + MAX_TS_VAR) < MAX_TS_VAR * 2)
  276. source->timing_adjust = 0;
  277. } else {
  278. diff = in.timestamp - source->next_audio_ts_min;
  279. /* don't need signed because negative will trigger it
  280. * regardless, which is what we want */
  281. if (diff > MAX_TIMESTAMP_JUMP)
  282. handle_ts_jump(source, in.timestamp, diff);
  283. }
  284. source->next_audio_ts_min = in.timestamp +
  285. conv_frames_to_time(source, in.frames);
  286. if (source->audio_reset_ref != 0)
  287. return;
  288. in.timestamp += source->timing_adjust;
  289. in.volume = source->volume;
  290. audio_line_output(source->audio_line, &in);
  291. }
  292. static bool set_texture_size(obs_source_t source, struct source_frame *frame)
  293. {
  294. if (source->output_texture) {
  295. uint32_t width = texture_getwidth(source->output_texture);
  296. uint32_t height = texture_getheight(source->output_texture);
  297. if (width == frame->width && height == frame->height)
  298. return true;
  299. }
  300. texture_destroy(source->output_texture);
  301. source->output_texture = gs_create_texture(frame->width, frame->height,
  302. GS_RGBA, 1, NULL, GS_DYNAMIC);
  303. return source->output_texture != NULL;
  304. }
  305. enum convert_type {
  306. CONVERT_NONE,
  307. CONVERT_NV12,
  308. CONVERT_420,
  309. CONVERT_422_U,
  310. CONVERT_422_Y,
  311. };
  312. static inline enum convert_type get_convert_type(enum video_format format)
  313. {
  314. switch (format) {
  315. case VIDEO_FORMAT_I420:
  316. return CONVERT_420;
  317. case VIDEO_FORMAT_NV12:
  318. return CONVERT_NV12;
  319. case VIDEO_FORMAT_YVYU:
  320. case VIDEO_FORMAT_YUY2:
  321. return CONVERT_422_Y;
  322. case VIDEO_FORMAT_UYVY:
  323. return CONVERT_422_U;
  324. case VIDEO_FORMAT_NONE:
  325. case VIDEO_FORMAT_YUVX:
  326. case VIDEO_FORMAT_UYVX:
  327. case VIDEO_FORMAT_RGBA:
  328. case VIDEO_FORMAT_BGRA:
  329. case VIDEO_FORMAT_BGRX:
  330. return CONVERT_NONE;
  331. }
  332. return CONVERT_NONE;
  333. }
  334. static inline bool is_yuv(enum video_format format)
  335. {
  336. switch (format) {
  337. case VIDEO_FORMAT_I420:
  338. case VIDEO_FORMAT_NV12:
  339. case VIDEO_FORMAT_YVYU:
  340. case VIDEO_FORMAT_YUY2:
  341. case VIDEO_FORMAT_UYVY:
  342. case VIDEO_FORMAT_YUVX:
  343. case VIDEO_FORMAT_UYVX:
  344. return true;
  345. case VIDEO_FORMAT_NONE:
  346. case VIDEO_FORMAT_RGBA:
  347. case VIDEO_FORMAT_BGRA:
  348. case VIDEO_FORMAT_BGRX:
  349. return false;
  350. }
  351. return false;
  352. }
  353. static bool upload_frame(texture_t tex, const struct source_frame *frame)
  354. {
  355. void *ptr;
  356. uint32_t row_bytes;
  357. enum convert_type type = get_convert_type(frame->format);
  358. if (type == CONVERT_NONE) {
  359. texture_setimage(tex, frame->data, frame->row_bytes, false);
  360. return true;
  361. }
  362. if (!texture_map(tex, &ptr, &row_bytes))
  363. return false;
  364. if (type == CONVERT_420)
  365. decompress_420(frame->data, frame->width, frame->height,
  366. frame->row_bytes, 0, frame->height, ptr);
  367. else if (type == CONVERT_NV12)
  368. decompress_nv12(frame->data, frame->width, frame->height,
  369. frame->row_bytes, 0, frame->height, ptr);
  370. else if (type == CONVERT_422_Y)
  371. decompress_422(frame->data, frame->width, frame->height,
  372. frame->row_bytes, 0, frame->height, ptr, true);
  373. else if (type == CONVERT_422_U)
  374. decompress_422(frame->data, frame->width, frame->height,
  375. frame->row_bytes, 0, frame->height, ptr, false);
  376. texture_unmap(tex);
  377. return true;
  378. }
  379. static void obs_source_draw_texture(texture_t tex, struct source_frame *frame)
  380. {
  381. effect_t effect = obs->video.default_effect;
  382. bool yuv = is_yuv(frame->format);
  383. const char *type = yuv ? "DrawYUV" : "DrawRGB";
  384. technique_t tech;
  385. eparam_t param;
  386. if (!upload_frame(tex, frame))
  387. return;
  388. tech = effect_gettechnique(effect, type);
  389. technique_begin(tech);
  390. technique_beginpass(tech, 0);
  391. if (yuv) {
  392. param = effect_getparambyname(effect, "yuv_matrix");
  393. effect_setval(effect, param, frame->yuv_matrix,
  394. sizeof(float) * 16);
  395. }
  396. param = effect_getparambyname(effect, "diffuse");
  397. effect_settexture(effect, param, tex);
  398. gs_draw_sprite(tex, frame->flip ? GS_FLIP_V : 0, 0, 0);
  399. technique_endpass(tech);
  400. technique_end(tech);
  401. }
  402. static void obs_source_render_async_video(obs_source_t source)
  403. {
  404. struct source_frame *frame = obs_source_getframe(source);
  405. if (!frame)
  406. return;
  407. if (set_texture_size(source, frame))
  408. obs_source_draw_texture(source->output_texture, frame);
  409. obs_source_releaseframe(source, frame);
  410. }
  411. static inline void obs_source_render_filters(obs_source_t source)
  412. {
  413. source->rendering_filter = true;
  414. obs_source_video_render(source->filters.array[0]);
  415. source->rendering_filter = false;
  416. }
  417. static inline void obs_source_default_render(obs_source_t source, bool yuv)
  418. {
  419. effect_t effect = obs->video.default_effect;
  420. const char *tech_name = yuv ? "DrawYUV" : "DrawRGB";
  421. technique_t tech = effect_gettechnique(effect, tech_name);
  422. size_t passes, i;
  423. passes = technique_begin(tech);
  424. for (i = 0; i < passes; i++) {
  425. technique_beginpass(tech, i);
  426. source->callbacks.video_render(source->data);
  427. technique_endpass(tech);
  428. }
  429. technique_end(tech);
  430. }
  431. static inline void obs_source_main_render(obs_source_t source)
  432. {
  433. uint32_t flags = source->callbacks.get_output_flags(source->data);
  434. bool default_effect = !source->filter_parent &&
  435. source->filters.num == 0 &&
  436. (flags & SOURCE_DEFAULT_EFFECT) != 0;
  437. if (default_effect)
  438. obs_source_default_render(source, (flags & SOURCE_YUV) != 0);
  439. else
  440. source->callbacks.video_render(source->data);
  441. }
  442. void obs_source_video_render(obs_source_t source)
  443. {
  444. if (source->callbacks.video_render) {
  445. if (source->filters.num && !source->rendering_filter)
  446. obs_source_render_filters(source);
  447. else
  448. obs_source_main_render(source);
  449. } else if (source->filter_target) {
  450. obs_source_video_render(source->filter_target);
  451. } else {
  452. obs_source_render_async_video(source);
  453. }
  454. }
  455. uint32_t obs_source_getwidth(obs_source_t source)
  456. {
  457. if (source->callbacks.getwidth)
  458. return source->callbacks.getwidth(source->data);
  459. return 0;
  460. }
  461. uint32_t obs_source_getheight(obs_source_t source)
  462. {
  463. if (source->callbacks.getheight)
  464. return source->callbacks.getheight(source->data);
  465. return 0;
  466. }
  467. size_t obs_source_getparam(obs_source_t source, const char *param, void *buf,
  468. size_t buf_size)
  469. {
  470. if (source->callbacks.getparam)
  471. return source->callbacks.getparam(source->data, param, buf,
  472. buf_size);
  473. return 0;
  474. }
  475. void obs_source_setparam(obs_source_t source, const char *param,
  476. const void *data, size_t size)
  477. {
  478. if (source->callbacks.setparam)
  479. source->callbacks.setparam(source->data, param, data, size);
  480. }
  481. obs_source_t obs_filter_getparent(obs_source_t filter)
  482. {
  483. return filter->filter_parent;
  484. }
  485. obs_source_t obs_filter_gettarget(obs_source_t filter)
  486. {
  487. return filter->filter_target;
  488. }
  489. void obs_source_filter_add(obs_source_t source, obs_source_t filter)
  490. {
  491. pthread_mutex_lock(&source->filter_mutex);
  492. if (da_find(source->filters, &filter, 0) != DARRAY_INVALID) {
  493. blog(LOG_WARNING, "Tried to add a filter that was already "
  494. "present on the source");
  495. return;
  496. }
  497. if (source->filters.num) {
  498. obs_source_t *back = da_end(source->filters);
  499. (*back)->filter_target = filter;
  500. }
  501. da_push_back(source->filters, &filter);
  502. pthread_mutex_unlock(&source->filter_mutex);
  503. filter->filter_parent = source;
  504. filter->filter_target = source;
  505. }
  506. void obs_source_filter_remove(obs_source_t source, obs_source_t filter)
  507. {
  508. size_t idx;
  509. pthread_mutex_lock(&source->filter_mutex);
  510. idx = da_find(source->filters, &filter, 0);
  511. if (idx == DARRAY_INVALID)
  512. return;
  513. if (idx > 0) {
  514. obs_source_t prev = source->filters.array[idx-1];
  515. prev->filter_target = filter->filter_target;
  516. }
  517. da_erase(source->filters, idx);
  518. pthread_mutex_unlock(&source->filter_mutex);
  519. filter->filter_parent = NULL;
  520. filter->filter_target = NULL;
  521. }
  522. void obs_source_filter_setorder(obs_source_t source, obs_source_t filter,
  523. enum order_movement movement)
  524. {
  525. size_t idx = da_find(source->filters, &filter, 0);
  526. size_t i;
  527. if (idx == DARRAY_INVALID)
  528. return;
  529. if (movement == ORDER_MOVE_UP) {
  530. if (idx == source->filters.num-1)
  531. return;
  532. da_move_item(source->filters, idx, idx+1);
  533. } else if (movement == ORDER_MOVE_DOWN) {
  534. if (idx == 0)
  535. return;
  536. da_move_item(source->filters, idx, idx-1);
  537. } else if (movement == ORDER_MOVE_TOP) {
  538. if (idx == source->filters.num-1)
  539. return;
  540. da_move_item(source->filters, idx, source->filters.num-1);
  541. } else if (movement == ORDER_MOVE_BOTTOM) {
  542. if (idx == 0)
  543. return;
  544. da_move_item(source->filters, idx, 0);
  545. }
  546. /* reorder filter targets, not the nicest way of dealing with things */
  547. for (i = 0; i < source->filters.num; i++) {
  548. obs_source_t next_filter = (i == source->filters.num-1) ?
  549. source : source->filters.array[idx+1];
  550. source->filters.array[i]->filter_target = next_filter;
  551. }
  552. }
  553. const char *obs_source_getsettings(obs_source_t source)
  554. {
  555. return source->settings.array;
  556. }
  557. void obs_source_savesettings(obs_source_t source, const char *settings)
  558. {
  559. dstr_copy(&source->settings, settings);
  560. }
  561. static inline struct source_frame *filter_async_video(obs_source_t source,
  562. struct source_frame *in)
  563. {
  564. size_t i;
  565. for (i = source->filters.num; i > 0; i--) {
  566. struct obs_source *filter = source->filters.array[i-1];
  567. if (filter->callbacks.filter_video) {
  568. in = filter->callbacks.filter_video(filter->data, in);
  569. if (!in)
  570. return NULL;
  571. }
  572. }
  573. return in;
  574. }
  575. static inline struct source_frame *cache_video(obs_source_t source,
  576. const struct source_frame *frame)
  577. {
  578. /* TODO: use an actual cache */
  579. struct source_frame *new_frame = bmalloc(sizeof(struct source_frame));
  580. memcpy(new_frame, frame, sizeof(struct source_frame));
  581. new_frame->data = bmalloc(frame->row_bytes * frame->height);
  582. return new_frame;
  583. }
  584. void obs_source_output_video(obs_source_t source,
  585. const struct source_frame *frame)
  586. {
  587. struct source_frame *output = cache_video(source, frame);
  588. pthread_mutex_lock(&source->filter_mutex);
  589. output = filter_async_video(source, output);
  590. pthread_mutex_unlock(&source->filter_mutex);
  591. if (output) {
  592. pthread_mutex_lock(&source->video_mutex);
  593. da_push_back(source->video_frames, &output);
  594. pthread_mutex_unlock(&source->video_mutex);
  595. }
  596. }
  597. static inline struct filtered_audio *filter_async_audio(obs_source_t source,
  598. struct filtered_audio *in)
  599. {
  600. size_t i;
  601. for (i = source->filters.num; i > 0; i--) {
  602. struct obs_source *filter = source->filters.array[i-1];
  603. if (filter->callbacks.filter_audio) {
  604. in = filter->callbacks.filter_audio(filter->data, in);
  605. if (!in)
  606. return NULL;
  607. }
  608. }
  609. return in;
  610. }
  611. static inline void reset_resampler(obs_source_t source,
  612. const struct source_audio *audio)
  613. {
  614. const struct audio_output_info *obs_info;
  615. struct resample_info output_info;
  616. obs_info = audio_output_getinfo(obs->audio.audio);
  617. output_info.format = obs_info->format;
  618. output_info.samples_per_sec = obs_info->samples_per_sec;
  619. output_info.speakers = obs_info->speakers;
  620. source->sample_info.format = audio->format;
  621. source->sample_info.samples_per_sec = audio->samples_per_sec;
  622. source->sample_info.speakers = audio->speakers;
  623. if (source->sample_info.samples_per_sec == obs_info->samples_per_sec &&
  624. source->sample_info.format == obs_info->format &&
  625. source->sample_info.speakers == obs_info->speakers) {
  626. source->audio_failed = false;
  627. return;
  628. }
  629. audio_resampler_destroy(source->resampler);
  630. source->resampler = audio_resampler_create(&output_info,
  631. &source->sample_info);
  632. source->audio_failed = source->resampler == NULL;
  633. if (source->resampler == NULL)
  634. blog(LOG_ERROR, "creation of resampler failed");
  635. }
  636. static inline void copy_audio_data(obs_source_t source,
  637. const void *data, uint32_t frames, uint64_t timestamp)
  638. {
  639. size_t blocksize = audio_output_blocksize(obs->audio.audio);
  640. size_t size = (size_t)frames * blocksize;
  641. /* ensure audio storage capacity */
  642. if (source->audio_storage_size < size) {
  643. bfree(source->audio_data.data);
  644. source->audio_data.data = bmalloc(size);
  645. source->audio_storage_size = size;
  646. }
  647. source->audio_data.frames = frames;
  648. source->audio_data.timestamp = timestamp;
  649. memcpy(source->audio_data.data, data, size);
  650. }
  651. /* resamples/remixes new audio to the designated main audio output format */
  652. static void process_audio(obs_source_t source, const struct source_audio *audio)
  653. {
  654. if (source->sample_info.samples_per_sec != audio->samples_per_sec ||
  655. source->sample_info.format != audio->format ||
  656. source->sample_info.speakers != audio->speakers)
  657. reset_resampler(source, audio);
  658. if (source->audio_failed)
  659. return;
  660. if (source->resampler) {
  661. void *output;
  662. uint32_t frames;
  663. uint64_t offset;
  664. audio_resampler_resample(source->resampler, &output, &frames,
  665. audio->data, audio->frames, &offset);
  666. copy_audio_data(source, output, frames,
  667. audio->timestamp - offset);
  668. } else {
  669. copy_audio_data(source, audio->data, audio->frames,
  670. audio->timestamp);
  671. }
  672. }
  673. void obs_source_output_audio(obs_source_t source,
  674. const struct source_audio *audio)
  675. {
  676. uint32_t flags = obs_source_get_output_flags(source);
  677. size_t blocksize = audio_output_blocksize(obs->audio.audio);
  678. struct filtered_audio *output;
  679. process_audio(source, audio);
  680. pthread_mutex_lock(&source->filter_mutex);
  681. output = filter_async_audio(source, &source->audio_data);
  682. if (output) {
  683. pthread_mutex_lock(&source->audio_mutex);
  684. /* wait for video to start before outputting any audio so we
  685. * have a base for sync */
  686. if (source->timing_set || (flags & SOURCE_ASYNC_VIDEO) == 0) {
  687. struct audio_data data;
  688. data.data = output->data;
  689. data.frames = output->frames;
  690. data.timestamp = output->timestamp;
  691. source_output_audio_line(source, &data);
  692. }
  693. pthread_mutex_unlock(&source->audio_mutex);
  694. }
  695. pthread_mutex_unlock(&source->filter_mutex);
  696. }
  697. static inline bool frame_out_of_bounds(obs_source_t source, uint64_t ts)
  698. {
  699. return ((ts - source->last_frame_ts) > MAX_TIMESTAMP_JUMP);
  700. }
  701. static inline struct source_frame *get_closest_frame(obs_source_t source,
  702. uint64_t sys_time, int *audio_time_refs)
  703. {
  704. struct source_frame *next_frame = source->video_frames.array[0];
  705. struct source_frame *frame = NULL;
  706. uint64_t sys_offset = sys_time - source->last_sys_timestamp;
  707. uint64_t frame_time = next_frame->timestamp;
  708. uint64_t frame_offset = 0;
  709. /* account for timestamp invalidation */
  710. if (frame_out_of_bounds(source, frame_time)) {
  711. source->last_frame_ts = next_frame->timestamp;
  712. (*audio_time_refs)++;
  713. } else {
  714. frame_offset = frame_time - source->last_frame_ts;
  715. source->last_frame_ts += sys_offset;
  716. }
  717. while (frame_offset <= sys_offset) {
  718. source_frame_destroy(frame);
  719. frame = next_frame;
  720. da_erase(source->video_frames, 0);
  721. if (!source->video_frames.num)
  722. break;
  723. next_frame = source->video_frames.array[0];
  724. /* more timestamp checking and compensating */
  725. if ((next_frame->timestamp - frame_time) > MAX_TIMESTAMP_JUMP) {
  726. source->last_frame_ts =
  727. next_frame->timestamp - frame_offset;
  728. (*audio_time_refs)++;
  729. }
  730. frame_time = next_frame->timestamp;
  731. frame_offset = frame_time - source->last_frame_ts;
  732. }
  733. return frame;
  734. }
  735. /*
  736. * Ensures that cached frames are displayed on time. If multiple frames
  737. * were cached between renders, then releases the unnecessary frames and uses
  738. * the frame with the closest timing to ensure sync. Also ensures that timing
  739. * with audio is synchronized.
  740. */
  741. struct source_frame *obs_source_getframe(obs_source_t source)
  742. {
  743. struct source_frame *frame = NULL;
  744. uint64_t last_frame_time = source->last_frame_ts;
  745. int audio_time_refs = 0;
  746. uint64_t sys_time;
  747. pthread_mutex_lock(&source->video_mutex);
  748. if (!source->video_frames.num)
  749. goto unlock;
  750. sys_time = os_gettime_ns();
  751. if (!source->last_frame_ts) {
  752. frame = source->video_frames.array[0];
  753. da_erase(source->video_frames, 0);
  754. source->last_frame_ts = frame->timestamp;
  755. } else {
  756. frame = get_closest_frame(source, sys_time, &audio_time_refs);
  757. }
  758. /* reset timing to current system time */
  759. if (frame) {
  760. source->audio_reset_ref += audio_time_refs;
  761. source->timing_adjust = sys_time - frame->timestamp;
  762. source->timing_set = true;
  763. }
  764. source->last_sys_timestamp = sys_time;
  765. unlock:
  766. pthread_mutex_unlock(&source->video_mutex);
  767. if (frame)
  768. obs_source_addref(source);
  769. return frame;
  770. }
  771. void obs_source_releaseframe(obs_source_t source, struct source_frame *frame)
  772. {
  773. if (frame) {
  774. source_frame_destroy(frame);
  775. obs_source_release(source);
  776. }
  777. }
  778. const char *obs_source_getname(obs_source_t source)
  779. {
  780. return source->name;
  781. }
  782. void obs_source_setname(obs_source_t source, const char *name)
  783. {
  784. bfree(source->name);
  785. source->name = bstrdup(name);
  786. }
  787. void obs_source_gettype(obs_source_t source, enum obs_source_type *type,
  788. const char **id)
  789. {
  790. if (type) *type = source->type;
  791. if (id) *id = source->callbacks.id;
  792. }
  793. static inline void render_filter_bypass(obs_source_t target, effect_t effect,
  794. uint32_t width, uint32_t height, bool yuv)
  795. {
  796. const char *tech_name = yuv ? "DrawYUV" : "DrawRGB";
  797. technique_t tech = effect_gettechnique(effect, tech_name);
  798. eparam_t diffuse = effect_getparambyname(effect, "diffuse");
  799. size_t passes, i;
  800. passes = technique_begin(tech);
  801. for (i = 0; i < passes; i++) {
  802. technique_beginpass(tech, i);
  803. obs_source_video_render(target);
  804. technique_endpass(tech);
  805. }
  806. technique_end(tech);
  807. }
  808. static inline void render_filter_tex(texture_t tex, effect_t effect,
  809. uint32_t width, uint32_t height, bool yuv)
  810. {
  811. const char *tech_name = yuv ? "DrawYUV" : "DrawRGB";
  812. technique_t tech = effect_gettechnique(effect, tech_name);
  813. eparam_t diffuse = effect_getparambyname(effect, "diffuse");
  814. size_t passes, i;
  815. effect_settexture(effect, diffuse, tex);
  816. passes = technique_begin(tech);
  817. for (i = 0; i < passes; i++) {
  818. technique_beginpass(tech, i);
  819. gs_draw_sprite(tex, width, height, 0);
  820. technique_endpass(tech);
  821. }
  822. technique_end(tech);
  823. }
  824. void obs_source_process_filter(obs_source_t filter, texrender_t texrender,
  825. effect_t effect, uint32_t width, uint32_t height,
  826. enum allow_direct_render allow_direct)
  827. {
  828. obs_source_t target = obs_filter_gettarget(filter);
  829. obs_source_t parent = obs_filter_getparent(filter);
  830. uint32_t target_flags = obs_source_get_output_flags(target);
  831. uint32_t parent_flags = obs_source_get_output_flags(parent);
  832. int cx = obs_source_getwidth(target);
  833. int cy = obs_source_getheight(target);
  834. bool yuv = (target_flags & SOURCE_YUV) != 0;
  835. bool expects_def = (parent_flags & SOURCE_DEFAULT_EFFECT) != 0;
  836. bool can_directly = allow_direct == ALLOW_DIRECT_RENDERING;
  837. /* if the parent does not use any custom effects, and this is the last
  838. * filter in the chain for the parent, then render the parent directly
  839. * using the filter effect instead of rendering to texture to reduce
  840. * the total number of passes */
  841. if (can_directly && expects_def && target == parent) {
  842. render_filter_bypass(target, effect, width, height, yuv);
  843. return;
  844. }
  845. if (texrender_begin(texrender, cx, cy)) {
  846. gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -100.0f, 100.0f);
  847. if (expects_def && parent == target)
  848. obs_source_default_render(parent, yuv);
  849. else
  850. obs_source_video_render(target);
  851. texrender_end(texrender);
  852. }
  853. /* --------------------------- */
  854. render_filter_tex(texrender_gettexture(texrender), effect,
  855. width, height, yuv);
  856. }
  857. signal_handler_t obs_source_signalhandler(obs_source_t source)
  858. {
  859. return source->signals;
  860. }
  861. proc_handler_t obs_source_prochandler(obs_source_t source)
  862. {
  863. return source->procs;
  864. }
  865. void obs_source_setvolume(obs_source_t source, float volume)
  866. {
  867. source->volume = volume;
  868. }
  869. float obs_source_getvolume(obs_source_t source)
  870. {
  871. return source->volume;
  872. }