SDL  2.0
SDL_naclaudio.c
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2020 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;
51  SDL_AudioCallback callback = _this->callbackspec.callback;
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) {
58  SDL_AudioStreamClear(_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);
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);
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 */
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: */
SDL_memset
#define SDL_memset
Definition: SDL_dynapi_overrides.h:386
SDL_AudioDriverImpl::ProvidesOwnCallbackThread
int ProvidesOwnCallbackThread
Definition: SDL_sysaudio.h:88
SDL_LockMutex
#define SDL_LockMutex
Definition: SDL_dynapi_overrides.h:260
NULL
#define NULL
Definition: begin_code.h:167
SDL_AudioStreamAvailable
#define SDL_AudioStreamAvailable
Definition: SDL_dynapi_overrides.h:647
handle
EGLImageKHR EGLint EGLint * handle
Definition: eglext.h:937
samples
GLsizei samples
Definition: SDL_opengl_glext.h:1188
stream
GLuint GLuint stream
Definition: SDL_opengl_glext.h:1779
SDL_AudioDriverImpl::OpenDevice
int(* OpenDevice)(_THIS, void *handle, const char *devname, int iscapture)
Definition: SDL_sysaudio.h:68
SDL_AudioStreamGet
#define SDL_AudioStreamGet
Definition: SDL_dynapi_overrides.h:645
SDL_CreateMutex
#define SDL_CreateMutex
Definition: SDL_dynapi_overrides.h:259
callback
static Uint32 callback(Uint32 interval, void *param)
Definition: testtimer.c:34
AudioBootStrap
Definition: SDL_sysaudio.h:176
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
SDL_PrivateAudioData
Definition: SDL_alsa_audio.h:33
SDL_PrivateAudioData::mutex
SDL_mutex * mutex
Definition: SDL_naclaudio.h:37
len
GLenum GLsizei len
Definition: SDL_opengl_glext.h:2929
_this
static SDL_VideoDevice * _this
Definition: SDL_video.c:121
SDL_audio.h
private
#define private
Definition: SDL_naclaudio.h:34
SDL_AudioDriverImpl
Definition: SDL_sysaudio.h:65
SDL_mutex.h
SDL_AudioDriverImpl::OnlyHasDefaultOutputDevice
int OnlyHasDefaultOutputDevice
Definition: SDL_sysaudio.h:91
NACLAUDIO_bootstrap
AudioBootStrap NACLAUDIO_bootstrap
SDL_CalculateAudioSpec
void SDL_CalculateAudioSpec(SDL_AudioSpec *spec)
Definition: SDL_audio.c:1686
_THIS
#define _THIS
Definition: SDL_alsa_audio.h:31
SDL_AudioStreamPut
#define SDL_AudioStreamPut
Definition: SDL_dynapi_overrides.h:644
SDL_assert
#define SDL_assert(condition)
Definition: SDL_assert.h:169
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_calloc
#define SDL_calloc
Definition: SDL_dynapi_overrides.h:375
uint32_t
unsigned int uint32_t
Definition: SDL_config_windows.h:63
SDL_PrivateAudioData::audio
PP_Resource audio
Definition: SDL_naclaudio.h:38
SDL_AudioStreamClear
#define SDL_AudioStreamClear
Definition: SDL_dynapi_overrides.h:646
SDL_DestroyMutex
#define SDL_DestroyMutex
Definition: SDL_dynapi_overrides.h:263
SDL_naclaudio.h
AUDIO_S16LSB
#define AUDIO_S16LSB
Definition: SDL_audio.h:92
SDL_AtomicSet
#define SDL_AtomicSet
Definition: SDL_dynapi_overrides.h:67
SDL_AtomicGet
#define SDL_AtomicGet
Definition: SDL_dynapi_overrides.h:68
SDL_AudioCallback
void(* SDL_AudioCallback)(void *userdata, Uint8 *stream, int len)
Definition: SDL_audio.h:163
SDL_UnlockMutex
#define SDL_UnlockMutex
Definition: SDL_dynapi_overrides.h:262
device
static SDL_AudioDeviceID device
Definition: loopwave.c:37
SDL_AudioDriverImpl::CloseDevice
void(* CloseDevice)(_THIS)
Definition: SDL_sysaudio.h:78
SDL_AudioDevice
Definition: SDL_sysaudio.h:131