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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
/* -*- Mode: c++; c-basic-offset: 2; tab-width: 20; indent-tabs-mode: nil; -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "base/basictypes.h"
#include "AndroidBridge.h"
#include <android/log.h>
#include <stdlib.h>
#include <time.h>
#include "assert.h"
#include "ANPBase.h"
#include "nsIThread.h"
#include "nsThreadUtils.h"
#include "mozilla/Mutex.h"
#define LOG(args...) __android_log_print(ANDROID_LOG_INFO, "GeckoPluginsAudio" , ## args)
#define ASSIGN(obj, name) (obj)->name = anp_audio_##name
/* android.media.AudioTrack */
struct AudioTrack {
jclass at_class;
jmethodID constructor;
jmethodID flush;
jmethodID pause;
jmethodID play;
jmethodID setvol;
jmethodID stop;
jmethodID write;
jmethodID getpos;
jmethodID getstate;
jmethodID release;
};
enum AudioTrackMode {
MODE_STATIC = 0,
MODE_STREAM = 1
};
/* android.media.AudioManager */
enum AudioManagerStream {
STREAM_VOICE_CALL = 0,
STREAM_SYSTEM = 1,
STREAM_RING = 2,
STREAM_MUSIC = 3,
STREAM_ALARM = 4,
STREAM_NOTIFICATION = 5,
STREAM_DTMF = 8
};
/* android.media.AudioFormat */
enum AudioFormatChannel {
CHANNEL_OUT_MONO = 4,
CHANNEL_OUT_STEREO = 12
};
enum AudioFormatEncoding {
ENCODING_PCM_16BIT = 2,
ENCODING_PCM_8BIT = 3
};
enum AudioFormatState {
STATE_UNINITIALIZED = 0,
STATE_INITIALIZED = 1,
STATE_NO_STATIC_DATA = 2
};
static struct AudioTrack at;
static jclass
init_jni_bindings(JNIEnv *jenv) {
jclass jc =
(jclass)jenv->NewGlobalRef(jenv->FindClass("android/media/AudioTrack"));
at.constructor = jenv->GetMethodID(jc, "<init>", "(IIIIII)V");
at.flush = jenv->GetMethodID(jc, "flush", "()V");
at.pause = jenv->GetMethodID(jc, "pause", "()V");
at.play = jenv->GetMethodID(jc, "play", "()V");
at.setvol = jenv->GetMethodID(jc, "setStereoVolume", "(FF)I");
at.stop = jenv->GetMethodID(jc, "stop", "()V");
at.write = jenv->GetMethodID(jc, "write", "([BII)I");
at.getpos = jenv->GetMethodID(jc, "getPlaybackHeadPosition", "()I");
at.getstate = jenv->GetMethodID(jc, "getState", "()I");
at.release = jenv->GetMethodID(jc, "release", "()V");
return jc;
}
struct ANPAudioTrack {
jobject output_unit;
jclass at_class;
unsigned int rate;
unsigned int channels;
unsigned int bufferSize;
unsigned int isStopped;
unsigned int keepGoing;
mozilla::Mutex lock;
void* user;
ANPAudioCallbackProc proc;
ANPSampleFormat format;
ANPAudioTrack() : lock("ANPAudioTrack") { }
};
class AudioRunnable : public mozilla::Runnable
{
public:
NS_DECL_NSIRUNNABLE
AudioRunnable(ANPAudioTrack* aAudioTrack) {
mTrack = aAudioTrack;
}
ANPAudioTrack* mTrack;
};
NS_IMETHODIMP
AudioRunnable::Run()
{
PR_SetCurrentThreadName("Android Audio");
JNIEnv* const jenv = mozilla::jni::GetEnvForThread();
mozilla::AutoLocalJNIFrame autoFrame(jenv, 2);
jbyteArray bytearray = jenv->NewByteArray(mTrack->bufferSize);
if (!bytearray) {
LOG("AudioRunnable:: Run. Could not create bytearray");
return NS_ERROR_FAILURE;
}
jbyte *byte = jenv->GetByteArrayElements(bytearray, nullptr);
if (!byte) {
LOG("AudioRunnable:: Run. Could not create bytearray");
return NS_ERROR_FAILURE;
}
ANPAudioBuffer buffer;
buffer.channelCount = mTrack->channels;
buffer.format = mTrack->format;
buffer.bufferData = (void*) byte;
while (true)
{
// reset the buffer size
buffer.size = mTrack->bufferSize;
{
mozilla::MutexAutoLock lock(mTrack->lock);
if (!mTrack->keepGoing)
break;
// Get data from the plugin
mTrack->proc(kMoreData_ANPAudioEvent, mTrack->user, &buffer);
}
if (buffer.size == 0) {
LOG("%p - kMoreData_ANPAudioEvent", mTrack);
continue;
}
size_t wroteSoFar = 0;
jint retval;
do {
retval = jenv->CallIntMethod(mTrack->output_unit,
at.write,
bytearray,
wroteSoFar,
buffer.size - wroteSoFar);
if (retval < 0) {
LOG("%p - Write failed %d", mTrack, retval);
break;
}
wroteSoFar += retval;
} while(wroteSoFar < buffer.size);
}
jenv->CallVoidMethod(mTrack->output_unit, at.release);
jenv->DeleteGlobalRef(mTrack->output_unit);
jenv->DeleteGlobalRef(mTrack->at_class);
delete mTrack;
jenv->ReleaseByteArrayElements(bytearray, byte, 0);
return NS_OK;
}
ANPAudioTrack*
anp_audio_newTrack(uint32_t sampleRate, // sampling rate in Hz
ANPSampleFormat format,
int channelCount, // MONO=1, STEREO=2
ANPAudioCallbackProc proc,
void* user)
{
ANPAudioTrack *s = new ANPAudioTrack();
if (s == nullptr) {
return nullptr;
}
JNIEnv* const jenv = mozilla::jni::GetEnvForThread();
s->at_class = init_jni_bindings(jenv);
s->rate = sampleRate;
s->channels = channelCount;
s->bufferSize = s->rate * s->channels;
s->isStopped = true;
s->keepGoing = false;
s->user = user;
s->proc = proc;
s->format = format;
int jformat;
switch (format) {
case kPCM16Bit_ANPSampleFormat:
jformat = ENCODING_PCM_16BIT;
break;
case kPCM8Bit_ANPSampleFormat:
jformat = ENCODING_PCM_8BIT;
break;
default:
LOG("Unknown audio format. defaulting to 16bit.");
jformat = ENCODING_PCM_16BIT;
break;
}
int jChannels;
switch (channelCount) {
case 1:
jChannels = CHANNEL_OUT_MONO;
break;
case 2:
jChannels = CHANNEL_OUT_STEREO;
break;
default:
LOG("Unknown channel count. defaulting to mono.");
jChannels = CHANNEL_OUT_MONO;
break;
}
mozilla::AutoLocalJNIFrame autoFrame(jenv);
jobject obj = jenv->NewObject(s->at_class,
at.constructor,
STREAM_MUSIC,
s->rate,
jChannels,
jformat,
s->bufferSize,
MODE_STREAM);
if (autoFrame.CheckForException() || obj == nullptr) {
jenv->DeleteGlobalRef(s->at_class);
delete s;
return nullptr;
}
jint state = jenv->CallIntMethod(obj, at.getstate);
if (autoFrame.CheckForException() || state == STATE_UNINITIALIZED) {
jenv->DeleteGlobalRef(s->at_class);
delete s;
return nullptr;
}
s->output_unit = jenv->NewGlobalRef(obj);
return s;
}
void
anp_audio_deleteTrack(ANPAudioTrack* s)
{
if (s == nullptr) {
return;
}
mozilla::MutexAutoLock lock(s->lock);
s->keepGoing = false;
// deallocation happens in the AudioThread. There is a
// potential leak if anp_audio_start is never called, but
// we do not see that from flash.
}
void
anp_audio_start(ANPAudioTrack* s)
{
if (s == nullptr || s->output_unit == nullptr) {
return;
}
if (s->keepGoing) {
// we are already playing. Ignore.
return;
}
JNIEnv* const jenv = mozilla::jni::GetEnvForThread();
mozilla::AutoLocalJNIFrame autoFrame(jenv, 0);
jenv->CallVoidMethod(s->output_unit, at.play);
if (autoFrame.CheckForException()) {
jenv->DeleteGlobalRef(s->at_class);
delete s;
return;
}
s->isStopped = false;
s->keepGoing = true;
// AudioRunnable now owns the ANPAudioTrack
RefPtr<AudioRunnable> runnable = new AudioRunnable(s);
nsCOMPtr<nsIThread> thread;
NS_NewThread(getter_AddRefs(thread), runnable);
}
void
anp_audio_pause(ANPAudioTrack* s)
{
if (s == nullptr || s->output_unit == nullptr) {
return;
}
JNIEnv* const jenv = mozilla::jni::GetEnvForThread();
mozilla::AutoLocalJNIFrame autoFrame(jenv, 0);
jenv->CallVoidMethod(s->output_unit, at.pause);
}
void
anp_audio_stop(ANPAudioTrack* s)
{
if (s == nullptr || s->output_unit == nullptr) {
return;
}
s->isStopped = true;
JNIEnv* const jenv = mozilla::jni::GetEnvForThread();
mozilla::AutoLocalJNIFrame autoFrame(jenv, 0);
jenv->CallVoidMethod(s->output_unit, at.stop);
}
bool
anp_audio_isStopped(ANPAudioTrack* s)
{
return s->isStopped;
}
uint32_t
anp_audio_trackLatency(ANPAudioTrack* s) {
// Hardcode an estimate of the system's audio latency. Flash hardcodes
// similar latency estimates for pre-Honeycomb devices that do not support
// ANPAudioTrackInterfaceV1's trackLatency(). The Android stock browser
// calls android::AudioTrack::latency(), an internal Android API that is
// not available in the public NDK:
// https://github.com/android/platform_external_webkit/commit/49bf866973cb3b2a6c74c0eab864e9562e4cbab1
return 100; // milliseconds
}
void InitAudioTrackInterfaceV0(ANPAudioTrackInterfaceV0 *i) {
_assert(i->inSize == sizeof(*i));
ASSIGN(i, newTrack);
ASSIGN(i, deleteTrack);
ASSIGN(i, start);
ASSIGN(i, pause);
ASSIGN(i, stop);
ASSIGN(i, isStopped);
}
void InitAudioTrackInterfaceV1(ANPAudioTrackInterfaceV1 *i) {
_assert(i->inSize == sizeof(*i));
ASSIGN(i, newTrack);
ASSIGN(i, deleteTrack);
ASSIGN(i, start);
ASSIGN(i, pause);
ASSIGN(i, stop);
ASSIGN(i, isStopped);
ASSIGN(i, trackLatency);
}
|