SDL  2.0
SDL_naclaudio.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2018 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* stream, uint32_t buffer_size, PP_TimeDelta latency, void* data) {
49  const int len = (int) buffer_size;
52 
53  SDL_LockMutex(private->mutex); /* !!! FIXME: is this mutex necessary? */
54 
55  /* Only do something if audio is enabled */
56  if (!SDL_AtomicGet(&_this->enabled) || SDL_AtomicGet(&_this->paused)) {
57  if (_this->stream) {
59  }
60  SDL_memset(stream, _this->spec.silence, len);
61  return;
62  }
63 
64  SDL_assert(_this->spec.size == len);
65 
66  if (_this->stream == NULL) { /* no conversion necessary. */
67  SDL_LockMutex(_this->mixer_lock);
68  callback(_this->callbackspec.userdata, stream, len);
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) {
76  SDL_AtomicSet(&_this->enabled, 0);
77  break;
78  }
79  }
80 
81  const int got = SDL_AudioStreamGet(_this->stream, stream, len);
82  SDL_assert((got < 0) || (got == len));
83  if (got != len) {
84  SDL_memset(stream, _this->spec.silence, len);
85  }
86  }
87 
88  SDL_UnlockMutex(private->mutex);
89 }
90 
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;
95 
96  ppb_audio->StopPlayback(hidden->audio);
97  SDL_DestroyMutex(hidden->mutex);
98  core->ReleaseResource(hidden->audio);
99 }
100 
101 static int
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();
106 
107  private = (SDL_PrivateAudioData *) SDL_calloc(1, (sizeof *private));
108  if (private == NULL) {
109  return SDL_OutOfMemory();
110  }
111 
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(
117  instance,
118  PP_AUDIOSAMPLERATE_44100,
119  SAMPLE_FRAME_COUNT);
120 
121  /* Calculate the final parameters for this audio specification */
122  SDL_CalculateAudioSpec(&_this->spec);
123 
124  private->audio = ppb_audio->Create(
125  instance,
126  ppb_audiocfg->CreateStereo16Bit(instance, PP_AUDIOSAMPLERATE_44100, _this->spec.samples),
127  nacl_audio_callback,
128  _this);
129 
130  /* Start audio playback while we are still on the main thread. */
131  ppb_audio->StartPlayback(private->audio);
132 
133  return 0;
134 }
135 
136 static int
137 NACLAUDIO_Init(SDL_AudioDriverImpl * impl)
138 {
139  if (PSGetInstanceId() == 0) {
140  return 0;
141  }
142 
143  /* Set the function pointers */
144  impl->OpenDevice = NACLAUDIO_OpenDevice;
145  impl->CloseDevice = NACLAUDIO_CloseDevice;
146  impl->OnlyHasDefaultOutputDevice = 1;
147  impl->ProvidesOwnCallbackThread = 1;
148  /*
149  * impl->WaitDevice = NACLAUDIO_WaitDevice;
150  * impl->GetDeviceBuf = NACLAUDIO_GetDeviceBuf;
151  * impl->PlayDevice = NACLAUDIO_PlayDevice;
152  * impl->Deinitialize = NACLAUDIO_Deinitialize;
153  */
154 
155  return 1;
156 }
157 
159  NACLAUDIO_DRIVER_NAME, "SDL NaCl Audio Driver",
160  NACLAUDIO_Init, 0
161 };
162 
163 #endif /* SDL_AUDIO_DRIVER_NACL */
164 
165 /* vi: set ts=4 sw=4 expandtab: */
struct SDL_PrivateAudioData * hidden
Definition: SDL_sysaudio.h:169
#define SDL_LockMutex
#define SDL_AudioStreamAvailable
SDL_mutex * mixer_lock
Definition: SDL_sysaudio.h:158
SDL_atomic_t enabled
Definition: SDL_sysaudio.h:147
Uint8 silence
Definition: SDL_audio.h:182
SDL_atomic_t paused
Definition: SDL_sysaudio.h:148
#define SDL_AudioStreamGet
#define SDL_CreateMutex
Uint16 samples
Definition: SDL_audio.h:183
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
SDL_AudioSpec spec
Definition: SDL_sysaudio.h:137
#define private
Definition: SDL_naclaudio.h:34
GLenum GLsizei len
static SDL_VideoDevice * _this
Definition: SDL_video.c:121
static SDL_AudioDeviceID device
Definition: loopwave.c:37
EGLImageKHR EGLint EGLint * handle
Definition: eglext.h:937
GLuint GLuint stream
void(* SDL_AudioCallback)(void *userdata, Uint8 *stream, int len)
Definition: SDL_audio.h:162
Uint8 channels
Definition: SDL_audio.h:181
#define _THIS
#define SDL_AudioStreamPut
static Uint32 callback(Uint32 interval, void *param)
Definition: testtimer.c:34
void SDL_CalculateAudioSpec(SDL_AudioSpec *spec)
Definition: SDL_audio.c:1605
SDL_AudioCallback callback
Definition: SDL_audio.h:186
Uint32 size
Definition: SDL_audio.h:185
#define SDL_assert(condition)
Definition: SDL_assert.h:169
int(* OpenDevice)(_THIS, void *handle, const char *devname, int iscapture)
Definition: SDL_sysaudio.h:68
#define NULL
Definition: begin_code.h:164
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
unsigned int uint32_t
void(* CloseDevice)(_THIS)
Definition: SDL_sysaudio.h:79
#define SDL_DestroyMutex
#define SDL_calloc
SDL_AudioFormat format
Definition: SDL_audio.h:180
AudioBootStrap NACLAUDIO_bootstrap
#define AUDIO_S16LSB
Definition: SDL_audio.h:92
#define SDL_AtomicSet
#define SDL_AudioStreamClear
#define SDL_AtomicGet
void * userdata
Definition: SDL_audio.h:187
#define SDL_UnlockMutex
GLsizei samples
SDL_AudioSpec callbackspec
Definition: SDL_sysaudio.h:140
SDL_AudioStream * stream
Definition: SDL_sysaudio.h:143
Uint8 * work_buffer
Definition: SDL_sysaudio.h:152
#define SDL_memset