obs-video.c 18.7 KB
Newer Older
J
jp9000 已提交
1
/******************************************************************************
2
    Copyright (C) 2013-2014 by Hugh Bailey <obs.jim@gmail.com>
J
jp9000 已提交
3 4 5

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
6
    the Free Software Foundation, either version 2 of the License, or
J
jp9000 已提交
7 8 9 10 11 12 13 14 15 16 17
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

18 19 20
#include <time.h>
#include <stdlib.h>

J
jp9000 已提交
21
#include "obs.h"
J
jp9000 已提交
22
#include "obs-internal.h"
J
jp9000 已提交
23
#include "graphics/vec4.h"
24
#include "media-io/format-conversion.h"
25
#include "media-io/video-frame.h"
J
jp9000 已提交
26

J
jp9000 已提交
27
static uint64_t tick_sources(uint64_t cur_time, uint64_t last_time)
J
jp9000 已提交
28
{
29 30 31 32
	struct obs_core_data *data = &obs->data;
	struct obs_source    *source;
	uint64_t             delta_time;
	float                seconds;
J
jp9000 已提交
33 34

	if (!last_time)
35 36 37
		last_time = cur_time -
			video_output_get_frame_time(obs->video.video);

J
jp9000 已提交
38
	delta_time = cur_time - last_time;
J
jp9000 已提交
39 40
	seconds = (float)((double)delta_time / 1000000000.0);

41 42 43 44 45 46 47 48 49 50
	/* ------------------------------------- */
	/* call tick callbacks                   */

	pthread_mutex_lock(&obs->data.draw_callbacks_mutex);

	for (size_t i = obs->data.tick_callbacks.num; i > 0; i--) {
		struct tick_callback *callback;
		callback = obs->data.tick_callbacks.array + (i - 1);
		callback->tick(callback->param, seconds);
	}
J
jp9000 已提交
51

52 53 54
	pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);

	/* ------------------------------------- */
55
	/* call the tick function of each source */
56 57 58

	pthread_mutex_lock(&data->sources_mutex);

59 60
	source = data->first_source;
	while (source) {
61
		obs_source_video_tick(source, seconds);
62 63 64 65 66 67
		source = (struct obs_source*)source->context.next;
	}

	pthread_mutex_unlock(&data->sources_mutex);

	return cur_time;
J
jp9000 已提交
68 69
}

70 71
/* in obs-display.c */
extern void render_display(struct obs_display *display);
J
jp9000 已提交
72

73 74
static inline void render_displays(void)
{
75 76
	struct obs_display *display;

77 78 79
	if (!obs->data.valid)
		return;

80
	gs_enter_context(obs->video.graphics);
81

82 83
	/* render extra displays/swaps */
	pthread_mutex_lock(&obs->data.displays_mutex);
84

85 86 87 88 89
	display = obs->data.first_display;
	while (display) {
		render_display(display);
		display = display->next;
	}
90

91
	pthread_mutex_unlock(&obs->data.displays_mutex);
92

93
	gs_leave_context();
94 95
}

96
static inline void set_render_size(uint32_t width, uint32_t height)
J
jp9000 已提交
97
{
98 99
	gs_enable_depth_test(false);
	gs_set_cull_mode(GS_NEITHER);
100

101
	gs_ortho(0.0f, (float)width, 0.0f, (float)height, -100.0f, 100.0f);
102
	gs_set_viewport(0, 0, width, height);
103 104
}

105
static inline void unmap_last_surface(struct obs_core_video *video)
106 107
{
	if (video->mapped_surface) {
108
		gs_stagesurface_unmap(video->mapped_surface);
109
		video->mapped_surface = NULL;
J
jp9000 已提交
110
	}
111
}
J
jp9000 已提交
112

P
Palana 已提交
113
static const char *render_main_texture_name = "render_main_texture";
114
static inline void render_main_texture(struct obs_core_video *video,
J
jp9000 已提交
115
		int cur_texture)
116
{
P
Palana 已提交
117 118
	profile_start(render_main_texture_name);

119
	struct vec4 clear_color;
J
jp9000 已提交
120
	vec4_set(&clear_color, 0.0f, 0.0f, 0.0f, 1.0f);
J
jp9000 已提交
121

122
	gs_set_render_target(video->render_textures[cur_texture], NULL);
123
	gs_clear(GS_CLEAR_COLOR, &clear_color, 1.0f, 0);
J
jp9000 已提交
124

125
	set_render_size(video->base_width, video->base_height);
126 127 128

	pthread_mutex_lock(&obs->data.draw_callbacks_mutex);

129
	for (size_t i = obs->data.draw_callbacks.num; i > 0; i--) {
130
		struct draw_callback *callback;
131
		callback = obs->data.draw_callbacks.array + (i - 1);
132 133 134 135 136 137 138

		callback->draw(callback->param,
				video->base_width, video->base_height);
	}

	pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);

J
jp9000 已提交
139
	obs_view_render(&obs->data.main_view);
J
jp9000 已提交
140

141
	video->textures_rendered[cur_texture] = true;
P
Palana 已提交
142 143

	profile_end(render_main_texture_name);
144 145
}

146 147 148
static inline gs_effect_t *get_scale_effect_internal(
		struct obs_core_video *video)
{
149 150 151 152 153 154 155 156
	/* if the dimension is under half the size of the original image,
	 * bicubic/lanczos can't sample enough pixels to create an accurate
	 * image, so use the bilinear low resolution effect instead */
	if (video->output_width  < (video->base_width  / 2) &&
	    video->output_height < (video->base_height / 2)) {
		return video->bilinear_lowres_effect;
	}

157 158 159
	switch (video->scale_type) {
	case OBS_SCALE_BILINEAR: return video->default_effect;
	case OBS_SCALE_LANCZOS:  return video->lanczos_effect;
160 161
	case OBS_SCALE_BICUBIC:
	default:;
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	}

	return video->bicubic_effect;
}

static inline bool resolution_close(struct obs_core_video *video,
		uint32_t width, uint32_t height)
{
	long width_cmp  = (long)video->base_width  - (long)width;
	long height_cmp = (long)video->base_height - (long)height;

	return labs(width_cmp) <= 16 && labs(height_cmp) <= 16;
}

static inline gs_effect_t *get_scale_effect(struct obs_core_video *video,
		uint32_t width, uint32_t height)
{
	if (resolution_close(video, width, height)) {
		return video->default_effect;
	} else {
		/* if the scale method couldn't be loaded, use either bicubic
		 * or bilinear by default */
		gs_effect_t *effect = get_scale_effect_internal(video);
		if (!effect)
			effect = !!video->bicubic_effect ?
				video->bicubic_effect :
				video->default_effect;
		return effect;
	}
}

P
Palana 已提交
193
static const char *render_output_texture_name = "render_output_texture";
194
static inline void render_output_texture(struct obs_core_video *video,
195 196
		int cur_texture, int prev_texture)
{
P
Palana 已提交
197 198
	profile_start(render_output_texture_name);

199 200
	gs_texture_t *texture = video->render_textures[prev_texture];
	gs_texture_t *target  = video->output_textures[cur_texture];
201 202
	uint32_t     width   = gs_texture_get_width(target);
	uint32_t     height  = gs_texture_get_height(target);
203 204 205 206 207
	struct vec2  base_i;

	vec2_set(&base_i,
		1.0f / (float)video->base_width,
		1.0f / (float)video->base_height);
208

209
	gs_effect_t    *effect  = get_scale_effect(video, width, height);
210 211 212
	gs_technique_t *tech    = gs_effect_get_technique(effect, "DrawMatrix");
	gs_eparam_t    *image   = gs_effect_get_param_by_name(effect, "image");
	gs_eparam_t    *matrix  = gs_effect_get_param_by_name(effect,
213
			"color_matrix");
214 215
	gs_eparam_t    *bres_i  = gs_effect_get_param_by_name(effect,
			"base_dimension_i");
216 217 218
	size_t      passes, i;

	if (!video->textures_rendered[prev_texture])
P
Palana 已提交
219
		goto end;
220

221
	gs_set_render_target(target, NULL);
222 223
	set_render_size(width, height);

224 225 226
	if (bres_i)
		gs_effect_set_vec2(bres_i, &base_i);

227
	gs_effect_set_val(matrix, video->color_matrix, sizeof(float) * 16);
228
	gs_effect_set_texture(image, texture);
229

230
	gs_enable_blending(false);
231
	passes = gs_technique_begin(tech);
232
	for (i = 0; i < passes; i++) {
233
		gs_technique_begin_pass(tech, i);
234
		gs_draw_sprite(texture, 0, width, height);
235
		gs_technique_end_pass(tech);
236
	}
237
	gs_technique_end(tech);
238
	gs_enable_blending(true);
239 240

	video->textures_output[cur_texture] = true;
P
Palana 已提交
241 242 243

end:
	profile_end(render_output_texture_name);
244 245
}

246
static inline void set_eparam(gs_effect_t *effect, const char *name, float val)
J
jp9000 已提交
247
{
248
	gs_eparam_t *param = gs_effect_get_param_by_name(effect, name);
249
	gs_effect_set_float(param, val);
J
jp9000 已提交
250 251
}

P
Palana 已提交
252
static const char *render_convert_texture_name = "render_convert_texture";
J
jp9000 已提交
253
static void render_convert_texture(struct obs_core_video *video,
254 255
		int cur_texture, int prev_texture)
{
P
Palana 已提交
256 257
	profile_start(render_convert_texture_name);

258 259
	gs_texture_t *texture = video->output_textures[prev_texture];
	gs_texture_t *target  = video->convert_textures[cur_texture];
260 261 262 263
	float        fwidth  = (float)video->output_width;
	float        fheight = (float)video->output_height;
	size_t       passes, i;

264 265 266
	gs_effect_t    *effect  = video->conversion_effect;
	gs_eparam_t    *image   = gs_effect_get_param_by_name(effect, "image");
	gs_technique_t *tech    = gs_effect_get_technique(effect,
J
jp9000 已提交
267
			video->conversion_tech);
268 269

	if (!video->textures_output[prev_texture])
P
Palana 已提交
270
		goto end;
271

J
jp9000 已提交
272 273 274 275 276 277 278 279 280 281 282 283
	set_eparam(effect, "u_plane_offset", (float)video->plane_offsets[1]);
	set_eparam(effect, "v_plane_offset", (float)video->plane_offsets[2]);
	set_eparam(effect, "width",  fwidth);
	set_eparam(effect, "height", fheight);
	set_eparam(effect, "width_i",  1.0f / fwidth);
	set_eparam(effect, "height_i", 1.0f / fheight);
	set_eparam(effect, "width_d2",  fwidth  * 0.5f);
	set_eparam(effect, "height_d2", fheight * 0.5f);
	set_eparam(effect, "width_d2_i",  1.0f / (fwidth  * 0.5f));
	set_eparam(effect, "height_d2_i", 1.0f / (fheight * 0.5f));
	set_eparam(effect, "input_height", (float)video->conversion_height);

284
	gs_effect_set_texture(image, texture);
J
jp9000 已提交
285

286
	gs_set_render_target(target, NULL);
J
jp9000 已提交
287 288
	set_render_size(video->output_width, video->conversion_height);

289
	gs_enable_blending(false);
290
	passes = gs_technique_begin(tech);
J
jp9000 已提交
291
	for (i = 0; i < passes; i++) {
292
		gs_technique_begin_pass(tech, i);
J
jp9000 已提交
293 294
		gs_draw_sprite(texture, 0, video->output_width,
				video->conversion_height);
295
		gs_technique_end_pass(tech);
J
jp9000 已提交
296
	}
297
	gs_technique_end(tech);
298
	gs_enable_blending(true);
J
jp9000 已提交
299 300

	video->textures_converted[cur_texture] = true;
P
Palana 已提交
301 302 303

end:
	profile_end(render_convert_texture_name);
J
jp9000 已提交
304 305
}

P
Palana 已提交
306
static const char *stage_output_texture_name = "stage_output_texture";
J
jp9000 已提交
307 308 309
static inline void stage_output_texture(struct obs_core_video *video,
		int cur_texture, int prev_texture)
{
P
Palana 已提交
310 311
	profile_start(stage_output_texture_name);

312
	gs_texture_t   *texture;
J
jp9000 已提交
313
	bool        texture_ready;
314
	gs_stagesurf_t *copy = video->copy_surfaces[cur_texture];
J
jp9000 已提交
315 316 317 318 319 320

	if (video->gpu_conversion) {
		texture = video->convert_textures[prev_texture];
		texture_ready = video->textures_converted[prev_texture];
	} else {
		texture = video->output_textures[prev_texture];
321
		texture_ready = video->textures_output[prev_texture];
J
jp9000 已提交
322 323 324 325 326
	}

	unmap_last_surface(video);

	if (!texture_ready)
P
Palana 已提交
327
		goto end;
J
jp9000 已提交
328

329 330 331
	gs_stage_texture(copy, texture);

	video->textures_copied[cur_texture] = true;
P
Palana 已提交
332 333 334

end:
	profile_end(stage_output_texture_name);
335 336
}

337
static inline void render_video(struct obs_core_video *video, int cur_texture,
338
		int prev_texture)
339
{
340
	gs_begin_scene();
341

342 343
	gs_enable_depth_test(false);
	gs_set_cull_mode(GS_NEITHER);
344

J
jp9000 已提交
345
	render_main_texture(video, cur_texture);
346
	render_output_texture(video, cur_texture, prev_texture);
J
jp9000 已提交
347 348 349
	if (video->gpu_conversion)
		render_convert_texture(video, cur_texture, prev_texture);

350 351
	stage_output_texture(video, cur_texture, prev_texture);

352
	gs_set_render_target(NULL, NULL);
J
jp9000 已提交
353
	gs_enable_blending(true);
354

355
	gs_end_scene();
356 357
}

J
jp9000 已提交
358
static inline bool download_frame(struct obs_core_video *video,
359
		int prev_texture, struct video_data *frame)
360
{
361
	gs_stagesurf_t *surface = video->copy_surfaces[prev_texture];
362 363

	if (!video->textures_copied[prev_texture])
364
		return false;
365

366
	if (!gs_stagesurface_map(surface, &frame->data[0], &frame->linesize[0]))
367 368 369 370 371
		return false;

	video->mapped_surface = surface;
	return true;
}
372

J
jp9000 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
static inline uint32_t calc_linesize(uint32_t pos, uint32_t linesize)
{
	uint32_t size = pos % linesize;
	return size ? size : linesize;
}

static void copy_dealign(
		uint8_t *dst, uint32_t dst_pos, uint32_t dst_linesize,
		const uint8_t *src, uint32_t src_pos, uint32_t src_linesize,
		uint32_t remaining)
{
	while (remaining) {
		uint32_t src_remainder = src_pos % src_linesize;
		uint32_t dst_offset = dst_linesize - src_remainder;
		uint32_t src_offset = src_linesize - src_remainder;

		if (remaining < dst_offset) {
			memcpy(dst + dst_pos, src + src_pos, remaining);
			src_pos += remaining;
			dst_pos += remaining;
			remaining = 0;
		} else {
			memcpy(dst + dst_pos, src + src_pos, dst_offset);
			src_pos += src_offset;
			dst_pos += dst_offset;
			remaining -= dst_offset;
		}
	}
}

static inline uint32_t make_aligned_linesize_offset(uint32_t offset,
		uint32_t dst_linesize, uint32_t src_linesize)
{
	uint32_t remainder = offset % dst_linesize;
	return (offset / dst_linesize) * src_linesize + remainder;
}

static void fix_gpu_converted_alignment(struct obs_core_video *video,
411
		struct video_frame *output, const struct video_data *input)
J
jp9000 已提交
412
{
413
	uint32_t src_linesize = input->linesize[0];
414
	uint32_t dst_linesize = output->linesize[0] * 4;
J
jp9000 已提交
415 416 417 418 419 420 421 422 423
	uint32_t src_pos      = 0;

	for (size_t i = 0; i < 3; i++) {
		if (video->plane_linewidth[i] == 0)
			break;

		src_pos = make_aligned_linesize_offset(video->plane_offsets[i],
				dst_linesize, src_linesize);

424 425
		copy_dealign(output->data[i], 0, dst_linesize,
				input->data[0], src_pos, src_linesize,
J
jp9000 已提交
426 427 428 429
				video->plane_sizes[i]);
	}
}

430 431 432
static void set_gpu_converted_data(struct obs_core_video *video,
		struct video_frame *output, const struct video_data *input,
		const struct video_output_info *info)
J
jp9000 已提交
433
{
434 435 436
	if (input->linesize[0] == video->output_width*4) {
		struct video_frame frame;

J
jp9000 已提交
437 438 439 440
		for (size_t i = 0; i < 3; i++) {
			if (video->plane_linewidth[i] == 0)
				break;

441 442 443
			frame.linesize[i] = video->plane_linewidth[i];
			frame.data[i] =
				input->data[0] + video->plane_offsets[i];
J
jp9000 已提交
444 445
		}

446 447
		video_frame_copy(output, &frame, info->format, info->height);

J
jp9000 已提交
448
	} else {
449
		fix_gpu_converted_alignment(video, output, input);
J
jp9000 已提交
450 451 452
	}
}

453 454 455
static void convert_frame(
		struct video_frame *output, const struct video_data *input,
		const struct video_output_info *info)
456 457 458
{
	if (info->format == VIDEO_FORMAT_I420) {
		compress_uyvx_to_i420(
459
				input->data[0], input->linesize[0],
460
				0, info->height,
461
				output->data, output->linesize);
462 463 464

	} else if (info->format == VIDEO_FORMAT_NV12) {
		compress_uyvx_to_nv12(
465
				input->data[0], input->linesize[0],
J
jp9000 已提交
466 467 468 469 470 471
				0, info->height,
				output->data, output->linesize);

	} else if (info->format == VIDEO_FORMAT_I444) {
		convert_uyvx_to_i444(
				input->data[0], input->linesize[0],
472
				0, info->height,
473
				output->data, output->linesize);
474 475

	} else {
J
jp9000 已提交
476
		blog(LOG_ERROR, "convert_frame: unsupported texture format");
477 478 479
	}
}

J
jp9000 已提交
480 481 482 483 484 485 486
static inline void copy_rgbx_frame(
		struct video_frame *output, const struct video_data *input,
		const struct video_output_info *info)
{
	uint8_t *in_ptr = input->data[0];
	uint8_t *out_ptr = output->data[0];

487 488 489 490 491 492 493 494 495
	/* if the line sizes match, do a single copy */
	if (input->linesize[0] == output->linesize[0]) {
		memcpy(out_ptr, in_ptr, input->linesize[0] * info->height);
	} else {
		for (size_t y = 0; y < info->height; y++) {
			memcpy(out_ptr, in_ptr, info->width * 4);
			in_ptr += input->linesize[0];
			out_ptr += output->linesize[0];
		}
J
jp9000 已提交
496 497 498
	}
}

499
static inline void output_video_data(struct obs_core_video *video,
500
		struct video_data *input_frame, int count)
501 502
{
	const struct video_output_info *info;
503 504 505
	struct video_frame output_frame;
	bool locked;

506
	info = video_output_get_info(video->video);
507

508 509 510 511 512 513 514 515 516
	locked = video_output_lock_frame(video->video, &output_frame, count,
			input_frame->timestamp);
	if (locked) {
		if (video->gpu_conversion) {
			set_gpu_converted_data(video, &output_frame,
					input_frame, info);

		} else if (format_is_yuv(info->format)) {
			convert_frame(&output_frame, input_frame, info);
J
jp9000 已提交
517 518
		} else {
			copy_rgbx_frame(&output_frame, input_frame, info);
519
		}
J
jp9000 已提交
520

521
		video_output_unlock_frame(video->video);
J
jp9000 已提交
522
	}
523
}
524

525 526 527 528 529 530 531 532
static inline void video_sleep(struct obs_core_video *video,
		uint64_t *p_time, uint64_t interval_ns)
{
	struct obs_vframe_info vframe_info;
	uint64_t cur_time = *p_time;
	uint64_t t = cur_time + interval_ns;
	int count;

533
	if (os_sleepto_ns(t)) {
534 535 536 537 538 539 540
		*p_time = t;
		count = 1;
	} else {
		count = (int)((os_gettime_ns() - cur_time) / interval_ns);
		*p_time = cur_time + interval_ns * count;
	}

541 542 543
	video->total_frames += count;
	video->lagged_frames += count - 1;

544 545 546 547
	vframe_info.timestamp = cur_time;
	vframe_info.count = count;
	circlebuf_push_back(&video->vframe_info_buffer, &vframe_info,
			sizeof(vframe_info));
548 549
}

P
Palana 已提交
550 551 552 553 554
static const char *output_frame_gs_context_name = "gs_context(video->graphics)";
static const char *output_frame_render_video_name = "render_video";
static const char *output_frame_download_frame_name = "download_frame";
static const char *output_frame_gs_flush_name = "gs_flush";
static const char *output_frame_output_video_data_name = "output_video_data";
P
Palana 已提交
555
static inline void output_frame(void)
556
{
557
	struct obs_core_video *video = &obs->video;
558 559
	int cur_texture  = video->cur_texture;
	int prev_texture = cur_texture == 0 ? NUM_TEXTURES-1 : cur_texture-1;
560
	struct video_data frame;
561 562
	bool frame_ready;

563
	memset(&frame, 0, sizeof(struct video_data));
564

P
Palana 已提交
565
	profile_start(output_frame_gs_context_name);
566
	gs_enter_context(video->graphics);
P
Palana 已提交
567 568

	profile_start(output_frame_render_video_name);
569
	render_video(video, cur_texture, prev_texture);
P
Palana 已提交
570 571 572
	profile_end(output_frame_render_video_name);

	profile_start(output_frame_download_frame_name);
J
jp9000 已提交
573
	frame_ready = download_frame(video, prev_texture, &frame);
P
Palana 已提交
574 575 576
	profile_end(output_frame_download_frame_name);

	profile_start(output_frame_gs_flush_name);
J
jp9000 已提交
577
	gs_flush();
P
Palana 已提交
578 579
	profile_end(output_frame_gs_flush_name);

580
	gs_leave_context();
P
Palana 已提交
581
	profile_end(output_frame_gs_context_name);
582

583
	if (frame_ready) {
584 585 586
		struct obs_vframe_info vframe_info;
		circlebuf_pop_front(&video->vframe_info_buffer, &vframe_info,
				sizeof(vframe_info));
587

588
		frame.timestamp = vframe_info.timestamp;
P
Palana 已提交
589
		profile_start(output_frame_output_video_data_name);
590
		output_video_data(video, &frame, vframe_info.count);
P
Palana 已提交
591
		profile_end(output_frame_output_video_data_name);
592
	}
J
jp9000 已提交
593

594 595
	if (++video->cur_texture == NUM_TEXTURES)
		video->cur_texture = 0;
J
jp9000 已提交
596 597
}

598 599
#define NBSP "\xC2\xA0"

P
Palana 已提交
600 601 602
static const char *tick_sources_name = "tick_sources";
static const char *render_displays_name = "render_displays";
static const char *output_frame_name = "output_frame";
603
void *obs_graphics_thread(void *param)
J
jp9000 已提交
604 605
{
	uint64_t last_time = 0;
606
	uint64_t interval = video_output_get_frame_time(obs->video.video);
607
	uint64_t frame_time_total_ns = 0;
J
jp9000 已提交
608 609
	uint64_t fps_total_ns = 0;
	uint32_t fps_total_frames = 0;
J
jp9000 已提交
610

J
jp9000 已提交
611 612
	obs->video.video_time = os_gettime_ns();

J
jp9000 已提交
613 614
	os_set_thread_name("libobs: graphics thread");

P
Palana 已提交
615 616
	const char *video_thread_name =
		profile_store_name(obs_get_profiler_name_store(),
617
			"obs_graphics_thread(%g"NBSP"ms)", interval / 1000000.);
P
Palana 已提交
618 619
	profile_register_root(video_thread_name, interval);

620 621
	srand((unsigned int)time(NULL));

622
	while (!video_output_stopped(obs->video.video)) {
623 624 625
		uint64_t frame_start = os_gettime_ns();
		uint64_t frame_time_ns;

P
Palana 已提交
626 627 628
		profile_start(video_thread_name);

		profile_start(tick_sources_name);
J
jp9000 已提交
629
		last_time = tick_sources(obs->video.video_time, last_time);
P
Palana 已提交
630
		profile_end(tick_sources_name);
631

P
Palana 已提交
632
		profile_start(output_frame_name);
P
Palana 已提交
633
		output_frame();
P
Palana 已提交
634 635
		profile_end(output_frame_name);

J
jp9000 已提交
636 637 638 639
		profile_start(render_displays_name);
		render_displays();
		profile_end(render_displays_name);

640 641
		frame_time_ns = os_gettime_ns() - frame_start;

P
Palana 已提交
642 643 644
		profile_end(video_thread_name);

		profile_reenable_thread();
P
Palana 已提交
645 646

		video_sleep(&obs->video, &obs->video.video_time, interval);
J
jp9000 已提交
647

648
		frame_time_total_ns += frame_time_ns;
J
jp9000 已提交
649 650 651 652 653 654
		fps_total_ns += (obs->video.video_time - last_time);
		fps_total_frames++;

		if (fps_total_ns >= 1000000000ULL) {
			obs->video.video_fps = (double)fps_total_frames /
				((double)fps_total_ns / 1000000000.0);
655 656 657 658
			obs->video.video_avg_frame_time_ns =
				frame_time_total_ns / (uint64_t)fps_total_frames;

			frame_time_total_ns = 0;
J
jp9000 已提交
659 660 661
			fps_total_ns = 0;
			fps_total_frames = 0;
		}
J
jp9000 已提交
662 663
	}

J
jp9000 已提交
664
	UNUSED_PARAMETER(param);
J
jp9000 已提交
665 666
	return NULL;
}