obs-source.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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 3 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 "obs.h"
  17. #include "obs-data.h"
  18. bool get_source_info(void *module, const char *module_name,
  19. const char *source_name, struct source_info *info)
  20. {
  21. info->create = load_module_subfunc(module, module_name,
  22. source_name,"create", true);
  23. info->destroy = load_module_subfunc(module, module_name,
  24. source_name, "destroy", true);
  25. info->get_output_flags = load_module_subfunc(module, module_name,
  26. source_name, "get_output_flags", true);
  27. if (!info->create || !info->destroy || !info->get_output_flags)
  28. return false;
  29. info->config = load_module_subfunc(module, module_name,
  30. source_name, "config", false);
  31. info->activate = load_module_subfunc(module, module_name,
  32. source_name, "activate", false);
  33. info->deactivate = load_module_subfunc(module, module_name,
  34. source_name, "deactivate", false);
  35. info->video_tick = load_module_subfunc(module, module_name,
  36. source_name, "video_tick", false);
  37. info->video_render = load_module_subfunc(module, module_name,
  38. source_name, "video_render", false);
  39. info->getwidth = load_module_subfunc(module, module_name,
  40. source_name, "getwidth", false);
  41. info->getheight = load_module_subfunc(module, module_name,
  42. source_name, "getheight", false);
  43. info->getparam = load_module_subfunc(module, module_name,
  44. source_name, "getparam", false);
  45. info->setparam = load_module_subfunc(module, module_name,
  46. source_name, "setparam", false);
  47. info->enum_children = load_module_subfunc(module, module_name,
  48. source_name, "enum_children", false);
  49. info->filter_video = load_module_subfunc(module, module_name,
  50. source_name, "filter_video", false);
  51. info->filter_audio = load_module_subfunc(module, module_name,
  52. source_name, "filter_audio", false);
  53. info->name = source_name;
  54. return true;
  55. }
  56. static inline const struct source_info *find_source(struct darray *list,
  57. const char *name)
  58. {
  59. size_t i;
  60. struct source_info *array = list->array;
  61. for (i = 0; i < list->num; i++) {
  62. struct source_info *info = array+i;
  63. if (strcmp(info->name, name) == 0)
  64. return info;
  65. }
  66. return NULL;
  67. }
  68. bool obs_source_init(struct obs_source *source, const char *settings,
  69. const struct source_info *info)
  70. {
  71. uint32_t flags = info->get_output_flags(source->data);
  72. pthread_mutex_init_value(&source->filter_mutex);
  73. pthread_mutex_init_value(&source->video_mutex);
  74. pthread_mutex_init_value(&source->audio_mutex);
  75. dstr_copy(&source->settings, settings);
  76. memcpy(&source->callbacks, info, sizeof(struct source_info));
  77. if (pthread_mutex_init(&source->filter_mutex, NULL) != 0)
  78. return false;
  79. if (pthread_mutex_init(&source->audio_mutex, NULL) != 0)
  80. return false;
  81. if (pthread_mutex_init(&source->video_mutex, NULL) != 0)
  82. return false;
  83. if (flags & SOURCE_AUDIO) {
  84. source->audio_line = audio_output_createline(obs->audio);
  85. if (!source->audio_line) {
  86. blog(LOG_ERROR, "Failed to create audio line for "
  87. "source");
  88. return false;
  89. }
  90. }
  91. source->valid = true;
  92. pthread_mutex_lock(&obs->source_list_mutex);
  93. da_push_back(obs->sources, &source);
  94. pthread_mutex_unlock(&obs->source_list_mutex);
  95. return true;
  96. }
  97. obs_source_t obs_source_create(enum obs_source_type type, const char *name,
  98. const char *settings)
  99. {
  100. const struct source_info *info = NULL;
  101. struct darray *list = NULL;
  102. struct obs_source *source;
  103. switch (type) {
  104. case SOURCE_INPUT: list = &obs->input_types.da; break;
  105. case SOURCE_FILTER: list = &obs->filter_types.da; break;
  106. case SOURCE_TRANSITION: list = &obs->transition_types.da; break;
  107. case SOURCE_SCENE:
  108. default:
  109. return NULL;
  110. }
  111. info = find_source(list, name);
  112. if (!info) {
  113. blog(LOG_WARNING, "Source '%s' not found", type);
  114. return NULL;
  115. }
  116. source = bmalloc(sizeof(struct obs_source));
  117. memset(source, 0, sizeof(struct obs_source));
  118. source->data = info->create(settings, source);
  119. if (!source->data)
  120. goto fail;
  121. if (!obs_source_init(source, settings, info))
  122. goto fail;
  123. return source;
  124. fail:
  125. blog(LOG_ERROR, "obs_source_create failed");
  126. obs_source_destroy(source);
  127. return NULL;
  128. }
  129. void obs_source_destroy(obs_source_t source)
  130. {
  131. if (source) {
  132. size_t i;
  133. if (source->filter_parent)
  134. obs_source_filter_remove(source->filter_parent, source);
  135. for (i = 0; i < source->filters.num; i++)
  136. obs_source_destroy(source->filters.array[i]);
  137. if (source->valid) {
  138. pthread_mutex_lock(&obs->source_list_mutex);
  139. da_erase_item(obs->sources, &source);
  140. pthread_mutex_unlock(&obs->source_list_mutex);
  141. }
  142. for (i = 0; i < source->audio_buffer.num; i++)
  143. audiobuf_free(source->audio_buffer.array+i);
  144. for (i = 0; i < source->video_frames.num; i++)
  145. source_frame_destroy(source->video_frames.array[i]);
  146. gs_entercontext(obs->graphics);
  147. texture_destroy(source->output_texture);
  148. gs_leavecontext();
  149. if (source->data)
  150. source->callbacks.destroy(source->data);
  151. audio_line_destroy(source->audio_line);
  152. da_free(source->video_frames);
  153. da_free(source->audio_buffer);
  154. da_free(source->filters);
  155. pthread_mutex_destroy(&source->filter_mutex);
  156. pthread_mutex_destroy(&source->audio_mutex);
  157. pthread_mutex_destroy(&source->video_mutex);
  158. dstr_free(&source->settings);
  159. bfree(source);
  160. }
  161. }
  162. uint32_t obs_source_get_output_flags(obs_source_t source)
  163. {
  164. return source->callbacks.get_output_flags(source->data);
  165. }
  166. bool obs_source_hasconfig(obs_source_t source)
  167. {
  168. return source->callbacks.config != NULL;
  169. }
  170. void obs_source_config(obs_source_t source, void *parent)
  171. {
  172. if (source->callbacks.config)
  173. source->callbacks.config(source->data, parent);
  174. }
  175. void obs_source_activate(obs_source_t source)
  176. {
  177. if (source->callbacks.activate)
  178. source->callbacks.activate(source->data);
  179. }
  180. void obs_source_deactivate(obs_source_t source)
  181. {
  182. if (source->callbacks.deactivate)
  183. source->callbacks.deactivate(source->data);
  184. }
  185. void obs_source_video_tick(obs_source_t source, float seconds)
  186. {
  187. if (source->callbacks.video_tick)
  188. source->callbacks.video_tick(source->data, seconds);
  189. }
  190. #define MAX_VARIANCE 2000000000ULL
  191. static void source_output_audio_line(obs_source_t source,
  192. const struct audio_data *data)
  193. {
  194. struct audio_data in = *data;
  195. if (!in.timestamp) {
  196. in.timestamp = os_gettime_ns();
  197. if (!source->timing_set) {
  198. source->timing_set = true;
  199. source->timing_adjust = 0;
  200. }
  201. }
  202. if (!source->timing_set) {
  203. source->timing_set = true;
  204. source->timing_adjust = in.timestamp - os_gettime_ns();
  205. /* detects 'directly' set timestamps as long as they're within
  206. * a certain threashold */
  207. if ((source->timing_adjust+MAX_VARIANCE) < MAX_VARIANCE*2)
  208. source->timing_adjust = 0;
  209. }
  210. in.timestamp += source->timing_adjust;
  211. audio_line_output(source->audio_line, &in);
  212. }
  213. static void obs_source_flush_audio_buffer(obs_source_t source)
  214. {
  215. size_t i;
  216. pthread_mutex_lock(&source->audio_mutex);
  217. source->timing_set = true;
  218. for (i = 0; i < source->audio_buffer.num; i++) {
  219. struct audiobuf *buf = source->audio_buffer.array+i;
  220. struct audio_data data;
  221. data.data = buf->data;
  222. data.frames = buf->frames;
  223. data.timestamp = buf->timestamp + source->timing_adjust;
  224. audio_line_output(source->audio_line, &data);
  225. audiobuf_free(buf);
  226. }
  227. da_free(source->audio_buffer);
  228. pthread_mutex_unlock(&source->audio_mutex);
  229. }
  230. static bool set_texture_size(obs_source_t source, struct source_frame *frame)
  231. {
  232. if (source->output_texture) {
  233. uint32_t width = texture_getwidth(source->output_texture);
  234. uint32_t height = texture_getheight(source->output_texture);
  235. if (width == frame->width && height == frame->height)
  236. return true;
  237. }
  238. texture_destroy(source->output_texture);
  239. source->output_texture = gs_create_texture(frame->width, frame->height,
  240. GS_RGBA, 1, NULL, GS_DYNAMIC);
  241. return source->output_texture != NULL;
  242. }
  243. enum convert_type {
  244. CONVERT_NONE,
  245. CONVERT_NV12,
  246. CONVERT_420,
  247. CONVERT_422_U,
  248. CONVERT_422_Y,
  249. };
  250. static inline enum convert_type get_convert_type(enum video_type type)
  251. {
  252. switch (type) {
  253. case VIDEO_FORMAT_I420:
  254. return CONVERT_420;
  255. case VIDEO_FORMAT_NV12:
  256. return CONVERT_NV12;
  257. case VIDEO_FORMAT_YVYU:
  258. case VIDEO_FORMAT_YUY2:
  259. return CONVERT_422_Y;
  260. case VIDEO_FORMAT_UYVY:
  261. return CONVERT_422_U;
  262. case VIDEO_FORMAT_UNKNOWN:
  263. case VIDEO_FORMAT_YUVX:
  264. case VIDEO_FORMAT_UYVX:
  265. case VIDEO_FORMAT_RGBA:
  266. case VIDEO_FORMAT_BGRA:
  267. case VIDEO_FORMAT_BGRX:
  268. return CONVERT_NONE;
  269. }
  270. return CONVERT_NONE;
  271. }
  272. static inline bool is_yuv(enum video_type type)
  273. {
  274. switch (type) {
  275. case VIDEO_FORMAT_I420:
  276. case VIDEO_FORMAT_NV12:
  277. case VIDEO_FORMAT_YVYU:
  278. case VIDEO_FORMAT_YUY2:
  279. case VIDEO_FORMAT_UYVY:
  280. case VIDEO_FORMAT_YUVX:
  281. case VIDEO_FORMAT_UYVX:
  282. return true;
  283. case VIDEO_FORMAT_UNKNOWN:
  284. case VIDEO_FORMAT_RGBA:
  285. case VIDEO_FORMAT_BGRA:
  286. case VIDEO_FORMAT_BGRX:
  287. return false;
  288. }
  289. return false;
  290. }
  291. static bool upload_frame(texture_t tex, const struct source_frame *frame)
  292. {
  293. void *ptr;
  294. uint32_t row_bytes;
  295. enum convert_type type = get_convert_type(frame->type);
  296. if (type == CONVERT_NONE) {
  297. texture_setimage(tex, frame->data, frame->row_bytes, false);
  298. return true;
  299. }
  300. if (!texture_map(tex, &ptr, &row_bytes))
  301. return false;
  302. if (type == CONVERT_420)
  303. decompress_420(frame->data, frame->width, frame->height,
  304. frame->row_bytes, 0, frame->height, ptr);
  305. else if (type == CONVERT_NV12)
  306. decompress_nv12(frame->data, frame->width, frame->height,
  307. frame->row_bytes, 0, frame->height, ptr);
  308. else if (type == CONVERT_422_Y)
  309. decompress_422(frame->data, frame->width, frame->height,
  310. frame->row_bytes, 0, frame->height, ptr, true);
  311. else if (type == CONVERT_422_U)
  312. decompress_422(frame->data, frame->width, frame->height,
  313. frame->row_bytes, 0, frame->height, ptr, false);
  314. texture_unmap(tex);
  315. return true;
  316. }
  317. static void obs_source_draw_texture(texture_t tex, struct source_frame *frame)
  318. {
  319. effect_t effect = obs->default_effect;
  320. bool yuv = is_yuv(frame->type);
  321. const char *type = yuv ? "DrawYUVToRGB" : "DrawRGB";
  322. technique_t tech;
  323. eparam_t param;
  324. if (!upload_frame(tex, frame))
  325. return;
  326. tech = effect_gettechnique(effect, type);
  327. technique_begin(tech);
  328. technique_beginpass(tech, 0);
  329. if (yuv) {
  330. param = effect_getparambyname(effect, "yuv_matrix");
  331. effect_setval(effect, param, frame->yuv_matrix,
  332. sizeof(float) * 16);
  333. }
  334. param = effect_getparambyname(effect, "diffuse");
  335. effect_settexture(effect, param, tex);
  336. gs_draw_sprite(tex, frame->flip ? GS_FLIP_V : 0);
  337. technique_endpass(tech);
  338. technique_end(tech);
  339. }
  340. static void obs_source_render_async_video(obs_source_t source)
  341. {
  342. struct source_frame *frame = obs_source_getframe(source);
  343. if (!frame)
  344. return;
  345. source->timing_adjust = frame->timestamp - os_gettime_ns();
  346. if (!source->timing_set && source->audio_buffer.num)
  347. obs_source_flush_audio_buffer(source);
  348. if (set_texture_size(source, frame))
  349. obs_source_draw_texture(source->output_texture, frame);
  350. obs_source_releaseframe(source, frame);
  351. }
  352. void obs_source_video_render(obs_source_t source)
  353. {
  354. if (source->callbacks.video_render) {
  355. if (source->filters.num && !source->rendering_filter) {
  356. source->rendering_filter = true;
  357. obs_source_video_render(source->filters.array[0]);
  358. source->rendering_filter = false;
  359. } else {
  360. source->callbacks.video_render(source->data);
  361. }
  362. } else if (source->filter_target) {
  363. obs_source_video_render(source->filter_target);
  364. } else {
  365. obs_source_render_async_video(source);
  366. }
  367. }
  368. uint32_t obs_source_getwidth(obs_source_t source)
  369. {
  370. if (source->callbacks.getwidth)
  371. return source->callbacks.getwidth(source->data);
  372. return 0;
  373. }
  374. uint32_t obs_source_getheight(obs_source_t source)
  375. {
  376. if (source->callbacks.getheight)
  377. return source->callbacks.getheight(source->data);
  378. return 0;
  379. }
  380. size_t obs_source_getparam(obs_source_t source, const char *param, void *buf,
  381. size_t buf_size)
  382. {
  383. if (source->callbacks.getparam)
  384. return source->callbacks.getparam(source->data, param, buf,
  385. buf_size);
  386. return 0;
  387. }
  388. void obs_source_setparam(obs_source_t source, const char *param,
  389. const void *data, size_t size)
  390. {
  391. if (source->callbacks.setparam)
  392. source->callbacks.setparam(source->data, param, data, size);
  393. }
  394. bool obs_source_enum_children(obs_source_t source, size_t idx,
  395. obs_source_t *child)
  396. {
  397. if (source->callbacks.enum_children)
  398. return source->callbacks.enum_children(source, idx, child);
  399. return false;
  400. }
  401. obs_source_t obs_filter_gettarget(obs_source_t filter)
  402. {
  403. return filter->filter_target;
  404. }
  405. void obs_source_filter_add(obs_source_t source, obs_source_t filter)
  406. {
  407. pthread_mutex_lock(&source->filter_mutex);
  408. if (da_find(source->filters, &filter, 0) != DARRAY_INVALID) {
  409. blog(LOG_WARNING, "Tried to add a filter that was already "
  410. "present on the source");
  411. return;
  412. }
  413. if (source->filters.num) {
  414. obs_source_t *back = da_end(source->filters);
  415. (*back)->filter_target = filter;
  416. }
  417. da_push_back(source->filters, &filter);
  418. pthread_mutex_unlock(&source->filter_mutex);
  419. filter->filter_parent = source;
  420. filter->filter_target = source;
  421. }
  422. void obs_source_filter_remove(obs_source_t source, obs_source_t filter)
  423. {
  424. size_t idx;
  425. pthread_mutex_lock(&source->filter_mutex);
  426. idx = da_find(source->filters, &filter, 0);
  427. if (idx == DARRAY_INVALID)
  428. return;
  429. if (idx > 0) {
  430. obs_source_t prev = source->filters.array[idx-1];
  431. prev->filter_target = filter->filter_target;
  432. }
  433. da_erase(source->filters, idx);
  434. pthread_mutex_unlock(&source->filter_mutex);
  435. filter->filter_parent = NULL;
  436. filter->filter_target = NULL;
  437. }
  438. void obs_source_filter_setorder(obs_source_t source, obs_source_t filter,
  439. enum order_movement movement)
  440. {
  441. size_t idx = da_find(source->filters, &filter, 0);
  442. size_t i;
  443. if (idx == DARRAY_INVALID)
  444. return;
  445. if (movement == ORDER_MOVE_UP) {
  446. if (idx == source->filters.num-1)
  447. return;
  448. da_move_item(source->filters, idx, idx+1);
  449. } else if (movement == ORDER_MOVE_DOWN) {
  450. if (idx == 0)
  451. return;
  452. da_move_item(source->filters, idx, idx-1);
  453. } else if (movement == ORDER_MOVE_TOP) {
  454. if (idx == source->filters.num-1)
  455. return;
  456. da_move_item(source->filters, idx, source->filters.num-1);
  457. } else if (movement == ORDER_MOVE_BOTTOM) {
  458. if (idx == 0)
  459. return;
  460. da_move_item(source->filters, idx, 0);
  461. }
  462. /* reorder filter targets */
  463. for (i = 0; i < source->filters.num; i++) {
  464. obs_source_t next_filter = (i == source->filters.num-1) ?
  465. source : source->filters.array[idx+1];
  466. source->filters.array[i]->filter_target = next_filter;
  467. }
  468. }
  469. const char *obs_source_get_settings(obs_source_t source)
  470. {
  471. return source->settings.array;
  472. }
  473. void obs_source_save_settings(obs_source_t source, const char *settings)
  474. {
  475. dstr_copy(&source->settings, settings);
  476. }
  477. static inline struct source_frame *filter_async_video(obs_source_t source,
  478. struct source_frame *in)
  479. {
  480. size_t i;
  481. for (i = source->filters.num; i > 0; i--) {
  482. struct obs_source *filter = source->filters.array[i-1];
  483. if (filter->callbacks.filter_video) {
  484. in = filter->callbacks.filter_video(filter->data, in);
  485. if (!in)
  486. return NULL;
  487. }
  488. }
  489. return in;
  490. }
  491. static inline struct source_frame *cache_video(obs_source_t source,
  492. const struct source_frame *frame)
  493. {
  494. /* TODO: use an actual cache */
  495. struct source_frame *new_frame = bmalloc(sizeof(struct source_frame));
  496. memcpy(new_frame, frame, sizeof(struct source_frame));
  497. new_frame->data = bmalloc(frame->row_bytes * frame->height);
  498. return new_frame;
  499. }
  500. void obs_source_output_video(obs_source_t source,
  501. const struct source_frame *frame)
  502. {
  503. struct source_frame *output = cache_video(source, frame);
  504. pthread_mutex_lock(&source->filter_mutex);
  505. output = filter_async_video(source, output);
  506. pthread_mutex_unlock(&source->filter_mutex);
  507. if (output) {
  508. pthread_mutex_lock(&source->video_mutex);
  509. da_push_back(source->video_frames, &output);
  510. pthread_mutex_unlock(&source->video_mutex);
  511. }
  512. }
  513. static inline const struct audio_data *filter_async_audio(obs_source_t source,
  514. const struct audio_data *in)
  515. {
  516. size_t i;
  517. for (i = source->filters.num; i > 0; i--) {
  518. struct obs_source *filter = source->filters.array[i-1];
  519. if (filter->callbacks.filter_audio) {
  520. in = filter->callbacks.filter_audio(filter->data, in);
  521. if (!in)
  522. return NULL;
  523. }
  524. }
  525. return in;
  526. }
  527. static struct audio_data *process_audio(obs_source_t source,
  528. const struct source_audio *audio)
  529. {
  530. /* TODO: upmix/downmix/resample */
  531. return NULL;
  532. }
  533. void obs_source_output_audio(obs_source_t source,
  534. const struct source_audio *audio)
  535. {
  536. uint32_t flags = obs_source_get_output_flags(source);
  537. struct audio_data *data;
  538. const struct audio_data *output;
  539. data = process_audio(source, audio);
  540. pthread_mutex_lock(&source->filter_mutex);
  541. output = filter_async_audio(source, data);
  542. if (output) {
  543. pthread_mutex_lock(&source->audio_mutex);
  544. if (!source->timing_set && flags & SOURCE_ASYNC_VIDEO) {
  545. struct audiobuf newbuf;
  546. size_t audio_size = audio_output_blocksize(obs->audio) *
  547. output->frames;
  548. newbuf.data = bmalloc(audio_size);
  549. newbuf.frames = output->frames;
  550. newbuf.timestamp = output->timestamp;
  551. memcpy(newbuf.data, output->data, audio_size);
  552. da_push_back(source->audio_buffer, &newbuf);
  553. } else {
  554. source_output_audio_line(source, output);
  555. }
  556. pthread_mutex_unlock(&source->audio_mutex);
  557. }
  558. pthread_mutex_unlock(&source->filter_mutex);
  559. }
  560. /*
  561. * Ensures that cached frames are displayed on time. If multiple frames
  562. * were cached between renders, then releases the unnecessary frames and uses
  563. * the frame with the closest timing.
  564. */
  565. struct source_frame *obs_source_getframe(obs_source_t source)
  566. {
  567. uint64_t last_frame_time = source->last_frame_timestamp;
  568. struct source_frame *frame = NULL;
  569. struct source_frame *next_frame;
  570. uint64_t sys_time, frame_time;
  571. pthread_mutex_lock(&source->video_mutex);
  572. if (!source->video_frames.num)
  573. goto unlock;
  574. next_frame = source->video_frames.array[0];
  575. sys_time = os_gettime_ns();
  576. frame_time = next_frame->timestamp;
  577. if (!source->last_frame_timestamp) {
  578. frame = next_frame;
  579. da_erase(source->video_frames, 0);
  580. source->last_frame_timestamp = frame_time;
  581. } else {
  582. uint64_t sys_offset, frame_offset;
  583. sys_offset = sys_time - source->last_sys_timestamp;
  584. frame_offset = frame_time - last_frame_time;
  585. source->last_frame_timestamp += sys_offset;
  586. while (frame_offset <= sys_offset) {
  587. if (frame)
  588. source_frame_destroy(frame);
  589. frame = next_frame;
  590. da_erase(source->video_frames, 0);
  591. if (!source->video_frames.num)
  592. break;
  593. next_frame = source->video_frames.array[0];
  594. frame_time = next_frame->timestamp;
  595. frame_offset = frame_time - last_frame_time;
  596. }
  597. }
  598. source->last_sys_timestamp = sys_time;
  599. unlock:
  600. pthread_mutex_unlock(&source->video_mutex);
  601. return frame;
  602. }
  603. void obs_source_releaseframe(obs_source_t source, struct source_frame *frame)
  604. {
  605. source_frame_destroy(frame);
  606. }