1
0

obs-source.c 28 KB

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