Lajos Molnar | b3d5fd2 | 2015-04-22 13:14:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015, 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 | |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame^] | 17 | #ifndef _ANDROID_MEDIA_PLAYBACK_PARAMS_H_ |
| 18 | #define _ANDROID_MEDIA_PLAYBACK_PARAMS_H_ |
Lajos Molnar | b3d5fd2 | 2015-04-22 13:14:34 -0700 | [diff] [blame] | 19 | |
| 20 | #include <media/AudioResamplerPublic.h> |
| 21 | |
| 22 | namespace android { |
| 23 | |
| 24 | // This entire class is inline as it is used from both core and media |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame^] | 25 | struct PlaybackParams { |
Lajos Molnar | b3d5fd2 | 2015-04-22 13:14:34 -0700 | [diff] [blame] | 26 | AudioPlaybackRate audioRate; |
| 27 | bool speedSet; |
| 28 | bool pitchSet; |
| 29 | bool audioFallbackModeSet; |
| 30 | bool audioStretchModeSet; |
| 31 | |
| 32 | struct fields_t { |
| 33 | jclass clazz; |
| 34 | jmethodID constructID; |
| 35 | |
| 36 | jfieldID speed; |
| 37 | jfieldID pitch; |
| 38 | jfieldID audio_fallback_mode; |
| 39 | jfieldID audio_stretch_mode; |
| 40 | jfieldID set; |
| 41 | jint set_speed; |
| 42 | jint set_pitch; |
| 43 | jint set_audio_fallback_mode; |
| 44 | jint set_audio_stretch_mode; |
| 45 | |
| 46 | void init(JNIEnv *env) { |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame^] | 47 | jclass lclazz = env->FindClass("android/media/PlaybackParams"); |
Lajos Molnar | b3d5fd2 | 2015-04-22 13:14:34 -0700 | [diff] [blame] | 48 | if (lclazz == NULL) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | clazz = (jclass)env->NewGlobalRef(lclazz); |
| 53 | if (clazz == NULL) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | constructID = env->GetMethodID(clazz, "<init>", "()V"); |
| 58 | |
| 59 | speed = env->GetFieldID(clazz, "mSpeed", "F"); |
| 60 | pitch = env->GetFieldID(clazz, "mPitch", "F"); |
| 61 | audio_fallback_mode = env->GetFieldID(clazz, "mAudioFallbackMode", "I"); |
| 62 | audio_stretch_mode = env->GetFieldID(clazz, "mAudioStretchMode", "I"); |
| 63 | set = env->GetFieldID(clazz, "mSet", "I"); |
| 64 | |
| 65 | set_speed = |
| 66 | env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "SET_SPEED", "I")); |
| 67 | set_pitch = |
| 68 | env->GetStaticIntField(clazz, env->GetStaticFieldID(clazz, "SET_PITCH", "I")); |
| 69 | set_audio_fallback_mode = env->GetStaticIntField( |
| 70 | clazz, env->GetStaticFieldID(clazz, "SET_AUDIO_FALLBACK_MODE", "I")); |
| 71 | set_audio_stretch_mode = env->GetStaticIntField( |
| 72 | clazz, env->GetStaticFieldID(clazz, "SET_AUDIO_STRETCH_MODE", "I")); |
| 73 | |
| 74 | env->DeleteLocalRef(lclazz); |
| 75 | } |
| 76 | |
| 77 | void exit(JNIEnv *env) { |
| 78 | env->DeleteGlobalRef(clazz); |
| 79 | clazz = NULL; |
| 80 | } |
| 81 | }; |
| 82 | |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame^] | 83 | void fillFromJobject(JNIEnv *env, const fields_t& fields, jobject params) { |
| 84 | audioRate.mSpeed = env->GetFloatField(params, fields.speed); |
| 85 | audioRate.mPitch = env->GetFloatField(params, fields.pitch); |
Lajos Molnar | b3d5fd2 | 2015-04-22 13:14:34 -0700 | [diff] [blame] | 86 | audioRate.mFallbackMode = |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame^] | 87 | (AudioTimestretchFallbackMode)env->GetIntField(params, fields.audio_fallback_mode); |
Lajos Molnar | b3d5fd2 | 2015-04-22 13:14:34 -0700 | [diff] [blame] | 88 | audioRate.mStretchMode = |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame^] | 89 | (AudioTimestretchStretchMode)env->GetIntField(params, fields.audio_stretch_mode); |
| 90 | int set = env->GetIntField(params, fields.set); |
Lajos Molnar | b3d5fd2 | 2015-04-22 13:14:34 -0700 | [diff] [blame] | 91 | |
| 92 | speedSet = set & fields.set_speed; |
| 93 | pitchSet = set & fields.set_pitch; |
| 94 | audioFallbackModeSet = set & fields.set_audio_fallback_mode; |
| 95 | audioStretchModeSet = set & fields.set_audio_stretch_mode; |
| 96 | } |
| 97 | |
| 98 | jobject asJobject(JNIEnv *env, const fields_t& fields) { |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame^] | 99 | jobject params = env->NewObject(fields.clazz, fields.constructID); |
| 100 | if (params == NULL) { |
Lajos Molnar | b3d5fd2 | 2015-04-22 13:14:34 -0700 | [diff] [blame] | 101 | return NULL; |
| 102 | } |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame^] | 103 | env->SetFloatField(params, fields.speed, (jfloat)audioRate.mSpeed); |
| 104 | env->SetFloatField(params, fields.pitch, (jfloat)audioRate.mPitch); |
| 105 | env->SetIntField(params, fields.audio_fallback_mode, (jint)audioRate.mFallbackMode); |
| 106 | env->SetIntField(params, fields.audio_stretch_mode, (jint)audioRate.mStretchMode); |
Lajos Molnar | b3d5fd2 | 2015-04-22 13:14:34 -0700 | [diff] [blame] | 107 | env->SetIntField( |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame^] | 108 | params, fields.set, |
Lajos Molnar | b3d5fd2 | 2015-04-22 13:14:34 -0700 | [diff] [blame] | 109 | (speedSet ? fields.set_speed : 0) |
| 110 | | (pitchSet ? fields.set_pitch : 0) |
| 111 | | (audioFallbackModeSet ? fields.set_audio_fallback_mode : 0) |
| 112 | | (audioStretchModeSet ? fields.set_audio_stretch_mode : 0)); |
| 113 | |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame^] | 114 | return params; |
Lajos Molnar | b3d5fd2 | 2015-04-22 13:14:34 -0700 | [diff] [blame] | 115 | } |
| 116 | }; |
| 117 | |
| 118 | } // namespace android |
| 119 | |
Wei Jia | 2d61e2b | 2015-05-08 15:23:28 -0700 | [diff] [blame^] | 120 | #endif // _ANDROID_MEDIA_PLAYBACK_PARAMS_H_ |