obs-source.c 28 KB

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