obs-source.c 28 KB

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