Hugo Hudson | b83ad73 | 2011-07-14 23:31:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | #include <assert.h> |
| 19 | |
| 20 | #include <jni.h> |
| 21 | #include <variablespeed.h> |
| 22 | |
| 23 | // Quick #define to make sure I get all the JNI method calls right. |
| 24 | #define JNI_METHOD(x, y) \ |
| 25 | JNIEXPORT y JNICALL \ |
| 26 | Java_com_android_ex_variablespeed_VariableSpeedNative_##x |
| 27 | |
| 28 | class MethodLog { |
| 29 | public: |
| 30 | explicit MethodLog(const char* name) : name_(name) { |
Hugo Hudson | 0bd6ec5 | 2011-07-26 20:05:25 +0100 | [diff] [blame] | 31 | LOGV("+ %s", name); |
Hugo Hudson | b83ad73 | 2011-07-14 23:31:17 +0100 | [diff] [blame] | 32 | } |
| 33 | virtual ~MethodLog() { |
Hugo Hudson | 0bd6ec5 | 2011-07-26 20:05:25 +0100 | [diff] [blame] | 34 | LOGV("- %s", name_); |
Hugo Hudson | b83ad73 | 2011-07-14 23:31:17 +0100 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | private: |
| 38 | const char* name_; |
| 39 | }; |
| 40 | |
| 41 | extern "C" { |
Ashok Bhat | 787277e | 2014-02-12 13:40:47 +0000 | [diff] [blame] | 42 | JNI_METHOD(playFileDescriptor, void) (JNIEnv*, jclass, jint fd, jlong offset, |
Hugo Hudson | b83ad73 | 2011-07-14 23:31:17 +0100 | [diff] [blame] | 43 | jlong length) { |
| 44 | MethodLog _("playFileDescriptor"); |
| 45 | AudioEngine::GetEngine()->PlayFileDescriptor(fd, offset, length); |
| 46 | } |
| 47 | |
| 48 | JNI_METHOD(playUri, void) (JNIEnv* env, jclass, jstring uri) { |
| 49 | MethodLog _("playUri"); |
| 50 | const char* utf8 = env->GetStringUTFChars(uri, NULL); |
| 51 | CHECK(NULL != utf8); |
| 52 | AudioEngine::GetEngine()->PlayUri(utf8); |
| 53 | } |
| 54 | |
Ashok Bhat | 787277e | 2014-02-12 13:40:47 +0000 | [diff] [blame] | 55 | JNI_METHOD(setVariableSpeed, void) (JNIEnv*, jclass, jfloat speed) { |
Hugo Hudson | b83ad73 | 2011-07-14 23:31:17 +0100 | [diff] [blame] | 56 | MethodLog _("setVariableSpeed"); |
| 57 | AudioEngine::GetEngine()->SetVariableSpeed(speed); |
| 58 | } |
| 59 | |
| 60 | JNI_METHOD(startPlayback, void) (JNIEnv*, jclass) { |
| 61 | MethodLog _("startPlayback"); |
| 62 | AudioEngine::GetEngine()->RequestStart(); |
| 63 | } |
| 64 | |
| 65 | JNI_METHOD(stopPlayback, void) (JNIEnv*, jclass) { |
| 66 | MethodLog _("stopPlayback"); |
| 67 | AudioEngine::GetEngine()->RequestStop(); |
| 68 | } |
| 69 | |
Ashok Bhat | 787277e | 2014-02-12 13:40:47 +0000 | [diff] [blame] | 70 | JNI_METHOD(getCurrentPosition, jint) (JNIEnv*, jclass) { |
Hugo Hudson | b83ad73 | 2011-07-14 23:31:17 +0100 | [diff] [blame] | 71 | return AudioEngine::GetEngine()->GetCurrentPosition(); |
| 72 | } |
| 73 | |
Ashok Bhat | 787277e | 2014-02-12 13:40:47 +0000 | [diff] [blame] | 74 | JNI_METHOD(getTotalDuration, jint) (JNIEnv*, jclass) { |
Hugo Hudson | b83ad73 | 2011-07-14 23:31:17 +0100 | [diff] [blame] | 75 | return AudioEngine::GetEngine()->GetTotalDuration(); |
| 76 | } |
| 77 | |
Hugo Hudson | 0bd6ec5 | 2011-07-26 20:05:25 +0100 | [diff] [blame] | 78 | JNI_METHOD(initializeEngine, void) (JNIEnv*, jclass, |
Ashok Bhat | 787277e | 2014-02-12 13:40:47 +0000 | [diff] [blame] | 79 | jint targetFrames, jfloat windowDuration, |
| 80 | jfloat windowOverlapDuration, jint maxPlayBufferCount, |
| 81 | jfloat initialRate, jint decodeInitialSize, jint decodeMaxSize, |
| 82 | jint startPositionMillis, jint audioStreamType) { |
Hugo Hudson | b83ad73 | 2011-07-14 23:31:17 +0100 | [diff] [blame] | 83 | MethodLog _("initializeEngine"); |
Jay Shrauner | 8502b72 | 2014-02-26 10:08:37 -0800 | [diff] [blame] | 84 | AudioEngine *engine = new AudioEngine(targetFrames, |
Hugo Hudson | b83ad73 | 2011-07-14 23:31:17 +0100 | [diff] [blame] | 85 | windowDuration, windowOverlapDuration, maxPlayBufferCount, initialRate, |
Jay Shrauner | 8502b72 | 2014-02-26 10:08:37 -0800 | [diff] [blame] | 86 | decodeInitialSize, decodeMaxSize, startPositionMillis, audioStreamType); |
| 87 | if (!AudioEngine::CompareAndSetEngine(NULL, engine)) { |
| 88 | delete engine; |
| 89 | } |
Hugo Hudson | b83ad73 | 2011-07-14 23:31:17 +0100 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | JNI_METHOD(shutdownEngine, void) (JNIEnv*, jclass) { |
| 93 | MethodLog _("shutdownEngine"); |
| 94 | AudioEngine::DeleteEngine(); |
| 95 | } |
| 96 | } // extern "C" |