• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
4 
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.
8 
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:
12 
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.
20 */
21 
22 #include "../../SDL_internal.h"
23 
24 #if SDL_AUDIO_DRIVER_NACL
25 
26 #include "SDL_naclaudio.h"
27 
28 #include "SDL_audio.h"
29 #include "SDL_mutex.h"
30 #include "../SDL_audio_c.h"
31 #include "../SDL_audiodev_c.h"
32 
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"
38 
39 /* The tag name used by NACL audio */
40 #define NACLAUDIO_DRIVER_NAME         "nacl"
41 
42 #define SAMPLE_FRAME_COUNT 4096
43 
44 /* Audio driver functions */
45 static void nacl_audio_callback(void* samples, uint32_t buffer_size, PP_TimeDelta latency, void* data);
46 
47 /* FIXME: Make use of latency if needed */
nacl_audio_callback(void * samples,uint32_t buffer_size,PP_TimeDelta latency,void * data)48 static void nacl_audio_callback(void* samples, uint32_t buffer_size, PP_TimeDelta latency, void* data) {
49     SDL_AudioDevice* _this = (SDL_AudioDevice*) data;
50 
51     SDL_LockMutex(private->mutex);  /* !!! FIXME: is this mutex necessary? */
52 
53     if (SDL_AtomicGet(&_this->enabled) && !SDL_AtomicGet(&_this->paused)) {
54         if (_this->convert.needed) {
55             SDL_LockMutex(_this->mixer_lock);
56             (*_this->spec.callback) (_this->spec.userdata,
57                                      (Uint8 *) _this->convert.buf,
58                                      _this->convert.len);
59             SDL_UnlockMutex(_this->mixer_lock);
60             SDL_ConvertAudio(&_this->convert);
61             SDL_memcpy(samples, _this->convert.buf, _this->convert.len_cvt);
62         } else {
63             SDL_LockMutex(_this->mixer_lock);
64             (*_this->spec.callback) (_this->spec.userdata, (Uint8 *) samples, buffer_size);
65             SDL_UnlockMutex(_this->mixer_lock);
66         }
67     } else {
68         SDL_memset(samples, _this->spec.silence, buffer_size);
69     }
70 
71     SDL_UnlockMutex(private->mutex);
72 }
73 
NACLAUDIO_CloseDevice(SDL_AudioDevice * device)74 static void NACLAUDIO_CloseDevice(SDL_AudioDevice *device) {
75     const PPB_Core *core = PSInterfaceCore();
76     const PPB_Audio *ppb_audio = PSInterfaceAudio();
77     SDL_PrivateAudioData *hidden = (SDL_PrivateAudioData *) device->hidden;
78 
79     ppb_audio->StopPlayback(hidden->audio);
80     SDL_DestroyMutex(hidden->mutex);
81     core->ReleaseResource(hidden->audio);
82 }
83 
84 static int
NACLAUDIO_OpenDevice(_THIS,void * handle,const char * devname,int iscapture)85 NACLAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) {
86     PP_Instance instance = PSGetInstanceId();
87     const PPB_Audio *ppb_audio = PSInterfaceAudio();
88     const PPB_AudioConfig *ppb_audiocfg = PSInterfaceAudioConfig();
89 
90     private = (SDL_PrivateAudioData *) SDL_calloc(1, (sizeof *private));
91     if (private == NULL) {
92         SDL_OutOfMemory();
93         return 0;
94     }
95 
96     private->mutex = SDL_CreateMutex();
97     _this->spec.freq = 44100;
98     _this->spec.format = AUDIO_S16LSB;
99     _this->spec.channels = 2;
100     _this->spec.samples = ppb_audiocfg->RecommendSampleFrameCount(
101         instance,
102         PP_AUDIOSAMPLERATE_44100,
103         SAMPLE_FRAME_COUNT);
104 
105     /* Calculate the final parameters for this audio specification */
106     SDL_CalculateAudioSpec(&_this->spec);
107 
108     private->audio = ppb_audio->Create(
109         instance,
110         ppb_audiocfg->CreateStereo16Bit(instance, PP_AUDIOSAMPLERATE_44100, _this->spec.samples),
111         nacl_audio_callback,
112         _this);
113 
114     /* Start audio playback while we are still on the main thread. */
115     ppb_audio->StartPlayback(private->audio);
116 
117     return 1;
118 }
119 
120 static int
NACLAUDIO_Init(SDL_AudioDriverImpl * impl)121 NACLAUDIO_Init(SDL_AudioDriverImpl * impl)
122 {
123     if (PSGetInstanceId() == 0) {
124         return 0;
125     }
126 
127     /* Set the function pointers */
128     impl->OpenDevice = NACLAUDIO_OpenDevice;
129     impl->CloseDevice = NACLAUDIO_CloseDevice;
130     impl->OnlyHasDefaultOutputDevice = 1;
131     impl->ProvidesOwnCallbackThread = 1;
132     /*
133      *    impl->WaitDevice = NACLAUDIO_WaitDevice;
134      *    impl->GetDeviceBuf = NACLAUDIO_GetDeviceBuf;
135      *    impl->PlayDevice = NACLAUDIO_PlayDevice;
136      *    impl->Deinitialize = NACLAUDIO_Deinitialize;
137      */
138 
139     return 1;
140 }
141 
142 AudioBootStrap NACLAUDIO_bootstrap = {
143     NACLAUDIO_DRIVER_NAME, "SDL NaCl Audio Driver",
144     NACLAUDIO_Init, 0
145 };
146 
147 #endif /* SDL_AUDIO_DRIVER_NACL */
148 
149 /* vi: set ts=4 sw=4 expandtab: */
150