obs-source.c 28 KB

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