obs-source.c 33 KB

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