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 17 #ifndef _ANDROID_MEDIA_PLAYBACK_PARAMS_H_ 18 #define _ANDROID_MEDIA_PLAYBACK_PARAMS_H_ 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 25 struct PlaybackParams { 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 initPlaybackParams::fields_t46 void init(JNIEnv *env) { 47 jclass lclazz = env->FindClass("android/media/PlaybackParams"); 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 exitPlaybackParams::fields_t77 void exit(JNIEnv *env) { 78 env->DeleteGlobalRef(clazz); 79 clazz = NULL; 80 } 81 }; 82 fillFromJobjectPlaybackParams83 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); 86 audioRate.mFallbackMode = 87 (AudioTimestretchFallbackMode)env->GetIntField(params, fields.audio_fallback_mode); 88 audioRate.mStretchMode = 89 (AudioTimestretchStretchMode)env->GetIntField(params, fields.audio_stretch_mode); 90 int set = env->GetIntField(params, fields.set); 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 asJobjectPlaybackParams98 jobject asJobject(JNIEnv *env, const fields_t& fields) { 99 jobject params = env->NewObject(fields.clazz, fields.constructID); 100 if (params == NULL) { 101 return NULL; 102 } 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); 107 env->SetIntField( 108 params, fields.set, 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 114 return params; 115 } 116 }; 117 118 } // namespace android 119 120 #endif // _ANDROID_MEDIA_PLAYBACK_PARAMS_H_ 121