src/audio/nacl/SDL_naclaudio.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 02 Jan 2016 10:10:34 -0800
changeset 9998 f67cf37e9cd4
parent 9619 b94b6d0bff0f
child 10234 d188ffeca506
permissions -rw-r--r--
Updated copyright to 2016
     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) {
    52     SDL_AudioDevice* _this = (SDL_AudioDevice*) 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);
    62             SDL_UnlockMutex(_this->mixer_lock);
    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);
    68             SDL_UnlockMutex(_this->mixer_lock);
    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) {
    95         SDL_OutOfMemory();
    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 
   145 AudioBootStrap NACLAUD_bootstrap = {
   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: */