obs-source.c 31 KB

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