obs-source.c 28 KB

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