obs-source.c 30 KB

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