2010-08-02 20:34:47 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* 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>
|
2011-03-16 14:33:55 +00:00
|
|
|
#define _SYSTEM_WITH_JNI /* for getJavaEnvironment */
|
2011-06-29 06:37:04 +00:00
|
|
|
#include <pthread.h>
|
2014-01-05 00:22:19 +00:00
|
|
|
#include "system.h"
|
|
|
|
#include "kernel.h"
|
2010-09-20 17:38:47 +00:00
|
|
|
#include "debug.h"
|
2010-08-02 20:34:47 +00:00
|
|
|
#include "pcm.h"
|
2011-06-29 06:37:04 +00:00
|
|
|
#include "pcm-internal.h"
|
2010-08-02 20:34:47 +00:00
|
|
|
|
2011-03-16 14:33:55 +00:00
|
|
|
extern JNIEnv *env_ptr;
|
|
|
|
|
2010-08-02 20:34:47 +00:00
|
|
|
/* infos about our pcm chunks */
|
2012-03-04 13:52:50 +00:00
|
|
|
static const void *pcm_data_start;
|
2010-08-02 20:34:47 +00:00
|
|
|
static size_t pcm_data_size;
|
2011-06-29 06:37:04 +00:00
|
|
|
static int audio_locked = 0;
|
|
|
|
static pthread_mutex_t audio_lock_mutex = PTHREAD_MUTEX_INITIALIZER;
|
2010-08-02 20:34:47 +00:00
|
|
|
|
|
|
|
/* cache frequently called methods */
|
|
|
|
static jmethodID play_pause_method;
|
|
|
|
static jmethodID stop_method;
|
|
|
|
static jmethodID set_volume_method;
|
2011-06-05 09:44:57 +00:00
|
|
|
static jmethodID write_method;
|
2011-03-16 15:17:24 +00:00
|
|
|
static jclass RockboxPCM_class;
|
2010-08-02 20:34:47 +00:00
|
|
|
static jobject RockboxPCM_instance;
|
|
|
|
|
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2010-08-02 20:34:47 +00:00
|
|
|
/*
|
2011-06-05 09:44:57 +00:00
|
|
|
* write pcm samples to the hardware. Calls AudioTrack.write directly (which
|
|
|
|
* is usually a blocking call)
|
2010-08-02 20:34:47 +00:00
|
|
|
*
|
2011-06-05 09:44:57 +00:00
|
|
|
* temp_array is not strictly needed as a parameter as we could
|
|
|
|
* create it here, but that would result in frequent garbage collection
|
2010-08-02 20:34:47 +00:00
|
|
|
*
|
|
|
|
* it is called from the PositionMarker callback of AudioTrack
|
|
|
|
**/
|
2011-06-05 09:44:57 +00:00
|
|
|
JNIEXPORT jint JNICALL
|
|
|
|
Java_org_rockbox_RockboxPCM_nativeWrite(JNIEnv *env, jobject this,
|
|
|
|
jbyteArray temp_array, jint max_size)
|
2010-08-02 20:34:47 +00:00
|
|
|
{
|
2011-06-29 06:37:04 +00:00
|
|
|
bool new_buffer = false;
|
|
|
|
|
|
|
|
lock_audio();
|
|
|
|
|
2011-06-05 09:44:57 +00:00
|
|
|
jint left = max_size;
|
|
|
|
|
|
|
|
if (!pcm_data_size) /* get some initial data */
|
2011-06-29 06:37:04 +00:00
|
|
|
{
|
2012-02-23 13:14:46 +00:00
|
|
|
new_buffer = pcm_play_dma_complete_callback(PCM_DMAST_OK,
|
2012-03-04 13:52:50 +00:00
|
|
|
&pcm_data_start, &pcm_data_size);
|
2011-06-29 06:37:04 +00:00
|
|
|
}
|
2011-06-05 09:44:57 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2011-06-29 06:37:04 +00:00
|
|
|
if (new_buffer)
|
|
|
|
{
|
|
|
|
new_buffer = false;
|
2012-02-23 13:14:46 +00:00
|
|
|
pcm_play_dma_status_callback(PCM_DMAST_STARTED);
|
2011-06-29 06:37:04 +00:00
|
|
|
}
|
2013-03-16 21:35:54 +00:00
|
|
|
/* 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;
|
2011-06-29 06:37:04 +00:00
|
|
|
|
2011-06-05 09:44:57 +00:00
|
|
|
if (ret < 0)
|
2011-06-29 06:37:04 +00:00
|
|
|
{
|
|
|
|
unlock_audio();
|
2011-06-05 09:44:57 +00:00
|
|
|
return ret;
|
2011-06-29 06:37:04 +00:00
|
|
|
}
|
2011-06-05 09:44:57 +00:00
|
|
|
|
|
|
|
if (pcm_data_size == 0) /* need new data */
|
2011-06-29 06:37:04 +00:00
|
|
|
{
|
2012-02-23 13:14:46 +00:00
|
|
|
new_buffer = pcm_play_dma_complete_callback(PCM_DMAST_OK,
|
2012-03-04 13:52:50 +00:00
|
|
|
&pcm_data_start, &pcm_data_size);
|
2011-06-29 06:37:04 +00:00
|
|
|
}
|
2011-06-05 09:44:57 +00:00
|
|
|
else /* increment data pointer and feed more */
|
|
|
|
pcm_data_start += transfer_size;
|
2010-08-02 20:34:47 +00:00
|
|
|
}
|
2011-06-29 06:37:04 +00:00
|
|
|
|
2012-02-23 13:14:46 +00:00
|
|
|
if (new_buffer)
|
|
|
|
pcm_play_dma_status_callback(PCM_DMAST_STARTED);
|
2011-06-29 06:37:04 +00:00
|
|
|
|
|
|
|
unlock_audio();
|
2011-06-05 09:44:57 +00:00
|
|
|
return max_size - left;
|
2010-08-02 20:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_play_lock(void)
|
|
|
|
{
|
2011-06-29 06:37:04 +00:00
|
|
|
if (++audio_locked == 1)
|
|
|
|
lock_audio();
|
2010-08-02 20:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_play_unlock(void)
|
|
|
|
{
|
2011-06-29 06:37:04 +00:00
|
|
|
if (--audio_locked == 0)
|
|
|
|
unlock_audio();
|
2010-08-02 20:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_dma_apply_settings(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_play_dma_start(const void *addr, size_t size)
|
|
|
|
{
|
2012-03-04 13:52:50 +00:00
|
|
|
pcm_data_start = addr;
|
2010-08-02 20:34:47 +00:00
|
|
|
pcm_data_size = size;
|
|
|
|
|
|
|
|
pcm_play_dma_pause(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_play_dma_stop(void)
|
|
|
|
{
|
2012-02-23 13:14:46 +00:00
|
|
|
/* NOTE: due to how pcm_play_dma_complete_callback() works, this is
|
2011-08-14 15:21:26 +00:00
|
|
|
* possibly called from nativeWrite(), i.e. another (host) thread
|
|
|
|
* => need to discover the appropriate JNIEnv* */
|
2011-03-16 14:33:55 +00:00
|
|
|
JNIEnv* env = getJavaEnvironment();
|
|
|
|
(*env)->CallVoidMethod(env,
|
|
|
|
RockboxPCM_instance,
|
|
|
|
stop_method);
|
2010-08-02 20:34:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_play_dma_pause(bool pause)
|
|
|
|
{
|
|
|
|
(*env_ptr)->CallVoidMethod(env_ptr,
|
|
|
|
RockboxPCM_instance,
|
|
|
|
play_pause_method,
|
|
|
|
(int)pause);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 */
|
2011-03-16 15:17:24 +00:00
|
|
|
RockboxPCM_class = e->FindClass(env_ptr, "org/rockbox/RockboxPCM");
|
2010-08-02 20:34:47 +00:00
|
|
|
jmethodID constructor = e->GetMethodID(env_ptr, RockboxPCM_class, "<init>", "()V");
|
|
|
|
/* instance = new RockboxPCM() */
|
2011-03-11 19:23:00 +00:00
|
|
|
RockboxPCM_instance = e->NewObject(env_ptr, RockboxPCM_class, constructor);
|
2010-08-02 20:34:47 +00:00
|
|
|
/* 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");
|
2011-06-05 09:44:57 +00:00
|
|
|
write_method = e->GetMethodID(env_ptr, RockboxPCM_class, "write", "([BII)I");
|
2010-08-02 20:34:47 +00:00
|
|
|
}
|
|
|
|
|
2011-09-01 12:15:43 +00:00
|
|
|
void pcm_play_dma_postinit(void)
|
2010-08-02 20:34:47 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void pcm_set_mixer_volume(int volume)
|
|
|
|
{
|
|
|
|
(*env_ptr)->CallVoidMethod(env_ptr, RockboxPCM_instance, set_volume_method, volume);
|
|
|
|
}
|
2011-03-14 12:25:48 +00:00
|
|
|
|
2011-03-16 15:17:24 +00:00
|
|
|
/*
|
|
|
|
* 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);
|
2011-06-29 06:37:04 +00:00
|
|
|
pthread_mutex_destroy(&audio_lock_mutex);
|
2011-03-16 15:17:24 +00:00
|
|
|
}
|
2011-03-14 12:25:48 +00:00
|
|
|
|
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
Java_org_rockbox_RockboxPCM_postVolumeChangedEvent(JNIEnv *env,
|
|
|
|
jobject this,
|
|
|
|
jint volume)
|
|
|
|
{
|
|
|
|
(void) env;
|
|
|
|
(void) this;
|
2011-08-14 13:38:10 +00:00
|
|
|
/* for the main queue, the volume will be available through
|
|
|
|
* button_get_data() */
|
|
|
|
queue_broadcast(SYS_VOLUME_CHANGED, volume);
|
2011-03-14 12:25:48 +00:00
|
|
|
}
|