obs-source.c 32 KB

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