obs-source.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "media-io/format-conversion.h"
  15. #include "util/platform.h"
  16. #include "graphics/matrix3.h"
  17. #include "graphics/vec3.h"
  18. #include "obs.h"
  19. #include "obs-data.h"
  20. static void obs_source_destroy(obs_source_t source);
  21. bool get_source_info(void *module, const char *module_name,
  22. const char *source_id, struct source_info *info)
  23. {
  24. info->getname = load_module_subfunc(module, module_name,
  25. source_id, "getname", true);
  26. info->create = load_module_subfunc(module, module_name,
  27. source_id,"create", true);
  28. info->destroy = load_module_subfunc(module, module_name,
  29. source_id, "destroy", true);
  30. info->get_output_flags = load_module_subfunc(module, module_name,
  31. source_id, "get_output_flags", true);
  32. if (!info->getname || !info->create || !info->destroy ||
  33. !info->get_output_flags)
  34. return false;
  35. info->config = load_module_subfunc(module, module_name,
  36. source_id, "config", false);
  37. info->activate = load_module_subfunc(module, module_name,
  38. source_id, "activate", false);
  39. info->deactivate = load_module_subfunc(module, module_name,
  40. source_id, "deactivate", false);
  41. info->video_tick = load_module_subfunc(module, module_name,
  42. source_id, "video_tick", false);
  43. info->video_render = load_module_subfunc(module, module_name,
  44. source_id, "video_render", false);
  45. info->getwidth = load_module_subfunc(module, module_name,
  46. source_id, "getwidth", false);
  47. info->getheight = load_module_subfunc(module, module_name,
  48. source_id, "getheight", false);
  49. info->getparam = load_module_subfunc(module, module_name,
  50. source_id, "getparam", false);
  51. info->setparam = load_module_subfunc(module, module_name,
  52. source_id, "setparam", false);
  53. info->filter_video = load_module_subfunc(module, module_name,
  54. source_id, "filter_video", false);
  55. info->filter_audio = load_module_subfunc(module, module_name,
  56. source_id, "filter_audio", false);
  57. info->id = source_id;
  58. return true;
  59. }
  60. static inline const struct source_info *find_source(struct darray *list,
  61. const char *id)
  62. {
  63. size_t i;
  64. struct source_info *array = list->array;
  65. for (i = 0; i < list->num; i++) {
  66. struct source_info *info = array+i;
  67. if (strcmp(info->id, id) == 0)
  68. return info;
  69. }
  70. return NULL;
  71. }
  72. static inline bool obs_source_init_handlers(struct obs_source *source)
  73. {
  74. source->signals = signal_handler_create();
  75. if (!source->signals)
  76. return false;
  77. source->procs = proc_handler_create();
  78. return (source->procs != NULL);
  79. }
  80. /* internal initialization */
  81. bool obs_source_init(struct obs_source *source, const char *settings,
  82. const struct source_info *info)
  83. {
  84. uint32_t flags = info->get_output_flags(source->data);
  85. source->refs = 1;
  86. pthread_mutex_init_value(&source->filter_mutex);
  87. pthread_mutex_init_value(&source->video_mutex);
  88. pthread_mutex_init_value(&source->audio_mutex);
  89. dstr_copy(&source->settings, settings);
  90. memcpy(&source->callbacks, info, sizeof(struct source_info));
  91. if (pthread_mutex_init(&source->filter_mutex, NULL) != 0)
  92. return false;
  93. if (pthread_mutex_init(&source->audio_mutex, NULL) != 0)
  94. return false;
  95. if (pthread_mutex_init(&source->video_mutex, NULL) != 0)
  96. return false;
  97. if (flags & SOURCE_AUDIO) {
  98. source->audio_line = audio_output_createline(obs->audio.audio);
  99. if (!source->audio_line) {
  100. blog(LOG_ERROR, "Failed to create audio line for "
  101. "source");
  102. return false;
  103. }
  104. }
  105. return true;
  106. }
  107. static inline void obs_source_dosignal(struct obs_source *source,
  108. const char *signal)
  109. {
  110. struct calldata data;
  111. calldata_init(&data);
  112. calldata_setptr(&data, "source", source);
  113. signal_handler_signal(obs->signals, signal, &data);
  114. calldata_free(&data);
  115. }
  116. obs_source_t obs_source_create(enum obs_source_type type, const char *id,
  117. const char *name, const char *settings)
  118. {
  119. const struct source_info *info = NULL;
  120. struct darray *list = NULL;
  121. struct obs_source *source;
  122. switch (type) {
  123. case SOURCE_INPUT: list = &obs->input_types.da; break;
  124. case SOURCE_FILTER: list = &obs->filter_types.da; break;
  125. case SOURCE_TRANSITION: list = &obs->transition_types.da; break;
  126. case SOURCE_SCENE:
  127. default:
  128. blog(LOG_WARNING, "Tried to create invalid source type");
  129. return NULL;
  130. }
  131. info = find_source(list, id);
  132. if (!info) {
  133. blog(LOG_WARNING, "Source '%s' not found", id);
  134. return NULL;
  135. }
  136. source = bmalloc(sizeof(struct obs_source));
  137. memset(source, 0, sizeof(struct obs_source));
  138. if (!obs_source_init_handlers(source))
  139. goto fail;
  140. source->name = bstrdup(name);
  141. source->type = type;
  142. source->data = info->create(settings, source);
  143. if (!source->data)
  144. goto fail;
  145. if (!obs_source_init(source, settings, info))
  146. goto fail;
  147. obs_source_dosignal(source, "source-create");
  148. return source;
  149. fail:
  150. blog(LOG_ERROR, "obs_source_create failed");
  151. obs_source_destroy(source);
  152. return NULL;
  153. }
  154. static void obs_source_destroy(obs_source_t source)
  155. {
  156. size_t i;
  157. obs_source_dosignal(source, "source-destroy");
  158. if (source->filter_parent)
  159. obs_source_filter_remove(source->filter_parent, source);
  160. for (i = 0; i < source->filters.num; i++)
  161. obs_source_release(source->filters.array[i]);
  162. for (i = 0; i < source->audio_wait_buffer.num; i++)
  163. audiobuf_free(source->audio_wait_buffer.array+i);
  164. for (i = 0; i < source->video_frames.num; i++)
  165. source_frame_destroy(source->video_frames.array[i]);
  166. gs_entercontext(obs->video.graphics);
  167. texture_destroy(source->output_texture);
  168. gs_leavecontext();
  169. if (source->data)
  170. source->callbacks.destroy(source->data);
  171. bfree(source->audio_data.data);
  172. audio_line_destroy(source->audio_line);
  173. audio_resampler_destroy(source->resampler);
  174. proc_handler_destroy(source->procs);
  175. signal_handler_destroy(source->signals);
  176. da_free(source->video_frames);
  177. da_free(source->audio_wait_buffer);
  178. da_free(source->filters);
  179. pthread_mutex_destroy(&source->filter_mutex);
  180. pthread_mutex_destroy(&source->audio_mutex);
  181. pthread_mutex_destroy(&source->video_mutex);
  182. dstr_free(&source->settings);
  183. bfree(source->name);
  184. bfree(source);
  185. }
  186. int obs_source_addref(obs_source_t source)
  187. {
  188. assert(source != NULL);
  189. if (!source)
  190. return 0;
  191. return ++source->refs;
  192. }
  193. int obs_source_release(obs_source_t source)
  194. {
  195. int refs;
  196. assert(source != NULL);
  197. if (!source)
  198. return 0;
  199. refs = --source->refs;
  200. if (refs == 0)
  201. obs_source_destroy(source);
  202. return refs;
  203. }
  204. void obs_source_remove(obs_source_t source)
  205. {
  206. struct obs_data *data = &obs->data;
  207. size_t id;
  208. source->removed = true;
  209. pthread_mutex_lock(&data->sources_mutex);
  210. id = da_find(data->sources, &source, 0);
  211. if (id != DARRAY_INVALID) {
  212. da_erase_item(data->sources, &source);
  213. obs_source_release(source);
  214. }
  215. pthread_mutex_unlock(&data->sources_mutex);
  216. }
  217. bool obs_source_removed(obs_source_t source)
  218. {
  219. return source->removed;
  220. }
  221. uint32_t obs_source_get_output_flags(obs_source_t source)
  222. {
  223. return source->callbacks.get_output_flags(source->data);
  224. }
  225. bool obs_source_hasconfig(obs_source_t source)
  226. {
  227. return source->callbacks.config != NULL;
  228. }
  229. void obs_source_config(obs_source_t source, void *parent)
  230. {
  231. if (source->callbacks.config)
  232. source->callbacks.config(source->data, parent);
  233. }
  234. void obs_source_activate(obs_source_t source)
  235. {
  236. if (source->callbacks.activate)
  237. source->callbacks.activate(source->data);
  238. }
  239. void obs_source_deactivate(obs_source_t source)
  240. {
  241. if (source->callbacks.deactivate)
  242. source->callbacks.deactivate(source->data);
  243. }
  244. void obs_source_video_tick(obs_source_t source, float seconds)
  245. {
  246. if (source->callbacks.video_tick)
  247. source->callbacks.video_tick(source->data, seconds);
  248. }
  249. /* maximum "direct" timestamp variance in nanoseconds */
  250. #define MAX_VARIANCE 2000000000ULL
  251. static void source_output_audio_line(obs_source_t source,
  252. const struct audio_data *data)
  253. {
  254. struct audio_data in = *data;
  255. if (!in.timestamp) {
  256. in.timestamp = os_gettime_ns();
  257. if (!source->timing_set) {
  258. source->timing_set = true;
  259. source->timing_adjust = 0;
  260. }
  261. }
  262. if (!source->timing_set) {
  263. source->timing_set = true;
  264. source->timing_adjust = in.timestamp - os_gettime_ns();
  265. /* detects 'directly' set timestamps as long as they're within
  266. * a certain threshold */
  267. if ((source->timing_adjust+MAX_VARIANCE) < MAX_VARIANCE*2)
  268. source->timing_adjust = 0;
  269. }
  270. in.timestamp += source->timing_adjust;
  271. audio_line_output(source->audio_line, &in);
  272. }
  273. static void obs_source_flush_audio_wait_buffer(obs_source_t source)
  274. {
  275. size_t i;
  276. pthread_mutex_lock(&source->audio_mutex);
  277. source->timing_set = true;
  278. for (i = 0; i < source->audio_wait_buffer.num; i++) {
  279. struct audiobuf *buf = source->audio_wait_buffer.array+i;
  280. struct audio_data data;
  281. data.data = buf->data;
  282. data.frames = buf->frames;
  283. data.timestamp = buf->timestamp + source->timing_adjust;
  284. audio_line_output(source->audio_line, &data);
  285. audiobuf_free(buf);
  286. }
  287. da_free(source->audio_wait_buffer);
  288. pthread_mutex_unlock(&source->audio_mutex);
  289. }
  290. static bool set_texture_size(obs_source_t source, struct source_frame *frame)
  291. {
  292. if (source->output_texture) {
  293. uint32_t width = texture_getwidth(source->output_texture);
  294. uint32_t height = texture_getheight(source->output_texture);
  295. if (width == frame->width && height == frame->height)
  296. return true;
  297. }
  298. texture_destroy(source->output_texture);
  299. source->output_texture = gs_create_texture(frame->width, frame->height,
  300. GS_RGBA, 1, NULL, GS_DYNAMIC);
  301. return source->output_texture != NULL;
  302. }
  303. enum convert_type {
  304. CONVERT_NONE,
  305. CONVERT_NV12,
  306. CONVERT_420,
  307. CONVERT_422_U,
  308. CONVERT_422_Y,
  309. };
  310. static inline enum convert_type get_convert_type(enum video_format format)
  311. {
  312. switch (format) {
  313. case VIDEO_FORMAT_I420:
  314. return CONVERT_420;
  315. case VIDEO_FORMAT_NV12:
  316. return CONVERT_NV12;
  317. case VIDEO_FORMAT_YVYU:
  318. case VIDEO_FORMAT_YUY2:
  319. return CONVERT_422_Y;
  320. case VIDEO_FORMAT_UYVY:
  321. return CONVERT_422_U;
  322. case VIDEO_FORMAT_UNKNOWN:
  323. case VIDEO_FORMAT_YUVX:
  324. case VIDEO_FORMAT_UYVX:
  325. case VIDEO_FORMAT_RGBA:
  326. case VIDEO_FORMAT_BGRA:
  327. case VIDEO_FORMAT_BGRX:
  328. return CONVERT_NONE;
  329. }
  330. return CONVERT_NONE;
  331. }
  332. static inline bool is_yuv(enum video_format format)
  333. {
  334. switch (format) {
  335. case VIDEO_FORMAT_I420:
  336. case VIDEO_FORMAT_NV12:
  337. case VIDEO_FORMAT_YVYU:
  338. case VIDEO_FORMAT_YUY2:
  339. case VIDEO_FORMAT_UYVY:
  340. case VIDEO_FORMAT_YUVX:
  341. case VIDEO_FORMAT_UYVX:
  342. return true;
  343. case VIDEO_FORMAT_UNKNOWN:
  344. case VIDEO_FORMAT_RGBA:
  345. case VIDEO_FORMAT_BGRA:
  346. case VIDEO_FORMAT_BGRX:
  347. return false;
  348. }
  349. return false;
  350. }
  351. static bool upload_frame(texture_t tex, const struct source_frame *frame)
  352. {
  353. void *ptr;
  354. uint32_t row_bytes;
  355. enum convert_type type = get_convert_type(frame->format);
  356. if (type == CONVERT_NONE) {
  357. texture_setimage(tex, frame->data, frame->row_bytes, false);
  358. return true;
  359. }
  360. if (!texture_map(tex, &ptr, &row_bytes))
  361. return false;
  362. if (type == CONVERT_420)
  363. decompress_420(frame->data, frame->width, frame->height,
  364. frame->row_bytes, 0, frame->height, ptr);
  365. else if (type == CONVERT_NV12)
  366. decompress_nv12(frame->data, frame->width, frame->height,
  367. frame->row_bytes, 0, frame->height, ptr);
  368. else if (type == CONVERT_422_Y)
  369. decompress_422(frame->data, frame->width, frame->height,
  370. frame->row_bytes, 0, frame->height, ptr, true);
  371. else if (type == CONVERT_422_U)
  372. decompress_422(frame->data, frame->width, frame->height,
  373. frame->row_bytes, 0, frame->height, ptr, false);
  374. texture_unmap(tex);
  375. return true;
  376. }
  377. static void obs_source_draw_texture(texture_t tex, struct source_frame *frame)
  378. {
  379. effect_t effect = obs->video.default_effect;
  380. bool yuv = is_yuv(frame->format);
  381. const char *type = yuv ? "DrawYUV" : "DrawRGB";
  382. technique_t tech;
  383. eparam_t param;
  384. if (!upload_frame(tex, frame))
  385. return;
  386. tech = effect_gettechnique(effect, type);
  387. technique_begin(tech);
  388. technique_beginpass(tech, 0);
  389. if (yuv) {
  390. param = effect_getparambyname(effect, "yuv_matrix");
  391. effect_setval(effect, param, frame->yuv_matrix,
  392. sizeof(float) * 16);
  393. }
  394. param = effect_getparambyname(effect, "diffuse");
  395. effect_settexture(effect, param, tex);
  396. gs_draw_sprite(tex, frame->flip ? GS_FLIP_V : 0, 0, 0);
  397. technique_endpass(tech);
  398. technique_end(tech);
  399. }
  400. static void obs_source_render_async_video(obs_source_t source)
  401. {
  402. struct source_frame *frame = obs_source_getframe(source);
  403. if (!frame)
  404. return;
  405. source->timing_adjust = frame->timestamp - os_gettime_ns();
  406. if (!source->timing_set && source->audio_wait_buffer.num)
  407. obs_source_flush_audio_wait_buffer(source);
  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. const char *obs_source_getsettings(obs_source_t source)
  555. {
  556. return source->settings.array;
  557. }
  558. void obs_source_savesettings(obs_source_t source, const char *settings)
  559. {
  560. dstr_copy(&source->settings, 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_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) {
  688. struct audiobuf newbuf;
  689. size_t audio_size = blocksize * output->frames;
  690. newbuf.data = bmalloc(audio_size);
  691. newbuf.frames = output->frames;
  692. newbuf.timestamp = output->timestamp;
  693. memcpy(newbuf.data, output->data, audio_size);
  694. da_push_back(source->audio_wait_buffer, &newbuf);
  695. } else {
  696. struct audio_data data;
  697. data.data = output->data;
  698. data.frames = output->frames;
  699. data.timestamp = output->timestamp;
  700. source_output_audio_line(source, &data);
  701. }
  702. pthread_mutex_unlock(&source->audio_mutex);
  703. }
  704. pthread_mutex_unlock(&source->filter_mutex);
  705. }
  706. /*
  707. * Ensures that cached frames are displayed on time. If multiple frames
  708. * were cached between renders, then releases the unnecessary frames and uses
  709. * the frame with the closest timing to ensure sync.
  710. */
  711. struct source_frame *obs_source_getframe(obs_source_t source)
  712. {
  713. uint64_t last_frame_time = source->last_frame_timestamp;
  714. struct source_frame *frame = NULL;
  715. struct source_frame *next_frame;
  716. uint64_t sys_time, frame_time;
  717. pthread_mutex_lock(&source->video_mutex);
  718. if (!source->video_frames.num)
  719. goto unlock;
  720. next_frame = source->video_frames.array[0];
  721. sys_time = os_gettime_ns();
  722. frame_time = next_frame->timestamp;
  723. if (!source->last_frame_timestamp) {
  724. frame = next_frame;
  725. da_erase(source->video_frames, 0);
  726. source->last_frame_timestamp = frame_time;
  727. } else {
  728. uint64_t sys_offset, frame_offset;
  729. sys_offset = sys_time - source->last_sys_timestamp;
  730. frame_offset = frame_time - last_frame_time;
  731. source->last_frame_timestamp += sys_offset;
  732. while (frame_offset <= sys_offset) {
  733. if (frame)
  734. source_frame_destroy(frame);
  735. frame = next_frame;
  736. da_erase(source->video_frames, 0);
  737. if (!source->video_frames.num)
  738. break;
  739. next_frame = source->video_frames.array[0];
  740. frame_time = next_frame->timestamp;
  741. frame_offset = frame_time - last_frame_time;
  742. }
  743. }
  744. source->last_sys_timestamp = sys_time;
  745. unlock:
  746. pthread_mutex_unlock(&source->video_mutex);
  747. if (frame != NULL)
  748. obs_source_addref(source);
  749. return frame;
  750. }
  751. void obs_source_releaseframe(obs_source_t source, struct source_frame *frame)
  752. {
  753. if (frame) {
  754. source_frame_destroy(frame);
  755. obs_source_release(source);
  756. }
  757. }
  758. const char *obs_source_getname(obs_source_t source)
  759. {
  760. return source->name;
  761. }
  762. void obs_source_setname(obs_source_t source, const char *name)
  763. {
  764. bfree(source->name);
  765. source->name = bstrdup(name);
  766. }
  767. void obs_source_gettype(obs_source_t source, enum obs_source_type *type,
  768. const char **id)
  769. {
  770. if (type) *type = source->type;
  771. if (id) *id = source->callbacks.id;
  772. }
  773. static inline void render_filter_bypass(obs_source_t target, effect_t effect,
  774. uint32_t width, uint32_t height, bool yuv)
  775. {
  776. const char *tech_name = yuv ? "DrawYUV" : "DrawRGB";
  777. technique_t tech = effect_gettechnique(effect, tech_name);
  778. eparam_t diffuse = effect_getparambyname(effect, "diffuse");
  779. size_t passes, i;
  780. passes = technique_begin(tech);
  781. for (i = 0; i < passes; i++) {
  782. technique_beginpass(tech, i);
  783. obs_source_video_render(target);
  784. technique_endpass(tech);
  785. }
  786. technique_end(tech);
  787. }
  788. static inline void render_filter_tex(texture_t tex, effect_t effect,
  789. uint32_t width, uint32_t height, bool yuv)
  790. {
  791. const char *tech_name = yuv ? "DrawYUV" : "DrawRGB";
  792. technique_t tech = effect_gettechnique(effect, tech_name);
  793. eparam_t diffuse = effect_getparambyname(effect, "diffuse");
  794. size_t passes, i;
  795. effect_settexture(effect, diffuse, tex);
  796. passes = technique_begin(tech);
  797. for (i = 0; i < passes; i++) {
  798. technique_beginpass(tech, i);
  799. gs_draw_sprite(tex, width, height, 0);
  800. technique_endpass(tech);
  801. }
  802. technique_end(tech);
  803. }
  804. void obs_source_process_filter(obs_source_t filter, texrender_t texrender,
  805. effect_t effect, uint32_t width, uint32_t height,
  806. enum allow_direct_render allow_direct)
  807. {
  808. obs_source_t target = obs_filter_gettarget(filter);
  809. obs_source_t parent = obs_filter_getparent(filter);
  810. uint32_t target_flags = obs_source_get_output_flags(target);
  811. uint32_t parent_flags = obs_source_get_output_flags(parent);
  812. int cx = obs_source_getwidth(target);
  813. int cy = obs_source_getheight(target);
  814. bool yuv = (target_flags & SOURCE_YUV) != 0;
  815. bool expects_def = (parent_flags & SOURCE_DEFAULT_EFFECT) != 0;
  816. bool can_directly = allow_direct == ALLOW_DIRECT_RENDERING;
  817. /* if the parent does not use any custom effects, and this is the last
  818. * filter in the chain for the parent, then render the parent directly
  819. * using the filter effect instead of rendering to texture to reduce
  820. * the total number of passes */
  821. if (can_directly && expects_def && target == parent) {
  822. render_filter_bypass(target, effect, width, height, yuv);
  823. return;
  824. }
  825. if (texrender_begin(texrender, cx, cy)) {
  826. gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -100.0f, 100.0f);
  827. if (expects_def && parent == target)
  828. obs_source_default_render(parent, yuv);
  829. else
  830. obs_source_video_render(target);
  831. texrender_end(texrender);
  832. }
  833. /* --------------------------- */
  834. render_filter_tex(texrender_gettexture(texrender), effect,
  835. width, height, yuv);
  836. }
  837. signal_handler_t obs_source_signalhandler(obs_source_t source)
  838. {
  839. return source->signals;
  840. }
  841. proc_handler_t obs_source_prochandler(obs_source_t source)
  842. {
  843. return source->procs;
  844. }