obs-source.c 28 KB

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