src/audio/android/SDL_androidaudio.c
author Sam Lantinga
Sat, 06 Oct 2012 11:23:47 -0700
changeset 6565 1f3c0df426dc
parent 6138 4c64952a58fb
child 6885 700f1b25f77f
permissions -rw-r--r--
When using Xinerama, XVidMode always works on screen 0. Otherwise use the real X11 screen.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2012 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 #include "SDL_config.h"
    22 
    23 #if SDL_AUDIO_DRIVER_ANDROID
    24 
    25 /* Output audio to Android */
    26 
    27 #include "SDL_audio.h"
    28 #include "../SDL_audio_c.h"
    29 #include "SDL_androidaudio.h"
    30 
    31 #include "../../core/android/SDL_android.h"
    32 
    33 #include <android/log.h>
    34 
    35 static void * audioDevice;
    36 
    37 static int
    38 AndroidAUD_OpenDevice(_THIS, const char *devname, int iscapture)
    39 {
    40     SDL_AudioFormat test_format;
    41     int valid_datatype = 0;
    42     
    43     if (iscapture) {
    44     	//TODO: implement capture
    45     	SDL_SetError("Capture not supported on Android");
    46     	return 0;
    47     }
    48 
    49     if (audioDevice != NULL) {
    50     	SDL_SetError("Only one audio device at a time please!");
    51     	return 0;
    52     }
    53 
    54     audioDevice = this;
    55 
    56     this->hidden = SDL_malloc(sizeof(*(this->hidden)));
    57     if (!this->hidden) {
    58         SDL_OutOfMemory();
    59         return 0;
    60     }
    61     SDL_memset(this->hidden, 0, (sizeof *this->hidden));
    62 
    63     test_format = SDL_FirstAudioFormat(this->spec.format);
    64     while (test_format != 0) { // no "UNKNOWN" constant
    65         if ((test_format == AUDIO_U8) || (test_format == AUDIO_S16LSB)) {
    66             this->spec.format = test_format;
    67             break;
    68         }
    69         test_format = SDL_NextAudioFormat();
    70     }
    71     
    72     if (test_format == 0) {
    73     	// Didn't find a compatible format :(
    74     	SDL_SetError("No compatible audio format!");
    75     	return 0;
    76     }
    77 
    78     if (this->spec.channels > 1) {
    79     	this->spec.channels = 2;
    80     } else {
    81     	this->spec.channels = 1;
    82     }
    83 
    84     if (this->spec.freq < 8000) {
    85     	this->spec.freq = 8000;
    86     }
    87     if (this->spec.freq > 48000) {
    88     	this->spec.freq = 48000;
    89     }
    90 
    91     // TODO: pass in/return a (Java) device ID, also whether we're opening for input or output
    92     this->spec.samples = Android_JNI_OpenAudioDevice(this->spec.freq, this->spec.format == AUDIO_U8 ? 0 : 1, this->spec.channels, this->spec.samples);
    93     SDL_CalculateAudioSpec(&this->spec);
    94 
    95     if (this->spec.samples == 0) {
    96     	// Init failed?
    97     	SDL_SetError("Java-side initialization failed!");
    98     	return 0;
    99     }
   100 
   101     return 1;
   102 }
   103 
   104 static void
   105 AndroidAUD_PlayDevice(_THIS)
   106 {
   107     Android_JNI_WriteAudioBuffer();
   108 }
   109 
   110 static Uint8 *
   111 AndroidAUD_GetDeviceBuf(_THIS)
   112 {
   113     return Android_JNI_GetAudioBuffer();
   114 }
   115 
   116 static void
   117 AndroidAUD_CloseDevice(_THIS)
   118 {
   119     if (this->hidden != NULL) {
   120     	SDL_free(this->hidden);
   121     	this->hidden = NULL;
   122     }
   123 	Android_JNI_CloseAudioDevice();
   124 
   125     if (audioDevice == this) {
   126     	audioDevice = NULL;
   127     }
   128 }
   129 
   130 static int
   131 AndroidAUD_Init(SDL_AudioDriverImpl * impl)
   132 {
   133     /* Set the function pointers */
   134     impl->OpenDevice = AndroidAUD_OpenDevice;
   135     impl->PlayDevice = AndroidAUD_PlayDevice;
   136     impl->GetDeviceBuf = AndroidAUD_GetDeviceBuf;
   137     impl->CloseDevice = AndroidAUD_CloseDevice;
   138 
   139     /* and the capabilities */
   140     impl->ProvidesOwnCallbackThread = 1;
   141     impl->HasCaptureSupport = 0; //TODO
   142     impl->OnlyHasDefaultOutputDevice = 1;
   143     impl->OnlyHasDefaultInputDevice = 1;
   144 
   145     return 1;   /* this audio target is available. */
   146 }
   147 
   148 AudioBootStrap ANDROIDAUD_bootstrap = {
   149     "android", "SDL Android audio driver", AndroidAUD_Init, 0
   150 };
   151 
   152 /* Called by the Java code to start the audio processing on a thread */
   153 void
   154 Android_RunAudioThread()
   155 {
   156 	SDL_RunAudio(audioDevice);
   157 }
   158 
   159 #endif /* SDL_AUDIO_DRIVER_ANDROID */
   160 
   161 /* vi: set ts=4 sw=4 expandtab: */