Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/sphinx/reference-libobs-util-platform.rst
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ Sleep/Time Functions

---------------------

.. function:: bool os_sleepto_ns_fast(uint64_t time_target)

Sleeps to a specific time without high precision, in nanoseconds.
The function won't return until reaching the specific time.

---------------------

.. function:: void os_sleep_ms(uint32_t duration)

Sleeps for a specific number of milliseconds.
Expand Down
21 changes: 6 additions & 15 deletions libobs/media-io/audio-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,6 @@ static void *audio_thread(void *param)
uint64_t samples = 0;
uint64_t start_time = os_gettime_ns();
uint64_t prev_time = start_time;
uint64_t audio_time = prev_time;
uint32_t audio_wait_time =
(uint32_t)(audio_frames_to_ns(rate, AUDIO_OUTPUT_FRAMES) /
1000000);

os_set_thread_name("audio-io: audio thread");

Expand All @@ -225,21 +221,16 @@ static void *audio_thread(void *param)
"audio_thread(%s)", audio->info.name);

while (os_event_try(audio->stop_event) == EAGAIN) {
uint64_t cur_time;
samples += AUDIO_OUTPUT_FRAMES;
uint64_t audio_time =
start_time + audio_frames_to_ns(rate, samples);

os_sleep_ms(audio_wait_time);
os_sleepto_ns_fast(audio_time);

profile_start(audio_thread_name);

cur_time = os_gettime_ns();
while (audio_time <= cur_time) {
samples += AUDIO_OUTPUT_FRAMES;
audio_time =
start_time + audio_frames_to_ns(rate, samples);

input_and_output(audio, audio_time, prev_time);
prev_time = audio_time;
}
input_and_output(audio, audio_time, prev_time);
prev_time = audio_time;

profile_end(audio_thread_name);

Expand Down
17 changes: 17 additions & 0 deletions libobs/util/platform-nix.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,23 @@ bool os_sleepto_ns(uint64_t time_target)
return true;
}

bool os_sleepto_ns_fast(uint64_t time_target)
{
uint64_t current = os_gettime_ns();
if (time_target < current)
return false;

do {
uint64_t remain_us = (time_target - current + 999) / 1000;
useconds_t us = remain_us >= 1000000 ? 999999 : remain_us;
usleep(us);

current = os_gettime_ns();
} while (time_target > current);

return true;
}

void os_sleep_ms(uint32_t duration)
{
usleep(duration * 1000);
Expand Down
18 changes: 18 additions & 0 deletions libobs/util/platform-windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,24 @@ bool os_sleepto_ns(uint64_t time_target)
return stall;
}

bool os_sleepto_ns_fast(uint64_t time_target)
{
uint64_t current = os_gettime_ns();
if (time_target < current)
return false;

do {
uint64_t remain_ms = (time_target - current) / 1000000;
if (!remain_ms)
remain_ms = 1;
Sleep((DWORD)remain_ms);

current = os_gettime_ns();
} while (time_target > current);

return true;
}

void os_sleep_ms(uint32_t duration)
{
/* windows 8+ appears to have decreased sleep precision */
Expand Down
1 change: 1 addition & 0 deletions libobs/util/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ EXPORT void os_end_high_performance(os_performance_token_t *);
* Returns false if already at or past target time.
*/
EXPORT bool os_sleepto_ns(uint64_t time_target);
EXPORT bool os_sleepto_ns_fast(uint64_t time_target);
EXPORT void os_sleep_ms(uint32_t duration);

EXPORT uint64_t os_gettime_ns(void);
Expand Down