Skip to content

Latest commit

 

History

History
166 lines (133 loc) · 5.39 KB

SDL_naclaudio.c

File metadata and controls

166 lines (133 loc) · 5.39 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 2, 2017
Jan 2, 2017
3
Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#if SDL_AUDIO_DRIVER_NACL
#include "SDL_naclaudio.h"
#include "SDL_audio.h"
#include "SDL_mutex.h"
#include "../SDL_audio_c.h"
#include "../SDL_audiodev_c.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/c/pp_instance.h"
#include "ppapi_simple/ps.h"
#include "ppapi_simple/ps_interface.h"
#include "ppapi_simple/ps_event.h"
/* The tag name used by NACL audio */
Aug 12, 2016
Aug 12, 2016
40
#define NACLAUDIO_DRIVER_NAME "nacl"
41
42
43
44
45
46
47
#define SAMPLE_FRAME_COUNT 4096
/* Audio driver functions */
static void nacl_audio_callback(void* samples, uint32_t buffer_size, PP_TimeDelta latency, void* data);
/* FIXME: Make use of latency if needed */
Jan 6, 2017
Jan 6, 2017
48
49
static void nacl_audio_callback(void* stream, uint32_t buffer_size, PP_TimeDelta latency, void* data) {
const int len = (int) buffer_size;
50
SDL_AudioDevice* _this = (SDL_AudioDevice*) data;
Jan 6, 2017
Jan 6, 2017
51
SDL_AudioCallback callback = _this->spec.callback;
Aug 1, 2016
Aug 1, 2016
53
SDL_LockMutex(private->mutex); /* !!! FIXME: is this mutex necessary? */
Jan 6, 2017
Jan 6, 2017
55
56
57
58
/* Only do something if audio is enabled */
if (!SDL_AtomicGet(&_this->enabled) || SDL_AtomicGet(&_this->paused)) {
if (_this->stream) {
SDL_AudioStreamClear(_this->stream);
Jan 6, 2017
Jan 6, 2017
60
61
SDL_memset(stream, _this->spec.silence, len);
return;
Jan 6, 2017
Jan 6, 2017
63
64
65
66
67
68
69
70
71
72
SDL_assert(_this->spec.size == len);
if (_this->stream == NULL) { /* no conversion necessary. */
SDL_LockMutex(_this->mixer_lock);
callback(_this->spec.userdata, stream, len);
SDL_UnlockMutex(_this->mixer_lock);
} else { /* streaming/converting */
const int stream_len = _this->callbackspec.size;
while (SDL_AudioStreamAvailable(_this->stream) < len) {
Jan 6, 2017
Jan 6, 2017
73
74
callback(_this->spec.userdata, _this->work_buffer, stream_len);
if (SDL_AudioStreamPut(_this->stream, _this->work_buffer, stream_len) == -1) {
Jan 6, 2017
Jan 6, 2017
75
76
77
78
79
80
SDL_AudioStreamClear(_this->stream);
SDL_AtomicSet(&_this->enabled, 0);
break;
}
}
Jan 6, 2017
Jan 6, 2017
81
const int got = SDL_AudioStreamGet(_this->stream, stream, len);
Jan 6, 2017
Jan 6, 2017
82
83
84
85
86
87
SDL_assert((got < 0) || (got == len));
if (got != len) {
SDL_memset(stream, _this->spec.silence, len);
}
}
Aug 1, 2016
Aug 1, 2016
88
SDL_UnlockMutex(private->mutex);
Aug 12, 2016
Aug 12, 2016
91
static void NACLAUDIO_CloseDevice(SDL_AudioDevice *device) {
92
93
94
95
96
97
98
99
100
101
const PPB_Core *core = PSInterfaceCore();
const PPB_Audio *ppb_audio = PSInterfaceAudio();
SDL_PrivateAudioData *hidden = (SDL_PrivateAudioData *) device->hidden;
ppb_audio->StopPlayback(hidden->audio);
SDL_DestroyMutex(hidden->mutex);
core->ReleaseResource(hidden->audio);
}
static int
Aug 12, 2016
Aug 12, 2016
102
NACLAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture) {
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
PP_Instance instance = PSGetInstanceId();
const PPB_Audio *ppb_audio = PSInterfaceAudio();
const PPB_AudioConfig *ppb_audiocfg = PSInterfaceAudioConfig();
private = (SDL_PrivateAudioData *) SDL_calloc(1, (sizeof *private));
if (private == NULL) {
SDL_OutOfMemory();
return 0;
}
private->mutex = SDL_CreateMutex();
_this->spec.freq = 44100;
_this->spec.format = AUDIO_S16LSB;
_this->spec.channels = 2;
_this->spec.samples = ppb_audiocfg->RecommendSampleFrameCount(
instance,
PP_AUDIOSAMPLERATE_44100,
SAMPLE_FRAME_COUNT);
/* Calculate the final parameters for this audio specification */
SDL_CalculateAudioSpec(&_this->spec);
private->audio = ppb_audio->Create(
instance,
ppb_audiocfg->CreateStereo16Bit(instance, PP_AUDIOSAMPLERATE_44100, _this->spec.samples),
nacl_audio_callback,
_this);
/* Start audio playback while we are still on the main thread. */
ppb_audio->StartPlayback(private->audio);
return 1;
}
static int
Aug 12, 2016
Aug 12, 2016
138
NACLAUDIO_Init(SDL_AudioDriverImpl * impl)
139
140
141
142
143
144
{
if (PSGetInstanceId() == 0) {
return 0;
}
/* Set the function pointers */
Aug 12, 2016
Aug 12, 2016
145
146
impl->OpenDevice = NACLAUDIO_OpenDevice;
impl->CloseDevice = NACLAUDIO_CloseDevice;
147
148
149
impl->OnlyHasDefaultOutputDevice = 1;
impl->ProvidesOwnCallbackThread = 1;
/*
Aug 12, 2016
Aug 12, 2016
150
151
152
153
* impl->WaitDevice = NACLAUDIO_WaitDevice;
* impl->GetDeviceBuf = NACLAUDIO_GetDeviceBuf;
* impl->PlayDevice = NACLAUDIO_PlayDevice;
* impl->Deinitialize = NACLAUDIO_Deinitialize;
154
155
156
157
158
*/
return 1;
}
Aug 12, 2016
Aug 12, 2016
159
160
161
AudioBootStrap NACLAUDIO_bootstrap = {
NACLAUDIO_DRIVER_NAME, "SDL NaCl Audio Driver",
NACLAUDIO_Init, 0
162
163
164
165
166
};
#endif /* SDL_AUDIO_DRIVER_NACL */
/* vi: set ts=4 sw=4 expandtab: */