summaryrefslogtreecommitdiffstats
path: root/firmware/target/hosted/android/pcm-android.c
blob: 4577c8078afa3d5b3de3d91922db9b59150b9c50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (c) 2010 Thomas Martitz
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <jni.h>
#include <stdbool.h>
#define _SYSTEM_WITH_JNI /* for getJavaEnvironment */
#include <pthread.h>
#include "system.h"
#include "kernel.h"
#include "debug.h"
#include "pcm.h"
#include "pcm-internal.h"

extern JNIEnv *env_ptr;

/* infos about our pcm chunks */
static const void *pcm_data_start;
static size_t  pcm_data_size;
static int     audio_locked = 0;
static pthread_mutex_t audio_lock_mutex = PTHREAD_MUTEX_INITIALIZER;

/* cache frequently called methods */
static jmethodID play_pause_method;
static jmethodID stop_method;
static jmethodID set_volume_method;
static jmethodID write_method;
static jclass    RockboxPCM_class;
static jobject   RockboxPCM_instance;


/*
 * mutex lock/unlock wrappers neatness' sake
 */
static inline void lock_audio(void)
{
    pthread_mutex_lock(&audio_lock_mutex);
}

static inline void unlock_audio(void)
{
    pthread_mutex_unlock(&audio_lock_mutex);
}

/*
 * write pcm samples to the hardware. Calls AudioTrack.write directly (which
 * is usually a blocking call)
 *
 * temp_array is not strictly needed as a parameter as we could
 * create it here, but that would result in frequent garbage collection
 *
 * it is called from the PositionMarker callback of AudioTrack
 **/
JNIEXPORT jint JNICALL
Java_org_rockbox_RockboxPCM_nativeWrite(JNIEnv *env, jobject this,
                                        jbyteArray temp_array, jint max_size)
{
    bool new_buffer = false;

    lock_audio();

    jint left = max_size;

    if (!pcm_data_size) /* get some initial data */
    {
        new_buffer = pcm_play_dma_complete_callback(PCM_DMAST_OK,
                            &pcm_data_start, &pcm_data_size);
    }

    while(left > 0 && pcm_data_size)
    {
        jint ret;
        jsize transfer_size = MIN((size_t)left, pcm_data_size);
        /* decrement both by the amount we're going to write */
        pcm_data_size -= transfer_size; left -= transfer_size;
        (*env)->SetByteArrayRegion(env, temp_array, 0,
                                        transfer_size, (jbyte*)pcm_data_start);

        if (new_buffer)
        {
            new_buffer = false;
            pcm_play_dma_status_callback(PCM_DMAST_STARTED);
        }
        /* SetByteArrayRegion copies, which enables us to unlock audio. This
         * is good because the below write() call almost certainly block.
         * This allows the mixer to be clocked at a regular interval which vastly
         * improves responsiveness when pausing/stopping playback */
        unlock_audio();
        ret = (*env)->CallIntMethod(env, this, write_method,
                                            temp_array, 0, transfer_size);
        lock_audio();

        /* check if still playing. might have changed during the write() call */
        if (!pcm_is_playing())
            break;

        if (ret < 0)
        {
            unlock_audio();
            return ret;
        }

        if (pcm_data_size == 0) /* need new data */
        {
            new_buffer = pcm_play_dma_complete_callback(PCM_DMAST_OK,
                                &pcm_data_start, &pcm_data_size);
        }
        else /* increment data pointer and feed more */
            pcm_data_start += transfer_size;
    }

    if (new_buffer)
        pcm_play_dma_status_callback(PCM_DMAST_STARTED);

    unlock_audio();
    return max_size - left;
}

void pcm_play_lock(void)
{
    if (++audio_locked == 1)
        lock_audio();
}

void pcm_play_unlock(void)
{
    if (--audio_locked == 0)
        unlock_audio();
}

void pcm_dma_apply_settings(void)
{
}

void pcm_play_dma_start(const void *addr, size_t size)
{
    pcm_data_start = addr;
    pcm_data_size = size;

    (*env_ptr)->CallVoidMethod(env_ptr,
                               RockboxPCM_instance,
                               play_pause_method,
                               0);
}

void pcm_play_dma_stop(void)
{
    /* NOTE: due to how pcm_play_dma_complete_callback() works, this is
     * possibly called from nativeWrite(), i.e. another (host) thread
     * => need to discover the appropriate JNIEnv* */
    JNIEnv* env = getJavaEnvironment();
    (*env)->CallVoidMethod(env,
                           RockboxPCM_instance,
                           stop_method);
}

size_t pcm_get_bytes_waiting(void)
{
    return pcm_data_size;
}

const void * pcm_play_dma_get_peak_buffer(int *count)
{
    uintptr_t addr = (uintptr_t)pcm_data_start;
    *count = pcm_data_size / 4;
    return (void *)((addr + 3) & ~3);
}

void pcm_play_dma_init(void)
{
    /* in order to have background music playing after leaving the activity,
     * we need to allocate the PCM object from the Rockbox thread (the Activity
     * runs in a separate thread because it would otherwise kill us when
     * stopping it)
     *
     * Luckily we only reference the PCM object from here, so it's safe (and
     * clean) to allocate it here
     **/
    JNIEnv e = *env_ptr;
    /* get the class and its constructor */
    RockboxPCM_class = e->FindClass(env_ptr, "org/rockbox/RockboxPCM");
    jmethodID constructor = e->GetMethodID(env_ptr, RockboxPCM_class, "<init>", "()V");
    /* instance = new RockboxPCM() */
    RockboxPCM_instance = e->NewObject(env_ptr, RockboxPCM_class, constructor);
    /* cache needed methods */
    play_pause_method = e->GetMethodID(env_ptr, RockboxPCM_class, "play_pause", "(Z)V");
    set_volume_method = e->GetMethodID(env_ptr, RockboxPCM_class, "set_volume", "(I)V");
    stop_method       = e->GetMethodID(env_ptr, RockboxPCM_class, "stop", "()V");
    write_method      = e->GetMethodID(env_ptr, RockboxPCM_class, "write", "([BII)I");
}

void pcm_play_dma_postinit(void)
{
}

void pcm_set_mixer_volume(int volume)
{
    (*env_ptr)->CallVoidMethod(env_ptr, RockboxPCM_instance, set_volume_method, volume);
}

/*
 * release audio resources */
void pcm_shutdown(void)
{
    JNIEnv e = *env_ptr;
    jmethodID release = e->GetMethodID(env_ptr, RockboxPCM_class, "release", "()V");
    e->CallVoidMethod(env_ptr, RockboxPCM_instance, release);
    pthread_mutex_destroy(&audio_lock_mutex);
}

JNIEXPORT void JNICALL
Java_org_rockbox_RockboxPCM_postVolumeChangedEvent(JNIEnv *env,
                                                   jobject this,
                                                   jint volume)
{
    (void) env;
    (void) this;
    /* for the main queue, the volume will be available through
     * button_get_data() */
    queue_broadcast(SYS_VOLUME_CHANGED, volume);
}