SDL  2.0
SDL_naclaudio.c
Go to the documentation of this file.
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 */
48 static void nacl_audio_callback(void* samples, uint32_t buffer_size, PP_TimeDelta latency, void* 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);
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);
66  }
67  } else {
68  SDL_memset(samples, _this->spec.silence, buffer_size);
69  }
70 
71  SDL_UnlockMutex(private->mutex);
72 }
73 
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
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) {
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
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 
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: */
struct SDL_PrivateAudioData * hidden
Definition: SDL_sysaudio.h:189
#define SDL_LockMutex
SDL_mutex * mixer_lock
Definition: SDL_sysaudio.h:175
SDL_atomic_t enabled
Definition: SDL_sysaudio.h:167
Uint8 silence
Definition: SDL_audio.h:173
Uint8 * buf
Definition: SDL_audio.h:206
SDL_atomic_t paused
Definition: SDL_sysaudio.h:168
#define SDL_CreateMutex
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1967
Uint16 samples
Definition: SDL_audio.h:174
SDL_AudioSpec spec
Definition: SDL_sysaudio.h:156
#define private
Definition: SDL_naclaudio.h:34
static SDL_VideoDevice * _this
Definition: SDL_video.c:118
#define SDL_memcpy
void * SDL_calloc(size_t nmemb, size_t size)
#define SDL_ConvertAudio
Uint8 channels
Definition: SDL_audio.h:172
#define _THIS
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
void SDL_CalculateAudioSpec(SDL_AudioSpec *spec)
Definition: SDL_audio.c:1632
GLsizei samples
SDL_AudioCallback callback
Definition: SDL_audio.h:177
int(* OpenDevice)(_THIS, void *handle, const char *devname, int iscapture)
Definition: SDL_sysaudio.h:76
#define NULL
Definition: begin_code.h:143
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
unsigned int uint32_t
void(* CloseDevice)(_THIS)
Definition: SDL_sysaudio.h:85
#define SDL_DestroyMutex
AudioBootStrap NACLAUDIO_bootstrap
SDL_AudioFormat format
Definition: SDL_audio.h:171
#define AUDIO_S16LSB
Definition: SDL_audio.h:92
#define SDL_AtomicGet
void * userdata
Definition: SDL_audio.h:178
#define SDL_UnlockMutex
SDL_AudioCVT convert
Definition: SDL_sysaudio.h:159
#define SDL_memset