obs-source.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  1. /******************************************************************************
  2. Copyright (C) 2013-2014 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 <inttypes.h>
  15. #include "media-io/format-conversion.h"
  16. #include "media-io/video-frame.h"
  17. #include "util/platform.h"
  18. #include "callback/calldata.h"
  19. #include "graphics/matrix3.h"
  20. #include "graphics/vec3.h"
  21. #include "obs.h"
  22. #include "obs-internal.h"
  23. static void obs_source_destroy(obs_source_t source);
  24. static inline const struct obs_source_info *find_source(struct darray *list,
  25. const char *id)
  26. {
  27. size_t i;
  28. struct obs_source_info *array = list->array;
  29. for (i = 0; i < list->num; i++) {
  30. struct obs_source_info *info = array+i;
  31. if (strcmp(info->id, id) == 0)
  32. return info;
  33. }
  34. return NULL;
  35. }
  36. static const struct obs_source_info *get_source_info(enum obs_source_type type,
  37. const char *id)
  38. {
  39. struct darray *list = NULL;
  40. switch (type) {
  41. case OBS_SOURCE_TYPE_INPUT:
  42. list = &obs->input_types.da;
  43. break;
  44. case OBS_SOURCE_TYPE_FILTER:
  45. list = &obs->filter_types.da;
  46. break;
  47. case OBS_SOURCE_TYPE_TRANSITION:
  48. list = &obs->transition_types.da;
  49. break;
  50. case OBS_SOURCE_TYPE_SCENE:
  51. default:
  52. blog(LOG_WARNING, "get_source_info: invalid source type");
  53. return NULL;
  54. }
  55. return find_source(list, id);
  56. }
  57. bool obs_source_init_handlers(struct obs_source *source)
  58. {
  59. source->signals = signal_handler_create();
  60. if (!source->signals)
  61. return false;
  62. source->procs = proc_handler_create();
  63. return (source->procs != NULL);
  64. }
  65. const char *obs_source_getdisplayname(enum obs_source_type type,
  66. const char *id, const char *locale)
  67. {
  68. const struct obs_source_info *info = get_source_info(type, id);
  69. return (info != NULL) ? info->getname(locale) : NULL;
  70. }
  71. /* internal initialization */
  72. bool obs_source_init(struct obs_source *source,
  73. const struct obs_source_info *info)
  74. {
  75. source->refs = 1;
  76. source->user_volume = 1.0f;
  77. source->present_volume = 1.0f;
  78. source->sync_offset = 0;
  79. pthread_mutex_init_value(&source->filter_mutex);
  80. pthread_mutex_init_value(&source->video_mutex);
  81. pthread_mutex_init_value(&source->audio_mutex);
  82. memcpy(&source->info, info, sizeof(struct obs_source_info));
  83. if (pthread_mutex_init(&source->filter_mutex, NULL) != 0)
  84. return false;
  85. if (pthread_mutex_init(&source->audio_mutex, NULL) != 0)
  86. return false;
  87. if (pthread_mutex_init(&source->video_mutex, NULL) != 0)
  88. return false;
  89. if (info->output_flags & OBS_SOURCE_AUDIO) {
  90. source->audio_line = audio_output_createline(obs->audio.audio,
  91. source->name);
  92. if (!source->audio_line) {
  93. blog(LOG_ERROR, "Failed to create audio line for "
  94. "source '%s'", source->name);
  95. return false;
  96. }
  97. }
  98. return true;
  99. }
  100. static inline void obs_source_dosignal(struct obs_source *source,
  101. const char *signal_obs, const char *signal_source)
  102. {
  103. struct calldata data;
  104. calldata_init(&data);
  105. calldata_setptr(&data, "source", source);
  106. if (signal_obs)
  107. signal_handler_signal(obs->signals, signal_obs, &data);
  108. if (signal_source)
  109. signal_handler_signal(source->signals, signal_source, &data);
  110. calldata_free(&data);
  111. }
  112. obs_source_t obs_source_create(enum obs_source_type type, const char *id,
  113. const char *name, obs_data_t settings)
  114. {
  115. struct obs_source *source;
  116. const struct obs_source_info *info = get_source_info(type, id);
  117. if (!info) {
  118. blog(LOG_WARNING, "Source '%s' not found", id);
  119. return NULL;
  120. }
  121. source = bzalloc(sizeof(struct obs_source));
  122. if (!obs_source_init_handlers(source))
  123. goto fail;
  124. source->name = bstrdup(name);
  125. source->settings = obs_data_newref(settings);
  126. source->data = info->create(source->settings, source);
  127. if (!source->data)
  128. goto fail;
  129. if (!obs_source_init(source, info))
  130. goto fail;
  131. obs_source_dosignal(source, "source-create", NULL);
  132. return source;
  133. fail:
  134. blog(LOG_ERROR, "obs_source_create failed");
  135. obs_source_destroy(source);
  136. return NULL;
  137. }
  138. void source_frame_init(struct source_frame *frame, enum video_format format,
  139. uint32_t width, uint32_t height)
  140. {
  141. struct video_frame vid_frame;
  142. video_frame_init(&vid_frame, format, width, height);
  143. frame->format = format;
  144. frame->width = width;
  145. frame->height = height;
  146. for (size_t i = 0; i < MAX_AV_PLANES; i++) {
  147. frame->data[i] = vid_frame.data[i];
  148. frame->linesize[i] = vid_frame.linesize[i];
  149. }
  150. }
  151. static void obs_source_destroy(struct obs_source *source)
  152. {
  153. size_t i;
  154. obs_source_dosignal(source, "source-destroy", "destroy");
  155. if (source->filter_parent)
  156. obs_source_filter_remove(source->filter_parent, source);
  157. for (i = 0; i < source->filters.num; i++)
  158. obs_source_release(source->filters.array[i]);
  159. for (i = 0; i < source->video_frames.num; i++)
  160. source_frame_destroy(source->video_frames.array[i]);
  161. gs_entercontext(obs->video.graphics);
  162. texture_destroy(source->output_texture);
  163. gs_leavecontext();
  164. if (source->data)
  165. source->info.destroy(source->data);
  166. for (i = 0; i < MAX_AV_PLANES; i++)
  167. bfree(source->audio_data.data[i]);
  168. audio_line_destroy(source->audio_line);
  169. audio_resampler_destroy(source->resampler);
  170. proc_handler_destroy(source->procs);
  171. signal_handler_destroy(source->signals);
  172. texrender_destroy(source->filter_texrender);
  173. da_free(source->video_frames);
  174. da_free(source->filters);
  175. pthread_mutex_destroy(&source->filter_mutex);
  176. pthread_mutex_destroy(&source->audio_mutex);
  177. pthread_mutex_destroy(&source->video_mutex);
  178. obs_data_release(source->settings);
  179. bfree(source->name);
  180. bfree(source);
  181. }
  182. void obs_source_addref(obs_source_t source)
  183. {
  184. if (source)
  185. ++source->refs;
  186. }
  187. void obs_source_release(obs_source_t source)
  188. {
  189. if (!source)
  190. return;
  191. if (--source->refs == 0)
  192. obs_source_destroy(source);
  193. }
  194. void obs_source_remove(obs_source_t source)
  195. {
  196. struct obs_core_data *data = &obs->data;
  197. size_t id;
  198. pthread_mutex_lock(&data->sources_mutex);
  199. if (!source || source->removed)
  200. return;
  201. source->removed = true;
  202. obs_source_addref(source);
  203. id = da_find(data->sources, &source, 0);
  204. if (id != DARRAY_INVALID) {
  205. da_erase_item(data->sources, &source);
  206. obs_source_release(source);
  207. }
  208. pthread_mutex_unlock(&data->sources_mutex);
  209. obs_source_dosignal(source, "source-remove", "remove");
  210. obs_source_release(source);
  211. }
  212. bool obs_source_removed(obs_source_t source)
  213. {
  214. return source->removed;
  215. }
  216. obs_properties_t obs_source_properties(enum obs_source_type type,
  217. const char *id, const char *locale)
  218. {
  219. const struct obs_source_info *info = get_source_info(type, id);
  220. if (info && info->get_properties)
  221. return info->get_properties(locale);
  222. return NULL;
  223. }
  224. uint32_t obs_source_get_output_flags(obs_source_t source)
  225. {
  226. return source->info.output_flags;
  227. }
  228. void obs_source_update(obs_source_t source, obs_data_t settings)
  229. {
  230. obs_data_apply(source->settings, settings);
  231. if (source->info.update)
  232. source->info.update(source->data, source->settings);
  233. }
  234. static void activate_source(obs_source_t source)
  235. {
  236. if (source->info.activate)
  237. source->info.activate(source->data);
  238. obs_source_dosignal(source, "source-activate", "activate");
  239. }
  240. static void deactivate_source(obs_source_t source)
  241. {
  242. if (source->info.deactivate)
  243. source->info.deactivate(source->data);
  244. obs_source_dosignal(source, "source-deactivate", "deactivate");
  245. }
  246. static void show_source(obs_source_t source)
  247. {
  248. if (source->info.show)
  249. source->info.show(source->data);
  250. obs_source_dosignal(source, "source-show", "show");
  251. }
  252. static void hide_source(obs_source_t source)
  253. {
  254. if (source->info.hide)
  255. source->info.hide(source->data);
  256. obs_source_dosignal(source, "source-hide", "hide");
  257. }
  258. static void activate_tree(obs_source_t parent, obs_source_t child, void *param)
  259. {
  260. if (++child->activate_refs == 1)
  261. activate_source(child);
  262. UNUSED_PARAMETER(parent);
  263. UNUSED_PARAMETER(param);
  264. }
  265. static void deactivate_tree(obs_source_t parent, obs_source_t child,
  266. void *param)
  267. {
  268. if (--child->activate_refs == 0)
  269. deactivate_source(child);
  270. UNUSED_PARAMETER(parent);
  271. UNUSED_PARAMETER(param);
  272. }
  273. static void show_tree(obs_source_t parent, obs_source_t child, void *param)
  274. {
  275. if (++child->show_refs == 1)
  276. show_source(child);
  277. UNUSED_PARAMETER(parent);
  278. UNUSED_PARAMETER(param);
  279. }
  280. static void hide_tree(obs_source_t parent, obs_source_t child, void *param)
  281. {
  282. if (--child->show_refs == 0)
  283. hide_source(child);
  284. UNUSED_PARAMETER(parent);
  285. UNUSED_PARAMETER(param);
  286. }
  287. void obs_source_activate(obs_source_t source, enum view_type type)
  288. {
  289. if (!source) return;
  290. if (++source->show_refs == 1) {
  291. show_source(source);
  292. obs_source_enum_tree(source, show_tree, NULL);
  293. }
  294. if (type == MAIN_VIEW) {
  295. if (++source->activate_refs == 1) {
  296. activate_source(source);
  297. obs_source_enum_tree(source, activate_tree, NULL);
  298. obs_source_set_present_volume(source, 1.0f);
  299. }
  300. }
  301. }
  302. void obs_source_deactivate(obs_source_t source, enum view_type type)
  303. {
  304. if (!source) return;
  305. if (--source->show_refs == 0) {
  306. hide_source(source);
  307. obs_source_enum_tree(source, hide_tree, NULL);
  308. }
  309. if (type == MAIN_VIEW) {
  310. if (--source->activate_refs == 0) {
  311. deactivate_source(source);
  312. obs_source_enum_tree(source, deactivate_tree, NULL);
  313. obs_source_set_present_volume(source, 0.0f);
  314. }
  315. }
  316. }
  317. void obs_source_video_tick(obs_source_t source, float seconds)
  318. {
  319. /* reset the filter render texture information once every frame */
  320. if (source->filter_texrender)
  321. texrender_reset(source->filter_texrender);
  322. if (source->info.video_tick)
  323. source->info.video_tick(source->data, seconds);
  324. }
  325. /* unless the value is 3+ hours worth of frames, this won't overflow */
  326. static inline uint64_t conv_frames_to_time(size_t frames)
  327. {
  328. const struct audio_output_info *info;
  329. info = audio_output_getinfo(obs->audio.audio);
  330. return (uint64_t)frames * 1000000000ULL /
  331. (uint64_t)info->samples_per_sec;
  332. }
  333. /* maximum "direct" timestamp variance in nanoseconds */
  334. #define MAX_TS_VAR 5000000000ULL
  335. /* maximum time that timestamp can jump in nanoseconds */
  336. #define MAX_TIMESTAMP_JUMP 2000000000ULL
  337. /* time threshold in nanoseconds to ensure audio timing is as seamless as
  338. * possible */
  339. #define TS_SMOOTHING_THRESHOLD 70000000ULL
  340. static inline void reset_audio_timing(obs_source_t source, uint64_t timetamp)
  341. {
  342. source->timing_set = true;
  343. source->timing_adjust = os_gettime_ns() - timetamp;
  344. }
  345. static inline void handle_ts_jump(obs_source_t source, uint64_t expected,
  346. uint64_t ts, uint64_t diff)
  347. {
  348. blog(LOG_DEBUG, "Timestamp for source '%s' jumped by '%"PRIu64"', "
  349. "expected value %"PRIu64", input value %"PRIu64,
  350. source->name, diff, expected, ts);
  351. /* if has video, ignore audio data until reset */
  352. if (source->info.output_flags & OBS_SOURCE_ASYNC_VIDEO)
  353. source->audio_reset_ref--;
  354. else
  355. reset_audio_timing(source, ts);
  356. }
  357. static void source_output_audio_line(obs_source_t source,
  358. const struct audio_data *data)
  359. {
  360. struct audio_data in = *data;
  361. uint64_t diff;
  362. if (!source->timing_set) {
  363. reset_audio_timing(source, in.timestamp);
  364. /* detects 'directly' set timestamps as long as they're within
  365. * a certain threshold */
  366. if ((source->timing_adjust + MAX_TS_VAR) < MAX_TS_VAR * 2)
  367. source->timing_adjust = 0;
  368. } else {
  369. bool ts_under = (in.timestamp < source->next_audio_ts_min);
  370. diff = ts_under ?
  371. (source->next_audio_ts_min - in.timestamp) :
  372. (in.timestamp - source->next_audio_ts_min);
  373. /* smooth audio if lower or within threshold */
  374. if (diff > MAX_TIMESTAMP_JUMP)
  375. handle_ts_jump(source, source->next_audio_ts_min,
  376. in.timestamp, diff);
  377. else if (ts_under || diff < TS_SMOOTHING_THRESHOLD)
  378. in.timestamp = source->next_audio_ts_min;
  379. }
  380. source->next_audio_ts_min = in.timestamp +
  381. conv_frames_to_time(in.frames);
  382. if (source->audio_reset_ref != 0)
  383. return;
  384. in.timestamp += source->timing_adjust + source->sync_offset;
  385. in.volume = source->user_volume * source->present_volume *
  386. obs->audio.user_volume * obs->audio.present_volume;
  387. audio_line_output(source->audio_line, &in);
  388. }
  389. static bool set_texture_size(obs_source_t source, struct source_frame *frame)
  390. {
  391. if (source->output_texture) {
  392. uint32_t width = texture_getwidth(source->output_texture);
  393. uint32_t height = texture_getheight(source->output_texture);
  394. if (width == frame->width && height == frame->height)
  395. return true;
  396. }
  397. texture_destroy(source->output_texture);
  398. source->output_texture = gs_create_texture(frame->width, frame->height,
  399. GS_RGBA, 1, NULL, GS_DYNAMIC);
  400. return source->output_texture != NULL;
  401. }
  402. enum convert_type {
  403. CONVERT_NONE,
  404. CONVERT_NV12,
  405. CONVERT_420,
  406. CONVERT_422_U,
  407. CONVERT_422_Y,
  408. };
  409. static inline enum convert_type get_convert_type(enum video_format format)
  410. {
  411. switch (format) {
  412. case VIDEO_FORMAT_I420:
  413. return CONVERT_420;
  414. case VIDEO_FORMAT_NV12:
  415. return CONVERT_NV12;
  416. case VIDEO_FORMAT_YVYU:
  417. case VIDEO_FORMAT_YUY2:
  418. return CONVERT_422_Y;
  419. case VIDEO_FORMAT_UYVY:
  420. return CONVERT_422_U;
  421. case VIDEO_FORMAT_NONE:
  422. case VIDEO_FORMAT_RGBA:
  423. case VIDEO_FORMAT_BGRA:
  424. case VIDEO_FORMAT_BGRX:
  425. return CONVERT_NONE;
  426. }
  427. return CONVERT_NONE;
  428. }
  429. static bool upload_frame(texture_t tex, const struct source_frame *frame)
  430. {
  431. void *ptr;
  432. uint32_t linesize;
  433. enum convert_type type = get_convert_type(frame->format);
  434. if (type == CONVERT_NONE) {
  435. texture_setimage(tex, frame->data[0], frame->linesize[0],
  436. false);
  437. return true;
  438. }
  439. if (!texture_map(tex, &ptr, &linesize))
  440. return false;
  441. if (type == CONVERT_420)
  442. decompress_420((const uint8_t* const*)frame->data,
  443. frame->linesize,
  444. 0, frame->height, ptr, linesize);
  445. else if (type == CONVERT_NV12)
  446. decompress_nv12((const uint8_t* const*)frame->data,
  447. frame->linesize,
  448. 0, frame->height, ptr, linesize);
  449. else if (type == CONVERT_422_Y)
  450. decompress_422(frame->data[0], frame->linesize[0],
  451. 0, frame->height, ptr, linesize, true);
  452. else if (type == CONVERT_422_U)
  453. decompress_422(frame->data[0], frame->linesize[0],
  454. 0, frame->height, ptr, linesize, false);
  455. texture_unmap(tex);
  456. return true;
  457. }
  458. static void obs_source_draw_texture(texture_t tex, struct source_frame *frame)
  459. {
  460. effect_t effect = obs->video.default_effect;
  461. bool yuv = format_is_yuv(frame->format);
  462. const char *type = yuv ? "DrawMatrix" : "Draw";
  463. technique_t tech;
  464. eparam_t param;
  465. if (!upload_frame(tex, frame))
  466. return;
  467. tech = effect_gettechnique(effect, type);
  468. technique_begin(tech);
  469. technique_beginpass(tech, 0);
  470. if (yuv) {
  471. param = effect_getparambyname(effect, "color_matrix");
  472. effect_setval(effect, param, frame->color_matrix,
  473. sizeof(float) * 16);
  474. }
  475. param = effect_getparambyname(effect, "image");
  476. effect_settexture(effect, param, tex);
  477. gs_draw_sprite(tex, frame->flip ? GS_FLIP_V : 0, 0, 0);
  478. technique_endpass(tech);
  479. technique_end(tech);
  480. }
  481. static void obs_source_render_async_video(obs_source_t source)
  482. {
  483. struct source_frame *frame = obs_source_getframe(source);
  484. if (!frame)
  485. return;
  486. if (set_texture_size(source, frame))
  487. obs_source_draw_texture(source->output_texture, frame);
  488. obs_source_releaseframe(source, frame);
  489. }
  490. static inline void obs_source_render_filters(obs_source_t source)
  491. {
  492. source->rendering_filter = true;
  493. obs_source_video_render(source->filters.array[0]);
  494. source->rendering_filter = false;
  495. }
  496. static inline void obs_source_default_render(obs_source_t source,
  497. bool color_matrix)
  498. {
  499. effect_t effect = obs->video.default_effect;
  500. const char *tech_name = color_matrix ? "DrawMatrix" : "Draw";
  501. technique_t tech = effect_gettechnique(effect, tech_name);
  502. size_t passes, i;
  503. passes = technique_begin(tech);
  504. for (i = 0; i < passes; i++) {
  505. technique_beginpass(tech, i);
  506. source->info.video_render(source->data, effect);
  507. technique_endpass(tech);
  508. }
  509. technique_end(tech);
  510. }
  511. static inline void obs_source_main_render(obs_source_t source)
  512. {
  513. uint32_t flags = source->info.output_flags;
  514. bool color_matrix = (flags & OBS_SOURCE_COLOR_MATRIX) != 0;
  515. bool default_effect = !source->filter_parent &&
  516. source->filters.num == 0 &&
  517. (flags & OBS_SOURCE_CUSTOM_DRAW) == 0;
  518. if (default_effect)
  519. obs_source_default_render(source, color_matrix);
  520. else
  521. source->info.video_render(source->data, NULL);
  522. }
  523. void obs_source_video_render(obs_source_t source)
  524. {
  525. if (source->info.video_render) {
  526. if (source->filters.num && !source->rendering_filter)
  527. obs_source_render_filters(source);
  528. else
  529. obs_source_main_render(source);
  530. } else if (source->filter_target) {
  531. obs_source_video_render(source->filter_target);
  532. } else {
  533. obs_source_render_async_video(source);
  534. }
  535. }
  536. uint32_t obs_source_getwidth(obs_source_t source)
  537. {
  538. if (source->info.getwidth)
  539. return source->info.getwidth(source->data);
  540. return 0;
  541. }
  542. uint32_t obs_source_getheight(obs_source_t source)
  543. {
  544. if (source->info.getheight)
  545. return source->info.getheight(source->data);
  546. return 0;
  547. }
  548. obs_source_t obs_filter_getparent(obs_source_t filter)
  549. {
  550. return filter->filter_parent;
  551. }
  552. obs_source_t obs_filter_gettarget(obs_source_t filter)
  553. {
  554. return filter->filter_target;
  555. }
  556. void obs_source_filter_add(obs_source_t source, obs_source_t filter)
  557. {
  558. pthread_mutex_lock(&source->filter_mutex);
  559. if (da_find(source->filters, &filter, 0) != DARRAY_INVALID) {
  560. blog(LOG_WARNING, "Tried to add a filter that was already "
  561. "present on the source");
  562. return;
  563. }
  564. if (source->filters.num) {
  565. obs_source_t *back = da_end(source->filters);
  566. (*back)->filter_target = filter;
  567. }
  568. da_push_back(source->filters, &filter);
  569. pthread_mutex_unlock(&source->filter_mutex);
  570. filter->filter_parent = source;
  571. filter->filter_target = source;
  572. }
  573. void obs_source_filter_remove(obs_source_t source, obs_source_t filter)
  574. {
  575. size_t idx;
  576. pthread_mutex_lock(&source->filter_mutex);
  577. idx = da_find(source->filters, &filter, 0);
  578. if (idx == DARRAY_INVALID)
  579. return;
  580. if (idx > 0) {
  581. obs_source_t prev = source->filters.array[idx-1];
  582. prev->filter_target = filter->filter_target;
  583. }
  584. da_erase(source->filters, idx);
  585. pthread_mutex_unlock(&source->filter_mutex);
  586. filter->filter_parent = NULL;
  587. filter->filter_target = NULL;
  588. }
  589. void obs_source_filter_setorder(obs_source_t source, obs_source_t filter,
  590. enum order_movement movement)
  591. {
  592. size_t idx = da_find(source->filters, &filter, 0);
  593. size_t i;
  594. if (idx == DARRAY_INVALID)
  595. return;
  596. if (movement == ORDER_MOVE_UP) {
  597. if (idx == source->filters.num-1)
  598. return;
  599. da_move_item(source->filters, idx, idx+1);
  600. } else if (movement == ORDER_MOVE_DOWN) {
  601. if (idx == 0)
  602. return;
  603. da_move_item(source->filters, idx, idx-1);
  604. } else if (movement == ORDER_MOVE_TOP) {
  605. if (idx == source->filters.num-1)
  606. return;
  607. da_move_item(source->filters, idx, source->filters.num-1);
  608. } else if (movement == ORDER_MOVE_BOTTOM) {
  609. if (idx == 0)
  610. return;
  611. da_move_item(source->filters, idx, 0);
  612. }
  613. /* reorder filter targets, not the nicest way of dealing with things */
  614. for (i = 0; i < source->filters.num; i++) {
  615. obs_source_t next_filter = (i == source->filters.num-1) ?
  616. source : source->filters.array[idx+1];
  617. source->filters.array[i]->filter_target = next_filter;
  618. }
  619. }
  620. obs_data_t obs_source_getsettings(obs_source_t source)
  621. {
  622. obs_data_addref(source->settings);
  623. return source->settings;
  624. }
  625. static inline struct source_frame *filter_async_video(obs_source_t source,
  626. struct source_frame *in)
  627. {
  628. size_t i;
  629. for (i = source->filters.num; i > 0; i--) {
  630. struct obs_source *filter = source->filters.array[i-1];
  631. if (filter->info.filter_video) {
  632. in = filter->info.filter_video(filter->data, in);
  633. if (!in)
  634. return NULL;
  635. }
  636. }
  637. return in;
  638. }
  639. static inline void copy_frame_data_line(struct source_frame *dst,
  640. const struct source_frame *src, uint32_t plane, uint32_t y)
  641. {
  642. uint32_t pos_src = y * src->linesize[plane];
  643. uint32_t pos_dst = y * dst->linesize[plane];
  644. uint32_t bytes = dst->linesize[plane] < src->linesize[plane] ?
  645. dst->linesize[plane] : src->linesize[plane];
  646. memcpy(dst->data[plane] + pos_dst, src->data[plane] + pos_src, bytes);
  647. }
  648. static inline void copy_frame_data_plane(struct source_frame *dst,
  649. const struct source_frame *src, uint32_t plane, uint32_t lines)
  650. {
  651. if (dst->linesize[plane] != src->linesize[plane])
  652. for (uint32_t y = 0; y < lines; y++)
  653. copy_frame_data_line(dst, src, plane, y);
  654. else
  655. memcpy(dst->data[plane], src->data[plane],
  656. dst->linesize[plane] * lines);
  657. }
  658. static void copy_frame_data(struct source_frame *dst,
  659. const struct source_frame *src)
  660. {
  661. dst->flip = src->flip;
  662. dst->timestamp = src->timestamp;
  663. memcpy(dst->color_matrix, src->color_matrix, sizeof(float) * 16);
  664. switch (dst->format) {
  665. case VIDEO_FORMAT_I420:
  666. copy_frame_data_plane(dst, src, 0, dst->height);
  667. copy_frame_data_plane(dst, src, 1, dst->height/2);
  668. copy_frame_data_plane(dst, src, 2, dst->height/2);
  669. break;
  670. case VIDEO_FORMAT_NV12:
  671. copy_frame_data_plane(dst, src, 0, dst->height);
  672. copy_frame_data_plane(dst, src, 1, dst->height/2);
  673. break;
  674. case VIDEO_FORMAT_YVYU:
  675. case VIDEO_FORMAT_YUY2:
  676. case VIDEO_FORMAT_UYVY:
  677. case VIDEO_FORMAT_NONE:
  678. case VIDEO_FORMAT_RGBA:
  679. case VIDEO_FORMAT_BGRA:
  680. case VIDEO_FORMAT_BGRX:
  681. copy_frame_data_plane(dst, src, 0, dst->height);
  682. }
  683. }
  684. static inline struct source_frame *cache_video(const struct source_frame *frame)
  685. {
  686. /* TODO: use an actual cache */
  687. struct source_frame *new_frame = source_frame_create(frame->format,
  688. frame->width, frame->height);
  689. copy_frame_data(new_frame, frame);
  690. return new_frame;
  691. }
  692. void obs_source_output_video(obs_source_t source,
  693. const struct source_frame *frame)
  694. {
  695. struct source_frame *output = cache_video(frame);
  696. pthread_mutex_lock(&source->filter_mutex);
  697. output = filter_async_video(source, output);
  698. pthread_mutex_unlock(&source->filter_mutex);
  699. if (output) {
  700. pthread_mutex_lock(&source->video_mutex);
  701. da_push_back(source->video_frames, &output);
  702. pthread_mutex_unlock(&source->video_mutex);
  703. }
  704. }
  705. static inline struct filtered_audio *filter_async_audio(obs_source_t source,
  706. struct filtered_audio *in)
  707. {
  708. size_t i;
  709. for (i = source->filters.num; i > 0; i--) {
  710. struct obs_source *filter = source->filters.array[i-1];
  711. if (filter->info.filter_audio) {
  712. in = filter->info.filter_audio(filter->data, in);
  713. if (!in)
  714. return NULL;
  715. }
  716. }
  717. return in;
  718. }
  719. static inline void reset_resampler(obs_source_t source,
  720. const struct source_audio *audio)
  721. {
  722. const struct audio_output_info *obs_info;
  723. struct resample_info output_info;
  724. obs_info = audio_output_getinfo(obs->audio.audio);
  725. output_info.format = obs_info->format;
  726. output_info.samples_per_sec = obs_info->samples_per_sec;
  727. output_info.speakers = obs_info->speakers;
  728. source->sample_info.format = audio->format;
  729. source->sample_info.samples_per_sec = audio->samples_per_sec;
  730. source->sample_info.speakers = audio->speakers;
  731. if (source->sample_info.samples_per_sec == obs_info->samples_per_sec &&
  732. source->sample_info.format == obs_info->format &&
  733. source->sample_info.speakers == obs_info->speakers) {
  734. source->audio_failed = false;
  735. return;
  736. }
  737. audio_resampler_destroy(source->resampler);
  738. source->resampler = audio_resampler_create(&output_info,
  739. &source->sample_info);
  740. source->audio_failed = source->resampler == NULL;
  741. if (source->resampler == NULL)
  742. blog(LOG_ERROR, "creation of resampler failed");
  743. }
  744. static inline void copy_audio_data(obs_source_t source,
  745. const uint8_t *const data[], uint32_t frames, uint64_t ts)
  746. {
  747. size_t planes = audio_output_planes(obs->audio.audio);
  748. size_t blocksize = audio_output_blocksize(obs->audio.audio);
  749. size_t size = (size_t)frames * blocksize;
  750. bool resize = source->audio_storage_size < size;
  751. source->audio_data.frames = frames;
  752. source->audio_data.timestamp = ts;
  753. for (size_t i = 0; i < planes; i++) {
  754. /* ensure audio storage capacity */
  755. if (resize) {
  756. bfree(source->audio_data.data[i]);
  757. source->audio_data.data[i] = bmalloc(size);
  758. }
  759. memcpy(source->audio_data.data[i], data[i], size);
  760. }
  761. if (resize)
  762. source->audio_storage_size = size;
  763. }
  764. /* resamples/remixes new audio to the designated main audio output format */
  765. static void process_audio(obs_source_t source, const struct source_audio *audio)
  766. {
  767. if (source->sample_info.samples_per_sec != audio->samples_per_sec ||
  768. source->sample_info.format != audio->format ||
  769. source->sample_info.speakers != audio->speakers)
  770. reset_resampler(source, audio);
  771. if (source->audio_failed)
  772. return;
  773. if (source->resampler) {
  774. uint8_t *output[MAX_AV_PLANES];
  775. uint32_t frames;
  776. uint64_t offset;
  777. memset(output, 0, sizeof(output));
  778. audio_resampler_resample(source->resampler,
  779. output, &frames, &offset,
  780. audio->data, audio->frames);
  781. copy_audio_data(source, (const uint8_t *const *)output, frames,
  782. audio->timestamp - offset);
  783. } else {
  784. copy_audio_data(source, audio->data, audio->frames,
  785. audio->timestamp);
  786. }
  787. }
  788. void obs_source_output_audio(obs_source_t source,
  789. const struct source_audio *audio)
  790. {
  791. uint32_t flags = source->info.output_flags;
  792. struct filtered_audio *output;
  793. process_audio(source, audio);
  794. pthread_mutex_lock(&source->filter_mutex);
  795. output = filter_async_audio(source, &source->audio_data);
  796. if (output) {
  797. bool async = (flags & OBS_SOURCE_ASYNC_VIDEO) != 0;
  798. pthread_mutex_lock(&source->audio_mutex);
  799. /* wait for video to start before outputting any audio so we
  800. * have a base for sync */
  801. if (source->timing_set || !async) {
  802. struct audio_data data;
  803. for (int i = 0; i < MAX_AV_PLANES; i++)
  804. data.data[i] = output->data[i];
  805. data.frames = output->frames;
  806. data.timestamp = output->timestamp;
  807. source_output_audio_line(source, &data);
  808. }
  809. pthread_mutex_unlock(&source->audio_mutex);
  810. }
  811. pthread_mutex_unlock(&source->filter_mutex);
  812. }
  813. static inline bool frame_out_of_bounds(obs_source_t source, uint64_t ts)
  814. {
  815. return ((ts - source->last_frame_ts) > MAX_TIMESTAMP_JUMP);
  816. }
  817. static inline struct source_frame *get_closest_frame(obs_source_t source,
  818. uint64_t sys_time, int *audio_time_refs)
  819. {
  820. struct source_frame *next_frame = source->video_frames.array[0];
  821. struct source_frame *frame = NULL;
  822. uint64_t sys_offset = sys_time - source->last_sys_timestamp;
  823. uint64_t frame_time = next_frame->timestamp;
  824. uint64_t frame_offset = 0;
  825. /* account for timestamp invalidation */
  826. if (frame_out_of_bounds(source, frame_time)) {
  827. source->last_frame_ts = next_frame->timestamp;
  828. (*audio_time_refs)++;
  829. } else {
  830. frame_offset = frame_time - source->last_frame_ts;
  831. source->last_frame_ts += sys_offset;
  832. }
  833. while (frame_offset <= sys_offset) {
  834. source_frame_destroy(frame);
  835. frame = next_frame;
  836. da_erase(source->video_frames, 0);
  837. if (!source->video_frames.num)
  838. break;
  839. next_frame = source->video_frames.array[0];
  840. /* more timestamp checking and compensating */
  841. if ((next_frame->timestamp - frame_time) > MAX_TIMESTAMP_JUMP) {
  842. source->last_frame_ts =
  843. next_frame->timestamp - frame_offset;
  844. (*audio_time_refs)++;
  845. }
  846. frame_time = next_frame->timestamp;
  847. frame_offset = frame_time - source->last_frame_ts;
  848. }
  849. return frame;
  850. }
  851. /*
  852. * Ensures that cached frames are displayed on time. If multiple frames
  853. * were cached between renders, then releases the unnecessary frames and uses
  854. * the frame with the closest timing to ensure sync. Also ensures that timing
  855. * with audio is synchronized.
  856. */
  857. struct source_frame *obs_source_getframe(obs_source_t source)
  858. {
  859. struct source_frame *frame = NULL;
  860. int audio_time_refs = 0;
  861. uint64_t sys_time;
  862. pthread_mutex_lock(&source->video_mutex);
  863. if (!source->video_frames.num)
  864. goto unlock;
  865. sys_time = os_gettime_ns();
  866. if (!source->last_frame_ts) {
  867. frame = source->video_frames.array[0];
  868. da_erase(source->video_frames, 0);
  869. source->last_frame_ts = frame->timestamp;
  870. } else {
  871. frame = get_closest_frame(source, sys_time, &audio_time_refs);
  872. }
  873. /* reset timing to current system time */
  874. if (frame) {
  875. source->audio_reset_ref += audio_time_refs;
  876. source->timing_adjust = sys_time - frame->timestamp;
  877. source->timing_set = true;
  878. }
  879. source->last_sys_timestamp = sys_time;
  880. unlock:
  881. pthread_mutex_unlock(&source->video_mutex);
  882. if (frame)
  883. obs_source_addref(source);
  884. return frame;
  885. }
  886. void obs_source_releaseframe(obs_source_t source, struct source_frame *frame)
  887. {
  888. if (frame) {
  889. source_frame_destroy(frame);
  890. obs_source_release(source);
  891. }
  892. }
  893. const char *obs_source_getname(obs_source_t source)
  894. {
  895. return source->name;
  896. }
  897. void obs_source_setname(obs_source_t source, const char *name)
  898. {
  899. bfree(source->name);
  900. source->name = bstrdup(name);
  901. }
  902. void obs_source_gettype(obs_source_t source, enum obs_source_type *type,
  903. const char **id)
  904. {
  905. if (type) *type = source->info.type;
  906. if (id) *id = source->info.id;
  907. }
  908. static inline void render_filter_bypass(obs_source_t target, effect_t effect,
  909. bool use_matrix)
  910. {
  911. const char *tech_name = use_matrix ? "DrawMatrix" : "Draw";
  912. technique_t tech = effect_gettechnique(effect, tech_name);
  913. size_t passes, i;
  914. passes = technique_begin(tech);
  915. for (i = 0; i < passes; i++) {
  916. technique_beginpass(tech, i);
  917. obs_source_video_render(target);
  918. technique_endpass(tech);
  919. }
  920. technique_end(tech);
  921. }
  922. static inline void render_filter_tex(texture_t tex, effect_t effect,
  923. uint32_t width, uint32_t height, bool use_matrix)
  924. {
  925. const char *tech_name = use_matrix ? "DrawMatrix" : "Draw";
  926. technique_t tech = effect_gettechnique(effect, tech_name);
  927. eparam_t image = effect_getparambyname(effect, "image");
  928. size_t passes, i;
  929. effect_settexture(effect, image, tex);
  930. passes = technique_begin(tech);
  931. for (i = 0; i < passes; i++) {
  932. technique_beginpass(tech, i);
  933. gs_draw_sprite(tex, width, height, 0);
  934. technique_endpass(tech);
  935. }
  936. technique_end(tech);
  937. }
  938. void obs_source_process_filter(obs_source_t filter, effect_t effect,
  939. uint32_t width, uint32_t height, enum gs_color_format format,
  940. enum allow_direct_render allow_direct)
  941. {
  942. obs_source_t target = obs_filter_gettarget(filter);
  943. obs_source_t parent = obs_filter_getparent(filter);
  944. uint32_t target_flags = target->info.output_flags;
  945. uint32_t parent_flags = parent->info.output_flags;
  946. int cx = obs_source_getwidth(target);
  947. int cy = obs_source_getheight(target);
  948. bool use_matrix = !!(target_flags & OBS_SOURCE_COLOR_MATRIX);
  949. bool expects_def = !(parent_flags & OBS_SOURCE_CUSTOM_DRAW);
  950. bool can_directly = allow_direct == ALLOW_DIRECT_RENDERING;
  951. /* if the parent does not use any custom effects, and this is the last
  952. * filter in the chain for the parent, then render the parent directly
  953. * using the filter effect instead of rendering to texture to reduce
  954. * the total number of passes */
  955. if (can_directly && expects_def && target == parent) {
  956. render_filter_bypass(target, effect, use_matrix);
  957. return;
  958. }
  959. if (!filter->filter_texrender)
  960. filter->filter_texrender = texrender_create(format,
  961. GS_ZS_NONE);
  962. if (texrender_begin(filter->filter_texrender, cx, cy)) {
  963. gs_ortho(0.0f, (float)cx, 0.0f, (float)cy, -100.0f, 100.0f);
  964. if (expects_def && parent == target)
  965. obs_source_default_render(parent, use_matrix);
  966. else
  967. obs_source_video_render(target);
  968. texrender_end(filter->filter_texrender);
  969. }
  970. /* --------------------------- */
  971. render_filter_tex(texrender_gettexture(filter->filter_texrender),
  972. effect, width, height, use_matrix);
  973. }
  974. signal_handler_t obs_source_signalhandler(obs_source_t source)
  975. {
  976. return source->signals;
  977. }
  978. proc_handler_t obs_source_prochandler(obs_source_t source)
  979. {
  980. return source ? source->procs : NULL;
  981. }
  982. void obs_source_setvolume(obs_source_t source, float volume)
  983. {
  984. if (source) {
  985. struct calldata data = {0};
  986. calldata_setptr(&data, "source", source);
  987. calldata_setfloat(&data, "volume", volume);
  988. signal_handler_signal(source->signals, "volume", &data);
  989. signal_handler_signal(obs->signals, "source-volume", &data);
  990. volume = calldata_float(&data, "volume");
  991. calldata_free(&data);
  992. source->user_volume = volume;
  993. }
  994. }
  995. static void set_tree_preset_vol(obs_source_t parent, obs_source_t child,
  996. void *param)
  997. {
  998. float *vol = param;
  999. child->present_volume = *vol;
  1000. UNUSED_PARAMETER(parent);
  1001. }
  1002. void obs_source_set_present_volume(obs_source_t source, float volume)
  1003. {
  1004. if (source) {
  1005. source->present_volume = volume;
  1006. /* don't set the presentation volume of the tree if a
  1007. * transition source, let the transition handle presentation
  1008. * volume for the child sources itself. */
  1009. if (source->info.type != OBS_SOURCE_TYPE_TRANSITION)
  1010. obs_source_enum_tree(source, set_tree_preset_vol,
  1011. &volume);
  1012. }
  1013. }
  1014. float obs_source_getvolume(obs_source_t source)
  1015. {
  1016. return source ? source->user_volume : 0.0f;
  1017. }
  1018. float obs_source_get_present_volume(obs_source_t source)
  1019. {
  1020. return source ? source->present_volume : 0.0f;
  1021. }
  1022. void obs_source_set_sync_offset(obs_source_t source, int64_t offset)
  1023. {
  1024. if (source)
  1025. source->sync_offset = offset;
  1026. }
  1027. int64_t obs_source_get_sync_offset(obs_source_t source)
  1028. {
  1029. return source ? source->sync_offset : 0;
  1030. }
  1031. struct source_enum_data {
  1032. obs_source_enum_proc_t enum_callback;
  1033. void *param;
  1034. };
  1035. static void enum_source_tree_callback(obs_source_t parent, obs_source_t child,
  1036. void *param)
  1037. {
  1038. struct source_enum_data *data = param;
  1039. if (child->info.enum_sources && !child->enum_refs) {
  1040. child->enum_refs++;
  1041. child->info.enum_sources(child->data,
  1042. enum_source_tree_callback, data);
  1043. child->enum_refs--;
  1044. }
  1045. data->enum_callback(parent, child, data->param);
  1046. }
  1047. void obs_source_enum_sources(obs_source_t source,
  1048. obs_source_enum_proc_t enum_callback,
  1049. void *param)
  1050. {
  1051. if (!source || !source->info.enum_sources || source->enum_refs)
  1052. return;
  1053. obs_source_addref(source);
  1054. source->enum_refs++;
  1055. source->info.enum_sources(source->data, enum_callback, param);
  1056. source->enum_refs--;
  1057. obs_source_release(source);
  1058. }
  1059. void obs_source_enum_tree(obs_source_t source,
  1060. obs_source_enum_proc_t enum_callback,
  1061. void *param)
  1062. {
  1063. struct source_enum_data data = {enum_callback, param};
  1064. if (!source || !source->info.enum_sources || source->enum_refs)
  1065. return;
  1066. obs_source_addref(source);
  1067. source->enum_refs++;
  1068. source->info.enum_sources(source->data, enum_source_tree_callback,
  1069. &data);
  1070. source->enum_refs--;
  1071. obs_source_release(source);
  1072. }
  1073. void obs_source_add_child(obs_source_t parent, obs_source_t child)
  1074. {
  1075. if (!parent || !child) return;
  1076. for (int i = 0; i < parent->show_refs; i++) {
  1077. enum view_type type;
  1078. type = (i < parent->activate_refs) ? MAIN_VIEW : AUX_VIEW;
  1079. obs_source_activate(child, type);
  1080. }
  1081. }
  1082. void obs_source_remove_child(obs_source_t parent, obs_source_t child)
  1083. {
  1084. if (!parent || !child) return;
  1085. for (int i = 0; i < parent->show_refs; i++) {
  1086. enum view_type type;
  1087. type = (i < parent->activate_refs) ? MAIN_VIEW : AUX_VIEW;
  1088. obs_source_deactivate(child, type);
  1089. }
  1090. }
  1091. static void reset_transition_vol(obs_source_t parent, obs_source_t child,
  1092. void *param)
  1093. {
  1094. child->transition_volume = 0.0f;
  1095. UNUSED_PARAMETER(parent);
  1096. UNUSED_PARAMETER(param);
  1097. }
  1098. static void add_transition_vol(obs_source_t parent, obs_source_t child,
  1099. void *param)
  1100. {
  1101. float *vol = param;
  1102. child->transition_volume += *vol;
  1103. UNUSED_PARAMETER(parent);
  1104. }
  1105. static void apply_transition_vol(obs_source_t parent, obs_source_t child,
  1106. void *param)
  1107. {
  1108. child->present_volume = child->transition_volume;
  1109. UNUSED_PARAMETER(parent);
  1110. UNUSED_PARAMETER(param);
  1111. }
  1112. void obs_transition_begin_frame(obs_source_t transition)
  1113. {
  1114. if (!transition) return;
  1115. obs_source_enum_tree(transition, reset_transition_vol, NULL);
  1116. }
  1117. void obs_source_set_transition_vol(obs_source_t source, float vol)
  1118. {
  1119. if (!source) return;
  1120. add_transition_vol(NULL, source, &vol);
  1121. obs_source_enum_tree(source, add_transition_vol, &vol);
  1122. }
  1123. void obs_transition_end_frame(obs_source_t transition)
  1124. {
  1125. if (!transition) return;
  1126. obs_source_enum_tree(transition, apply_transition_vol, NULL);
  1127. }