Skip to content

Commit 29a0909

Browse files
committed
Replace run-time stream types by compile-time
Use the AUDIO_STREAM_* constants defined in <system/audio.h> instead of dynamically looking up stream types from AudioManager. Replace a series of "if" statements by a switch. Change-Id: I6d015bc151c9ab97a02492e84c63298b1f6f31ac
1 parent 73d27c3 commit 29a0909

File tree

1 file changed

+25
-77
lines changed

1 file changed

+25
-77
lines changed

core/jni/android_media_AudioTrack.cpp

Lines changed: 25 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,6 @@ struct fields_t {
4949
jmethodID postNativeEventInJava; //... event post callback method
5050
int PCM16; //... format constants
5151
int PCM8; //... format constants
52-
int STREAM_VOICE_CALL; //... stream type constants
53-
int STREAM_SYSTEM; //... stream type constants
54-
int STREAM_RING; //... stream type constants
55-
int STREAM_MUSIC; //... stream type constants
56-
int STREAM_ALARM; //... stream type constants
57-
int STREAM_NOTIFICATION; //... stream type constants
58-
int STREAM_BLUETOOTH_SCO; //... stream type constants
59-
int STREAM_DTMF; //... stream type constants
6052
int MODE_STREAM; //... memory mode
6153
int MODE_STATIC; //... memory mode
6254
jfieldID nativeTrackInJavaObj; // stores in Java the native AudioTrack object
@@ -197,23 +189,18 @@ android_media_AudioTrack_native_setup(JNIEnv *env, jobject thiz, jobject weak_th
197189

198190
// check the stream type
199191
audio_stream_type_t atStreamType;
200-
if (streamType == javaAudioTrackFields.STREAM_VOICE_CALL) {
201-
atStreamType = AUDIO_STREAM_VOICE_CALL;
202-
} else if (streamType == javaAudioTrackFields.STREAM_SYSTEM) {
203-
atStreamType = AUDIO_STREAM_SYSTEM;
204-
} else if (streamType == javaAudioTrackFields.STREAM_RING) {
205-
atStreamType = AUDIO_STREAM_RING;
206-
} else if (streamType == javaAudioTrackFields.STREAM_MUSIC) {
207-
atStreamType = AUDIO_STREAM_MUSIC;
208-
} else if (streamType == javaAudioTrackFields.STREAM_ALARM) {
209-
atStreamType = AUDIO_STREAM_ALARM;
210-
} else if (streamType == javaAudioTrackFields.STREAM_NOTIFICATION) {
211-
atStreamType = AUDIO_STREAM_NOTIFICATION;
212-
} else if (streamType == javaAudioTrackFields.STREAM_BLUETOOTH_SCO) {
213-
atStreamType = AUDIO_STREAM_BLUETOOTH_SCO;
214-
} else if (streamType == javaAudioTrackFields.STREAM_DTMF) {
215-
atStreamType = AUDIO_STREAM_DTMF;
216-
} else {
192+
switch (streamType) {
193+
case AUDIO_STREAM_VOICE_CALL:
194+
case AUDIO_STREAM_SYSTEM:
195+
case AUDIO_STREAM_RING:
196+
case AUDIO_STREAM_MUSIC:
197+
case AUDIO_STREAM_ALARM:
198+
case AUDIO_STREAM_NOTIFICATION:
199+
case AUDIO_STREAM_BLUETOOTH_SCO:
200+
case AUDIO_STREAM_DTMF:
201+
atStreamType = (audio_stream_type_t) streamType;
202+
break;
203+
default:
217204
ALOGE("Error creating AudioTrack: unknown stream type.");
218205
return AUDIOTRACK_ERROR_SETUP_INVALIDSTREAMTYPE;
219206
}
@@ -764,24 +751,20 @@ static jint android_media_AudioTrack_get_output_sample_rate(JNIEnv *env, jobjec
764751
// convert the stream type from Java to native value
765752
// FIXME: code duplication with android_media_AudioTrack_native_setup()
766753
audio_stream_type_t nativeStreamType;
767-
if (javaStreamType == javaAudioTrackFields.STREAM_VOICE_CALL) {
768-
nativeStreamType = AUDIO_STREAM_VOICE_CALL;
769-
} else if (javaStreamType == javaAudioTrackFields.STREAM_SYSTEM) {
770-
nativeStreamType = AUDIO_STREAM_SYSTEM;
771-
} else if (javaStreamType == javaAudioTrackFields.STREAM_RING) {
772-
nativeStreamType = AUDIO_STREAM_RING;
773-
} else if (javaStreamType == javaAudioTrackFields.STREAM_MUSIC) {
774-
nativeStreamType = AUDIO_STREAM_MUSIC;
775-
} else if (javaStreamType == javaAudioTrackFields.STREAM_ALARM) {
776-
nativeStreamType = AUDIO_STREAM_ALARM;
777-
} else if (javaStreamType == javaAudioTrackFields.STREAM_NOTIFICATION) {
778-
nativeStreamType = AUDIO_STREAM_NOTIFICATION;
779-
} else if (javaStreamType == javaAudioTrackFields.STREAM_BLUETOOTH_SCO) {
780-
nativeStreamType = AUDIO_STREAM_BLUETOOTH_SCO;
781-
} else if (javaStreamType == javaAudioTrackFields.STREAM_DTMF) {
782-
nativeStreamType = AUDIO_STREAM_DTMF;
783-
} else {
754+
switch (javaStreamType) {
755+
case AUDIO_STREAM_VOICE_CALL:
756+
case AUDIO_STREAM_SYSTEM:
757+
case AUDIO_STREAM_RING:
758+
case AUDIO_STREAM_MUSIC:
759+
case AUDIO_STREAM_ALARM:
760+
case AUDIO_STREAM_NOTIFICATION:
761+
case AUDIO_STREAM_BLUETOOTH_SCO:
762+
case AUDIO_STREAM_DTMF:
763+
nativeStreamType = (audio_stream_type_t) javaStreamType;
764+
break;
765+
default:
784766
nativeStreamType = AUDIO_STREAM_DEFAULT;
767+
break;
785768
}
786769

787770
if (AudioSystem::getOutputSamplingRate(&afSamplingRate, nativeStreamType) != NO_ERROR) {
@@ -987,41 +970,6 @@ int register_android_media_AudioTrack(JNIEnv *env)
987970
return -1;
988971
}
989972

990-
// Get the stream types from the AudioManager class
991-
jclass audioManagerClass = NULL;
992-
audioManagerClass = env->FindClass(JAVA_AUDIOMANAGER_CLASS_NAME);
993-
if (audioManagerClass == NULL) {
994-
ALOGE("Can't find %s", JAVA_AUDIOMANAGER_CLASS_NAME);
995-
return -1;
996-
}
997-
if ( !android_media_getIntConstantFromClass(env, audioManagerClass,
998-
JAVA_AUDIOMANAGER_CLASS_NAME,
999-
JAVA_CONST_STREAM_VOICE_CALL_NAME, &(javaAudioTrackFields.STREAM_VOICE_CALL))
1000-
|| !android_media_getIntConstantFromClass(env, audioManagerClass,
1001-
JAVA_AUDIOMANAGER_CLASS_NAME,
1002-
JAVA_CONST_STREAM_MUSIC_NAME, &(javaAudioTrackFields.STREAM_MUSIC))
1003-
|| !android_media_getIntConstantFromClass(env, audioManagerClass,
1004-
JAVA_AUDIOMANAGER_CLASS_NAME,
1005-
JAVA_CONST_STREAM_SYSTEM_NAME, &(javaAudioTrackFields.STREAM_SYSTEM))
1006-
|| !android_media_getIntConstantFromClass(env, audioManagerClass,
1007-
JAVA_AUDIOMANAGER_CLASS_NAME,
1008-
JAVA_CONST_STREAM_RING_NAME, &(javaAudioTrackFields.STREAM_RING))
1009-
|| !android_media_getIntConstantFromClass(env, audioManagerClass,
1010-
JAVA_AUDIOMANAGER_CLASS_NAME,
1011-
JAVA_CONST_STREAM_ALARM_NAME, &(javaAudioTrackFields.STREAM_ALARM))
1012-
|| !android_media_getIntConstantFromClass(env, audioManagerClass,
1013-
JAVA_AUDIOMANAGER_CLASS_NAME,
1014-
JAVA_CONST_STREAM_NOTIFICATION_NAME, &(javaAudioTrackFields.STREAM_NOTIFICATION))
1015-
|| !android_media_getIntConstantFromClass(env, audioManagerClass,
1016-
JAVA_AUDIOMANAGER_CLASS_NAME,
1017-
JAVA_CONST_STREAM_BLUETOOTH_SCO_NAME, &(javaAudioTrackFields.STREAM_BLUETOOTH_SCO))
1018-
|| !android_media_getIntConstantFromClass(env, audioManagerClass,
1019-
JAVA_AUDIOMANAGER_CLASS_NAME,
1020-
JAVA_CONST_STREAM_DTMF_NAME, &(javaAudioTrackFields.STREAM_DTMF))) {
1021-
// error log performed in android_media_getIntConstantFromClass()
1022-
return -1;
1023-
}
1024-
1025973
return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
1026974
}
1027975

0 commit comments

Comments
 (0)