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, obs_data_t 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. source->settings = obs_data_newref(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, obs_data_t 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. obs_data_release(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_program_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, obs_data_t settings)
  218. {
  219. obs_data_replace(&source->settings, settings);
  220. if (source->callbacks.update)
  221. source->callbacks.update(source->data, settings);
  222. }
  223. void obs_source_activate(obs_source_t source)
  224. {
  225. if (source->callbacks.activate)
  226. source->callbacks.activate(source->data);
  227. }
  228. void obs_source_deactivate(obs_source_t source)
  229. {
  230. if (source->callbacks.deactivate)
  231. source->callbacks.deactivate(source->data);
  232. }
  233. void obs_source_video_tick(obs_source_t source, float seconds)
  234. {
  235. if (source->callbacks.video_tick)
  236. source->callbacks.video_tick(source->data, seconds);
  237. }
  238. static inline uint64_t conv_frames_to_time(obs_source_t source, size_t frames)
  239. {
  240. const struct audio_output_info *info;
  241. double sps_to_ns;
  242. info = audio_output_getinfo(obs->audio.audio);
  243. sps_to_ns = 1000000000.0 / (double)info->samples_per_sec;
  244. return (uint64_t)((double)frames * sps_to_ns);
  245. }
  246. /* maximum "direct" timestamp variance in nanoseconds */
  247. #define MAX_TS_VAR 5000000000ULL
  248. /* maximum time that timestamp can jump in nanoseconds */
  249. #define MAX_TIMESTAMP_JUMP 2000000000ULL
  250. static inline void reset_audio_timing(obs_source_t source, uint64_t timetamp)
  251. {
  252. source->timing_set = true;
  253. source->timing_adjust = os_gettime_ns() - timetamp;
  254. }
  255. static inline void handle_ts_jump(obs_source_t source, uint64_t ts,
  256. uint64_t diff)
  257. {
  258. uint32_t flags = source->callbacks.get_output_flags(source->data);
  259. blog(LOG_DEBUG, "Timestamp for source '%s' jumped by '%lld', "
  260. "resetting audio timing", source->name, diff);
  261. /* if has video, ignore audio data until reset */
  262. if (flags & SOURCE_ASYNC_VIDEO)
  263. source->audio_reset_ref--;
  264. else
  265. reset_audio_timing(source, ts);
  266. }
  267. static void source_output_audio_line(obs_source_t source,
  268. const struct audio_data *data)
  269. {
  270. struct audio_data in = *data;
  271. uint64_t diff;
  272. if (!source->timing_set) {
  273. reset_audio_timing(source, in.timestamp);
  274. /* detects 'directly' set timestamps as long as they're within
  275. * a certain threshold */
  276. if ((source->timing_adjust + MAX_TS_VAR) < MAX_TS_VAR * 2)
  277. source->timing_adjust = 0;
  278. } else {
  279. diff = in.timestamp - source->next_audio_ts_min;
  280. /* don't need signed because negative will trigger it
  281. * regardless, which is what we want */
  282. if (diff > MAX_TIMESTAMP_JUMP)
  283. handle_ts_jump(source, in.timestamp, diff);
  284. }
  285. source->next_audio_ts_min = in.timestamp +
  286. conv_frames_to_time(source, in.frames);
  287. if (source->audio_reset_ref != 0)
  288. return;
  289. in.timestamp += source->timing_adjust;
  290. in.volume = source->volume;
  291. audio_line_output(source->audio_line, &in);
  292. }
  293. static bool set_texture_size(obs_source_t source, struct source_frame *frame)
  294. {
  295. if (source->output_texture) {
  296. uint32_t width = texture_getwidth(source->output_texture);
  297. uint32_t height = texture_getheight(source->output_texture);
  298. if (width == frame->width && height == frame->height)
  299. return true;
  300. }
  301. texture_destroy(source->output_texture);
  302. source->output_texture = gs_create_texture(frame->width, frame->height,
  303. GS_RGBA, 1, NULL, GS_DYNAMIC);
  304. return source->output_texture != NULL;
  305. }
  306. enum convert_type {
  307. CONVERT_NONE,
  308. CONVERT_NV12,
  309. CONVERT_420,
  310. CONVERT_422_U,
  311. CONVERT_422_Y,
  312. };
  313. static inline enum convert_type get_convert_type(enum video_format format)
  314. {
  315. switch (format) {
  316. case VIDEO_FORMAT_I420:
  317. return CONVERT_420;
  318. case VIDEO_FORMAT_NV12:
  319. return CONVERT_NV12;
  320. case VIDEO_FORMAT_YVYU:
  321. case VIDEO_FORMAT_YUY2:
  322. return CONVERT_422_Y;
  323. case VIDEO_FORMAT_UYVY:
  324. return CONVERT_422_U;
  325. case VIDEO_FORMAT_NONE:
  326. case VIDEO_FORMAT_YUVX:
  327. case VIDEO_FORMAT_UYVX:
  328. case VIDEO_FORMAT_RGBA:
  329. case VIDEO_FORMAT_BGRA:
  330. case VIDEO_FORMAT_BGRX:
  331. return CONVERT_NONE;
  332. }
  333. return CONVERT_NONE;
  334. }
  335. static inline bool is_yuv(enum video_format format)
  336. {
  337. switch (format) {
  338. case VIDEO_FORMAT_I420:
  339. case VIDEO_FORMAT_NV12:
  340. case VIDEO_FORMAT_YVYU:
  341. case VIDEO_FORMAT_YUY2:
  342. case VIDEO_FORMAT_UYVY:
  343. case VIDEO_FORMAT_YUVX:
  344. case VIDEO_FORMAT_UYVX:
  345. return true;
  346. case VIDEO_FORMAT_NONE:
  347. case VIDEO_FORMAT_RGBA:
  348. case VIDEO_FORMAT_BGRA:
  349. case VIDEO_FORMAT_BGRX:
  350. return false;
  351. }
  352. return false;
  353. }
  354. static bool upload_frame(texture_t tex, const struct source_frame *frame)
  355. {
  356. void *ptr;
  357. uint32_t row_bytes;
  358. enum convert_type type = get_convert_type(frame->format);
  359. if (type == CONVERT_NONE) {
  360. texture_setimage(tex, frame->data, frame->row_bytes, false);
  361. return true;
  362. }
  363. if (!texture_map(tex, &ptr, &row_bytes))
  364. return false;
  365. if (type == CONVERT_420)
  366. decompress_420(frame->data, frame->width, frame->height,
  367. frame->row_bytes, 0, frame->height, ptr);
  368. else if (type == CONVERT_NV12)
  369. decompress_nv12(frame->data, frame->width, frame->height,
  370. frame->row_bytes, 0, frame->height, ptr);
  371. else if (type == CONVERT_422_Y)
  372. decompress_422(frame->data, frame->width, frame->height,
  373. frame->row_bytes, 0, frame->height, ptr, true);
  374. else if (type == CONVERT_422_U)
  375. decompress_422(frame->data, frame->width, frame->height,
  376. frame->row_bytes, 0, frame->height, ptr, false);
  377. texture_unmap(tex);
  378. return true;
  379. }
  380. static void obs_source_draw_texture(texture_t tex, struct source_frame *frame)
  381. {
  382. effect_t effect = obs->video.default_effect;
  383. bool yuv = is_yuv(frame->format);
  384. const char *type = yuv ? "DrawYUV" : "DrawRGB";
  385. technique_t tech;
  386. eparam_t param;
  387. if (!upload_frame(tex, frame))
  388. return;
  389. tech = effect_gettechnique(effect, type);
  390. technique_begin(tech);
  391. technique_beginpass(tech, 0);
  392. if (yuv) {
  393. param = effect_getparambyname(effect, "yuv_matrix");
  394. effect_setval(effect, param, frame->yuv_matrix,
  395. sizeof(float) * 16);
  396. }
  397. param = effect_getparambyname(effect, "diffuse");
  398. effect_settexture(effect, param, tex);
  399. gs_draw_sprite(tex, frame->flip ? GS_FLIP_V : 0, 0, 0);
  400. technique_endpass(tech);
  401. technique_end(tech);
  402. }
  403. static void obs_source_render_async_video(obs_source_t source)
  404. {
  405. struct source_frame *frame = obs_source_getframe(source);
  406. if (!frame)
  407. return;
  408. if (set_texture_size(source, frame))
  409. obs_source_draw_texture(source->output_texture, frame);
  410. obs_source_releaseframe(source, frame);
  411. }
  412. static inline void obs_source_render_filters(obs_source_t source)
  413. {
  414. source->rendering_filter = true;
  415. obs_source_video_render(source->filters.array[0]);
  416. source->rendering_filter = false;
  417. }
  418. static inline void obs_source_default_render(obs_source_t source, bool yuv)
  419. {
  420. effect_t effect = obs->video.default_effect;
  421. const char *tech_name = yuv ? "DrawYUV" : "DrawRGB";
  422. technique_t tech = effect_gettechnique(effect, tech_name);
  423. size_t passes, i;
  424. passes = technique_begin(tech);
  425. for (i = 0; i < passes; i++) {
  426. technique_beginpass(tech, i);
  427. source->callbacks.video_render(source->data);
  428. technique_endpass(tech);
  429. }
  430. technique_end(tech);
  431. }
  432. static inline void obs_source_main_render(obs_source_t source)
  433. {
  434. uint32_t flags = source->callbacks.get_output_flags(source->data);
  435. bool default_effect = !source->filter_parent &&
  436. source->filters.num == 0 &&
  437. (flags & SOURCE_DEFAULT_EFFECT) != 0;
  438. if (default_effect)
  439. obs_source_default_render(source, (flags & SOURCE_YUV) != 0);
  440. else
  441. source->callbacks.video_render(source->data);
  442. }
  443. void obs_source_video_render(obs_source_t source)
  444. {
  445. if (source->callbacks.video_render) {
  446. if (source->filters.num && !source->rendering_filter)
  447. obs_source_render_filters(source);
  448. else
  449. obs_source_main_render(source);
  450. } else if (source->filter_target) {
  451. obs_source_video_render(source->filter_target);
  452. } else {
  453. obs_source_render_async_video(source);
  454. }
  455. }
  456. uint32_t obs_source_getwidth(obs_source_t source)
  457. {
  458. if (source->callbacks.getwidth)
  459. return source->callbacks.getwidth(source->data);
  460. return 0;
  461. }
  462. uint32_t obs_source_getheight(obs_source_t source)
  463. {
  464. if (source->callbacks.getheight)
  465. return source->callbacks.getheight(source->data);
  466. return 0;
  467. }
  468. size_t obs_source_getparam(obs_source_t source, const char *param, void *buf,
  469. size_t buf_size)
  470. {
  471. if (source->callbacks.getparam)
  472. return source->callbacks.getparam(source->data, param, buf,
  473. buf_size);
  474. return 0;
  475. }
  476. void obs_source_setparam(obs_source_t source, const char *param,
  477. const void *data, size_t size)
  478. {
  479. if (source->callbacks.setparam)
  480. source->callbacks.setparam(source->data, param, data, size);
  481. }
  482. obs_source_t obs_filter_getparent(obs_source_t filter)
  483. {
  484. return filter->filter_parent;
  485. }
  486. obs_source_t obs_filter_gettarget(obs_source_t filter)
  487. {
  488. return filter->filter_target;
  489. }
  490. void obs_source_filter_add(obs_source_t source, obs_source_t filter)
  491. {
  492. pthread_mutex_lock(&source->filter_mutex);
  493. if (da_find(source->filters, &filter, 0) != DARRAY_INVALID) {
  494. blog(LOG_WARNING, "Tried to add a filter that was already "
  495. "present on the source");
  496. return;
  497. }
  498. if (source->filters.num) {
  499. obs_source_t *back = da_end(source->filters);
  500. (*back)->filter_target = filter;
  501. }
  502. da_push_back(source->filters, &filter);
  503. pthread_mutex_unlock(&source->filter_mutex);
  504. filter->filter_parent = source;
  505. filter->filter_target = source;
  506. }
  507. void obs_source_filter_remove(obs_source_t source, obs_source_t filter)
  508. {
  509. size_t idx;
  510. pthread_mutex_lock(&source->filter_mutex);
  511. idx = da_find(source->filters, &filter, 0);
  512. if (idx == DARRAY_INVALID)
  513. return;
  514. if (idx > 0) {
  515. obs_source_t prev = source->filters.array[idx-1];
  516. prev->filter_target = filter->filter_target;
  517. }
  518. da_erase(source->filters, idx);
  519. pthread_mutex_unlock(&source->filter_mutex);
  520. filter->filter_parent = NULL;
  521. filter->filter_target = NULL;
  522. }
  523. void obs_source_filter_setorder(obs_source_t source, obs_source_t filter,
  524. enum order_movement movement)
  525. {
  526. size_t idx = da_find(source->filters, &filter, 0);
  527. size_t i;
  528. if (idx == DARRAY_INVALID)
  529. return;
  530. if (movement == ORDER_MOVE_UP) {
  531. if (idx == source->filters.num-1)
  532. return;
  533. da_move_item(source->filters, idx, idx+1);
  534. } else if (movement == ORDER_MOVE_DOWN) {
  535. if (idx == 0)
  536. return;
  537. da_move_item(source->filters, idx, idx-1);
  538. } else if (movement == ORDER_MOVE_TOP) {
  539. if (idx == source->filters.num-1)
  540. return;
  541. da_move_item(source->filters, idx, source->filters.num-1);
  542. } else if (movement == ORDER_MOVE_BOTTOM) {
  543. if (idx == 0)
  544. return;
  545. da_move_item(source->filters, idx, 0);
  546. }
  547. /* reorder filter targets, not the nicest way of dealing with things */
  548. for (i = 0; i < source->filters.num; i++) {
  549. obs_source_t next_filter = (i == source->filters.num-1) ?
  550. source : source->filters.array[idx+1];
  551. source->filters.array[i]->filter_target = next_filter;
  552. }
  553. }
  554. obs_data_t obs_source_getsettings(obs_source_t source)
  555. {
  556. obs_data_addref(source->settings);
  557. return source->settings;
  558. }
  559. static inline struct source_frame *filter_async_video(obs_source_t source,
  560. struct source_frame *in)
  561. {
  562. size_t i;
  563. for (i = source->filters.num; i > 0; i--) {
  564. struct obs_source *filter = source->filters.array[i-1];
  565. if (filter->callbacks.filter_video) {
  566. in = filter->callbacks.filter_video(filter->data, in);
  567. if (!in)
  568. return NULL;
  569. }
  570. }
  571. return in;
  572. }
  573. static inline struct source_frame *cache_video(obs_source_t source,
  574. const struct source_frame *frame)
  575. {
  576. /* TODO: use an actual cache */
  577. struct source_frame *new_frame = bmalloc(sizeof(struct source_frame));
  578. memcpy(new_frame, frame, sizeof(struct source_frame));
  579. new_frame->data = bmalloc(frame->row_bytes * frame->height);
  580. return new_frame;
  581. }
  582. void obs_source_output_video(obs_source_t source,
  583. const struct source_frame *frame)
  584. {
  585. struct source_frame *output = cache_video(source, frame);
  586. pthread_mutex_lock(&source->filter_mutex);
  587. output = filter_async_video(source, output);
  588. pthread_mutex_unlock(&source->filter_mutex);
  589. if (output) {
  590. pthread_mutex_lock(&source->video_mutex);
  591. da_push_back(source->video_frames, &output);
  592. pthread_mutex_unlock(&source->video_mutex);
  593. }
  594. }
  595. static inline struct filtered_audio *filter_async_audio(obs_source_t source,
  596. struct filtered_audio *in)
  597. {
  598. size_t i;
  599. for (i = source->filters.num; i > 0; i--) {
  600. struct obs_source *filter = source->filters.array[i-1];
  601. if (filter->callbacks.filter_audio) {
  602. in = filter->callbacks.filter_audio(filter->data, in);
  603. if (!in)
  604. return NULL;
  605. }
  606. }
  607. return in;
  608. }
  609. static inline void reset_resampler(obs_source_t source,
  610. const struct source_audio *audio)
  611. {
  612. const struct audio_output_info *obs_info;
  613. struct resample_info output_info;
  614. obs_info = audio_output_getinfo(obs->audio.audio);
  615. output_info.format = obs_info->format;
  616. output_info.samples_per_sec = obs_info->samples_per_sec;
  617. output_info.speakers = obs_info->speakers;
  618. source->sample_info.format = audio->format;
  619. source->sample_info.samples_per_sec = audio->samples_per_sec;
  620. source->sample_info.speakers = audio->speakers;
  621. if (source->sample_info.samples_per_sec == obs_info->samples_per_sec &&
  622. source->sample_info.format == obs_info->format &&
  623. source->sample_info.speakers == obs_info->speakers) {
  624. source->audio_failed = false;
  625. return;
  626. }
  627. audio_resampler_destroy(source->resampler);
  628. source->resampler = audio_resampler_create(&output_info,
  629. &source->sample_info);
  630. source->audio_failed = source->resampler == NULL;
  631. if (source->resampler == NULL)
  632. blog(LOG_ERROR, "creation of resampler failed");
  633. }
  634. static inline void copy_audio_data(obs_source_t source,
  635. const void *data, uint32_t frames, uint64_t timestamp)
  636. {
  637. size_t blocksize = audio_output_blocksize(obs->audio.audio);
  638. size_t size = (size_t)frames * blocksize;
  639. /* ensure audio storage capacity */
  640. if (source->audio_storage_size < size) {
  641. bfree(source->audio_data.data);
  642. source->audio_data.data = bmalloc(size);
  643. source->audio_storage_size = size;
  644. }
  645. source->audio_data.frames = frames;
  646. source->audio_data.timestamp = timestamp;
  647. memcpy(source->audio_data.data, data, size);
  648. }
  649. /* resamples/remixes new audio to the designated main audio output format */
  650. static void process_audio(obs_source_t source, const struct source_audio *audio)
  651. {
  652. if (source->sample_info.samples_per_sec != audio->samples_per_sec ||
  653. source->sample_info.format != audio->format ||
  654. source->sample_info.speakers != audio->speakers)
  655. reset_resampler(source, audio);
  656. if (source->audio_failed)
  657. return;
  658. if (source->resampler) {
  659. void *output;
  660. uint32_t frames;
  661. uint64_t offset;
  662. audio_resampler_resample(source->resampler, &output, &frames,
  663. audio->data, audio->frames, &offset);
  664. copy_audio_data(source, output, frames,
  665. audio->timestamp - offset);
  666. } else {
  667. copy_audio_data(source, audio->data, audio->frames,
  668. audio->timestamp);
  669. }
  670. }
  671. void obs_source_output_audio(obs_source_t source,
  672. const struct source_audio *audio)
  673. {
  674. uint32_t flags = obs_source_get_output_flags(source);
  675. size_t blocksize = audio_output_blocksize(obs->audio.audio);
  676. struct filtered_audio *output;
  677. process_audio(source, audio);
  678. pthread_mutex_lock(&source->filter_mutex);
  679. output = filter_async_audio(source, &source->audio_data);
  680. if (output) {
  681. pthread_mutex_lock(&source->audio_mutex);
  682. /* wait for video to start before outputting any audio so we
  683. * have a base for sync */
  684. if (source->timing_set || (flags & SOURCE_ASYNC_VIDEO) == 0) {
  685. struct audio_data data;
  686. data.data = output->data;
  687. data.frames = output->frames;
  688. data.timestamp = output->timestamp;
  689. source_output_audio_line(source, &data);
  690. }
  691. pthread_mutex_unlock(&source->audio_mutex);
  692. }
  693. pthread_mutex_unlock(&source->filter_mutex);
  694. }
  695. static inline bool frame_out_of_bounds(obs_source_t source, uint64_t ts)
  696. {
  697. return ((ts - source->last_frame_ts) > MAX_TIMESTAMP_JUMP);
  698. }
  699. static inline struct source_frame *get_closest_frame(obs_source_t source,
  700. uint64_t sys_time, int *audio_time_refs)
  701. {
  702. struct source_frame *next_frame = source->video_frames.array[0];
  703. struct source_frame *frame = NULL;
  704. uint64_t sys_offset = sys_time - source->last_sys_timestamp;
  705. uint64_t frame_time = next_frame->timestamp;
  706. uint64_t frame_offset = 0;
  707. /* account for timestamp invalidation */
  708. if (frame_out_of_bounds(source, frame_time)) {
  709. source->last_frame_ts = next_frame->timestamp;
  710. (*audio_time_refs)++;
  711. } else {
  712. frame_offset = frame_time - source->last_frame_ts;
  713. source->last_frame_ts += sys_offset;
  714. }
  715. while (frame_offset <= sys_offset) {
  716. source_frame_destroy(frame);
  717. frame = next_frame;
  718. da_erase(source->video_frames, 0);
  719. if (!source->video_frames.num)
  720. break;
  721. next_frame = source->video_frames.array[0];
  722. /* more timestamp checking and compensating */
  723. if ((next_frame->timestamp - frame_time) > MAX_TIMESTAMP_JUMP) {
  724. source->last_frame_ts =
  725. next_frame->timestamp - frame_offset;
  726. (*audio_time_refs)++;
  727. }
  728. frame_time = next_frame->timestamp;
  729. frame_offset = frame_time - source->last_frame_ts;
  730. }
  731. return frame;
  732. }
  733. /*
  734. * Ensures that cached frames are displayed on time. If multiple frames
  735. * were cached between renders, then releases the unnecessary frames and uses
  736. * the frame with the closest timing to ensure sync. Also ensures that timing
  737. * with audio is synchronized.
  738. */
  739. struct source_frame *obs_source_getframe(obs_source_t source)
  740. {
  741. struct source_frame *frame = NULL;
  742. uint64_t last_frame_time = source->last_frame_ts;
  743. int audio_time_refs = 0;
  744. uint64_t sys_time;
  745. pthread_mutex_lock(&source->video_mutex);
  746. if (!source->video_frames.num)
  747. goto unlock;
  748. sys_time = os_gettime_ns();
  749. if (!source->last_frame_ts) {
  750. frame = source->video_frames.array[0];
  751. da_erase(source->video_frames, 0);
  752. source->last_frame_ts = frame->timestamp;
  753. } else {
  754. frame = get_closest_frame(source, sys_time, &audio_time_refs);
  755. }
  756. /* reset timing to current system time */
  757. if (frame) {
  758. source->audio_reset_ref += audio_time_refs;
  759. source->timing_adjust = sys_time - frame->timestamp;
  760. source->timing_set = true;
  761. }
  762. source->last_sys_timestamp = sys_time;
  763. unlock:
  764. pthread_mutex_unlock(&source->video_mutex);
  765. if (frame)
  766. obs_source_addref(source);
  767. return frame;
  768. }
  769. void obs_source_releaseframe(obs_source_t source, struct source_frame *frame)
  770. {
  771. if (frame) {
  772. source_frame_destroy(frame);
  773. obs_source_release(source);
  774. }
  775. }
  776. const char *obs_source_getname(obs_source_t source)
  777. {
  778. return source->name;
  779. }
  780. void obs_source_setname(obs_source_t source, const char *name)
  781. {
  782. bfree(source->name);
  783. source->name = bstrdup(name);
  784. }
  785. void obs_source_gettype(obs_source_t source, enum obs_source_type *type,
  786. const char **id)
  787. {
  788. if (type) *type = source->type;
  789. if (id) *id = source->callbacks.id;
  790. }
  791. static inline void render_filter_bypass(obs_source_t target, effect_t effect,
  792. uint32_t width, uint32_t height, bool yuv)
  793. {
  794. const char *tech_name = yuv ? "DrawYUV" : "DrawRGB";
  795. technique_t tech = effect_gettechnique(effect, tech_name);
  796. eparam_t diffuse = effect_getparambyname(effect, "diffuse");
  797. size_t passes, i;
  798. passes = technique_begin(tech);
  799. for (i = 0; i < passes; i++) {
  800. technique_beginpass(tech, i);
  801. obs_source_video_render(target);
  802. technique_endpass(tech);
  803. }
  804. technique_end(tech);
  805. }
  806. static inline void render_filter_tex(texture_t tex, effect_t effect,
  807. uint32_t width, uint32_t height, bool yuv)
  808. {
  809. const char *tech_name = yuv ? "DrawYUV" : "DrawRGB";
  810. technique_t tech = effect_gettechnique(effect, tech_name);
  811. eparam_t diffuse = effect_getparambyname(effect, "diffuse");
  812. size_t passes, i;
  813. effect_settexture(effect, diffuse, tex);
  814. passes = technique_begin(tech);
  815. for (i = 0; i < passes; i++) {
  816. technique_beginpass(tech, i);
  817. gs_draw_sprite(tex, width, height, 0);
  818. technique_endpass(tech);
  819. }
  820. technique_end(tech);
  821. }
  822. void obs_source_process_filter(obs_source_t filter, texrender_t texrender,
  823. effect_t effect, uint32_t width, uint32_t height,
  824. enum allow_direct_render allow_direct)
  825. {
  826. obs_source_t target = obs_filter_gettarget(filter);
  827. obs_source_t parent = obs_filter_getparent(filter);
  828. uint32_t target_flags = obs_source_get_output_flags(target);
  829. uint32_t parent_flags = obs_source_get_output_flags(parent);
  830. int cx = obs_source_getwidth(target);
  831. int cy = obs_source_getheight(target);
  832. bool yuv = (target_flags & SOURCE_YUV) != 0;
  833. bool expects_def = (parent_flags & SOURCE_DEFAULT_EFFECT) != 0;
  834. bool can_directly = allow_direct == ALLOW_DIRECT_RENDERING;
  835. /* if the parent does not use any custom effects, and this is the last
  836. * filter in the chain for the parent, then render the parent directly
  837. * using the filter effect instead of rendering to texture to reduce
  838. * the total number of passes */
  839. if (can_directly && expects_def && target == parent) {
  840. render_filter_bypass(target, effect, width, height, yuv);
  841. return;
  842. }
  843. if (texrender_begin(texrender, cx, cy)) {
  844. gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -100.0f, 100.0f);
  845. if (expects_def && parent == target)
  846. obs_source_default_render(parent, yuv);
  847. else
  848. obs_source_video_render(target);
  849. texrender_end(texrender);
  850. }
  851. /* --------------------------- */
  852. render_filter_tex(texrender_gettexture(texrender), effect,
  853. width, height, yuv);
  854. }
  855. signal_handler_t obs_source_signalhandler(obs_source_t source)
  856. {
  857. return source->signals;
  858. }
  859. proc_handler_t obs_source_prochandler(obs_source_t source)
  860. {
  861. return source->procs;
  862. }
  863. void obs_source_setvolume(obs_source_t source, float volume)
  864. {
  865. source->volume = volume;
  866. }
  867. float obs_source_getvolume(obs_source_t source)
  868. {
  869. return source->volume;
  870. }