/* * Copyright (C) 2014 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.media; import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.SystemApi; import android.os.Parcel; import android.os.Parcelable; import android.text.TextUtils; import android.util.Log; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.Collections; import java.util.HashSet; import java.util.Objects; import java.util.Set; /** * A class to encapsulate a collection of attributes describing information about an audio * stream. *
AudioAttributes
supersede the notion of stream types (see for instance
* {@link AudioManager#STREAM_MUSIC} or {@link AudioManager#STREAM_ALARM}) for defining the
* behavior of audio playback. Attributes allow an application to specify more information than is
* conveyed in a stream type by allowing the application to define:
*
AudioAttributes
* and it is recommended to build any instance with this information supplied, see
* {@link AudioAttributes.Builder} for exceptions. Here is an example where By default all types of information (usage, content type, flags) conveyed by an
* AudioAttributes
are used for example in one of the {@link AudioTrack}
* constructors (see {@link AudioTrack#AudioTrack(AudioAttributes, AudioFormat, int, int, int)}),
* to configure a {@link MediaPlayer}
* (see {@link MediaPlayer#setAudioAttributes(AudioAttributes)} or a
* {@link android.app.Notification} (see {@link android.app.Notification#audioAttributes}). An
* AudioAttributes
instance is built through its builder,
* {@link AudioAttributes.Builder}.
*/
public final class AudioAttributes implements Parcelable {
private final static String TAG = "AudioAttributes";
/**
* Content type value to use when the content type is unknown, or other than the ones defined.
*/
public final static int CONTENT_TYPE_UNKNOWN = 0;
/**
* Content type value to use when the content type is speech.
*/
public final static int CONTENT_TYPE_SPEECH = 1;
/**
* Content type value to use when the content type is music.
*/
public final static int CONTENT_TYPE_MUSIC = 2;
/**
* Content type value to use when the content type is a soundtrack, typically accompanying
* a movie or TV program.
*/
public final static int CONTENT_TYPE_MOVIE = 3;
/**
* Content type value to use when the content type is a sound used to accompany a user
* action, such as a beep or sound effect expressing a key click, or event, such as the
* type of a sound for a bonus being received in a game. These sounds are mostly synthesized
* or short Foley sounds.
*/
public final static int CONTENT_TYPE_SONIFICATION = 4;
/**
* Usage value to use when the usage is unknown.
*/
public final static int USAGE_UNKNOWN = 0;
/**
* Usage value to use when the usage is media, such as music, or movie
* soundtracks.
*/
public final static int USAGE_MEDIA = 1;
/**
* Usage value to use when the usage is voice communications, such as telephony
* or VoIP.
*/
public final static int USAGE_VOICE_COMMUNICATION = 2;
/**
* Usage value to use when the usage is in-call signalling, such as with
* a "busy" beep, or DTMF tones.
*/
public final static int USAGE_VOICE_COMMUNICATION_SIGNALLING = 3;
/**
* Usage value to use when the usage is an alarm (e.g. wake-up alarm).
*/
public final static int USAGE_ALARM = 4;
/**
* Usage value to use when the usage is notification. See other
* notification usages for more specialized uses.
*/
public final static int USAGE_NOTIFICATION = 5;
/**
* Usage value to use when the usage is telephony ringtone.
*/
public final static int USAGE_NOTIFICATION_RINGTONE = 6;
/**
* Usage value to use when the usage is a request to enter/end a
* communication, such as a VoIP communication or video-conference.
*/
public final static int USAGE_NOTIFICATION_COMMUNICATION_REQUEST = 7;
/**
* Usage value to use when the usage is notification for an "instant"
* communication such as a chat, or SMS.
*/
public final static int USAGE_NOTIFICATION_COMMUNICATION_INSTANT = 8;
/**
* Usage value to use when the usage is notification for a
* non-immediate type of communication such as e-mail.
*/
public final static int USAGE_NOTIFICATION_COMMUNICATION_DELAYED = 9;
/**
* Usage value to use when the usage is to attract the user's attention,
* such as a reminder or low battery warning.
*/
public final static int USAGE_NOTIFICATION_EVENT = 10;
/**
* Usage value to use when the usage is for accessibility, such as with
* a screen reader.
*/
public final static int USAGE_ASSISTANCE_ACCESSIBILITY = 11;
/**
* Usage value to use when the usage is driving or navigation directions.
*/
public final static int USAGE_ASSISTANCE_NAVIGATION_GUIDANCE = 12;
/**
* Usage value to use when the usage is sonification, such as with user
* interface sounds.
*/
public final static int USAGE_ASSISTANCE_SONIFICATION = 13;
/**
* Usage value to use when the usage is for game audio.
*/
public final static int USAGE_GAME = 14;
/**
* @hide
* Usage value to use when feeding audio to the platform and replacing "traditional" audio
* source, such as audio capture devices.
*/
public final static int USAGE_VIRTUAL_SOURCE = 15;
/**
* Flag defining a behavior where the audibility of the sound will be ensured by the system.
*/
public final static int FLAG_AUDIBILITY_ENFORCED = 0x1 << 0;
/**
* @hide
* Flag defining a behavior where the playback of the sound is ensured without
* degradation only when going to a secure sink.
*/
// FIXME not guaranteed yet
// TODO add in FLAG_ALL_PUBLIC when supported and in public API
public final static int FLAG_SECURE = 0x1 << 1;
/**
* @hide
* Flag to enable when the stream is associated with SCO usage.
* Internal use only for dealing with legacy STREAM_BLUETOOTH_SCO
*/
public final static int FLAG_SCO = 0x1 << 2;
/**
* @hide
* Flag defining a behavior where the system ensures that the playback of the sound will
* be compatible with its use as a broadcast for surrounding people and/or devices.
* Ensures audibility with no or minimal post-processing applied.
*/
@SystemApi
public final static int FLAG_BEACON = 0x1 << 3;
/**
* Flag requesting the use of an output stream supporting hardware A/V synchronization.
*/
public final static int FLAG_HW_AV_SYNC = 0x1 << 4;
/**
* @hide
* Flag requesting capture from the source used for hardware hotword detection.
* To be used with capture preset MediaRecorder.AudioSource.HOTWORD or
* MediaRecorder.AudioSource.VOICE_RECOGNITION.
*/
@SystemApi
public final static int FLAG_HW_HOTWORD = 0x1 << 5;
/**
* @hide
* Flag requesting audible playback even under limited interruptions.
*/
@SystemApi
public final static int FLAG_BYPASS_INTERRUPTION_POLICY = 0x1 << 6;
/**
* @hide
* Flag requesting audible playback even when the underlying stream is muted.
*/
@SystemApi
public final static int FLAG_BYPASS_MUTE = 0x1 << 7;
private final static int FLAG_ALL = FLAG_AUDIBILITY_ENFORCED | FLAG_SECURE | FLAG_SCO |
FLAG_BEACON | FLAG_HW_AV_SYNC | FLAG_HW_HOTWORD | FLAG_BYPASS_INTERRUPTION_POLICY |
FLAG_BYPASS_MUTE;
private final static int FLAG_ALL_PUBLIC = FLAG_AUDIBILITY_ENFORCED | FLAG_HW_AV_SYNC;
private int mUsage = USAGE_UNKNOWN;
private int mContentType = CONTENT_TYPE_UNKNOWN;
private int mSource = MediaRecorder.AudioSource.AUDIO_SOURCE_INVALID;
private int mFlags = 0x0;
private HashSetBuilder
is used to define the
* {@link AudioAttributes} to be used by a new AudioTrack
instance:
*
*
* AudioTrack myTrack = new AudioTrack(
* new AudioAttributes.Builder()
* .setUsage(AudioAttributes.USAGE_MEDIA)
* .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
* .build(),
* myFormat, myBuffSize, AudioTrack.MODE_STREAM, mySession);
*
*
* AudioAttributes
instance are set to "unknown". Unknown information will be
* interpreted as a default value that is dependent on the context of use, for instance a
* {@link MediaPlayer} will use a default usage of {@link AudioAttributes#USAGE_MEDIA}.
*/
public static class Builder {
private int mUsage = USAGE_UNKNOWN;
private int mContentType = CONTENT_TYPE_UNKNOWN;
private int mSource = MediaRecorder.AudioSource.AUDIO_SOURCE_INVALID;
private int mFlags = 0x0;
private HashSet
Do not use this method to set the stream type on an audio player object
* (e.g. {@link AudioTrack}, {@link MediaPlayer}), use AudioAttributes
instead.
* @param aa non-null AudioAttributes.
* @return a valid stream type for Activity
or stream volume control that matches
* the attributes, or {@link AudioManager#USE_DEFAULT_STREAM_TYPE} if there isn't a direct
* match. Note that USE_DEFAULT_STREAM_TYPE
is not a valid value
* for {@link AudioManager#setStreamVolume(int, int, int)}.
*/
public static int getVolumeControlStream(@NonNull AudioAttributes aa) {
if (aa == null) {
throw new IllegalArgumentException("Invalid null audio attributes");
}
return toVolumeStreamType(true /*fromGetVolumeControlStream*/, aa);
}
/**
* @hide
* Only use to get which stream type should be used for volume control, NOT for audio playback
* (all audio playback APIs are supposed to take AudioAttributes as input parameters)
* @param aa non-null AudioAttributes.
* @return a valid stream type for volume control that matches the attributes.
*/
public static int toLegacyStreamType(@NonNull AudioAttributes aa) {
return toVolumeStreamType(false /*fromGetVolumeControlStream*/, aa);
}
private static int toVolumeStreamType(boolean fromGetVolumeControlStream, AudioAttributes aa) {
// flags to stream type mapping
if ((aa.getFlags() & FLAG_AUDIBILITY_ENFORCED) == FLAG_AUDIBILITY_ENFORCED) {
return fromGetVolumeControlStream ?
AudioSystem.STREAM_SYSTEM : AudioSystem.STREAM_SYSTEM_ENFORCED;
}
if ((aa.getFlags() & FLAG_SCO) == FLAG_SCO) {
return fromGetVolumeControlStream ?
AudioSystem.STREAM_VOICE_CALL : AudioSystem.STREAM_BLUETOOTH_SCO;
}
// usage to stream type mapping
switch (aa.getUsage()) {
case USAGE_MEDIA:
case USAGE_GAME:
case USAGE_ASSISTANCE_ACCESSIBILITY:
case USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
return AudioSystem.STREAM_MUSIC;
case USAGE_ASSISTANCE_SONIFICATION:
return AudioSystem.STREAM_SYSTEM;
case USAGE_VOICE_COMMUNICATION:
return AudioSystem.STREAM_VOICE_CALL;
case USAGE_VOICE_COMMUNICATION_SIGNALLING:
return fromGetVolumeControlStream ?
AudioSystem.STREAM_VOICE_CALL : AudioSystem.STREAM_DTMF;
case USAGE_ALARM:
return AudioSystem.STREAM_ALARM;
case USAGE_NOTIFICATION_RINGTONE:
return AudioSystem.STREAM_RING;
case USAGE_NOTIFICATION:
case USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
case USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
case USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
case USAGE_NOTIFICATION_EVENT:
return AudioSystem.STREAM_NOTIFICATION;
case USAGE_UNKNOWN:
return fromGetVolumeControlStream ?
AudioManager.USE_DEFAULT_STREAM_TYPE : AudioSystem.STREAM_MUSIC;
default:
if (fromGetVolumeControlStream) {
throw new IllegalArgumentException("Unknown usage value " + aa.getUsage() +
" in audio attributes");
} else {
return AudioSystem.STREAM_MUSIC;
}
}
}
/** @hide */
@IntDef({
USAGE_UNKNOWN,
USAGE_MEDIA,
USAGE_VOICE_COMMUNICATION,
USAGE_VOICE_COMMUNICATION_SIGNALLING,
USAGE_ALARM,
USAGE_NOTIFICATION,
USAGE_NOTIFICATION_RINGTONE,
USAGE_NOTIFICATION_COMMUNICATION_REQUEST,
USAGE_NOTIFICATION_COMMUNICATION_INSTANT,
USAGE_NOTIFICATION_COMMUNICATION_DELAYED,
USAGE_NOTIFICATION_EVENT,
USAGE_ASSISTANCE_ACCESSIBILITY,
USAGE_ASSISTANCE_NAVIGATION_GUIDANCE,
USAGE_ASSISTANCE_SONIFICATION,
USAGE_GAME
})
@Retention(RetentionPolicy.SOURCE)
public @interface AttributeUsage {}
/** @hide */
@IntDef({
CONTENT_TYPE_UNKNOWN,
CONTENT_TYPE_SPEECH,
CONTENT_TYPE_MUSIC,
CONTENT_TYPE_MOVIE,
CONTENT_TYPE_SONIFICATION
})
@Retention(RetentionPolicy.SOURCE)
public @interface AttributeContentType {}
}