obs-source.c 31 KB

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