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