obs-source.c 35 KB

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