SDL_naclaudio.c (4962B)
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2014 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#include "SDL_naclaudio.h" 24 25#include "SDL_audio.h" 26#include "SDL_mutex.h" 27#include "../SDL_audiomem.h" 28#include "../SDL_audio_c.h" 29#include "../SDL_audiodev_c.h" 30 31#include "ppapi/c/pp_errors.h" 32#include "ppapi/c/pp_instance.h" 33#include "ppapi_simple/ps.h" 34#include "ppapi_simple/ps_interface.h" 35#include "ppapi_simple/ps_event.h" 36 37/* The tag name used by NACL audio */ 38#define NACLAUD_DRIVER_NAME "nacl" 39 40#define SAMPLE_FRAME_COUNT 4096 41 42/* Audio driver functions */ 43static int NACLAUD_OpenDevice(_THIS, const char *devname, int iscapture); 44static void NACLAUD_CloseDevice(_THIS); 45static void nacl_audio_callback(void* samples, uint32_t buffer_size, PP_TimeDelta latency, void* data); 46 47/* FIXME: Make use of latency if needed */ 48static void nacl_audio_callback(void* samples, uint32_t buffer_size, PP_TimeDelta latency, void* data) { 49 SDL_AudioDevice* _this = (SDL_AudioDevice*) data; 50 51 SDL_LockMutex(private->mutex); 52 53 if (_this->enabled && !_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); 59 SDL_UnlockMutex(_this->mixer_lock); 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); 65 SDL_UnlockMutex(_this->mixer_lock); 66 } 67 } else { 68 SDL_memset(samples, 0, buffer_size); 69 } 70 71 return; 72} 73 74static void NACLAUD_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 84static int 85NACLAUD_OpenDevice(_THIS, 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) { 92 SDL_OutOfMemory(); 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 120static int 121NACLAUD_Init(SDL_AudioDriverImpl * impl) 122{ 123 if (PSGetInstanceId() == 0) { 124 return 0; 125 } 126 127 /* Set the function pointers */ 128 impl->OpenDevice = NACLAUD_OpenDevice; 129 impl->CloseDevice = NACLAUD_CloseDevice; 130 impl->HasCaptureSupport = 0; 131 impl->OnlyHasDefaultOutputDevice = 1; 132 impl->OnlyHasDefaultInputDevice = 1; 133 impl->ProvidesOwnCallbackThread = 1; 134 /* 135 * impl->WaitDevice = NACLAUD_WaitDevice; 136 * impl->GetDeviceBuf = NACLAUD_GetDeviceBuf; 137 * impl->PlayDevice = NACLAUD_PlayDevice; 138 * impl->Deinitialize = NACLAUD_Deinitialize; 139 */ 140 141 return 1; 142} 143 144AudioBootStrap NACLAUD_bootstrap = { 145 NACLAUD_DRIVER_NAME, "SDL NaCl Audio Driver", 146 NACLAUD_Init, 0 147};