src/audio/android/SDL_androidaudio.c
author Ryan C. Gordon
Sun, 03 Jan 2016 20:52:44 -0500
changeset 10004 8f2f519d1e61
parent 9998 f67cf37e9cd4
child 10238 6fa358b97f4b
permissions -rw-r--r--
CMake: Don't make a libSDL2.so symlink on Mac OS X (do .dylib instead).
paul@4718
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
paul@4718
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
paul@4718
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
paul@4718
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
paul@4718
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
paul@4718
    22
slouken@6044
    23
#if SDL_AUDIO_DRIVER_ANDROID
slouken@6044
    24
paul@4718
    25
/* Output audio to Android */
paul@4718
    26
paul@4718
    27
#include "SDL_audio.h"
paul@4718
    28
#include "../SDL_audio_c.h"
paul@4718
    29
#include "SDL_androidaudio.h"
slouken@4995
    30
slouken@5090
    31
#include "../../core/android/SDL_android.h"
paul@4724
    32
paul@4718
    33
#include <android/log.h>
paul@4718
    34
gabomdq@9148
    35
static SDL_AudioDevice* audioDevice = NULL;
slouken@4995
    36
paul@4718
    37
static int
icculus@9394
    38
AndroidAUD_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
paul@4718
    39
{
slouken@4995
    40
    SDL_AudioFormat test_format;
philipp@7130
    41
slouken@4995
    42
    if (iscapture) {
slouken@7191
    43
        /* TODO: implement capture */
slouken@7191
    44
        return SDL_SetError("Capture not supported on Android");
slouken@4995
    45
    }
slouken@4995
    46
slouken@4995
    47
    if (audioDevice != NULL) {
slouken@7191
    48
        return SDL_SetError("Only one audio device at a time please!");
slouken@4995
    49
    }
slouken@4995
    50
slouken@4995
    51
    audioDevice = this;
gabomdq@9148
    52
    
gabomdq@9148
    53
    this->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, (sizeof *this->hidden));
gabomdq@9148
    54
    if (this->hidden == NULL) {
gabomdq@9148
    55
        return SDL_OutOfMemory();
gabomdq@9148
    56
    }
paul@4724
    57
slouken@4995
    58
    test_format = SDL_FirstAudioFormat(this->spec.format);
slouken@7191
    59
    while (test_format != 0) { /* no "UNKNOWN" constant */
slouken@4995
    60
        if ((test_format == AUDIO_U8) || (test_format == AUDIO_S16LSB)) {
slouken@4995
    61
            this->spec.format = test_format;
paul@4724
    62
            break;
paul@4724
    63
        }
slouken@4995
    64
        test_format = SDL_NextAudioFormat();
paul@4724
    65
    }
slouken@7191
    66
slouken@4995
    67
    if (test_format == 0) {
slouken@7191
    68
        /* Didn't find a compatible format :( */
slouken@7191
    69
        return SDL_SetError("No compatible audio format!");
slouken@4995
    70
    }
slouken@4995
    71
slouken@4995
    72
    if (this->spec.channels > 1) {
slouken@7191
    73
        this->spec.channels = 2;
slouken@4995
    74
    } else {
slouken@7191
    75
        this->spec.channels = 1;
slouken@4995
    76
    }
slouken@4995
    77
slouken@4995
    78
    if (this->spec.freq < 8000) {
slouken@7191
    79
        this->spec.freq = 8000;
slouken@4995
    80
    }
slouken@4995
    81
    if (this->spec.freq > 48000) {
slouken@7191
    82
        this->spec.freq = 48000;
slouken@4995
    83
    }
slouken@4995
    84
slouken@7191
    85
    /* TODO: pass in/return a (Java) device ID, also whether we're opening for input or output */
slouken@4995
    86
    this->spec.samples = Android_JNI_OpenAudioDevice(this->spec.freq, this->spec.format == AUDIO_U8 ? 0 : 1, this->spec.channels, this->spec.samples);
slouken@4995
    87
    SDL_CalculateAudioSpec(&this->spec);
slouken@4995
    88
slouken@4995
    89
    if (this->spec.samples == 0) {
slouken@7191
    90
        /* Init failed? */
slouken@7191
    91
        return SDL_SetError("Java-side initialization failed!");
slouken@4995
    92
    }
slouken@4995
    93
icculus@7038
    94
    return 0;
paul@4718
    95
}
paul@4718
    96
paul@4718
    97
static void
paul@4718
    98
AndroidAUD_PlayDevice(_THIS)
paul@4718
    99
{
slouken@4996
   100
    Android_JNI_WriteAudioBuffer();
paul@4718
   101
}
paul@4718
   102
paul@4718
   103
static Uint8 *
paul@4718
   104
AndroidAUD_GetDeviceBuf(_THIS)
paul@4718
   105
{
slouken@4996
   106
    return Android_JNI_GetAudioBuffer();
paul@4718
   107
}
paul@4718
   108
paul@4718
   109
static void
paul@4718
   110
AndroidAUD_CloseDevice(_THIS)
paul@4718
   111
{
gabomdq@7612
   112
    /* At this point SDL_CloseAudioDevice via close_audio_device took care of terminating the audio thread
gabomdq@7612
   113
       so it's safe to terminate the Java side buffer and AudioTrack
gabomdq@7612
   114
     */
slouken@7191
   115
    Android_JNI_CloseAudioDevice();
slouken@4995
   116
slouken@4995
   117
    if (audioDevice == this) {
gabomdq@9148
   118
        if (audioDevice->hidden != NULL) {
gabomdq@9148
   119
            SDL_free(this->hidden);
gabomdq@9148
   120
            this->hidden = NULL;
gabomdq@9148
   121
        }
slouken@7191
   122
        audioDevice = NULL;
slouken@4995
   123
    }
paul@4718
   124
}
paul@4718
   125
paul@4718
   126
static int
paul@4718
   127
AndroidAUD_Init(SDL_AudioDriverImpl * impl)
paul@4718
   128
{
paul@4718
   129
    /* Set the function pointers */
paul@4718
   130
    impl->OpenDevice = AndroidAUD_OpenDevice;
paul@4718
   131
    impl->PlayDevice = AndroidAUD_PlayDevice;
paul@4718
   132
    impl->GetDeviceBuf = AndroidAUD_GetDeviceBuf;
paul@4718
   133
    impl->CloseDevice = AndroidAUD_CloseDevice;
paul@4718
   134
paul@4718
   135
    /* and the capabilities */
slouken@7191
   136
    impl->HasCaptureSupport = 0; /* TODO */
paul@4718
   137
    impl->OnlyHasDefaultOutputDevice = 1;
paul@4718
   138
    impl->OnlyHasDefaultInputDevice = 1;
paul@4718
   139
paul@4718
   140
    return 1;   /* this audio target is available. */
paul@4718
   141
}
paul@4718
   142
paul@4718
   143
AudioBootStrap ANDROIDAUD_bootstrap = {
icculus@5579
   144
    "android", "SDL Android audio driver", AndroidAUD_Init, 0
paul@4718
   145
};
paul@4718
   146
gabomdq@9148
   147
/* Pause (block) all non already paused audio devices by taking their mixer lock */
gabomdq@9148
   148
void AndroidAUD_PauseDevices(void)
gabomdq@9148
   149
{
gabomdq@9148
   150
    /* TODO: Handle multiple devices? */
gabomdq@9148
   151
    struct SDL_PrivateAudioData *private;
gabomdq@9148
   152
    if(audioDevice != NULL && audioDevice->hidden != NULL) {
gabomdq@9148
   153
        private = (struct SDL_PrivateAudioData *) audioDevice->hidden;
gabomdq@9148
   154
        if (audioDevice->paused) {
gabomdq@9148
   155
            /* The device is already paused, leave it alone */
gabomdq@9148
   156
            private->resume = SDL_FALSE;
gabomdq@9148
   157
        }
gabomdq@9148
   158
        else {
gabomdq@9148
   159
            SDL_LockMutex(audioDevice->mixer_lock);
gabomdq@9148
   160
            audioDevice->paused = SDL_TRUE;
gabomdq@9148
   161
            private->resume = SDL_TRUE;
gabomdq@9148
   162
        }
gabomdq@9148
   163
    }
gabomdq@9148
   164
}
gabomdq@9148
   165
gabomdq@9148
   166
/* Resume (unblock) all non already paused audio devices by releasing their mixer lock */
gabomdq@9148
   167
void AndroidAUD_ResumeDevices(void)
gabomdq@9148
   168
{
gabomdq@9148
   169
    /* TODO: Handle multiple devices? */
gabomdq@9148
   170
    struct SDL_PrivateAudioData *private;
gabomdq@9148
   171
    if(audioDevice != NULL && audioDevice->hidden != NULL) {
gabomdq@9148
   172
        private = (struct SDL_PrivateAudioData *) audioDevice->hidden;
gabomdq@9148
   173
        if (private->resume) {
gabomdq@9148
   174
            audioDevice->paused = SDL_FALSE;
gabomdq@9148
   175
            private->resume = SDL_FALSE;
gabomdq@9148
   176
            SDL_UnlockMutex(audioDevice->mixer_lock);
gabomdq@9148
   177
        }
gabomdq@9148
   178
    }
gabomdq@9148
   179
}
gabomdq@9148
   180
gabomdq@9148
   181
slouken@6044
   182
#endif /* SDL_AUDIO_DRIVER_ANDROID */
slouken@6044
   183
paul@4718
   184
/* vi: set ts=4 sw=4 expandtab: */
gabomdq@7612
   185