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_audiomem.h"
31 #include "../SDL_audio_c.h"
32 #include "../SDL_audiodev_c.h"
33 
34 #include "ppapi/c/pp_errors.h"
35 #include "ppapi/c/pp_instance.h"
36 #include "ppapi_simple/ps.h"
37 #include "ppapi_simple/ps_interface.h"
38 #include "ppapi_simple/ps_event.h"
39 
40 /* The tag name used by NACL audio */
41 #define NACLAUD_DRIVER_NAME "nacl"
42 
43 #define SAMPLE_FRAME_COUNT 4096
44 
45 /* Audio driver functions */
46 static int NACLAUD_OpenDevice(_THIS, void *handle, const char *devname, int iscapture);
47 static void NACLAUD_CloseDevice(_THIS);
48 static void nacl_audio_callback(void* samples, uint32_t buffer_size, PP_TimeDelta latency, void* data);
49 
50 /* FIXME: Make use of latency if needed */
51 static void nacl_audio_callback(void* samples, uint32_t buffer_size, PP_TimeDelta latency, void* data) {
53 
54  SDL_LockMutex(private->mutex);
55 
56  if (_this->enabled && !_this->paused) {
57  if (_this->convert.needed) {
58  SDL_LockMutex(_this->mixer_lock);
59  (*_this->spec.callback) (_this->spec.userdata,
60  (Uint8 *) _this->convert.buf,
61  _this->convert.len);
63  SDL_ConvertAudio(&_this->convert);
64  SDL_memcpy(samples, _this->convert.buf, _this->convert.len_cvt);
65  } else {
66  SDL_LockMutex(_this->mixer_lock);
67  (*_this->spec.callback) (_this->spec.userdata, (Uint8 *) samples, buffer_size);
69  }
70  } else {
71  SDL_memset(samples, 0, buffer_size);
72  }
73 
74  return;
75 }
76 
77 static void NACLAUD_CloseDevice(SDL_AudioDevice *device) {
78  const PPB_Core *core = PSInterfaceCore();
79  const PPB_Audio *ppb_audio = PSInterfaceAudio();
80  SDL_PrivateAudioData *hidden = (SDL_PrivateAudioData *) device->hidden;
81 
82  ppb_audio->StopPlayback(hidden->audio);
83  SDL_DestroyMutex(hidden->mutex);
84  core->ReleaseResource(hidden->audio);
85 }
86 
87 static int
88 NACLAUD_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) {
89  PP_Instance instance = PSGetInstanceId();
90  const PPB_Audio *ppb_audio = PSInterfaceAudio();
91  const PPB_AudioConfig *ppb_audiocfg = PSInterfaceAudioConfig();
92 
93  private = (SDL_PrivateAudioData *) SDL_calloc(1, (sizeof *private));
94  if (private == NULL) {
96  return 0;
97  }
98 
99  private->mutex = SDL_CreateMutex();
100  _this->spec.freq = 44100;
101  _this->spec.format = AUDIO_S16LSB;
102  _this->spec.channels = 2;
103  _this->spec.samples = ppb_audiocfg->RecommendSampleFrameCount(
104  instance,
105  PP_AUDIOSAMPLERATE_44100,
106  SAMPLE_FRAME_COUNT);
107 
108  /* Calculate the final parameters for this audio specification */
109  SDL_CalculateAudioSpec(&_this->spec);
110 
111  private->audio = ppb_audio->Create(
112  instance,
113  ppb_audiocfg->CreateStereo16Bit(instance, PP_AUDIOSAMPLERATE_44100, _this->spec.samples),
114  nacl_audio_callback,
115  _this);
116 
117  /* Start audio playback while we are still on the main thread. */
118  ppb_audio->StartPlayback(private->audio);
119 
120  return 1;
121 }
122 
123 static int
124 NACLAUD_Init(SDL_AudioDriverImpl * impl)
125 {
126  if (PSGetInstanceId() == 0) {
127  return 0;
128  }
129 
130  /* Set the function pointers */
131  impl->OpenDevice = NACLAUD_OpenDevice;
132  impl->CloseDevice = NACLAUD_CloseDevice;
133  impl->OnlyHasDefaultOutputDevice = 1;
134  impl->ProvidesOwnCallbackThread = 1;
135  /*
136  * impl->WaitDevice = NACLAUD_WaitDevice;
137  * impl->GetDeviceBuf = NACLAUD_GetDeviceBuf;
138  * impl->PlayDevice = NACLAUD_PlayDevice;
139  * impl->Deinitialize = NACLAUD_Deinitialize;
140  */
141 
142  return 1;
143 }
144 
146  NACLAUD_DRIVER_NAME, "SDL NaCl Audio Driver",
147  NACLAUD_Init, 0
148 };
149 
150 #endif /* SDL_AUDIO_DRIVER_NACL */
151 
152 /* vi: set ts=4 sw=4 expandtab: */
struct SDL_PrivateAudioData * hidden
Definition: SDL_sysaudio.h:185
#define SDL_LockMutex
SDL_mutex * mixer_lock
Definition: SDL_sysaudio.h:171
Uint8 * buf
Definition: SDL_audio.h:206
#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:150
#define private
Definition: SDL_naclaudio.h:34
static SDL_VideoDevice * _this
Definition: SDL_video.c:114
#define SDL_memcpy
void * SDL_calloc(size_t nmemb, size_t size)
#define SDL_ConvertAudio
AudioBootStrap NACLAUD_bootstrap
Uint8 channels
Definition: SDL_audio.h:172
#define _THIS
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
void SDL_CalculateAudioSpec(SDL_AudioSpec *spec)
Definition: SDL_audio.c:1419
GLsizei samples
SDL_AudioCallback callback
Definition: SDL_audio.h:177
int(* OpenDevice)(_THIS, void *handle, const char *devname, int iscapture)
Definition: SDL_sysaudio.h:72
#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:79
#define SDL_DestroyMutex
SDL_AudioFormat format
Definition: SDL_audio.h:171
#define AUDIO_S16LSB
Definition: SDL_audio.h:92
void * userdata
Definition: SDL_audio.h:178
#define SDL_UnlockMutex
SDL_AudioCVT convert
Definition: SDL_sysaudio.h:153
#define SDL_memset