2 Simple DirectMedia Layer
3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
22 #include "../../SDL_internal.h"
24 #if SDL_AUDIO_DRIVER_NACL
26 #include "SDL_naclaudio.h"
28 #include "SDL_audio.h"
29 #include "SDL_mutex.h"
30 #include "../SDL_audio_c.h"
31 #include "../SDL_audiodev_c.h"
33 #include "ppapi/c/pp_errors.h"
34 #include "ppapi/c/pp_instance.h"
35 #include "ppapi_simple/ps.h"
36 #include "ppapi_simple/ps_interface.h"
37 #include "ppapi_simple/ps_event.h"
39 /* The tag name used by NACL audio */
40 #define NACLAUDIO_DRIVER_NAME "nacl"
42 #define SAMPLE_FRAME_COUNT 4096
44 /* Audio driver functions */
45 static void nacl_audio_callback(void* samples, uint32_t buffer_size, PP_TimeDelta latency, void* data);
47 /* FIXME: Make use of latency if needed */
48 static void nacl_audio_callback(void* stream, uint32_t buffer_size, PP_TimeDelta latency, void* data) {
49 const int len = (int) buffer_size;
50 SDL_AudioDevice* _this = (SDL_AudioDevice*) data;
51 SDL_AudioCallback callback = _this->callbackspec.callback;
53 SDL_LockMutex(private->mutex); /* !!! FIXME: is this mutex necessary? */
55 /* Only do something if audio is enabled */
56 if (!SDL_AtomicGet(&_this->enabled) || SDL_AtomicGet(&_this->paused)) {
58 SDL_AudioStreamClear(_this->stream);
60 SDL_memset(stream, _this->spec.silence, len);
64 SDL_assert(_this->spec.size == len);
66 if (_this->stream == NULL) { /* no conversion necessary. */
67 SDL_LockMutex(_this->mixer_lock);
68 callback(_this->callbackspec.userdata, stream, len);
69 SDL_UnlockMutex(_this->mixer_lock);
70 } else { /* streaming/converting */
71 const int stream_len = _this->callbackspec.size;
72 while (SDL_AudioStreamAvailable(_this->stream) < len) {
73 callback(_this->callbackspec.userdata, _this->work_buffer, stream_len);
74 if (SDL_AudioStreamPut(_this->stream, _this->work_buffer, stream_len) == -1) {
75 SDL_AudioStreamClear(_this->stream);
76 SDL_AtomicSet(&_this->enabled, 0);
81 const int got = SDL_AudioStreamGet(_this->stream, stream, len);
82 SDL_assert((got < 0) || (got == len));
84 SDL_memset(stream, _this->spec.silence, len);
88 SDL_UnlockMutex(private->mutex);
91 static void NACLAUDIO_CloseDevice(SDL_AudioDevice *device) {
92 const PPB_Core *core = PSInterfaceCore();
93 const PPB_Audio *ppb_audio = PSInterfaceAudio();
94 SDL_PrivateAudioData *hidden = (SDL_PrivateAudioData *) device->hidden;
96 ppb_audio->StopPlayback(hidden->audio);
97 SDL_DestroyMutex(hidden->mutex);
98 core->ReleaseResource(hidden->audio);
102 NACLAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) {
103 PP_Instance instance = PSGetInstanceId();
104 const PPB_Audio *ppb_audio = PSInterfaceAudio();
105 const PPB_AudioConfig *ppb_audiocfg = PSInterfaceAudioConfig();
107 private = (SDL_PrivateAudioData *) SDL_calloc(1, (sizeof *private));
108 if (private == NULL) {
109 return SDL_OutOfMemory();
112 private->mutex = SDL_CreateMutex();
113 _this->spec.freq = 44100;
114 _this->spec.format = AUDIO_S16LSB;
115 _this->spec.channels = 2;
116 _this->spec.samples = ppb_audiocfg->RecommendSampleFrameCount(
118 PP_AUDIOSAMPLERATE_44100,
121 /* Calculate the final parameters for this audio specification */
122 SDL_CalculateAudioSpec(&_this->spec);
124 private->audio = ppb_audio->Create(
126 ppb_audiocfg->CreateStereo16Bit(instance, PP_AUDIOSAMPLERATE_44100, _this->spec.samples),
130 /* Start audio playback while we are still on the main thread. */
131 ppb_audio->StartPlayback(private->audio);
137 NACLAUDIO_Init(SDL_AudioDriverImpl * impl)
139 if (PSGetInstanceId() == 0) {
143 /* Set the function pointers */
144 impl->OpenDevice = NACLAUDIO_OpenDevice;
145 impl->CloseDevice = NACLAUDIO_CloseDevice;
146 impl->OnlyHasDefaultOutputDevice = 1;
147 impl->ProvidesOwnCallbackThread = 1;
149 * impl->WaitDevice = NACLAUDIO_WaitDevice;
150 * impl->GetDeviceBuf = NACLAUDIO_GetDeviceBuf;
151 * impl->PlayDevice = NACLAUDIO_PlayDevice;
152 * impl->Deinitialize = NACLAUDIO_Deinitialize;
158 AudioBootStrap NACLAUDIO_bootstrap = {
159 NACLAUDIO_DRIVER_NAME, "SDL NaCl Audio Driver",
163 #endif /* SDL_AUDIO_DRIVER_NACL */
165 /* vi: set ts=4 sw=4 expandtab: */