Skip to content

Commit 20ad304

Browse files
committed
Implement CUBEB_STREAM_PREF_EXCLUSIVE
1 parent 475d97f commit 20ad304

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

include/cubeb/cubeb.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,16 @@ typedef enum {
217217

218218
/** Miscellaneous stream preferences. */
219219
typedef enum {
220-
CUBEB_STREAM_PREF_NONE = 0x00, /**< No stream preferences are requested. */
221-
CUBEB_STREAM_PREF_LOOPBACK = 0x01 /**< Request a loopback stream. Should be
222-
specified on the input params and an
223-
output device to loopback from should
224-
be passed in place of an input device. */
220+
CUBEB_STREAM_PREF_NONE = 0x00, /**< No stream preferences are requested. */
221+
CUBEB_STREAM_PREF_LOOPBACK = 0x01, /**< Request a loopback stream. Should be
222+
specified on the input params and an
223+
output device to loopback from should
224+
be passed in place of an input device. */
225+
CUBEB_STREAM_PREF_EXCLUSIVE = 0x02 /**< (Windows / WASAPI only) Request that
226+
this stream is run in exclusive mode
227+
which results in lower latency but
228+
doesn't allow other applications to
229+
use the same device */
225230
} cubeb_stream_prefs;
226231

227232
/** Stream format initialization parameters. */

src/cubeb.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@ cubeb_stream_init(cubeb * context, cubeb_stream ** stream, char const * stream_n
318318
return r;
319319
}
320320

321+
if ((output_stream_params && output_stream_params->prefs) & CUBEB_STREAM_PREF_EXCLUSIVE || (input_stream_params && input_stream_params->prefs & CUBEB_STREAM_PREF_EXCLUSIVE)) {
322+
if (strcmp(cubeb_get_backend_id(context), "wasapi") != 0)
323+
return CUBEB_ERROR_NOT_SUPPORTED;
324+
}
325+
321326
r = context->ops->stream_init(context, stream, stream_name,
322327
input_device,
323328
input_stream_params,

src/cubeb_wasapi.cpp

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -617,9 +617,20 @@ refill(cubeb_stream * stm, void * input_buffer, long input_frames_count,
617617

618618
int wasapi_stream_reset_default_device(cubeb_stream * stm);
619619

620+
/* Helper for making get_input_buffer work in exclusive mode */
621+
HRESULT get_next_packet_size(cubeb_stream * stm, PUINT32 next)
622+
{
623+
if (stm->input_stream_params.prefs & CUBEB_STREAM_PREF_EXCLUSIVE) {
624+
*next = stm->input_buffer_frame_count;
625+
return S_OK;
626+
} else {
627+
return stm->capture_client->GetNextPacketSize(next);
628+
}
629+
}
630+
620631
/* This helper grabs all the frames available from a capture client, put them in
621632
* linear_input_buffer. linear_input_buffer should be cleared before the
622-
* callback exits. This helper does not work with exclusive mode streams. */
633+
* callback exits. */
623634
bool get_input_buffer(cubeb_stream * stm)
624635
{
625636
XASSERT(has_input(stm));
@@ -636,9 +647,9 @@ bool get_input_buffer(cubeb_stream * stm)
636647
// single packet each time. However, if we're pulling from the stream we may
637648
// need to grab multiple packets worth of frames that have accumulated (so
638649
// need a loop).
639-
for (hr = stm->capture_client->GetNextPacketSize(&next);
650+
for (hr = get_next_packet_size(stm, &next);
640651
next > 0;
641-
hr = stm->capture_client->GetNextPacketSize(&next)) {
652+
hr = get_next_packet_size(stm, &next)) {
642653
if (hr == AUDCLNT_E_DEVICE_INVALIDATED) {
643654
// Application can recover from this error. More info
644655
// https://msdn.microsoft.com/en-us/library/windows/desktop/dd316605(v=vs.85).aspx
@@ -705,6 +716,9 @@ bool get_input_buffer(cubeb_stream * stm)
705716
return false;
706717
}
707718
offset += packet_size;
719+
720+
if (stm->input_stream_params.prefs & CUBEB_STREAM_PREF_EXCLUSIVE)
721+
break;
708722
}
709723

710724
XASSERT(stm->linear_input_buffer->length() >= offset);
@@ -1395,8 +1409,11 @@ wasapi_get_min_latency(cubeb * ctx, cubeb_stream_params params, uint32_t * laten
13951409
return CUBEB_ERROR;
13961410
}
13971411

1398-
/* The second parameter is for exclusive mode, that we don't use. */
1399-
hr = client->GetDevicePeriod(&default_period, NULL);
1412+
if (params.prefs & CUBEB_STREAM_PREF_EXCLUSIVE)
1413+
hr = client->GetDevicePeriod(NULL, &default_period);
1414+
else
1415+
hr = client->GetDevicePeriod(&default_period, NULL);
1416+
14001417
if (FAILED(hr)) {
14011418
LOG("Could not get device period: %lx", hr);
14021419
return CUBEB_ERROR;
@@ -1531,7 +1548,7 @@ handle_channel_layout(cubeb_stream * stm, EDataFlow direction, com_heap_ptr<WAV
15311548

15321549
/* Check if wasapi will accept our channel layout request. */
15331550
WAVEFORMATEX * closest;
1534-
HRESULT hr = audio_client->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED,
1551+
HRESULT hr = audio_client->IsFormatSupported(stream_params->prefs & CUBEB_STREAM_PREF_EXCLUSIVE ? AUDCLNT_SHAREMODE_EXCLUSIVE : AUDCLNT_SHAREMODE_SHARED,
15351552
mix_format.get(),
15361553
&closest);
15371554
if (hr == S_FALSE) {
@@ -1665,6 +1682,13 @@ int setup_wasapi_stream_one_side(cubeb_stream * stm,
16651682
// audio according to layout.
16661683
LOG("Channel count is different from the layout standard!\n");
16671684
}
1685+
1686+
1687+
if (stream_params->prefs & CUBEB_STREAM_PREF_EXCLUSIVE)
1688+
LOG("Setup requested=[f=%d r=%u c=%u] mix=[f=%d r=%u c=%u]",
1689+
stream_params->format, stream_params->rate, stream_params->channels,
1690+
mix_params->format, mix_params->rate, mix_params->channels);
1691+
else
16681692
LOG("Setup requested=[f=%d r=%u c=%u l=%s] mix=[f=%d r=%u c=%u l=%s]",
16691693
stream_params->format, stream_params->rate, stream_params->channels,
16701694
CUBEB_CHANNEL_LAYOUT_MAPS[stream_params->layout].name,
@@ -1681,10 +1705,10 @@ int setup_wasapi_stream_one_side(cubeb_stream * stm,
16811705
flags |= AUDCLNT_STREAMFLAGS_EVENTCALLBACK;
16821706
}
16831707

1684-
hr = audio_client->Initialize(AUDCLNT_SHAREMODE_SHARED,
1708+
hr = audio_client->Initialize(stream_params->prefs & CUBEB_STREAM_PREF_EXCLUSIVE ? AUDCLNT_SHAREMODE_EXCLUSIVE : AUDCLNT_SHAREMODE_SHARED,
16851709
flags,
16861710
frames_to_hns(stm, stm->latency),
1687-
0,
1711+
stream_params->prefs & CUBEB_STREAM_PREF_EXCLUSIVE ? frames_to_hns(stm, stm->latency) : 0,
16881712
mix_format.get(),
16891713
NULL);
16901714
if (FAILED(hr)) {

0 commit comments

Comments
 (0)