src/audio/android/SDL_androidaudio.c
author Gabriel Jacobo <gabomdq@gmail.com>
Thu, 08 Aug 2013 21:25:09 -0300
changeset 7612 627d571587d3
parent 7191 75360622e65f
child 7613 27e0ae109c71
permissions -rw-r--r--
Start Android Audio Thread after audio buffer and AudioTrack are ready.

Also, it starts the Audio Thread from the native side, putting the code in line
with other backends.
paul@4718
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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
*/
paul@4718
    21
#include "SDL_config.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
slouken@4995
    35
static void * audioDevice;
slouken@4995
    36
paul@4718
    37
static int
paul@4718
    38
AndroidAUD_OpenDevice(_THIS, 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;
paul@4724
    52
gabomdq@7612
    53
    this->hidden = (struct SDL_PrivateAudioData *) SDL_malloc(sizeof(*(this->hidden)));
paul@4724
    54
    if (!this->hidden) {
icculus@7038
    55
        return SDL_OutOfMemory();
paul@4724
    56
    }
paul@4724
    57
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
paul@4724
    58
slouken@4995
    59
    test_format = SDL_FirstAudioFormat(this->spec.format);
slouken@7191
    60
    while (test_format != 0) { /* no "UNKNOWN" constant */
slouken@4995
    61
        if ((test_format == AUDIO_U8) || (test_format == AUDIO_S16LSB)) {
slouken@4995
    62
            this->spec.format = test_format;
paul@4724
    63
            break;
paul@4724
    64
        }
slouken@4995
    65
        test_format = SDL_NextAudioFormat();
paul@4724
    66
    }
slouken@7191
    67
slouken@4995
    68
    if (test_format == 0) {
slouken@7191
    69
        /* Didn't find a compatible format :( */
slouken@7191
    70
        return SDL_SetError("No compatible audio format!");
slouken@4995
    71
    }
slouken@4995
    72
slouken@4995
    73
    if (this->spec.channels > 1) {
slouken@7191
    74
        this->spec.channels = 2;
slouken@4995
    75
    } else {
slouken@7191
    76
        this->spec.channels = 1;
slouken@4995
    77
    }
slouken@4995
    78
slouken@4995
    79
    if (this->spec.freq < 8000) {
slouken@7191
    80
        this->spec.freq = 8000;
slouken@4995
    81
    }
slouken@4995
    82
    if (this->spec.freq > 48000) {
slouken@7191
    83
        this->spec.freq = 48000;
slouken@4995
    84
    }
slouken@4995
    85
slouken@7191
    86
    /* TODO: pass in/return a (Java) device ID, also whether we're opening for input or output */
slouken@4995
    87
    this->spec.samples = Android_JNI_OpenAudioDevice(this->spec.freq, this->spec.format == AUDIO_U8 ? 0 : 1, this->spec.channels, this->spec.samples);
slouken@4995
    88
    SDL_CalculateAudioSpec(&this->spec);
slouken@4995
    89
slouken@4995
    90
    if (this->spec.samples == 0) {
slouken@7191
    91
        /* Init failed? */
slouken@7191
    92
        return SDL_SetError("Java-side initialization failed!");
slouken@4995
    93
    }
gabomdq@7612
    94
    
gabomdq@7612
    95
    /* Audio thread is started here, after audio buffers and Java's AudioTrack are in place and ready to go */
gabomdq@7612
    96
    this->thread = SDL_CreateThread(SDL_RunAudio, "AndroidAudioThread", this);
gabomdq@7612
    97
    if (this->thread == NULL) {
gabomdq@7612
    98
        AndroidAUD_CloseDevice(this);
gabomdq@7612
    99
        return SDL_SetError("Couldn't create audio thread");
gabomdq@7612
   100
    }
slouken@4995
   101
icculus@7038
   102
    return 0;
paul@4718
   103
}
paul@4718
   104
paul@4718
   105
static void
paul@4718
   106
AndroidAUD_PlayDevice(_THIS)
paul@4718
   107
{
slouken@4996
   108
    Android_JNI_WriteAudioBuffer();
paul@4718
   109
}
paul@4718
   110
paul@4718
   111
static Uint8 *
paul@4718
   112
AndroidAUD_GetDeviceBuf(_THIS)
paul@4718
   113
{
slouken@4996
   114
    return Android_JNI_GetAudioBuffer();
paul@4718
   115
}
paul@4718
   116
paul@4718
   117
static void
paul@4718
   118
AndroidAUD_CloseDevice(_THIS)
paul@4718
   119
{
gabomdq@7612
   120
    /* At this point SDL_CloseAudioDevice via close_audio_device took care of terminating the audio thread
gabomdq@7612
   121
       so it's safe to terminate the Java side buffer and AudioTrack
gabomdq@7612
   122
     */
gabomdq@7612
   123
   
slouken@4995
   124
    if (this->hidden != NULL) {
slouken@7191
   125
        SDL_free(this->hidden);
slouken@7191
   126
        this->hidden = NULL;
slouken@4995
   127
    }
slouken@7191
   128
    Android_JNI_CloseAudioDevice();
slouken@4995
   129
slouken@4995
   130
    if (audioDevice == this) {
slouken@7191
   131
        audioDevice = NULL;
slouken@4995
   132
    }
paul@4718
   133
}
paul@4718
   134
paul@4718
   135
static int
paul@4718
   136
AndroidAUD_Init(SDL_AudioDriverImpl * impl)
paul@4718
   137
{
paul@4718
   138
    /* Set the function pointers */
paul@4718
   139
    impl->OpenDevice = AndroidAUD_OpenDevice;
paul@4718
   140
    impl->PlayDevice = AndroidAUD_PlayDevice;
paul@4718
   141
    impl->GetDeviceBuf = AndroidAUD_GetDeviceBuf;
paul@4718
   142
    impl->CloseDevice = AndroidAUD_CloseDevice;
paul@4718
   143
paul@4718
   144
    /* and the capabilities */
slouken@4995
   145
    impl->ProvidesOwnCallbackThread = 1;
slouken@7191
   146
    impl->HasCaptureSupport = 0; /* TODO */
paul@4718
   147
    impl->OnlyHasDefaultOutputDevice = 1;
paul@4718
   148
    impl->OnlyHasDefaultInputDevice = 1;
paul@4718
   149
paul@4718
   150
    return 1;   /* this audio target is available. */
paul@4718
   151
}
paul@4718
   152
paul@4718
   153
AudioBootStrap ANDROIDAUD_bootstrap = {
icculus@5579
   154
    "android", "SDL Android audio driver", AndroidAUD_Init, 0
paul@4718
   155
};
paul@4718
   156
slouken@6044
   157
#endif /* SDL_AUDIO_DRIVER_ANDROID */
slouken@6044
   158
paul@4718
   159
/* vi: set ts=4 sw=4 expandtab: */
gabomdq@7612
   160