Skip to content

Commit ad7084c

Browse files
committed
SDL2: Add wakeup callback.
1 parent 2f18c53 commit ad7084c

File tree

2 files changed

+64
-25
lines changed

2 files changed

+64
-25
lines changed

extras/backends/sdl/backend_sdl.c

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ static ma_device_state_sdl* ma_device_get_backend_state__sdl(ma_device* pDevice)
169169
}
170170

171171

172-
static ma_result ma_device_step__sdl(ma_device* pDevice);
172+
static ma_result ma_device_step__sdl(ma_device* pDevice, ma_blocking_mode blockingMode);
173173

174174

175175
static void ma_backend_info__sdl(ma_device_backend_info* pBackendInfo)
@@ -562,7 +562,7 @@ static ma_result ma_device_start__sdl(ma_device* pDevice)
562562
ma_device_type deviceType = ma_device_get_type(pDevice);
563563

564564
/* Step the device once to ensure buffers are pre-filled before starting. */
565-
ma_device_step__sdl(pDevice);
565+
ma_device_step__sdl(pDevice, MA_BLOCKING_MODE_NON_BLOCKING);
566566

567567
if (deviceType == ma_device_type_capture || deviceType == ma_device_type_duplex) {
568568
pContextStateSDL->SDL_PauseAudioDevice(pDeviceStateSDL->capture.deviceID, 0);
@@ -593,42 +593,59 @@ static ma_result ma_device_stop__sdl(ma_device* pDevice)
593593
}
594594

595595

596-
static ma_result ma_device_wait__sdl(ma_device* pDevice)
596+
static ma_result ma_device_step__sdl(ma_device* pDevice, ma_blocking_mode blockingMode)
597597
{
598598
ma_device_state_sdl* pDeviceStateSDL = ma_device_get_backend_state__sdl(pDevice);
599-
ma_device_state_async_wait(&pDeviceStateSDL->async);
600599

601-
return MA_SUCCESS;
602-
}
600+
for (;;) {
601+
ma_result result;
602+
603+
if (blockingMode == MA_BLOCKING_MODE_BLOCKING) {
604+
ma_device_state_async_wait(&pDeviceStateSDL->async);
605+
}
606+
607+
if (!ma_device_is_started(pDevice)) {
608+
return MA_DEVICE_NOT_STARTED;
609+
}
610+
611+
result = ma_device_state_async_step(&pDeviceStateSDL->async, pDevice);
612+
if (result == MA_SUCCESS) {
613+
break;
614+
}
615+
616+
if (result != MA_NO_DATA_AVAILABLE) {
617+
return result;
618+
}
619+
620+
/* Getting here means no data was processed. In non-blocking mode we don't care, just get out of the loop. */
621+
if (blockingMode == MA_BLOCKING_MODE_NON_BLOCKING) {
622+
break;
623+
}
624+
625+
/* Getting here means we're in blocking mode and no data was processed. In this case we'd rather keep waiting for data to be available. */
626+
continue;
627+
}
603628

604-
static ma_result ma_device_step__sdl(ma_device* pDevice)
605-
{
606-
ma_device_state_sdl* pDeviceStateSDL = ma_device_get_backend_state__sdl(pDevice);
607-
ma_device_state_async_step(&pDeviceStateSDL->async, pDevice);
608629

609630
return MA_SUCCESS;
610631
}
611632

612633
static void ma_device_loop__sdl(ma_device* pDevice)
613634
{
614635
for (;;) {
615-
ma_result result = ma_device_wait__sdl(pDevice);
616-
if (result != MA_SUCCESS) {
617-
break;
618-
}
619-
620-
/* If the wait terminated due to the device being stopped, abort now. */
621-
if (!ma_device_is_started(pDevice)) {
622-
break;
623-
}
624-
625-
result = ma_device_step__sdl(pDevice);
636+
ma_result result = ma_device_step__sdl(pDevice, MA_BLOCKING_MODE_BLOCKING);
626637
if (result != MA_SUCCESS) {
627638
break;
628639
}
629640
}
630641
}
631642

643+
static void ma_device_wake__sdl(ma_device* pDevice)
644+
{
645+
ma_device_state_sdl* pDeviceStateSDL = ma_device_get_backend_state__sdl(pDevice);
646+
ma_device_state_async_release(&pDeviceStateSDL->async);
647+
}
648+
632649

633650
static ma_device_backend_vtable ma_gDeviceBackendVTable_SDL =
634651
{
@@ -643,7 +660,7 @@ static ma_device_backend_vtable ma_gDeviceBackendVTable_SDL =
643660
NULL, /* onDeviceRead */
644661
NULL, /* onDeviceWrite */
645662
ma_device_loop__sdl,
646-
NULL /* onDeviceWakeup */
663+
ma_device_wake__sdl
647664
};
648665

649666
ma_device_backend_vtable* ma_device_backend_sdl = &ma_gDeviceBackendVTable_SDL;

miniaudio.h

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9522,8 +9522,9 @@ typedef struct ma_device_state_async
95229522
MA_API ma_result ma_device_state_async_init(ma_device_type deviceType, const ma_device_descriptor* pDescriptorPlayback, const ma_device_descriptor* pDescriptorCapture, const ma_allocation_callbacks* pAllocationCallbacks, ma_device_state_async* pAsyncDeviceState);
95239523
MA_API void ma_device_state_async_uninit(ma_device_state_async* pAsyncDeviceState, const ma_allocation_callbacks* pAllocationCallbacks);
95249524
MA_API ma_result ma_device_state_async_resize(ma_device_state_async* pAsyncDeviceState, ma_uint32 sizeInFramesPlayback, ma_uint32 sizeInFramesCapture, const ma_allocation_callbacks* pAllocationCallbacks);
9525+
MA_API void ma_device_state_async_release(ma_device_state_async* pAsyncDeviceState);
95259526
MA_API void ma_device_state_async_wait(ma_device_state_async* pAsyncDeviceState);
9526-
MA_API void ma_device_state_async_step(ma_device_state_async* pAsyncDeviceState, ma_device* pDevice);
9527+
MA_API ma_result ma_device_state_async_step(ma_device_state_async* pAsyncDeviceState, ma_device* pDevice); /* Returns MA_SUCCESS if some data was processed, MA_NO_DATA_AVAILABLE if no data was processed. */
95279528
MA_API void ma_device_state_async_process(ma_device_state_async* pAsyncDeviceState, ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount);
95289529
/* END ma_device_state_async.h */
95299530

@@ -46235,6 +46236,21 @@ MA_API ma_result ma_device_state_async_resize(ma_device_state_async* pAsyncDevic
4623546236
return MA_SUCCESS;
4623646237
}
4623746238

46239+
MA_API void ma_device_state_async_release(ma_device_state_async* pAsyncDeviceState)
46240+
{
46241+
if (pAsyncDeviceState == NULL) {
46242+
return;
46243+
}
46244+
46245+
if (pAsyncDeviceState->deviceType == ma_device_type_capture || pAsyncDeviceState->deviceType == ma_device_type_duplex) {
46246+
ma_semaphore_release(&pAsyncDeviceState->capture.semaphore);
46247+
}
46248+
46249+
if (pAsyncDeviceState->deviceType == ma_device_type_playback || pAsyncDeviceState->deviceType == ma_device_type_duplex) {
46250+
ma_semaphore_release(&pAsyncDeviceState->playback.semaphore);
46251+
}
46252+
}
46253+
4623846254
MA_API void ma_device_state_async_wait(ma_device_state_async* pAsyncDeviceState)
4623946255
{
4624046256
if (pAsyncDeviceState == NULL) {
@@ -46250,10 +46266,12 @@ MA_API void ma_device_state_async_wait(ma_device_state_async* pAsyncDeviceState)
4625046266
}
4625146267
}
4625246268

46253-
MA_API void ma_device_state_async_step(ma_device_state_async* pAsyncDeviceState, ma_device* pDevice)
46269+
MA_API ma_result ma_device_state_async_step(ma_device_state_async* pAsyncDeviceState, ma_device* pDevice)
4625446270
{
46271+
ma_result result = MA_NO_DATA_AVAILABLE;
46272+
4625546273
if (pAsyncDeviceState == NULL || pDevice == NULL) {
46256-
return;
46274+
return MA_INVALID_ARGS;
4625746275
}
4625846276

4625946277
if (pAsyncDeviceState->deviceType == ma_device_type_capture || pAsyncDeviceState->deviceType == ma_device_type_duplex) {
@@ -46262,6 +46280,7 @@ MA_API void ma_device_state_async_step(ma_device_state_async* pAsyncDeviceState,
4626246280
if (pAsyncDeviceState->capture.frameCount > 0) {
4626346281
ma_device_handle_backend_data_callback(pDevice, NULL, pAsyncDeviceState->capture.pBuffer, pAsyncDeviceState->capture.frameCount);
4626446282
pAsyncDeviceState->capture.frameCount = 0;
46283+
result = MA_SUCCESS;
4626546284
}
4626646285
}
4626746286
ma_spinlock_unlock(&pAsyncDeviceState->capture.lock);
@@ -46275,10 +46294,13 @@ MA_API void ma_device_state_async_step(ma_device_state_async* pAsyncDeviceState,
4627546294
if (pAsyncDeviceState->playback.frameCount < pAsyncDeviceState->playback.frameCap) {
4627646295
ma_device_handle_backend_data_callback(pDevice, ma_offset_ptr(pAsyncDeviceState->playback.pBuffer, bytesPerFrame * pAsyncDeviceState->playback.frameCount), NULL, (pAsyncDeviceState->playback.frameCap - pAsyncDeviceState->playback.frameCount));
4627746296
pAsyncDeviceState->playback.frameCount = pAsyncDeviceState->playback.frameCap;
46297+
result = MA_SUCCESS;
4627846298
}
4627946299
}
4628046300
ma_spinlock_unlock(&pAsyncDeviceState->playback.lock);
4628146301
}
46302+
46303+
return result;
4628246304
}
4628346305

4628446306
MA_API void ma_device_state_async_process(ma_device_state_async* pAsyncDeviceState, ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount)

0 commit comments

Comments
 (0)