obs-source.c 29 KB

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