From a192cc2a132cb0ee8588e2df755563ec7008c179 Mon Sep 17 00:00:00 2001 From: Jeff Davidson Date: Thu, 8 Feb 2018 15:30:06 -0800 Subject: Update fullsdk to 4575844 /google/data/ro/projects/android/fetch_artifact \ --bid 4575844 \ --target sdk_phone_x86_64-sdk \ sdk-repo-linux-sources-4575844.zip Test: TreeHugger Change-Id: I81e0eb157b4ac3b38408d0ef86f9d6286471f87a --- android/media/AudioAttributes.java | 7 +- android/media/AudioFocusInfo.java | 2 - android/media/AudioFocusRequest.java | 31 + android/media/AudioFormat.java | 73 +- android/media/AudioManager.java | 40 + android/media/AudioPort.java | 3 +- android/media/AudioSystem.java | 14 +- android/media/AudioTrack.java | 156 +- android/media/DataSourceDesc.java | 465 ++ android/media/Media2DataSource.java | 62 + android/media/Media2HTTPConnection.java | 385 ++ android/media/Media2HTTPService.java | 98 + android/media/MediaBrowser2.java | 176 + android/media/MediaBrowser2Test.java | 141 + android/media/MediaCodecInfo.java | 5 +- android/media/MediaController2.java | 616 +++ android/media/MediaController2Test.java | 487 ++ android/media/MediaDrm.java | 301 +- android/media/MediaFormat.java | 17 +- android/media/MediaItem2.java | 146 + android/media/MediaLibraryService2.java | 350 ++ android/media/MediaMetadata2.java | 815 ++++ android/media/MediaPlayer2.java | 2476 ++++++++++ android/media/MediaPlayer2Impl.java | 4899 ++++++++++++++++++++ android/media/MediaPlayerBase.java | 72 + android/media/MediaRecorder.java | 33 +- android/media/MediaSession2.java | 1223 +++++ android/media/MediaSession2Test.java | 273 ++ android/media/MediaSession2TestBase.java | 210 + .../media/MediaSessionManager_MediaSession2.java | 223 + android/media/MediaSessionService2.java | 247 + android/media/MockMediaLibraryService2.java | 98 + android/media/MockMediaSessionService2.java | 102 + android/media/MockPlayer.java | 146 + android/media/PlaybackListenerHolder.java | 73 + android/media/PlaybackState2.java | 216 + android/media/Rating2.java | 304 ++ android/media/SessionToken2.java | 225 + android/media/TestServiceRegistry.java | 135 + android/media/TestUtils.java | 124 + android/media/session/MediaSessionManager.java | 102 +- android/media/update/ApiLoader.java | 60 + android/media/update/MediaBrowser2Provider.java | 33 + .../media/update/MediaControlView2Provider.java | 47 + android/media/update/MediaController2Provider.java | 64 + .../media/update/MediaLibraryService2Provider.java | 30 + android/media/update/MediaSession2Provider.java | 65 + .../media/update/MediaSessionService2Provider.java | 35 + android/media/update/StaticProvider.java | 81 + android/media/update/TransportControlProvider.java | 39 + android/media/update/VideoView2Provider.java | 73 + android/media/update/ViewProvider.java | 49 + 52 files changed, 16060 insertions(+), 87 deletions(-) create mode 100644 android/media/DataSourceDesc.java create mode 100644 android/media/Media2DataSource.java create mode 100644 android/media/Media2HTTPConnection.java create mode 100644 android/media/Media2HTTPService.java create mode 100644 android/media/MediaBrowser2.java create mode 100644 android/media/MediaBrowser2Test.java create mode 100644 android/media/MediaController2.java create mode 100644 android/media/MediaController2Test.java create mode 100644 android/media/MediaItem2.java create mode 100644 android/media/MediaLibraryService2.java create mode 100644 android/media/MediaMetadata2.java create mode 100644 android/media/MediaPlayer2.java create mode 100644 android/media/MediaPlayer2Impl.java create mode 100644 android/media/MediaPlayerBase.java create mode 100644 android/media/MediaSession2.java create mode 100644 android/media/MediaSession2Test.java create mode 100644 android/media/MediaSession2TestBase.java create mode 100644 android/media/MediaSessionManager_MediaSession2.java create mode 100644 android/media/MediaSessionService2.java create mode 100644 android/media/MockMediaLibraryService2.java create mode 100644 android/media/MockMediaSessionService2.java create mode 100644 android/media/MockPlayer.java create mode 100644 android/media/PlaybackListenerHolder.java create mode 100644 android/media/PlaybackState2.java create mode 100644 android/media/Rating2.java create mode 100644 android/media/SessionToken2.java create mode 100644 android/media/TestServiceRegistry.java create mode 100644 android/media/TestUtils.java create mode 100644 android/media/update/ApiLoader.java create mode 100644 android/media/update/MediaBrowser2Provider.java create mode 100644 android/media/update/MediaControlView2Provider.java create mode 100644 android/media/update/MediaController2Provider.java create mode 100644 android/media/update/MediaLibraryService2Provider.java create mode 100644 android/media/update/MediaSession2Provider.java create mode 100644 android/media/update/MediaSessionService2Provider.java create mode 100644 android/media/update/StaticProvider.java create mode 100644 android/media/update/TransportControlProvider.java create mode 100644 android/media/update/VideoView2Provider.java create mode 100644 android/media/update/ViewProvider.java (limited to 'android/media') diff --git a/android/media/AudioAttributes.java b/android/media/AudioAttributes.java index e0289f0b..44a2ff9e 100644 --- a/android/media/AudioAttributes.java +++ b/android/media/AudioAttributes.java @@ -180,6 +180,7 @@ public final class AudioAttributes implements Parcelable { /** * IMPORTANT: when adding new usage types, add them to SDK_USAGES and update SUPPRESSIBLE_USAGES * if applicable, as well as audioattributes.proto. + * Also consider adding them to for the NDK. */ /** @@ -879,7 +880,9 @@ public final class AudioAttributes implements Parcelable { } /** @hide */ - public void toProto(ProtoOutputStream proto) { + public void writeToProto(ProtoOutputStream proto, long fieldId) { + final long token = proto.start(fieldId); + proto.write(AudioAttributesProto.USAGE, mUsage); proto.write(AudioAttributesProto.CONTENT_TYPE, mContentType); proto.write(AudioAttributesProto.FLAGS, mFlags); @@ -891,6 +894,8 @@ public final class AudioAttributes implements Parcelable { } } // TODO: is the data in mBundle useful for debugging? + + proto.end(token); } /** @hide */ diff --git a/android/media/AudioFocusInfo.java b/android/media/AudioFocusInfo.java index 6d9c5e2a..5d0c8e23 100644 --- a/android/media/AudioFocusInfo.java +++ b/android/media/AudioFocusInfo.java @@ -130,13 +130,11 @@ public final class AudioFocusInfo implements Parcelable { dest.writeInt(mSdkTarget); } - @SystemApi @Override public int hashCode() { return Objects.hash(mAttributes, mClientUid, mClientId, mPackageName, mGainRequest, mFlags); } - @SystemApi @Override public boolean equals(Object obj) { if (this == obj) diff --git a/android/media/AudioFocusRequest.java b/android/media/AudioFocusRequest.java index de59ac39..7104dad4 100644 --- a/android/media/AudioFocusRequest.java +++ b/android/media/AudioFocusRequest.java @@ -20,6 +20,7 @@ import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.SystemApi; import android.media.AudioManager.OnAudioFocusChangeListener; +import android.os.Bundle; import android.os.Handler; import android.os.Looper; @@ -220,6 +221,9 @@ public final class AudioFocusRequest { private final static AudioAttributes FOCUS_DEFAULT_ATTR = new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_MEDIA).build(); + /** @hide */ + public static final String KEY_ACCESSIBILITY_FORCE_FOCUS_DUCKING = "a11y_force_ducking"; + private final OnAudioFocusChangeListener mFocusListener; // may be null private final Handler mListenerHandler; // may be null private final AudioAttributes mAttr; // never null @@ -349,6 +353,7 @@ public final class AudioFocusRequest { private boolean mPausesOnDuck = false; private boolean mDelayedFocus = false; private boolean mFocusLocked = false; + private boolean mA11yForceDucking = false; /** * Constructs a new {@code Builder}, and specifies how audio focus @@ -525,6 +530,21 @@ public final class AudioFocusRequest { return this; } + /** + * Marks this focus request as forcing ducking, regardless of the conditions in which + * the system would or would not enforce ducking. + * Forcing ducking will only be honored when requesting AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK + * with an {@link AudioAttributes} usage of + * {@link AudioAttributes#USAGE_ASSISTANCE_ACCESSIBILITY}, coming from an accessibility + * service, and will be ignored otherwise. + * @param forceDucking {@code true} to force ducking + * @return this {@code Builder} instance + */ + public @NonNull Builder setForceDucking(boolean forceDucking) { + mA11yForceDucking = forceDucking; + return this; + } + /** * Builds a new {@code AudioFocusRequest} instance combining all the information gathered * by this {@code Builder}'s configuration methods. @@ -538,6 +558,17 @@ public final class AudioFocusRequest { throw new IllegalStateException( "Can't use delayed focus or pause on duck without a listener"); } + if (mA11yForceDucking) { + final Bundle extraInfo; + if (mAttr.getBundle() == null) { + extraInfo = new Bundle(); + } else { + extraInfo = mAttr.getBundle(); + } + // checking of usage and focus request is done server side + extraInfo.putBoolean(KEY_ACCESSIBILITY_FORCE_FOCUS_DUCKING, true); + mAttr = new AudioAttributes.Builder(mAttr).addBundle(extraInfo).build(); + } final int flags = 0 | (mDelayedFocus ? AudioManager.AUDIOFOCUS_FLAG_DELAY_OK : 0) | (mPausesOnDuck ? AudioManager.AUDIOFOCUS_FLAG_PAUSES_ON_DUCKABLE_LOSS : 0) diff --git a/android/media/AudioFormat.java b/android/media/AudioFormat.java index 93fc3da5..b07d0422 100644 --- a/android/media/AudioFormat.java +++ b/android/media/AudioFormat.java @@ -238,22 +238,15 @@ public final class AudioFormat implements Parcelable { public static final int ENCODING_DTS = 7; /** Audio data format: DTS HD compressed */ public static final int ENCODING_DTS_HD = 8; - /** Audio data format: MP3 compressed - * @hide - * */ + /** Audio data format: MP3 compressed */ public static final int ENCODING_MP3 = 9; - /** Audio data format: AAC LC compressed - * @hide - * */ + /** Audio data format: AAC LC compressed */ public static final int ENCODING_AAC_LC = 10; - /** Audio data format: AAC HE V1 compressed - * @hide - * */ + /** Audio data format: AAC HE V1 compressed */ public static final int ENCODING_AAC_HE_V1 = 11; - /** Audio data format: AAC HE V2 compressed - * @hide - * */ + /** Audio data format: AAC HE V2 compressed */ public static final int ENCODING_AAC_HE_V2 = 12; + /** Audio data format: compressed audio wrapped in PCM for HDMI * or S/PDIF passthrough. * IEC61937 uses a stereo stream of 16-bit samples as the wrapper. @@ -266,6 +259,12 @@ public final class AudioFormat implements Parcelable { /** Audio data format: DOLBY TRUEHD compressed **/ public static final int ENCODING_DOLBY_TRUEHD = 14; + /** Audio data format: AAC ELD compressed */ + public static final int ENCODING_AAC_ELD = 15; + /** Audio data format: AAC xHE compressed */ + public static final int ENCODING_AAC_XHE = 16; + /** Audio data format: AC-4 sync frame transport format */ + public static final int ENCODING_AC4 = 17; /** @hide */ public static String toLogFriendlyEncoding(int enc) { @@ -298,6 +297,12 @@ public final class AudioFormat implements Parcelable { return "ENCODING_IEC61937"; case ENCODING_DOLBY_TRUEHD: return "ENCODING_DOLBY_TRUEHD"; + case ENCODING_AAC_ELD: + return "ENCODING_AAC_ELD"; + case ENCODING_AAC_XHE: + return "ENCODING_AAC_XHE"; + case ENCODING_AC4: + return "ENCODING_AC4"; default : return "invalid encoding " + enc; } @@ -514,6 +519,9 @@ public final class AudioFormat implements Parcelable { case ENCODING_AAC_HE_V1: case ENCODING_AAC_HE_V2: case ENCODING_IEC61937: + case ENCODING_AAC_ELD: + case ENCODING_AAC_XHE: + case ENCODING_AC4: return true; default: return false; @@ -532,6 +540,13 @@ public final class AudioFormat implements Parcelable { case ENCODING_DTS: case ENCODING_DTS_HD: case ENCODING_IEC61937: + case ENCODING_MP3: + case ENCODING_AAC_LC: + case ENCODING_AAC_HE_V1: + case ENCODING_AAC_HE_V2: + case ENCODING_AAC_ELD: + case ENCODING_AAC_XHE: + case ENCODING_AC4: return true; default: return false; @@ -556,6 +571,9 @@ public final class AudioFormat implements Parcelable { case ENCODING_AAC_HE_V1: case ENCODING_AAC_HE_V2: case ENCODING_IEC61937: // wrapped in PCM but compressed + case ENCODING_AAC_ELD: + case ENCODING_AAC_XHE: + case ENCODING_AC4: return false; case ENCODING_INVALID: default: @@ -581,6 +599,9 @@ public final class AudioFormat implements Parcelable { case ENCODING_AAC_LC: case ENCODING_AAC_HE_V1: case ENCODING_AAC_HE_V2: + case ENCODING_AAC_ELD: + case ENCODING_AAC_XHE: + case ENCODING_AC4: return false; case ENCODING_INVALID: default: @@ -794,14 +815,7 @@ public final class AudioFormat implements Parcelable { /** * Sets the data encoding format. - * @param encoding one of {@link AudioFormat#ENCODING_DEFAULT}, - * {@link AudioFormat#ENCODING_PCM_8BIT}, - * {@link AudioFormat#ENCODING_PCM_16BIT}, - * {@link AudioFormat#ENCODING_PCM_FLOAT}, - * {@link AudioFormat#ENCODING_AC3}, - * {@link AudioFormat#ENCODING_E_AC3}. - * {@link AudioFormat#ENCODING_DTS}, - * {@link AudioFormat#ENCODING_DTS_HD}. + * @param encoding the specified encoding or default. * @return the same Builder instance. * @throws java.lang.IllegalArgumentException */ @@ -818,6 +832,13 @@ public final class AudioFormat implements Parcelable { case ENCODING_DTS: case ENCODING_DTS_HD: case ENCODING_IEC61937: + case ENCODING_MP3: + case ENCODING_AAC_LC: + case ENCODING_AAC_HE_V1: + case ENCODING_AAC_HE_V2: + case ENCODING_AAC_ELD: + case ENCODING_AAC_XHE: + case ENCODING_AC4: mEncoding = encoding; break; case ENCODING_INVALID: @@ -1016,7 +1037,7 @@ public final class AudioFormat implements Parcelable { } /** @hide */ - @IntDef({ + @IntDef(flag = false, prefix = "ENCODING", value = { ENCODING_DEFAULT, ENCODING_PCM_8BIT, ENCODING_PCM_16BIT, @@ -1025,8 +1046,14 @@ public final class AudioFormat implements Parcelable { ENCODING_E_AC3, ENCODING_DTS, ENCODING_DTS_HD, - ENCODING_IEC61937 - }) + ENCODING_IEC61937, + ENCODING_AAC_HE_V1, + ENCODING_AAC_HE_V2, + ENCODING_AAC_LC, + ENCODING_AAC_ELD, + ENCODING_AAC_XHE, + ENCODING_AC4 } + ) @Retention(RetentionPolicy.SOURCE) public @interface Encoding {} diff --git a/android/media/AudioManager.java b/android/media/AudioManager.java index 913b5e84..2ac4063d 100644 --- a/android/media/AudioManager.java +++ b/android/media/AudioManager.java @@ -1328,6 +1328,19 @@ public class AudioManager { } } + //==================================================================== + // Offload query + /** + * Returns whether offloaded playback of an audio format is supported on the device. + * Offloaded playback is where the decoding of an audio stream is not competing with other + * software resources. In general, it is supported by dedicated hardware, such as audio DSPs. + * @param format the audio format (codec, sample rate, channels) being checked. + * @return true if the given audio format can be offloaded. + */ + public boolean isOffloadedPlaybackSupported(@NonNull AudioFormat format) { + return AudioSystem.isOffloadSupported(format); + } + //==================================================================== // Bluetooth SCO control /** @@ -3745,6 +3758,33 @@ public class AudioManager { return delay; } + /** + * Indicate A2DP source or sink connection state change and eventually suppress + * the {@link AudioManager.ACTION_AUDIO_BECOMING_NOISY} intent. + * @param device Bluetooth device connected/disconnected + * @param state new connection state (BluetoothProfile.STATE_xxx) + * @param profile profile for the A2DP device + * (either {@link android.bluetooth.BluetoothProfile.A2DP} or + * {@link android.bluetooth.BluetoothProfile.A2DP_SINK}) + * @param suppressNoisyIntent if true the + * {@link AudioManager.ACTION_AUDIO_BECOMING_NOISY} intent will not be sent. + * @return a delay in ms that the caller should wait before broadcasting + * BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED intent. + * {@hide} + */ + public int setBluetoothA2dpDeviceConnectionStateSuppressNoisyIntent( + BluetoothDevice device, int state, int profile, boolean suppressNoisyIntent) { + final IAudioService service = getService(); + int delay = 0; + try { + delay = service.setBluetoothA2dpDeviceConnectionStateSuppressNoisyIntent(device, + state, profile, suppressNoisyIntent); + } catch (RemoteException e) { + throw e.rethrowFromSystemServer(); + } + return delay; + } + /** * Indicate A2DP device configuration has changed. * @param device Bluetooth device whose configuration has changed. diff --git a/android/media/AudioPort.java b/android/media/AudioPort.java index 19bf51d9..047db194 100644 --- a/android/media/AudioPort.java +++ b/android/media/AudioPort.java @@ -20,7 +20,7 @@ package android.media; * An audio port is a node of the audio framework or hardware that can be connected to or * disconnect from another audio node to create a specific audio routing configuration. * Examples of audio ports are an output device (speaker) or an output mix (see AudioMixPort). - * All attributes that are relevant for applications to make routing selection are decribed + * All attributes that are relevant for applications to make routing selection are described * in an AudioPort, in particular: * - possible channel mask configurations. * - audio format (PCM 16bit, PCM 24bit...) @@ -173,6 +173,7 @@ public class AudioPort { /** * Build a specific configuration of this audio port for use by methods * like AudioManager.connectAudioPatch(). + * @param samplingRate * @param channelMask The desired channel mask. AudioFormat.CHANNEL_OUT_DEFAULT if no change * from active configuration requested. * @param format The desired audio format. AudioFormat.ENCODING_DEFAULT if no change diff --git a/android/media/AudioSystem.java b/android/media/AudioSystem.java index e56944df..dcd37daf 100644 --- a/android/media/AudioSystem.java +++ b/android/media/AudioSystem.java @@ -16,6 +16,7 @@ package android.media; +import android.annotation.NonNull; import android.content.Context; import android.content.pm.PackageManager; import android.media.audiopolicy.AudioMix; @@ -792,7 +793,7 @@ public class AudioSystem public static native int getPrimaryOutputFrameCount(); public static native int getOutputLatency(int stream); - public static native int setLowRamDevice(boolean isLowRamDevice); + public static native int setLowRamDevice(boolean isLowRamDevice, long totalMemory); public static native int checkAudioFlinger(); public static native int listAudioPorts(ArrayList ports, int[] generation); @@ -818,6 +819,14 @@ public class AudioSystem public static native float getStreamVolumeDB(int stream, int index, int device); + static boolean isOffloadSupported(@NonNull AudioFormat format) { + return native_is_offload_supported(format.getEncoding(), format.getSampleRate(), + format.getChannelMask(), format.getChannelIndexMask()); + } + + private static native boolean native_is_offload_supported(int encoding, int sampleRate, + int channelMask, int channelIndexMask); + // Items shared with audio service /** @@ -914,7 +923,8 @@ public class AudioSystem (1 << STREAM_MUSIC) | (1 << STREAM_RING) | (1 << STREAM_NOTIFICATION) | - (1 << STREAM_SYSTEM); + (1 << STREAM_SYSTEM) | + (1 << STREAM_VOICE_CALL); /** * Event posted by AudioTrack and AudioRecord JNI (JNIDeviceCallback) when routing changes. diff --git a/android/media/AudioTrack.java b/android/media/AudioTrack.java index e535fdf5..5928d03d 100644 --- a/android/media/AudioTrack.java +++ b/android/media/AudioTrack.java @@ -24,7 +24,9 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.NioUtils; import java.util.Collection; +import java.util.concurrent.Executor; +import android.annotation.CallbackExecutor; import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.Nullable; @@ -185,6 +187,22 @@ public class AudioTrack extends PlayerBase * Event id denotes when previously set update period has elapsed during playback. */ private static final int NATIVE_EVENT_NEW_POS = 4; + /** + * Callback for more data + * TODO only for offload + */ + private static final int NATIVE_EVENT_MORE_DATA = 0; + /** + * IAudioTrack tear down for offloaded tracks + * TODO: when received, java AudioTrack must be released + */ + private static final int NATIVE_EVENT_NEW_IAUDIOTRACK = 6; + /** + * Event id denotes when all the buffers queued in AF and HW are played + * back (after stop is called) for an offloaded track. + * TODO: not just for offload + */ + private static final int NATIVE_EVENT_STREAM_END = 7; private final static String TAG = "android.media.AudioTrack"; @@ -540,6 +558,12 @@ public class AudioTrack extends PlayerBase public AudioTrack(AudioAttributes attributes, AudioFormat format, int bufferSizeInBytes, int mode, int sessionId) throws IllegalArgumentException { + this(attributes, format, bufferSizeInBytes, mode, sessionId, false /*offload*/); + } + + private AudioTrack(AudioAttributes attributes, AudioFormat format, int bufferSizeInBytes, + int mode, int sessionId, boolean offload) + throws IllegalArgumentException { super(attributes, AudioPlaybackConfiguration.PLAYER_TYPE_JAM_AUDIOTRACK); // mState already == STATE_UNINITIALIZED @@ -601,7 +625,8 @@ public class AudioTrack extends PlayerBase // native initialization int initResult = native_setup(new WeakReference(this), mAttributes, sampleRate, mChannelMask, mChannelIndexMask, mAudioFormat, - mNativeBufferSizeInBytes, mDataLoadMode, session, 0 /*nativeTrackInJavaObj*/); + mNativeBufferSizeInBytes, mDataLoadMode, session, 0 /*nativeTrackInJavaObj*/, + offload); if (initResult != SUCCESS) { loge("Error code "+initResult+" when initializing AudioTrack."); return; // with mState == STATE_UNINITIALIZED @@ -681,7 +706,8 @@ public class AudioTrack extends PlayerBase 0 /*mNativeBufferSizeInBytes - NA*/, 0 /*mDataLoadMode - NA*/, session, - nativeTrackInJavaObj); + nativeTrackInJavaObj, + false /*offload*/); if (initResult != SUCCESS) { loge("Error code "+initResult+" when initializing AudioTrack."); return; // with mState == STATE_UNINITIALIZED @@ -729,6 +755,7 @@ public class AudioTrack extends PlayerBase * MODE_STREAM will be used. *
If the session ID is not specified with {@link #setSessionId(int)}, a new one will * be generated. + *
Offload is false by default. */ public static class Builder { private AudioAttributes mAttributes; @@ -737,6 +764,7 @@ public class AudioTrack extends PlayerBase private int mSessionId = AudioManager.AUDIO_SESSION_ID_GENERATE; private int mMode = MODE_STREAM; private int mPerformanceMode = PERFORMANCE_MODE_NONE; + private boolean mOffload = false; /** * Constructs a new Builder with the default values as described above. @@ -866,6 +894,21 @@ public class AudioTrack extends PlayerBase return this; } + /** + * Sets whether this track will play through the offloaded audio path. + * When set to true, at build time, the audio format will be checked against + * {@link AudioManager#isOffloadedPlaybackSupported(AudioFormat)} to verify the audio format + * used by this track is supported on the device's offload path (if any). + *
Offload is only supported for media audio streams, and therefore requires that + * the usage be {@link AudioAttributes#USAGE_MEDIA}. + * @param offload true to require the offload path for playback. + * @return the same Builder instance. + */ + public @NonNull Builder setOffloadedPlayback(boolean offload) { + mOffload = offload; + return this; + } + /** * Builds an {@link AudioTrack} instance initialized with all the parameters set * on this Builder. @@ -909,6 +952,19 @@ public class AudioTrack extends PlayerBase .setEncoding(AudioFormat.ENCODING_DEFAULT) .build(); } + + //TODO tie offload to PERFORMANCE_MODE_POWER_SAVING? + if (mOffload) { + if (mAttributes.getUsage() != AudioAttributes.USAGE_MEDIA) { + throw new UnsupportedOperationException( + "Cannot create AudioTrack, offload requires USAGE_MEDIA"); + } + if (!AudioSystem.isOffloadSupported(mFormat)) { + throw new UnsupportedOperationException( + "Cannot create AudioTrack, offload format not supported"); + } + } + try { // If the buffer size is not specified in streaming mode, // use a single frame for the buffer size and let the @@ -918,7 +974,7 @@ public class AudioTrack extends PlayerBase * mFormat.getBytesPerSample(mFormat.getEncoding()); } final AudioTrack track = new AudioTrack( - mAttributes, mFormat, mBufferSizeInBytes, mMode, mSessionId); + mAttributes, mFormat, mBufferSizeInBytes, mMode, mSessionId, mOffload); if (track.getState() == STATE_UNINITIALIZED) { // release is not necessary throw new UnsupportedOperationException("Cannot create AudioTrack"); @@ -2882,6 +2938,69 @@ public class AudioTrack extends PlayerBase void onPeriodicNotification(AudioTrack track); } + /** + * Abstract class to receive event notification about the stream playback. + * See {@link AudioTrack#setStreamEventCallback(Executor, StreamEventCallback)} to register + * the callback on the given {@link AudioTrack} instance. + */ + public abstract static class StreamEventCallback { + /** @hide */ // add hidden empty constructor so it doesn't show in SDK + public StreamEventCallback() { } + /** + * Called when an offloaded track is no longer valid and has been discarded by the system. + * An example of this happening is when an offloaded track has been paused too long, and + * gets invalidated by the system to prevent any other offload. + * @param track the {@link AudioTrack} on which the event happened + */ + public void onTearDown(AudioTrack track) { } + /** + * Called when all the buffers of an offloaded track that were queued in the audio system + * (e.g. the combination of the Android audio framework and the device's audio hardware) + * have been played after {@link AudioTrack#stop()} has been called. + * @param track the {@link AudioTrack} on which the event happened + */ + public void onStreamPresentationEnd(AudioTrack track) { } + /** + * Called when more audio data can be written without blocking on an offloaded track. + * @param track the {@link AudioTrack} on which the event happened + */ + public void onStreamDataRequest(AudioTrack track) { } + } + + private Executor mStreamEventExec; + private StreamEventCallback mStreamEventCb; + private final Object mStreamEventCbLock = new Object(); + + /** + * Sets the callback for the notification of stream events. + * @param executor {@link Executor} to handle the callbacks + * @param eventCallback the callback to receive the stream event notifications + */ + public void setStreamEventCallback(@NonNull @CallbackExecutor Executor executor, + @NonNull StreamEventCallback eventCallback) { + if (eventCallback == null) { + throw new IllegalArgumentException("Illegal null StreamEventCallback"); + } + if (executor == null) { + throw new IllegalArgumentException("Illegal null Executor for the StreamEventCallback"); + } + synchronized (mStreamEventCbLock) { + mStreamEventExec = executor; + mStreamEventCb = eventCallback; + } + } + + /** + * Unregisters the callback for notification of stream events, previously set + * by {@link #setStreamEventCallback(Executor, StreamEventCallback)}. + */ + public void removeStreamEventCallback() { + synchronized (mStreamEventCbLock) { + mStreamEventExec = null; + mStreamEventCb = null; + } + } + //--------------------------------------------------------- // Inner classes //-------------------- @@ -2965,7 +3084,7 @@ public class AudioTrack extends PlayerBase private static void postEventFromNative(Object audiotrack_ref, int what, int arg1, int arg2, Object obj) { //logd("Event posted from the native side: event="+ what + " args="+ arg1+" "+arg2); - AudioTrack track = (AudioTrack)((WeakReference)audiotrack_ref).get(); + final AudioTrack track = (AudioTrack)((WeakReference)audiotrack_ref).get(); if (track == null) { return; } @@ -2974,6 +3093,32 @@ public class AudioTrack extends PlayerBase track.broadcastRoutingChange(); return; } + + if (what == NATIVE_EVENT_MORE_DATA || what == NATIVE_EVENT_NEW_IAUDIOTRACK + || what == NATIVE_EVENT_STREAM_END) { + final Executor exec; + final StreamEventCallback cb; + synchronized (track.mStreamEventCbLock) { + exec = track.mStreamEventExec; + cb = track.mStreamEventCb; + } + if ((exec == null) || (cb == null)) { + return; + } + switch (what) { + case NATIVE_EVENT_MORE_DATA: + exec.execute(() -> cb.onStreamDataRequest(track)); + return; + case NATIVE_EVENT_NEW_IAUDIOTRACK: + // TODO also release track as it's not longer usable + exec.execute(() -> cb.onTearDown(track)); + return; + case NATIVE_EVENT_STREAM_END: + exec.execute(() -> cb.onStreamPresentationEnd(track)); + return; + } + } + NativePositionEventHandlerDelegate delegate = track.mEventHandlerDelegate; if (delegate != null) { Handler handler = delegate.getHandler(); @@ -2995,7 +3140,8 @@ public class AudioTrack extends PlayerBase private native final int native_setup(Object /*WeakReference*/ audiotrack_this, Object /*AudioAttributes*/ attributes, int[] sampleRate, int channelMask, int channelIndexMask, int audioFormat, - int buffSizeInBytes, int mode, int[] sessionId, long nativeAudioTrack); + int buffSizeInBytes, int mode, int[] sessionId, long nativeAudioTrack, + boolean offload); private native final void native_finalize(); diff --git a/android/media/DataSourceDesc.java b/android/media/DataSourceDesc.java new file mode 100644 index 00000000..73fad7ad --- /dev/null +++ b/android/media/DataSourceDesc.java @@ -0,0 +1,465 @@ +/* + * Copyright 2018 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.Nullable; +import android.content.Context; +import android.content.res.AssetFileDescriptor; +import android.net.Uri; +import android.os.Parcel; +import android.os.Parcelable; + +import com.android.internal.util.Preconditions; + +import java.io.FileDescriptor; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.net.HttpCookie; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Structure for data source descriptor. + * + * Used by {@link MediaPlayer2#setDataSource(DataSourceDesc)} + * to set data source for playback. + * + *

Users should use {@link Builder} to change {@link DataSourceDesc}. + * + */ +public final class DataSourceDesc { + /* No data source has been set yet */ + public static final int TYPE_NONE = 0; + /* data source is type of MediaDataSource */ + public static final int TYPE_CALLBACK = 1; + /* data source is type of FileDescriptor */ + public static final int TYPE_FD = 2; + /* data source is type of Uri */ + public static final int TYPE_URI = 3; + + // intentionally less than long.MAX_VALUE + public static final long LONG_MAX = 0x7ffffffffffffffL; + + private int mType = TYPE_NONE; + + private Media2DataSource mMedia2DataSource; + + private FileDescriptor mFD; + private long mFDOffset = 0; + private long mFDLength = LONG_MAX; + + private Uri mUri; + private Map mUriHeader; + private List mUriCookies; + private Context mUriContext; + + private long mId = 0; + private long mStartPositionMs = 0; + private long mEndPositionMs = LONG_MAX; + + private DataSourceDesc() { + } + + /** + * Return the Id of data source. + * @return the Id of data source + */ + public long getId() { + return mId; + } + + /** + * Return the position in milliseconds at which the playback will start. + * @return the position in milliseconds at which the playback will start + */ + public long getStartPosition() { + return mStartPositionMs; + } + + /** + * Return the position in milliseconds at which the playback will end. + * -1 means ending at the end of source content. + * @return the position in milliseconds at which the playback will end + */ + public long getEndPosition() { + return mEndPositionMs; + } + + /** + * Return the type of data source. + * @return the type of data source + */ + public int getType() { + return mType; + } + + /** + * Return the Media2DataSource of this data source. + * It's meaningful only when {@code getType} returns {@link #TYPE_CALLBACK}. + * @return the Media2DataSource of this data source + */ + public Media2DataSource getMedia2DataSource() { + return mMedia2DataSource; + } + + /** + * Return the FileDescriptor of this data source. + * It's meaningful only when {@code getType} returns {@link #TYPE_FD}. + * @return the FileDescriptor of this data source + */ + public FileDescriptor getFileDescriptor() { + return mFD; + } + + /** + * Return the offset associated with the FileDescriptor of this data source. + * It's meaningful only when {@code getType} returns {@link #TYPE_FD} and it has + * been set by the {@link Builder}. + * @return the offset associated with the FileDescriptor of this data source + */ + public long getFileDescriptorOffset() { + return mFDOffset; + } + + /** + * Return the content length associated with the FileDescriptor of this data source. + * It's meaningful only when {@code getType} returns {@link #TYPE_FD}. + * -1 means same as the length of source content. + * @return the content length associated with the FileDescriptor of this data source + */ + public long getFileDescriptorLength() { + return mFDLength; + } + + /** + * Return the Uri of this data source. + * It's meaningful only when {@code getType} returns {@link #TYPE_URI}. + * @return the Uri of this data source + */ + public Uri getUri() { + return mUri; + } + + /** + * Return the Uri headers of this data source. + * It's meaningful only when {@code getType} returns {@link #TYPE_URI}. + * @return the Uri headers of this data source + */ + public Map getUriHeaders() { + if (mUriHeader == null) { + return null; + } + return new HashMap(mUriHeader); + } + + /** + * Return the Uri cookies of this data source. + * It's meaningful only when {@code getType} returns {@link #TYPE_URI}. + * @return the Uri cookies of this data source + */ + public List getUriCookies() { + if (mUriCookies == null) { + return null; + } + return new ArrayList(mUriCookies); + } + + /** + * Return the Context used for resolving the Uri of this data source. + * It's meaningful only when {@code getType} returns {@link #TYPE_URI}. + * @return the Context used for resolving the Uri of this data source + */ + public Context getUriContext() { + return mUriContext; + } + + /** + * Builder class for {@link DataSourceDesc} objects. + *

Here is an example where Builder is used to define the + * {@link DataSourceDesc} to be used by a {@link MediaPlayer2} instance: + * + *

+     * DataSourceDesc oldDSD = mediaplayer2.getDataSourceDesc();
+     * DataSourceDesc newDSD = new DataSourceDesc.Builder(oldDSD)
+     *         .setStartPosition(1000)
+     *         .setEndPosition(15000)
+     *         .build();
+     * mediaplayer2.setDataSourceDesc(newDSD);
+     * 
+ */ + public static class Builder { + private int mType = TYPE_NONE; + + private Media2DataSource mMedia2DataSource; + + private FileDescriptor mFD; + private long mFDOffset = 0; + private long mFDLength = LONG_MAX; + + private Uri mUri; + private Map mUriHeader; + private List mUriCookies; + private Context mUriContext; + + private long mId = 0; + private long mStartPositionMs = 0; + private long mEndPositionMs = LONG_MAX; + + /** + * Constructs a new Builder with the defaults. + */ + public Builder() { + } + + /** + * Constructs a new Builder from a given {@link DataSourceDesc} instance + * @param dsd the {@link DataSourceDesc} object whose data will be reused + * in the new Builder. + */ + public Builder(DataSourceDesc dsd) { + mType = dsd.mType; + mMedia2DataSource = dsd.mMedia2DataSource; + mFD = dsd.mFD; + mFDOffset = dsd.mFDOffset; + mFDLength = dsd.mFDLength; + mUri = dsd.mUri; + mUriHeader = dsd.mUriHeader; + mUriCookies = dsd.mUriCookies; + mUriContext = dsd.mUriContext; + + mId = dsd.mId; + mStartPositionMs = dsd.mStartPositionMs; + mEndPositionMs = dsd.mEndPositionMs; + } + + /** + * Combines all of the fields that have been set and return a new + * {@link DataSourceDesc} object. IllegalStateException will be + * thrown if there is conflict between fields. + * + * @return a new {@link DataSourceDesc} object + */ + public DataSourceDesc build() { + if (mType != TYPE_CALLBACK + && mType != TYPE_FD + && mType != TYPE_URI) { + throw new IllegalStateException("Illegal type: " + mType); + } + if (mStartPositionMs > mEndPositionMs) { + throw new IllegalStateException("Illegal start/end position: " + + mStartPositionMs + " : " + mEndPositionMs); + } + + DataSourceDesc dsd = new DataSourceDesc(); + dsd.mType = mType; + dsd.mMedia2DataSource = mMedia2DataSource; + dsd.mFD = mFD; + dsd.mFDOffset = mFDOffset; + dsd.mFDLength = mFDLength; + dsd.mUri = mUri; + dsd.mUriHeader = mUriHeader; + dsd.mUriCookies = mUriCookies; + dsd.mUriContext = mUriContext; + + dsd.mId = mId; + dsd.mStartPositionMs = mStartPositionMs; + dsd.mEndPositionMs = mEndPositionMs; + + return dsd; + } + + /** + * Sets the Id of this data source. + * + * @param id the Id of this data source + * @return the same Builder instance. + */ + public Builder setId(long id) { + mId = id; + return this; + } + + /** + * Sets the start position in milliseconds at which the playback will start. + * Any negative number is treated as 0. + * + * @param position the start position in milliseconds at which the playback will start + * @return the same Builder instance. + * + */ + public Builder setStartPosition(long position) { + if (position < 0) { + position = 0; + } + mStartPositionMs = position; + return this; + } + + /** + * Sets the end position in milliseconds at which the playback will end. + * Any negative number is treated as maximum length of the data source. + * + * @param position the end position in milliseconds at which the playback will end + * @return the same Builder instance. + */ + public Builder setEndPosition(long position) { + if (position < 0) { + position = LONG_MAX; + } + mEndPositionMs = position; + return this; + } + + /** + * Sets the data source (Media2DataSource) to use. + * + * @param m2ds the Media2DataSource for the media you want to play + * @return the same Builder instance. + * @throws NullPointerException if m2ds is null. + */ + public Builder setDataSource(Media2DataSource m2ds) { + Preconditions.checkNotNull(m2ds); + resetDataSource(); + mType = TYPE_CALLBACK; + mMedia2DataSource = m2ds; + return this; + } + + /** + * Sets the data source (FileDescriptor) to use. The FileDescriptor must be + * seekable (N.B. a LocalSocket is not seekable). It is the caller's responsibility + * to close the file descriptor after the source has been used. + * + * @param fd the FileDescriptor for the file you want to play + * @return the same Builder instance. + * @throws NullPointerException if fd is null. + */ + public Builder setDataSource(FileDescriptor fd) { + Preconditions.checkNotNull(fd); + resetDataSource(); + mType = TYPE_FD; + mFD = fd; + return this; + } + + /** + * Sets the data source (FileDescriptor) to use. The FileDescriptor must be + * seekable (N.B. a LocalSocket is not seekable). It is the caller's responsibility + * to close the file descriptor after the source has been used. + * + * Any negative number for offset is treated as 0. + * Any negative number for length is treated as maximum length of the data source. + * + * @param fd the FileDescriptor for the file you want to play + * @param offset the offset into the file where the data to be played starts, in bytes + * @param length the length in bytes of the data to be played + * @return the same Builder instance. + * @throws NullPointerException if fd is null. + */ + public Builder setDataSource(FileDescriptor fd, long offset, long length) { + Preconditions.checkNotNull(fd); + if (offset < 0) { + offset = 0; + } + if (length < 0) { + length = LONG_MAX; + } + resetDataSource(); + mType = TYPE_FD; + mFD = fd; + mFDOffset = offset; + mFDLength = length; + return this; + } + + /** + * Sets the data source as a content Uri. + * + * @param context the Context to use when resolving the Uri + * @param uri the Content URI of the data you want to play + * @return the same Builder instance. + * @throws NullPointerException if context or uri is null. + */ + public Builder setDataSource(@NonNull Context context, @NonNull Uri uri) { + Preconditions.checkNotNull(context, "context cannot be null"); + Preconditions.checkNotNull(uri, "uri cannot be null"); + resetDataSource(); + mType = TYPE_URI; + mUri = uri; + mUriContext = context; + return this; + } + + /** + * Sets the data source as a content Uri. + * + * To provide cookies for the subsequent HTTP requests, you can install your own default + * cookie handler and use other variants of setDataSource APIs instead. Alternatively, you + * can use this API to pass the cookies as a list of HttpCookie. If the app has not + * installed a CookieHandler already, {@link MediaPlayer2} will create a CookieManager + * and populates its CookieStore with the provided cookies when this data source is passed + * to {@link MediaPlayer2}. If the app has installed its own handler already, the handler + * is required to be of CookieManager type such that {@link MediaPlayer2} can update the + * manager’s CookieStore. + * + *

Note that the cross domain redirection is allowed by default, + * but that can be changed with key/value pairs through the headers parameter with + * "android-allow-cross-domain-redirect" as the key and "0" or "1" as the value to + * disallow or allow cross domain redirection. + * + * @param context the Context to use when resolving the Uri + * @param uri the Content URI of the data you want to play + * @param headers the headers to be sent together with the request for the data + * The headers must not include cookies. Instead, use the cookies param. + * @param cookies the cookies to be sent together with the request + * @return the same Builder instance. + * @throws NullPointerException if context or uri is null. + */ + public Builder setDataSource(@NonNull Context context, @NonNull Uri uri, + @Nullable Map headers, @Nullable List cookies) { + Preconditions.checkNotNull(uri); + resetDataSource(); + mType = TYPE_URI; + mUri = uri; + if (headers != null) { + mUriHeader = new HashMap(headers); + } + if (cookies != null) { + mUriCookies = new ArrayList(cookies); + } + mUriContext = context; + return this; + } + + private void resetDataSource() { + mType = TYPE_NONE; + mMedia2DataSource = null; + mFD = null; + mFDOffset = 0; + mFDLength = LONG_MAX; + mUri = null; + mUriHeader = null; + mUriCookies = null; + mUriContext = null; + } + } +} diff --git a/android/media/Media2DataSource.java b/android/media/Media2DataSource.java new file mode 100644 index 00000000..8ee4a705 --- /dev/null +++ b/android/media/Media2DataSource.java @@ -0,0 +1,62 @@ +/* + * Copyright 2017 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 java.io.Closeable; +import java.io.IOException; + +/** + * For supplying media data to the framework. Implement this if your app has + * special requirements for the way media data is obtained. + * + *

Methods of this interface may be called on multiple different + * threads. There will be a thread synchronization point between each call to ensure that + * modifications to the state of your Media2DataSource are visible to future calls. This means + * you don't need to do your own synchronization unless you're modifying the + * Media2DataSource from another thread while it's being used by the framework.

+ * + */ +public abstract class Media2DataSource implements Closeable { + /** + * Called to request data from the given position. + * + * Implementations should should write up to {@code size} bytes into + * {@code buffer}, and return the number of bytes written. + * + * Return {@code 0} if size is zero (thus no bytes are read). + * + * Return {@code -1} to indicate that end of stream is reached. + * + * @param position the position in the data source to read from. + * @param buffer the buffer to read the data into. + * @param offset the offset within buffer to read the data into. + * @param size the number of bytes to read. + * @throws IOException on fatal errors. + * @return the number of bytes read, or -1 if there was an error. + */ + public abstract int readAt(long position, byte[] buffer, int offset, int size) + throws IOException; + + /** + * Called to get the size of the data source. + * + * @throws IOException on fatal errors + * @return the size of data source in bytes, or -1 if the size is unknown. + */ + public abstract long getSize() throws IOException; +} diff --git a/android/media/Media2HTTPConnection.java b/android/media/Media2HTTPConnection.java new file mode 100644 index 00000000..0d7825a0 --- /dev/null +++ b/android/media/Media2HTTPConnection.java @@ -0,0 +1,385 @@ +/* + * Copyright 2017 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.net.NetworkUtils; +import android.os.StrictMode; +import android.util.Log; + +import java.io.BufferedInputStream; +import java.io.InputStream; +import java.io.IOException; +import java.net.CookieHandler; +import java.net.CookieManager; +import java.net.Proxy; +import java.net.URL; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.NoRouteToHostException; +import java.net.ProtocolException; +import java.net.UnknownServiceException; +import java.util.HashMap; +import java.util.Map; + +import static android.media.MediaPlayer2.MEDIA_ERROR_UNSUPPORTED; + +/** @hide */ +public class Media2HTTPConnection { + private static final String TAG = "Media2HTTPConnection"; + private static final boolean VERBOSE = false; + + // connection timeout - 30 sec + private static final int CONNECT_TIMEOUT_MS = 30 * 1000; + + private long mCurrentOffset = -1; + private URL mURL = null; + private Map mHeaders = null; + private HttpURLConnection mConnection = null; + private long mTotalSize = -1; + private InputStream mInputStream = null; + + private boolean mAllowCrossDomainRedirect = true; + private boolean mAllowCrossProtocolRedirect = true; + + // from com.squareup.okhttp.internal.http + private final static int HTTP_TEMP_REDIRECT = 307; + private final static int MAX_REDIRECTS = 20; + + public Media2HTTPConnection() { + CookieHandler cookieHandler = CookieHandler.getDefault(); + if (cookieHandler == null) { + Log.w(TAG, "Media2HTTPConnection: Unexpected. No CookieHandler found."); + } + } + + public boolean connect(String uri, String headers) { + if (VERBOSE) { + Log.d(TAG, "connect: uri=" + uri + ", headers=" + headers); + } + + try { + disconnect(); + mAllowCrossDomainRedirect = true; + mURL = new URL(uri); + mHeaders = convertHeaderStringToMap(headers); + } catch (MalformedURLException e) { + return false; + } + + return true; + } + + private boolean parseBoolean(String val) { + try { + return Long.parseLong(val) != 0; + } catch (NumberFormatException e) { + return "true".equalsIgnoreCase(val) || + "yes".equalsIgnoreCase(val); + } + } + + /* returns true iff header is internal */ + private boolean filterOutInternalHeaders(String key, String val) { + if ("android-allow-cross-domain-redirect".equalsIgnoreCase(key)) { + mAllowCrossDomainRedirect = parseBoolean(val); + // cross-protocol redirects are also controlled by this flag + mAllowCrossProtocolRedirect = mAllowCrossDomainRedirect; + } else { + return false; + } + return true; + } + + private Map convertHeaderStringToMap(String headers) { + HashMap map = new HashMap(); + + String[] pairs = headers.split("\r\n"); + for (String pair : pairs) { + int colonPos = pair.indexOf(":"); + if (colonPos >= 0) { + String key = pair.substring(0, colonPos); + String val = pair.substring(colonPos + 1); + + if (!filterOutInternalHeaders(key, val)) { + map.put(key, val); + } + } + } + + return map; + } + + public void disconnect() { + teardownConnection(); + mHeaders = null; + mURL = null; + } + + private void teardownConnection() { + if (mConnection != null) { + if (mInputStream != null) { + try { + mInputStream.close(); + } catch (IOException e) { + } + mInputStream = null; + } + + mConnection.disconnect(); + mConnection = null; + + mCurrentOffset = -1; + } + } + + private static final boolean isLocalHost(URL url) { + if (url == null) { + return false; + } + + String host = url.getHost(); + + if (host == null) { + return false; + } + + try { + if (host.equalsIgnoreCase("localhost")) { + return true; + } + if (NetworkUtils.numericToInetAddress(host).isLoopbackAddress()) { + return true; + } + } catch (IllegalArgumentException iex) { + } + return false; + } + + private void seekTo(long offset) throws IOException { + teardownConnection(); + + try { + int response; + int redirectCount = 0; + + URL url = mURL; + + // do not use any proxy for localhost (127.0.0.1) + boolean noProxy = isLocalHost(url); + + while (true) { + if (noProxy) { + mConnection = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY); + } else { + mConnection = (HttpURLConnection)url.openConnection(); + } + mConnection.setConnectTimeout(CONNECT_TIMEOUT_MS); + + // handle redirects ourselves if we do not allow cross-domain redirect + mConnection.setInstanceFollowRedirects(mAllowCrossDomainRedirect); + + if (mHeaders != null) { + for (Map.Entry entry : mHeaders.entrySet()) { + mConnection.setRequestProperty( + entry.getKey(), entry.getValue()); + } + } + + if (offset > 0) { + mConnection.setRequestProperty( + "Range", "bytes=" + offset + "-"); + } + + response = mConnection.getResponseCode(); + if (response != HttpURLConnection.HTTP_MULT_CHOICE && + response != HttpURLConnection.HTTP_MOVED_PERM && + response != HttpURLConnection.HTTP_MOVED_TEMP && + response != HttpURLConnection.HTTP_SEE_OTHER && + response != HTTP_TEMP_REDIRECT) { + // not a redirect, or redirect handled by HttpURLConnection + break; + } + + if (++redirectCount > MAX_REDIRECTS) { + throw new NoRouteToHostException("Too many redirects: " + redirectCount); + } + + String method = mConnection.getRequestMethod(); + if (response == HTTP_TEMP_REDIRECT && + !method.equals("GET") && !method.equals("HEAD")) { + // "If the 307 status code is received in response to a + // request other than GET or HEAD, the user agent MUST NOT + // automatically redirect the request" + throw new NoRouteToHostException("Invalid redirect"); + } + String location = mConnection.getHeaderField("Location"); + if (location == null) { + throw new NoRouteToHostException("Invalid redirect"); + } + url = new URL(mURL /* TRICKY: don't use url! */, location); + if (!url.getProtocol().equals("https") && + !url.getProtocol().equals("http")) { + throw new NoRouteToHostException("Unsupported protocol redirect"); + } + boolean sameProtocol = mURL.getProtocol().equals(url.getProtocol()); + if (!mAllowCrossProtocolRedirect && !sameProtocol) { + throw new NoRouteToHostException("Cross-protocol redirects are disallowed"); + } + boolean sameHost = mURL.getHost().equals(url.getHost()); + if (!mAllowCrossDomainRedirect && !sameHost) { + throw new NoRouteToHostException("Cross-domain redirects are disallowed"); + } + + if (response != HTTP_TEMP_REDIRECT) { + // update effective URL, unless it is a Temporary Redirect + mURL = url; + } + } + + if (mAllowCrossDomainRedirect) { + // remember the current, potentially redirected URL if redirects + // were handled by HttpURLConnection + mURL = mConnection.getURL(); + } + + if (response == HttpURLConnection.HTTP_PARTIAL) { + // Partial content, we cannot just use getContentLength + // because what we want is not just the length of the range + // returned but the size of the full content if available. + + String contentRange = + mConnection.getHeaderField("Content-Range"); + + mTotalSize = -1; + if (contentRange != null) { + // format is "bytes xxx-yyy/zzz + // where "zzz" is the total number of bytes of the + // content or '*' if unknown. + + int lastSlashPos = contentRange.lastIndexOf('/'); + if (lastSlashPos >= 0) { + String total = + contentRange.substring(lastSlashPos + 1); + + try { + mTotalSize = Long.parseLong(total); + } catch (NumberFormatException e) { + } + } + } + } else if (response != HttpURLConnection.HTTP_OK) { + throw new IOException(); + } else { + mTotalSize = mConnection.getContentLength(); + } + + if (offset > 0 && response != HttpURLConnection.HTTP_PARTIAL) { + // Some servers simply ignore "Range" requests and serve + // data from the start of the content. + throw new ProtocolException(); + } + + mInputStream = + new BufferedInputStream(mConnection.getInputStream()); + + mCurrentOffset = offset; + } catch (IOException e) { + mTotalSize = -1; + teardownConnection(); + mCurrentOffset = -1; + + throw e; + } + } + + public int readAt(long offset, byte[] data, int size) { + StrictMode.ThreadPolicy policy = + new StrictMode.ThreadPolicy.Builder().permitAll().build(); + + StrictMode.setThreadPolicy(policy); + + try { + if (offset != mCurrentOffset) { + seekTo(offset); + } + + int n = mInputStream.read(data, 0, size); + + if (n == -1) { + // InputStream signals EOS using a -1 result, our semantics + // are to return a 0-length read. + n = 0; + } + + mCurrentOffset += n; + + if (VERBOSE) { + Log.d(TAG, "readAt " + offset + " / " + size + " => " + n); + } + + return n; + } catch (ProtocolException e) { + Log.w(TAG, "readAt " + offset + " / " + size + " => " + e); + return MEDIA_ERROR_UNSUPPORTED; + } catch (NoRouteToHostException e) { + Log.w(TAG, "readAt " + offset + " / " + size + " => " + e); + return MEDIA_ERROR_UNSUPPORTED; + } catch (UnknownServiceException e) { + Log.w(TAG, "readAt " + offset + " / " + size + " => " + e); + return MEDIA_ERROR_UNSUPPORTED; + } catch (IOException e) { + if (VERBOSE) { + Log.d(TAG, "readAt " + offset + " / " + size + " => -1"); + } + return -1; + } catch (Exception e) { + if (VERBOSE) { + Log.d(TAG, "unknown exception " + e); + Log.d(TAG, "readAt " + offset + " / " + size + " => -1"); + } + return -1; + } + } + + public long getSize() { + if (mConnection == null) { + try { + seekTo(0); + } catch (IOException e) { + return -1; + } + } + + return mTotalSize; + } + + public String getMIMEType() { + if (mConnection == null) { + try { + seekTo(0); + } catch (IOException e) { + return "application/octet-stream"; + } + } + + return mConnection.getContentType(); + } + + public String getUri() { + return mURL.toString(); + } +} diff --git a/android/media/Media2HTTPService.java b/android/media/Media2HTTPService.java new file mode 100644 index 00000000..957aceca --- /dev/null +++ b/android/media/Media2HTTPService.java @@ -0,0 +1,98 @@ +/* + * Copyright (C) 2017 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.util.Log; + +import java.net.CookieHandler; +import java.net.CookieManager; +import java.net.CookieStore; +import java.net.HttpCookie; +import java.util.List; + +/** @hide */ +public class Media2HTTPService { + private static final String TAG = "Media2HTTPService"; + private List mCookies; + private Boolean mCookieStoreInitialized = new Boolean(false); + + public Media2HTTPService(List cookies) { + mCookies = cookies; + Log.v(TAG, "Media2HTTPService(" + this + "): Cookies: " + cookies); + } + + public Media2HTTPConnection makeHTTPConnection() { + + synchronized (mCookieStoreInitialized) { + // Only need to do it once for all connections + if ( !mCookieStoreInitialized ) { + CookieHandler cookieHandler = CookieHandler.getDefault(); + if (cookieHandler == null) { + cookieHandler = new CookieManager(); + CookieHandler.setDefault(cookieHandler); + Log.v(TAG, "makeHTTPConnection: CookieManager created: " + cookieHandler); + } else { + Log.v(TAG, "makeHTTPConnection: CookieHandler (" + cookieHandler + ") exists."); + } + + // Applying the bootstrapping cookies + if ( mCookies != null ) { + if ( cookieHandler instanceof CookieManager ) { + CookieManager cookieManager = (CookieManager)cookieHandler; + CookieStore store = cookieManager.getCookieStore(); + for ( HttpCookie cookie : mCookies ) { + try { + store.add(null, cookie); + } catch ( Exception e ) { + Log.v(TAG, "makeHTTPConnection: CookieStore.add" + e); + } + //for extended debugging when needed + //Log.v(TAG, "MediaHTTPConnection adding Cookie[" + cookie.getName() + + // "]: " + cookie); + } + } else { + Log.w(TAG, "makeHTTPConnection: The installed CookieHandler is not a " + + "CookieManager. Can’t add the provided cookies to the cookie " + + "store."); + } + } // mCookies + + mCookieStoreInitialized = true; + + Log.v(TAG, "makeHTTPConnection(" + this + "): cookieHandler: " + cookieHandler + + " Cookies: " + mCookies); + } // mCookieStoreInitialized + } // synchronized + + return new Media2HTTPConnection(); + } + + /* package private */ static Media2HTTPService createHTTPService(String path) { + return createHTTPService(path, null); + } + + // when cookies are provided + static Media2HTTPService createHTTPService(String path, List cookies) { + if (path.startsWith("http://") || path.startsWith("https://")) { + return (new Media2HTTPService(cookies)); + } else if (path.startsWith("widevine://")) { + Log.d(TAG, "Widevine classic is no longer supported"); + } + + return null; + } +} diff --git a/android/media/MediaBrowser2.java b/android/media/MediaBrowser2.java new file mode 100644 index 00000000..be4be3fc --- /dev/null +++ b/android/media/MediaBrowser2.java @@ -0,0 +1,176 @@ +/* + * Copyright 2018 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.NonNull; +import android.annotation.Nullable; +import android.content.Context; +import android.media.update.ApiLoader; +import android.media.update.MediaBrowser2Provider; +import android.os.Bundle; + +import java.util.List; +import java.util.concurrent.Executor; + +/** + * Browses media content offered by a {@link MediaLibraryService2}. + * @hide + */ +public class MediaBrowser2 extends MediaController2 { + // Equals to the ((MediaBrowser2Provider) getProvider()) + private final MediaBrowser2Provider mProvider; + + /** + * Callback to listen events from {@link MediaLibraryService2}. + */ + public static class BrowserCallback extends MediaController2.ControllerCallback { + /** + * Called with the result of {@link #getBrowserRoot(Bundle)}. + *

+ * {@code rootMediaId} and {@code rootExtra} can be {@code null} if the browser root isn't + * available. + * + * @param rootHints rootHints that you previously requested. + * @param rootMediaId media id of the browser root. Can be {@code null} + * @param rootExtra extra of the browser root. Can be {@code null} + */ + public void onGetRootResult(Bundle rootHints, @Nullable String rootMediaId, + @Nullable Bundle rootExtra) { } + + /** + * Called when the item has been returned by the library service for the previous + * {@link MediaBrowser2#getItem} call. + *

+ * Result can be null if there had been error. + * + * @param mediaId media id + * @param result result. Can be {@code null} + */ + public void onItemLoaded(@NonNull String mediaId, @Nullable MediaItem2 result) { } + + /** + * Called when the list of items has been returned by the library service for the previous + * {@link MediaBrowser2#getChildren(String, int, int, Bundle)}. + * + * @param parentId parent id + * @param page page number that you've specified + * @param pageSize page size that you've specified + * @param options optional bundle that you've specified + * @param result result. Can be {@code null} + */ + public void onChildrenLoaded(@NonNull String parentId, int page, int pageSize, + @Nullable Bundle options, @Nullable List result) { } + + /** + * Called when there's change in the parent's children. + * + * @param parentId parent id that you've specified with subscribe + * @param options optional bundle that you've specified with subscribe + */ + public void onChildrenChanged(@NonNull String parentId, @Nullable Bundle options) { } + + /** + * Called when the search result has been returned by the library service for the previous + * {@link MediaBrowser2#search(String, int, int, Bundle)}. + *

+ * Result can be null if there had been error. + * + * @param query query string that you've specified + * @param page page number that you've specified + * @param pageSize page size that you've specified + * @param options optional bundle that you've specified + * @param result result. Can be {@code null} + */ + public void onSearchResult(@NonNull String query, int page, int pageSize, + @Nullable Bundle options, @Nullable List result) { } + } + + public MediaBrowser2(Context context, SessionToken2 token, BrowserCallback callback, + Executor executor) { + super(context, token, callback, executor); + mProvider = (MediaBrowser2Provider) getProvider(); + } + + @Override + MediaBrowser2Provider createProvider(Context context, SessionToken2 token, + ControllerCallback callback, Executor executor) { + return ApiLoader.getProvider(context) + .createMediaBrowser2(this, context, token, (BrowserCallback) callback, executor); + } + + public void getBrowserRoot(Bundle rootHints) { + mProvider.getBrowserRoot_impl(rootHints); + } + + /** + * Subscribe to a parent id for the change in its children. When there's a change, + * {@link BrowserCallback#onChildrenChanged(String, Bundle)} will be called with the bundle + * that you've specified. You should call {@link #getChildren(String, int, int, Bundle)} to get + * the actual contents for the parent. + * + * @param parentId parent id + * @param options optional bundle + */ + public void subscribe(String parentId, @Nullable Bundle options) { + mProvider.subscribe_impl(parentId, options); + } + + /** + * Unsubscribe for changes to the children of the parent, which was previously subscribed with + * {@link #subscribe(String, Bundle)}. + * + * @param parentId parent id + * @param options optional bundle + */ + public void unsubscribe(String parentId, @Nullable Bundle options) { + mProvider.unsubscribe_impl(parentId, options); + } + + /** + * Get the media item with the given media id. Result would be sent back asynchronously with the + * {@link BrowserCallback#onItemLoaded(String, MediaItem2)}. + * + * @param mediaId media id + */ + public void getItem(String mediaId) { + mProvider.getItem_impl(mediaId); + } + + /** + * Get list of children under the parent. Result would be sent back asynchronously with the + * {@link BrowserCallback#onChildrenLoaded(String, int, int, Bundle, List)}. + * + * @param parentId + * @param page + * @param pageSize + * @param options + */ + public void getChildren(String parentId, int page, int pageSize, @Nullable Bundle options) { + mProvider.getChildren_impl(parentId, page, pageSize, options); + } + + /** + * + * @param query search query deliminated by string + * @param page page number to get search result. Starts from {@code 1} + * @param pageSize page size. Should be greater or equal to {@code 1} + * @param extras extra bundle + */ + public void search(String query, int page, int pageSize, Bundle extras) { + mProvider.search_impl(query, page, pageSize, extras); + } +} diff --git a/android/media/MediaBrowser2Test.java b/android/media/MediaBrowser2Test.java new file mode 100644 index 00000000..5c960c85 --- /dev/null +++ b/android/media/MediaBrowser2Test.java @@ -0,0 +1,141 @@ +/* + * Copyright 2018 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 static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertTrue; + +import android.content.Context; +import android.media.MediaBrowser2.BrowserCallback; +import android.media.MediaSession2.CommandGroup; +import android.os.Bundle; +import android.support.annotation.CallSuper; +import android.support.annotation.NonNull; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +/** + * Tests {@link MediaBrowser2}. + *

+ * This test inherits {@link MediaController2Test} to ensure that inherited APIs from + * {@link MediaController2} works cleanly. + */ +// TODO(jaewan): Implement host-side test so browser and service can run in different processes. +@RunWith(AndroidJUnit4.class) +@SmallTest +public class MediaBrowser2Test extends MediaController2Test { + private static final String TAG = "MediaBrowser2Test"; + + @Override + TestControllerInterface onCreateController(@NonNull SessionToken2 token, + @NonNull TestControllerCallbackInterface callback) { + return new TestMediaBrowser(mContext, token, new TestBrowserCallback(callback)); + } + + @Test + public void testGetBrowserRoot() throws InterruptedException { + final Bundle param = new Bundle(); + param.putString(TAG, TAG); + + final CountDownLatch latch = new CountDownLatch(1); + final TestControllerCallbackInterface callback = new TestControllerCallbackInterface() { + @Override + public void onGetRootResult(Bundle rootHints, String rootMediaId, Bundle rootExtra) { + assertTrue(TestUtils.equals(param, rootHints)); + assertEquals(MockMediaLibraryService2.ROOT_ID, rootMediaId); + assertTrue(TestUtils.equals(MockMediaLibraryService2.EXTRA, rootExtra)); + latch.countDown(); + } + }; + + final SessionToken2 token = MockMediaLibraryService2.getToken(mContext); + MediaBrowser2 browser = + (MediaBrowser2) createController(token,true, callback); + browser.getBrowserRoot(param); + assertTrue(latch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS)); + } + + public static class TestBrowserCallback extends BrowserCallback + implements WaitForConnectionInterface { + private final TestControllerCallbackInterface mCallbackProxy; + public final CountDownLatch connectLatch = new CountDownLatch(1); + public final CountDownLatch disconnectLatch = new CountDownLatch(1); + + TestBrowserCallback(TestControllerCallbackInterface callbackProxy) { + mCallbackProxy = callbackProxy; + } + + @CallSuper + @Override + public void onConnected(CommandGroup commands) { + super.onConnected(commands); + connectLatch.countDown(); + } + + @CallSuper + @Override + public void onDisconnected() { + super.onDisconnected(); + disconnectLatch.countDown(); + } + + @Override + public void onGetRootResult(Bundle rootHints, String rootMediaId, Bundle rootExtra) { + mCallbackProxy.onGetRootResult(rootHints, rootMediaId, rootExtra); + } + + @Override + public void waitForConnect(boolean expect) throws InterruptedException { + if (expect) { + assertTrue(connectLatch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS)); + } else { + assertFalse(connectLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); + } + } + + @Override + public void waitForDisconnect(boolean expect) throws InterruptedException { + if (expect) { + assertTrue(disconnectLatch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS)); + } else { + assertFalse(disconnectLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); + } + } + } + + public class TestMediaBrowser extends MediaBrowser2 implements TestControllerInterface { + private final BrowserCallback mCallback; + + public TestMediaBrowser(@NonNull Context context, @NonNull SessionToken2 token, + @NonNull ControllerCallback callback) { + super(context, token, (BrowserCallback) callback, sHandlerExecutor); + mCallback = (BrowserCallback) callback; + } + + @Override + public BrowserCallback getCallback() { + return mCallback; + } + } +} \ No newline at end of file diff --git a/android/media/MediaCodecInfo.java b/android/media/MediaCodecInfo.java index f41e33f7..44d90997 100644 --- a/android/media/MediaCodecInfo.java +++ b/android/media/MediaCodecInfo.java @@ -2639,7 +2639,8 @@ public final class MediaCodecInfo { /** * Returns the supported range of quality values. * - * @hide + * Quality is implementation-specific. As a general rule, a higher quality + * setting results in a better image quality and a lower compression ratio. */ public Range getQualityRange() { return mQualityRange; @@ -2751,7 +2752,7 @@ public final class MediaCodecInfo { } if (info.containsKey("feature-bitrate-modes")) { for (String mode: info.getString("feature-bitrate-modes").split(",")) { - mBitControl |= parseBitrateMode(mode); + mBitControl |= (1 << parseBitrateMode(mode)); } } diff --git a/android/media/MediaController2.java b/android/media/MediaController2.java new file mode 100644 index 00000000..d669bc12 --- /dev/null +++ b/android/media/MediaController2.java @@ -0,0 +1,616 @@ +/* + * Copyright 2018 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.NonNull; +import android.annotation.Nullable; +import android.app.PendingIntent; +import android.content.Context; +import android.media.MediaSession2.Command; +import android.media.MediaSession2.CommandButton; +import android.media.MediaSession2.CommandGroup; +import android.media.MediaSession2.ControllerInfo; +import android.media.MediaSession2.PlaylistParam; +import android.media.session.MediaSessionManager; +import android.media.update.ApiLoader; +import android.media.update.MediaController2Provider; +import android.net.Uri; +import android.os.Bundle; +import android.os.ResultReceiver; + +import java.util.List; +import java.util.concurrent.Executor; + +/** + * Allows an app to interact with an active {@link MediaSession2} or a + * {@link MediaSessionService2} in any status. Media buttons and other commands can be sent to + * the session. + *

+ * When you're done, use {@link #close()} to clean up resources. This also helps session service + * to be destroyed when there's no controller associated with it. + *

+ * When controlling {@link MediaSession2}, the controller will be available immediately after + * the creation. + *

+ * When controlling {@link MediaSessionService2}, the {@link MediaController2} would be + * available only if the session service allows this controller by + * {@link MediaSession2.SessionCallback#onConnect(ControllerInfo)} for the service. Wait + * {@link ControllerCallback#onConnected(CommandGroup)} or + * {@link ControllerCallback#onDisconnected()} for the result. + *

+ * A controller can be created through token from {@link MediaSessionManager} if you hold the + * signature|privileged permission "android.permission.MEDIA_CONTENT_CONTROL" permission or are + * an enabled notification listener or by getting a {@link SessionToken2} directly the + * the session owner. + *

+ * MediaController2 objects are thread-safe. + *

+ * @see MediaSession2 + * @see MediaSessionService2 + * @hide + */ +// TODO(jaewan): Unhide +// TODO(jaewan): Revisit comments. Currently MediaBrowser case is missing. +public class MediaController2 implements AutoCloseable { + /** + * Interface for listening to change in activeness of the {@link MediaSession2}. It's + * active if and only if it has set a player. + */ + public abstract static class ControllerCallback { + /** + * Called when the controller is successfully connected to the session. The controller + * becomes available afterwards. + * + * @param allowedCommands commands that's allowed by the session. + */ + public void onConnected(CommandGroup allowedCommands) { } + + /** + * Called when the session refuses the controller or the controller is disconnected from + * the session. The controller becomes unavailable afterwards and the callback wouldn't + * be called. + *

+ * It will be also called after the {@link #close()}, so you can put clean up code here. + * You don't need to call {@link #close()} after this. + */ + public void onDisconnected() { } + + /** + * Called when the session set the custom layout through the + * {@link MediaSession2#setCustomLayout(ControllerInfo, List)}. + *

+ * Can be called before {@link #onConnected(CommandGroup)} is called. + * + * @param layout + */ + public void onCustomLayoutChanged(List layout) { } + + /** + * Called when the session has changed anything related with the {@link PlaybackInfo}. + * + * @param info new playback info + */ + public void onAudioInfoChanged(PlaybackInfo info) { } + + /** + * Called when the allowed commands are changed by session. + * + * @param commands newly allowed commands + */ + public void onAllowedCommandsChanged(CommandGroup commands) { } + + /** + * Called when the session sent a custom command. + * + * @param command + * @param args + * @param receiver + */ + public void onCustomCommand(Command command, @Nullable Bundle args, + @Nullable ResultReceiver receiver) { } + + /** + * Called when the playlist is changed. + * + * @param list + * @param param + */ + public void onPlaylistChanged( + @NonNull List list, @NonNull PlaylistParam param) { } + + /** + * Called when the playback state is changed. + * + * @param state + */ + public void onPlaybackStateChanged(@NonNull PlaybackState2 state) { } + } + + /** + * Holds information about the current playback and how audio is handled for + * this session. + */ + // The same as MediaController.PlaybackInfo + public static final class PlaybackInfo { + /** + * The session uses remote playback. + */ + public static final int PLAYBACK_TYPE_REMOTE = 2; + /** + * The session uses local playback. + */ + public static final int PLAYBACK_TYPE_LOCAL = 1; + + private final int mVolumeType; + private final int mVolumeControl; + private final int mMaxVolume; + private final int mCurrentVolume; + private final AudioAttributes mAudioAttrs; + + /** + * @hide + */ + public PlaybackInfo(int type, AudioAttributes attrs, int control, int max, int current) { + mVolumeType = type; + mAudioAttrs = attrs; + mVolumeControl = control; + mMaxVolume = max; + mCurrentVolume = current; + } + + /** + * Get the type of playback which affects volume handling. One of: + *

    + *
  • {@link #PLAYBACK_TYPE_LOCAL}
  • + *
  • {@link #PLAYBACK_TYPE_REMOTE}
  • + *
+ * + * @return The type of playback this session is using. + */ + public int getPlaybackType() { + return mVolumeType; + } + + /** + * Get the audio attributes for this session. The attributes will affect + * volume handling for the session. When the volume type is + * {@link PlaybackInfo#PLAYBACK_TYPE_REMOTE} these may be ignored by the + * remote volume handler. + * + * @return The attributes for this session. + */ + public AudioAttributes getAudioAttributes() { + return mAudioAttrs; + } + + /** + * Get the type of volume control that can be used. One of: + *
    + *
  • {@link VolumeProvider#VOLUME_CONTROL_ABSOLUTE}
  • + *
  • {@link VolumeProvider#VOLUME_CONTROL_RELATIVE}
  • + *
  • {@link VolumeProvider#VOLUME_CONTROL_FIXED}
  • + *
+ * + * @return The type of volume control that may be used with this + * session. + */ + public int getVolumeControl() { + return mVolumeControl; + } + + /** + * Get the maximum volume that may be set for this session. + * + * @return The maximum allowed volume where this session is playing. + */ + public int getMaxVolume() { + return mMaxVolume; + } + + /** + * Get the current volume for this session. + * + * @return The current volume where this session is playing. + */ + public int getCurrentVolume() { + return mCurrentVolume; + } + } + + private final MediaController2Provider mProvider; + + /** + * Create a {@link MediaController2} from the {@link SessionToken2}. This connects to the session + * and may wake up the service if it's not available. + * + * @param context Context + * @param token token to connect to + * @param callback controller callback to receive changes in + * @param executor executor to run callbacks on. + */ + // TODO(jaewan): Put @CallbackExecutor to the constructor. + public MediaController2(@NonNull Context context, @NonNull SessionToken2 token, + @NonNull ControllerCallback callback, @NonNull Executor executor) { + super(); + + // This also connects to the token. + // Explicit connect() isn't added on purpose because retrying connect() is impossible with + // session whose session binder is only valid while it's active. + // prevent a controller from reusable after the + // session is released and recreated. + mProvider = createProvider(context, token, callback, executor); + } + + MediaController2Provider createProvider(@NonNull Context context, + @NonNull SessionToken2 token, @NonNull ControllerCallback callback, + @NonNull Executor executor) { + return ApiLoader.getProvider(context) + .createMediaController2(this, context, token, callback, executor); + } + + /** + * Release this object, and disconnect from the session. After this, callbacks wouldn't be + * received. + */ + @Override + public void close() { + mProvider.close_impl(); + } + + /** + * @hide + */ + public MediaController2Provider getProvider() { + return mProvider; + } + + /** + * @return token + */ + public @NonNull + SessionToken2 getSessionToken() { + return mProvider.getSessionToken_impl(); + } + + /** + * Returns whether this class is connected to active {@link MediaSession2} or not. + */ + public boolean isConnected() { + return mProvider.isConnected_impl(); + } + + public void play() { + mProvider.play_impl(); + } + + public void pause() { + mProvider.pause_impl(); + } + + public void stop() { + mProvider.stop_impl(); + } + + public void skipToPrevious() { + mProvider.skipToPrevious_impl(); + } + + public void skipToNext() { + mProvider.skipToNext_impl(); + } + + /** + * Request that the player prepare its playback. In other words, other sessions can continue + * to play during the preparation of this session. This method can be used to speed up the + * start of the playback. Once the preparation is done, the session will change its playback + * state to {@link PlaybackState2#STATE_PAUSED}. Afterwards, {@link #play} can be called to + * start playback. + */ + public void prepare() { + mProvider.prepare_impl(); + } + + /** + * Start fast forwarding. If playback is already fast forwarding this + * may increase the rate. + */ + public void fastForward() { + mProvider.fastForward_impl(); + } + + /** + * Start rewinding. If playback is already rewinding this may increase + * the rate. + */ + public void rewind() { + mProvider.rewind_impl(); + } + + /** + * Move to a new location in the media stream. + * + * @param pos Position to move to, in milliseconds. + */ + public void seekTo(long pos) { + mProvider.seekTo_impl(pos); + } + + /** + * Sets the index of current DataSourceDesc in the play list to be played. + * + * @param index the index of DataSourceDesc in the play list you want to play + * @throws IllegalArgumentException if the play list is null + * @throws NullPointerException if index is outside play list range + */ + public void setCurrentPlaylistItem(int index) { + mProvider.setCurrentPlaylistItem_impl(index); + } + + /** + * @hide + */ + public void skipForward() { + // To match with KEYCODE_MEDIA_SKIP_FORWARD + } + + /** + * @hide + */ + public void skipBackward() { + // To match with KEYCODE_MEDIA_SKIP_BACKWARD + } + + /** + * Request that the player start playback for a specific media id. + * + * @param mediaId The id of the requested media. + * @param extras Optional extras that can include extra information about the media item + * to be played. + */ + public void playFromMediaId(@NonNull String mediaId, @Nullable Bundle extras) { + mProvider.playFromMediaId_impl(mediaId, extras); + } + + /** + * Request that the player start playback for a specific search query. + * An empty or null query should be treated as a request to play any + * music. + * + * @param query The search query. + * @param extras Optional extras that can include extra information + * about the query. + */ + public void playFromSearch(@NonNull String query, @Nullable Bundle extras) { + mProvider.playFromSearch_impl(query, extras); + } + + /** + * Request that the player start playback for a specific {@link Uri}. + * + * @param uri The URI of the requested media. + * @param extras Optional extras that can include extra information about the media item + * to be played. + */ + public void playFromUri(@NonNull String uri, @Nullable Bundle extras) { + mProvider.playFromUri_impl(uri, extras); + } + + + /** + * Request that the player prepare playback for a specific media id. In other words, other + * sessions can continue to play during the preparation of this session. This method can be + * used to speed up the start of the playback. Once the preparation is done, the session + * will change its playback state to {@link PlaybackState2#STATE_PAUSED}. Afterwards, + * {@link #play} can be called to start playback. If the preparation is not needed, + * {@link #playFromMediaId} can be directly called without this method. + * + * @param mediaId The id of the requested media. + * @param extras Optional extras that can include extra information about the media item + * to be prepared. + */ + public void prepareFromMediaId(@NonNull String mediaId, @Nullable Bundle extras) { + mProvider.prepareMediaId_impl(mediaId, extras); + } + + /** + * Request that the player prepare playback for a specific search query. An empty or null + * query should be treated as a request to prepare any music. In other words, other sessions + * can continue to play during the preparation of this session. This method can be used to + * speed up the start of the playback. Once the preparation is done, the session will + * change its playback state to {@link PlaybackState2#STATE_PAUSED}. Afterwards, + * {@link #play} can be called to start playback. If the preparation is not needed, + * {@link #playFromSearch} can be directly called without this method. + * + * @param query The search query. + * @param extras Optional extras that can include extra information + * about the query. + */ + public void prepareFromSearch(@NonNull String query, @Nullable Bundle extras) { + mProvider.prepareFromSearch_impl(query, extras); + } + + /** + * Request that the player prepare playback for a specific {@link Uri}. In other words, + * other sessions can continue to play during the preparation of this session. This method + * can be used to speed up the start of the playback. Once the preparation is done, the + * session will change its playback state to {@link PlaybackState2#STATE_PAUSED}. Afterwards, + * {@link #play} can be called to start playback. If the preparation is not needed, + * {@link #playFromUri} can be directly called without this method. + * + * @param uri The URI of the requested media. + * @param extras Optional extras that can include extra information about the media item + * to be prepared. + */ + public void prepareFromUri(@NonNull Uri uri, @Nullable Bundle extras) { + mProvider.prepareFromUri_impl(uri, extras); + } + + /** + * Set the volume of the output this session is playing on. The command will be ignored if it + * does not support {@link VolumeProvider#VOLUME_CONTROL_ABSOLUTE}. + *

+ * If the session is local playback, this changes the device's volume with the stream that + * session's player is using. Flags will be specified for the {@link AudioManager}. + *

+ * If the session is remote player (i.e. session has set volume provider), its volume provider + * will receive this request instead. + * + * @see #getPlaybackInfo() + * @param value The value to set it to, between 0 and the reported max. + * @param flags flags from {@link AudioManager} to include with the volume request for local + * playback + */ + public void setVolumeTo(int value, int flags) { + mProvider.setVolumeTo_impl(value, flags); + } + + /** + * Adjust the volume of the output this session is playing on. The direction + * must be one of {@link AudioManager#ADJUST_LOWER}, + * {@link AudioManager#ADJUST_RAISE}, or {@link AudioManager#ADJUST_SAME}. + * The command will be ignored if the session does not support + * {@link VolumeProvider#VOLUME_CONTROL_RELATIVE} or + * {@link VolumeProvider#VOLUME_CONTROL_ABSOLUTE}. + *

+ * If the session is local playback, this changes the device's volume with the stream that + * session's player is using. Flags will be specified for the {@link AudioManager}. + *

+ * If the session is remote player (i.e. session has set volume provider), its volume provider + * will receive this request instead. + * + * @see #getPlaybackInfo() + * @param direction The direction to adjust the volume in. + * @param flags flags from {@link AudioManager} to include with the volume request for local + * playback + */ + public void adjustVolume(int direction, int flags) { + mProvider.adjustVolume_impl(direction, flags); + } + + /** + * Get the rating type supported by the session. One of: + *

    + *
  • {@link Rating2#RATING_NONE}
  • + *
  • {@link Rating2#RATING_HEART}
  • + *
  • {@link Rating2#RATING_THUMB_UP_DOWN}
  • + *
  • {@link Rating2#RATING_3_STARS}
  • + *
  • {@link Rating2#RATING_4_STARS}
  • + *
  • {@link Rating2#RATING_5_STARS}
  • + *
  • {@link Rating2#RATING_PERCENTAGE}
  • + *
+ * + * @return The supported rating type + */ + public int getRatingType() { + return mProvider.getRatingType_impl(); + } + + /** + * Get an intent for launching UI associated with this session if one exists. + * + * @return A {@link PendingIntent} to launch UI or null. + */ + public @Nullable PendingIntent getSessionActivity() { + return mProvider.getSessionActivity_impl(); + } + + /** + * Get the latest {@link PlaybackState2} from the session. + * + * @return a playback state + */ + public PlaybackState2 getPlaybackState() { + return mProvider.getPlaybackState_impl(); + } + + /** + * Get the current playback info for this session. + * + * @return The current playback info or null. + */ + public @Nullable PlaybackInfo getPlaybackInfo() { + return mProvider.getPlaybackInfo_impl(); + } + + /** + * Rate the current content. This will cause the rating to be set for + * the current user. The Rating type must match the type returned by + * {@link #getRatingType()}. + * + * @param rating The rating to set for the current content + */ + public void setRating(Rating2 rating) { + mProvider.setRating_impl(rating); + } + + /** + * Send custom command to the session + * + * @param command custom command + * @param args optional argument + * @param cb optional result receiver + */ + public void sendCustomCommand(@NonNull Command command, @Nullable Bundle args, + @Nullable ResultReceiver cb) { + mProvider.sendCustomCommand_impl(command, args, cb); + } + + /** + * Return playlist from the session. + * + * @return playlist. Can be {@code null} if the controller doesn't have enough permission. + */ + public @Nullable List getPlaylist() { + return mProvider.getPlaylist_impl(); + } + + public @Nullable PlaylistParam getPlaylistParam() { + return mProvider.getPlaylistParam_impl(); + } + + /** + * Removes the media item at index in the play list. + *

+ * If index is same as the current index of the playlist, current playback + * will be stopped and playback moves to next source in the list. + * + * @return the removed DataSourceDesc at index in the play list + * @throws IllegalArgumentException if the play list is null + * @throws IndexOutOfBoundsException if index is outside play list range + */ + // TODO(jaewan): Remove with index was previously rejected by council (b/36524925) + // TODO(jaewan): Should we also add movePlaylistItem from index to index? + public void removePlaylistItem(MediaItem2 item) { + mProvider.removePlaylistItem_impl(item); + } + + /** + * Inserts the media item to the play list at position index. + *

+ * This will not change the currently playing media item. + * If index is less than or equal to the current index of the play list, + * the current index of the play list will be incremented correspondingly. + * + * @param index the index you want to add dsd to the play list + * @param item the media item you want to add to the play list + * @throws IndexOutOfBoundsException if index is outside play list range + * @throws NullPointerException if dsd is null + */ + public void addPlaylistItem(int index, MediaItem2 item) { + mProvider.addPlaylistItem_impl(index, item); + } +} diff --git a/android/media/MediaController2Test.java b/android/media/MediaController2Test.java new file mode 100644 index 00000000..ae67a952 --- /dev/null +++ b/android/media/MediaController2Test.java @@ -0,0 +1,487 @@ +/* + * Copyright 2018 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.media.MediaPlayerBase.PlaybackListener; +import android.media.MediaSession2.ControllerInfo; +import android.media.MediaSession2.SessionCallback; +import android.media.TestUtils.SyncHandler; +import android.media.session.PlaybackState; +import android.os.Handler; +import android.os.HandlerThread; +import android.os.Process; +import android.support.test.filters.FlakyTest; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import static android.media.TestUtils.createPlaybackState; +import static org.junit.Assert.*; + +/** + * Tests {@link MediaController2}. + */ +// TODO(jaewan): Implement host-side test so controller and session can run in different processes. +// TODO(jaewan): Fix flaky failure -- see MediaController2Impl.getController() +@RunWith(AndroidJUnit4.class) +@SmallTest +@FlakyTest +public class MediaController2Test extends MediaSession2TestBase { + private static final String TAG = "MediaController2Test"; + + MediaSession2 mSession; + MediaController2 mController; + MockPlayer mPlayer; + + @Before + @Override + public void setUp() throws Exception { + super.setUp(); + // Create this test specific MediaSession2 to use our own Handler. + sHandler.postAndSync(()->{ + mPlayer = new MockPlayer(1); + mSession = new MediaSession2.Builder(mContext, mPlayer).setId(TAG).build(); + }); + + mController = createController(mSession.getToken()); + TestServiceRegistry.getInstance().setHandler(sHandler); + } + + @After + @Override + public void cleanUp() throws Exception { + super.cleanUp(); + sHandler.postAndSync(() -> { + if (mSession != null) { + mSession.close(); + } + }); + TestServiceRegistry.getInstance().cleanUp(); + } + + @Test + public void testPlay() throws InterruptedException { + mController.play(); + try { + assertTrue(mPlayer.mCountDownLatch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS)); + } catch (InterruptedException e) { + fail(e.getMessage()); + } + assertTrue(mPlayer.mPlayCalled); + } + + @Test + public void testPause() throws InterruptedException { + mController.pause(); + try { + assertTrue(mPlayer.mCountDownLatch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS)); + } catch (InterruptedException e) { + fail(e.getMessage()); + } + assertTrue(mPlayer.mPauseCalled); + } + + + @Test + public void testSkipToPrevious() throws InterruptedException { + mController.skipToPrevious(); + try { + assertTrue(mPlayer.mCountDownLatch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS)); + } catch (InterruptedException e) { + fail(e.getMessage()); + } + assertTrue(mPlayer.mSkipToPreviousCalled); + } + + @Test + public void testSkipToNext() throws InterruptedException { + mController.skipToNext(); + try { + assertTrue(mPlayer.mCountDownLatch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS)); + } catch (InterruptedException e) { + fail(e.getMessage()); + } + assertTrue(mPlayer.mSkipToNextCalled); + } + + @Test + public void testStop() throws InterruptedException { + mController.stop(); + try { + assertTrue(mPlayer.mCountDownLatch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS)); + } catch (InterruptedException e) { + fail(e.getMessage()); + } + assertTrue(mPlayer.mStopCalled); + } + + @Test + public void testGetPackageName() { + assertEquals(mContext.getPackageName(), mController.getSessionToken().getPackageName()); + } + + @Test + public void testGetPlaybackState() throws InterruptedException { + // TODO(jaewan): add equivalent test later + /* + final CountDownLatch latch = new CountDownLatch(1); + final MediaPlayerBase.PlaybackListener listener = (state) -> { + assertEquals(PlaybackState.STATE_BUFFERING, state.getState()); + latch.countDown(); + }; + assertNull(mController.getPlaybackState()); + mController.addPlaybackListener(listener, sHandler); + + mPlayer.notifyPlaybackState(createPlaybackState(PlaybackState.STATE_BUFFERING)); + assertTrue(latch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS)); + assertEquals(PlaybackState.STATE_BUFFERING, mController.getPlaybackState().getState()); + */ + } + + // TODO(jaewan): add equivalent test later + /* + @Test + public void testAddPlaybackListener() throws InterruptedException { + final CountDownLatch latch = new CountDownLatch(2); + final MediaPlayerBase.PlaybackListener listener = (state) -> { + switch ((int) latch.getCount()) { + case 2: + assertEquals(PlaybackState.STATE_PLAYING, state.getState()); + break; + case 1: + assertEquals(PlaybackState.STATE_PAUSED, state.getState()); + break; + } + latch.countDown(); + }; + + mController.addPlaybackListener(listener, sHandler); + sHandler.postAndSync(()->{ + mPlayer.notifyPlaybackState(createPlaybackState(PlaybackState.STATE_PLAYING)); + mPlayer.notifyPlaybackState(createPlaybackState(PlaybackState.STATE_PAUSED)); + }); + assertTrue(latch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS)); + } + + @Test + public void testRemovePlaybackListener() throws InterruptedException { + final CountDownLatch latch = new CountDownLatch(1); + final MediaPlayerBase.PlaybackListener listener = (state) -> { + fail(); + latch.countDown(); + }; + mController.addPlaybackListener(listener, sHandler); + mController.removePlaybackListener(listener); + mPlayer.notifyPlaybackState(createPlaybackState(PlaybackState.STATE_PLAYING)); + assertFalse(latch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); + } + */ + + @Test + public void testControllerCallback_onConnected() throws InterruptedException { + // createController() uses controller callback to wait until the controller becomes + // available. + MediaController2 controller = createController(mSession.getToken()); + assertNotNull(controller); + } + + @Test + public void testControllerCallback_sessionRejects() throws InterruptedException { + final MediaSession2.SessionCallback sessionCallback = new SessionCallback() { + @Override + public MediaSession2.CommandGroup onConnect(ControllerInfo controller) { + return null; + } + }; + sHandler.postAndSync(() -> { + mSession.close(); + mSession = new MediaSession2.Builder(mContext, mPlayer) + .setSessionCallback(sHandlerExecutor, sessionCallback).build(); + }); + MediaController2 controller = + createController(mSession.getToken(), false, null); + assertNotNull(controller); + waitForConnect(controller, false); + waitForDisconnect(controller, true); + } + + @Test + public void testControllerCallback_releaseSession() throws InterruptedException { + sHandler.postAndSync(() -> { + mSession.close(); + }); + waitForDisconnect(mController, true); + } + + @Test + public void testControllerCallback_release() throws InterruptedException { + mController.close(); + waitForDisconnect(mController, true); + } + + @Test + public void testIsConnected() throws InterruptedException { + assertTrue(mController.isConnected()); + sHandler.postAndSync(()->{ + mSession.close(); + }); + // postAndSync() to wait until the disconnection is propagated. + sHandler.postAndSync(()->{ + assertFalse(mController.isConnected()); + }); + } + + /** + * Test potential deadlock for calls between controller and session. + */ + @Test + public void testDeadlock() throws InterruptedException { + sHandler.postAndSync(() -> { + mSession.close(); + mSession = null; + }); + + // Two more threads are needed not to block test thread nor test wide thread (sHandler). + final HandlerThread sessionThread = new HandlerThread("testDeadlock_session"); + final HandlerThread testThread = new HandlerThread("testDeadlock_test"); + sessionThread.start(); + testThread.start(); + final SyncHandler sessionHandler = new SyncHandler(sessionThread.getLooper()); + final Handler testHandler = new Handler(testThread.getLooper()); + final CountDownLatch latch = new CountDownLatch(1); + try { + final MockPlayer player = new MockPlayer(0); + sessionHandler.postAndSync(() -> { + mSession = new MediaSession2.Builder(mContext, mPlayer) + .setId("testDeadlock").build(); + }); + final MediaController2 controller = createController(mSession.getToken()); + testHandler.post(() -> { + final PlaybackState2 state = createPlaybackState(PlaybackState.STATE_ERROR); + for (int i = 0; i < 100; i++) { + // triggers call from session to controller. + player.notifyPlaybackState(state); + // triggers call from controller to session. + controller.play(); + + // Repeat above + player.notifyPlaybackState(state); + controller.pause(); + player.notifyPlaybackState(state); + controller.stop(); + player.notifyPlaybackState(state); + controller.skipToNext(); + player.notifyPlaybackState(state); + controller.skipToPrevious(); + } + // This may hang if deadlock happens. + latch.countDown(); + }); + assertTrue(latch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS)); + } finally { + if (mSession != null) { + sessionHandler.postAndSync(() -> { + // Clean up here because sessionHandler will be removed afterwards. + mSession.close(); + mSession = null; + }); + } + if (sessionThread != null) { + sessionThread.quitSafely(); + } + if (testThread != null) { + testThread.quitSafely(); + } + } + } + + @Ignore + @Test + public void testGetServiceToken() { + SessionToken2 token = TestUtils.getServiceToken(mContext, MockMediaSessionService2.ID); + assertNotNull(token); + assertEquals(mContext.getPackageName(), token.getPackageName()); + assertEquals(MockMediaSessionService2.ID, token.getId()); + assertNull(token.getSessionBinder()); + assertEquals(SessionToken2.TYPE_SESSION_SERVICE, token.getType()); + } + + private void connectToService(SessionToken2 token) throws InterruptedException { + mController = createController(token); + mSession = TestServiceRegistry.getInstance().getServiceInstance().getSession(); + mPlayer = (MockPlayer) mSession.getPlayer(); + } + + // TODO(jaewan): Reenable when session manager detects app installs + @Ignore + @Test + public void testConnectToService_sessionService() throws InterruptedException { + connectToService(TestUtils.getServiceToken(mContext, MockMediaSessionService2.ID)); + testConnectToService(); + } + + // TODO(jaewan): Reenable when session manager detects app installs + @Ignore + @Test + public void testConnectToService_libraryService() throws InterruptedException { + connectToService(TestUtils.getServiceToken(mContext, MockMediaLibraryService2.ID)); + testConnectToService(); + } + + public void testConnectToService() throws InterruptedException { + TestServiceRegistry serviceInfo = TestServiceRegistry.getInstance(); + ControllerInfo info = serviceInfo.getOnConnectControllerInfo(); + assertEquals(mContext.getPackageName(), info.getPackageName()); + assertEquals(Process.myUid(), info.getUid()); + assertFalse(info.isTrusted()); + + // Test command from controller to session service + mController.play(); + assertTrue(mPlayer.mCountDownLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); + assertTrue(mPlayer.mPlayCalled); + + // Test command from session service to controller + // TODO(jaewan): Add equivalent tests again + /* + final CountDownLatch latch = new CountDownLatch(1); + mController.addPlaybackListener((state) -> { + assertNotNull(state); + assertEquals(PlaybackState.STATE_REWINDING, state.getState()); + latch.countDown(); + }, sHandler); + mPlayer.notifyPlaybackState( + TestUtils.createPlaybackState(PlaybackState.STATE_REWINDING)); + assertTrue(latch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS)); + */ + } + + @Test + public void testControllerAfterSessionIsGone_session() throws InterruptedException { + testControllerAfterSessionIsGone(mSession.getToken().getId()); + } + + @Ignore + @Test + public void testControllerAfterSessionIsGone_sessionService() throws InterruptedException { + connectToService(TestUtils.getServiceToken(mContext, MockMediaSessionService2.ID)); + testControllerAfterSessionIsGone(MockMediaSessionService2.ID); + } + + @Test + public void testClose_beforeConnected() throws InterruptedException { + MediaController2 controller = + createController(mSession.getToken(), false, null); + controller.close(); + } + + @Test + public void testClose_twice() throws InterruptedException { + mController.close(); + mController.close(); + } + + @Test + public void testClose_session() throws InterruptedException { + final String id = mSession.getToken().getId(); + mController.close(); + // close is done immediately for session. + testNoInteraction(); + + // Test whether the controller is notified about later close of the session or + // re-creation. + testControllerAfterSessionIsGone(id); + } + + // TODO(jaewan): Reenable when session manager detects app installs + @Ignore + @Test + public void testClose_sessionService() throws InterruptedException { + connectToService(TestUtils.getServiceToken(mContext, MockMediaSessionService2.ID)); + testCloseFromService(); + } + + // TODO(jaewan): Reenable when session manager detects app installs + @Ignore + @Test + public void testClose_libraryService() throws InterruptedException { + connectToService(TestUtils.getServiceToken(mContext, MockMediaSessionService2.ID)); + testCloseFromService(); + } + + private void testCloseFromService() throws InterruptedException { + final String id = mController.getSessionToken().getId(); + final CountDownLatch latch = new CountDownLatch(1); + TestServiceRegistry.getInstance().setServiceInstanceChangedCallback((service) -> { + if (service == null) { + // Destroying.. + latch.countDown(); + } + }); + mController.close(); + // Wait until close triggers onDestroy() of the session service. + assertTrue(latch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS)); + assertNull(TestServiceRegistry.getInstance().getServiceInstance()); + testNoInteraction(); + + // Test whether the controller is notified about later close of the session or + // re-creation. + testControllerAfterSessionIsGone(id); + } + + private void testControllerAfterSessionIsGone(final String id) throws InterruptedException { + sHandler.postAndSync(() -> { + // TODO(jaewan): Use Session.close later when we add the API. + mSession.close(); + }); + waitForDisconnect(mController, true); + testNoInteraction(); + + // Test with the newly created session. + sHandler.postAndSync(() -> { + // Recreated session has different session stub, so previously created controller + // shouldn't be available. + mSession = new MediaSession2.Builder(mContext, mPlayer).setId(id).build(); + }); + testNoInteraction(); + } + + private void testNoInteraction() throws InterruptedException { + final CountDownLatch latch = new CountDownLatch(1); + final PlaybackListener playbackListener = (state) -> { + fail("Controller shouldn't be notified about change in session after the close."); + latch.countDown(); + }; + // TODO(jaewan): Add equivalent tests again + /* + mController.addPlaybackListener(playbackListener, sHandler); + mPlayer.notifyPlaybackState(TestUtils.createPlaybackState(PlaybackState.STATE_BUFFERING)); + assertFalse(latch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); + mController.removePlaybackListener(playbackListener); + */ + } + + // TODO(jaewan): Add test for service connect rejection, when we differentiate session + // active/inactive and connection accept/refuse +} diff --git a/android/media/MediaDrm.java b/android/media/MediaDrm.java index e2f9b47e..063186d7 100644 --- a/android/media/MediaDrm.java +++ b/android/media/MediaDrm.java @@ -16,13 +16,6 @@ package android.media; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.ref.WeakReference; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.UUID; import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.Nullable; @@ -33,7 +26,18 @@ import android.os.Handler; import android.os.Looper; import android.os.Message; import android.os.Parcel; +import android.os.PersistableBundle; import android.util.Log; +import dalvik.system.CloseGuard; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.ref.WeakReference; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.atomic.AtomicBoolean; + /** * MediaDrm can be used to obtain keys for decrypting protected media streams, in @@ -117,10 +121,13 @@ import android.util.Log; * MediaDrm objects on a thread with its own Looper running (main UI * thread by default has a Looper running). */ -public final class MediaDrm { +public final class MediaDrm implements AutoCloseable { private static final String TAG = "MediaDrm"; + private final AtomicBoolean mClosed = new AtomicBoolean(); + private final CloseGuard mCloseGuard = CloseGuard.get(); + private static final String PERMISSION = android.Manifest.permission.ACCESS_DRM_CERTIFICATES; private EventHandler mEventHandler; @@ -215,6 +222,8 @@ public final class MediaDrm { */ native_setup(new WeakReference(this), getByteArrayFromUUID(uuid), ActivityThread.currentOpPackageName()); + + mCloseGuard.open("release"); } /** @@ -670,12 +679,14 @@ public final class MediaDrm { private int mRequestType; /** - * Key request type is initial license request + * Key request type is initial license request. A license request + * is necessary to load keys. */ public static final int REQUEST_TYPE_INITIAL = 0; /** - * Key request type is license renewal + * Key request type is license renewal. A license request is + * necessary to prevent the keys from expiring. */ public static final int REQUEST_TYPE_RENEWAL = 1; @@ -684,11 +695,25 @@ public final class MediaDrm { */ public static final int REQUEST_TYPE_RELEASE = 2; + /** + * Keys are already loaded. No license request is necessary, and no + * key request data is returned. + */ + public static final int REQUEST_TYPE_NONE = 3; + + /** + * Keys have been loaded but an additional license request is needed + * to update their values. + */ + public static final int REQUEST_TYPE_UPDATE = 4; + /** @hide */ @IntDef({ REQUEST_TYPE_INITIAL, REQUEST_TYPE_RENEWAL, REQUEST_TYPE_RELEASE, + REQUEST_TYPE_NONE, + REQUEST_TYPE_UPDATE, }) @Retention(RetentionPolicy.SOURCE) public @interface RequestType {} @@ -729,7 +754,8 @@ public final class MediaDrm { /** * Get the type of the request * @return one of {@link #REQUEST_TYPE_INITIAL}, - * {@link #REQUEST_TYPE_RENEWAL} or {@link #REQUEST_TYPE_RELEASE} + * {@link #REQUEST_TYPE_RENEWAL}, {@link #REQUEST_TYPE_RELEASE}, + * {@link #REQUEST_TYPE_NONE} or {@link #REQUEST_TYPE_UPDATE} */ @RequestType public int getRequestType() { return mRequestType; } @@ -954,6 +980,168 @@ public final class MediaDrm { */ public native void releaseAllSecureStops(); + @Retention(RetentionPolicy.SOURCE) + @IntDef({HDCP_LEVEL_UNKNOWN, HDCP_NONE, HDCP_V1, HDCP_V2, + HDCP_V2_1, HDCP_V2_2, HDCP_NO_DIGITAL_OUTPUT}) + public @interface HdcpLevel {} + + + /** + * The DRM plugin did not report an HDCP level, or an error + * occurred accessing it + */ + public static final int HDCP_LEVEL_UNKNOWN = 0; + + /** + * HDCP is not supported on this device, content is unprotected + */ + public static final int HDCP_NONE = 1; + + /** + * HDCP version 1.0 + */ + public static final int HDCP_V1 = 2; + + /** + * HDCP version 2.0 Type 1. + */ + public static final int HDCP_V2 = 3; + + /** + * HDCP version 2.1 Type 1. + */ + public static final int HDCP_V2_1 = 4; + + /** + * HDCP version 2.2 Type 1. + */ + public static final int HDCP_V2_2 = 5; + + /** + * No digital output, implicitly secure + */ + public static final int HDCP_NO_DIGITAL_OUTPUT = Integer.MAX_VALUE; + + /** + * Return the HDCP level negotiated with downstream receivers the + * device is connected to. If multiple HDCP-capable displays are + * simultaneously connected to separate interfaces, this method + * returns the lowest negotiated level of all interfaces. + *

+ * This method should only be used for informational purposes, not for + * enforcing compliance with HDCP requirements. Trusted enforcement of + * HDCP policies must be handled by the DRM system. + *

+ * @return one of {@link #HDCP_LEVEL_UNKNOWN}, {@link #HDCP_NONE}, + * {@link #HDCP_V1}, {@link #HDCP_V2}, {@link #HDCP_V2_1}, {@link #HDCP_V2_2} + * or {@link #HDCP_NO_DIGITAL_OUTPUT}. + */ + @HdcpLevel + public native int getConnectedHdcpLevel(); + + /** + * Return the maximum supported HDCP level. The maximum HDCP level is a + * constant for a given device, it does not depend on downstream receivers + * that may be connected. If multiple HDCP-capable interfaces are present, + * it indicates the highest of the maximum HDCP levels of all interfaces. + *

+ * @return one of {@link #HDCP_LEVEL_UNKNOWN}, {@link #HDCP_NONE}, + * {@link #HDCP_V1}, {@link #HDCP_V2}, {@link #HDCP_V2_1}, {@link #HDCP_V2_2} + * or {@link #HDCP_NO_DIGITAL_OUTPUT}. + */ + @HdcpLevel + public native int getMaxHdcpLevel(); + + /** + * Return the number of MediaDrm sessions that are currently opened + * simultaneously among all MediaDrm instances for the active DRM scheme. + * @return the number of open sessions. + */ + public native int getOpenSessionCount(); + + /** + * Return the maximum number of MediaDrm sessions that may be opened + * simultaneosly among all MediaDrm instances for the active DRM + * scheme. The maximum number of sessions is not affected by any + * sessions that may have already been opened. + * @return maximum sessions. + */ + public native int getMaxSessionCount(); + + /** + * Security level indicates the robustness of the device's DRM + * implementation. + */ + @Retention(RetentionPolicy.SOURCE) + @IntDef({SECURITY_LEVEL_UNKNOWN, SW_SECURE_CRYPTO, SW_SECURE_DECODE, + HW_SECURE_CRYPTO, HW_SECURE_DECODE, HW_SECURE_ALL}) + public @interface SecurityLevel {} + + /** + * The DRM plugin did not report a security level, or an error occurred + * accessing it + */ + public static final int SECURITY_LEVEL_UNKNOWN = 0; + + /** + * Software-based whitebox crypto + */ + public static final int SW_SECURE_CRYPTO = 1; + + /** + * Software-based whitebox crypto and an obfuscated decoder + */ + public static final int SW_SECURE_DECODE = 2; + + /** + * DRM key management and crypto operations are performed within a + * hardware backed trusted execution environment + */ + public static final int HW_SECURE_CRYPTO = 3; + + /** + * DRM key management, crypto operations and decoding of content + * are performed within a hardware backed trusted execution environment + */ + public static final int HW_SECURE_DECODE = 4; + + /** + * DRM key management, crypto operations, decoding of content and all + * handling of the media (compressed and uncompressed) is handled within + * a hardware backed trusted execution environment. + */ + public static final int HW_SECURE_ALL = 5; + + /** + * Return the current security level of a session. A session + * has an initial security level determined by the robustness of + * the DRM system's implementation on the device. The security + * level may be adjusted using {@link #setSecurityLevel}. + * @param sessionId the session to query. + *

+ * @return one of {@link #SECURITY_LEVEL_UNKNOWN}, + * {@link #SW_SECURE_CRYPTO}, {@link #SW_SECURE_DECODE}, + * {@link #HW_SECURE_CRYPTO}, {@link #HW_SECURE_DECODE} or + * {@link #HW_SECURE_ALL}. + */ + @SecurityLevel + public native int getSecurityLevel(@NonNull byte[] sessionId); + + /** + * Set the security level of a session. This can be useful if specific + * attributes of a lower security level are needed by an application, + * such as image manipulation or compositing. Reducing the security + * level will typically limit decryption to lower content resolutions, + * depending on the license policy. + * @param sessionId the session to set the security level on. + * @param level the new security level, one of + * {@link #SW_SECURE_CRYPTO}, {@link #SW_SECURE_DECODE}, + * {@link #HW_SECURE_CRYPTO}, {@link #HW_SECURE_DECODE} or + * {@link #HW_SECURE_ALL}. + */ + public native void setSecurityLevel(@NonNull byte[] sessionId, + @SecurityLevel int level); + /** * String property name: identifies the maker of the DRM plugin */ @@ -1031,7 +1219,6 @@ public final class MediaDrm { public native void setPropertyByteArray(@NonNull @ArrayProperty String propertyName, @NonNull byte[] value); - private static final native void setCipherAlgorithmNative( @NonNull MediaDrm drm, @NonNull byte[] sessionId, @NonNull String algorithm); @@ -1057,6 +1244,25 @@ public final class MediaDrm { @NonNull MediaDrm drm, @NonNull byte[] sessionId, @NonNull byte[] keyId, @NonNull byte[] message, @NonNull byte[] signature); + /** + * Return Metrics data about the current MediaDrm instance. + * + * @return a {@link PersistableBundle} containing the set of attributes and values + * available for this instance of MediaDrm. + * The attributes are described in {@link MetricsConstants}. + * + * Additional vendor-specific fields may also be present in + * the return value. + * + * @hide - not part of the public API at this time + */ + public PersistableBundle getMetrics() { + PersistableBundle bundle = getMetricsNative(); + return bundle; + } + + private native PersistableBundle getMetricsNative(); + /** * In addition to supporting decryption of DASH Common Encrypted Media, the * MediaDrm APIs provide the ability to securely deliver session keys from @@ -1311,20 +1517,81 @@ public final class MediaDrm { } @Override - protected void finalize() { - native_finalize(); + protected void finalize() throws Throwable { + try { + if (mCloseGuard != null) { + mCloseGuard.warnIfOpen(); + } + release(); + } finally { + super.finalize(); + } + } + + /** + * Releases resources associated with the current session of + * MediaDrm. It is considered good practice to call this method when + * the {@link MediaDrm} object is no longer needed in your + * application. After this method is called, {@link MediaDrm} is no + * longer usable since it has lost all of its required resource. + * + * This method was added in API 28. In API versions 18 through 27, release() + * should be called instead. There is no need to do anything for API + * versions prior to 18. + */ + @Override + public void close() { + release(); + } + + /** + * @deprecated replaced by {@link #close()}. + */ + @Deprecated + public void release() { + mCloseGuard.close(); + if (mClosed.compareAndSet(false, true)) { + native_release(); + } } - public native final void release(); + /** @hide */ + public native final void native_release(); + private static native final void native_init(); private native final void native_setup(Object mediadrm_this, byte[] uuid, String appPackageName); - private native final void native_finalize(); - static { System.loadLibrary("media_jni"); native_init(); } + + /** + * Definitions for the metrics that are reported via the + * {@link #getMetrics} call. + * + * @hide - not part of the public API at this time + */ + public final static class MetricsConstants + { + private MetricsConstants() {} + + /** + * Key to extract the number of successful {@link #openSession} calls + * from the {@link PersistableBundle} returned by a + * {@link #getMetrics} call. + */ + public static final String OPEN_SESSION_OK_COUNT + = "/drm/mediadrm/open_session/ok/count"; + + /** + * Key to extract the number of failed {@link #openSession} calls + * from the {@link PersistableBundle} returned by a + * {@link #getMetrics} call. + */ + public static final String OPEN_SESSION_ERROR_COUNT + = "/drm/mediadrm/open_session/error/count"; + } } diff --git a/android/media/MediaFormat.java b/android/media/MediaFormat.java index 306ed83c..e9128e4c 100644 --- a/android/media/MediaFormat.java +++ b/android/media/MediaFormat.java @@ -601,8 +601,6 @@ public final class MediaFormat { * codec specific, but lower values generally result in more efficient * (smaller-sized) encoding. * - * @hide - * * @see MediaCodecInfo.EncoderCapabilities#getQualityRange() */ public static final String KEY_QUALITY = "quality"; @@ -679,6 +677,21 @@ public final class MediaFormat { */ public static final String KEY_LATENCY = "latency"; + /** + * An optional key describing the maximum number of non-display-order coded frames. + * This is an optional parameter that applies only to video encoders. Application should + * check the value for this key in the output format to see if codec will produce + * non-display-order coded frames. If encoder supports it, the output frames' order will be + * different from the display order and each frame's display order could be retrived from + * {@link MediaCodec.BufferInfo#presentationTimeUs}. Before API level 27, application may + * receive non-display-order coded frames even though the application did not request it. + * Note: Application should not rearrange the frames to display order before feeding them + * to {@link MediaMuxer#writeSampleData}. + *

+ * The default value is 0. + */ + public static final String KEY_OUTPUT_REORDER_DEPTH = "output-reorder-depth"; + /** * A key describing the desired clockwise rotation on an output surface. * This key is only used when the codec is configured using an output surface. diff --git a/android/media/MediaItem2.java b/android/media/MediaItem2.java new file mode 100644 index 00000000..96a87d5d --- /dev/null +++ b/android/media/MediaItem2.java @@ -0,0 +1,146 @@ +/* + * Copyright 2018 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.Nullable; +import android.os.Bundle; +import android.text.TextUtils; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * A class with information on a single media item with the metadata information. + * Media item are application dependent so we cannot guarantee that they contain the right values. + *

+ * When it's sent to a controller or browser, it's anonymized and data descriptor wouldn't be sent. + *

+ * This object isn't a thread safe. + * + * @hide + */ +// TODO(jaewan): Unhide and extends from DataSourceDesc. +// Note) Feels like an anti-pattern. We should anonymize MediaItem2 to remove *all* +// information in the DataSourceDesc. Why it should extends from this? +// TODO(jaewan): Move this to updatable +// Previously MediaBrowser.MediaItem +public class MediaItem2 { + private final int mFlags; + private MediaMetadata2 mMetadata; + + /** @hide */ + @Retention(RetentionPolicy.SOURCE) + @IntDef(flag=true, value = { FLAG_BROWSABLE, FLAG_PLAYABLE }) + public @interface Flags { } + + /** + * Flag: Indicates that the item has children of its own. + */ + public static final int FLAG_BROWSABLE = 1 << 0; + + /** + * Flag: Indicates that the item is playable. + *

+ * The id of this item may be passed to + * {@link MediaController2#playFromMediaId(String, Bundle)} + */ + public static final int FLAG_PLAYABLE = 1 << 1; + + /** + * Create a new media item. + * + * @param metadata metadata with the media id. + * @param flags The flags for this item. + */ + public MediaItem2(@NonNull MediaMetadata2 metadata, @Flags int flags) { + mFlags = flags; + setMetadata(metadata); + } + + /** + * Return this object as a bundle to share between processes. + * + * @return a new bundle instance + */ + public Bundle toBundle() { + // TODO(jaewan): Fill here when we rebase. + return new Bundle(); + } + + public String toString() { + final StringBuilder sb = new StringBuilder("MediaItem2{"); + sb.append("mFlags=").append(mFlags); + sb.append(", mMetadata=").append(mMetadata); + sb.append('}'); + return sb.toString(); + } + + /** + * Gets the flags of the item. + */ + public @Flags int getFlags() { + return mFlags; + } + + /** + * Returns whether this item is browsable. + * @see #FLAG_BROWSABLE + */ + public boolean isBrowsable() { + return (mFlags & FLAG_BROWSABLE) != 0; + } + + /** + * Returns whether this item is playable. + * @see #FLAG_PLAYABLE + */ + public boolean isPlayable() { + return (mFlags & FLAG_PLAYABLE) != 0; + } + + /** + * Set a metadata. Metadata shouldn't be null and should have non-empty media id. + * + * @param metadata + */ + public void setMetadata(@NonNull MediaMetadata2 metadata) { + if (metadata == null) { + throw new IllegalArgumentException("metadata cannot be null"); + } + if (TextUtils.isEmpty(metadata.getMediaId())) { + throw new IllegalArgumentException("metadata must have a non-empty media id"); + } + mMetadata = metadata; + } + + /** + * Returns the metadata of the media. + */ + public @NonNull MediaMetadata2 getMetadata() { + return mMetadata; + } + + /** + * Returns the media id in the {@link MediaMetadata2} for this item. + * @see MediaMetadata2#METADATA_KEY_MEDIA_ID + */ + public @Nullable String getMediaId() { + return mMetadata.getMediaId(); + } +} diff --git a/android/media/MediaLibraryService2.java b/android/media/MediaLibraryService2.java new file mode 100644 index 00000000..d7e43ec9 --- /dev/null +++ b/android/media/MediaLibraryService2.java @@ -0,0 +1,350 @@ +/* + * Copyright 2018 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.CallbackExecutor; +import android.annotation.NonNull; +import android.annotation.Nullable; +import android.app.PendingIntent; +import android.content.Context; +import android.media.MediaSession2.BuilderBase; +import android.media.MediaSession2.ControllerInfo; +import android.media.update.ApiLoader; +import android.media.update.MediaLibraryService2Provider.MediaLibrarySessionProvider; +import android.media.update.MediaSession2Provider; +import android.media.update.MediaSessionService2Provider; +import android.os.Bundle; +import android.service.media.MediaBrowserService.BrowserRoot; + +import java.util.List; +import java.util.concurrent.Executor; + +/** + * Base class for media library services. + *

+ * Media library services enable applications to browse media content provided by an application + * and ask the application to start playing it. They may also be used to control content that + * is already playing by way of a {@link MediaSession2}. + *

+ * To extend this class, adding followings directly to your {@code AndroidManifest.xml}. + *

+ * <service android:name="component_name_of_your_implementation" >
+ *   <intent-filter>
+ *     <action android:name="android.media.MediaLibraryService2" />
+ *   </intent-filter>
+ * </service>
+ *

+ * A {@link MediaLibraryService2} is extension of {@link MediaSessionService2}. IDs shouldn't + * be shared between the {@link MediaSessionService2} and {@link MediaSession2}. By + * default, an empty string will be used for ID of the service. If you want to specify an ID, + * declare metadata in the manifest as follows. + * @hide + */ +// TODO(jaewan): Unhide +public abstract class MediaLibraryService2 extends MediaSessionService2 { + /** + * This is the interface name that a service implementing a session service should say that it + * support -- that is, this is the action it uses for its intent filter. + */ + public static final String SERVICE_INTERFACE = "android.media.MediaLibraryService2"; + + /** + * Session for the media library service. + */ + public class MediaLibrarySession extends MediaSession2 { + private final MediaLibrarySessionProvider mProvider; + + MediaLibrarySession(Context context, MediaPlayerBase player, String id, + Executor callbackExecutor, SessionCallback callback, VolumeProvider volumeProvider, + int ratingType, PendingIntent sessionActivity) { + super(context, player, id, callbackExecutor, callback, volumeProvider, ratingType, + sessionActivity); + mProvider = (MediaLibrarySessionProvider) getProvider(); + } + + @Override + MediaSession2Provider createProvider(Context context, MediaPlayerBase player, String id, + Executor callbackExecutor, SessionCallback callback, VolumeProvider volumeProvider, + int ratingType, PendingIntent sessionActivity) { + return ApiLoader.getProvider(context) + .createMediaLibraryService2MediaLibrarySession(this, context, player, id, + callbackExecutor, (MediaLibrarySessionCallback) callback, + volumeProvider, ratingType, sessionActivity); + } + + /** + * Notify subscribed controller about change in a parent's children. + * + * @param controller controller to notify + * @param parentId + * @param options + */ + public void notifyChildrenChanged(@NonNull ControllerInfo controller, + @NonNull String parentId, @NonNull Bundle options) { + mProvider.notifyChildrenChanged_impl(controller, parentId, options); + } + + /** + * Notify subscribed controller about change in a parent's children. + * + * @param parentId parent id + * @param options optional bundle + */ + // This is for the backward compatibility. + public void notifyChildrenChanged(@NonNull String parentId, @Nullable Bundle options) { + mProvider.notifyChildrenChanged_impl(parentId, options); + } + } + + public static class MediaLibrarySessionCallback extends MediaSession2.SessionCallback { + /** + * Called to get the root information for browsing by a particular client. + *

+ * The implementation should verify that the client package has permission + * to access browse media information before returning the root id; it + * should return null if the client is not allowed to access this + * information. + * + * @param controllerInfo information of the controller requesting access to browse media. + * @param rootHints An optional bundle of service-specific arguments to send + * to the media browser service when connecting and retrieving the + * root id for browsing, or null if none. The contents of this + * bundle may affect the information returned when browsing. + * @return The {@link BrowserRoot} for accessing this app's content or null. + * @see BrowserRoot#EXTRA_RECENT + * @see BrowserRoot#EXTRA_OFFLINE + * @see BrowserRoot#EXTRA_SUGGESTED + */ + public @Nullable BrowserRoot onGetRoot(@NonNull ControllerInfo controllerInfo, + @Nullable Bundle rootHints) { + return null; + } + + /** + * Called to get the search result. Return search result here for the browser. + *

+ * Return an empty list for no search result, and return {@code null} for the error. + * + * @param query The search query sent from the media browser. It contains keywords separated + * by space. + * @param extras The bundle of service-specific arguments sent from the media browser. + * @return search result. {@code null} for error. + */ + public @Nullable List onSearch(@NonNull ControllerInfo controllerInfo, + @NonNull String query, @Nullable Bundle extras) { + return null; + } + + /** + * Called to get the search result . Return result here for the browser. + *

+ * Return an empty list for no search result, and return {@code null} for the error. + * + * @param itemId item id to get media item. + * @return media item2. {@code null} for error. + */ + public @Nullable MediaItem2 onLoadItem(@NonNull ControllerInfo controllerInfo, + @NonNull String itemId) { + return null; + } + + /** + * Called to get the search result. Return search result here for the browser. + *

+ * Return an empty list for no search result, and return {@code null} for the error. + * + * @param parentId parent id to get children + * @param page number of page + * @param pageSize size of the page + * @param options + * @return list of children. Can be {@code null}. + */ + public @Nullable List onLoadChildren(@NonNull ControllerInfo controller, + @NonNull String parentId, int page, int pageSize, @Nullable Bundle options) { + return null; + } + + /** + * Called when a controller subscribes to the parent. + * + * @param controller controller + * @param parentId parent id + * @param options optional bundle + */ + public void onSubscribed(@NonNull ControllerInfo controller, + String parentId, @Nullable Bundle options) { + } + + /** + * Called when a controller unsubscribes to the parent. + * + * @param controller controller + * @param parentId parent id + * @param options optional bundle + */ + public void onUnsubscribed(@NonNull ControllerInfo controller, + String parentId, @Nullable Bundle options) { + } + } + + /** + * Builder for {@link MediaLibrarySession}. + */ + // TODO(jaewan): Move this to updatable. + public class MediaLibrarySessionBuilder + extends BuilderBase { + public MediaLibrarySessionBuilder( + @NonNull Context context, @NonNull MediaPlayerBase player, + @NonNull @CallbackExecutor Executor callbackExecutor, + @NonNull MediaLibrarySessionCallback callback) { + super(context, player); + setSessionCallback(callbackExecutor, callback); + } + + @Override + public MediaLibrarySessionBuilder setSessionCallback( + @NonNull @CallbackExecutor Executor callbackExecutor, + @NonNull MediaLibrarySessionCallback callback) { + if (callback == null) { + throw new IllegalArgumentException("MediaLibrarySessionCallback cannot be null"); + } + return super.setSessionCallback(callbackExecutor, callback); + } + + @Override + public MediaLibrarySession build() throws IllegalStateException { + return new MediaLibrarySession(mContext, mPlayer, mId, mCallbackExecutor, mCallback, + mVolumeProvider, mRatingType, mSessionActivity); + } + } + + @Override + MediaSessionService2Provider createProvider() { + return ApiLoader.getProvider(this).createMediaLibraryService2(this); + } + + /** + * Called when another app requested to start this service. + *

+ * Library service will accept or reject the connection with the + * {@link MediaLibrarySessionCallback} in the created session. + *

+ * Service wouldn't run if {@code null} is returned or session's ID doesn't match with the + * expected ID that you've specified through the AndroidManifest.xml. + *

+ * This method will be called on the main thread. + * + * @param sessionId session id written in the AndroidManifest.xml. + * @return a new browser session + * @see MediaLibrarySessionBuilder + * @see #getSession() + * @throws RuntimeException if returned session is invalid + */ + @Override + public @NonNull abstract MediaLibrarySession onCreateSession(String sessionId); + + /** + * Contains information that the browser service needs to send to the client + * when first connected. + */ + public static final class BrowserRoot { + /** + * The lookup key for a boolean that indicates whether the browser service should return a + * browser root for recently played media items. + * + *

When creating a media browser for a given media browser service, this key can be + * supplied as a root hint for retrieving media items that are recently played. + * If the media browser service can provide such media items, the implementation must return + * the key in the root hint when + * {@link MediaLibrarySessionCallback#onGetRoot(ControllerInfo, Bundle)} is called back. + * + *

The root hint may contain multiple keys. + * + * @see #EXTRA_OFFLINE + * @see #EXTRA_SUGGESTED + */ + public static final String EXTRA_RECENT = "android.service.media.extra.RECENT"; + + /** + * The lookup key for a boolean that indicates whether the browser service should return a + * browser root for offline media items. + * + *

When creating a media browser for a given media browser service, this key can be + * supplied as a root hint for retrieving media items that are can be played without an + * internet connection. + * If the media browser service can provide such media items, the implementation must return + * the key in the root hint when + * {@link MediaLibrarySessionCallback#onGetRoot(ControllerInfo, Bundle)} is called back. + * + *

The root hint may contain multiple keys. + * + * @see #EXTRA_RECENT + * @see #EXTRA_SUGGESTED + */ + public static final String EXTRA_OFFLINE = "android.service.media.extra.OFFLINE"; + + /** + * The lookup key for a boolean that indicates whether the browser service should return a + * browser root for suggested media items. + * + *

When creating a media browser for a given media browser service, this key can be + * supplied as a root hint for retrieving the media items suggested by the media browser + * service. The list of media items passed in {@link android.media.browse.MediaBrowser.SubscriptionCallback#onChildrenLoaded(String, List)} + * is considered ordered by relevance, first being the top suggestion. + * If the media browser service can provide such media items, the implementation must return + * the key in the root hint when + * {@link MediaLibrarySessionCallback#onGetRoot(ControllerInfo, Bundle)} is called back. + * + *

The root hint may contain multiple keys. + * + * @see #EXTRA_RECENT + * @see #EXTRA_OFFLINE + */ + public static final String EXTRA_SUGGESTED = "android.service.media.extra.SUGGESTED"; + + final private String mRootId; + final private Bundle mExtras; + + /** + * Constructs a browser root. + * @param rootId The root id for browsing. + * @param extras Any extras about the browser service. + */ + public BrowserRoot(@NonNull String rootId, @Nullable Bundle extras) { + if (rootId == null) { + throw new IllegalArgumentException("The root id in BrowserRoot cannot be null. " + + "Use null for BrowserRoot instead."); + } + mRootId = rootId; + mExtras = extras; + } + + /** + * Gets the root id for browsing. + */ + public String getRootId() { + return mRootId; + } + + /** + * Gets any extras about the browser service. + */ + public Bundle getExtras() { + return mExtras; + } + } +} diff --git a/android/media/MediaMetadata2.java b/android/media/MediaMetadata2.java new file mode 100644 index 00000000..0e24db65 --- /dev/null +++ b/android/media/MediaMetadata2.java @@ -0,0 +1,815 @@ +/* + * Copyright 2018 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.Nullable; +import android.annotation.StringDef; +import android.graphics.Bitmap; +import android.net.Uri; +import android.os.Build; +import android.os.Bundle; +import android.os.Parcel; +import android.os.Parcelable; +import android.text.TextUtils; +import android.util.Log; +import android.util.ArrayMap; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.util.Set; + +/** + * Contains metadata about an item, such as the title, artist, etc. + * @hide + */ +// TODO(jaewan): Move this to updatable +public final class MediaMetadata2 { + private static final String TAG = "MediaMetadata2"; + + /** + * The title of the media. + */ + public static final String METADATA_KEY_TITLE = "android.media.metadata.TITLE"; + + /** + * The artist of the media. + */ + public static final String METADATA_KEY_ARTIST = "android.media.metadata.ARTIST"; + + /** + * The duration of the media in ms. A negative duration indicates that the + * duration is unknown (or infinite). + */ + public static final String METADATA_KEY_DURATION = "android.media.metadata.DURATION"; + + /** + * The album title for the media. + */ + public static final String METADATA_KEY_ALBUM = "android.media.metadata.ALBUM"; + + /** + * The author of the media. + */ + public static final String METADATA_KEY_AUTHOR = "android.media.metadata.AUTHOR"; + + /** + * The writer of the media. + */ + public static final String METADATA_KEY_WRITER = "android.media.metadata.WRITER"; + + /** + * The composer of the media. + */ + public static final String METADATA_KEY_COMPOSER = "android.media.metadata.COMPOSER"; + + /** + * The compilation status of the media. + */ + public static final String METADATA_KEY_COMPILATION = "android.media.metadata.COMPILATION"; + + /** + * The date the media was created or published. The format is unspecified + * but RFC 3339 is recommended. + */ + public static final String METADATA_KEY_DATE = "android.media.metadata.DATE"; + + /** + * The year the media was created or published as a long. + */ + public static final String METADATA_KEY_YEAR = "android.media.metadata.YEAR"; + + /** + * The genre of the media. + */ + public static final String METADATA_KEY_GENRE = "android.media.metadata.GENRE"; + + /** + * The track number for the media. + */ + public static final String METADATA_KEY_TRACK_NUMBER = "android.media.metadata.TRACK_NUMBER"; + + /** + * The number of tracks in the media's original source. + */ + public static final String METADATA_KEY_NUM_TRACKS = "android.media.metadata.NUM_TRACKS"; + + /** + * The disc number for the media's original source. + */ + public static final String METADATA_KEY_DISC_NUMBER = "android.media.metadata.DISC_NUMBER"; + + /** + * The artist for the album of the media's original source. + */ + public static final String METADATA_KEY_ALBUM_ARTIST = "android.media.metadata.ALBUM_ARTIST"; + + /** + * The artwork for the media as a {@link Bitmap}. + * + * The artwork should be relatively small and may be scaled down + * if it is too large. For higher resolution artwork + * {@link #METADATA_KEY_ART_URI} should be used instead. + */ + public static final String METADATA_KEY_ART = "android.media.metadata.ART"; + + /** + * The artwork for the media as a Uri style String. + */ + public static final String METADATA_KEY_ART_URI = "android.media.metadata.ART_URI"; + + /** + * The artwork for the album of the media's original source as a + * {@link Bitmap}. + * The artwork should be relatively small and may be scaled down + * if it is too large. For higher resolution artwork + * {@link #METADATA_KEY_ALBUM_ART_URI} should be used instead. + */ + public static final String METADATA_KEY_ALBUM_ART = "android.media.metadata.ALBUM_ART"; + + /** + * The artwork for the album of the media's original source as a Uri style + * String. + */ + public static final String METADATA_KEY_ALBUM_ART_URI = "android.media.metadata.ALBUM_ART_URI"; + + /** + * The user's rating for the media. + * + * @see Rating + */ + public static final String METADATA_KEY_USER_RATING = "android.media.metadata.USER_RATING"; + + /** + * The overall rating for the media. + * + * @see Rating + */ + public static final String METADATA_KEY_RATING = "android.media.metadata.RATING"; + + /** + * A title that is suitable for display to the user. This will generally be + * the same as {@link #METADATA_KEY_TITLE} but may differ for some formats. + * When displaying media described by this metadata this should be preferred + * if present. + */ + public static final String METADATA_KEY_DISPLAY_TITLE = "android.media.metadata.DISPLAY_TITLE"; + + /** + * A subtitle that is suitable for display to the user. When displaying a + * second line for media described by this metadata this should be preferred + * to other fields if present. + */ + public static final String METADATA_KEY_DISPLAY_SUBTITLE + = "android.media.metadata.DISPLAY_SUBTITLE"; + + /** + * A description that is suitable for display to the user. When displaying + * more information for media described by this metadata this should be + * preferred to other fields if present. + */ + public static final String METADATA_KEY_DISPLAY_DESCRIPTION + = "android.media.metadata.DISPLAY_DESCRIPTION"; + + /** + * An icon or thumbnail that is suitable for display to the user. When + * displaying an icon for media described by this metadata this should be + * preferred to other fields if present. This must be a {@link Bitmap}. + * + * The icon should be relatively small and may be scaled down + * if it is too large. For higher resolution artwork + * {@link #METADATA_KEY_DISPLAY_ICON_URI} should be used instead. + */ + public static final String METADATA_KEY_DISPLAY_ICON + = "android.media.metadata.DISPLAY_ICON"; + + /** + * An icon or thumbnail that is suitable for display to the user. When + * displaying more information for media described by this metadata the + * display description should be preferred to other fields when present. + * This must be a Uri style String. + */ + public static final String METADATA_KEY_DISPLAY_ICON_URI + = "android.media.metadata.DISPLAY_ICON_URI"; + + /** + * A String key for identifying the content. This value is specific to the + * service providing the content. If used, this should be a persistent + * unique key for the underlying content. It may be used with + * {@link MediaController2#playFromMediaId(String, Bundle)} + * to initiate playback when provided by a {@link MediaBrowser2} connected to + * the same app. + */ + public static final String METADATA_KEY_MEDIA_ID = "android.media.metadata.MEDIA_ID"; + + /** + * A Uri formatted String representing the content. This value is specific to the + * service providing the content. It may be used with + * {@link MediaController2#playFromUri(Uri, Bundle)} + * to initiate playback when provided by a {@link MediaBrowser2} connected to + * the same app. + */ + public static final String METADATA_KEY_MEDIA_URI = "android.media.metadata.MEDIA_URI"; + + /** + * The bluetooth folder type of the media specified in the section 6.10.2.2 of the Bluetooth + * AVRCP 1.5. It should be one of the following: + *

    + *
  • {@link #BT_FOLDER_TYPE_MIXED}
  • + *
  • {@link #BT_FOLDER_TYPE_TITLES}
  • + *
  • {@link #BT_FOLDER_TYPE_ALBUMS}
  • + *
  • {@link #BT_FOLDER_TYPE_ARTISTS}
  • + *
  • {@link #BT_FOLDER_TYPE_GENRES}
  • + *
  • {@link #BT_FOLDER_TYPE_PLAYLISTS}
  • + *
  • {@link #BT_FOLDER_TYPE_YEARS}
  • + *
+ */ + public static final String METADATA_KEY_BT_FOLDER_TYPE + = "android.media.metadata.BT_FOLDER_TYPE"; + + /** + * The type of folder that is unknown or contains media elements of mixed types as specified in + * the section 6.10.2.2 of the Bluetooth AVRCP 1.5. + */ + public static final long BT_FOLDER_TYPE_MIXED = 0; + + /** + * The type of folder that contains media elements only as specified in the section 6.10.2.2 of + * the Bluetooth AVRCP 1.5. + */ + public static final long BT_FOLDER_TYPE_TITLES = 1; + + /** + * The type of folder that contains folders categorized by album as specified in the section + * 6.10.2.2 of the Bluetooth AVRCP 1.5. + */ + public static final long BT_FOLDER_TYPE_ALBUMS = 2; + + /** + * The type of folder that contains folders categorized by artist as specified in the section + * 6.10.2.2 of the Bluetooth AVRCP 1.5. + */ + public static final long BT_FOLDER_TYPE_ARTISTS = 3; + + /** + * The type of folder that contains folders categorized by genre as specified in the section + * 6.10.2.2 of the Bluetooth AVRCP 1.5. + */ + public static final long BT_FOLDER_TYPE_GENRES = 4; + + /** + * The type of folder that contains folders categorized by playlist as specified in the section + * 6.10.2.2 of the Bluetooth AVRCP 1.5. + */ + public static final long BT_FOLDER_TYPE_PLAYLISTS = 5; + + /** + * The type of folder that contains folders categorized by year as specified in the section + * 6.10.2.2 of the Bluetooth AVRCP 1.5. + */ + public static final long BT_FOLDER_TYPE_YEARS = 6; + + /** + * Whether the media is an advertisement. A value of 0 indicates it is not an advertisement. A + * value of 1 or non-zero indicates it is an advertisement. If not specified, this value is set + * to 0 by default. + */ + public static final String METADATA_KEY_ADVERTISEMENT = "android.media.metadata.ADVERTISEMENT"; + + /** + * The download status of the media which will be used for later offline playback. It should be + * one of the following: + * + *
    + *
  • {@link #STATUS_NOT_DOWNLOADED}
  • + *
  • {@link #STATUS_DOWNLOADING}
  • + *
  • {@link #STATUS_DOWNLOADED}
  • + *
+ */ + public static final String METADATA_KEY_DOWNLOAD_STATUS = + "android.media.metadata.DOWNLOAD_STATUS"; + + /** + * The status value to indicate the media item is not downloaded. + * + * @see #METADATA_KEY_DOWNLOAD_STATUS + */ + public static final long STATUS_NOT_DOWNLOADED = 0; + + /** + * The status value to indicate the media item is being downloaded. + * + * @see #METADATA_KEY_DOWNLOAD_STATUS + */ + public static final long STATUS_DOWNLOADING = 1; + + /** + * The status value to indicate the media item is downloaded for later offline playback. + * + * @see #METADATA_KEY_DOWNLOAD_STATUS + */ + public static final long STATUS_DOWNLOADED = 2; + + /** + * A {@link Bundle} extra. + * @hide + */ + public static final String METADATA_KEY_EXTRA = "android.media.metadata.EXTRA"; + + /** + * @hide + */ + @StringDef({METADATA_KEY_TITLE, METADATA_KEY_ARTIST, METADATA_KEY_ALBUM, METADATA_KEY_AUTHOR, + METADATA_KEY_WRITER, METADATA_KEY_COMPOSER, METADATA_KEY_COMPILATION, + METADATA_KEY_DATE, METADATA_KEY_GENRE, METADATA_KEY_ALBUM_ARTIST, METADATA_KEY_ART_URI, + METADATA_KEY_ALBUM_ART_URI, METADATA_KEY_DISPLAY_TITLE, METADATA_KEY_DISPLAY_SUBTITLE, + METADATA_KEY_DISPLAY_DESCRIPTION, METADATA_KEY_DISPLAY_ICON_URI, + METADATA_KEY_MEDIA_ID, METADATA_KEY_MEDIA_URI}) + @Retention(RetentionPolicy.SOURCE) + public @interface TextKey {} + + /** + * @hide + */ + @StringDef({METADATA_KEY_DURATION, METADATA_KEY_YEAR, METADATA_KEY_TRACK_NUMBER, + METADATA_KEY_NUM_TRACKS, METADATA_KEY_DISC_NUMBER, METADATA_KEY_BT_FOLDER_TYPE, + METADATA_KEY_ADVERTISEMENT, METADATA_KEY_DOWNLOAD_STATUS}) + @Retention(RetentionPolicy.SOURCE) + public @interface LongKey {} + + /** + * @hide + */ + @StringDef({METADATA_KEY_ART, METADATA_KEY_ALBUM_ART, METADATA_KEY_DISPLAY_ICON}) + @Retention(RetentionPolicy.SOURCE) + public @interface BitmapKey {} + + /** + * @hide + */ + @StringDef({METADATA_KEY_USER_RATING, METADATA_KEY_RATING}) + @Retention(RetentionPolicy.SOURCE) + public @interface RatingKey {} + + static final int METADATA_TYPE_LONG = 0; + static final int METADATA_TYPE_TEXT = 1; + static final int METADATA_TYPE_BITMAP = 2; + static final int METADATA_TYPE_RATING = 3; + static final ArrayMap METADATA_KEYS_TYPE; + + static { + METADATA_KEYS_TYPE = new ArrayMap(); + METADATA_KEYS_TYPE.put(METADATA_KEY_TITLE, METADATA_TYPE_TEXT); + METADATA_KEYS_TYPE.put(METADATA_KEY_ARTIST, METADATA_TYPE_TEXT); + METADATA_KEYS_TYPE.put(METADATA_KEY_DURATION, METADATA_TYPE_LONG); + METADATA_KEYS_TYPE.put(METADATA_KEY_ALBUM, METADATA_TYPE_TEXT); + METADATA_KEYS_TYPE.put(METADATA_KEY_AUTHOR, METADATA_TYPE_TEXT); + METADATA_KEYS_TYPE.put(METADATA_KEY_WRITER, METADATA_TYPE_TEXT); + METADATA_KEYS_TYPE.put(METADATA_KEY_COMPOSER, METADATA_TYPE_TEXT); + METADATA_KEYS_TYPE.put(METADATA_KEY_COMPILATION, METADATA_TYPE_TEXT); + METADATA_KEYS_TYPE.put(METADATA_KEY_DATE, METADATA_TYPE_TEXT); + METADATA_KEYS_TYPE.put(METADATA_KEY_YEAR, METADATA_TYPE_LONG); + METADATA_KEYS_TYPE.put(METADATA_KEY_GENRE, METADATA_TYPE_TEXT); + METADATA_KEYS_TYPE.put(METADATA_KEY_TRACK_NUMBER, METADATA_TYPE_LONG); + METADATA_KEYS_TYPE.put(METADATA_KEY_NUM_TRACKS, METADATA_TYPE_LONG); + METADATA_KEYS_TYPE.put(METADATA_KEY_DISC_NUMBER, METADATA_TYPE_LONG); + METADATA_KEYS_TYPE.put(METADATA_KEY_ALBUM_ARTIST, METADATA_TYPE_TEXT); + METADATA_KEYS_TYPE.put(METADATA_KEY_ART, METADATA_TYPE_BITMAP); + METADATA_KEYS_TYPE.put(METADATA_KEY_ART_URI, METADATA_TYPE_TEXT); + METADATA_KEYS_TYPE.put(METADATA_KEY_ALBUM_ART, METADATA_TYPE_BITMAP); + METADATA_KEYS_TYPE.put(METADATA_KEY_ALBUM_ART_URI, METADATA_TYPE_TEXT); + METADATA_KEYS_TYPE.put(METADATA_KEY_USER_RATING, METADATA_TYPE_RATING); + METADATA_KEYS_TYPE.put(METADATA_KEY_RATING, METADATA_TYPE_RATING); + METADATA_KEYS_TYPE.put(METADATA_KEY_DISPLAY_TITLE, METADATA_TYPE_TEXT); + METADATA_KEYS_TYPE.put(METADATA_KEY_DISPLAY_SUBTITLE, METADATA_TYPE_TEXT); + METADATA_KEYS_TYPE.put(METADATA_KEY_DISPLAY_DESCRIPTION, METADATA_TYPE_TEXT); + METADATA_KEYS_TYPE.put(METADATA_KEY_DISPLAY_ICON, METADATA_TYPE_BITMAP); + METADATA_KEYS_TYPE.put(METADATA_KEY_DISPLAY_ICON_URI, METADATA_TYPE_TEXT); + METADATA_KEYS_TYPE.put(METADATA_KEY_MEDIA_ID, METADATA_TYPE_TEXT); + METADATA_KEYS_TYPE.put(METADATA_KEY_BT_FOLDER_TYPE, METADATA_TYPE_LONG); + METADATA_KEYS_TYPE.put(METADATA_KEY_MEDIA_URI, METADATA_TYPE_TEXT); + METADATA_KEYS_TYPE.put(METADATA_KEY_ADVERTISEMENT, METADATA_TYPE_LONG); + METADATA_KEYS_TYPE.put(METADATA_KEY_DOWNLOAD_STATUS, METADATA_TYPE_LONG); + } + + private static final @TextKey String[] PREFERRED_DESCRIPTION_ORDER = { + METADATA_KEY_TITLE, + METADATA_KEY_ARTIST, + METADATA_KEY_ALBUM, + METADATA_KEY_ALBUM_ARTIST, + METADATA_KEY_WRITER, + METADATA_KEY_AUTHOR, + METADATA_KEY_COMPOSER + }; + + private static final @BitmapKey String[] PREFERRED_BITMAP_ORDER = { + METADATA_KEY_DISPLAY_ICON, + METADATA_KEY_ART, + METADATA_KEY_ALBUM_ART + }; + + private static final @TextKey String[] PREFERRED_URI_ORDER = { + METADATA_KEY_DISPLAY_ICON_URI, + METADATA_KEY_ART_URI, + METADATA_KEY_ALBUM_ART_URI + }; + + final Bundle mBundle; + + /** + * @hide + */ + public MediaMetadata2(Bundle bundle) { + mBundle = new Bundle(bundle); + } + + /** + * Returns true if the given key is contained in the metadata + * + * @param key a String key + * @return true if the key exists in this metadata, false otherwise + */ + public boolean containsKey(String key) { + return mBundle.containsKey(key); + } + + /** + * Returns the value associated with the given key, or null if no mapping of + * the desired type exists for the given key or a null value is explicitly + * associated with the key. + * + * @param key The key the value is stored under + * @return a CharSequence value, or null + */ + public CharSequence getText(@TextKey String key) { + return mBundle.getCharSequence(key); + } + + /** + * Returns the value associated with the given key, or null if no mapping of + * the desired type exists for the given key or a null value is explicitly + * associated with the key. + * + * @ + * @return media id. Can be {@code null} + */ + public @Nullable String getMediaId() { + return getString(METADATA_KEY_MEDIA_ID); + } + + /** + * Returns the value associated with the given key, or null if no mapping of + * the desired type exists for the given key or a null value is explicitly + * associated with the key. + * + * @param key The key the value is stored under + * @return a String value, or null + */ + public String getString(@TextKey String key) { + CharSequence text = mBundle.getCharSequence(key); + if (text != null) { + return text.toString(); + } + return null; + } + + /** + * Returns the value associated with the given key, or 0L if no long exists + * for the given key. + * + * @param key The key the value is stored under + * @return a long value + */ + public long getLong(@LongKey String key) { + return mBundle.getLong(key, 0); + } + + /** + * Return a {@link Rating2} for the given key or null if no rating exists for + * the given key. + * + * @param key The key the value is stored under + * @return A {@link Rating2} or null + */ + public Rating2 getRating(@RatingKey String key) { + // TODO(jaewan): Add backward compatibility + Rating2 rating = null; + try { + rating = Rating2.fromBundle(mBundle.getBundle(key)); + } catch (Exception e) { + // ignore, value was not a rating + Log.w(TAG, "Failed to retrieve a key as Rating.", e); + } + return rating; + } + + /** + * Return a {@link Bitmap} for the given key or null if no bitmap exists for + * the given key. + * + * @param key The key the value is stored under + * @return A {@link Bitmap} or null + */ + public Bitmap getBitmap(@BitmapKey String key) { + Bitmap bmp = null; + try { + bmp = mBundle.getParcelable(key); + } catch (Exception e) { + // ignore, value was not a bitmap + Log.w(TAG, "Failed to retrieve a key as Bitmap.", e); + } + return bmp; + } + + /** + * Get the extra {@link Bundle} from the metadata object. + * + * @return A {@link Bundle} or {@code null} + */ + public Bundle getExtra() { + try { + return mBundle.getBundle(METADATA_KEY_EXTRA); + } catch (Exception e) { + // ignore, value was not an bundle + Log.w(TAG, "Failed to retrieve an extra"); + } + return null; + } + + /** + * Get the number of fields in this metadata. + * + * @return The number of fields in the metadata. + */ + public int size() { + return mBundle.size(); + } + + /** + * Returns a Set containing the Strings used as keys in this metadata. + * + * @return a Set of String keys + */ + public Set keySet() { + return mBundle.keySet(); + } + + /** + * Gets the bundle backing the metadata object. This is available to support + * backwards compatibility. Apps should not modify the bundle directly. + * + * @return The Bundle backing this metadata. + */ + public Bundle getBundle() { + return mBundle; + } + + /** + * Use to build MediaMetadata2 objects. The system defined metadata keys must + * use the appropriate data type. + */ + public static final class Builder { + private final Bundle mBundle; + + /** + * Create an empty Builder. Any field that should be included in the + * {@link MediaMetadata2} must be added. + */ + public Builder() { + mBundle = new Bundle(); + } + + /** + * Create a Builder using a {@link MediaMetadata2} instance to set the + * initial values. All fields in the source metadata will be included in + * the new metadata. Fields can be overwritten by adding the same key. + * + * @param source + */ + public Builder(MediaMetadata2 source) { + mBundle = new Bundle(source.mBundle); + } + + /** + * Create a Builder using a {@link MediaMetadata2} instance to set + * initial values, but replace bitmaps with a scaled down copy if they + * are larger than maxBitmapSize. + * + * @param source The original metadata to copy. + * @param maxBitmapSize The maximum height/width for bitmaps contained + * in the metadata. + * @hide + */ + public Builder(MediaMetadata2 source, int maxBitmapSize) { + this(source); + for (String key : mBundle.keySet()) { + Object value = mBundle.get(key); + if (value instanceof Bitmap) { + Bitmap bmp = (Bitmap) value; + if (bmp.getHeight() > maxBitmapSize || bmp.getWidth() > maxBitmapSize) { + putBitmap(key, scaleBitmap(bmp, maxBitmapSize)); + } + } + } + } + + /** + * Put a CharSequence value into the metadata. Custom keys may be used, + * but if the METADATA_KEYs defined in this class are used they may only + * be one of the following: + *
    + *
  • {@link #METADATA_KEY_TITLE}
  • + *
  • {@link #METADATA_KEY_ARTIST}
  • + *
  • {@link #METADATA_KEY_ALBUM}
  • + *
  • {@link #METADATA_KEY_AUTHOR}
  • + *
  • {@link #METADATA_KEY_WRITER}
  • + *
  • {@link #METADATA_KEY_COMPOSER}
  • + *
  • {@link #METADATA_KEY_DATE}
  • + *
  • {@link #METADATA_KEY_GENRE}
  • + *
  • {@link #METADATA_KEY_ALBUM_ARTIST}
  • + *
  • {@link #METADATA_KEY_ART_URI}
  • + *
  • {@link #METADATA_KEY_ALBUM_ART_URI}
  • + *
  • {@link #METADATA_KEY_DISPLAY_TITLE}
  • + *
  • {@link #METADATA_KEY_DISPLAY_SUBTITLE}
  • + *
  • {@link #METADATA_KEY_DISPLAY_DESCRIPTION}
  • + *
  • {@link #METADATA_KEY_DISPLAY_ICON_URI}
  • + *
+ * + * @param key The key for referencing this value + * @param value The CharSequence value to store + * @return The Builder to allow chaining + */ + public Builder putText(@TextKey String key, CharSequence value) { + if (METADATA_KEYS_TYPE.containsKey(key)) { + if (METADATA_KEYS_TYPE.get(key) != METADATA_TYPE_TEXT) { + throw new IllegalArgumentException("The " + key + + " key cannot be used to put a CharSequence"); + } + } + mBundle.putCharSequence(key, value); + return this; + } + + /** + * Put a String value into the metadata. Custom keys may be used, but if + * the METADATA_KEYs defined in this class are used they may only be one + * of the following: + *
    + *
  • {@link #METADATA_KEY_TITLE}
  • + *
  • {@link #METADATA_KEY_ARTIST}
  • + *
  • {@link #METADATA_KEY_ALBUM}
  • + *
  • {@link #METADATA_KEY_AUTHOR}
  • + *
  • {@link #METADATA_KEY_WRITER}
  • + *
  • {@link #METADATA_KEY_COMPOSER}
  • + *
  • {@link #METADATA_KEY_DATE}
  • + *
  • {@link #METADATA_KEY_GENRE}
  • + *
  • {@link #METADATA_KEY_ALBUM_ARTIST}
  • + *
  • {@link #METADATA_KEY_ART_URI}
  • + *
  • {@link #METADATA_KEY_ALBUM_ART_URI}
  • + *
  • {@link #METADATA_KEY_DISPLAY_TITLE}
  • + *
  • {@link #METADATA_KEY_DISPLAY_SUBTITLE}
  • + *
  • {@link #METADATA_KEY_DISPLAY_DESCRIPTION}
  • + *
  • {@link #METADATA_KEY_DISPLAY_ICON_URI}
  • + *
+ * + * @param key The key for referencing this value + * @param value The String value to store + * @return The Builder to allow chaining + */ + public Builder putString(@TextKey String key, String value) { + if (METADATA_KEYS_TYPE.containsKey(key)) { + if (METADATA_KEYS_TYPE.get(key) != METADATA_TYPE_TEXT) { + throw new IllegalArgumentException("The " + key + + " key cannot be used to put a String"); + } + } + mBundle.putCharSequence(key, value); + return this; + } + + /** + * Put a long value into the metadata. Custom keys may be used, but if + * the METADATA_KEYs defined in this class are used they may only be one + * of the following: + *
    + *
  • {@link #METADATA_KEY_DURATION}
  • + *
  • {@link #METADATA_KEY_TRACK_NUMBER}
  • + *
  • {@link #METADATA_KEY_NUM_TRACKS}
  • + *
  • {@link #METADATA_KEY_DISC_NUMBER}
  • + *
  • {@link #METADATA_KEY_YEAR}
  • + *
  • {@link #METADATA_KEY_BT_FOLDER_TYPE}
  • + *
  • {@link #METADATA_KEY_ADVERTISEMENT}
  • + *
  • {@link #METADATA_KEY_DOWNLOAD_STATUS}
  • + *
+ * + * @param key The key for referencing this value + * @param value The String value to store + * @return The Builder to allow chaining + */ + public Builder putLong(@LongKey String key, long value) { + if (METADATA_KEYS_TYPE.containsKey(key)) { + if (METADATA_KEYS_TYPE.get(key) != METADATA_TYPE_LONG) { + throw new IllegalArgumentException("The " + key + + " key cannot be used to put a long"); + } + } + mBundle.putLong(key, value); + return this; + } + + /** + * Put a {@link Rating2} into the metadata. Custom keys may be used, but + * if the METADATA_KEYs defined in this class are used they may only be + * one of the following: + *
    + *
  • {@link #METADATA_KEY_RATING}
  • + *
  • {@link #METADATA_KEY_USER_RATING}
  • + *
+ * + * @param key The key for referencing this value + * @param value The String value to store + * @return The Builder to allow chaining + */ + public Builder putRating(@RatingKey String key, Rating2 value) { + if (METADATA_KEYS_TYPE.containsKey(key)) { + if (METADATA_KEYS_TYPE.get(key) != METADATA_TYPE_RATING) { + throw new IllegalArgumentException("The " + key + + " key cannot be used to put a Rating"); + } + } + mBundle.putBundle(key, value.toBundle()); + + return this; + } + + /** + * Put a {@link Bitmap} into the metadata. Custom keys may be used, but + * if the METADATA_KEYs defined in this class are used they may only be + * one of the following: + *
    + *
  • {@link #METADATA_KEY_ART}
  • + *
  • {@link #METADATA_KEY_ALBUM_ART}
  • + *
  • {@link #METADATA_KEY_DISPLAY_ICON}
  • + *
+ * Large bitmaps may be scaled down by the system when + * {@link android.media.session.MediaSession#setMetadata} is called. + * To pass full resolution images {@link Uri Uris} should be used with + * {@link #putString}. + * + * @param key The key for referencing this value + * @param value The Bitmap to store + * @return The Builder to allow chaining + */ + public Builder putBitmap(@BitmapKey String key, Bitmap value) { + if (METADATA_KEYS_TYPE.containsKey(key)) { + if (METADATA_KEYS_TYPE.get(key) != METADATA_TYPE_BITMAP) { + throw new IllegalArgumentException("The " + key + + " key cannot be used to put a Bitmap"); + } + } + mBundle.putParcelable(key, value); + return this; + } + + /** + * Set an extra {@link Bundle} into the metadata. + */ + public Builder setExtra(Bundle bundle) { + mBundle.putBundle(METADATA_KEY_EXTRA, bundle); + return this; + } + + /** + * Creates a {@link MediaMetadata2} instance with the specified fields. + * + * @return The new MediaMetadata2 instance + */ + public MediaMetadata2 build() { + return new MediaMetadata2(mBundle); + } + + private Bitmap scaleBitmap(Bitmap bmp, int maxSize) { + float maxSizeF = maxSize; + float widthScale = maxSizeF / bmp.getWidth(); + float heightScale = maxSizeF / bmp.getHeight(); + float scale = Math.min(widthScale, heightScale); + int height = (int) (bmp.getHeight() * scale); + int width = (int) (bmp.getWidth() * scale); + return Bitmap.createScaledBitmap(bmp, width, height, true); + } + } +} + diff --git a/android/media/MediaPlayer2.java b/android/media/MediaPlayer2.java new file mode 100644 index 00000000..d36df845 --- /dev/null +++ b/android/media/MediaPlayer2.java @@ -0,0 +1,2476 @@ +/* + * Copyright 2017 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.CallbackExecutor; +import android.annotation.IntDef; +import android.annotation.NonNull; +import android.annotation.Nullable; +import android.content.Context; +import android.graphics.SurfaceTexture; +import android.net.Uri; +import android.os.Handler; +import android.os.Parcel; +import android.os.PersistableBundle; +import android.view.Surface; +import android.view.SurfaceHolder; +import android.media.MediaDrm; +import android.media.MediaFormat; +import android.media.MediaPlayer2Impl; +import android.media.MediaTimeProvider; +import android.media.PlaybackParams; +import android.media.SubtitleController; +import android.media.SubtitleController.Anchor; +import android.media.SubtitleData; +import android.media.SubtitleTrack.RenderingWidget; +import android.media.SyncParams; + +import java.io.FileDescriptor; +import java.io.IOException; +import java.io.InputStream; +import java.lang.AutoCloseable; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.net.InetSocketAddress; +import java.util.concurrent.Executor; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.UUID; + + +/** + * MediaPlayer2 class can be used to control playback + * of audio/video files and streams. An example on how to use the methods in + * this class can be found in {@link android.widget.VideoView}. + * + *

Topics covered here are: + *

    + *
  1. State Diagram + *
  2. Valid and Invalid States + *
  3. Permissions + *
  4. Register informational and error callbacks + *
+ * + *
+ *

Developer Guides

+ *

For more information about how to use MediaPlayer2, read the + * Media Playback developer guide.

+ *
+ * + * + *

State Diagram

+ * + *

Playback control of audio/video files and streams is managed as a state + * machine. The following diagram shows the life cycle and the states of a + * MediaPlayer2 object driven by the supported playback control operations. + * The ovals represent the states a MediaPlayer2 object may reside + * in. The arcs represent the playback control operations that drive the object + * state transition. There are two types of arcs. The arcs with a single arrow + * head represent synchronous method calls, while those with + * a double arrow head represent asynchronous method calls.

+ * + *

MediaPlayer State diagram

+ * + *

From this state diagram, one can see that a MediaPlayer2 object has the + * following states:

+ *
    + *
  • When a MediaPlayer2 object is just created using new or + * after {@link #reset()} is called, it is in the Idle state; and after + * {@link #close()} is called, it is in the End state. Between these + * two states is the life cycle of the MediaPlayer2 object. + *
      + *
    • There is a subtle but important difference between a newly constructed + * MediaPlayer2 object and the MediaPlayer2 object after {@link #reset()} + * is called. It is a programming error to invoke methods such + * as {@link #getCurrentPosition()}, + * {@link #getDuration()}, {@link #getVideoHeight()}, + * {@link #getVideoWidth()}, {@link #setAudioAttributes(AudioAttributes)}, + * {@link #setVolume(float, float)}, {@link #pause()}, {@link #play()}, + * {@link #seekTo(long, int)} or + * {@link #prepareAsync()} in the Idle state for both cases. If any of these + * methods is called right after a MediaPlayer2 object is constructed, + * the user supplied callback method OnErrorListener.onError() won't be + * called by the internal player engine and the object state remains + * unchanged; but if these methods are called right after {@link #reset()}, + * the user supplied callback method OnErrorListener.onError() will be + * invoked by the internal player engine and the object will be + * transfered to the Error state.
    • + *
    • It is also recommended that once + * a MediaPlayer2 object is no longer being used, call {@link #close()} immediately + * so that resources used by the internal player engine associated with the + * MediaPlayer2 object can be released immediately. Resource may include + * singleton resources such as hardware acceleration components and + * failure to call {@link #close()} may cause subsequent instances of + * MediaPlayer2 objects to fallback to software implementations or fail + * altogether. Once the MediaPlayer2 + * object is in the End state, it can no longer be used and + * there is no way to bring it back to any other state.
    • + *
    • Furthermore, + * the MediaPlayer2 objects created using new is in the + * Idle state. + *
    • + *
    + *
  • + *
  • In general, some playback control operation may fail due to various + * reasons, such as unsupported audio/video format, poorly interleaved + * audio/video, resolution too high, streaming timeout, and the like. + * Thus, error reporting and recovery is an important concern under + * these circumstances. Sometimes, due to programming errors, invoking a playback + * control operation in an invalid state may also occur. Under all these + * error conditions, the internal player engine invokes a user supplied + * EventCallback.onError() method if an EventCallback has been + * registered beforehand via + * {@link #registerEventCallback(Executor, EventCallback)}. + *
      + *
    • It is important to note that once an error occurs, the + * MediaPlayer2 object enters the Error state (except as noted + * above), even if an error listener has not been registered by the application.
    • + *
    • In order to reuse a MediaPlayer2 object that is in the + * Error state and recover from the error, + * {@link #reset()} can be called to restore the object to its Idle + * state.
    • + *
    • It is good programming practice to have your application + * register a OnErrorListener to look out for error notifications from + * the internal player engine.
    • + *
    • IllegalStateException is + * thrown to prevent programming errors such as calling + * {@link #prepareAsync()}, {@link #setDataSource(DataSourceDesc)}, or + * {@code setPlaylist} methods in an invalid state.
    • + *
    + *
  • + *
  • Calling + * {@link #setDataSource(DataSourceDesc)}, or + * {@code setPlaylist} transfers a + * MediaPlayer2 object in the Idle state to the + * Initialized state. + *
      + *
    • An IllegalStateException is thrown if + * setDataSource() or setPlaylist() is called in any other state.
    • + *
    • It is good programming + * practice to always look out for IllegalArgumentException + * and IOException that may be thrown from + * setDataSource and setPlaylist methods.
    • + *
    + *
  • + *
  • A MediaPlayer2 object must first enter the Prepared state + * before playback can be started. + *
      + *
    • There are an asynchronous way that the Prepared state can be reached: + * a call to {@link #prepareAsync()} (asynchronous) which + * first transfers the object to the Preparing state after the + * call returns (which occurs almost right way) while the internal + * player engine continues working on the rest of preparation work + * until the preparation work completes. When the preparation completes, + * the internal player engine then calls a user supplied callback method, + * onInfo() of the EventCallback interface with {@link #MEDIA_INFO_PREPARED}, if an + * EventCallback is registered beforehand via + * {@link #registerEventCallback(Executor, EventCallback)}.
    • + *
    • It is important to note that + * the Preparing state is a transient state, and the behavior + * of calling any method with side effect while a MediaPlayer2 object is + * in the Preparing state is undefined.
    • + *
    • An IllegalStateException is + * thrown if {@link #prepareAsync()} is called in + * any other state.
    • + *
    • While in the Prepared state, properties + * such as audio/sound volume, screenOnWhilePlaying, looping can be + * adjusted by invoking the corresponding set methods.
    • + *
    + *
  • + *
  • To start the playback, {@link #play()} must be called. After + * {@link #play()} returns successfully, the MediaPlayer2 object is in the + * Started state. {@link #isPlaying()} can be called to test + * whether the MediaPlayer2 object is in the Started state. + *
      + *
    • While in the Started state, the internal player engine calls + * a user supplied EventCallback.onBufferingUpdate() callback + * method if an EventCallback has been registered beforehand + * via {@link #registerEventCallback(Executor, EventCallback)}. + * This callback allows applications to keep track of the buffering status + * while streaming audio/video.
    • + *
    • Calling {@link #play()} has not effect + * on a MediaPlayer2 object that is already in the Started state.
    • + *
    + *
  • + *
  • Playback can be paused and stopped, and the current playback position + * can be adjusted. Playback can be paused via {@link #pause()}. When the call to + * {@link #pause()} returns, the MediaPlayer2 object enters the + * Paused state. Note that the transition from the Started + * state to the Paused state and vice versa happens + * asynchronously in the player engine. It may take some time before + * the state is updated in calls to {@link #isPlaying()}, and it can be + * a number of seconds in the case of streamed content. + *
      + *
    • Calling {@link #play()} to resume playback for a paused + * MediaPlayer2 object, and the resumed playback + * position is the same as where it was paused. When the call to + * {@link #play()} returns, the paused MediaPlayer2 object goes back to + * the Started state.
    • + *
    • Calling {@link #pause()} has no effect on + * a MediaPlayer2 object that is already in the Paused state.
    • + *
    + *
  • + *
  • The playback position can be adjusted with a call to + * {@link #seekTo(long, int)}. + *
      + *
    • Although the asynchronuous {@link #seekTo(long, int)} + * call returns right away, the actual seek operation may take a while to + * finish, especially for audio/video being streamed. When the actual + * seek operation completes, the internal player engine calls a user + * supplied EventCallback.onInfo() with {@link #MEDIA_INFO_COMPLETE_CALL_SEEK} + * if an EventCallback has been registered beforehand via + * {@link #registerEventCallback(Executor, EventCallback)}.
    • + *
    • Please + * note that {@link #seekTo(long, int)} can also be called in the other states, + * such as Prepared, Paused and PlaybackCompleted + * state. When {@link #seekTo(long, int)} is called in those states, + * one video frame will be displayed if the stream has video and the requested + * position is valid. + *
    • + *
    • Furthermore, the actual current playback position + * can be retrieved with a call to {@link #getCurrentPosition()}, which + * is helpful for applications such as a Music player that need to keep + * track of the playback progress.
    • + *
    + *
  • + *
  • When the playback reaches the end of stream, the playback completes. + *
      + *
    • If the looping mode was being set to one of the values of + * {@link #LOOPING_MODE_FULL}, {@link #LOOPING_MODE_SINGLE} or + * {@link #LOOPING_MODE_SHUFFLE} with + * {@link #setLoopingMode(int)}, the MediaPlayer2 object shall remain in + * the Started state.
    • + *
    • If the looping mode was set to false + * , the player engine calls a user supplied callback method, + * EventCallback.onCompletion(), if an EventCallback is registered + * beforehand via {@link #registerEventCallback(Executor, EventCallback)}. + * The invoke of the callback signals that the object is now in the + * PlaybackCompleted state.
    • + *
    • While in the PlaybackCompleted + * state, calling {@link #play()} can restart the playback from the + * beginning of the audio/video source.
    • + *
    + * + * + * + *

    Valid and invalid states

    + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
    Method Name

    Valid Sates

    Invalid States

    Comments

    attachAuxEffect

    {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted}

    {Idle, Error}

    This method must be called after setDataSource or setPlaylist. + * Calling it does not change the object state.

    getAudioSessionId

    any

    {}

    This method can be called in any state and calling it does not change + * the object state.

    getCurrentPosition

    {Idle, Initialized, Prepared, Started, Paused, Stopped, + * PlaybackCompleted}

    {Error}

    Successful invoke of this method in a valid state does not change the + * state. Calling this method in an invalid state transfers the object + * to the Error state.

    getDuration

    {Prepared, Started, Paused, Stopped, PlaybackCompleted}

    {Idle, Initialized, Error}

    Successful invoke of this method in a valid state does not change the + * state. Calling this method in an invalid state transfers the object + * to the Error state.

    getVideoHeight

    {Idle, Initialized, Prepared, Started, Paused, Stopped, + * PlaybackCompleted}

    {Error}

    Successful invoke of this method in a valid state does not change the + * state. Calling this method in an invalid state transfers the object + * to the Error state.

    getVideoWidth

    {Idle, Initialized, Prepared, Started, Paused, Stopped, + * PlaybackCompleted}

    {Error}

    Successful invoke of this method in a valid state does not change + * the state. Calling this method in an invalid state transfers the + * object to the Error state.

    isPlaying

    {Idle, Initialized, Prepared, Started, Paused, Stopped, + * PlaybackCompleted}

    {Error}

    Successful invoke of this method in a valid state does not change + * the state. Calling this method in an invalid state transfers the + * object to the Error state.

    pause

    {Started, Paused, PlaybackCompleted}

    {Idle, Initialized, Prepared, Stopped, Error}

    Successful invoke of this method in a valid state transfers the + * object to the Paused state. Calling this method in an + * invalid state transfers the object to the Error state.

    prepareAsync

    {Initialized, Stopped}

    {Idle, Prepared, Started, Paused, PlaybackCompleted, Error}

    Successful invoke of this method in a valid state transfers the + * object to the Preparing state. Calling this method in an + * invalid state throws an IllegalStateException.

    release

    any

    {}

    After {@link #close()}, the object is no longer available.

    reset

    {Idle, Initialized, Prepared, Started, Paused, Stopped, + * PlaybackCompleted, Error}

    {}

    After {@link #reset()}, the object is like being just created.

    seekTo

    {Prepared, Started, Paused, PlaybackCompleted}

    {Idle, Initialized, Stopped, Error}

    Successful invoke of this method in a valid state does not change + * the state. Calling this method in an invalid state transfers the + * object to the Error state.

    setAudioAttributes

    {Idle, Initialized, Stopped, Prepared, Started, Paused, + * PlaybackCompleted}

    {Error}

    Successful invoke of this method does not change the state. In order for the + * target audio attributes type to become effective, this method must be called before + * prepareAsync().

    setAudioSessionId

    {Idle}

    {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, + * Error}

    This method must be called in idle state as the audio session ID must be known before + * calling setDataSource or setPlaylist. Calling it does not change the object + * state.

    setAudioStreamType (deprecated)

    {Idle, Initialized, Stopped, Prepared, Started, Paused, + * PlaybackCompleted}

    {Error}

    Successful invoke of this method does not change the state. In order for the + * target audio stream type to become effective, this method must be called before + * prepareAsync().

    setAuxEffectSendLevel

    any

    {}

    Calling this method does not change the object state.

    setDataSource

    {Idle}

    {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, + * Error}

    Successful invoke of this method in a valid state transfers the + * object to the Initialized state. Calling this method in an + * invalid state throws an IllegalStateException.

    setPlaylist

    {Idle}

    {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, + * Error}

    Successful invoke of this method in a valid state transfers the + * object to the Initialized state. Calling this method in an + * invalid state throws an IllegalStateException.

    setDisplay

    any

    {}

    This method can be called in any state and calling it does not change + * the object state.

    setSurface

    any

    {}

    This method can be called in any state and calling it does not change + * the object state.

    setLoopingMode

    {Idle, Initialized, Stopped, Prepared, Started, Paused, + * PlaybackCompleted}

    {Error}

    Successful invoke of this method in a valid state does not change + * the state. Calling this method in an + * invalid state transfers the object to the Error state.

    isLooping

    any

    {}

    This method can be called in any state and calling it does not change + * the object state.

    registerDrmEventCallback

    any

    {}

    This method can be called in any state and calling it does not change + * the object state.

    registerEventCallback

    any

    {}

    This method can be called in any state and calling it does not change + * the object state.

    setPlaybackParams

    {Initialized, Prepared, Started, Paused, PlaybackCompleted, Error}

    {Idle, Stopped}

    This method will change state in some cases, depending on when it's called. + *

    setVolume

    {Idle, Initialized, Stopped, Prepared, Started, Paused, + * PlaybackCompleted}

    {Error}

    Successful invoke of this method does not change the state. + *
    play

    {Prepared, Started, Paused, PlaybackCompleted}

    {Idle, Initialized, Stopped, Error}

    Successful invoke of this method in a valid state transfers the + * object to the Started state. Calling this method in an + * invalid state transfers the object to the Error state.

    stop

    {Prepared, Started, Stopped, Paused, PlaybackCompleted}

    {Idle, Initialized, Error}

    Successful invoke of this method in a valid state transfers the + * object to the Stopped state. Calling this method in an + * invalid state transfers the object to the Error state.

    getTrackInfo

    {Prepared, Started, Stopped, Paused, PlaybackCompleted}

    {Idle, Initialized, Error}

    Successful invoke of this method does not change the state.

    selectTrack

    {Prepared, Started, Stopped, Paused, PlaybackCompleted}

    {Idle, Initialized, Error}

    Successful invoke of this method does not change the state.

    deselectTrack

    {Prepared, Started, Stopped, Paused, PlaybackCompleted}

    {Idle, Initialized, Error}

    Successful invoke of this method does not change the state.

    + * + * + *

    Permissions

    + *

    One may need to declare a corresponding WAKE_LOCK permission {@link + * android.R.styleable#AndroidManifestUsesPermission <uses-permission>} + * element. + * + *

    This class requires the {@link android.Manifest.permission#INTERNET} permission + * when used with network-based content. + * + * + *

    Callbacks

    + *

    Applications may want to register for informational and error + * events in order to be informed of some internal state update and + * possible runtime errors during playback or streaming. Registration for + * these events is done by properly setting the appropriate listeners (via calls + * to + * {@link #registerEventCallback(Executor, EventCallback)}, + * {@link #registerDrmEventCallback(Executor, DrmEventCallback)}). + * In order to receive the respective callback + * associated with these listeners, applications are required to create + * MediaPlayer2 objects on a thread with its own Looper running (main UI + * thread by default has a Looper running). + * + */ +public abstract class MediaPlayer2 implements SubtitleController.Listener + , AudioRouting + , AutoCloseable +{ + /** + Constant to retrieve only the new metadata since the last + call. + // FIXME: unhide. + // FIXME: add link to getMetadata(boolean, boolean) + {@hide} + */ + public static final boolean METADATA_UPDATE_ONLY = true; + + /** + Constant to retrieve all the metadata. + // FIXME: unhide. + // FIXME: add link to getMetadata(boolean, boolean) + {@hide} + */ + public static final boolean METADATA_ALL = false; + + /** + Constant to enable the metadata filter during retrieval. + // FIXME: unhide. + // FIXME: add link to getMetadata(boolean, boolean) + {@hide} + */ + public static final boolean APPLY_METADATA_FILTER = true; + + /** + Constant to disable the metadata filter during retrieval. + // FIXME: unhide. + // FIXME: add link to getMetadata(boolean, boolean) + {@hide} + */ + public static final boolean BYPASS_METADATA_FILTER = false; + + /** + * Create a MediaPlayer2 object. + * + * @return A MediaPlayer2 object created + */ + public static final MediaPlayer2 create() { + // TODO: load MediaUpdate APK + return new MediaPlayer2Impl(); + } + + /** + * @hide + */ + // add hidden empty constructor so it doesn't show in SDK + public MediaPlayer2() { } + + /** + * Create a request parcel which can be routed to the native media + * player using {@link #invoke(Parcel, Parcel)}. The Parcel + * returned has the proper InterfaceToken set. The caller should + * not overwrite that token, i.e it can only append data to the + * Parcel. + * + * @return A parcel suitable to hold a request for the native + * player. + * {@hide} + */ + public Parcel newRequest() { + return null; + } + + /** + * Invoke a generic method on the native player using opaque + * parcels for the request and reply. Both payloads' format is a + * convention between the java caller and the native player. + * Must be called after setDataSource or setPlaylist to make sure a native player + * exists. On failure, a RuntimeException is thrown. + * + * @param request Parcel with the data for the extension. The + * caller must use {@link #newRequest()} to get one. + * + * @param reply Output parcel with the data returned by the + * native player. + * {@hide} + */ + public void invoke(Parcel request, Parcel reply) { } + + /** + * Sets the {@link SurfaceHolder} to use for displaying the video + * portion of the media. + * + * Either a surface holder or surface must be set if a display or video sink + * is needed. Not calling this method or {@link #setSurface(Surface)} + * when playing back a video will result in only the audio track being played. + * A null surface holder or surface will result in only the audio track being + * played. + * + * @param sh the SurfaceHolder to use for video display + * @throws IllegalStateException if the internal player engine has not been + * initialized or has been released. + * @hide + */ + public abstract void setDisplay(SurfaceHolder sh); + + /** + * Sets the {@link Surface} to be used as the sink for the video portion of + * the media. Setting a + * Surface will un-set any Surface or SurfaceHolder that was previously set. + * A null surface will result in only the audio track being played. + * + * If the Surface sends frames to a {@link SurfaceTexture}, the timestamps + * returned from {@link SurfaceTexture#getTimestamp()} will have an + * unspecified zero point. These timestamps cannot be directly compared + * between different media sources, different instances of the same media + * source, or multiple runs of the same program. The timestamp is normally + * monotonically increasing and is unaffected by time-of-day adjustments, + * but it is reset when the position is set. + * + * @param surface The {@link Surface} to be used for the video portion of + * the media. + * @throws IllegalStateException if the internal player engine has not been + * initialized or has been released. + */ + public abstract void setSurface(Surface surface); + + /* Do not change these video scaling mode values below without updating + * their counterparts in system/window.h! Please do not forget to update + * {@link #isVideoScalingModeSupported} when new video scaling modes + * are added. + */ + /** + * Specifies a video scaling mode. The content is stretched to the + * surface rendering area. When the surface has the same aspect ratio + * as the content, the aspect ratio of the content is maintained; + * otherwise, the aspect ratio of the content is not maintained when video + * is being rendered. + * There is no content cropping with this video scaling mode. + */ + public static final int VIDEO_SCALING_MODE_SCALE_TO_FIT = 1; + + /** + * Specifies a video scaling mode. The content is scaled, maintaining + * its aspect ratio. The whole surface area is always used. When the + * aspect ratio of the content is the same as the surface, no content + * is cropped; otherwise, content is cropped to fit the surface. + * @hide + */ + public static final int VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING = 2; + + /** + * Sets video scaling mode. To make the target video scaling mode + * effective during playback, this method must be called after + * data source is set. If not called, the default video + * scaling mode is {@link #VIDEO_SCALING_MODE_SCALE_TO_FIT}. + * + *

    The supported video scaling modes are: + *

      + *
    • {@link #VIDEO_SCALING_MODE_SCALE_TO_FIT} + *
    + * + * @param mode target video scaling mode. Must be one of the supported + * video scaling modes; otherwise, IllegalArgumentException will be thrown. + * + * @see MediaPlayer2#VIDEO_SCALING_MODE_SCALE_TO_FIT + * @hide + */ + public void setVideoScalingMode(int mode) { } + + /** + * Discards all pending commands. + */ + public abstract void clearPendingCommands(); + + /** + * Sets the data source as described by a DataSourceDesc. + * + * @param dsd the descriptor of data source you want to play + * @throws IllegalStateException if it is called in an invalid state + * @throws NullPointerException if dsd is null + */ + public abstract void setDataSource(@NonNull DataSourceDesc dsd) throws IOException; + + /** + * Gets the current data source as described by a DataSourceDesc. + * + * @return the current DataSourceDesc + */ + public abstract DataSourceDesc getCurrentDataSource(); + + /** + * Sets the play list. + * + * If startIndex falls outside play list range, it will be clamped to the nearest index + * in the play list. + * + * @param pl the play list of data source you want to play + * @param startIndex the index of the DataSourceDesc in the play list you want to play first + * @throws IllegalStateException if it is called in an invalid state + * @throws IllegalArgumentException if pl is null or empty, or pl contains null DataSourceDesc + */ + public abstract void setPlaylist(@NonNull List pl, int startIndex) + throws IOException; + + /** + * Gets a copy of the play list. + * + * @return a copy of the play list used by {@link MediaPlayer2} + */ + public abstract List getPlaylist(); + + /** + * Sets the index of current DataSourceDesc in the play list to be played. + * + * @param index the index of DataSourceDesc in the play list you want to play + * @throws IllegalArgumentException if the play list is null + * @throws NullPointerException if index is outside play list range + */ + public abstract void setCurrentPlaylistItem(int index); + + /** + * Sets the index of next-to-be-played DataSourceDesc in the play list. + * + * @param index the index of next-to-be-played DataSourceDesc in the play list + * @throws IllegalArgumentException if the play list is null + * @throws NullPointerException if index is outside play list range + */ + public abstract void setNextPlaylistItem(int index); + + /** + * Gets the current index of play list. + * + * @return the index of the current DataSourceDesc in the play list + */ + public abstract int getCurrentPlaylistItemIndex(); + + /** + * Specifies a playback looping mode. The source will not be played in looping mode. + */ + public static final int LOOPING_MODE_NONE = 0; + /** + * Specifies a playback looping mode. The full list of source will be played in looping mode, + * and in the order specified in the play list. + */ + public static final int LOOPING_MODE_FULL = 1; + /** + * Specifies a playback looping mode. The current DataSourceDesc will be played in looping mode. + */ + public static final int LOOPING_MODE_SINGLE = 2; + /** + * Specifies a playback looping mode. The full list of source will be played in looping mode, + * and in a random order. + */ + public static final int LOOPING_MODE_SHUFFLE = 3; + + /** @hide */ + @IntDef( + value = { + LOOPING_MODE_NONE, + LOOPING_MODE_FULL, + LOOPING_MODE_SINGLE, + LOOPING_MODE_SHUFFLE, + }) + @Retention(RetentionPolicy.SOURCE) + public @interface LoopingMode {} + + /** + * Sets the looping mode of the play list. + * The mode shall be one of {@link #LOOPING_MODE_NONE}, {@link #LOOPING_MODE_FULL}, + * {@link #LOOPING_MODE_SINGLE}, {@link #LOOPING_MODE_SHUFFLE}. + * + * @param mode the mode in which the play list will be played + * @throws IllegalArgumentException if mode is not supported + */ + public abstract void setLoopingMode(@LoopingMode int mode); + + /** + * Gets the looping mode of play list. + * + * @return the looping mode of the play list + */ + public abstract int getLoopingMode(); + + /** + * Moves the DataSourceDesc at indexFrom in the play list to indexTo. + * + * @throws IllegalArgumentException if the play list is null + * @throws IndexOutOfBoundsException if indexFrom or indexTo is outside play list range + */ + public abstract void movePlaylistItem(int indexFrom, int indexTo); + + /** + * Removes the DataSourceDesc at index in the play list. + * + * If index is same as the current index of the play list, current DataSourceDesc + * will be stopped and playback moves to next source in the list. + * + * @return the removed DataSourceDesc at index in the play list + * @throws IllegalArgumentException if the play list is null + * @throws IndexOutOfBoundsException if index is outside play list range + */ + public abstract DataSourceDesc removePlaylistItem(int index); + + /** + * Inserts the DataSourceDesc to the play list at position index. + * + * This will not change the DataSourceDesc currently being played. + * If index is less than or equal to the current index of the play list, + * the current index of the play list will be incremented correspondingly. + * + * @param index the index you want to add dsd to the play list + * @param dsd the descriptor of data source you want to add to the play list + * @throws IndexOutOfBoundsException if index is outside play list range + * @throws NullPointerException if dsd is null + */ + public abstract void addPlaylistItem(int index, DataSourceDesc dsd); + + /** + * replaces the DataSourceDesc at index in the play list with given dsd. + * + * When index is same as the current index of the play list, the current source + * will be stopped and the new source will be played, except that if new + * and old source only differ on end position and current media position is + * smaller then the new end position. + * + * This will not change the DataSourceDesc currently being played. + * If index is less than or equal to the current index of the play list, + * the current index of the play list will be incremented correspondingly. + * + * @param index the index you want to add dsd to the play list + * @param dsd the descriptor of data source you want to add to the play list + * @throws IndexOutOfBoundsException if index is outside play list range + * @throws NullPointerException if dsd is null + */ + public abstract DataSourceDesc editPlaylistItem(int index, DataSourceDesc dsd); + + /** + * Prepares the player for playback, synchronously. + * + * After setting the datasource and the display surface, you need to either + * call prepare() or prepareAsync(). For files, it is OK to call prepare(), + * which blocks until MediaPlayer2 is ready for playback. + * + * @throws IOException if source can not be accessed + * @throws IllegalStateException if it is called in an invalid state + * @hide + */ + public void prepare() throws IOException { } + + /** + * Prepares the player for playback, asynchronously. + * + * After setting the datasource and the display surface, you need to + * call prepareAsync(). + * + * @throws IllegalStateException if it is called in an invalid state + */ + public abstract void prepareAsync(); + + /** + * Starts or resumes playback. If playback had previously been paused, + * playback will continue from where it was paused. If playback had + * been stopped, or never started before, playback will start at the + * beginning. + * + * @throws IllegalStateException if it is called in an invalid state + */ + public abstract void play(); + + /** + * Stops playback after playback has been started or paused. + * + * @throws IllegalStateException if the internal player engine has not been + * initialized. + * @hide + */ + public void stop() { } + + /** + * Pauses playback. Call play() to resume. + * + * @throws IllegalStateException if the internal player engine has not been + * initialized. + */ + public abstract void pause(); + + //-------------------------------------------------------------------------- + // Explicit Routing + //-------------------- + + /** + * Specifies an audio device (via an {@link AudioDeviceInfo} object) to route + * the output from this MediaPlayer2. + * @param deviceInfo The {@link AudioDeviceInfo} specifying the audio sink or source. + * If deviceInfo is null, default routing is restored. + * @return true if succesful, false if the specified {@link AudioDeviceInfo} is non-null and + * does not correspond to a valid audio device. + */ + @Override + public abstract boolean setPreferredDevice(AudioDeviceInfo deviceInfo); + + /** + * Returns the selected output specified by {@link #setPreferredDevice}. Note that this + * is not guaranteed to correspond to the actual device being used for playback. + */ + @Override + public abstract AudioDeviceInfo getPreferredDevice(); + + /** + * Returns an {@link AudioDeviceInfo} identifying the current routing of this MediaPlayer2 + * Note: The query is only valid if the MediaPlayer2 is currently playing. + * If the player is not playing, the returned device can be null or correspond to previously + * selected device when the player was last active. + */ + @Override + public abstract AudioDeviceInfo getRoutedDevice(); + + /** + * Adds an {@link AudioRouting.OnRoutingChangedListener} to receive notifications of routing + * changes on this MediaPlayer2. + * @param listener The {@link AudioRouting.OnRoutingChangedListener} interface to receive + * notifications of rerouting events. + * @param handler Specifies the {@link Handler} object for the thread on which to execute + * the callback. If null, the handler on the main looper will be used. + */ + @Override + public abstract void addOnRoutingChangedListener(AudioRouting.OnRoutingChangedListener listener, + Handler handler); + + /** + * Removes an {@link AudioRouting.OnRoutingChangedListener} which has been previously added + * to receive rerouting notifications. + * @param listener The previously added {@link AudioRouting.OnRoutingChangedListener} interface + * to remove. + */ + @Override + public abstract void removeOnRoutingChangedListener(AudioRouting.OnRoutingChangedListener listener); + + /** + * Set the low-level power management behavior for this MediaPlayer2. + * + *

    This function has the MediaPlayer2 access the low-level power manager + * service to control the device's power usage while playing is occurring. + * The parameter is a combination of {@link android.os.PowerManager} wake flags. + * Use of this method requires {@link android.Manifest.permission#WAKE_LOCK} + * permission. + * By default, no attempt is made to keep the device awake during playback. + * + * @param context the Context to use + * @param mode the power/wake mode to set + * @see android.os.PowerManager + * @hide + */ + public abstract void setWakeMode(Context context, int mode); + + /** + * Control whether we should use the attached SurfaceHolder to keep the + * screen on while video playback is occurring. This is the preferred + * method over {@link #setWakeMode} where possible, since it doesn't + * require that the application have permission for low-level wake lock + * access. + * + * @param screenOn Supply true to keep the screen on, false to allow it + * to turn off. + * @hide + */ + public abstract void setScreenOnWhilePlaying(boolean screenOn); + + /** + * Returns the width of the video. + * + * @return the width of the video, or 0 if there is no video, + * no display surface was set, or the width has not been determined + * yet. The {@code EventCallback} can be registered via + * {@link #registerEventCallback(Executor, EventCallback)} to provide a + * notification {@code EventCallback.onVideoSizeChanged} when the width is available. + */ + public abstract int getVideoWidth(); + + /** + * Returns the height of the video. + * + * @return the height of the video, or 0 if there is no video, + * no display surface was set, or the height has not been determined + * yet. The {@code EventCallback} can be registered via + * {@link #registerEventCallback(Executor, EventCallback)} to provide a + * notification {@code EventCallback.onVideoSizeChanged} when the height is available. + */ + public abstract int getVideoHeight(); + + /** + * Return Metrics data about the current player. + * + * @return a {@link PersistableBundle} containing the set of attributes and values + * available for the media being handled by this instance of MediaPlayer2 + * The attributes are descibed in {@link MetricsConstants}. + * + * Additional vendor-specific fields may also be present in + * the return value. + */ + public abstract PersistableBundle getMetrics(); + + /** + * Checks whether the MediaPlayer2 is playing. + * + * @return true if currently playing, false otherwise + * @throws IllegalStateException if the internal player engine has not been + * initialized or has been released. + */ + public abstract boolean isPlaying(); + + /** + * Gets the current buffering management params used by the source component. + * Calling it only after {@code setDataSource} has been called. + * Each type of data source might have different set of default params. + * + * @return the current buffering management params used by the source component. + * @throws IllegalStateException if the internal player engine has not been + * initialized, or {@code setDataSource} has not been called. + * @hide + */ + @NonNull + public BufferingParams getBufferingParams() { + return new BufferingParams.Builder().build(); + } + + /** + * Sets buffering management params. + * The object sets its internal BufferingParams to the input, except that the input is + * invalid or not supported. + * Call it only after {@code setDataSource} has been called. + * The input is a hint to MediaPlayer2. + * + * @param params the buffering management params. + * + * @throws IllegalStateException if the internal player engine has not been + * initialized or has been released, or {@code setDataSource} has not been called. + * @throws IllegalArgumentException if params is invalid or not supported. + * @hide + */ + public void setBufferingParams(@NonNull BufferingParams params) { } + + /** + * Change playback speed of audio by resampling the audio. + *

    + * Specifies resampling as audio mode for variable rate playback, i.e., + * resample the waveform based on the requested playback rate to get + * a new waveform, and play back the new waveform at the original sampling + * frequency. + * When rate is larger than 1.0, pitch becomes higher. + * When rate is smaller than 1.0, pitch becomes lower. + * + * @hide + */ + public static final int PLAYBACK_RATE_AUDIO_MODE_RESAMPLE = 2; + + /** + * Change playback speed of audio without changing its pitch. + *

    + * Specifies time stretching as audio mode for variable rate playback. + * Time stretching changes the duration of the audio samples without + * affecting its pitch. + *

    + * This mode is only supported for a limited range of playback speed factors, + * e.g. between 1/2x and 2x. + * + * @hide + */ + public static final int PLAYBACK_RATE_AUDIO_MODE_STRETCH = 1; + + /** + * Change playback speed of audio without changing its pitch, and + * possibly mute audio if time stretching is not supported for the playback + * speed. + *

    + * Try to keep audio pitch when changing the playback rate, but allow the + * system to determine how to change audio playback if the rate is out + * of range. + * + * @hide + */ + public static final int PLAYBACK_RATE_AUDIO_MODE_DEFAULT = 0; + + /** @hide */ + @IntDef( + value = { + PLAYBACK_RATE_AUDIO_MODE_DEFAULT, + PLAYBACK_RATE_AUDIO_MODE_STRETCH, + PLAYBACK_RATE_AUDIO_MODE_RESAMPLE, + }) + @Retention(RetentionPolicy.SOURCE) + public @interface PlaybackRateAudioMode {} + + /** + * Sets playback rate and audio mode. + * + * @param rate the ratio between desired playback rate and normal one. + * @param audioMode audio playback mode. Must be one of the supported + * audio modes. + * + * @throws IllegalStateException if the internal player engine has not been + * initialized. + * @throws IllegalArgumentException if audioMode is not supported. + * + * @hide + */ + @NonNull + public PlaybackParams easyPlaybackParams(float rate, @PlaybackRateAudioMode int audioMode) { + return new PlaybackParams(); + } + + /** + * Sets playback rate using {@link PlaybackParams}. The object sets its internal + * PlaybackParams to the input, except that the object remembers previous speed + * when input speed is zero. This allows the object to resume at previous speed + * when play() is called. Calling it before the object is prepared does not change + * the object state. After the object is prepared, calling it with zero speed is + * equivalent to calling pause(). After the object is prepared, calling it with + * non-zero speed is equivalent to calling play(). + * + * @param params the playback params. + * + * @throws IllegalStateException if the internal player engine has not been + * initialized or has been released. + * @throws IllegalArgumentException if params is not supported. + */ + public abstract void setPlaybackParams(@NonNull PlaybackParams params); + + /** + * Gets the playback params, containing the current playback rate. + * + * @return the playback params. + * @throws IllegalStateException if the internal player engine has not been + * initialized. + */ + @NonNull + public abstract PlaybackParams getPlaybackParams(); + + /** + * Sets A/V sync mode. + * + * @param params the A/V sync params to apply + * + * @throws IllegalStateException if the internal player engine has not been + * initialized. + * @throws IllegalArgumentException if params are not supported. + */ + public abstract void setSyncParams(@NonNull SyncParams params); + + /** + * Gets the A/V sync mode. + * + * @return the A/V sync params + * + * @throws IllegalStateException if the internal player engine has not been + * initialized. + */ + @NonNull + public abstract SyncParams getSyncParams(); + + /** + * Seek modes used in method seekTo(long, int) to move media position + * to a specified location. + * + * Do not change these mode values without updating their counterparts + * in include/media/IMediaSource.h! + */ + /** + * This mode is used with {@link #seekTo(long, int)} to move media position to + * a sync (or key) frame associated with a data source that is located + * right before or at the given time. + * + * @see #seekTo(long, int) + */ + public static final int SEEK_PREVIOUS_SYNC = 0x00; + /** + * This mode is used with {@link #seekTo(long, int)} to move media position to + * a sync (or key) frame associated with a data source that is located + * right after or at the given time. + * + * @see #seekTo(long, int) + */ + public static final int SEEK_NEXT_SYNC = 0x01; + /** + * This mode is used with {@link #seekTo(long, int)} to move media position to + * a sync (or key) frame associated with a data source that is located + * closest to (in time) or at the given time. + * + * @see #seekTo(long, int) + */ + public static final int SEEK_CLOSEST_SYNC = 0x02; + /** + * This mode is used with {@link #seekTo(long, int)} to move media position to + * a frame (not necessarily a key frame) associated with a data source that + * is located closest to or at the given time. + * + * @see #seekTo(long, int) + */ + public static final int SEEK_CLOSEST = 0x03; + + /** @hide */ + @IntDef( + value = { + SEEK_PREVIOUS_SYNC, + SEEK_NEXT_SYNC, + SEEK_CLOSEST_SYNC, + SEEK_CLOSEST, + }) + @Retention(RetentionPolicy.SOURCE) + public @interface SeekMode {} + + /** + * Moves the media to specified time position by considering the given mode. + *

    + * When seekTo is finished, the user will be notified via OnSeekComplete supplied by the user. + * There is at most one active seekTo processed at any time. If there is a to-be-completed + * seekTo, new seekTo requests will be queued in such a way that only the last request + * is kept. When current seekTo is completed, the queued request will be processed if + * that request is different from just-finished seekTo operation, i.e., the requested + * position or mode is different. + * + * @param msec the offset in milliseconds from the start to seek to. + * When seeking to the given time position, there is no guarantee that the data source + * has a frame located at the position. When this happens, a frame nearby will be rendered. + * If msec is negative, time position zero will be used. + * If msec is larger than duration, duration will be used. + * @param mode the mode indicating where exactly to seek to. + * Use {@link #SEEK_PREVIOUS_SYNC} if one wants to seek to a sync frame + * that has a timestamp earlier than or the same as msec. Use + * {@link #SEEK_NEXT_SYNC} if one wants to seek to a sync frame + * that has a timestamp later than or the same as msec. Use + * {@link #SEEK_CLOSEST_SYNC} if one wants to seek to a sync frame + * that has a timestamp closest to or the same as msec. Use + * {@link #SEEK_CLOSEST} if one wants to seek to a frame that may + * or may not be a sync frame but is closest to or the same as msec. + * {@link #SEEK_CLOSEST} often has larger performance overhead compared + * to the other options if there is no sync frame located at msec. + * @throws IllegalStateException if the internal player engine has not been + * initialized + * @throws IllegalArgumentException if the mode is invalid. + */ + public abstract void seekTo(long msec, @SeekMode int mode); + + /** + * Get current playback position as a {@link MediaTimestamp}. + *

    + * The MediaTimestamp represents how the media time correlates to the system time in + * a linear fashion using an anchor and a clock rate. During regular playback, the media + * time moves fairly constantly (though the anchor frame may be rebased to a current + * system time, the linear correlation stays steady). Therefore, this method does not + * need to be called often. + *

    + * To help users get current playback position, this method always anchors the timestamp + * to the current {@link System#nanoTime system time}, so + * {@link MediaTimestamp#getAnchorMediaTimeUs} can be used as current playback position. + * + * @return a MediaTimestamp object if a timestamp is available, or {@code null} if no timestamp + * is available, e.g. because the media player has not been initialized. + * + * @see MediaTimestamp + */ + @Nullable + public abstract MediaTimestamp getTimestamp(); + + /** + * Gets the current playback position. + * + * @return the current position in milliseconds + */ + public abstract int getCurrentPosition(); + + /** + * Gets the duration of the file. + * + * @return the duration in milliseconds, if no duration is available + * (for example, if streaming live content), -1 is returned. + */ + public abstract int getDuration(); + + /** + * Gets the media metadata. + * + * @param update_only controls whether the full set of available + * metadata is returned or just the set that changed since the + * last call. See {@see #METADATA_UPDATE_ONLY} and {@see + * #METADATA_ALL}. + * + * @param apply_filter if true only metadata that matches the + * filter is returned. See {@see #APPLY_METADATA_FILTER} and {@see + * #BYPASS_METADATA_FILTER}. + * + * @return The metadata, possibly empty. null if an error occured. + // FIXME: unhide. + * {@hide} + */ + public Metadata getMetadata(final boolean update_only, + final boolean apply_filter) { + return null; + } + + /** + * Set a filter for the metadata update notification and update + * retrieval. The caller provides 2 set of metadata keys, allowed + * and blocked. The blocked set always takes precedence over the + * allowed one. + * Metadata.MATCH_ALL and Metadata.MATCH_NONE are 2 sets available as + * shorthands to allow/block all or no metadata. + * + * By default, there is no filter set. + * + * @param allow Is the set of metadata the client is interested + * in receiving new notifications for. + * @param block Is the set of metadata the client is not interested + * in receiving new notifications for. + * @return The call status code. + * + // FIXME: unhide. + * {@hide} + */ + public int setMetadataFilter(Set allow, Set block) { + return 0; + } + + /** + * Set the MediaPlayer2 to start when this MediaPlayer2 finishes playback + * (i.e. reaches the end of the stream). + * The media framework will attempt to transition from this player to + * the next as seamlessly as possible. The next player can be set at + * any time before completion, but shall be after setDataSource has been + * called successfully. The next player must be prepared by the + * app, and the application should not call play() on it. + * The next MediaPlayer2 must be different from 'this'. An exception + * will be thrown if next == this. + * The application may call setNextMediaPlayer(null) to indicate no + * next player should be started at the end of playback. + * If the current player is looping, it will keep looping and the next + * player will not be started. + * + * @param next the player to start after this one completes playback. + * + * @hide + */ + public void setNextMediaPlayer(MediaPlayer2 next) { } + + /** + * Resets the MediaPlayer2 to its uninitialized state. After calling + * this method, you will have to initialize it again by setting the + * data source and calling prepareAsync(). + */ + public abstract void reset(); + + /** + * Set up a timer for {@link #TimeProvider}. {@link #TimeProvider} will be + * notified when the presentation time reaches (becomes greater than or equal to) + * the value specified. + * + * @param mediaTimeUs presentation time to get timed event callback at + * @hide + */ + public void notifyAt(long mediaTimeUs) { } + + /** + * Sets the audio attributes for this MediaPlayer2. + * See {@link AudioAttributes} for how to build and configure an instance of this class. + * You must call this method before {@link #prepareAsync()} in order + * for the audio attributes to become effective thereafter. + * @param attributes a non-null set of audio attributes + * @throws IllegalArgumentException if the attributes are null or invalid. + */ + public abstract void setAudioAttributes(AudioAttributes attributes); + + /** + * Sets the player to be looping or non-looping. + * + * @param looping whether to loop or not + * @hide + */ + public void setLooping(boolean looping) { } + + /** + * Checks whether the MediaPlayer2 is looping or non-looping. + * + * @return true if the MediaPlayer2 is currently looping, false otherwise + * @hide + */ + public boolean isLooping() { + return false; + } + + /** + * Sets the volume on this player. + * This API is recommended for balancing the output of audio streams + * within an application. Unless you are writing an application to + * control user settings, this API should be used in preference to + * {@link AudioManager#setStreamVolume(int, int, int)} which sets the volume of ALL streams of + * a particular type. Note that the passed volume values are raw scalars in range 0.0 to 1.0. + * UI controls should be scaled logarithmically. + * + * @param leftVolume left volume scalar + * @param rightVolume right volume scalar + */ + /* + * FIXME: Merge this into javadoc comment above when setVolume(float) is not @hide. + * The single parameter form below is preferred if the channel volumes don't need + * to be set independently. + */ + public abstract void setVolume(float leftVolume, float rightVolume); + + /** + * Similar, excepts sets volume of all channels to same value. + * @hide + */ + public void setVolume(float volume) { } + + /** + * Sets the audio session ID. + * + * @param sessionId the audio session ID. + * The audio session ID is a system wide unique identifier for the audio stream played by + * this MediaPlayer2 instance. + * The primary use of the audio session ID is to associate audio effects to a particular + * instance of MediaPlayer2: if an audio session ID is provided when creating an audio effect, + * this effect will be applied only to the audio content of media players within the same + * audio session and not to the output mix. + * When created, a MediaPlayer2 instance automatically generates its own audio session ID. + * However, it is possible to force this player to be part of an already existing audio session + * by calling this method. + * This method must be called before one of the overloaded setDataSource methods. + * @throws IllegalStateException if it is called in an invalid state + * @throws IllegalArgumentException if the sessionId is invalid. + */ + public abstract void setAudioSessionId(int sessionId); + + /** + * Returns the audio session ID. + * + * @return the audio session ID. {@see #setAudioSessionId(int)} + * Note that the audio session ID is 0 only if a problem occured when the MediaPlayer2 was contructed. + */ + public abstract int getAudioSessionId(); + + /** + * Attaches an auxiliary effect to the player. A typical auxiliary effect is a reverberation + * effect which can be applied on any sound source that directs a certain amount of its + * energy to this effect. This amount is defined by setAuxEffectSendLevel(). + * See {@link #setAuxEffectSendLevel(float)}. + *

    After creating an auxiliary effect (e.g. + * {@link android.media.audiofx.EnvironmentalReverb}), retrieve its ID with + * {@link android.media.audiofx.AudioEffect#getId()} and use it when calling this method + * to attach the player to the effect. + *

    To detach the effect from the player, call this method with a null effect id. + *

    This method must be called after one of the overloaded setDataSource + * methods. + * @param effectId system wide unique id of the effect to attach + */ + public abstract void attachAuxEffect(int effectId); + + + /** + * Sets the send level of the player to the attached auxiliary effect. + * See {@link #attachAuxEffect(int)}. The level value range is 0 to 1.0. + *

    By default the send level is 0, so even if an effect is attached to the player + * this method must be called for the effect to be applied. + *

    Note that the passed level value is a raw scalar. UI controls should be scaled + * logarithmically: the gain applied by audio framework ranges from -72dB to 0dB, + * so an appropriate conversion from linear UI input x to level is: + * x == 0 -> level = 0 + * 0 < x <= R -> level = 10^(72*(x-R)/20/R) + * @param level send level scalar + */ + public abstract void setAuxEffectSendLevel(float level); + + /** + * Class for MediaPlayer2 to return each audio/video/subtitle track's metadata. + * + * @see android.media.MediaPlayer2#getTrackInfo + */ + public abstract static class TrackInfo { + /** + * Gets the track type. + * @return TrackType which indicates if the track is video, audio, timed text. + */ + public abstract int getTrackType(); + + /** + * Gets the language code of the track. + * @return a language code in either way of ISO-639-1 or ISO-639-2. + * When the language is unknown or could not be determined, + * ISO-639-2 language code, "und", is returned. + */ + public abstract String getLanguage(); + + /** + * Gets the {@link MediaFormat} of the track. If the format is + * unknown or could not be determined, null is returned. + */ + public abstract MediaFormat getFormat(); + + public static final int MEDIA_TRACK_TYPE_UNKNOWN = 0; + public static final int MEDIA_TRACK_TYPE_VIDEO = 1; + public static final int MEDIA_TRACK_TYPE_AUDIO = 2; + + /** @hide */ + public static final int MEDIA_TRACK_TYPE_TIMEDTEXT = 3; + + public static final int MEDIA_TRACK_TYPE_SUBTITLE = 4; + public static final int MEDIA_TRACK_TYPE_METADATA = 5; + + @Override + public abstract String toString(); + }; + + /** + * Returns a List of track information. + * + * @return List of track info. The total number of tracks is the array length. + * Must be called again if an external timed text source has been added after + * addTimedTextSource method is called. + * @throws IllegalStateException if it is called in an invalid state. + */ + public abstract List getTrackInfo(); + + /* Do not change these values without updating their counterparts + * in include/media/stagefright/MediaDefs.h and media/libstagefright/MediaDefs.cpp! + */ + /** + * MIME type for SubRip (SRT) container. Used in addTimedTextSource APIs. + * @hide + */ + public static final String MEDIA_MIMETYPE_TEXT_SUBRIP = "application/x-subrip"; + + /** + * MIME type for WebVTT subtitle data. + * @hide + */ + public static final String MEDIA_MIMETYPE_TEXT_VTT = "text/vtt"; + + /** + * MIME type for CEA-608 closed caption data. + * @hide + */ + public static final String MEDIA_MIMETYPE_TEXT_CEA_608 = "text/cea-608"; + + /** + * MIME type for CEA-708 closed caption data. + * @hide + */ + public static final String MEDIA_MIMETYPE_TEXT_CEA_708 = "text/cea-708"; + + /** @hide */ + public void setSubtitleAnchor( + SubtitleController controller, + SubtitleController.Anchor anchor) { } + + /** @hide */ + @Override + public void onSubtitleTrackSelected(SubtitleTrack track) { } + + /** @hide */ + public void addSubtitleSource(InputStream is, MediaFormat format) { } + + /* TODO: Limit the total number of external timed text source to a reasonable number. + */ + /** + * Adds an external timed text source file. + * + * Currently supported format is SubRip with the file extension .srt, case insensitive. + * Note that a single external timed text source may contain multiple tracks in it. + * One can find the total number of available tracks using {@link #getTrackInfo()} to see what + * additional tracks become available after this method call. + * + * @param path The file path of external timed text source file. + * @param mimeType The mime type of the file. Must be one of the mime types listed above. + * @throws IOException if the file cannot be accessed or is corrupted. + * @throws IllegalArgumentException if the mimeType is not supported. + * @throws IllegalStateException if called in an invalid state. + * @hide + */ + public void addTimedTextSource(String path, String mimeType) throws IOException { } + + /** + * Adds an external timed text source file (Uri). + * + * Currently supported format is SubRip with the file extension .srt, case insensitive. + * Note that a single external timed text source may contain multiple tracks in it. + * One can find the total number of available tracks using {@link #getTrackInfo()} to see what + * additional tracks become available after this method call. + * + * @param context the Context to use when resolving the Uri + * @param uri the Content URI of the data you want to play + * @param mimeType The mime type of the file. Must be one of the mime types listed above. + * @throws IOException if the file cannot be accessed or is corrupted. + * @throws IllegalArgumentException if the mimeType is not supported. + * @throws IllegalStateException if called in an invalid state. + * @hide + */ + public void addTimedTextSource(Context context, Uri uri, String mimeType) throws IOException { } + + /** + * Adds an external timed text source file (FileDescriptor). + * + * It is the caller's responsibility to close the file descriptor. + * It is safe to do so as soon as this call returns. + * + * Currently supported format is SubRip. Note that a single external timed text source may + * contain multiple tracks in it. One can find the total number of available tracks + * using {@link #getTrackInfo()} to see what additional tracks become available + * after this method call. + * + * @param fd the FileDescriptor for the file you want to play + * @param mimeType The mime type of the file. Must be one of the mime types listed above. + * @throws IllegalArgumentException if the mimeType is not supported. + * @throws IllegalStateException if called in an invalid state. + * @hide + */ + public void addTimedTextSource(FileDescriptor fd, String mimeType) { } + + /** + * Adds an external timed text file (FileDescriptor). + * + * It is the caller's responsibility to close the file descriptor. + * It is safe to do so as soon as this call returns. + * + * Currently supported format is SubRip. Note that a single external timed text source may + * contain multiple tracks in it. One can find the total number of available tracks + * using {@link #getTrackInfo()} to see what additional tracks become available + * after this method call. + * + * @param fd the FileDescriptor for the file you want to play + * @param offset the offset into the file where the data to be played starts, in bytes + * @param length the length in bytes of the data to be played + * @param mime The mime type of the file. Must be one of the mime types listed above. + * @throws IllegalArgumentException if the mimeType is not supported. + * @throws IllegalStateException if called in an invalid state. + * @hide + */ + public abstract void addTimedTextSource(FileDescriptor fd, long offset, long length, String mime); + + /** + * Returns the index of the audio, video, or subtitle track currently selected for playback, + * The return value is an index into the array returned by {@link #getTrackInfo()}, and can + * be used in calls to {@link #selectTrack(int)} or {@link #deselectTrack(int)}. + * + * @param trackType should be one of {@link TrackInfo#MEDIA_TRACK_TYPE_VIDEO}, + * {@link TrackInfo#MEDIA_TRACK_TYPE_AUDIO}, or + * {@link TrackInfo#MEDIA_TRACK_TYPE_SUBTITLE} + * @return index of the audio, video, or subtitle track currently selected for playback; + * a negative integer is returned when there is no selected track for {@code trackType} or + * when {@code trackType} is not one of audio, video, or subtitle. + * @throws IllegalStateException if called after {@link #close()} + * + * @see #getTrackInfo() + * @see #selectTrack(int) + * @see #deselectTrack(int) + */ + public abstract int getSelectedTrack(int trackType); + + /** + * Selects a track. + *

    + * If a MediaPlayer2 is in invalid state, it throws an IllegalStateException exception. + * If a MediaPlayer2 is in Started state, the selected track is presented immediately. + * If a MediaPlayer2 is not in Started state, it just marks the track to be played. + *

    + *

    + * In any valid state, if it is called multiple times on the same type of track (ie. Video, + * Audio, Timed Text), the most recent one will be chosen. + *

    + *

    + * The first audio and video tracks are selected by default if available, even though + * this method is not called. However, no timed text track will be selected until + * this function is called. + *

    + *

    + * Currently, only timed text tracks or audio tracks can be selected via this method. + * In addition, the support for selecting an audio track at runtime is pretty limited + * in that an audio track can only be selected in the Prepared state. + *

    + * @param index the index of the track to be selected. The valid range of the index + * is 0..total number of track - 1. The total number of tracks as well as the type of + * each individual track can be found by calling {@link #getTrackInfo()} method. + * @throws IllegalStateException if called in an invalid state. + * + * @see android.media.MediaPlayer2#getTrackInfo + */ + public abstract void selectTrack(int index); + + /** + * Deselect a track. + *

    + * Currently, the track must be a timed text track and no audio or video tracks can be + * deselected. If the timed text track identified by index has not been + * selected before, it throws an exception. + *

    + * @param index the index of the track to be deselected. The valid range of the index + * is 0..total number of tracks - 1. The total number of tracks as well as the type of + * each individual track can be found by calling {@link #getTrackInfo()} method. + * @throws IllegalStateException if called in an invalid state. + * + * @see android.media.MediaPlayer2#getTrackInfo + */ + public abstract void deselectTrack(int index); + + /** + * Sets the target UDP re-transmit endpoint for the low level player. + * Generally, the address portion of the endpoint is an IP multicast + * address, although a unicast address would be equally valid. When a valid + * retransmit endpoint has been set, the media player will not decode and + * render the media presentation locally. Instead, the player will attempt + * to re-multiplex its media data using the Android@Home RTP profile and + * re-transmit to the target endpoint. Receiver devices (which may be + * either the same as the transmitting device or different devices) may + * instantiate, prepare, and start a receiver player using a setDataSource + * URL of the form... + * + * aahRX://<multicastIP>:<port> + * + * to receive, decode and render the re-transmitted content. + * + * setRetransmitEndpoint may only be called before setDataSource has been + * called; while the player is in the Idle state. + * + * @param endpoint the address and UDP port of the re-transmission target or + * null if no re-transmission is to be performed. + * @throws IllegalStateException if it is called in an invalid state + * @throws IllegalArgumentException if the retransmit endpoint is supplied, + * but invalid. + * + * {@hide} pending API council + */ + public void setRetransmitEndpoint(InetSocketAddress endpoint) { } + + /** + * Releases the resources held by this {@code MediaPlayer2} object. + * + * It is considered good practice to call this method when you're + * done using the MediaPlayer2. In particular, whenever an Activity + * of an application is paused (its onPause() method is called), + * or stopped (its onStop() method is called), this method should be + * invoked to release the MediaPlayer2 object, unless the application + * has a special need to keep the object around. In addition to + * unnecessary resources (such as memory and instances of codecs) + * being held, failure to call this method immediately if a + * MediaPlayer2 object is no longer needed may also lead to + * continuous battery consumption for mobile devices, and playback + * failure for other applications if no multiple instances of the + * same codec are supported on a device. Even if multiple instances + * of the same codec are supported, some performance degradation + * may be expected when unnecessary multiple instances are used + * at the same time. + * + * {@code close()} may be safely called after a prior {@code close()}. + * This class implements the Java {@code AutoCloseable} interface and + * may be used with try-with-resources. + */ + @Override + public abstract void close(); + + /** @hide */ + public MediaTimeProvider getMediaTimeProvider() { + return null; + } + + /** + * Interface definition for callbacks to be invoked when the player has the corresponding + * events. + */ + public abstract static class EventCallback { + /** + * Called to update status in buffering a media source received through + * progressive downloading. The received buffering percentage + * indicates how much of the content has been buffered or played. + * For example a buffering update of 80 percent when half the content + * has already been played indicates that the next 30 percent of the + * content to play has been buffered. + * + * @param mp the MediaPlayer2 the update pertains to + * @param srcId the Id of this data source + * @param percent the percentage (0-100) of the content + * that has been buffered or played thus far + */ + public void onBufferingUpdate(MediaPlayer2 mp, long srcId, int percent) { } + + /** + * Called to indicate the video size + * + * The video size (width and height) could be 0 if there was no video, + * no display surface was set, or the value was not determined yet. + * + * @param mp the MediaPlayer2 associated with this callback + * @param srcId the Id of this data source + * @param width the width of the video + * @param height the height of the video + */ + public void onVideoSizeChanged(MediaPlayer2 mp, long srcId, int width, int height) { } + + /** + * Called to indicate an avaliable timed text + * + * @param mp the MediaPlayer2 associated with this callback + * @param srcId the Id of this data source + * @param text the timed text sample which contains the text + * needed to be displayed and the display format. + * @hide + */ + public void onTimedText(MediaPlayer2 mp, long srcId, TimedText text) { } + + /** + * Called to indicate avaliable timed metadata + *

    + * This method will be called as timed metadata is extracted from the media, + * in the same order as it occurs in the media. The timing of this event is + * not controlled by the associated timestamp. + *

    + * Currently only HTTP live streaming data URI's embedded with timed ID3 tags generates + * {@link TimedMetaData}. + * + * @see MediaPlayer2#selectTrack(int) + * @see MediaPlayer2.OnTimedMetaDataAvailableListener + * @see TimedMetaData + * + * @param mp the MediaPlayer2 associated with this callback + * @param srcId the Id of this data source + * @param data the timed metadata sample associated with this event + */ + public void onTimedMetaDataAvailable(MediaPlayer2 mp, long srcId, TimedMetaData data) { } + + /** + * Called to indicate an error. + * + * @param mp the MediaPlayer2 the error pertains to + * @param srcId the Id of this data source + * @param what the type of error that has occurred: + *

      + *
    • {@link #MEDIA_ERROR_UNKNOWN} + *
    + * @param extra an extra code, specific to the error. Typically + * implementation dependent. + *
      + *
    • {@link #MEDIA_ERROR_IO} + *
    • {@link #MEDIA_ERROR_MALFORMED} + *
    • {@link #MEDIA_ERROR_UNSUPPORTED} + *
    • {@link #MEDIA_ERROR_TIMED_OUT} + *
    • MEDIA_ERROR_SYSTEM (-2147483648) - low-level system error. + *
    + */ + public void onError(MediaPlayer2 mp, long srcId, int what, int extra) { } + + /** + * Called to indicate an info or a warning. + * + * @param mp the MediaPlayer2 the info pertains to. + * @param srcId the Id of this data source + * @param what the type of info or warning. + *
      + *
    • {@link #MEDIA_INFO_UNKNOWN} + *
    • {@link #MEDIA_INFO_STARTED_AS_NEXT} + *
    • {@link #MEDIA_INFO_VIDEO_RENDERING_START} + *
    • {@link #MEDIA_INFO_AUDIO_RENDERING_START} + *
    • {@link #MEDIA_INFO_PLAYBACK_COMPLETE} + *
    • {@link #MEDIA_INFO_PLAYLIST_END} + *
    • {@link #MEDIA_INFO_PREPARED} + *
    • {@link #MEDIA_INFO_COMPLETE_CALL_PLAY} + *
    • {@link #MEDIA_INFO_COMPLETE_CALL_PAUSE} + *
    • {@link #MEDIA_INFO_COMPLETE_CALL_SEEK} + *
    • {@link #MEDIA_INFO_VIDEO_TRACK_LAGGING} + *
    • {@link #MEDIA_INFO_BUFFERING_START} + *
    • {@link #MEDIA_INFO_BUFFERING_END} + *
    • MEDIA_INFO_NETWORK_BANDWIDTH (703) - + * bandwidth information is available (as extra kbps) + *
    • {@link #MEDIA_INFO_BAD_INTERLEAVING} + *
    • {@link #MEDIA_INFO_NOT_SEEKABLE} + *
    • {@link #MEDIA_INFO_METADATA_UPDATE} + *
    • {@link #MEDIA_INFO_UNSUPPORTED_SUBTITLE} + *
    • {@link #MEDIA_INFO_SUBTITLE_TIMED_OUT} + *
    + * @param extra an extra code, specific to the info. Typically + * implementation dependent. + */ + public void onInfo(MediaPlayer2 mp, long srcId, int what, int extra) { } + } + + /** + * Register a callback to be invoked when the media source is ready + * for playback. + * + * @param eventCallback the callback that will be run + * @param executor the executor through which the callback should be invoked + */ + public abstract void registerEventCallback(@NonNull @CallbackExecutor Executor executor, + @NonNull EventCallback eventCallback); + + /** + * Unregisters an {@link EventCallback}. + * + * @param callback an {@link EventCallback} to unregister + */ + public abstract void unregisterEventCallback(EventCallback callback); + + /** + * Interface definition of a callback to be invoked when a + * track has data available. + * + * @hide + */ + public interface OnSubtitleDataListener + { + public void onSubtitleData(MediaPlayer2 mp, SubtitleData data); + } + + /** + * Register a callback to be invoked when a track has data available. + * + * @param listener the callback that will be run + * + * @hide + */ + public void setOnSubtitleDataListener(OnSubtitleDataListener listener) { } + + + /* Do not change these values without updating their counterparts + * in include/media/mediaplayer2.h! + */ + /** Unspecified media player error. + * @see android.media.MediaPlayer2.EventCallback.onError + */ + public static final int MEDIA_ERROR_UNKNOWN = 1; + + /** The video is streamed and its container is not valid for progressive + * playback i.e the video's index (e.g moov atom) is not at the start of the + * file. + * @see android.media.MediaPlayer2.EventCallback.onError + */ + public static final int MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK = 200; + + /** File or network related operation errors. */ + public static final int MEDIA_ERROR_IO = -1004; + /** Bitstream is not conforming to the related coding standard or file spec. */ + public static final int MEDIA_ERROR_MALFORMED = -1007; + /** Bitstream is conforming to the related coding standard or file spec, but + * the media framework does not support the feature. */ + public static final int MEDIA_ERROR_UNSUPPORTED = -1010; + /** Some operation takes too long to complete, usually more than 3-5 seconds. */ + public static final int MEDIA_ERROR_TIMED_OUT = -110; + + /** Unspecified low-level system error. This value originated from UNKNOWN_ERROR in + * system/core/include/utils/Errors.h + * @see android.media.MediaPlayer2.EventCallback.onError + * @hide + */ + public static final int MEDIA_ERROR_SYSTEM = -2147483648; + + + /* Do not change these values without updating their counterparts + * in include/media/mediaplayer2.h! + */ + /** Unspecified media player info. + * @see android.media.MediaPlayer2.EventCallback.onInfo + */ + public static final int MEDIA_INFO_UNKNOWN = 1; + + /** The player switched to this datas source because it is the + * next-to-be-played in the play list. + * @see android.media.MediaPlayer2.EventCallback.onInfo + */ + public static final int MEDIA_INFO_STARTED_AS_NEXT = 2; + + /** The player just pushed the very first video frame for rendering. + * @see android.media.MediaPlayer2.EventCallback.onInfo + */ + public static final int MEDIA_INFO_VIDEO_RENDERING_START = 3; + + /** The player just rendered the very first audio sample. + * @see android.media.MediaPlayer2.EventCallback.onInfo + */ + public static final int MEDIA_INFO_AUDIO_RENDERING_START = 4; + + /** The player just completed the playback of this data source. + * @see android.media.MediaPlayer2.EventCallback.onInfo + */ + public static final int MEDIA_INFO_PLAYBACK_COMPLETE = 5; + + /** The player just completed the playback of the full play list. + * @see android.media.MediaPlayer2.EventCallback.onInfo + */ + public static final int MEDIA_INFO_PLAYLIST_END = 6; + + /** The player just prepared a data source. + * This also serves as call completion notification for {@link #prepareAsync()}. + * @see android.media.MediaPlayer2.EventCallback.onInfo + */ + public static final int MEDIA_INFO_PREPARED = 100; + + /** The player just completed a call {@link #play()}. + * @see android.media.MediaPlayer2.EventCallback.onInfo + */ + public static final int MEDIA_INFO_COMPLETE_CALL_PLAY = 101; + + /** The player just completed a call {@link #pause()}. + * @see android.media.MediaPlayer2.EventCallback.onInfo + */ + public static final int MEDIA_INFO_COMPLETE_CALL_PAUSE = 102; + + /** The player just completed a call {@link #seekTo(long, int)}. + * @see android.media.MediaPlayer2.EventCallback.onInfo + */ + public static final int MEDIA_INFO_COMPLETE_CALL_SEEK = 103; + + /** The video is too complex for the decoder: it can't decode frames fast + * enough. Possibly only the audio plays fine at this stage. + * @see android.media.MediaPlayer2.EventCallback.onInfo + */ + public static final int MEDIA_INFO_VIDEO_TRACK_LAGGING = 700; + + /** MediaPlayer2 is temporarily pausing playback internally in order to + * buffer more data. + * @see android.media.MediaPlayer2.EventCallback.onInfo + */ + public static final int MEDIA_INFO_BUFFERING_START = 701; + + /** MediaPlayer2 is resuming playback after filling buffers. + * @see android.media.MediaPlayer2.EventCallback.onInfo + */ + public static final int MEDIA_INFO_BUFFERING_END = 702; + + /** Estimated network bandwidth information (kbps) is available; currently this event fires + * simultaneously as {@link #MEDIA_INFO_BUFFERING_START} and {@link #MEDIA_INFO_BUFFERING_END} + * when playing network files. + * @see android.media.MediaPlayer2.EventCallback.onInfo + * @hide + */ + public static final int MEDIA_INFO_NETWORK_BANDWIDTH = 703; + + /** Bad interleaving means that a media has been improperly interleaved or + * not interleaved at all, e.g has all the video samples first then all the + * audio ones. Video is playing but a lot of disk seeks may be happening. + * @see android.media.MediaPlayer2.EventCallback.onInfo + */ + public static final int MEDIA_INFO_BAD_INTERLEAVING = 800; + + /** The media cannot be seeked (e.g live stream) + * @see android.media.MediaPlayer2.EventCallback.onInfo + */ + public static final int MEDIA_INFO_NOT_SEEKABLE = 801; + + /** A new set of metadata is available. + * @see android.media.MediaPlayer2.EventCallback.onInfo + */ + public static final int MEDIA_INFO_METADATA_UPDATE = 802; + + /** A new set of external-only metadata is available. Used by + * JAVA framework to avoid triggering track scanning. + * @hide + */ + public static final int MEDIA_INFO_EXTERNAL_METADATA_UPDATE = 803; + + /** Informs that audio is not playing. Note that playback of the video + * is not interrupted. + * @see android.media.MediaPlayer2.EventCallback.onInfo + */ + public static final int MEDIA_INFO_AUDIO_NOT_PLAYING = 804; + + /** Informs that video is not playing. Note that playback of the audio + * is not interrupted. + * @see android.media.MediaPlayer2.EventCallback.onInfo + */ + public static final int MEDIA_INFO_VIDEO_NOT_PLAYING = 805; + + /** Failed to handle timed text track properly. + * @see android.media.MediaPlayer2.EventCallback.onInfo + * + * {@hide} + */ + public static final int MEDIA_INFO_TIMED_TEXT_ERROR = 900; + + /** Subtitle track was not supported by the media framework. + * @see android.media.MediaPlayer2.EventCallback.onInfo + */ + public static final int MEDIA_INFO_UNSUPPORTED_SUBTITLE = 901; + + /** Reading the subtitle track takes too long. + * @see android.media.MediaPlayer2.EventCallback.onInfo + */ + public static final int MEDIA_INFO_SUBTITLE_TIMED_OUT = 902; + + + // Modular DRM begin + + /** + * Interface definition of a callback to be invoked when the app + * can do DRM configuration (get/set properties) before the session + * is opened. This facilitates configuration of the properties, like + * 'securityLevel', which has to be set after DRM scheme creation but + * before the DRM session is opened. + * + * The only allowed DRM calls in this listener are {@code getDrmPropertyString} + * and {@code setDrmPropertyString}. + */ + public interface OnDrmConfigHelper + { + /** + * Called to give the app the opportunity to configure DRM before the session is created + * + * @param mp the {@code MediaPlayer2} associated with this callback + */ + public void onDrmConfig(MediaPlayer2 mp); + } + + /** + * Register a callback to be invoked for configuration of the DRM object before + * the session is created. + * The callback will be invoked synchronously during the execution + * of {@link #prepareDrm(UUID uuid)}. + * + * @param listener the callback that will be run + */ + public abstract void setOnDrmConfigHelper(OnDrmConfigHelper listener); + + /** + * Interface definition for callbacks to be invoked when the player has the corresponding + * DRM events. + */ + public abstract static class DrmEventCallback { + /** + * Called to indicate DRM info is available + * + * @param mp the {@code MediaPlayer2} associated with this callback + * @param drmInfo DRM info of the source including PSSH, and subset + * of crypto schemes supported by this device + */ + public void onDrmInfo(MediaPlayer2 mp, DrmInfo drmInfo) { } + + /** + * Called to notify the client that {@code prepareDrm} is finished and ready for key request/response. + * + * @param mp the {@code MediaPlayer2} associated with this callback + * @param status the result of DRM preparation which can be + * {@link #PREPARE_DRM_STATUS_SUCCESS}, + * {@link #PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR}, + * {@link #PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR}, or + * {@link #PREPARE_DRM_STATUS_PREPARATION_ERROR}. + */ + public void onDrmPrepared(MediaPlayer2 mp, @PrepareDrmStatusCode int status) { } + + } + + /** + * Register a callback to be invoked when the media source is ready + * for playback. + * + * @param eventCallback the callback that will be run + * @param executor the executor through which the callback should be invoked + */ + public abstract void registerDrmEventCallback(@NonNull @CallbackExecutor Executor executor, + @NonNull DrmEventCallback eventCallback); + + /** + * Unregisters a {@link DrmEventCallback}. + * + * @param callback a {@link DrmEventCallback} to unregister + */ + public abstract void unregisterDrmEventCallback(DrmEventCallback callback); + + /** + * The status codes for {@link DrmEventCallback#onDrmPrepared} listener. + *

    + * + * DRM preparation has succeeded. + */ + public static final int PREPARE_DRM_STATUS_SUCCESS = 0; + + /** + * The device required DRM provisioning but couldn't reach the provisioning server. + */ + public static final int PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR = 1; + + /** + * The device required DRM provisioning but the provisioning server denied the request. + */ + public static final int PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR = 2; + + /** + * The DRM preparation has failed . + */ + public static final int PREPARE_DRM_STATUS_PREPARATION_ERROR = 3; + + + /** @hide */ + @IntDef({ + PREPARE_DRM_STATUS_SUCCESS, + PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR, + PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR, + PREPARE_DRM_STATUS_PREPARATION_ERROR, + }) + @Retention(RetentionPolicy.SOURCE) + public @interface PrepareDrmStatusCode {} + + /** + * Retrieves the DRM Info associated with the current source + * + * @throws IllegalStateException if called before being prepared + */ + public abstract DrmInfo getDrmInfo(); + + /** + * Prepares the DRM for the current source + *

    + * If {@code OnDrmConfigHelper} is registered, it will be called during + * preparation to allow configuration of the DRM properties before opening the + * DRM session. Note that the callback is called synchronously in the thread that called + * {@code prepareDrm}. It should be used only for a series of {@code getDrmPropertyString} + * and {@code setDrmPropertyString} calls and refrain from any lengthy operation. + *

    + * If the device has not been provisioned before, this call also provisions the device + * which involves accessing the provisioning server and can take a variable time to + * complete depending on the network connectivity. + * If {@code OnDrmPreparedListener} is registered, prepareDrm() runs in non-blocking + * mode by launching the provisioning in the background and returning. The listener + * will be called when provisioning and preparation has finished. If a + * {@code OnDrmPreparedListener} is not registered, prepareDrm() waits till provisioning + * and preparation has finished, i.e., runs in blocking mode. + *

    + * If {@code OnDrmPreparedListener} is registered, it is called to indicate the DRM + * session being ready. The application should not make any assumption about its call + * sequence (e.g., before or after prepareDrm returns), or the thread context that will + * execute the listener (unless the listener is registered with a handler thread). + *

    + * + * @param uuid The UUID of the crypto scheme. If not known beforehand, it can be retrieved + * from the source through {@code getDrmInfo} or registering a {@code onDrmInfoListener}. + * + * @throws IllegalStateException if called before being prepared or the DRM was + * prepared already + * @throws UnsupportedSchemeException if the crypto scheme is not supported + * @throws ResourceBusyException if required DRM resources are in use + * @throws ProvisioningNetworkErrorException if provisioning is required but failed due to a + * network error + * @throws ProvisioningServerErrorException if provisioning is required but failed due to + * the request denied by the provisioning server + */ + public abstract void prepareDrm(@NonNull UUID uuid) + throws UnsupportedSchemeException, ResourceBusyException, + ProvisioningNetworkErrorException, ProvisioningServerErrorException; + + /** + * Releases the DRM session + *

    + * The player has to have an active DRM session and be in stopped, or prepared + * state before this call is made. + * A {@code reset()} call will release the DRM session implicitly. + * + * @throws NoDrmSchemeException if there is no active DRM session to release + */ + public abstract void releaseDrm() throws NoDrmSchemeException; + + /** + * A key request/response exchange occurs between the app and a license server + * to obtain or release keys used to decrypt encrypted content. + *

    + * getKeyRequest() is used to obtain an opaque key request byte array that is + * delivered to the license server. The opaque key request byte array is returned + * in KeyRequest.data. The recommended URL to deliver the key request to is + * returned in KeyRequest.defaultUrl. + *

    + * After the app has received the key request response from the server, + * it should deliver to the response to the DRM engine plugin using the method + * {@link #provideKeyResponse}. + * + * @param keySetId is the key-set identifier of the offline keys being released when keyType is + * {@link MediaDrm#KEY_TYPE_RELEASE}. It should be set to null for other key requests, when + * keyType is {@link MediaDrm#KEY_TYPE_STREAMING} or {@link MediaDrm#KEY_TYPE_OFFLINE}. + * + * @param initData is the container-specific initialization data when the keyType is + * {@link MediaDrm#KEY_TYPE_STREAMING} or {@link MediaDrm#KEY_TYPE_OFFLINE}. Its meaning is + * interpreted based on the mime type provided in the mimeType parameter. It could + * contain, for example, the content ID, key ID or other data obtained from the content + * metadata that is required in generating the key request. + * When the keyType is {@link MediaDrm#KEY_TYPE_RELEASE}, it should be set to null. + * + * @param mimeType identifies the mime type of the content + * + * @param keyType specifies the type of the request. The request may be to acquire + * keys for streaming, {@link MediaDrm#KEY_TYPE_STREAMING}, or for offline content + * {@link MediaDrm#KEY_TYPE_OFFLINE}, or to release previously acquired + * keys ({@link MediaDrm#KEY_TYPE_RELEASE}), which are identified by a keySetId. + * + * @param optionalParameters are included in the key request message to + * allow a client application to provide additional message parameters to the server. + * This may be {@code null} if no additional parameters are to be sent. + * + * @throws NoDrmSchemeException if there is no active DRM session + */ + @NonNull + public abstract MediaDrm.KeyRequest getKeyRequest(@Nullable byte[] keySetId, @Nullable byte[] initData, + @Nullable String mimeType, @MediaDrm.KeyType int keyType, + @Nullable Map optionalParameters) + throws NoDrmSchemeException; + + /** + * A key response is received from the license server by the app, then it is + * provided to the DRM engine plugin using provideKeyResponse. When the + * response is for an offline key request, a key-set identifier is returned that + * can be used to later restore the keys to a new session with the method + * {@ link # restoreKeys}. + * When the response is for a streaming or release request, null is returned. + * + * @param keySetId When the response is for a release request, keySetId identifies + * the saved key associated with the release request (i.e., the same keySetId + * passed to the earlier {@ link # getKeyRequest} call. It MUST be null when the + * response is for either streaming or offline key requests. + * + * @param response the byte array response from the server + * + * @throws NoDrmSchemeException if there is no active DRM session + * @throws DeniedByServerException if the response indicates that the + * server rejected the request + */ + public abstract byte[] provideKeyResponse(@Nullable byte[] keySetId, @NonNull byte[] response) + throws NoDrmSchemeException, DeniedByServerException; + + /** + * Restore persisted offline keys into a new session. keySetId identifies the + * keys to load, obtained from a prior call to {@link #provideKeyResponse}. + * + * @param keySetId identifies the saved key set to restore + */ + public abstract void restoreKeys(@NonNull byte[] keySetId) + throws NoDrmSchemeException; + + /** + * Read a DRM engine plugin String property value, given the property name string. + *

    + * @param propertyName the property name + * + * Standard fields names are: + * {@link MediaDrm#PROPERTY_VENDOR}, {@link MediaDrm#PROPERTY_VERSION}, + * {@link MediaDrm#PROPERTY_DESCRIPTION}, {@link MediaDrm#PROPERTY_ALGORITHMS} + */ + @NonNull + public abstract String getDrmPropertyString(@NonNull @MediaDrm.StringProperty String propertyName) + throws NoDrmSchemeException; + + /** + * Set a DRM engine plugin String property value. + *

    + * @param propertyName the property name + * @param value the property value + * + * Standard fields names are: + * {@link MediaDrm#PROPERTY_VENDOR}, {@link MediaDrm#PROPERTY_VERSION}, + * {@link MediaDrm#PROPERTY_DESCRIPTION}, {@link MediaDrm#PROPERTY_ALGORITHMS} + */ + public abstract void setDrmPropertyString(@NonNull @MediaDrm.StringProperty String propertyName, + @NonNull String value) + throws NoDrmSchemeException; + + /** + * Encapsulates the DRM properties of the source. + */ + public abstract static class DrmInfo { + /** + * Returns the PSSH info of the data source for each supported DRM scheme. + */ + public abstract Map getPssh(); + + /** + * Returns the intersection of the data source and the device DRM schemes. + * It effectively identifies the subset of the source's DRM schemes which + * are supported by the device too. + */ + public abstract List getSupportedSchemes(); + }; // DrmInfo + + /** + * Thrown when a DRM method is called before preparing a DRM scheme through prepareDrm(). + * Extends MediaDrm.MediaDrmException + */ + public abstract static class NoDrmSchemeException extends MediaDrmException { + protected NoDrmSchemeException(String detailMessage) { + super(detailMessage); + } + } + + /** + * Thrown when the device requires DRM provisioning but the provisioning attempt has + * failed due to a network error (Internet reachability, timeout, etc.). + * Extends MediaDrm.MediaDrmException + */ + public abstract static class ProvisioningNetworkErrorException extends MediaDrmException { + protected ProvisioningNetworkErrorException(String detailMessage) { + super(detailMessage); + } + } + + /** + * Thrown when the device requires DRM provisioning but the provisioning attempt has + * failed due to the provisioning server denying the request. + * Extends MediaDrm.MediaDrmException + */ + public abstract static class ProvisioningServerErrorException extends MediaDrmException { + protected ProvisioningServerErrorException(String detailMessage) { + super(detailMessage); + } + } + + public static final class MetricsConstants { + private MetricsConstants() {} + + /** + * Key to extract the MIME type of the video track + * from the {@link MediaPlayer2#getMetrics} return value. + * The value is a String. + */ + public static final String MIME_TYPE_VIDEO = "android.media.mediaplayer.video.mime"; + + /** + * Key to extract the codec being used to decode the video track + * from the {@link MediaPlayer2#getMetrics} return value. + * The value is a String. + */ + public static final String CODEC_VIDEO = "android.media.mediaplayer.video.codec"; + + /** + * Key to extract the width (in pixels) of the video track + * from the {@link MediaPlayer2#getMetrics} return value. + * The value is an integer. + */ + public static final String WIDTH = "android.media.mediaplayer.width"; + + /** + * Key to extract the height (in pixels) of the video track + * from the {@link MediaPlayer2#getMetrics} return value. + * The value is an integer. + */ + public static final String HEIGHT = "android.media.mediaplayer.height"; + + /** + * Key to extract the count of video frames played + * from the {@link MediaPlayer2#getMetrics} return value. + * The value is an integer. + */ + public static final String FRAMES = "android.media.mediaplayer.frames"; + + /** + * Key to extract the count of video frames dropped + * from the {@link MediaPlayer2#getMetrics} return value. + * The value is an integer. + */ + public static final String FRAMES_DROPPED = "android.media.mediaplayer.dropped"; + + /** + * Key to extract the MIME type of the audio track + * from the {@link MediaPlayer2#getMetrics} return value. + * The value is a String. + */ + public static final String MIME_TYPE_AUDIO = "android.media.mediaplayer.audio.mime"; + + /** + * Key to extract the codec being used to decode the audio track + * from the {@link MediaPlayer2#getMetrics} return value. + * The value is a String. + */ + public static final String CODEC_AUDIO = "android.media.mediaplayer.audio.codec"; + + /** + * Key to extract the duration (in milliseconds) of the + * media being played + * from the {@link MediaPlayer2#getMetrics} return value. + * The value is a long. + */ + public static final String DURATION = "android.media.mediaplayer.durationMs"; + + /** + * Key to extract the playing time (in milliseconds) of the + * media being played + * from the {@link MediaPlayer2#getMetrics} return value. + * The value is a long. + */ + public static final String PLAYING = "android.media.mediaplayer.playingMs"; + + /** + * Key to extract the count of errors encountered while + * playing the media + * from the {@link MediaPlayer2#getMetrics} return value. + * The value is an integer. + */ + public static final String ERRORS = "android.media.mediaplayer.err"; + + /** + * Key to extract an (optional) error code detected while + * playing the media + * from the {@link MediaPlayer2#getMetrics} return value. + * The value is an integer. + */ + public static final String ERROR_CODE = "android.media.mediaplayer.errcode"; + + } +} diff --git a/android/media/MediaPlayer2Impl.java b/android/media/MediaPlayer2Impl.java new file mode 100644 index 00000000..86a285cc --- /dev/null +++ b/android/media/MediaPlayer2Impl.java @@ -0,0 +1,4899 @@ +/* + * Copyright 2018 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.CallbackExecutor; +import android.annotation.IntDef; +import android.annotation.NonNull; +import android.annotation.Nullable; +import android.app.ActivityThread; +import android.content.ContentProvider; +import android.content.ContentResolver; +import android.content.Context; +import android.content.res.AssetFileDescriptor; +import android.net.Uri; +import android.os.Bundle; +import android.os.Handler; +import android.os.HandlerThread; +import android.os.Looper; +import android.os.Message; +import android.os.Parcel; +import android.os.Parcelable; +import android.os.PersistableBundle; +import android.os.Process; +import android.os.PowerManager; +import android.os.SystemProperties; +import android.provider.Settings; +import android.system.ErrnoException; +import android.system.Os; +import android.system.OsConstants; +import android.util.Log; +import android.util.Pair; +import android.util.ArrayMap; +import android.view.Surface; +import android.view.SurfaceHolder; +import android.widget.VideoView; +import android.graphics.SurfaceTexture; +import android.media.AudioManager; +import android.media.MediaDrm; +import android.media.MediaFormat; +import android.media.MediaPlayer2; +import android.media.MediaTimeProvider; +import android.media.PlaybackParams; +import android.media.SubtitleController; +import android.media.SubtitleController.Anchor; +import android.media.SubtitleData; +import android.media.SubtitleTrack.RenderingWidget; +import android.media.SyncParams; + +import com.android.internal.annotations.GuardedBy; +import com.android.internal.util.Preconditions; + +import dalvik.system.CloseGuard; + +import libcore.io.IoBridge; +import libcore.io.Streams; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileDescriptor; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.lang.AutoCloseable; +import java.lang.Runnable; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.ref.WeakReference; +import java.net.CookieHandler; +import java.net.CookieManager; +import java.net.HttpCookie; +import java.net.HttpURLConnection; +import java.net.InetSocketAddress; +import java.net.URL; +import java.nio.ByteOrder; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.BitSet; +import java.util.Collections; +import java.util.concurrent.Executor; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Scanner; +import java.util.Set; +import java.util.UUID; +import java.util.Vector; + + +/** + * MediaPlayer2 class can be used to control playback + * of audio/video files and streams. An example on how to use the methods in + * this class can be found in {@link android.widget.VideoView}. + * + *

    Topics covered here are: + *

      + *
    1. State Diagram + *
    2. Valid and Invalid States + *
    3. Permissions + *
    4. Register informational and error callbacks + *
    + * + *
    + *

    Developer Guides

    + *

    For more information about how to use MediaPlayer2, read the + * Media Playback developer guide.

    + *
    + * + * + *

    State Diagram

    + * + *

    Playback control of audio/video files and streams is managed as a state + * machine. The following diagram shows the life cycle and the states of a + * MediaPlayer2 object driven by the supported playback control operations. + * The ovals represent the states a MediaPlayer2 object may reside + * in. The arcs represent the playback control operations that drive the object + * state transition. There are two types of arcs. The arcs with a single arrow + * head represent synchronous method calls, while those with + * a double arrow head represent asynchronous method calls.

    + * + *

    MediaPlayer State diagram

    + * + *

    From this state diagram, one can see that a MediaPlayer2 object has the + * following states:

    + *
      + *
    • When a MediaPlayer2 object is just created using new or + * after {@link #reset()} is called, it is in the Idle state; and after + * {@link #close()} is called, it is in the End state. Between these + * two states is the life cycle of the MediaPlayer2 object. + *
        + *
      • There is a subtle but important difference between a newly constructed + * MediaPlayer2 object and the MediaPlayer2 object after {@link #reset()} + * is called. It is a programming error to invoke methods such + * as {@link #getCurrentPosition()}, + * {@link #getDuration()}, {@link #getVideoHeight()}, + * {@link #getVideoWidth()}, {@link #setAudioAttributes(AudioAttributes)}, + * {@link #setLooping(boolean)}, + * {@link #setVolume(float, float)}, {@link #pause()}, {@link #play()}, + * {@link #seekTo(long, int)}, {@link #prepare()} or + * {@link #prepareAsync()} in the Idle state for both cases. If any of these + * methods is called right after a MediaPlayer2 object is constructed, + * the user supplied callback method OnErrorListener.onError() won't be + * called by the internal player engine and the object state remains + * unchanged; but if these methods are called right after {@link #reset()}, + * the user supplied callback method OnErrorListener.onError() will be + * invoked by the internal player engine and the object will be + * transfered to the Error state.
      • + *
      • It is also recommended that once + * a MediaPlayer2 object is no longer being used, call {@link #close()} immediately + * so that resources used by the internal player engine associated with the + * MediaPlayer2 object can be released immediately. Resource may include + * singleton resources such as hardware acceleration components and + * failure to call {@link #close()} may cause subsequent instances of + * MediaPlayer2 objects to fallback to software implementations or fail + * altogether. Once the MediaPlayer2 + * object is in the End state, it can no longer be used and + * there is no way to bring it back to any other state.
      • + *
      • Furthermore, + * the MediaPlayer2 objects created using new is in the + * Idle state. + *
      • + *
      + *
    • + *
    • In general, some playback control operation may fail due to various + * reasons, such as unsupported audio/video format, poorly interleaved + * audio/video, resolution too high, streaming timeout, and the like. + * Thus, error reporting and recovery is an important concern under + * these circumstances. Sometimes, due to programming errors, invoking a playback + * control operation in an invalid state may also occur. Under all these + * error conditions, the internal player engine invokes a user supplied + * EventCallback.onError() method if an EventCallback has been + * registered beforehand via + * {@link #registerEventCallback(Executor, EventCallback)}. + *
        + *
      • It is important to note that once an error occurs, the + * MediaPlayer2 object enters the Error state (except as noted + * above), even if an error listener has not been registered by the application.
      • + *
      • In order to reuse a MediaPlayer2 object that is in the + * Error state and recover from the error, + * {@link #reset()} can be called to restore the object to its Idle + * state.
      • + *
      • It is good programming practice to have your application + * register a OnErrorListener to look out for error notifications from + * the internal player engine.
      • + *
      • IllegalStateException is + * thrown to prevent programming errors such as calling {@link #prepare()}, + * {@link #prepareAsync()}, {@link #setDataSource(DataSourceDesc)}, or + * {@code setPlaylist} methods in an invalid state.
      • + *
      + *
    • + *
    • Calling + * {@link #setDataSource(DataSourceDesc)}, or + * {@code setPlaylist} transfers a + * MediaPlayer2 object in the Idle state to the + * Initialized state. + *
        + *
      • An IllegalStateException is thrown if + * setDataSource() or setPlaylist() is called in any other state.
      • + *
      • It is good programming + * practice to always look out for IllegalArgumentException + * and IOException that may be thrown from + * setDataSource and setPlaylist methods.
      • + *
      + *
    • + *
    • A MediaPlayer2 object must first enter the Prepared state + * before playback can be started. + *
        + *
      • There are two ways (synchronous vs. + * asynchronous) that the Prepared state can be reached: + * either a call to {@link #prepare()} (synchronous) which + * transfers the object to the Prepared state once the method call + * returns, or a call to {@link #prepareAsync()} (asynchronous) which + * first transfers the object to the Preparing state after the + * call returns (which occurs almost right way) while the internal + * player engine continues working on the rest of preparation work + * until the preparation work completes. When the preparation completes or when {@link #prepare()} call returns, + * the internal player engine then calls a user supplied callback method, + * onPrepared() of the EventCallback interface, if an + * EventCallback is registered beforehand via {@link + * #registerEventCallback(Executor, EventCallback)}.
      • + *
      • It is important to note that + * the Preparing state is a transient state, and the behavior + * of calling any method with side effect while a MediaPlayer2 object is + * in the Preparing state is undefined.
      • + *
      • An IllegalStateException is + * thrown if {@link #prepare()} or {@link #prepareAsync()} is called in + * any other state.
      • + *
      • While in the Prepared state, properties + * such as audio/sound volume, screenOnWhilePlaying, looping can be + * adjusted by invoking the corresponding set methods.
      • + *
      + *
    • + *
    • To start the playback, {@link #play()} must be called. After + * {@link #play()} returns successfully, the MediaPlayer2 object is in the + * Started state. {@link #isPlaying()} can be called to test + * whether the MediaPlayer2 object is in the Started state. + *
        + *
      • While in the Started state, the internal player engine calls + * a user supplied EventCallback.onBufferingUpdate() callback + * method if an EventCallback has been registered beforehand + * via {@link #registerEventCallback(Executor, EventCallback)}. + * This callback allows applications to keep track of the buffering status + * while streaming audio/video.
      • + *
      • Calling {@link #play()} has not effect + * on a MediaPlayer2 object that is already in the Started state.
      • + *
      + *
    • + *
    • Playback can be paused and stopped, and the current playback position + * can be adjusted. Playback can be paused via {@link #pause()}. When the call to + * {@link #pause()} returns, the MediaPlayer2 object enters the + * Paused state. Note that the transition from the Started + * state to the Paused state and vice versa happens + * asynchronously in the player engine. It may take some time before + * the state is updated in calls to {@link #isPlaying()}, and it can be + * a number of seconds in the case of streamed content. + *
        + *
      • Calling {@link #play()} to resume playback for a paused + * MediaPlayer2 object, and the resumed playback + * position is the same as where it was paused. When the call to + * {@link #play()} returns, the paused MediaPlayer2 object goes back to + * the Started state.
      • + *
      • Calling {@link #pause()} has no effect on + * a MediaPlayer2 object that is already in the Paused state.
      • + *
      + *
    • + *
    • The playback position can be adjusted with a call to + * {@link #seekTo(long, int)}. + *
        + *
      • Although the asynchronuous {@link #seekTo(long, int)} + * call returns right away, the actual seek operation may take a while to + * finish, especially for audio/video being streamed. When the actual + * seek operation completes, the internal player engine calls a user + * supplied EventCallback.onSeekComplete() if an EventCallback + * has been registered beforehand via + * {@link #registerEventCallback(Executor, EventCallback)}.
      • + *
      • Please + * note that {@link #seekTo(long, int)} can also be called in the other states, + * such as Prepared, Paused and PlaybackCompleted + * state. When {@link #seekTo(long, int)} is called in those states, + * one video frame will be displayed if the stream has video and the requested + * position is valid. + *
      • + *
      • Furthermore, the actual current playback position + * can be retrieved with a call to {@link #getCurrentPosition()}, which + * is helpful for applications such as a Music player that need to keep + * track of the playback progress.
      • + *
      + *
    • + *
    • When the playback reaches the end of stream, the playback completes. + *
        + *
      • If the looping mode was being set to truewith + * {@link #setLooping(boolean)}, the MediaPlayer2 object shall remain in + * the Started state.
      • + *
      • If the looping mode was set to false + * , the player engine calls a user supplied callback method, + * EventCallback.onCompletion(), if an EventCallback is registered + * beforehand via {@link #registerEventCallback(Executor, EventCallback)}. + * The invoke of the callback signals that the object is now in the + * PlaybackCompleted state.
      • + *
      • While in the PlaybackCompleted + * state, calling {@link #play()} can restart the playback from the + * beginning of the audio/video source.
      • + *
      + * + * + * + *

      Valid and invalid states

      + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
      Method Name

      Valid Sates

      Invalid States

      Comments

      attachAuxEffect

      {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted}

      {Idle, Error}

      This method must be called after setDataSource or setPlaylist. + * Calling it does not change the object state.

      getAudioSessionId

      any

      {}

      This method can be called in any state and calling it does not change + * the object state.

      getCurrentPosition

      {Idle, Initialized, Prepared, Started, Paused, Stopped, + * PlaybackCompleted}

      {Error}

      Successful invoke of this method in a valid state does not change the + * state. Calling this method in an invalid state transfers the object + * to the Error state.

      getDuration

      {Prepared, Started, Paused, Stopped, PlaybackCompleted}

      {Idle, Initialized, Error}

      Successful invoke of this method in a valid state does not change the + * state. Calling this method in an invalid state transfers the object + * to the Error state.

      getVideoHeight

      {Idle, Initialized, Prepared, Started, Paused, Stopped, + * PlaybackCompleted}

      {Error}

      Successful invoke of this method in a valid state does not change the + * state. Calling this method in an invalid state transfers the object + * to the Error state.

      getVideoWidth

      {Idle, Initialized, Prepared, Started, Paused, Stopped, + * PlaybackCompleted}

      {Error}

      Successful invoke of this method in a valid state does not change + * the state. Calling this method in an invalid state transfers the + * object to the Error state.

      isPlaying

      {Idle, Initialized, Prepared, Started, Paused, Stopped, + * PlaybackCompleted}

      {Error}

      Successful invoke of this method in a valid state does not change + * the state. Calling this method in an invalid state transfers the + * object to the Error state.

      pause

      {Started, Paused, PlaybackCompleted}

      {Idle, Initialized, Prepared, Stopped, Error}

      Successful invoke of this method in a valid state transfers the + * object to the Paused state. Calling this method in an + * invalid state transfers the object to the Error state.

      prepare

      {Initialized, Stopped}

      {Idle, Prepared, Started, Paused, PlaybackCompleted, Error}

      Successful invoke of this method in a valid state transfers the + * object to the Prepared state. Calling this method in an + * invalid state throws an IllegalStateException.

      prepareAsync

      {Initialized, Stopped}

      {Idle, Prepared, Started, Paused, PlaybackCompleted, Error}

      Successful invoke of this method in a valid state transfers the + * object to the Preparing state. Calling this method in an + * invalid state throws an IllegalStateException.

      release

      any

      {}

      After {@link #close()}, the object is no longer available.

      reset

      {Idle, Initialized, Prepared, Started, Paused, Stopped, + * PlaybackCompleted, Error}

      {}

      After {@link #reset()}, the object is like being just created.

      seekTo

      {Prepared, Started, Paused, PlaybackCompleted}

      {Idle, Initialized, Stopped, Error}

      Successful invoke of this method in a valid state does not change + * the state. Calling this method in an invalid state transfers the + * object to the Error state.

      setAudioAttributes

      {Idle, Initialized, Stopped, Prepared, Started, Paused, + * PlaybackCompleted}

      {Error}

      Successful invoke of this method does not change the state. In order for the + * target audio attributes type to become effective, this method must be called before + * prepare() or prepareAsync().

      setAudioSessionId

      {Idle}

      {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, + * Error}

      This method must be called in idle state as the audio session ID must be known before + * calling setDataSource or setPlaylist. Calling it does not change the object + * state.

      setAudioStreamType (deprecated)

      {Idle, Initialized, Stopped, Prepared, Started, Paused, + * PlaybackCompleted}

      {Error}

      Successful invoke of this method does not change the state. In order for the + * target audio stream type to become effective, this method must be called before + * prepare() or prepareAsync().

      setAuxEffectSendLevel

      any

      {}

      Calling this method does not change the object state.

      setDataSource

      {Idle}

      {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, + * Error}

      Successful invoke of this method in a valid state transfers the + * object to the Initialized state. Calling this method in an + * invalid state throws an IllegalStateException.

      setPlaylist

      {Idle}

      {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted, + * Error}

      Successful invoke of this method in a valid state transfers the + * object to the Initialized state. Calling this method in an + * invalid state throws an IllegalStateException.

      setDisplay

      any

      {}

      This method can be called in any state and calling it does not change + * the object state.

      setSurface

      any

      {}

      This method can be called in any state and calling it does not change + * the object state.

      setVideoScalingMode

      {Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted}

      {Idle, Error}

      Successful invoke of this method does not change the state.

      setLooping

      {Idle, Initialized, Stopped, Prepared, Started, Paused, + * PlaybackCompleted}

      {Error}

      Successful invoke of this method in a valid state does not change + * the state. Calling this method in an + * invalid state transfers the object to the Error state.

      isLooping

      any

      {}

      This method can be called in any state and calling it does not change + * the object state.

      registerDrmEventCallback

      any

      {}

      This method can be called in any state and calling it does not change + * the object state.

      registerEventCallback

      any

      {}

      This method can be called in any state and calling it does not change + * the object state.

      setPlaybackParams

      {Initialized, Prepared, Started, Paused, PlaybackCompleted, Error}

      {Idle, Stopped}

      This method will change state in some cases, depending on when it's called. + *

      setScreenOnWhilePlayingany

      {}

      This method can be called in any state and calling it does not change + * the object state.

      setVolume

      {Idle, Initialized, Stopped, Prepared, Started, Paused, + * PlaybackCompleted}

      {Error}

      Successful invoke of this method does not change the state. + *
      setWakeMode

      any

      {}

      This method can be called in any state and calling it does not change + * the object state.

      start

      {Prepared, Started, Paused, PlaybackCompleted}

      {Idle, Initialized, Stopped, Error}

      Successful invoke of this method in a valid state transfers the + * object to the Started state. Calling this method in an + * invalid state transfers the object to the Error state.

      stop

      {Prepared, Started, Stopped, Paused, PlaybackCompleted}

      {Idle, Initialized, Error}

      Successful invoke of this method in a valid state transfers the + * object to the Stopped state. Calling this method in an + * invalid state transfers the object to the Error state.

      getTrackInfo

      {Prepared, Started, Stopped, Paused, PlaybackCompleted}

      {Idle, Initialized, Error}

      Successful invoke of this method does not change the state.

      addTimedTextSource

      {Prepared, Started, Stopped, Paused, PlaybackCompleted}

      {Idle, Initialized, Error}

      Successful invoke of this method does not change the state.

      selectTrack

      {Prepared, Started, Stopped, Paused, PlaybackCompleted}

      {Idle, Initialized, Error}

      Successful invoke of this method does not change the state.

      deselectTrack

      {Prepared, Started, Stopped, Paused, PlaybackCompleted}

      {Idle, Initialized, Error}

      Successful invoke of this method does not change the state.

      + * + * + *

      Permissions

      + *

      One may need to declare a corresponding WAKE_LOCK permission {@link + * android.R.styleable#AndroidManifestUsesPermission <uses-permission>} + * element. + * + *

      This class requires the {@link android.Manifest.permission#INTERNET} permission + * when used with network-based content. + * + * + *

      Callbacks

      + *

      Applications may want to register for informational and error + * events in order to be informed of some internal state update and + * possible runtime errors during playback or streaming. Registration for + * these events is done by properly setting the appropriate listeners (via calls + * to + * {@link #registerEventCallback(Executor, EventCallback)}, + * {@link #registerDrmEventCallback(Executor, DrmEventCallback)}). + * In order to receive the respective callback + * associated with these listeners, applications are required to create + * MediaPlayer2 objects on a thread with its own Looper running (main UI + * thread by default has a Looper running). + * + * @hide + */ +public final class MediaPlayer2Impl extends MediaPlayer2 { + static { + System.loadLibrary("media2_jni"); + native_init(); + } + + private final static String TAG = "MediaPlayer2Impl"; + + private long mNativeContext; // accessed by native methods + private long mNativeSurfaceTexture; // accessed by native methods + private int mListenerContext; // accessed by native methods + private SurfaceHolder mSurfaceHolder; + private EventHandler mEventHandler; + private PowerManager.WakeLock mWakeLock = null; + private boolean mScreenOnWhilePlaying; + private boolean mStayAwake; + private int mStreamType = AudioManager.USE_DEFAULT_STREAM_TYPE; + private int mUsage = -1; + private boolean mBypassInterruptionPolicy; + private final CloseGuard mGuard = CloseGuard.get(); + + private List mPlaylist; + private int mPLCurrentIndex = 0; + private int mPLNextIndex = -1; + private int mLoopingMode = LOOPING_MODE_NONE; + + // Modular DRM + private UUID mDrmUUID; + private final Object mDrmLock = new Object(); + private DrmInfoImpl mDrmInfoImpl; + private MediaDrm mDrmObj; + private byte[] mDrmSessionId; + private boolean mDrmInfoResolved; + private boolean mActiveDrmScheme; + private boolean mDrmConfigAllowed; + private boolean mDrmProvisioningInProgress; + private boolean mPrepareDrmInProgress; + private ProvisioningThread mDrmProvisioningThread; + + /** + * Default constructor. + *

      When done with the MediaPlayer2Impl, you should call {@link #close()}, + * to free the resources. If not released, too many MediaPlayer2Impl instances may + * result in an exception.

      + */ + public MediaPlayer2Impl() { + Looper looper; + if ((looper = Looper.myLooper()) != null) { + mEventHandler = new EventHandler(this, looper); + } else if ((looper = Looper.getMainLooper()) != null) { + mEventHandler = new EventHandler(this, looper); + } else { + mEventHandler = null; + } + + mTimeProvider = new TimeProvider(this); + mOpenSubtitleSources = new Vector(); + mGuard.open("close"); + + /* Native setup requires a weak reference to our object. + * It's easier to create it here than in C++. + */ + native_setup(new WeakReference(this)); + } + + /* + * Update the MediaPlayer2Impl SurfaceTexture. + * Call after setting a new display surface. + */ + private native void _setVideoSurface(Surface surface); + + /* Do not change these values (starting with INVOKE_ID) without updating + * their counterparts in include/media/mediaplayer2.h! + */ + private static final int INVOKE_ID_GET_TRACK_INFO = 1; + private static final int INVOKE_ID_ADD_EXTERNAL_SOURCE = 2; + private static final int INVOKE_ID_ADD_EXTERNAL_SOURCE_FD = 3; + private static final int INVOKE_ID_SELECT_TRACK = 4; + private static final int INVOKE_ID_DESELECT_TRACK = 5; + private static final int INVOKE_ID_SET_VIDEO_SCALE_MODE = 6; + private static final int INVOKE_ID_GET_SELECTED_TRACK = 7; + + /** + * Create a request parcel which can be routed to the native media + * player using {@link #invoke(Parcel, Parcel)}. The Parcel + * returned has the proper InterfaceToken set. The caller should + * not overwrite that token, i.e it can only append data to the + * Parcel. + * + * @return A parcel suitable to hold a request for the native + * player. + * {@hide} + */ + @Override + public Parcel newRequest() { + Parcel parcel = Parcel.obtain(); + return parcel; + } + + /** + * Invoke a generic method on the native player using opaque + * parcels for the request and reply. Both payloads' format is a + * convention between the java caller and the native player. + * Must be called after setDataSource or setPlaylist to make sure a native player + * exists. On failure, a RuntimeException is thrown. + * + * @param request Parcel with the data for the extension. The + * caller must use {@link #newRequest()} to get one. + * + * @param reply Output parcel with the data returned by the + * native player. + * {@hide} + */ + @Override + public void invoke(Parcel request, Parcel reply) { + int retcode = native_invoke(request, reply); + reply.setDataPosition(0); + if (retcode != 0) { + throw new RuntimeException("failure code: " + retcode); + } + } + + /** + * Sets the {@link SurfaceHolder} to use for displaying the video + * portion of the media. + * + * Either a surface holder or surface must be set if a display or video sink + * is needed. Not calling this method or {@link #setSurface(Surface)} + * when playing back a video will result in only the audio track being played. + * A null surface holder or surface will result in only the audio track being + * played. + * + * @param sh the SurfaceHolder to use for video display + * @throws IllegalStateException if the internal player engine has not been + * initialized or has been released. + * @hide + */ + @Override + public void setDisplay(SurfaceHolder sh) { + mSurfaceHolder = sh; + Surface surface; + if (sh != null) { + surface = sh.getSurface(); + } else { + surface = null; + } + _setVideoSurface(surface); + updateSurfaceScreenOn(); + } + + /** + * Sets the {@link Surface} to be used as the sink for the video portion of + * the media. This is similar to {@link #setDisplay(SurfaceHolder)}, but + * does not support {@link #setScreenOnWhilePlaying(boolean)}. Setting a + * Surface will un-set any Surface or SurfaceHolder that was previously set. + * A null surface will result in only the audio track being played. + * + * If the Surface sends frames to a {@link SurfaceTexture}, the timestamps + * returned from {@link SurfaceTexture#getTimestamp()} will have an + * unspecified zero point. These timestamps cannot be directly compared + * between different media sources, different instances of the same media + * source, or multiple runs of the same program. The timestamp is normally + * monotonically increasing and is unaffected by time-of-day adjustments, + * but it is reset when the position is set. + * + * @param surface The {@link Surface} to be used for the video portion of + * the media. + * @throws IllegalStateException if the internal player engine has not been + * initialized or has been released. + */ + @Override + public void setSurface(Surface surface) { + if (mScreenOnWhilePlaying && surface != null) { + Log.w(TAG, "setScreenOnWhilePlaying(true) is ineffective for Surface"); + } + mSurfaceHolder = null; + _setVideoSurface(surface); + updateSurfaceScreenOn(); + } + + /** + * Sets video scaling mode. To make the target video scaling mode + * effective during playback, this method must be called after + * data source is set. If not called, the default video + * scaling mode is {@link #VIDEO_SCALING_MODE_SCALE_TO_FIT}. + * + *

      The supported video scaling modes are: + *

        + *
      • {@link #VIDEO_SCALING_MODE_SCALE_TO_FIT} + *
      • {@link #VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING} + *
      + * + * @param mode target video scaling mode. Must be one of the supported + * video scaling modes; otherwise, IllegalArgumentException will be thrown. + * + * @see MediaPlayer2#VIDEO_SCALING_MODE_SCALE_TO_FIT + * @see MediaPlayer2#VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING + * @hide + */ + @Override + public void setVideoScalingMode(int mode) { + if (!isVideoScalingModeSupported(mode)) { + final String msg = "Scaling mode " + mode + " is not supported"; + throw new IllegalArgumentException(msg); + } + Parcel request = Parcel.obtain(); + Parcel reply = Parcel.obtain(); + try { + request.writeInt(INVOKE_ID_SET_VIDEO_SCALE_MODE); + request.writeInt(mode); + invoke(request, reply); + } finally { + request.recycle(); + reply.recycle(); + } + } + + /** + * Discards all pending commands. + */ + @Override + public void clearPendingCommands() { + } + + /** + * Sets the data source as described by a DataSourceDesc. + * + * @param dsd the descriptor of data source you want to play + * @throws IllegalStateException if it is called in an invalid state + * @throws NullPointerException if dsd is null + */ + @Override + public void setDataSource(@NonNull DataSourceDesc dsd) throws IOException { + Preconditions.checkNotNull(dsd, "the DataSourceDesc cannot be null"); + mPlaylist = Collections.synchronizedList(new ArrayList(1)); + mPlaylist.add(dsd); + mPLCurrentIndex = 0; + setDataSourcePriv(dsd); + } + + /** + * Gets the current data source as described by a DataSourceDesc. + * + * @return the current DataSourceDesc + */ + @Override + public DataSourceDesc getCurrentDataSource() { + if (mPlaylist == null) { + return null; + } + return mPlaylist.get(mPLCurrentIndex); + } + + /** + * Sets the play list. + * + * If startIndex falls outside play list range, it will be clamped to the nearest index + * in the play list. + * + * @param pl the play list of data source you want to play + * @param startIndex the index of the DataSourceDesc in the play list you want to play first + * @throws IllegalStateException if it is called in an invalid state + * @throws IllegalArgumentException if pl is null or empty, or pl contains null DataSourceDesc + */ + @Override + public void setPlaylist(@NonNull List pl, int startIndex) + throws IOException { + if (pl == null || pl.size() == 0) { + throw new IllegalArgumentException("play list cannot be null or empty."); + } + HashSet ids = new HashSet(pl.size()); + for (DataSourceDesc dsd : pl) { + if (dsd == null) { + throw new IllegalArgumentException("DataSourceDesc in play list cannot be null."); + } + if (ids.add(dsd.getId()) == false) { + throw new IllegalArgumentException("DataSourceDesc Id in play list should be unique."); + } + } + + if (startIndex < 0) { + startIndex = 0; + } else if (startIndex >= pl.size()) { + startIndex = pl.size() - 1; + } + + mPlaylist = Collections.synchronizedList(new ArrayList(pl)); + mPLCurrentIndex = startIndex; + setDataSourcePriv(mPlaylist.get(startIndex)); + // TODO: handle the preparation of next source in the play list. + // It should be processed after current source is prepared. + } + + /** + * Gets a copy of the play list. + * + * @return a copy of the play list used by {@link MediaPlayer2} + */ + @Override + public List getPlaylist() { + if (mPlaylist == null) { + return null; + } + return new ArrayList(mPlaylist); + } + + /** + * Sets the index of current DataSourceDesc in the play list to be played. + * + * @param index the index of DataSourceDesc in the play list you want to play + * @throws IllegalArgumentException if the play list is null + * @throws NullPointerException if index is outside play list range + */ + @Override + public void setCurrentPlaylistItem(int index) { + if (mPlaylist == null) { + throw new IllegalArgumentException("play list has not been set yet."); + } + if (index < 0 || index >= mPlaylist.size()) { + throw new IndexOutOfBoundsException("index is out of play list range."); + } + + if (index == mPLCurrentIndex) { + return; + } + + // TODO: in playing state, stop current source and start to play source of index. + mPLCurrentIndex = index; + } + + /** + * Sets the index of next-to-be-played DataSourceDesc in the play list. + * + * @param index the index of next-to-be-played DataSourceDesc in the play list + * @throws IllegalArgumentException if the play list is null + * @throws NullPointerException if index is outside play list range + */ + @Override + public void setNextPlaylistItem(int index) { + if (mPlaylist == null) { + throw new IllegalArgumentException("play list has not been set yet."); + } + if (index < 0 || index >= mPlaylist.size()) { + throw new IndexOutOfBoundsException("index is out of play list range."); + } + + if (index == mPLNextIndex) { + return; + } + + // TODO: prepare the new next-to-be-played DataSourceDesc + mPLNextIndex = index; + } + + /** + * Gets the current index of play list. + * + * @return the index of the current DataSourceDesc in the play list + */ + @Override + public int getCurrentPlaylistItemIndex() { + return mPLCurrentIndex; + } + + /** + * Sets the looping mode of the play list. + * The mode shall be one of {@link #LOOPING_MODE_NONE}, {@link #LOOPING_MODE_FULL}, + * {@link #LOOPING_MODE_SINGLE}, {@link #LOOPING_MODE_SHUFFLE}. + * + * @param mode the mode in which the play list will be played + * @throws IllegalArgumentException if mode is not supported + */ + @Override + public void setLoopingMode(@LoopingMode int mode) { + if (mode != LOOPING_MODE_NONE + && mode != LOOPING_MODE_FULL + && mode != LOOPING_MODE_SINGLE + && mode != LOOPING_MODE_SHUFFLE) { + throw new IllegalArgumentException("mode is not supported."); + } + mLoopingMode = mode; + if (mPlaylist == null) { + return; + } + + // TODO: handle the new mode if necessary. + } + + /** + * Gets the looping mode of play list. + * + * @return the looping mode of the play list + */ + @Override + public int getLoopingMode() { + return mPLCurrentIndex; + } + + /** + * Moves the DataSourceDesc at indexFrom in the play list to indexTo. + * + * @throws IllegalArgumentException if the play list is null + * @throws IndexOutOfBoundsException if indexFrom or indexTo is outside play list range + */ + @Override + public void movePlaylistItem(int indexFrom, int indexTo) { + if (mPlaylist == null) { + throw new IllegalArgumentException("play list has not been set yet."); + } + // TODO: move the DataSourceDesc from indexFrom to indexTo. + } + + /** + * Removes the DataSourceDesc at index in the play list. + * + * If index is same as the current index of the play list, current DataSourceDesc + * will be stopped and playback moves to next source in the list. + * + * @return the removed DataSourceDesc at index in the play list + * @throws IllegalArgumentException if the play list is null + * @throws IndexOutOfBoundsException if index is outside play list range + */ + @Override + public DataSourceDesc removePlaylistItem(int index) { + if (mPlaylist == null) { + throw new IllegalArgumentException("play list has not been set yet."); + } + + DataSourceDesc oldDsd = mPlaylist.remove(index); + // TODO: if index == mPLCurrentIndex, stop current source and move to next one. + // if index == mPLNextIndex, prepare the new next-to-be-played source. + return oldDsd; + } + + /** + * Inserts the DataSourceDesc to the play list at position index. + * + * This will not change the DataSourceDesc currently being played. + * If index is less than or equal to the current index of the play list, + * the current index of the play list will be incremented correspondingly. + * + * @param index the index you want to add dsd to the play list + * @param dsd the descriptor of data source you want to add to the play list + * @throws IndexOutOfBoundsException if index is outside play list range + * @throws NullPointerException if dsd is null + */ + @Override + public void addPlaylistItem(int index, DataSourceDesc dsd) { + Preconditions.checkNotNull(dsd, "the DataSourceDesc cannot be null"); + + if (mPlaylist == null) { + if (index == 0) { + mPlaylist = Collections.synchronizedList(new ArrayList()); + mPlaylist.add(dsd); + mPLCurrentIndex = 0; + return; + } + throw new IllegalArgumentException("index should be 0 for first DataSourceDesc."); + } + + long id = dsd.getId(); + for (DataSourceDesc pldsd : mPlaylist) { + if (id == pldsd.getId()) { + throw new IllegalArgumentException("Id of dsd already exists in the play list."); + } + } + + mPlaylist.add(index, dsd); + if (index <= mPLCurrentIndex) { + ++mPLCurrentIndex; + } + } + + /** + * replaces the DataSourceDesc at index in the play list with given dsd. + * + * When index is same as the current index of the play list, the current source + * will be stopped and the new source will be played, except that if new + * and old source only differ on end position and current media position is + * smaller then the new end position. + * + * This will not change the DataSourceDesc currently being played. + * If index is less than or equal to the current index of the play list, + * the current index of the play list will be incremented correspondingly. + * + * @param index the index you want to add dsd to the play list + * @param dsd the descriptor of data source you want to add to the play list + * @throws IndexOutOfBoundsException if index is outside play list range + * @throws NullPointerException if dsd is null + */ + @Override + public DataSourceDesc editPlaylistItem(int index, DataSourceDesc dsd) { + Preconditions.checkNotNull(dsd, "the DataSourceDesc cannot be null"); + Preconditions.checkNotNull(mPlaylist, "the play list cannot be null"); + + long id = dsd.getId(); + for (int i = 0; i < mPlaylist.size(); ++i) { + if (i == index) { + continue; + } + if (id == mPlaylist.get(i).getId()) { + throw new IllegalArgumentException("Id of dsd already exists in the play list."); + } + } + + // TODO: if needed, stop playback of current source, and start new dsd. + DataSourceDesc oldDsd = mPlaylist.set(index, dsd); + return mPlaylist.set(index, dsd); + } + + private void setDataSourcePriv(@NonNull DataSourceDesc dsd) throws IOException { + Preconditions.checkNotNull(dsd, "the DataSourceDesc cannot be null"); + + switch (dsd.getType()) { + case DataSourceDesc.TYPE_CALLBACK: + setDataSourcePriv(dsd.getId(), + dsd.getMedia2DataSource()); + break; + + case DataSourceDesc.TYPE_FD: + setDataSourcePriv(dsd.getId(), + dsd.getFileDescriptor(), + dsd.getFileDescriptorOffset(), + dsd.getFileDescriptorLength()); + break; + + case DataSourceDesc.TYPE_URI: + setDataSourcePriv(dsd.getId(), + dsd.getUriContext(), + dsd.getUri(), + dsd.getUriHeaders(), + dsd.getUriCookies()); + break; + + default: + break; + } + } + + /** + * To provide cookies for the subsequent HTTP requests, you can install your own default cookie + * handler and use other variants of setDataSource APIs instead. Alternatively, you can use + * this API to pass the cookies as a list of HttpCookie. If the app has not installed + * a CookieHandler already, this API creates a CookieManager and populates its CookieStore with + * the provided cookies. If the app has installed its own handler already, this API requires the + * handler to be of CookieManager type such that the API can update the manager’s CookieStore. + * + *

      Note that the cross domain redirection is allowed by default, + * but that can be changed with key/value pairs through the headers parameter with + * "android-allow-cross-domain-redirect" as the key and "0" or "1" as the value to + * disallow or allow cross domain redirection. + * + * @throws IllegalArgumentException if cookies are provided and the installed handler is not + * a CookieManager + * @throws IllegalStateException if it is called in an invalid state + * @throws NullPointerException if context or uri is null + * @throws IOException if uri has a file scheme and an I/O error occurs + */ + private void setDataSourcePriv(long srcId, @NonNull Context context, @NonNull Uri uri, + @Nullable Map headers, @Nullable List cookies) + throws IOException { + if (context == null) { + throw new NullPointerException("context param can not be null."); + } + + if (uri == null) { + throw new NullPointerException("uri param can not be null."); + } + + if (cookies != null) { + CookieHandler cookieHandler = CookieHandler.getDefault(); + if (cookieHandler != null && !(cookieHandler instanceof CookieManager)) { + throw new IllegalArgumentException("The cookie handler has to be of CookieManager " + + "type when cookies are provided."); + } + } + + // The context and URI usually belong to the calling user. Get a resolver for that user + // and strip out the userId from the URI if present. + final ContentResolver resolver = context.getContentResolver(); + final String scheme = uri.getScheme(); + final String authority = ContentProvider.getAuthorityWithoutUserId(uri.getAuthority()); + if (ContentResolver.SCHEME_FILE.equals(scheme)) { + setDataSourcePriv(srcId, uri.getPath(), null, null); + return; + } else if (ContentResolver.SCHEME_CONTENT.equals(scheme) + && Settings.AUTHORITY.equals(authority)) { + // Try cached ringtone first since the actual provider may not be + // encryption aware, or it may be stored on CE media storage + final int type = RingtoneManager.getDefaultType(uri); + final Uri cacheUri = RingtoneManager.getCacheForType(type, context.getUserId()); + final Uri actualUri = RingtoneManager.getActualDefaultRingtoneUri(context, type); + if (attemptDataSource(srcId, resolver, cacheUri)) { + return; + } else if (attemptDataSource(srcId, resolver, actualUri)) { + return; + } else { + setDataSourcePriv(srcId, uri.toString(), headers, cookies); + } + } else { + // Try requested Uri locally first, or fallback to media server + if (attemptDataSource(srcId, resolver, uri)) { + return; + } else { + setDataSourcePriv(srcId, uri.toString(), headers, cookies); + } + } + } + + private boolean attemptDataSource(long srcId, ContentResolver resolver, Uri uri) { + try (AssetFileDescriptor afd = resolver.openAssetFileDescriptor(uri, "r")) { + if (afd.getDeclaredLength() < 0) { + setDataSourcePriv(srcId, afd.getFileDescriptor(), 0, DataSourceDesc.LONG_MAX); + } else { + setDataSourcePriv(srcId, + afd.getFileDescriptor(), + afd.getStartOffset(), + afd.getDeclaredLength()); + } + return true; + } catch (NullPointerException | SecurityException | IOException ex) { + Log.w(TAG, "Couldn't open " + uri + ": " + ex); + return false; + } + } + + private void setDataSourcePriv( + long srcId, String path, Map headers, List cookies) + throws IOException, IllegalArgumentException, SecurityException, IllegalStateException + { + String[] keys = null; + String[] values = null; + + if (headers != null) { + keys = new String[headers.size()]; + values = new String[headers.size()]; + + int i = 0; + for (Map.Entry entry: headers.entrySet()) { + keys[i] = entry.getKey(); + values[i] = entry.getValue(); + ++i; + } + } + setDataSourcePriv(srcId, path, keys, values, cookies); + } + + private void setDataSourcePriv(long srcId, String path, String[] keys, String[] values, + List cookies) + throws IOException, IllegalArgumentException, SecurityException, IllegalStateException { + final Uri uri = Uri.parse(path); + final String scheme = uri.getScheme(); + if ("file".equals(scheme)) { + path = uri.getPath(); + } else if (scheme != null) { + // handle non-file sources + nativeSetDataSource( + Media2HTTPService.createHTTPService(path, cookies), + path, + keys, + values); + return; + } + + final File file = new File(path); + if (file.exists()) { + FileInputStream is = new FileInputStream(file); + FileDescriptor fd = is.getFD(); + setDataSourcePriv(srcId, fd, 0, DataSourceDesc.LONG_MAX); + is.close(); + } else { + throw new IOException("setDataSourcePriv failed."); + } + } + + private native void nativeSetDataSource( + Media2HTTPService httpService, String path, String[] keys, String[] values) + throws IOException, IllegalArgumentException, SecurityException, IllegalStateException; + + /** + * Sets the data source (FileDescriptor) to use. The FileDescriptor must be + * seekable (N.B. a LocalSocket is not seekable). It is the caller's responsibility + * to close the file descriptor. It is safe to do so as soon as this call returns. + * + * @throws IllegalStateException if it is called in an invalid state + * @throws IllegalArgumentException if fd is not a valid FileDescriptor + * @throws IOException if fd can not be read + */ + private void setDataSourcePriv(long srcId, FileDescriptor fd, long offset, long length) + throws IOException { + _setDataSource(fd, offset, length); + } + + private native void _setDataSource(FileDescriptor fd, long offset, long length) + throws IOException; + + /** + * @throws IllegalStateException if it is called in an invalid state + * @throws IllegalArgumentException if dataSource is not a valid Media2DataSource + */ + private void setDataSourcePriv(long srcId, Media2DataSource dataSource) { + _setDataSource(dataSource); + } + + private native void _setDataSource(Media2DataSource dataSource); + + /** + * Prepares the player for playback, synchronously. + * + * After setting the datasource and the display surface, you need to either + * call prepare() or prepareAsync(). For files, it is OK to call prepare(), + * which blocks until MediaPlayer2 is ready for playback. + * + * @throws IOException if source can not be accessed + * @throws IllegalStateException if it is called in an invalid state + * @hide + */ + @Override + public void prepare() throws IOException { + _prepare(); + scanInternalSubtitleTracks(); + + // DrmInfo, if any, has been resolved by now. + synchronized (mDrmLock) { + mDrmInfoResolved = true; + } + } + + private native void _prepare() throws IOException, IllegalStateException; + + /** + * Prepares the player for playback, asynchronously. + * + * After setting the datasource and the display surface, you need to either + * call prepare() or prepareAsync(). For streams, you should call prepareAsync(), + * which returns immediately, rather than blocking until enough data has been + * buffered. + * + * @throws IllegalStateException if it is called in an invalid state + */ + @Override + public native void prepareAsync(); + + /** + * Starts or resumes playback. If playback had previously been paused, + * playback will continue from where it was paused. If playback had + * been stopped, or never started before, playback will start at the + * beginning. + * + * @throws IllegalStateException if it is called in an invalid state + */ + @Override + public void play() { + stayAwake(true); + _start(); + } + + private native void _start() throws IllegalStateException; + + + private int getAudioStreamType() { + if (mStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { + mStreamType = _getAudioStreamType(); + } + return mStreamType; + } + + private native int _getAudioStreamType() throws IllegalStateException; + + /** + * Stops playback after playback has been started or paused. + * + * @throws IllegalStateException if the internal player engine has not been + * initialized. + * #hide + */ + @Override + public void stop() { + stayAwake(false); + _stop(); + } + + private native void _stop() throws IllegalStateException; + + /** + * Pauses playback. Call play() to resume. + * + * @throws IllegalStateException if the internal player engine has not been + * initialized. + */ + @Override + public void pause() { + stayAwake(false); + _pause(); + } + + private native void _pause() throws IllegalStateException; + + //-------------------------------------------------------------------------- + // Explicit Routing + //-------------------- + private AudioDeviceInfo mPreferredDevice = null; + + /** + * Specifies an audio device (via an {@link AudioDeviceInfo} object) to route + * the output from this MediaPlayer2. + * @param deviceInfo The {@link AudioDeviceInfo} specifying the audio sink or source. + * If deviceInfo is null, default routing is restored. + * @return true if succesful, false if the specified {@link AudioDeviceInfo} is non-null and + * does not correspond to a valid audio device. + */ + @Override + public boolean setPreferredDevice(AudioDeviceInfo deviceInfo) { + if (deviceInfo != null && !deviceInfo.isSink()) { + return false; + } + int preferredDeviceId = deviceInfo != null ? deviceInfo.getId() : 0; + boolean status = native_setOutputDevice(preferredDeviceId); + if (status == true) { + synchronized (this) { + mPreferredDevice = deviceInfo; + } + } + return status; + } + + /** + * Returns the selected output specified by {@link #setPreferredDevice}. Note that this + * is not guaranteed to correspond to the actual device being used for playback. + */ + @Override + public AudioDeviceInfo getPreferredDevice() { + synchronized (this) { + return mPreferredDevice; + } + } + + /** + * Returns an {@link AudioDeviceInfo} identifying the current routing of this MediaPlayer2 + * Note: The query is only valid if the MediaPlayer2 is currently playing. + * If the player is not playing, the returned device can be null or correspond to previously + * selected device when the player was last active. + */ + @Override + public AudioDeviceInfo getRoutedDevice() { + int deviceId = native_getRoutedDeviceId(); + if (deviceId == 0) { + return null; + } + AudioDeviceInfo[] devices = + AudioManager.getDevicesStatic(AudioManager.GET_DEVICES_OUTPUTS); + for (int i = 0; i < devices.length; i++) { + if (devices[i].getId() == deviceId) { + return devices[i]; + } + } + return null; + } + + /* + * Call BEFORE adding a routing callback handler or AFTER removing a routing callback handler. + */ + private void enableNativeRoutingCallbacksLocked(boolean enabled) { + if (mRoutingChangeListeners.size() == 0) { + native_enableDeviceCallback(enabled); + } + } + + /** + * The list of AudioRouting.OnRoutingChangedListener interfaces added (with + * {@link #addOnRoutingChangedListener(android.media.AudioRouting.OnRoutingChangedListener, Handler)} + * by an app to receive (re)routing notifications. + */ + @GuardedBy("mRoutingChangeListeners") + private ArrayMap mRoutingChangeListeners = new ArrayMap<>(); + + /** + * Adds an {@link AudioRouting.OnRoutingChangedListener} to receive notifications of routing + * changes on this MediaPlayer2. + * @param listener The {@link AudioRouting.OnRoutingChangedListener} interface to receive + * notifications of rerouting events. + * @param handler Specifies the {@link Handler} object for the thread on which to execute + * the callback. If null, the handler on the main looper will be used. + */ + @Override + public void addOnRoutingChangedListener(AudioRouting.OnRoutingChangedListener listener, + Handler handler) { + synchronized (mRoutingChangeListeners) { + if (listener != null && !mRoutingChangeListeners.containsKey(listener)) { + enableNativeRoutingCallbacksLocked(true); + mRoutingChangeListeners.put( + listener, new NativeRoutingEventHandlerDelegate(this, listener, + handler != null ? handler : mEventHandler)); + } + } + } + + /** + * Removes an {@link AudioRouting.OnRoutingChangedListener} which has been previously added + * to receive rerouting notifications. + * @param listener The previously added {@link AudioRouting.OnRoutingChangedListener} interface + * to remove. + */ + @Override + public void removeOnRoutingChangedListener(AudioRouting.OnRoutingChangedListener listener) { + synchronized (mRoutingChangeListeners) { + if (mRoutingChangeListeners.containsKey(listener)) { + mRoutingChangeListeners.remove(listener); + enableNativeRoutingCallbacksLocked(false); + } + } + } + + private native final boolean native_setOutputDevice(int deviceId); + private native final int native_getRoutedDeviceId(); + private native final void native_enableDeviceCallback(boolean enabled); + + /** + * Set the low-level power management behavior for this MediaPlayer2. This + * can be used when the MediaPlayer2 is not playing through a SurfaceHolder + * set with {@link #setDisplay(SurfaceHolder)} and thus can use the + * high-level {@link #setScreenOnWhilePlaying(boolean)} feature. + * + *

      This function has the MediaPlayer2 access the low-level power manager + * service to control the device's power usage while playing is occurring. + * The parameter is a combination of {@link android.os.PowerManager} wake flags. + * Use of this method requires {@link android.Manifest.permission#WAKE_LOCK} + * permission. + * By default, no attempt is made to keep the device awake during playback. + * + * @param context the Context to use + * @param mode the power/wake mode to set + * @see android.os.PowerManager + * @hide + */ + @Override + public void setWakeMode(Context context, int mode) { + boolean washeld = false; + + /* Disable persistant wakelocks in media player based on property */ + if (SystemProperties.getBoolean("audio.offload.ignore_setawake", false) == true) { + Log.w(TAG, "IGNORING setWakeMode " + mode); + return; + } + + if (mWakeLock != null) { + if (mWakeLock.isHeld()) { + washeld = true; + mWakeLock.release(); + } + mWakeLock = null; + } + + PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); + mWakeLock = pm.newWakeLock(mode|PowerManager.ON_AFTER_RELEASE, MediaPlayer2Impl.class.getName()); + mWakeLock.setReferenceCounted(false); + if (washeld) { + mWakeLock.acquire(); + } + } + + /** + * Control whether we should use the attached SurfaceHolder to keep the + * screen on while video playback is occurring. This is the preferred + * method over {@link #setWakeMode} where possible, since it doesn't + * require that the application have permission for low-level wake lock + * access. + * + * @param screenOn Supply true to keep the screen on, false to allow it + * to turn off. + * @hide + */ + @Override + public void setScreenOnWhilePlaying(boolean screenOn) { + if (mScreenOnWhilePlaying != screenOn) { + if (screenOn && mSurfaceHolder == null) { + Log.w(TAG, "setScreenOnWhilePlaying(true) is ineffective without a SurfaceHolder"); + } + mScreenOnWhilePlaying = screenOn; + updateSurfaceScreenOn(); + } + } + + private void stayAwake(boolean awake) { + if (mWakeLock != null) { + if (awake && !mWakeLock.isHeld()) { + mWakeLock.acquire(); + } else if (!awake && mWakeLock.isHeld()) { + mWakeLock.release(); + } + } + mStayAwake = awake; + updateSurfaceScreenOn(); + } + + private void updateSurfaceScreenOn() { + if (mSurfaceHolder != null) { + mSurfaceHolder.setKeepScreenOn(mScreenOnWhilePlaying && mStayAwake); + } + } + + /** + * Returns the width of the video. + * + * @return the width of the video, or 0 if there is no video, + * no display surface was set, or the width has not been determined + * yet. The {@code EventCallback} can be registered via + * {@link #registerEventCallback(Executor, EventCallback)} to provide a + * notification {@code EventCallback.onVideoSizeChanged} when the width is available. + */ + @Override + public native int getVideoWidth(); + + /** + * Returns the height of the video. + * + * @return the height of the video, or 0 if there is no video, + * no display surface was set, or the height has not been determined + * yet. The {@code EventCallback} can be registered via + * {@link #registerEventCallback(Executor, EventCallback)} to provide a + * notification {@code EventCallback.onVideoSizeChanged} when the height is available. + */ + @Override + public native int getVideoHeight(); + + /** + * Return Metrics data about the current player. + * + * @return a {@link PersistableBundle} containing the set of attributes and values + * available for the media being handled by this instance of MediaPlayer2 + * The attributes are descibed in {@link MetricsConstants}. + * + * Additional vendor-specific fields may also be present in + * the return value. + */ + @Override + public PersistableBundle getMetrics() { + PersistableBundle bundle = native_getMetrics(); + return bundle; + } + + private native PersistableBundle native_getMetrics(); + + /** + * Checks whether the MediaPlayer2 is playing. + * + * @return true if currently playing, false otherwise + * @throws IllegalStateException if the internal player engine has not been + * initialized or has been released. + */ + @Override + public native boolean isPlaying(); + + /** + * Gets the current buffering management params used by the source component. + * Calling it only after {@code setDataSource} has been called. + * Each type of data source might have different set of default params. + * + * @return the current buffering management params used by the source component. + * @throws IllegalStateException if the internal player engine has not been + * initialized, or {@code setDataSource} has not been called. + * @hide + */ + @Override + @NonNull + public native BufferingParams getBufferingParams(); + + /** + * Sets buffering management params. + * The object sets its internal BufferingParams to the input, except that the input is + * invalid or not supported. + * Call it only after {@code setDataSource} has been called. + * The input is a hint to MediaPlayer2. + * + * @param params the buffering management params. + * + * @throws IllegalStateException if the internal player engine has not been + * initialized or has been released, or {@code setDataSource} has not been called. + * @throws IllegalArgumentException if params is invalid or not supported. + * @hide + */ + @Override + public native void setBufferingParams(@NonNull BufferingParams params); + + /** + * Sets playback rate and audio mode. + * + * @param rate the ratio between desired playback rate and normal one. + * @param audioMode audio playback mode. Must be one of the supported + * audio modes. + * + * @throws IllegalStateException if the internal player engine has not been + * initialized. + * @throws IllegalArgumentException if audioMode is not supported. + * + * @hide + */ + @Override + @NonNull + public PlaybackParams easyPlaybackParams(float rate, @PlaybackRateAudioMode int audioMode) { + PlaybackParams params = new PlaybackParams(); + params.allowDefaults(); + switch (audioMode) { + case PLAYBACK_RATE_AUDIO_MODE_DEFAULT: + params.setSpeed(rate).setPitch(1.0f); + break; + case PLAYBACK_RATE_AUDIO_MODE_STRETCH: + params.setSpeed(rate).setPitch(1.0f) + .setAudioFallbackMode(params.AUDIO_FALLBACK_MODE_FAIL); + break; + case PLAYBACK_RATE_AUDIO_MODE_RESAMPLE: + params.setSpeed(rate).setPitch(rate); + break; + default: + final String msg = "Audio playback mode " + audioMode + " is not supported"; + throw new IllegalArgumentException(msg); + } + return params; + } + + /** + * Sets playback rate using {@link PlaybackParams}. The object sets its internal + * PlaybackParams to the input, except that the object remembers previous speed + * when input speed is zero. This allows the object to resume at previous speed + * when play() is called. Calling it before the object is prepared does not change + * the object state. After the object is prepared, calling it with zero speed is + * equivalent to calling pause(). After the object is prepared, calling it with + * non-zero speed is equivalent to calling play(). + * + * @param params the playback params. + * + * @throws IllegalStateException if the internal player engine has not been + * initialized or has been released. + * @throws IllegalArgumentException if params is not supported. + */ + @Override + public native void setPlaybackParams(@NonNull PlaybackParams params); + + /** + * Gets the playback params, containing the current playback rate. + * + * @return the playback params. + * @throws IllegalStateException if the internal player engine has not been + * initialized. + */ + @Override + @NonNull + public native PlaybackParams getPlaybackParams(); + + /** + * Sets A/V sync mode. + * + * @param params the A/V sync params to apply + * + * @throws IllegalStateException if the internal player engine has not been + * initialized. + * @throws IllegalArgumentException if params are not supported. + */ + @Override + public native void setSyncParams(@NonNull SyncParams params); + + /** + * Gets the A/V sync mode. + * + * @return the A/V sync params + * + * @throws IllegalStateException if the internal player engine has not been + * initialized. + */ + @Override + @NonNull + public native SyncParams getSyncParams(); + + private native final void _seekTo(long msec, int mode); + + /** + * Moves the media to specified time position by considering the given mode. + *

      + * When seekTo is finished, the user will be notified via OnSeekComplete supplied by the user. + * There is at most one active seekTo processed at any time. If there is a to-be-completed + * seekTo, new seekTo requests will be queued in such a way that only the last request + * is kept. When current seekTo is completed, the queued request will be processed if + * that request is different from just-finished seekTo operation, i.e., the requested + * position or mode is different. + * + * @param msec the offset in milliseconds from the start to seek to. + * When seeking to the given time position, there is no guarantee that the data source + * has a frame located at the position. When this happens, a frame nearby will be rendered. + * If msec is negative, time position zero will be used. + * If msec is larger than duration, duration will be used. + * @param mode the mode indicating where exactly to seek to. + * Use {@link #SEEK_PREVIOUS_SYNC} if one wants to seek to a sync frame + * that has a timestamp earlier than or the same as msec. Use + * {@link #SEEK_NEXT_SYNC} if one wants to seek to a sync frame + * that has a timestamp later than or the same as msec. Use + * {@link #SEEK_CLOSEST_SYNC} if one wants to seek to a sync frame + * that has a timestamp closest to or the same as msec. Use + * {@link #SEEK_CLOSEST} if one wants to seek to a frame that may + * or may not be a sync frame but is closest to or the same as msec. + * {@link #SEEK_CLOSEST} often has larger performance overhead compared + * to the other options if there is no sync frame located at msec. + * @throws IllegalStateException if the internal player engine has not been + * initialized + * @throws IllegalArgumentException if the mode is invalid. + */ + @Override + public void seekTo(long msec, @SeekMode int mode) { + if (mode < SEEK_PREVIOUS_SYNC || mode > SEEK_CLOSEST) { + final String msg = "Illegal seek mode: " + mode; + throw new IllegalArgumentException(msg); + } + // TODO: pass long to native, instead of truncating here. + if (msec > Integer.MAX_VALUE) { + Log.w(TAG, "seekTo offset " + msec + " is too large, cap to " + Integer.MAX_VALUE); + msec = Integer.MAX_VALUE; + } else if (msec < Integer.MIN_VALUE) { + Log.w(TAG, "seekTo offset " + msec + " is too small, cap to " + Integer.MIN_VALUE); + msec = Integer.MIN_VALUE; + } + _seekTo(msec, mode); + } + + /** + * Get current playback position as a {@link MediaTimestamp}. + *

      + * The MediaTimestamp represents how the media time correlates to the system time in + * a linear fashion using an anchor and a clock rate. During regular playback, the media + * time moves fairly constantly (though the anchor frame may be rebased to a current + * system time, the linear correlation stays steady). Therefore, this method does not + * need to be called often. + *

      + * To help users get current playback position, this method always anchors the timestamp + * to the current {@link System#nanoTime system time}, so + * {@link MediaTimestamp#getAnchorMediaTimeUs} can be used as current playback position. + * + * @return a MediaTimestamp object if a timestamp is available, or {@code null} if no timestamp + * is available, e.g. because the media player has not been initialized. + * + * @see MediaTimestamp + */ + @Override + @Nullable + public MediaTimestamp getTimestamp() + { + try { + // TODO: get the timestamp from native side + return new MediaTimestamp( + getCurrentPosition() * 1000L, + System.nanoTime(), + isPlaying() ? getPlaybackParams().getSpeed() : 0.f); + } catch (IllegalStateException e) { + return null; + } + } + + /** + * Gets the current playback position. + * + * @return the current position in milliseconds + */ + @Override + public native int getCurrentPosition(); + + /** + * Gets the duration of the file. + * + * @return the duration in milliseconds, if no duration is available + * (for example, if streaming live content), -1 is returned. + */ + @Override + public native int getDuration(); + + /** + * Gets the media metadata. + * + * @param update_only controls whether the full set of available + * metadata is returned or just the set that changed since the + * last call. See {@see #METADATA_UPDATE_ONLY} and {@see + * #METADATA_ALL}. + * + * @param apply_filter if true only metadata that matches the + * filter is returned. See {@see #APPLY_METADATA_FILTER} and {@see + * #BYPASS_METADATA_FILTER}. + * + * @return The metadata, possibly empty. null if an error occured. + // FIXME: unhide. + * {@hide} + */ + @Override + public Metadata getMetadata(final boolean update_only, + final boolean apply_filter) { + Parcel reply = Parcel.obtain(); + Metadata data = new Metadata(); + + if (!native_getMetadata(update_only, apply_filter, reply)) { + reply.recycle(); + return null; + } + + // Metadata takes over the parcel, don't recycle it unless + // there is an error. + if (!data.parse(reply)) { + reply.recycle(); + return null; + } + return data; + } + + /** + * Set a filter for the metadata update notification and update + * retrieval. The caller provides 2 set of metadata keys, allowed + * and blocked. The blocked set always takes precedence over the + * allowed one. + * Metadata.MATCH_ALL and Metadata.MATCH_NONE are 2 sets available as + * shorthands to allow/block all or no metadata. + * + * By default, there is no filter set. + * + * @param allow Is the set of metadata the client is interested + * in receiving new notifications for. + * @param block Is the set of metadata the client is not interested + * in receiving new notifications for. + * @return The call status code. + * + // FIXME: unhide. + * {@hide} + */ + @Override + public int setMetadataFilter(Set allow, Set block) { + // Do our serialization manually instead of calling + // Parcel.writeArray since the sets are made of the same type + // we avoid paying the price of calling writeValue (used by + // writeArray) which burns an extra int per element to encode + // the type. + Parcel request = newRequest(); + + // The parcel starts already with an interface token. There + // are 2 filters. Each one starts with a 4bytes number to + // store the len followed by a number of int (4 bytes as well) + // representing the metadata type. + int capacity = request.dataSize() + 4 * (1 + allow.size() + 1 + block.size()); + + if (request.dataCapacity() < capacity) { + request.setDataCapacity(capacity); + } + + request.writeInt(allow.size()); + for(Integer t: allow) { + request.writeInt(t); + } + request.writeInt(block.size()); + for(Integer t: block) { + request.writeInt(t); + } + return native_setMetadataFilter(request); + } + + /** + * Set the MediaPlayer2 to start when this MediaPlayer2 finishes playback + * (i.e. reaches the end of the stream). + * The media framework will attempt to transition from this player to + * the next as seamlessly as possible. The next player can be set at + * any time before completion, but shall be after setDataSource has been + * called successfully. The next player must be prepared by the + * app, and the application should not call play() on it. + * The next MediaPlayer2 must be different from 'this'. An exception + * will be thrown if next == this. + * The application may call setNextMediaPlayer(null) to indicate no + * next player should be started at the end of playback. + * If the current player is looping, it will keep looping and the next + * player will not be started. + * + * @param next the player to start after this one completes playback. + * + * @hide + */ + @Override + public native void setNextMediaPlayer(MediaPlayer2 next); + + /** + * Resets the MediaPlayer2 to its uninitialized state. After calling + * this method, you will have to initialize it again by setting the + * data source and calling prepare(). + */ + @Override + public void reset() { + mSelectedSubtitleTrackIndex = -1; + synchronized(mOpenSubtitleSources) { + for (final InputStream is: mOpenSubtitleSources) { + try { + is.close(); + } catch (IOException e) { + } + } + mOpenSubtitleSources.clear(); + } + if (mSubtitleController != null) { + mSubtitleController.reset(); + } + if (mTimeProvider != null) { + mTimeProvider.close(); + mTimeProvider = null; + } + + stayAwake(false); + _reset(); + // make sure none of the listeners get called anymore + if (mEventHandler != null) { + mEventHandler.removeCallbacksAndMessages(null); + } + + synchronized (mIndexTrackPairs) { + mIndexTrackPairs.clear(); + mInbandTrackIndices.clear(); + }; + + resetDrmState(); + } + + private native void _reset(); + + /** + * Set up a timer for {@link #TimeProvider}. {@link #TimeProvider} will be + * notified when the presentation time reaches (becomes greater than or equal to) + * the value specified. + * + * @param mediaTimeUs presentation time to get timed event callback at + * @hide + */ + @Override + public void notifyAt(long mediaTimeUs) { + _notifyAt(mediaTimeUs); + } + + private native void _notifyAt(long mediaTimeUs); + + // Keep KEY_PARAMETER_* in sync with include/media/mediaplayer2.h + private final static int KEY_PARAMETER_AUDIO_ATTRIBUTES = 1400; + /** + * Sets the parameter indicated by key. + * @param key key indicates the parameter to be set. + * @param value value of the parameter to be set. + * @return true if the parameter is set successfully, false otherwise + * {@hide} + */ + private native boolean setParameter(int key, Parcel value); + + /** + * Sets the audio attributes for this MediaPlayer2. + * See {@link AudioAttributes} for how to build and configure an instance of this class. + * You must call this method before {@link #prepare()} or {@link #prepareAsync()} in order + * for the audio attributes to become effective thereafter. + * @param attributes a non-null set of audio attributes + * @throws IllegalArgumentException if the attributes are null or invalid. + */ + @Override + public void setAudioAttributes(AudioAttributes attributes) { + if (attributes == null) { + final String msg = "Cannot set AudioAttributes to null"; + throw new IllegalArgumentException(msg); + } + mUsage = attributes.getUsage(); + mBypassInterruptionPolicy = (attributes.getAllFlags() + & AudioAttributes.FLAG_BYPASS_INTERRUPTION_POLICY) != 0; + Parcel pattributes = Parcel.obtain(); + attributes.writeToParcel(pattributes, AudioAttributes.FLATTEN_TAGS); + setParameter(KEY_PARAMETER_AUDIO_ATTRIBUTES, pattributes); + pattributes.recycle(); + } + + /** + * Sets the player to be looping or non-looping. + * + * @param looping whether to loop or not + * @hide + */ + @Override + public native void setLooping(boolean looping); + + /** + * Checks whether the MediaPlayer2 is looping or non-looping. + * + * @return true if the MediaPlayer2 is currently looping, false otherwise + * @hide + */ + @Override + public native boolean isLooping(); + + /** + * Sets the volume on this player. + * This API is recommended for balancing the output of audio streams + * within an application. Unless you are writing an application to + * control user settings, this API should be used in preference to + * {@link AudioManager#setStreamVolume(int, int, int)} which sets the volume of ALL streams of + * a particular type. Note that the passed volume values are raw scalars in range 0.0 to 1.0. + * UI controls should be scaled logarithmically. + * + * @param leftVolume left volume scalar + * @param rightVolume right volume scalar + */ + /* + * FIXME: Merge this into javadoc comment above when setVolume(float) is not @hide. + * The single parameter form below is preferred if the channel volumes don't need + * to be set independently. + */ + @Override + public void setVolume(float leftVolume, float rightVolume) { + _setVolume(leftVolume, rightVolume); + } + + private native void _setVolume(float leftVolume, float rightVolume); + + /** + * Similar, excepts sets volume of all channels to same value. + * @hide + */ + @Override + public void setVolume(float volume) { + setVolume(volume, volume); + } + + /** + * Sets the audio session ID. + * + * @param sessionId the audio session ID. + * The audio session ID is a system wide unique identifier for the audio stream played by + * this MediaPlayer2 instance. + * The primary use of the audio session ID is to associate audio effects to a particular + * instance of MediaPlayer2: if an audio session ID is provided when creating an audio effect, + * this effect will be applied only to the audio content of media players within the same + * audio session and not to the output mix. + * When created, a MediaPlayer2 instance automatically generates its own audio session ID. + * However, it is possible to force this player to be part of an already existing audio session + * by calling this method. + * This method must be called before one of the overloaded setDataSource methods. + * @throws IllegalStateException if it is called in an invalid state + * @throws IllegalArgumentException if the sessionId is invalid. + */ + @Override + public native void setAudioSessionId(int sessionId); + + /** + * Returns the audio session ID. + * + * @return the audio session ID. {@see #setAudioSessionId(int)} + * Note that the audio session ID is 0 only if a problem occured when the MediaPlayer2 was contructed. + */ + @Override + public native int getAudioSessionId(); + + /** + * Attaches an auxiliary effect to the player. A typical auxiliary effect is a reverberation + * effect which can be applied on any sound source that directs a certain amount of its + * energy to this effect. This amount is defined by setAuxEffectSendLevel(). + * See {@link #setAuxEffectSendLevel(float)}. + *

      After creating an auxiliary effect (e.g. + * {@link android.media.audiofx.EnvironmentalReverb}), retrieve its ID with + * {@link android.media.audiofx.AudioEffect#getId()} and use it when calling this method + * to attach the player to the effect. + *

      To detach the effect from the player, call this method with a null effect id. + *

      This method must be called after one of the overloaded setDataSource + * methods. + * @param effectId system wide unique id of the effect to attach + */ + @Override + public native void attachAuxEffect(int effectId); + + + /** + * Sets the send level of the player to the attached auxiliary effect. + * See {@link #attachAuxEffect(int)}. The level value range is 0 to 1.0. + *

      By default the send level is 0, so even if an effect is attached to the player + * this method must be called for the effect to be applied. + *

      Note that the passed level value is a raw scalar. UI controls should be scaled + * logarithmically: the gain applied by audio framework ranges from -72dB to 0dB, + * so an appropriate conversion from linear UI input x to level is: + * x == 0 -> level = 0 + * 0 < x <= R -> level = 10^(72*(x-R)/20/R) + * @param level send level scalar + */ + @Override + public void setAuxEffectSendLevel(float level) { + _setAuxEffectSendLevel(level); + } + + private native void _setAuxEffectSendLevel(float level); + + /* + * @param request Parcel destinated to the media player. + * @param reply[out] Parcel that will contain the reply. + * @return The status code. + */ + private native final int native_invoke(Parcel request, Parcel reply); + + + /* + * @param update_only If true fetch only the set of metadata that have + * changed since the last invocation of getMetadata. + * The set is built using the unfiltered + * notifications the native player sent to the + * MediaPlayer2Manager during that period of + * time. If false, all the metadatas are considered. + * @param apply_filter If true, once the metadata set has been built based on + * the value update_only, the current filter is applied. + * @param reply[out] On return contains the serialized + * metadata. Valid only if the call was successful. + * @return The status code. + */ + private native final boolean native_getMetadata(boolean update_only, + boolean apply_filter, + Parcel reply); + + /* + * @param request Parcel with the 2 serialized lists of allowed + * metadata types followed by the one to be + * dropped. Each list starts with an integer + * indicating the number of metadata type elements. + * @return The status code. + */ + private native final int native_setMetadataFilter(Parcel request); + + private static native final void native_init(); + private native final void native_setup(Object mediaplayer2_this); + private native final void native_finalize(); + + /** + * Class for MediaPlayer2 to return each audio/video/subtitle track's metadata. + * + * @see android.media.MediaPlayer2#getTrackInfo + */ + public static final class TrackInfoImpl extends TrackInfo { + /** + * Gets the track type. + * @return TrackType which indicates if the track is video, audio, timed text. + */ + @Override + public int getTrackType() { + return mTrackType; + } + + /** + * Gets the language code of the track. + * @return a language code in either way of ISO-639-1 or ISO-639-2. + * When the language is unknown or could not be determined, + * ISO-639-2 language code, "und", is returned. + */ + @Override + public String getLanguage() { + String language = mFormat.getString(MediaFormat.KEY_LANGUAGE); + return language == null ? "und" : language; + } + + /** + * Gets the {@link MediaFormat} of the track. If the format is + * unknown or could not be determined, null is returned. + */ + @Override + public MediaFormat getFormat() { + if (mTrackType == MEDIA_TRACK_TYPE_TIMEDTEXT + || mTrackType == MEDIA_TRACK_TYPE_SUBTITLE) { + return mFormat; + } + return null; + } + + final int mTrackType; + final MediaFormat mFormat; + + TrackInfoImpl(Parcel in) { + mTrackType = in.readInt(); + // TODO: parcel in the full MediaFormat; currently we are using createSubtitleFormat + // even for audio/video tracks, meaning we only set the mime and language. + String mime = in.readString(); + String language = in.readString(); + mFormat = MediaFormat.createSubtitleFormat(mime, language); + + if (mTrackType == MEDIA_TRACK_TYPE_SUBTITLE) { + mFormat.setInteger(MediaFormat.KEY_IS_AUTOSELECT, in.readInt()); + mFormat.setInteger(MediaFormat.KEY_IS_DEFAULT, in.readInt()); + mFormat.setInteger(MediaFormat.KEY_IS_FORCED_SUBTITLE, in.readInt()); + } + } + + /** @hide */ + TrackInfoImpl(int type, MediaFormat format) { + mTrackType = type; + mFormat = format; + } + + /** + * Flatten this object in to a Parcel. + * + * @param dest The Parcel in which the object should be written. + * @param flags Additional flags about how the object should be written. + * May be 0 or {@link android.os.Parcelable#PARCELABLE_WRITE_RETURN_VALUE}. + */ + /* package private */ void writeToParcel(Parcel dest, int flags) { + dest.writeInt(mTrackType); + dest.writeString(getLanguage()); + + if (mTrackType == MEDIA_TRACK_TYPE_SUBTITLE) { + dest.writeString(mFormat.getString(MediaFormat.KEY_MIME)); + dest.writeInt(mFormat.getInteger(MediaFormat.KEY_IS_AUTOSELECT)); + dest.writeInt(mFormat.getInteger(MediaFormat.KEY_IS_DEFAULT)); + dest.writeInt(mFormat.getInteger(MediaFormat.KEY_IS_FORCED_SUBTITLE)); + } + } + + @Override + public String toString() { + StringBuilder out = new StringBuilder(128); + out.append(getClass().getName()); + out.append('{'); + switch (mTrackType) { + case MEDIA_TRACK_TYPE_VIDEO: + out.append("VIDEO"); + break; + case MEDIA_TRACK_TYPE_AUDIO: + out.append("AUDIO"); + break; + case MEDIA_TRACK_TYPE_TIMEDTEXT: + out.append("TIMEDTEXT"); + break; + case MEDIA_TRACK_TYPE_SUBTITLE: + out.append("SUBTITLE"); + break; + default: + out.append("UNKNOWN"); + break; + } + out.append(", " + mFormat.toString()); + out.append("}"); + return out.toString(); + } + + /** + * Used to read a TrackInfoImpl from a Parcel. + */ + /* package private */ static final Parcelable.Creator CREATOR + = new Parcelable.Creator() { + @Override + public TrackInfoImpl createFromParcel(Parcel in) { + return new TrackInfoImpl(in); + } + + @Override + public TrackInfoImpl[] newArray(int size) { + return new TrackInfoImpl[size]; + } + }; + + }; + + // We would like domain specific classes with more informative names than the `first` and `second` + // in generic Pair, but we would also like to avoid creating new/trivial classes. As a compromise + // we document the meanings of `first` and `second` here: + // + // Pair.first - inband track index; non-null iff representing an inband track. + // Pair.second - a SubtitleTrack registered with mSubtitleController; non-null iff representing + // an inband subtitle track or any out-of-band track (subtitle or timedtext). + private Vector> mIndexTrackPairs = new Vector<>(); + private BitSet mInbandTrackIndices = new BitSet(); + + /** + * Returns a List of track information. + * + * @return List of track info. The total number of tracks is the array length. + * Must be called again if an external timed text source has been added after + * addTimedTextSource method is called. + * @throws IllegalStateException if it is called in an invalid state. + */ + @Override + public List getTrackInfo() { + TrackInfoImpl trackInfo[] = getInbandTrackInfoImpl(); + // add out-of-band tracks + synchronized (mIndexTrackPairs) { + TrackInfoImpl allTrackInfo[] = new TrackInfoImpl[mIndexTrackPairs.size()]; + for (int i = 0; i < allTrackInfo.length; i++) { + Pair p = mIndexTrackPairs.get(i); + if (p.first != null) { + // inband track + allTrackInfo[i] = trackInfo[p.first]; + } else { + SubtitleTrack track = p.second; + allTrackInfo[i] = new TrackInfoImpl(track.getTrackType(), track.getFormat()); + } + } + return Arrays.asList(allTrackInfo); + } + } + + private TrackInfoImpl[] getInbandTrackInfoImpl() throws IllegalStateException { + Parcel request = Parcel.obtain(); + Parcel reply = Parcel.obtain(); + try { + request.writeInt(INVOKE_ID_GET_TRACK_INFO); + invoke(request, reply); + TrackInfoImpl trackInfo[] = reply.createTypedArray(TrackInfoImpl.CREATOR); + return trackInfo; + } finally { + request.recycle(); + reply.recycle(); + } + } + + /* + * A helper function to check if the mime type is supported by media framework. + */ + private static boolean availableMimeTypeForExternalSource(String mimeType) { + if (MEDIA_MIMETYPE_TEXT_SUBRIP.equals(mimeType)) { + return true; + } + return false; + } + + private SubtitleController mSubtitleController; + + /** @hide */ + @Override + public void setSubtitleAnchor( + SubtitleController controller, + SubtitleController.Anchor anchor) { + // TODO: create SubtitleController in MediaPlayer2 + mSubtitleController = controller; + mSubtitleController.setAnchor(anchor); + } + + /** + * The private version of setSubtitleAnchor is used internally to set mSubtitleController if + * necessary when clients don't provide their own SubtitleControllers using the public version + * {@link #setSubtitleAnchor(SubtitleController, Anchor)} (e.g. {@link VideoView} provides one). + */ + private synchronized void setSubtitleAnchor() { + if ((mSubtitleController == null) && (ActivityThread.currentApplication() != null)) { + final HandlerThread thread = new HandlerThread("SetSubtitleAnchorThread"); + thread.start(); + Handler handler = new Handler(thread.getLooper()); + handler.post(new Runnable() { + @Override + public void run() { + Context context = ActivityThread.currentApplication(); + mSubtitleController = new SubtitleController(context, mTimeProvider, MediaPlayer2Impl.this); + mSubtitleController.setAnchor(new Anchor() { + @Override + public void setSubtitleWidget(RenderingWidget subtitleWidget) { + } + + @Override + public Looper getSubtitleLooper() { + return Looper.getMainLooper(); + } + }); + thread.getLooper().quitSafely(); + } + }); + try { + thread.join(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + Log.w(TAG, "failed to join SetSubtitleAnchorThread"); + } + } + } + + private int mSelectedSubtitleTrackIndex = -1; + private Vector mOpenSubtitleSources; + + private OnSubtitleDataListener mSubtitleDataListener = new OnSubtitleDataListener() { + @Override + public void onSubtitleData(MediaPlayer2 mp, SubtitleData data) { + int index = data.getTrackIndex(); + synchronized (mIndexTrackPairs) { + for (Pair p : mIndexTrackPairs) { + if (p.first != null && p.first == index && p.second != null) { + // inband subtitle track that owns data + SubtitleTrack track = p.second; + track.onData(data); + } + } + } + } + }; + + /** @hide */ + @Override + public void onSubtitleTrackSelected(SubtitleTrack track) { + if (mSelectedSubtitleTrackIndex >= 0) { + try { + selectOrDeselectInbandTrack(mSelectedSubtitleTrackIndex, false); + } catch (IllegalStateException e) { + } + mSelectedSubtitleTrackIndex = -1; + } + setOnSubtitleDataListener(null); + if (track == null) { + return; + } + + synchronized (mIndexTrackPairs) { + for (Pair p : mIndexTrackPairs) { + if (p.first != null && p.second == track) { + // inband subtitle track that is selected + mSelectedSubtitleTrackIndex = p.first; + break; + } + } + } + + if (mSelectedSubtitleTrackIndex >= 0) { + try { + selectOrDeselectInbandTrack(mSelectedSubtitleTrackIndex, true); + } catch (IllegalStateException e) { + } + setOnSubtitleDataListener(mSubtitleDataListener); + } + // no need to select out-of-band tracks + } + + /** @hide */ + @Override + public void addSubtitleSource(InputStream is, MediaFormat format) + throws IllegalStateException + { + final InputStream fIs = is; + final MediaFormat fFormat = format; + + if (is != null) { + // Ensure all input streams are closed. It is also a handy + // way to implement timeouts in the future. + synchronized(mOpenSubtitleSources) { + mOpenSubtitleSources.add(is); + } + } else { + Log.w(TAG, "addSubtitleSource called with null InputStream"); + } + + getMediaTimeProvider(); + + // process each subtitle in its own thread + final HandlerThread thread = new HandlerThread("SubtitleReadThread", + Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_MORE_FAVORABLE); + thread.start(); + Handler handler = new Handler(thread.getLooper()); + handler.post(new Runnable() { + private int addTrack() { + if (fIs == null || mSubtitleController == null) { + return MEDIA_INFO_UNSUPPORTED_SUBTITLE; + } + + SubtitleTrack track = mSubtitleController.addTrack(fFormat); + if (track == null) { + return MEDIA_INFO_UNSUPPORTED_SUBTITLE; + } + + // TODO: do the conversion in the subtitle track + Scanner scanner = new Scanner(fIs, "UTF-8"); + String contents = scanner.useDelimiter("\\A").next(); + synchronized(mOpenSubtitleSources) { + mOpenSubtitleSources.remove(fIs); + } + scanner.close(); + synchronized (mIndexTrackPairs) { + mIndexTrackPairs.add(Pair.create(null, track)); + } + Handler h = mTimeProvider.mEventHandler; + int what = TimeProvider.NOTIFY; + int arg1 = TimeProvider.NOTIFY_TRACK_DATA; + Pair trackData = Pair.create(track, contents.getBytes()); + Message m = h.obtainMessage(what, arg1, 0, trackData); + h.sendMessage(m); + return MEDIA_INFO_EXTERNAL_METADATA_UPDATE; + } + + public void run() { + int res = addTrack(); + if (mEventHandler != null) { + Message m = mEventHandler.obtainMessage(MEDIA_INFO, res, 0, null); + mEventHandler.sendMessage(m); + } + thread.getLooper().quitSafely(); + } + }); + } + + private void scanInternalSubtitleTracks() { + setSubtitleAnchor(); + + populateInbandTracks(); + + if (mSubtitleController != null) { + mSubtitleController.selectDefaultTrack(); + } + } + + private void populateInbandTracks() { + TrackInfoImpl[] tracks = getInbandTrackInfoImpl(); + synchronized (mIndexTrackPairs) { + for (int i = 0; i < tracks.length; i++) { + if (mInbandTrackIndices.get(i)) { + continue; + } else { + mInbandTrackIndices.set(i); + } + + // newly appeared inband track + if (tracks[i].getTrackType() == TrackInfo.MEDIA_TRACK_TYPE_SUBTITLE) { + SubtitleTrack track = mSubtitleController.addTrack( + tracks[i].getFormat()); + mIndexTrackPairs.add(Pair.create(i, track)); + } else { + mIndexTrackPairs.add(Pair.create(i, null)); + } + } + } + } + + /* TODO: Limit the total number of external timed text source to a reasonable number. + */ + /** + * Adds an external timed text source file. + * + * Currently supported format is SubRip with the file extension .srt, case insensitive. + * Note that a single external timed text source may contain multiple tracks in it. + * One can find the total number of available tracks using {@link #getTrackInfo()} to see what + * additional tracks become available after this method call. + * + * @param path The file path of external timed text source file. + * @param mimeType The mime type of the file. Must be one of the mime types listed above. + * @throws IOException if the file cannot be accessed or is corrupted. + * @throws IllegalArgumentException if the mimeType is not supported. + * @throws IllegalStateException if called in an invalid state. + * @hide + */ + @Override + public void addTimedTextSource(String path, String mimeType) + throws IOException { + if (!availableMimeTypeForExternalSource(mimeType)) { + final String msg = "Illegal mimeType for timed text source: " + mimeType; + throw new IllegalArgumentException(msg); + } + + File file = new File(path); + if (file.exists()) { + FileInputStream is = new FileInputStream(file); + FileDescriptor fd = is.getFD(); + addTimedTextSource(fd, mimeType); + is.close(); + } else { + // We do not support the case where the path is not a file. + throw new IOException(path); + } + } + + + /** + * Adds an external timed text source file (Uri). + * + * Currently supported format is SubRip with the file extension .srt, case insensitive. + * Note that a single external timed text source may contain multiple tracks in it. + * One can find the total number of available tracks using {@link #getTrackInfo()} to see what + * additional tracks become available after this method call. + * + * @param context the Context to use when resolving the Uri + * @param uri the Content URI of the data you want to play + * @param mimeType The mime type of the file. Must be one of the mime types listed above. + * @throws IOException if the file cannot be accessed or is corrupted. + * @throws IllegalArgumentException if the mimeType is not supported. + * @throws IllegalStateException if called in an invalid state. + * @hide + */ + @Override + public void addTimedTextSource(Context context, Uri uri, String mimeType) + throws IOException { + String scheme = uri.getScheme(); + if(scheme == null || scheme.equals("file")) { + addTimedTextSource(uri.getPath(), mimeType); + return; + } + + AssetFileDescriptor fd = null; + try { + ContentResolver resolver = context.getContentResolver(); + fd = resolver.openAssetFileDescriptor(uri, "r"); + if (fd == null) { + return; + } + addTimedTextSource(fd.getFileDescriptor(), mimeType); + return; + } catch (SecurityException ex) { + } catch (IOException ex) { + } finally { + if (fd != null) { + fd.close(); + } + } + } + + /** + * Adds an external timed text source file (FileDescriptor). + * + * It is the caller's responsibility to close the file descriptor. + * It is safe to do so as soon as this call returns. + * + * Currently supported format is SubRip. Note that a single external timed text source may + * contain multiple tracks in it. One can find the total number of available tracks + * using {@link #getTrackInfo()} to see what additional tracks become available + * after this method call. + * + * @param fd the FileDescriptor for the file you want to play + * @param mimeType The mime type of the file. Must be one of the mime types listed above. + * @throws IllegalArgumentException if the mimeType is not supported. + * @throws IllegalStateException if called in an invalid state. + * @hide + */ + @Override + public void addTimedTextSource(FileDescriptor fd, String mimeType) { + // intentionally less than LONG_MAX + addTimedTextSource(fd, 0, 0x7ffffffffffffffL, mimeType); + } + + /** + * Adds an external timed text file (FileDescriptor). + * + * It is the caller's responsibility to close the file descriptor. + * It is safe to do so as soon as this call returns. + * + * Currently supported format is SubRip. Note that a single external timed text source may + * contain multiple tracks in it. One can find the total number of available tracks + * using {@link #getTrackInfo()} to see what additional tracks become available + * after this method call. + * + * @param fd the FileDescriptor for the file you want to play + * @param offset the offset into the file where the data to be played starts, in bytes + * @param length the length in bytes of the data to be played + * @param mime The mime type of the file. Must be one of the mime types listed above. + * @throws IllegalArgumentException if the mimeType is not supported. + * @throws IllegalStateException if called in an invalid state. + * @hide + */ + @Override + public void addTimedTextSource(FileDescriptor fd, long offset, long length, String mime) { + if (!availableMimeTypeForExternalSource(mime)) { + throw new IllegalArgumentException("Illegal mimeType for timed text source: " + mime); + } + + final FileDescriptor dupedFd; + try { + dupedFd = Os.dup(fd); + } catch (ErrnoException ex) { + Log.e(TAG, ex.getMessage(), ex); + throw new RuntimeException(ex); + } + + final MediaFormat fFormat = new MediaFormat(); + fFormat.setString(MediaFormat.KEY_MIME, mime); + fFormat.setInteger(MediaFormat.KEY_IS_TIMED_TEXT, 1); + + // A MediaPlayer2 created by a VideoView should already have its mSubtitleController set. + if (mSubtitleController == null) { + setSubtitleAnchor(); + } + + if (!mSubtitleController.hasRendererFor(fFormat)) { + // test and add not atomic + Context context = ActivityThread.currentApplication(); + mSubtitleController.registerRenderer(new SRTRenderer(context, mEventHandler)); + } + final SubtitleTrack track = mSubtitleController.addTrack(fFormat); + synchronized (mIndexTrackPairs) { + mIndexTrackPairs.add(Pair.create(null, track)); + } + + getMediaTimeProvider(); + + final long offset2 = offset; + final long length2 = length; + final HandlerThread thread = new HandlerThread( + "TimedTextReadThread", + Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_MORE_FAVORABLE); + thread.start(); + Handler handler = new Handler(thread.getLooper()); + handler.post(new Runnable() { + private int addTrack() { + final ByteArrayOutputStream bos = new ByteArrayOutputStream(); + try { + Os.lseek(dupedFd, offset2, OsConstants.SEEK_SET); + byte[] buffer = new byte[4096]; + for (long total = 0; total < length2;) { + int bytesToRead = (int) Math.min(buffer.length, length2 - total); + int bytes = IoBridge.read(dupedFd, buffer, 0, bytesToRead); + if (bytes < 0) { + break; + } else { + bos.write(buffer, 0, bytes); + total += bytes; + } + } + Handler h = mTimeProvider.mEventHandler; + int what = TimeProvider.NOTIFY; + int arg1 = TimeProvider.NOTIFY_TRACK_DATA; + Pair trackData = Pair.create(track, bos.toByteArray()); + Message m = h.obtainMessage(what, arg1, 0, trackData); + h.sendMessage(m); + return MEDIA_INFO_EXTERNAL_METADATA_UPDATE; + } catch (Exception e) { + Log.e(TAG, e.getMessage(), e); + return MEDIA_INFO_TIMED_TEXT_ERROR; + } finally { + try { + Os.close(dupedFd); + } catch (ErrnoException e) { + Log.e(TAG, e.getMessage(), e); + } + } + } + + public void run() { + int res = addTrack(); + if (mEventHandler != null) { + Message m = mEventHandler.obtainMessage(MEDIA_INFO, res, 0, null); + mEventHandler.sendMessage(m); + } + thread.getLooper().quitSafely(); + } + }); + } + + /** + * Returns the index of the audio, video, or subtitle track currently selected for playback, + * The return value is an index into the array returned by {@link #getTrackInfo()}, and can + * be used in calls to {@link #selectTrack(int)} or {@link #deselectTrack(int)}. + * + * @param trackType should be one of {@link TrackInfo#MEDIA_TRACK_TYPE_VIDEO}, + * {@link TrackInfo#MEDIA_TRACK_TYPE_AUDIO}, or + * {@link TrackInfo#MEDIA_TRACK_TYPE_SUBTITLE} + * @return index of the audio, video, or subtitle track currently selected for playback; + * a negative integer is returned when there is no selected track for {@code trackType} or + * when {@code trackType} is not one of audio, video, or subtitle. + * @throws IllegalStateException if called after {@link #close()} + * + * @see #getTrackInfo() + * @see #selectTrack(int) + * @see #deselectTrack(int) + */ + @Override + public int getSelectedTrack(int trackType) { + if (mSubtitleController != null + && (trackType == TrackInfo.MEDIA_TRACK_TYPE_SUBTITLE + || trackType == TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT)) { + SubtitleTrack subtitleTrack = mSubtitleController.getSelectedTrack(); + if (subtitleTrack != null) { + synchronized (mIndexTrackPairs) { + for (int i = 0; i < mIndexTrackPairs.size(); i++) { + Pair p = mIndexTrackPairs.get(i); + if (p.second == subtitleTrack && subtitleTrack.getTrackType() == trackType) { + return i; + } + } + } + } + } + + Parcel request = Parcel.obtain(); + Parcel reply = Parcel.obtain(); + try { + request.writeInt(INVOKE_ID_GET_SELECTED_TRACK); + request.writeInt(trackType); + invoke(request, reply); + int inbandTrackIndex = reply.readInt(); + synchronized (mIndexTrackPairs) { + for (int i = 0; i < mIndexTrackPairs.size(); i++) { + Pair p = mIndexTrackPairs.get(i); + if (p.first != null && p.first == inbandTrackIndex) { + return i; + } + } + } + return -1; + } finally { + request.recycle(); + reply.recycle(); + } + } + + /** + * Selects a track. + *

      + * If a MediaPlayer2 is in invalid state, it throws an IllegalStateException exception. + * If a MediaPlayer2 is in Started state, the selected track is presented immediately. + * If a MediaPlayer2 is not in Started state, it just marks the track to be played. + *

      + *

      + * In any valid state, if it is called multiple times on the same type of track (ie. Video, + * Audio, Timed Text), the most recent one will be chosen. + *

      + *

      + * The first audio and video tracks are selected by default if available, even though + * this method is not called. However, no timed text track will be selected until + * this function is called. + *

      + *

      + * Currently, only timed text tracks or audio tracks can be selected via this method. + * In addition, the support for selecting an audio track at runtime is pretty limited + * in that an audio track can only be selected in the Prepared state. + *

      + * @param index the index of the track to be selected. The valid range of the index + * is 0..total number of track - 1. The total number of tracks as well as the type of + * each individual track can be found by calling {@link #getTrackInfo()} method. + * @throws IllegalStateException if called in an invalid state. + * + * @see android.media.MediaPlayer2#getTrackInfo + */ + @Override + public void selectTrack(int index) { + selectOrDeselectTrack(index, true /* select */); + } + + /** + * Deselect a track. + *

      + * Currently, the track must be a timed text track and no audio or video tracks can be + * deselected. If the timed text track identified by index has not been + * selected before, it throws an exception. + *

      + * @param index the index of the track to be deselected. The valid range of the index + * is 0..total number of tracks - 1. The total number of tracks as well as the type of + * each individual track can be found by calling {@link #getTrackInfo()} method. + * @throws IllegalStateException if called in an invalid state. + * + * @see android.media.MediaPlayer2#getTrackInfo + */ + @Override + public void deselectTrack(int index) { + selectOrDeselectTrack(index, false /* select */); + } + + private void selectOrDeselectTrack(int index, boolean select) + throws IllegalStateException { + // handle subtitle track through subtitle controller + populateInbandTracks(); + + Pair p = null; + try { + p = mIndexTrackPairs.get(index); + } catch (ArrayIndexOutOfBoundsException e) { + // ignore bad index + return; + } + + SubtitleTrack track = p.second; + if (track == null) { + // inband (de)select + selectOrDeselectInbandTrack(p.first, select); + return; + } + + if (mSubtitleController == null) { + return; + } + + if (!select) { + // out-of-band deselect + if (mSubtitleController.getSelectedTrack() == track) { + mSubtitleController.selectTrack(null); + } else { + Log.w(TAG, "trying to deselect track that was not selected"); + } + return; + } + + // out-of-band select + if (track.getTrackType() == TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT) { + int ttIndex = getSelectedTrack(TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT); + synchronized (mIndexTrackPairs) { + if (ttIndex >= 0 && ttIndex < mIndexTrackPairs.size()) { + Pair p2 = mIndexTrackPairs.get(ttIndex); + if (p2.first != null && p2.second == null) { + // deselect inband counterpart + selectOrDeselectInbandTrack(p2.first, false); + } + } + } + } + mSubtitleController.selectTrack(track); + } + + private void selectOrDeselectInbandTrack(int index, boolean select) + throws IllegalStateException { + Parcel request = Parcel.obtain(); + Parcel reply = Parcel.obtain(); + try { + request.writeInt(select? INVOKE_ID_SELECT_TRACK: INVOKE_ID_DESELECT_TRACK); + request.writeInt(index); + invoke(request, reply); + } finally { + request.recycle(); + reply.recycle(); + } + } + + /** + * Sets the target UDP re-transmit endpoint for the low level player. + * Generally, the address portion of the endpoint is an IP multicast + * address, although a unicast address would be equally valid. When a valid + * retransmit endpoint has been set, the media player will not decode and + * render the media presentation locally. Instead, the player will attempt + * to re-multiplex its media data using the Android@Home RTP profile and + * re-transmit to the target endpoint. Receiver devices (which may be + * either the same as the transmitting device or different devices) may + * instantiate, prepare, and start a receiver player using a setDataSource + * URL of the form... + * + * aahRX://<multicastIP>:<port> + * + * to receive, decode and render the re-transmitted content. + * + * setRetransmitEndpoint may only be called before setDataSource has been + * called; while the player is in the Idle state. + * + * @param endpoint the address and UDP port of the re-transmission target or + * null if no re-transmission is to be performed. + * @throws IllegalStateException if it is called in an invalid state + * @throws IllegalArgumentException if the retransmit endpoint is supplied, + * but invalid. + * + * {@hide} pending API council + */ + @Override + public void setRetransmitEndpoint(InetSocketAddress endpoint) + throws IllegalStateException, IllegalArgumentException + { + String addrString = null; + int port = 0; + + if (null != endpoint) { + addrString = endpoint.getAddress().getHostAddress(); + port = endpoint.getPort(); + } + + int ret = native_setRetransmitEndpoint(addrString, port); + if (ret != 0) { + throw new IllegalArgumentException("Illegal re-transmit endpoint; native ret " + ret); + } + } + + private native final int native_setRetransmitEndpoint(String addrString, int port); + + /** + * Releases the resources held by this {@code MediaPlayer2} object. + * + * It is considered good practice to call this method when you're + * done using the MediaPlayer2. In particular, whenever an Activity + * of an application is paused (its onPause() method is called), + * or stopped (its onStop() method is called), this method should be + * invoked to release the MediaPlayer2 object, unless the application + * has a special need to keep the object around. In addition to + * unnecessary resources (such as memory and instances of codecs) + * being held, failure to call this method immediately if a + * MediaPlayer2 object is no longer needed may also lead to + * continuous battery consumption for mobile devices, and playback + * failure for other applications if no multiple instances of the + * same codec are supported on a device. Even if multiple instances + * of the same codec are supported, some performance degradation + * may be expected when unnecessary multiple instances are used + * at the same time. + * + * {@code close()} may be safely called after a prior {@code close()}. + * This class implements the Java {@code AutoCloseable} interface and + * may be used with try-with-resources. + */ + @Override + public void close() { + synchronized (mGuard) { + release(); + } + } + + // Have to declare protected for finalize() since it is protected + // in the base class Object. + @Override + protected void finalize() throws Throwable { + if (mGuard != null) { + mGuard.warnIfOpen(); + } + + close(); + native_finalize(); + } + + private void release() { + stayAwake(false); + updateSurfaceScreenOn(); + synchronized (mEventCbLock) { + mEventCb = null; + mEventExec = null; + } + if (mTimeProvider != null) { + mTimeProvider.close(); + mTimeProvider = null; + } + mOnSubtitleDataListener = null; + + // Modular DRM clean up + mOnDrmConfigHelper = null; + synchronized (mDrmEventCbLock) { + mDrmEventCb = null; + mDrmEventExec = null; + } + resetDrmState(); + + _release(); + } + + private native void _release(); + + /* Do not change these values without updating their counterparts + * in include/media/mediaplayer2.h! + */ + private static final int MEDIA_NOP = 0; // interface test message + private static final int MEDIA_PREPARED = 1; + private static final int MEDIA_PLAYBACK_COMPLETE = 2; + private static final int MEDIA_BUFFERING_UPDATE = 3; + private static final int MEDIA_SEEK_COMPLETE = 4; + private static final int MEDIA_SET_VIDEO_SIZE = 5; + private static final int MEDIA_STARTED = 6; + private static final int MEDIA_PAUSED = 7; + private static final int MEDIA_STOPPED = 8; + private static final int MEDIA_SKIPPED = 9; + private static final int MEDIA_NOTIFY_TIME = 98; + private static final int MEDIA_TIMED_TEXT = 99; + private static final int MEDIA_ERROR = 100; + private static final int MEDIA_INFO = 200; + private static final int MEDIA_SUBTITLE_DATA = 201; + private static final int MEDIA_META_DATA = 202; + private static final int MEDIA_DRM_INFO = 210; + private static final int MEDIA_AUDIO_ROUTING_CHANGED = 10000; + + private TimeProvider mTimeProvider; + + /** @hide */ + @Override + public MediaTimeProvider getMediaTimeProvider() { + if (mTimeProvider == null) { + mTimeProvider = new TimeProvider(this); + } + return mTimeProvider; + } + + private class EventHandler extends Handler { + private MediaPlayer2Impl mMediaPlayer; + + public EventHandler(MediaPlayer2Impl mp, Looper looper) { + super(looper); + mMediaPlayer = mp; + } + + @Override + public void handleMessage(Message msg) { + if (mMediaPlayer.mNativeContext == 0) { + Log.w(TAG, "mediaplayer2 went away with unhandled events"); + return; + } + final Executor eventExec; + final EventCallback eventCb; + synchronized (mEventCbLock) { + eventExec = mEventExec; + eventCb = mEventCb; + } + final Executor drmEventExec; + final DrmEventCallback drmEventCb; + synchronized (mDrmEventCbLock) { + drmEventExec = mDrmEventExec; + drmEventCb = mDrmEventCb; + } + switch(msg.what) { + case MEDIA_PREPARED: + try { + scanInternalSubtitleTracks(); + } catch (RuntimeException e) { + // send error message instead of crashing; + // send error message instead of inlining a call to onError + // to avoid code duplication. + Message msg2 = obtainMessage( + MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, MEDIA_ERROR_UNSUPPORTED, null); + sendMessage(msg2); + } + + if (eventCb != null && eventExec != null) { + eventExec.execute(() -> eventCb.onInfo( + mMediaPlayer, 0, MEDIA_INFO_PREPARED, 0)); + } + return; + + case MEDIA_DRM_INFO: + Log.v(TAG, "MEDIA_DRM_INFO " + mDrmEventCb); + + if (msg.obj == null) { + Log.w(TAG, "MEDIA_DRM_INFO msg.obj=NULL"); + } else if (msg.obj instanceof Parcel) { + if (drmEventExec != null && drmEventCb != null) { + // The parcel was parsed already in postEventFromNative + final DrmInfoImpl drmInfo; + + synchronized (mDrmLock) { + if (mDrmInfoImpl != null) { + drmInfo = mDrmInfoImpl.makeCopy(); + } else { + drmInfo = null; + } + } + + // notifying the client outside the lock + if (drmInfo != null) { + drmEventExec.execute(() -> drmEventCb.onDrmInfo(mMediaPlayer, drmInfo)); + } + } + } else { + Log.w(TAG, "MEDIA_DRM_INFO msg.obj of unexpected type " + msg.obj); + } + return; + + case MEDIA_PLAYBACK_COMPLETE: + if (eventCb != null && eventExec != null) { + eventExec.execute(() -> eventCb.onInfo( + mMediaPlayer, 0, MEDIA_INFO_PLAYBACK_COMPLETE, 0)); + } + stayAwake(false); + return; + + case MEDIA_STOPPED: + { + TimeProvider timeProvider = mTimeProvider; + if (timeProvider != null) { + timeProvider.onStopped(); + } + } + break; + + case MEDIA_STARTED: + case MEDIA_PAUSED: + { + TimeProvider timeProvider = mTimeProvider; + if (timeProvider != null) { + timeProvider.onPaused(msg.what == MEDIA_PAUSED); + } + } + break; + + case MEDIA_BUFFERING_UPDATE: + if (eventCb != null && eventExec != null) { + final int percent = msg.arg1; + eventExec.execute(() -> eventCb.onBufferingUpdate(mMediaPlayer, 0, percent)); + } + return; + + case MEDIA_SEEK_COMPLETE: + if (eventCb != null && eventExec != null) { + eventExec.execute(() -> eventCb.onInfo( + mMediaPlayer, 0, MEDIA_INFO_COMPLETE_CALL_SEEK, 0)); + } + // fall through + + case MEDIA_SKIPPED: + { + TimeProvider timeProvider = mTimeProvider; + if (timeProvider != null) { + timeProvider.onSeekComplete(mMediaPlayer); + } + } + return; + + case MEDIA_SET_VIDEO_SIZE: + if (eventCb != null && eventExec != null) { + final int width = msg.arg1; + final int height = msg.arg2; + eventExec.execute(() -> eventCb.onVideoSizeChanged( + mMediaPlayer, 0, width, height)); + } + return; + + case MEDIA_ERROR: + Log.e(TAG, "Error (" + msg.arg1 + "," + msg.arg2 + ")"); + if (eventCb != null && eventExec != null) { + final int what = msg.arg1; + final int extra = msg.arg2; + eventExec.execute(() -> eventCb.onError(mMediaPlayer, 0, what, extra)); + eventExec.execute(() -> eventCb.onInfo( + mMediaPlayer, 0, MEDIA_INFO_PLAYBACK_COMPLETE, 0)); + } + stayAwake(false); + return; + + case MEDIA_INFO: + switch (msg.arg1) { + case MEDIA_INFO_VIDEO_TRACK_LAGGING: + Log.i(TAG, "Info (" + msg.arg1 + "," + msg.arg2 + ")"); + break; + case MEDIA_INFO_METADATA_UPDATE: + try { + scanInternalSubtitleTracks(); + } catch (RuntimeException e) { + Message msg2 = obtainMessage( + MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, MEDIA_ERROR_UNSUPPORTED, null); + sendMessage(msg2); + } + // fall through + + case MEDIA_INFO_EXTERNAL_METADATA_UPDATE: + msg.arg1 = MEDIA_INFO_METADATA_UPDATE; + // update default track selection + if (mSubtitleController != null) { + mSubtitleController.selectDefaultTrack(); + } + break; + case MEDIA_INFO_BUFFERING_START: + case MEDIA_INFO_BUFFERING_END: + TimeProvider timeProvider = mTimeProvider; + if (timeProvider != null) { + timeProvider.onBuffering(msg.arg1 == MEDIA_INFO_BUFFERING_START); + } + break; + } + + if (eventCb != null && eventExec != null) { + final int what = msg.arg1; + final int extra = msg.arg2; + eventExec.execute(() -> eventCb.onInfo(mMediaPlayer, 0, what, extra)); + } + // No real default action so far. + return; + + case MEDIA_NOTIFY_TIME: + TimeProvider timeProvider = mTimeProvider; + if (timeProvider != null) { + timeProvider.onNotifyTime(); + } + return; + + case MEDIA_TIMED_TEXT: + if (eventCb == null || eventExec == null) { + return; + } + if (msg.obj == null) { + eventExec.execute(() -> eventCb.onTimedText(mMediaPlayer, 0, null)); + } else { + if (msg.obj instanceof Parcel) { + Parcel parcel = (Parcel)msg.obj; + TimedText text = new TimedText(parcel); + parcel.recycle(); + eventExec.execute(() -> eventCb.onTimedText(mMediaPlayer, 0, text)); + } + } + return; + + case MEDIA_SUBTITLE_DATA: + OnSubtitleDataListener onSubtitleDataListener = mOnSubtitleDataListener; + if (onSubtitleDataListener == null) { + return; + } + if (msg.obj instanceof Parcel) { + Parcel parcel = (Parcel) msg.obj; + SubtitleData data = new SubtitleData(parcel); + parcel.recycle(); + onSubtitleDataListener.onSubtitleData(mMediaPlayer, data); + } + return; + + case MEDIA_META_DATA: + if (eventCb == null || eventExec == null) { + return; + } + if (msg.obj instanceof Parcel) { + Parcel parcel = (Parcel) msg.obj; + TimedMetaData data = TimedMetaData.createTimedMetaDataFromParcel(parcel); + parcel.recycle(); + eventExec.execute(() -> eventCb.onTimedMetaDataAvailable( + mMediaPlayer, 0, data)); + } + return; + + case MEDIA_NOP: // interface test message - ignore + break; + + case MEDIA_AUDIO_ROUTING_CHANGED: + AudioManager.resetAudioPortGeneration(); + synchronized (mRoutingChangeListeners) { + for (NativeRoutingEventHandlerDelegate delegate + : mRoutingChangeListeners.values()) { + delegate.notifyClient(); + } + } + return; + + default: + Log.e(TAG, "Unknown message type " + msg.what); + return; + } + } + } + + /* + * Called from native code when an interesting event happens. This method + * just uses the EventHandler system to post the event back to the main app thread. + * We use a weak reference to the original MediaPlayer2 object so that the native + * code is safe from the object disappearing from underneath it. (This is + * the cookie passed to native_setup().) + */ + private static void postEventFromNative(Object mediaplayer2_ref, + int what, int arg1, int arg2, Object obj) + { + final MediaPlayer2Impl mp = (MediaPlayer2Impl)((WeakReference)mediaplayer2_ref).get(); + if (mp == null) { + return; + } + + switch (what) { + case MEDIA_INFO: + if (arg1 == MEDIA_INFO_STARTED_AS_NEXT) { + new Thread(new Runnable() { + @Override + public void run() { + // this acquires the wakelock if needed, and sets the client side state + mp.play(); + } + }).start(); + Thread.yield(); + } + break; + + case MEDIA_DRM_INFO: + // We need to derive mDrmInfoImpl before prepare() returns so processing it here + // before the notification is sent to EventHandler below. EventHandler runs in the + // notification looper so its handleMessage might process the event after prepare() + // has returned. + Log.v(TAG, "postEventFromNative MEDIA_DRM_INFO"); + if (obj instanceof Parcel) { + Parcel parcel = (Parcel)obj; + DrmInfoImpl drmInfo = new DrmInfoImpl(parcel); + synchronized (mp.mDrmLock) { + mp.mDrmInfoImpl = drmInfo; + } + } else { + Log.w(TAG, "MEDIA_DRM_INFO msg.obj of unexpected type " + obj); + } + break; + + case MEDIA_PREPARED: + // By this time, we've learned about DrmInfo's presence or absence. This is meant + // mainly for prepareAsync() use case. For prepare(), this still can run to a race + // condition b/c MediaPlayerNative releases the prepare() lock before calling notify + // so we also set mDrmInfoResolved in prepare(). + synchronized (mp.mDrmLock) { + mp.mDrmInfoResolved = true; + } + break; + + } + + if (mp.mEventHandler != null) { + Message m = mp.mEventHandler.obtainMessage(what, arg1, arg2, obj); + mp.mEventHandler.sendMessage(m); + } + } + + private Executor mEventExec; + private EventCallback mEventCb; + private final Object mEventCbLock = new Object(); + + /** + * Register a callback to be invoked when the media source is ready + * for playback. + * + * @param eventCallback the callback that will be run + * @param executor the executor through which the callback should be invoked + */ + @Override + public void registerEventCallback(@NonNull @CallbackExecutor Executor executor, + @NonNull EventCallback eventCallback) { + if (eventCallback == null) { + throw new IllegalArgumentException("Illegal null EventCallback"); + } + if (executor == null) { + throw new IllegalArgumentException("Illegal null Executor for the EventCallback"); + } + synchronized (mEventCbLock) { + // TODO: support multiple callbacks. + mEventExec = executor; + mEventCb = eventCallback; + } + } + + /** + * Unregisters an {@link EventCallback}. + * + * @param callback an {@link EventCallback} to unregister + */ + @Override + public void unregisterEventCallback(EventCallback callback) { + synchronized (mEventCbLock) { + if (callback == mEventCb) { + mEventExec = null; + mEventCb = null; + } + } + } + + /** + * Register a callback to be invoked when a track has data available. + * + * @param listener the callback that will be run + * + * @hide + */ + @Override + public void setOnSubtitleDataListener(OnSubtitleDataListener listener) { + mOnSubtitleDataListener = listener; + } + + private OnSubtitleDataListener mOnSubtitleDataListener; + + + // Modular DRM begin + + /** + * Register a callback to be invoked for configuration of the DRM object before + * the session is created. + * The callback will be invoked synchronously during the execution + * of {@link #prepareDrm(UUID uuid)}. + * + * @param listener the callback that will be run + */ + @Override + public void setOnDrmConfigHelper(OnDrmConfigHelper listener) + { + synchronized (mDrmLock) { + mOnDrmConfigHelper = listener; + } // synchronized + } + + private OnDrmConfigHelper mOnDrmConfigHelper; + + private Executor mDrmEventExec; + private DrmEventCallback mDrmEventCb; + private final Object mDrmEventCbLock = new Object(); + + /** + * Register a callback to be invoked when the media source is ready + * for playback. + * + * @param eventCallback the callback that will be run + * @param executor the executor through which the callback should be invoked + */ + @Override + public void registerDrmEventCallback(@NonNull @CallbackExecutor Executor executor, + @NonNull DrmEventCallback eventCallback) { + if (eventCallback == null) { + throw new IllegalArgumentException("Illegal null EventCallback"); + } + if (executor == null) { + throw new IllegalArgumentException("Illegal null Executor for the EventCallback"); + } + synchronized (mDrmEventCbLock) { + // TODO: support multiple callbacks. + mDrmEventExec = executor; + mDrmEventCb = eventCallback; + } + } + + /** + * Unregisters a {@link DrmEventCallback}. + * + * @param callback a {@link DrmEventCallback} to unregister + */ + @Override + public void unregisterDrmEventCallback(DrmEventCallback callback) { + synchronized (mDrmEventCbLock) { + if (callback == mDrmEventCb) { + mDrmEventExec = null; + mDrmEventCb = null; + } + } + } + + + /** + * Retrieves the DRM Info associated with the current source + * + * @throws IllegalStateException if called before prepare() + */ + @Override + public DrmInfo getDrmInfo() { + DrmInfoImpl drmInfo = null; + + // there is not much point if the app calls getDrmInfo within an OnDrmInfoListenet; + // regardless below returns drmInfo anyway instead of raising an exception + synchronized (mDrmLock) { + if (!mDrmInfoResolved && mDrmInfoImpl == null) { + final String msg = "The Player has not been prepared yet"; + Log.v(TAG, msg); + throw new IllegalStateException(msg); + } + + if (mDrmInfoImpl != null) { + drmInfo = mDrmInfoImpl.makeCopy(); + } + } // synchronized + + return drmInfo; + } + + + /** + * Prepares the DRM for the current source + *

      + * If {@code OnDrmConfigHelper} is registered, it will be called during + * preparation to allow configuration of the DRM properties before opening the + * DRM session. Note that the callback is called synchronously in the thread that called + * {@code prepareDrm}. It should be used only for a series of {@code getDrmPropertyString} + * and {@code setDrmPropertyString} calls and refrain from any lengthy operation. + *

      + * If the device has not been provisioned before, this call also provisions the device + * which involves accessing the provisioning server and can take a variable time to + * complete depending on the network connectivity. + * If {@code OnDrmPreparedListener} is registered, prepareDrm() runs in non-blocking + * mode by launching the provisioning in the background and returning. The listener + * will be called when provisioning and preparation has finished. If a + * {@code OnDrmPreparedListener} is not registered, prepareDrm() waits till provisioning + * and preparation has finished, i.e., runs in blocking mode. + *

      + * If {@code OnDrmPreparedListener} is registered, it is called to indicate the DRM + * session being ready. The application should not make any assumption about its call + * sequence (e.g., before or after prepareDrm returns), or the thread context that will + * execute the listener (unless the listener is registered with a handler thread). + *

      + * + * @param uuid The UUID of the crypto scheme. If not known beforehand, it can be retrieved + * from the source through {@code getDrmInfo} or registering a {@code onDrmInfoListener}. + * + * @throws IllegalStateException if called before prepare(), or the DRM was + * prepared already + * @throws UnsupportedSchemeException if the crypto scheme is not supported + * @throws ResourceBusyException if required DRM resources are in use + * @throws ProvisioningNetworkErrorException if provisioning is required but failed due to a + * network error + * @throws ProvisioningServerErrorException if provisioning is required but failed due to + * the request denied by the provisioning server + */ + @Override + public void prepareDrm(@NonNull UUID uuid) + throws UnsupportedSchemeException, ResourceBusyException, + ProvisioningNetworkErrorException, ProvisioningServerErrorException + { + Log.v(TAG, "prepareDrm: uuid: " + uuid + " mOnDrmConfigHelper: " + mOnDrmConfigHelper); + + boolean allDoneWithoutProvisioning = false; + + synchronized (mDrmLock) { + + // only allowing if tied to a protected source; might relax for releasing offline keys + if (mDrmInfoImpl == null) { + final String msg = "prepareDrm(): Wrong usage: The player must be prepared and " + + "DRM info be retrieved before this call."; + Log.e(TAG, msg); + throw new IllegalStateException(msg); + } + + if (mActiveDrmScheme) { + final String msg = "prepareDrm(): Wrong usage: There is already " + + "an active DRM scheme with " + mDrmUUID; + Log.e(TAG, msg); + throw new IllegalStateException(msg); + } + + if (mPrepareDrmInProgress) { + final String msg = "prepareDrm(): Wrong usage: There is already " + + "a pending prepareDrm call."; + Log.e(TAG, msg); + throw new IllegalStateException(msg); + } + + if (mDrmProvisioningInProgress) { + final String msg = "prepareDrm(): Unexpectd: Provisioning is already in progress."; + Log.e(TAG, msg); + throw new IllegalStateException(msg); + } + + // shouldn't need this; just for safeguard + cleanDrmObj(); + + mPrepareDrmInProgress = true; + + try { + // only creating the DRM object to allow pre-openSession configuration + prepareDrm_createDrmStep(uuid); + } catch (Exception e) { + Log.w(TAG, "prepareDrm(): Exception ", e); + mPrepareDrmInProgress = false; + throw e; + } + + mDrmConfigAllowed = true; + } // synchronized + + + // call the callback outside the lock + if (mOnDrmConfigHelper != null) { + mOnDrmConfigHelper.onDrmConfig(this); + } + + synchronized (mDrmLock) { + mDrmConfigAllowed = false; + boolean earlyExit = false; + + try { + prepareDrm_openSessionStep(uuid); + + mDrmUUID = uuid; + mActiveDrmScheme = true; + + allDoneWithoutProvisioning = true; + } catch (IllegalStateException e) { + final String msg = "prepareDrm(): Wrong usage: The player must be " + + "in the prepared state to call prepareDrm()."; + Log.e(TAG, msg); + earlyExit = true; + throw new IllegalStateException(msg); + } catch (NotProvisionedException e) { + Log.w(TAG, "prepareDrm: NotProvisionedException"); + + // handle provisioning internally; it'll reset mPrepareDrmInProgress + int result = HandleProvisioninig(uuid); + + // if blocking mode, we're already done; + // if non-blocking mode, we attempted to launch background provisioning + if (result != PREPARE_DRM_STATUS_SUCCESS) { + earlyExit = true; + String msg; + + switch (result) { + case PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR: + msg = "prepareDrm: Provisioning was required but failed " + + "due to a network error."; + Log.e(TAG, msg); + throw new ProvisioningNetworkErrorExceptionImpl(msg); + + case PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR: + msg = "prepareDrm: Provisioning was required but the request " + + "was denied by the server."; + Log.e(TAG, msg); + throw new ProvisioningServerErrorExceptionImpl(msg); + + case PREPARE_DRM_STATUS_PREPARATION_ERROR: + default: // default for safeguard + msg = "prepareDrm: Post-provisioning preparation failed."; + Log.e(TAG, msg); + throw new IllegalStateException(msg); + } + } + // nothing else to do; + // if blocking or non-blocking, HandleProvisioninig does the re-attempt & cleanup + } catch (Exception e) { + Log.e(TAG, "prepareDrm: Exception " + e); + earlyExit = true; + throw e; + } finally { + if (!mDrmProvisioningInProgress) {// if early exit other than provisioning exception + mPrepareDrmInProgress = false; + } + if (earlyExit) { // cleaning up object if didn't succeed + cleanDrmObj(); + } + } // finally + } // synchronized + + + // if finished successfully without provisioning, call the callback outside the lock + if (allDoneWithoutProvisioning) { + final Executor drmEventExec; + final DrmEventCallback drmEventCb; + synchronized (mDrmEventCbLock) { + drmEventExec = mDrmEventExec; + drmEventCb = mDrmEventCb; + } + if (drmEventExec != null && drmEventCb != null) { + drmEventExec.execute(() -> drmEventCb.onDrmPrepared( + this, PREPARE_DRM_STATUS_SUCCESS)); + } + } + + } + + + private native void _releaseDrm(); + + /** + * Releases the DRM session + *

      + * The player has to have an active DRM session and be in stopped, or prepared + * state before this call is made. + * A {@code reset()} call will release the DRM session implicitly. + * + * @throws NoDrmSchemeException if there is no active DRM session to release + */ + @Override + public void releaseDrm() + throws NoDrmSchemeException + { + Log.v(TAG, "releaseDrm:"); + + synchronized (mDrmLock) { + if (!mActiveDrmScheme) { + Log.e(TAG, "releaseDrm(): No active DRM scheme to release."); + throw new NoDrmSchemeExceptionImpl("releaseDrm: No active DRM scheme to release."); + } + + try { + // we don't have the player's state in this layer. The below call raises + // exception if we're in a non-stopped/prepared state. + + // for cleaning native/mediaserver crypto object + _releaseDrm(); + + // for cleaning client-side MediaDrm object; only called if above has succeeded + cleanDrmObj(); + + mActiveDrmScheme = false; + } catch (IllegalStateException e) { + Log.w(TAG, "releaseDrm: Exception ", e); + throw new IllegalStateException("releaseDrm: The player is not in a valid state."); + } catch (Exception e) { + Log.e(TAG, "releaseDrm: Exception ", e); + } + } // synchronized + } + + + /** + * A key request/response exchange occurs between the app and a license server + * to obtain or release keys used to decrypt encrypted content. + *

      + * getKeyRequest() is used to obtain an opaque key request byte array that is + * delivered to the license server. The opaque key request byte array is returned + * in KeyRequest.data. The recommended URL to deliver the key request to is + * returned in KeyRequest.defaultUrl. + *

      + * After the app has received the key request response from the server, + * it should deliver to the response to the DRM engine plugin using the method + * {@link #provideKeyResponse}. + * + * @param keySetId is the key-set identifier of the offline keys being released when keyType is + * {@link MediaDrm#KEY_TYPE_RELEASE}. It should be set to null for other key requests, when + * keyType is {@link MediaDrm#KEY_TYPE_STREAMING} or {@link MediaDrm#KEY_TYPE_OFFLINE}. + * + * @param initData is the container-specific initialization data when the keyType is + * {@link MediaDrm#KEY_TYPE_STREAMING} or {@link MediaDrm#KEY_TYPE_OFFLINE}. Its meaning is + * interpreted based on the mime type provided in the mimeType parameter. It could + * contain, for example, the content ID, key ID or other data obtained from the content + * metadata that is required in generating the key request. + * When the keyType is {@link MediaDrm#KEY_TYPE_RELEASE}, it should be set to null. + * + * @param mimeType identifies the mime type of the content + * + * @param keyType specifies the type of the request. The request may be to acquire + * keys for streaming, {@link MediaDrm#KEY_TYPE_STREAMING}, or for offline content + * {@link MediaDrm#KEY_TYPE_OFFLINE}, or to release previously acquired + * keys ({@link MediaDrm#KEY_TYPE_RELEASE}), which are identified by a keySetId. + * + * @param optionalParameters are included in the key request message to + * allow a client application to provide additional message parameters to the server. + * This may be {@code null} if no additional parameters are to be sent. + * + * @throws NoDrmSchemeException if there is no active DRM session + */ + @Override + @NonNull + public MediaDrm.KeyRequest getKeyRequest(@Nullable byte[] keySetId, @Nullable byte[] initData, + @Nullable String mimeType, @MediaDrm.KeyType int keyType, + @Nullable Map optionalParameters) + throws NoDrmSchemeException + { + Log.v(TAG, "getKeyRequest: " + + " keySetId: " + keySetId + " initData:" + initData + " mimeType: " + mimeType + + " keyType: " + keyType + " optionalParameters: " + optionalParameters); + + synchronized (mDrmLock) { + if (!mActiveDrmScheme) { + Log.e(TAG, "getKeyRequest NoDrmSchemeException"); + throw new NoDrmSchemeExceptionImpl("getKeyRequest: Has to set a DRM scheme first."); + } + + try { + byte[] scope = (keyType != MediaDrm.KEY_TYPE_RELEASE) ? + mDrmSessionId : // sessionId for KEY_TYPE_STREAMING/OFFLINE + keySetId; // keySetId for KEY_TYPE_RELEASE + + HashMap hmapOptionalParameters = + (optionalParameters != null) ? + new HashMap(optionalParameters) : + null; + + MediaDrm.KeyRequest request = mDrmObj.getKeyRequest(scope, initData, mimeType, + keyType, hmapOptionalParameters); + Log.v(TAG, "getKeyRequest: --> request: " + request); + + return request; + + } catch (NotProvisionedException e) { + Log.w(TAG, "getKeyRequest NotProvisionedException: " + + "Unexpected. Shouldn't have reached here."); + throw new IllegalStateException("getKeyRequest: Unexpected provisioning error."); + } catch (Exception e) { + Log.w(TAG, "getKeyRequest Exception " + e); + throw e; + } + + } // synchronized + } + + + /** + * A key response is received from the license server by the app, then it is + * provided to the DRM engine plugin using provideKeyResponse. When the + * response is for an offline key request, a key-set identifier is returned that + * can be used to later restore the keys to a new session with the method + * {@ link # restoreKeys}. + * When the response is for a streaming or release request, null is returned. + * + * @param keySetId When the response is for a release request, keySetId identifies + * the saved key associated with the release request (i.e., the same keySetId + * passed to the earlier {@ link # getKeyRequest} call. It MUST be null when the + * response is for either streaming or offline key requests. + * + * @param response the byte array response from the server + * + * @throws NoDrmSchemeException if there is no active DRM session + * @throws DeniedByServerException if the response indicates that the + * server rejected the request + */ + @Override + public byte[] provideKeyResponse(@Nullable byte[] keySetId, @NonNull byte[] response) + throws NoDrmSchemeException, DeniedByServerException + { + Log.v(TAG, "provideKeyResponse: keySetId: " + keySetId + " response: " + response); + + synchronized (mDrmLock) { + + if (!mActiveDrmScheme) { + Log.e(TAG, "getKeyRequest NoDrmSchemeException"); + throw new NoDrmSchemeExceptionImpl("getKeyRequest: Has to set a DRM scheme first."); + } + + try { + byte[] scope = (keySetId == null) ? + mDrmSessionId : // sessionId for KEY_TYPE_STREAMING/OFFLINE + keySetId; // keySetId for KEY_TYPE_RELEASE + + byte[] keySetResult = mDrmObj.provideKeyResponse(scope, response); + + Log.v(TAG, "provideKeyResponse: keySetId: " + keySetId + " response: " + response + + " --> " + keySetResult); + + + return keySetResult; + + } catch (NotProvisionedException e) { + Log.w(TAG, "provideKeyResponse NotProvisionedException: " + + "Unexpected. Shouldn't have reached here."); + throw new IllegalStateException("provideKeyResponse: " + + "Unexpected provisioning error."); + } catch (Exception e) { + Log.w(TAG, "provideKeyResponse Exception " + e); + throw e; + } + } // synchronized + } + + + /** + * Restore persisted offline keys into a new session. keySetId identifies the + * keys to load, obtained from a prior call to {@link #provideKeyResponse}. + * + * @param keySetId identifies the saved key set to restore + */ + @Override + public void restoreKeys(@NonNull byte[] keySetId) + throws NoDrmSchemeException + { + Log.v(TAG, "restoreKeys: keySetId: " + keySetId); + + synchronized (mDrmLock) { + + if (!mActiveDrmScheme) { + Log.w(TAG, "restoreKeys NoDrmSchemeException"); + throw new NoDrmSchemeExceptionImpl("restoreKeys: Has to set a DRM scheme first."); + } + + try { + mDrmObj.restoreKeys(mDrmSessionId, keySetId); + } catch (Exception e) { + Log.w(TAG, "restoreKeys Exception " + e); + throw e; + } + + } // synchronized + } + + + /** + * Read a DRM engine plugin String property value, given the property name string. + *

      + * @param propertyName the property name + * + * Standard fields names are: + * {@link MediaDrm#PROPERTY_VENDOR}, {@link MediaDrm#PROPERTY_VERSION}, + * {@link MediaDrm#PROPERTY_DESCRIPTION}, {@link MediaDrm#PROPERTY_ALGORITHMS} + */ + @Override + @NonNull + public String getDrmPropertyString(@NonNull @MediaDrm.StringProperty String propertyName) + throws NoDrmSchemeException + { + Log.v(TAG, "getDrmPropertyString: propertyName: " + propertyName); + + String value; + synchronized (mDrmLock) { + + if (!mActiveDrmScheme && !mDrmConfigAllowed) { + Log.w(TAG, "getDrmPropertyString NoDrmSchemeException"); + throw new NoDrmSchemeExceptionImpl("getDrmPropertyString: Has to prepareDrm() first."); + } + + try { + value = mDrmObj.getPropertyString(propertyName); + } catch (Exception e) { + Log.w(TAG, "getDrmPropertyString Exception " + e); + throw e; + } + } // synchronized + + Log.v(TAG, "getDrmPropertyString: propertyName: " + propertyName + " --> value: " + value); + + return value; + } + + + /** + * Set a DRM engine plugin String property value. + *

      + * @param propertyName the property name + * @param value the property value + * + * Standard fields names are: + * {@link MediaDrm#PROPERTY_VENDOR}, {@link MediaDrm#PROPERTY_VERSION}, + * {@link MediaDrm#PROPERTY_DESCRIPTION}, {@link MediaDrm#PROPERTY_ALGORITHMS} + */ + @Override + public void setDrmPropertyString(@NonNull @MediaDrm.StringProperty String propertyName, + @NonNull String value) + throws NoDrmSchemeException + { + Log.v(TAG, "setDrmPropertyString: propertyName: " + propertyName + " value: " + value); + + synchronized (mDrmLock) { + + if ( !mActiveDrmScheme && !mDrmConfigAllowed ) { + Log.w(TAG, "setDrmPropertyString NoDrmSchemeException"); + throw new NoDrmSchemeExceptionImpl("setDrmPropertyString: Has to prepareDrm() first."); + } + + try { + mDrmObj.setPropertyString(propertyName, value); + } catch ( Exception e ) { + Log.w(TAG, "setDrmPropertyString Exception " + e); + throw e; + } + } // synchronized + } + + /** + * Encapsulates the DRM properties of the source. + */ + public static final class DrmInfoImpl extends DrmInfo { + private Map mapPssh; + private UUID[] supportedSchemes; + + /** + * Returns the PSSH info of the data source for each supported DRM scheme. + */ + @Override + public Map getPssh() { + return mapPssh; + } + + /** + * Returns the intersection of the data source and the device DRM schemes. + * It effectively identifies the subset of the source's DRM schemes which + * are supported by the device too. + */ + @Override + public List getSupportedSchemes() { + return Arrays.asList(supportedSchemes); + } + + private DrmInfoImpl(Map Pssh, UUID[] SupportedSchemes) { + mapPssh = Pssh; + supportedSchemes = SupportedSchemes; + } + + private DrmInfoImpl(Parcel parcel) { + Log.v(TAG, "DrmInfoImpl(" + parcel + ") size " + parcel.dataSize()); + + int psshsize = parcel.readInt(); + byte[] pssh = new byte[psshsize]; + parcel.readByteArray(pssh); + + Log.v(TAG, "DrmInfoImpl() PSSH: " + arrToHex(pssh)); + mapPssh = parsePSSH(pssh, psshsize); + Log.v(TAG, "DrmInfoImpl() PSSH: " + mapPssh); + + int supportedDRMsCount = parcel.readInt(); + supportedSchemes = new UUID[supportedDRMsCount]; + for (int i = 0; i < supportedDRMsCount; i++) { + byte[] uuid = new byte[16]; + parcel.readByteArray(uuid); + + supportedSchemes[i] = bytesToUUID(uuid); + + Log.v(TAG, "DrmInfoImpl() supportedScheme[" + i + "]: " + + supportedSchemes[i]); + } + + Log.v(TAG, "DrmInfoImpl() Parcel psshsize: " + psshsize + + " supportedDRMsCount: " + supportedDRMsCount); + } + + private DrmInfoImpl makeCopy() { + return new DrmInfoImpl(this.mapPssh, this.supportedSchemes); + } + + private String arrToHex(byte[] bytes) { + String out = "0x"; + for (int i = 0; i < bytes.length; i++) { + out += String.format("%02x", bytes[i]); + } + + return out; + } + + private UUID bytesToUUID(byte[] uuid) { + long msb = 0, lsb = 0; + for (int i = 0; i < 8; i++) { + msb |= ( ((long)uuid[i] & 0xff) << (8 * (7 - i)) ); + lsb |= ( ((long)uuid[i+8] & 0xff) << (8 * (7 - i)) ); + } + + return new UUID(msb, lsb); + } + + private Map parsePSSH(byte[] pssh, int psshsize) { + Map result = new HashMap(); + + final int UUID_SIZE = 16; + final int DATALEN_SIZE = 4; + + int len = psshsize; + int numentries = 0; + int i = 0; + + while (len > 0) { + if (len < UUID_SIZE) { + Log.w(TAG, String.format("parsePSSH: len is too short to parse " + + "UUID: (%d < 16) pssh: %d", len, psshsize)); + return null; + } + + byte[] subset = Arrays.copyOfRange(pssh, i, i + UUID_SIZE); + UUID uuid = bytesToUUID(subset); + i += UUID_SIZE; + len -= UUID_SIZE; + + // get data length + if (len < 4) { + Log.w(TAG, String.format("parsePSSH: len is too short to parse " + + "datalen: (%d < 4) pssh: %d", len, psshsize)); + return null; + } + + subset = Arrays.copyOfRange(pssh, i, i+DATALEN_SIZE); + int datalen = (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) ? + ((subset[3] & 0xff) << 24) | ((subset[2] & 0xff) << 16) | + ((subset[1] & 0xff) << 8) | (subset[0] & 0xff) : + ((subset[0] & 0xff) << 24) | ((subset[1] & 0xff) << 16) | + ((subset[2] & 0xff) << 8) | (subset[3] & 0xff) ; + i += DATALEN_SIZE; + len -= DATALEN_SIZE; + + if (len < datalen) { + Log.w(TAG, String.format("parsePSSH: len is too short to parse " + + "data: (%d < %d) pssh: %d", len, datalen, psshsize)); + return null; + } + + byte[] data = Arrays.copyOfRange(pssh, i, i+datalen); + + // skip the data + i += datalen; + len -= datalen; + + Log.v(TAG, String.format("parsePSSH[%d]: <%s, %s> pssh: %d", + numentries, uuid, arrToHex(data), psshsize)); + numentries++; + result.put(uuid, data); + } + + return result; + } + + }; // DrmInfoImpl + + /** + * Thrown when a DRM method is called before preparing a DRM scheme through prepareDrm(). + * Extends MediaDrm.MediaDrmException + */ + public static final class NoDrmSchemeExceptionImpl extends NoDrmSchemeException { + public NoDrmSchemeExceptionImpl(String detailMessage) { + super(detailMessage); + } + } + + /** + * Thrown when the device requires DRM provisioning but the provisioning attempt has + * failed due to a network error (Internet reachability, timeout, etc.). + * Extends MediaDrm.MediaDrmException + */ + public static final class ProvisioningNetworkErrorExceptionImpl + extends ProvisioningNetworkErrorException { + public ProvisioningNetworkErrorExceptionImpl(String detailMessage) { + super(detailMessage); + } + } + + /** + * Thrown when the device requires DRM provisioning but the provisioning attempt has + * failed due to the provisioning server denying the request. + * Extends MediaDrm.MediaDrmException + */ + public static final class ProvisioningServerErrorExceptionImpl + extends ProvisioningServerErrorException { + public ProvisioningServerErrorExceptionImpl(String detailMessage) { + super(detailMessage); + } + } + + + private native void _prepareDrm(@NonNull byte[] uuid, @NonNull byte[] drmSessionId); + + // Modular DRM helpers + + private void prepareDrm_createDrmStep(@NonNull UUID uuid) + throws UnsupportedSchemeException { + Log.v(TAG, "prepareDrm_createDrmStep: UUID: " + uuid); + + try { + mDrmObj = new MediaDrm(uuid); + Log.v(TAG, "prepareDrm_createDrmStep: Created mDrmObj=" + mDrmObj); + } catch (Exception e) { // UnsupportedSchemeException + Log.e(TAG, "prepareDrm_createDrmStep: MediaDrm failed with " + e); + throw e; + } + } + + private void prepareDrm_openSessionStep(@NonNull UUID uuid) + throws NotProvisionedException, ResourceBusyException { + Log.v(TAG, "prepareDrm_openSessionStep: uuid: " + uuid); + + // TODO: don't need an open session for a future specialKeyReleaseDrm mode but we should do + // it anyway so it raises provisioning error if needed. We'd rather handle provisioning + // at prepareDrm/openSession rather than getKeyRequest/provideKeyResponse + try { + mDrmSessionId = mDrmObj.openSession(); + Log.v(TAG, "prepareDrm_openSessionStep: mDrmSessionId=" + mDrmSessionId); + + // Sending it down to native/mediaserver to create the crypto object + // This call could simply fail due to bad player state, e.g., after play(). + _prepareDrm(getByteArrayFromUUID(uuid), mDrmSessionId); + Log.v(TAG, "prepareDrm_openSessionStep: _prepareDrm/Crypto succeeded"); + + } catch (Exception e) { //ResourceBusyException, NotProvisionedException + Log.e(TAG, "prepareDrm_openSessionStep: open/crypto failed with " + e); + throw e; + } + + } + + private class ProvisioningThread extends Thread { + public static final int TIMEOUT_MS = 60000; + + private UUID uuid; + private String urlStr; + private Object drmLock; + private MediaPlayer2Impl mediaPlayer; + private int status; + private boolean finished; + public int status() { + return status; + } + + public ProvisioningThread initialize(MediaDrm.ProvisionRequest request, + UUID uuid, MediaPlayer2Impl mediaPlayer) { + // lock is held by the caller + drmLock = mediaPlayer.mDrmLock; + this.mediaPlayer = mediaPlayer; + + urlStr = request.getDefaultUrl() + "&signedRequest=" + new String(request.getData()); + this.uuid = uuid; + + status = PREPARE_DRM_STATUS_PREPARATION_ERROR; + + Log.v(TAG, "HandleProvisioninig: Thread is initialised url: " + urlStr); + return this; + } + + public void run() { + + byte[] response = null; + boolean provisioningSucceeded = false; + try { + URL url = new URL(urlStr); + final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + try { + connection.setRequestMethod("POST"); + connection.setDoOutput(false); + connection.setDoInput(true); + connection.setConnectTimeout(TIMEOUT_MS); + connection.setReadTimeout(TIMEOUT_MS); + + connection.connect(); + response = Streams.readFully(connection.getInputStream()); + + Log.v(TAG, "HandleProvisioninig: Thread run: response " + + response.length + " " + response); + } catch (Exception e) { + status = PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR; + Log.w(TAG, "HandleProvisioninig: Thread run: connect " + e + " url: " + url); + } finally { + connection.disconnect(); + } + } catch (Exception e) { + status = PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR; + Log.w(TAG, "HandleProvisioninig: Thread run: openConnection " + e); + } + + if (response != null) { + try { + mDrmObj.provideProvisionResponse(response); + Log.v(TAG, "HandleProvisioninig: Thread run: " + + "provideProvisionResponse SUCCEEDED!"); + + provisioningSucceeded = true; + } catch (Exception e) { + status = PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR; + Log.w(TAG, "HandleProvisioninig: Thread run: " + + "provideProvisionResponse " + e); + } + } + + boolean succeeded = false; + + final Executor drmEventExec; + final DrmEventCallback drmEventCb; + synchronized (mDrmEventCbLock) { + drmEventExec = mDrmEventExec; + drmEventCb = mDrmEventCb; + } + // non-blocking mode needs the lock + if (drmEventExec != null && drmEventCb != null) { + + synchronized (drmLock) { + // continuing with prepareDrm + if (provisioningSucceeded) { + succeeded = mediaPlayer.resumePrepareDrm(uuid); + status = (succeeded) ? + PREPARE_DRM_STATUS_SUCCESS : + PREPARE_DRM_STATUS_PREPARATION_ERROR; + } + mediaPlayer.mDrmProvisioningInProgress = false; + mediaPlayer.mPrepareDrmInProgress = false; + if (!succeeded) { + cleanDrmObj(); // cleaning up if it hasn't gone through while in the lock + } + } // synchronized + + // calling the callback outside the lock + drmEventExec.execute(() -> drmEventCb.onDrmPrepared(mediaPlayer, status)); + } else { // blocking mode already has the lock + + // continuing with prepareDrm + if (provisioningSucceeded) { + succeeded = mediaPlayer.resumePrepareDrm(uuid); + status = (succeeded) ? + PREPARE_DRM_STATUS_SUCCESS : + PREPARE_DRM_STATUS_PREPARATION_ERROR; + } + mediaPlayer.mDrmProvisioningInProgress = false; + mediaPlayer.mPrepareDrmInProgress = false; + if (!succeeded) { + cleanDrmObj(); // cleaning up if it hasn't gone through + } + } + + finished = true; + } // run() + + } // ProvisioningThread + + private int HandleProvisioninig(UUID uuid) { + // the lock is already held by the caller + + if (mDrmProvisioningInProgress) { + Log.e(TAG, "HandleProvisioninig: Unexpected mDrmProvisioningInProgress"); + return PREPARE_DRM_STATUS_PREPARATION_ERROR; + } + + MediaDrm.ProvisionRequest provReq = mDrmObj.getProvisionRequest(); + if (provReq == null) { + Log.e(TAG, "HandleProvisioninig: getProvisionRequest returned null."); + return PREPARE_DRM_STATUS_PREPARATION_ERROR; + } + + Log.v(TAG, "HandleProvisioninig provReq " + + " data: " + provReq.getData() + " url: " + provReq.getDefaultUrl()); + + // networking in a background thread + mDrmProvisioningInProgress = true; + + mDrmProvisioningThread = new ProvisioningThread().initialize(provReq, uuid, this); + mDrmProvisioningThread.start(); + + int result; + + // non-blocking: this is not the final result + final Executor drmEventExec; + final DrmEventCallback drmEventCb; + synchronized (mDrmEventCbLock) { + drmEventExec = mDrmEventExec; + drmEventCb = mDrmEventCb; + } + if (drmEventCb != null && drmEventExec != null) { + result = PREPARE_DRM_STATUS_SUCCESS; + } else { + // if blocking mode, wait till provisioning is done + try { + mDrmProvisioningThread.join(); + } catch (Exception e) { + Log.w(TAG, "HandleProvisioninig: Thread.join Exception " + e); + } + result = mDrmProvisioningThread.status(); + // no longer need the thread + mDrmProvisioningThread = null; + } + + return result; + } + + private boolean resumePrepareDrm(UUID uuid) { + Log.v(TAG, "resumePrepareDrm: uuid: " + uuid); + + // mDrmLock is guaranteed to be held + boolean success = false; + try { + // resuming + prepareDrm_openSessionStep(uuid); + + mDrmUUID = uuid; + mActiveDrmScheme = true; + + success = true; + } catch (Exception e) { + Log.w(TAG, "HandleProvisioninig: Thread run _prepareDrm resume failed with " + e); + // mDrmObj clean up is done by the caller + } + + return success; + } + + private void resetDrmState() { + synchronized (mDrmLock) { + Log.v(TAG, "resetDrmState: " + + " mDrmInfoImpl=" + mDrmInfoImpl + + " mDrmProvisioningThread=" + mDrmProvisioningThread + + " mPrepareDrmInProgress=" + mPrepareDrmInProgress + + " mActiveDrmScheme=" + mActiveDrmScheme); + + mDrmInfoResolved = false; + mDrmInfoImpl = null; + + if (mDrmProvisioningThread != null) { + // timeout; relying on HttpUrlConnection + try { + mDrmProvisioningThread.join(); + } + catch (InterruptedException e) { + Log.w(TAG, "resetDrmState: ProvThread.join Exception " + e); + } + mDrmProvisioningThread = null; + } + + mPrepareDrmInProgress = false; + mActiveDrmScheme = false; + + cleanDrmObj(); + } // synchronized + } + + private void cleanDrmObj() { + // the caller holds mDrmLock + Log.v(TAG, "cleanDrmObj: mDrmObj=" + mDrmObj + " mDrmSessionId=" + mDrmSessionId); + + if (mDrmSessionId != null) { + mDrmObj.closeSession(mDrmSessionId); + mDrmSessionId = null; + } + if (mDrmObj != null) { + mDrmObj.release(); + mDrmObj = null; + } + } + + private static final byte[] getByteArrayFromUUID(@NonNull UUID uuid) { + long msb = uuid.getMostSignificantBits(); + long lsb = uuid.getLeastSignificantBits(); + + byte[] uuidBytes = new byte[16]; + for (int i = 0; i < 8; ++i) { + uuidBytes[i] = (byte)(msb >>> (8 * (7 - i))); + uuidBytes[8 + i] = (byte)(lsb >>> (8 * (7 - i))); + } + + return uuidBytes; + } + + // Modular DRM end + + /* + * Test whether a given video scaling mode is supported. + */ + private boolean isVideoScalingModeSupported(int mode) { + return (mode == VIDEO_SCALING_MODE_SCALE_TO_FIT || + mode == VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING); + } + + /** @hide */ + static class TimeProvider implements MediaTimeProvider { + private static final String TAG = "MTP"; + private static final long MAX_NS_WITHOUT_POSITION_CHECK = 5000000000L; + private static final long MAX_EARLY_CALLBACK_US = 1000; + private static final long TIME_ADJUSTMENT_RATE = 2; /* meaning 1/2 */ + private long mLastTimeUs = 0; + private MediaPlayer2Impl mPlayer; + private boolean mPaused = true; + private boolean mStopped = true; + private boolean mBuffering; + private long mLastReportedTime; + // since we are expecting only a handful listeners per stream, there is + // no need for log(N) search performance + private MediaTimeProvider.OnMediaTimeListener mListeners[]; + private long mTimes[]; + private Handler mEventHandler; + private boolean mRefresh = false; + private boolean mPausing = false; + private boolean mSeeking = false; + private static final int NOTIFY = 1; + private static final int NOTIFY_TIME = 0; + private static final int NOTIFY_STOP = 2; + private static final int NOTIFY_SEEK = 3; + private static final int NOTIFY_TRACK_DATA = 4; + private HandlerThread mHandlerThread; + + /** @hide */ + public boolean DEBUG = false; + + public TimeProvider(MediaPlayer2Impl mp) { + mPlayer = mp; + try { + getCurrentTimeUs(true, false); + } catch (IllegalStateException e) { + // we assume starting position + mRefresh = true; + } + + Looper looper; + if ((looper = Looper.myLooper()) == null && + (looper = Looper.getMainLooper()) == null) { + // Create our own looper here in case MP was created without one + mHandlerThread = new HandlerThread("MediaPlayer2MTPEventThread", + Process.THREAD_PRIORITY_FOREGROUND); + mHandlerThread.start(); + looper = mHandlerThread.getLooper(); + } + mEventHandler = new EventHandler(looper); + + mListeners = new MediaTimeProvider.OnMediaTimeListener[0]; + mTimes = new long[0]; + mLastTimeUs = 0; + } + + private void scheduleNotification(int type, long delayUs) { + // ignore time notifications until seek is handled + if (mSeeking && type == NOTIFY_TIME) { + return; + } + + if (DEBUG) Log.v(TAG, "scheduleNotification " + type + " in " + delayUs); + mEventHandler.removeMessages(NOTIFY); + Message msg = mEventHandler.obtainMessage(NOTIFY, type, 0); + mEventHandler.sendMessageDelayed(msg, (int) (delayUs / 1000)); + } + + /** @hide */ + public void close() { + mEventHandler.removeMessages(NOTIFY); + if (mHandlerThread != null) { + mHandlerThread.quitSafely(); + mHandlerThread = null; + } + } + + /** @hide */ + protected void finalize() { + if (mHandlerThread != null) { + mHandlerThread.quitSafely(); + } + } + + /** @hide */ + public void onNotifyTime() { + synchronized (this) { + if (DEBUG) Log.d(TAG, "onNotifyTime: "); + scheduleNotification(NOTIFY_TIME, 0 /* delay */); + } + } + + /** @hide */ + public void onPaused(boolean paused) { + synchronized(this) { + if (DEBUG) Log.d(TAG, "onPaused: " + paused); + if (mStopped) { // handle as seek if we were stopped + mStopped = false; + mSeeking = true; + scheduleNotification(NOTIFY_SEEK, 0 /* delay */); + } else { + mPausing = paused; // special handling if player disappeared + mSeeking = false; + scheduleNotification(NOTIFY_TIME, 0 /* delay */); + } + } + } + + /** @hide */ + public void onBuffering(boolean buffering) { + synchronized (this) { + if (DEBUG) Log.d(TAG, "onBuffering: " + buffering); + mBuffering = buffering; + scheduleNotification(NOTIFY_TIME, 0 /* delay */); + } + } + + /** @hide */ + public void onStopped() { + synchronized(this) { + if (DEBUG) Log.d(TAG, "onStopped"); + mPaused = true; + mStopped = true; + mSeeking = false; + mBuffering = false; + scheduleNotification(NOTIFY_STOP, 0 /* delay */); + } + } + + /** @hide */ + public void onSeekComplete(MediaPlayer2Impl mp) { + synchronized(this) { + mStopped = false; + mSeeking = true; + scheduleNotification(NOTIFY_SEEK, 0 /* delay */); + } + } + + /** @hide */ + public void onNewPlayer() { + if (mRefresh) { + synchronized(this) { + mStopped = false; + mSeeking = true; + mBuffering = false; + scheduleNotification(NOTIFY_SEEK, 0 /* delay */); + } + } + } + + private synchronized void notifySeek() { + mSeeking = false; + try { + long timeUs = getCurrentTimeUs(true, false); + if (DEBUG) Log.d(TAG, "onSeekComplete at " + timeUs); + + for (MediaTimeProvider.OnMediaTimeListener listener: mListeners) { + if (listener == null) { + break; + } + listener.onSeek(timeUs); + } + } catch (IllegalStateException e) { + // we should not be there, but at least signal pause + if (DEBUG) Log.d(TAG, "onSeekComplete but no player"); + mPausing = true; // special handling if player disappeared + notifyTimedEvent(false /* refreshTime */); + } + } + + private synchronized void notifyTrackData(Pair trackData) { + SubtitleTrack track = trackData.first; + byte[] data = trackData.second; + track.onData(data, true /* eos */, ~0 /* runID: keep forever */); + } + + private synchronized void notifyStop() { + for (MediaTimeProvider.OnMediaTimeListener listener: mListeners) { + if (listener == null) { + break; + } + listener.onStop(); + } + } + + private int registerListener(MediaTimeProvider.OnMediaTimeListener listener) { + int i = 0; + for (; i < mListeners.length; i++) { + if (mListeners[i] == listener || mListeners[i] == null) { + break; + } + } + + // new listener + if (i >= mListeners.length) { + MediaTimeProvider.OnMediaTimeListener[] newListeners = + new MediaTimeProvider.OnMediaTimeListener[i + 1]; + long[] newTimes = new long[i + 1]; + System.arraycopy(mListeners, 0, newListeners, 0, mListeners.length); + System.arraycopy(mTimes, 0, newTimes, 0, mTimes.length); + mListeners = newListeners; + mTimes = newTimes; + } + + if (mListeners[i] == null) { + mListeners[i] = listener; + mTimes[i] = MediaTimeProvider.NO_TIME; + } + return i; + } + + public void notifyAt( + long timeUs, MediaTimeProvider.OnMediaTimeListener listener) { + synchronized(this) { + if (DEBUG) Log.d(TAG, "notifyAt " + timeUs); + mTimes[registerListener(listener)] = timeUs; + scheduleNotification(NOTIFY_TIME, 0 /* delay */); + } + } + + public void scheduleUpdate(MediaTimeProvider.OnMediaTimeListener listener) { + synchronized(this) { + if (DEBUG) Log.d(TAG, "scheduleUpdate"); + int i = registerListener(listener); + + if (!mStopped) { + mTimes[i] = 0; + scheduleNotification(NOTIFY_TIME, 0 /* delay */); + } + } + } + + public void cancelNotifications( + MediaTimeProvider.OnMediaTimeListener listener) { + synchronized(this) { + int i = 0; + for (; i < mListeners.length; i++) { + if (mListeners[i] == listener) { + System.arraycopy(mListeners, i + 1, + mListeners, i, mListeners.length - i - 1); + System.arraycopy(mTimes, i + 1, + mTimes, i, mTimes.length - i - 1); + mListeners[mListeners.length - 1] = null; + mTimes[mTimes.length - 1] = NO_TIME; + break; + } else if (mListeners[i] == null) { + break; + } + } + + scheduleNotification(NOTIFY_TIME, 0 /* delay */); + } + } + + private synchronized void notifyTimedEvent(boolean refreshTime) { + // figure out next callback + long nowUs; + try { + nowUs = getCurrentTimeUs(refreshTime, true); + } catch (IllegalStateException e) { + // assume we paused until new player arrives + mRefresh = true; + mPausing = true; // this ensures that call succeeds + nowUs = getCurrentTimeUs(refreshTime, true); + } + long nextTimeUs = nowUs; + + if (mSeeking) { + // skip timed-event notifications until seek is complete + return; + } + + if (DEBUG) { + StringBuilder sb = new StringBuilder(); + sb.append("notifyTimedEvent(").append(mLastTimeUs).append(" -> ") + .append(nowUs).append(") from {"); + boolean first = true; + for (long time: mTimes) { + if (time == NO_TIME) { + continue; + } + if (!first) sb.append(", "); + sb.append(time); + first = false; + } + sb.append("}"); + Log.d(TAG, sb.toString()); + } + + Vector activatedListeners = + new Vector(); + for (int ix = 0; ix < mTimes.length; ix++) { + if (mListeners[ix] == null) { + break; + } + if (mTimes[ix] <= NO_TIME) { + // ignore, unless we were stopped + } else if (mTimes[ix] <= nowUs + MAX_EARLY_CALLBACK_US) { + activatedListeners.add(mListeners[ix]); + if (DEBUG) Log.d(TAG, "removed"); + mTimes[ix] = NO_TIME; + } else if (nextTimeUs == nowUs || mTimes[ix] < nextTimeUs) { + nextTimeUs = mTimes[ix]; + } + } + + if (nextTimeUs > nowUs && !mPaused) { + // schedule callback at nextTimeUs + if (DEBUG) Log.d(TAG, "scheduling for " + nextTimeUs + " and " + nowUs); + mPlayer.notifyAt(nextTimeUs); + } else { + mEventHandler.removeMessages(NOTIFY); + // no more callbacks + } + + for (MediaTimeProvider.OnMediaTimeListener listener: activatedListeners) { + listener.onTimedEvent(nowUs); + } + } + + public long getCurrentTimeUs(boolean refreshTime, boolean monotonic) + throws IllegalStateException { + synchronized (this) { + // we always refresh the time when the paused-state changes, because + // we expect to have received the pause-change event delayed. + if (mPaused && !refreshTime) { + return mLastReportedTime; + } + + try { + mLastTimeUs = mPlayer.getCurrentPosition() * 1000L; + mPaused = !mPlayer.isPlaying() || mBuffering; + if (DEBUG) Log.v(TAG, (mPaused ? "paused" : "playing") + " at " + mLastTimeUs); + } catch (IllegalStateException e) { + if (mPausing) { + // if we were pausing, get last estimated timestamp + mPausing = false; + if (!monotonic || mLastReportedTime < mLastTimeUs) { + mLastReportedTime = mLastTimeUs; + } + mPaused = true; + if (DEBUG) Log.d(TAG, "illegal state, but pausing: estimating at " + mLastReportedTime); + return mLastReportedTime; + } + // TODO get time when prepared + throw e; + } + if (monotonic && mLastTimeUs < mLastReportedTime) { + /* have to adjust time */ + if (mLastReportedTime - mLastTimeUs > 1000000) { + // schedule seeked event if time jumped significantly + // TODO: do this properly by introducing an exception + mStopped = false; + mSeeking = true; + scheduleNotification(NOTIFY_SEEK, 0 /* delay */); + } + } else { + mLastReportedTime = mLastTimeUs; + } + + return mLastReportedTime; + } + } + + private class EventHandler extends Handler { + public EventHandler(Looper looper) { + super(looper); + } + + @Override + public void handleMessage(Message msg) { + if (msg.what == NOTIFY) { + switch (msg.arg1) { + case NOTIFY_TIME: + notifyTimedEvent(true /* refreshTime */); + break; + case NOTIFY_STOP: + notifyStop(); + break; + case NOTIFY_SEEK: + notifySeek(); + break; + case NOTIFY_TRACK_DATA: + notifyTrackData((Pair)msg.obj); + break; + } + } + } + } + } +} diff --git a/android/media/MediaPlayerBase.java b/android/media/MediaPlayerBase.java new file mode 100644 index 00000000..d638a9f9 --- /dev/null +++ b/android/media/MediaPlayerBase.java @@ -0,0 +1,72 @@ +/* + * Copyright 2018 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.media.MediaSession2.PlaylistParam; +import android.media.session.PlaybackState; +import android.os.Handler; + +import java.util.List; +import java.util.concurrent.Executor; + +/** + * Base interfaces for all media players that want media session. + * + * @hide + */ +public abstract class MediaPlayerBase { + /** + * Listens change in {@link PlaybackState2}. + */ + public interface PlaybackListener { + /** + * Called when {@link PlaybackState2} for this player is changed. + */ + void onPlaybackChanged(PlaybackState2 state); + } + + public abstract void play(); + public abstract void prepare(); + public abstract void pause(); + public abstract void stop(); + public abstract void skipToPrevious(); + public abstract void skipToNext(); + public abstract void seekTo(long pos); + public abstract void fastFoward(); + public abstract void rewind(); + + public abstract PlaybackState2 getPlaybackState(); + public abstract AudioAttributes getAudioAttributes(); + + public abstract void setPlaylist(List item, PlaylistParam param); + public abstract void setCurrentPlaylistItem(int index); + + /** + * Add a {@link PlaybackListener} to be invoked when the playback state is changed. + * + * @param executor the Handler that will receive the listener + * @param listener the listener that will be run + */ + public abstract void addPlaybackListener(Executor executor, PlaybackListener listener); + + /** + * Remove previously added {@link PlaybackListener}. + * + * @param listener the listener to be removed + */ + public abstract void removePlaybackListener(PlaybackListener listener); +} diff --git a/android/media/MediaRecorder.java b/android/media/MediaRecorder.java index 3c49b80b..78477f75 100644 --- a/android/media/MediaRecorder.java +++ b/android/media/MediaRecorder.java @@ -1380,7 +1380,8 @@ public class MediaRecorder implements AudioRouting if (listener != null && !mRoutingChangeListeners.containsKey(listener)) { enableNativeRoutingCallbacksLocked(true); mRoutingChangeListeners.put( - listener, new NativeRoutingEventHandlerDelegate(this, listener, handler)); + listener, new NativeRoutingEventHandlerDelegate(this, listener, + handler != null ? handler : mEventHandler)); } } } @@ -1401,36 +1402,6 @@ public class MediaRecorder implements AudioRouting } } - /** - * Helper class to handle the forwarding of native events to the appropriate listener - * (potentially) handled in a different thread - */ - private class NativeRoutingEventHandlerDelegate { - private MediaRecorder mMediaRecorder; - private AudioRouting.OnRoutingChangedListener mOnRoutingChangedListener; - private Handler mHandler; - - NativeRoutingEventHandlerDelegate(final MediaRecorder mediaRecorder, - final AudioRouting.OnRoutingChangedListener listener, Handler handler) { - mMediaRecorder = mediaRecorder; - mOnRoutingChangedListener = listener; - mHandler = handler != null ? handler : mEventHandler; - } - - void notifyClient() { - if (mHandler != null) { - mHandler.post(new Runnable() { - @Override - public void run() { - if (mOnRoutingChangedListener != null) { - mOnRoutingChangedListener.onRoutingChanged(mMediaRecorder); - } - } - }); - } - } - } - private native final boolean native_setInputDevice(int deviceId); private native final int native_getRoutedDeviceId(); private native final void native_enableDeviceCallback(boolean enabled); diff --git a/android/media/MediaSession2.java b/android/media/MediaSession2.java new file mode 100644 index 00000000..0e90040a --- /dev/null +++ b/android/media/MediaSession2.java @@ -0,0 +1,1223 @@ +/* + * Copyright 2018 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.CallbackExecutor; +import android.annotation.IntDef; +import android.annotation.NonNull; +import android.annotation.Nullable; +import android.app.Activity; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import android.media.MediaPlayerBase.PlaybackListener; +import android.media.session.MediaSession; +import android.media.session.MediaSession.Callback; +import android.media.session.PlaybackState; +import android.media.update.ApiLoader; +import android.media.update.MediaSession2Provider; +import android.media.update.MediaSession2Provider.ControllerInfoProvider; +import android.net.Uri; +import android.os.Bundle; +import android.os.Handler; +import android.os.Parcelable; +import android.os.ResultReceiver; +import android.text.TextUtils; +import android.util.ArraySet; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Executor; + +/** + * Allows a media app to expose its transport controls and playback information in a process to + * other processes including the Android framework and other apps. Common use cases are as follows. + *

        + *
      • Bluetooth/wired headset key events support
      • + *
      • Android Auto/Wearable support
      • + *
      • Separating UI process and playback process
      • + *
      + *

      + * A MediaSession2 should be created when an app wants to publish media playback information or + * handle media keys. In general an app only needs one session for all playback, though multiple + * sessions can be created to provide finer grain controls of media. + *

      + * If you want to support background playback, {@link MediaSessionService2} is preferred + * instead. With it, your playback can be revived even after you've finished playback. See + * {@link MediaSessionService2} for details. + *

      + * A session can be obtained by {@link Builder}. The owner of the session may pass its session token + * to other processes to allow them to create a {@link MediaController2} to interact with the + * session. + *

      + * When a session receive transport control commands, the session sends the commands directly to + * the the underlying media player set by {@link Builder} or {@link #setPlayer(MediaPlayerBase)}. + *

      + * When an app is finished performing playback it must call {@link #close()} to clean up the session + * and notify any controllers. + *

      + * {@link MediaSession2} objects should be used on the thread on the looper. + * + * @see MediaSessionService2 + * @hide + */ +// TODO(jaewan): Unhide +// TODO(jaewan): Revisit comments. Currently it's borrowed from the MediaSession. +// TODO(jaewan): Should we support thread safe? It may cause tricky issue such as b/63797089 +// TODO(jaewan): Should we make APIs for MediaSessionService2 public? It's helpful for +// developers that doesn't want to override from Browser, but user may not use this +// correctly. +public class MediaSession2 implements AutoCloseable { + private final MediaSession2Provider mProvider; + + // Note: Do not define IntDef because subclass can add more command code on top of these. + // TODO(jaewan): Shouldn't we pull out? + public static final int COMMAND_CODE_CUSTOM = 0; + public static final int COMMAND_CODE_PLAYBACK_START = 1; + public static final int COMMAND_CODE_PLAYBACK_PAUSE = 2; + public static final int COMMAND_CODE_PLAYBACK_STOP = 3; + public static final int COMMAND_CODE_PLAYBACK_SKIP_NEXT_ITEM = 4; + public static final int COMMAND_CODE_PLAYBACK_SKIP_PREV_ITEM = 5; + public static final int COMMAND_CODE_PLAYBACK_PREPARE = 6; + public static final int COMMAND_CODE_PLAYBACK_FAST_FORWARD = 7; + public static final int COMMAND_CODE_PLAYBACK_REWIND = 8; + public static final int COMMAND_CODE_PLAYBACK_SEEK_TO = 9; + public static final int COMMAND_CODE_PLAYBACK_SET_CURRENT_PLAYLIST_ITEM = 10; + + public static final int COMMAND_CODE_PLAYLIST_GET = 11; + public static final int COMMAND_CODE_PLAYLIST_ADD = 12; + public static final int COMMAND_CODE_PLAYLIST_REMOVE = 13; + + public static final int COMMAND_CODE_PLAY_FROM_MEDIA_ID = 14; + public static final int COMMAND_CODE_PLAY_FROM_URI = 15; + public static final int COMMAND_CODE_PLAY_FROM_SEARCH = 16; + + public static final int COMMAND_CODE_PREPARE_FROM_MEDIA_ID = 17; + public static final int COMMAND_CODE_PREPARE_FROM_URI = 18; + public static final int COMMAND_CODE_PREPARE_FROM_SEARCH = 19; + + /** + * Define a command that a {@link MediaController2} can send to a {@link MediaSession2}. + *

      + * If {@link #getCommandCode()} isn't {@link #COMMAND_CODE_CUSTOM}), it's predefined command. + * If {@link #getCommandCode()} is {@link #COMMAND_CODE_CUSTOM}), it's custom command and + * {@link #getCustomCommand()} shouldn't be {@code null}. + */ + // TODO(jaewan): Move this into the updatable. + public static final class Command { + private static final String KEY_COMMAND_CODE + = "android.media.media_session2.command.command_code"; + private static final String KEY_COMMAND_CUSTOM_COMMAND + = "android.media.media_session2.command.custom_command"; + private static final String KEY_COMMAND_EXTRA + = "android.media.media_session2.command.extra"; + + private final int mCommandCode; + // Nonnull if it's custom command + private final String mCustomCommand; + private final Bundle mExtra; + + public Command(int commandCode) { + mCommandCode = commandCode; + mCustomCommand = null; + mExtra = null; + } + + public Command(@NonNull String action, @Nullable Bundle extra) { + if (action == null) { + throw new IllegalArgumentException("action shouldn't be null"); + } + mCommandCode = COMMAND_CODE_CUSTOM; + mCustomCommand = action; + mExtra = extra; + } + + public int getCommandCode() { + return mCommandCode; + } + + public @Nullable String getCustomCommand() { + return mCustomCommand; + } + + public @Nullable Bundle getExtra() { + return mExtra; + } + + /** + * @return a new Bundle instance from the Command + * @hide + */ + public Bundle toBundle() { + Bundle bundle = new Bundle(); + bundle.putInt(KEY_COMMAND_CODE, mCommandCode); + bundle.putString(KEY_COMMAND_CUSTOM_COMMAND, mCustomCommand); + bundle.putBundle(KEY_COMMAND_EXTRA, mExtra); + return bundle; + } + + /** + * @return a new Command instance from the Bundle + * @hide + */ + public static Command fromBundle(Bundle command) { + int code = command.getInt(KEY_COMMAND_CODE); + if (code != COMMAND_CODE_CUSTOM) { + return new Command(code); + } else { + String customCommand = command.getString(KEY_COMMAND_CUSTOM_COMMAND); + if (customCommand == null) { + return null; + } + return new Command(customCommand, command.getBundle(KEY_COMMAND_EXTRA)); + } + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Command)) { + return false; + } + Command other = (Command) obj; + // TODO(jaewan): Should we also compare contents in bundle? + // It may not be possible if the bundle contains private class. + return mCommandCode == other.mCommandCode + && TextUtils.equals(mCustomCommand, other.mCustomCommand); + } + + @Override + public int hashCode() { + final int prime = 31; + return ((mCustomCommand != null) ? mCustomCommand.hashCode() : 0) * prime + mCommandCode; + } + } + + /** + * Represent set of {@link Command}. + */ + // TODO(jaewan): Move this to updatable + public static class CommandGroup { + private static final String KEY_COMMANDS = + "android.media.mediasession2.commandgroup.commands"; + private ArraySet mCommands = new ArraySet<>(); + + public CommandGroup() { + } + + public CommandGroup(CommandGroup others) { + mCommands.addAll(others.mCommands); + } + + public void addCommand(Command command) { + mCommands.add(command); + } + + public void addAllPredefinedCommands() { + // TODO(jaewan): Is there any better way than this? + mCommands.add(new Command(COMMAND_CODE_PLAYBACK_START)); + mCommands.add(new Command(COMMAND_CODE_PLAYBACK_PAUSE)); + mCommands.add(new Command(COMMAND_CODE_PLAYBACK_STOP)); + mCommands.add(new Command(COMMAND_CODE_PLAYBACK_SKIP_NEXT_ITEM)); + mCommands.add(new Command(COMMAND_CODE_PLAYBACK_SKIP_PREV_ITEM)); + } + + public void removeCommand(Command command) { + mCommands.remove(command); + } + + public boolean hasCommand(Command command) { + return mCommands.contains(command); + } + + public boolean hasCommand(int code) { + if (code == COMMAND_CODE_CUSTOM) { + throw new IllegalArgumentException("Use hasCommand(Command) for custom command"); + } + for (int i = 0; i < mCommands.size(); i++) { + if (mCommands.valueAt(i).getCommandCode() == code) { + return true; + } + } + return false; + } + + /** + * @return new bundle from the CommandGroup + * @hide + */ + public Bundle toBundle() { + ArrayList list = new ArrayList<>(); + for (int i = 0; i < mCommands.size(); i++) { + list.add(mCommands.valueAt(i).toBundle()); + } + Bundle bundle = new Bundle(); + bundle.putParcelableArrayList(KEY_COMMANDS, list); + return bundle; + } + + /** + * @return new instance of CommandGroup from the bundle + * @hide + */ + public static @Nullable CommandGroup fromBundle(Bundle commands) { + if (commands == null) { + return null; + } + List list = commands.getParcelableArrayList(KEY_COMMANDS); + if (list == null) { + return null; + } + CommandGroup commandGroup = new CommandGroup(); + for (int i = 0; i < list.size(); i++) { + Parcelable parcelable = list.get(i); + if (!(parcelable instanceof Bundle)) { + continue; + } + Bundle commandBundle = (Bundle) parcelable; + Command command = Command.fromBundle(commandBundle); + if (command != null) { + commandGroup.addCommand(command); + } + } + return commandGroup; + } + } + + /** + * Callback to be called for all incoming commands from {@link MediaController2}s. + *

      + * If it's not set, the session will accept all controllers and all incoming commands by + * default. + */ + // TODO(jaewan): Can we move this inside of the updatable for default implementation. + public static class SessionCallback { + /** + * Called when a controller is created for this session. Return allowed commands for + * controller. By default it allows all connection requests and commands. + *

      + * You can reject the connection by return {@code null}. In that case, controller receives + * {@link MediaController2.ControllerCallback#onDisconnected()} and cannot be usable. + * + * @param controller controller information. + * @return allowed commands. Can be {@code null} to reject coonnection. + */ + // TODO(jaewan): Change return type. Once we do, null is for reject. + public @Nullable CommandGroup onConnect(@NonNull ControllerInfo controller) { + CommandGroup commands = new CommandGroup(); + commands.addAllPredefinedCommands(); + return commands; + } + + /** + * Called when a controller is disconnected + * + * @param controller controller information + */ + public void onDisconnected(@NonNull ControllerInfo controller) { } + + /** + * Called when a controller sent a command to the session, and the command will be sent to + * the player directly unless you reject the request by {@code false}. + * + * @param controller controller information. + * @param command a command. This method will be called for every single command. + * @return {@code true} if you want to accept incoming command. {@code false} otherwise. + */ + // TODO(jaewan): Add more documentations (or make it clear) which commands can be filtered + // with this. + public boolean onCommandRequest(@NonNull ControllerInfo controller, + @NonNull Command command) { + return true; + } + + /** + * Called when a controller set rating on the currently playing contents. + * + * @param + */ + public void onSetRating(@NonNull ControllerInfo controller, @NonNull Rating2 rating) { } + + /** + * Called when a controller sent a custom command. + * + * @param controller controller information + * @param customCommand custom command. + * @param args optional arguments + * @param cb optional result receiver + */ + public void onCustomCommand(@NonNull ControllerInfo controller, + @NonNull Command customCommand, @Nullable Bundle args, + @Nullable ResultReceiver cb) { } + + /** + * Override to handle requests to prepare for playing a specific mediaId. + * During the preparation, a session should not hold audio focus in order to allow other + * sessions play seamlessly. The state of playback should be updated to + * {@link PlaybackState#STATE_PAUSED} after the preparation is done. + *

      + * The playback of the prepared content should start in the later calls of + * {@link MediaSession2#play()}. + *

      + * Override {@link #onPlayFromMediaId} to handle requests for starting + * playback without preparation. + */ + public void onPlayFromMediaId(@NonNull ControllerInfo controller, + @NonNull String mediaId, @Nullable Bundle extras) { } + + /** + * Override to handle requests to prepare playback from a search query. An empty query + * indicates that the app may prepare any music. The implementation should attempt to make a + * smart choice about what to play. During the preparation, a session should not hold audio + * focus in order to allow other sessions play seamlessly. The state of playback should be + * updated to {@link PlaybackState#STATE_PAUSED} after the preparation is done. + *

      + * The playback of the prepared content should start in the later calls of + * {@link MediaSession2#play()}. + *

      + * Override {@link #onPlayFromSearch} to handle requests for starting playback without + * preparation. + */ + public void onPlayFromSearch(@NonNull ControllerInfo controller, + @NonNull String query, @Nullable Bundle extras) { } + + /** + * Override to handle requests to prepare a specific media item represented by a URI. + * During the preparation, a session should not hold audio focus in order to allow + * other sessions play seamlessly. The state of playback should be updated to + * {@link PlaybackState#STATE_PAUSED} after the preparation is done. + *

      + * The playback of the prepared content should start in the later calls of + * {@link MediaSession2#play()}. + *

      + * Override {@link #onPlayFromUri} to handle requests for starting playback without + * preparation. + */ + public void onPlayFromUri(@NonNull ControllerInfo controller, + @NonNull String uri, @Nullable Bundle extras) { } + + /** + * Override to handle requests to play a specific mediaId. + */ + public void onPrepareFromMediaId(@NonNull ControllerInfo controller, + @NonNull String mediaId, @Nullable Bundle extras) { } + + /** + * Override to handle requests to begin playback from a search query. An + * empty query indicates that the app may play any music. The + * implementation should attempt to make a smart choice about what to + * play. + */ + public void onPrepareFromSearch(@NonNull ControllerInfo controller, + @NonNull String query, @Nullable Bundle extras) { } + + /** + * Override to handle requests to play a specific media item represented by a URI. + */ + public void prepareFromUri(@NonNull ControllerInfo controller, + @NonNull Uri uri, @Nullable Bundle extras) { } + + /** + * Called when a controller wants to add a {@link MediaItem2} at the specified position + * in the play queue. + *

      + * The item from the media controller wouldn't have valid data source descriptor because + * it would have been anonymized when it's sent to the remote process. + * + * @param item The media item to be inserted. + * @param index The index at which the item is to be inserted. + */ + public void onAddPlaylistItem(@NonNull ControllerInfo controller, + @NonNull MediaItem2 item, int index) { } + + /** + * Called when a controller wants to remove the {@link MediaItem2} + * + * @param item + */ + // Can we do this automatically? + public void onRemovePlaylistItem(@NonNull MediaItem2 item) { } + }; + + /** + * Base builder class for MediaSession2 and its subclass. + * + * @hide + */ + static abstract class BuilderBase + , C extends SessionCallback> { + final Context mContext; + final MediaPlayerBase mPlayer; + String mId; + Executor mCallbackExecutor; + C mCallback; + VolumeProvider mVolumeProvider; + int mRatingType; + PendingIntent mSessionActivity; + + /** + * Constructor. + * + * @param context a context + * @param player a player to handle incoming command from any controller. + * @throws IllegalArgumentException if any parameter is null, or the player is a + * {@link MediaSession2} or {@link MediaController2}. + */ + // TODO(jaewan): Also need executor + public BuilderBase(@NonNull Context context, @NonNull MediaPlayerBase player) { + if (context == null) { + throw new IllegalArgumentException("context shouldn't be null"); + } + if (player == null) { + throw new IllegalArgumentException("player shouldn't be null"); + } + mContext = context; + mPlayer = player; + // Ensure non-null + mId = ""; + } + + /** + * Set volume provider to configure this session to use remote volume handling. + * This must be called to receive volume button events, otherwise the system + * will adjust the appropriate stream volume for this session's player. + *

      + * Set {@code null} to reset. + * + * @param volumeProvider The provider that will handle volume changes. Can be {@code null} + */ + public T setVolumeProvider(@Nullable VolumeProvider volumeProvider) { + mVolumeProvider = volumeProvider; + return (T) this; + } + + /** + * Set the style of rating used by this session. Apps trying to set the + * rating should use this style. Must be one of the following: + *

        + *
      • {@link Rating2#RATING_NONE}
      • + *
      • {@link Rating2#RATING_3_STARS}
      • + *
      • {@link Rating2#RATING_4_STARS}
      • + *
      • {@link Rating2#RATING_5_STARS}
      • + *
      • {@link Rating2#RATING_HEART}
      • + *
      • {@link Rating2#RATING_PERCENTAGE}
      • + *
      • {@link Rating2#RATING_THUMB_UP_DOWN}
      • + *
      + */ + public T setRatingType(@Rating2.Style int type) { + mRatingType = type; + return (T) this; + } + + /** + * Set an intent for launching UI for this Session. This can be used as a + * quick link to an ongoing media screen. The intent should be for an + * activity that may be started using {@link Activity#startActivity(Intent)}. + * + * @param pi The intent to launch to show UI for this session. + */ + public T setSessionActivity(@Nullable PendingIntent pi) { + mSessionActivity = pi; + return (T) this; + } + + /** + * Set ID of the session. If it's not set, an empty string with used to create a session. + *

      + * Use this if and only if your app supports multiple playback at the same time and also + * wants to provide external apps to have finer controls of them. + * + * @param id id of the session. Must be unique per package. + * @throws IllegalArgumentException if id is {@code null} + * @return + */ + public T setId(@NonNull String id) { + if (id == null) { + throw new IllegalArgumentException("id shouldn't be null"); + } + mId = id; + return (T) this; + } + + /** + * Set {@link SessionCallback}. + * + * @param executor callback executor + * @param callback session callback. + * @return + */ + public T setSessionCallback(@NonNull @CallbackExecutor Executor executor, + @NonNull C callback) { + if (executor == null) { + throw new IllegalArgumentException("executor shouldn't be null"); + } + if (callback == null) { + throw new IllegalArgumentException("callback shouldn't be null"); + } + mCallbackExecutor = executor; + mCallback = callback; + return (T) this; + } + + /** + * Build {@link MediaSession2}. + * + * @return a new session + * @throws IllegalStateException if the session with the same id is already exists for the + * package. + */ + public abstract MediaSession2 build() throws IllegalStateException; + } + + /** + * Builder for {@link MediaSession2}. + *

      + * Any incoming event from the {@link MediaController2} will be handled on the thread + * that created session with the {@link Builder#build()}. + */ + // TODO(jaewan): Move this to updatable + // TODO(jaewan): Add setRatingType() + // TODO(jaewan): Add setSessionActivity() + public static final class Builder extends BuilderBase { + public Builder(Context context, @NonNull MediaPlayerBase player) { + super(context, player); + } + + @Override + public MediaSession2 build() throws IllegalStateException { + if (mCallback == null) { + mCallback = new SessionCallback(); + } + return new MediaSession2(mContext, mPlayer, mId, mCallbackExecutor, mCallback, + mVolumeProvider, mRatingType, mSessionActivity); + } + } + + /** + * Information of a controller. + */ + // TODO(jaewan): Move implementation to the updatable. + public static final class ControllerInfo { + private final ControllerInfoProvider mProvider; + + /** + * @hide + */ + // TODO(jaewan): SystemApi + // TODO(jaewan): Also accept componentName to check notificaiton listener. + public ControllerInfo(Context context, int uid, int pid, String packageName, + IMediaSession2Callback callback) { + mProvider = ApiLoader.getProvider(context) + .createMediaSession2ControllerInfoProvider( + this, context, uid, pid, packageName, callback); + } + + /** + * @return package name of the controller + */ + public String getPackageName() { + return mProvider.getPackageName_impl(); + } + + /** + * @return uid of the controller + */ + public int getUid() { + return mProvider.getUid_impl(); + } + + /** + * Return if the controller has granted {@code android.permission.MEDIA_CONTENT_CONTROL} or + * has a enabled notification listener so can be trusted to accept connection and incoming + * command request. + * + * @return {@code true} if the controller is trusted. + */ + public boolean isTrusted() { + return mProvider.isTrusted_impl(); + } + + /** + * @hide + * @return + */ + // TODO(jaewan): SystemApi + public ControllerInfoProvider getProvider() { + return mProvider; + } + + @Override + public int hashCode() { + return mProvider.hashCode_impl(); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof ControllerInfo)) { + return false; + } + ControllerInfo other = (ControllerInfo) obj; + return mProvider.equals_impl(other.mProvider); + } + + @Override + public String toString() { + // TODO(jaewan): Move this to updatable. + return "ControllerInfo {pkg=" + getPackageName() + ", uid=" + getUid() + ", trusted=" + + isTrusted() + "}"; + } + } + + /** + * Button for a {@link Command} that will be shown by the controller. + *

      + * It's up to the controller's decision to respect or ignore this customization request. + */ + // TODO(jaewan): Move this to updatable. + public static class CommandButton { + private static final String KEY_COMMAND + = "android.media.media_session2.command_button.command"; + private static final String KEY_ICON_RES_ID + = "android.media.media_session2.command_button.icon_res_id"; + private static final String KEY_DISPLAY_NAME + = "android.media.media_session2.command_button.display_name"; + private static final String KEY_EXTRA + = "android.media.media_session2.command_button.extra"; + private static final String KEY_ENABLED + = "android.media.media_session2.command_button.enabled"; + + private Command mCommand; + private int mIconResId; + private String mDisplayName; + private Bundle mExtra; + private boolean mEnabled; + + private CommandButton(@Nullable Command command, int iconResId, + @Nullable String displayName, Bundle extra, boolean enabled) { + mCommand = command; + mIconResId = iconResId; + mDisplayName = displayName; + mExtra = extra; + mEnabled = enabled; + } + + /** + * Get command associated with this button. Can be {@code null} if the button isn't enabled + * and only providing placeholder. + * + * @return command or {@code null} + */ + public @Nullable Command getCommand() { + return mCommand; + } + + /** + * Resource id of the button in this package. Can be {@code 0} if the command is predefined + * and custom icon isn't needed. + * + * @return resource id of the icon. Can be {@code 0}. + */ + public int getIconResId() { + return mIconResId; + } + + /** + * Display name of the button. Can be {@code null} or empty if the command is predefined + * and custom name isn't needed. + * + * @return custom display name. Can be {@code null} or empty. + */ + public @Nullable String getDisplayName() { + return mDisplayName; + } + + /** + * Extra information of the button. It's private information between session and controller. + * + * @return + */ + public @Nullable Bundle getExtra() { + return mExtra; + } + + /** + * Return whether it's enabled + * + * @return {@code true} if enabled. {@code false} otherwise. + */ + public boolean isEnabled() { + return mEnabled; + } + + /** + * @hide + */ + // TODO(jaewan): @SystemApi + public @NonNull Bundle toBundle() { + Bundle bundle = new Bundle(); + bundle.putBundle(KEY_COMMAND, mCommand.toBundle()); + bundle.putInt(KEY_ICON_RES_ID, mIconResId); + bundle.putString(KEY_DISPLAY_NAME, mDisplayName); + bundle.putBundle(KEY_EXTRA, mExtra); + bundle.putBoolean(KEY_ENABLED, mEnabled); + return bundle; + } + + /** + * @hide + */ + // TODO(jaewan): @SystemApi + public static @Nullable CommandButton fromBundle(Bundle bundle) { + Builder builder = new Builder(); + builder.setCommand(Command.fromBundle(bundle.getBundle(KEY_COMMAND))); + builder.setIconResId(bundle.getInt(KEY_ICON_RES_ID, 0)); + builder.setDisplayName(bundle.getString(KEY_DISPLAY_NAME)); + builder.setExtra(bundle.getBundle(KEY_EXTRA)); + builder.setEnabled(bundle.getBoolean(KEY_ENABLED)); + try { + return builder.build(); + } catch (IllegalStateException e) { + // Malformed or version mismatch. Return null for now. + return null; + } + } + + /** + * Builder for {@link CommandButton}. + */ + public static class Builder { + private Command mCommand; + private int mIconResId; + private String mDisplayName; + private Bundle mExtra; + private boolean mEnabled; + + public Builder() { + mEnabled = true; + } + + public Builder setCommand(Command command) { + mCommand = command; + return this; + } + + public Builder setIconResId(int resId) { + mIconResId = resId; + return this; + } + + public Builder setDisplayName(String displayName) { + mDisplayName = displayName; + return this; + } + + public Builder setEnabled(boolean enabled) { + mEnabled = enabled; + return this; + } + + public Builder setExtra(Bundle extra) { + mExtra = extra; + return this; + } + + public CommandButton build() { + if (mEnabled && mCommand == null) { + throw new IllegalStateException("Enabled button needs Command" + + " for controller to invoke the command"); + } + if (mCommand != null && mCommand.getCommandCode() == COMMAND_CODE_CUSTOM + && (mIconResId == 0 || TextUtils.isEmpty(mDisplayName))) { + throw new IllegalStateException("Custom commands needs icon and" + + " and name to display"); + } + return new CommandButton(mCommand, mIconResId, mDisplayName, mExtra, mEnabled); + } + } + } + + /** + * Parameter for the playlist. + */ + // TODO(jaewan): add fromBundle()/toBundle() + public static class PlaylistParam { + /** + * @hide + */ + @IntDef({REPEAT_MODE_NONE, REPEAT_MODE_ONE, REPEAT_MODE_ALL, + REPEAT_MODE_GROUP}) + @Retention(RetentionPolicy.SOURCE) + public @interface RepeatMode {} + + /** + * Playback will be stopped at the end of the playing media list. + */ + public static final int REPEAT_MODE_NONE = 0; + + /** + * Playback of the current playing media item will be repeated. + */ + public static final int REPEAT_MODE_ONE = 1; + + /** + * Playing media list will be repeated. + */ + public static final int REPEAT_MODE_ALL = 2; + + /** + * Playback of the playing media group will be repeated. + * A group is a logical block of media items which is specified in the section 5.7 of the + * Bluetooth AVRCP 1.6. + */ + public static final int REPEAT_MODE_GROUP = 3; + + /** + * @hide + */ + @IntDef({SHUFFLE_MODE_NONE, SHUFFLE_MODE_ALL, SHUFFLE_MODE_GROUP}) + @Retention(RetentionPolicy.SOURCE) + public @interface ShuffleMode {} + + /** + * Media list will be played in order. + */ + public static final int SHUFFLE_MODE_NONE = 0; + + /** + * Media list will be played in shuffled order. + */ + public static final int SHUFFLE_MODE_ALL = 1; + + /** + * Media group will be played in shuffled order. + * A group is a logical block of media items which is specified in the section 5.7 of the + * Bluetooth AVRCP 1.6. + */ + public static final int SHUFFLE_MODE_GROUP = 2; + + private @RepeatMode int mRepeatMode; + private @ShuffleMode int mShuffleMode; + + private MediaMetadata2 mPlaylistMetadata; + + public PlaylistParam(@RepeatMode int repeatMode, @ShuffleMode int shuffleMode, + @Nullable MediaMetadata2 playlistMetadata) { + mRepeatMode = repeatMode; + mShuffleMode = shuffleMode; + mPlaylistMetadata = playlistMetadata; + } + + public @RepeatMode int getRepeatMode() { + return mRepeatMode; + } + + public @ShuffleMode int getShuffleMode() { + return mShuffleMode; + } + + public MediaMetadata2 getPlaylistMetadata() { + return mPlaylistMetadata; + } + } + + /** + * Constructor is hidden and apps can only instantiate indirectly through {@link Builder}. + *

      + * This intended behavior and here's the reasons. + * 1. Prevent multiple sessions with the same tag in a media app. + * Whenever it happens only one session was properly setup and others were all dummies. + * Android framework couldn't find the right session to dispatch media key event. + * 2. Simplify session's lifecycle. + * {@link MediaSession} can be available after all of {@link MediaSession#setFlags(int)}, + * {@link MediaSession#setCallback(Callback)}, and + * {@link MediaSession#setActive(boolean)}. It was common for an app to omit one, so + * framework had to add heuristics to figure out if an app is + * @hide + */ + MediaSession2(Context context, MediaPlayerBase player, String id, Executor callbackExecutor, + SessionCallback callback, VolumeProvider volumeProvider, int ratingType, + PendingIntent sessionActivity) { + super(); + mProvider = createProvider(context, player, id, callbackExecutor, callback, + volumeProvider, ratingType, sessionActivity); + } + + MediaSession2Provider createProvider(Context context, MediaPlayerBase player, String id, + Executor callbackExecutor, SessionCallback callback, VolumeProvider volumeProvider, + int ratingType, PendingIntent sessionActivity) { + return ApiLoader.getProvider(context) + .createMediaSession2(this, context, player, id, callbackExecutor, + callback, volumeProvider, ratingType, sessionActivity); + } + + /** + * @hide + */ + // TODO(jaewan): SystemApi + public MediaSession2Provider getProvider() { + return mProvider; + } + + /** + * Set the underlying {@link MediaPlayerBase} for this session to dispatch incoming event to. + * Events from the {@link MediaController2} will be sent directly to the underlying + * player on the {@link Handler} where the session is created on. + *

      + * If the new player is successfully set, {@link PlaybackListener} + * will be called to tell the current playback state of the new player. + *

      + * You can also specify a volume provider. If so, playback in the player is considered as + * remote playback. + * + * @param player a {@link MediaPlayerBase} that handles actual media playback in your app. + * @throws IllegalArgumentException if the player is {@code null}. + */ + public void setPlayer(@NonNull MediaPlayerBase player) { + mProvider.setPlayer_impl(player); + } + + /** + * Set the underlying {@link MediaPlayerBase} with the volume provider for remote playback. + * + * @param player a {@link MediaPlayerBase} that handles actual media playback in your app. + * @param volumeProvider a volume provider + * @see #setPlayer(MediaPlayerBase) + * @see Builder#setVolumeProvider(VolumeProvider) + * @throws IllegalArgumentException if a parameter is {@code null}. + */ + public void setPlayer(@NonNull MediaPlayerBase player, @NonNull VolumeProvider volumeProvider) + throws IllegalArgumentException { + mProvider.setPlayer_impl(player, volumeProvider); + } + + @Override + public void close() { + mProvider.close_impl(); + } + + /** + * @return player + */ + public @Nullable MediaPlayerBase getPlayer() { + return mProvider.getPlayer_impl(); + } + + /** + * Returns the {@link SessionToken2} for creating {@link MediaController2}. + */ + public @NonNull + SessionToken2 getToken() { + return mProvider.getToken_impl(); + } + + public @NonNull List getConnectedControllers() { + return mProvider.getConnectedControllers_impl(); + } + + /** + * Sets the {@link AudioAttributes} to be used during the playback of the video. + * + * @param attributes non-null AudioAttributes. + */ + public void setAudioAttributes(@NonNull AudioAttributes attributes) { + mProvider.setAudioAttributes_impl(attributes); + } + + /** + * Sets which type of audio focus will be requested during the playback, or configures playback + * to not request audio focus. Valid values for focus requests are + * {@link AudioManager#AUDIOFOCUS_GAIN}, {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT}, + * {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK}, and + * {@link AudioManager#AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE}. Or use + * {@link AudioManager#AUDIOFOCUS_NONE} to express that audio focus should not be + * requested when playback starts. You can for instance use this when playing a silent animation + * through this class, and you don't want to affect other audio applications playing in the + * background. + * + * @param focusGain the type of audio focus gain that will be requested, or + * {@link AudioManager#AUDIOFOCUS_NONE} to disable the use audio focus during + * playback. + */ + public void setAudioFocusRequest(int focusGain) { + mProvider.setAudioFocusRequest_impl(focusGain); + } + + /** + * Sets ordered list of {@link CommandButton} for controllers to build UI with it. + *

      + * It's up to controller's decision how to represent the layout in its own UI. + * Here's the same way + * (layout[i] means a CommandButton at index i in the given list) + * For 5 icons row + * layout[3] layout[1] layout[0] layout[2] layout[4] + * For 3 icons row + * layout[1] layout[0] layout[2] + * For 5 icons row with overflow icon (can show +5 extra buttons with overflow button) + * expanded row: layout[5] layout[6] layout[7] layout[8] layout[9] + * main row: layout[3] layout[1] layout[0] layout[2] layout[4] + *

      + * This API can be called in the {@link SessionCallback#onConnect(ControllerInfo)}. + * + * @param controller controller to specify layout. + * @param layout oredered list of layout. + */ + public void setCustomLayout(@NonNull ControllerInfo controller, + @NonNull List layout) { + mProvider.setCustomLayout_impl(controller, layout); + } + + /** + * Set the new allowed command group for the controller + * + * @param controller controller to change allowed commands + * @param commands new allowed commands + */ + public void setAllowedCommands(@NonNull ControllerInfo controller, + @NonNull CommandGroup commands) { + mProvider.setAllowedCommands_impl(controller, commands); + } + + /** + * Notify changes in metadata of previously set playlist. Controller will get the whole set of + * playlist again. + */ + public void notifyMetadataChanged() { + mProvider.notifyMetadataChanged_impl(); + } + + /** + * Send custom command to all connected controllers. + * + * @param command a command + * @param args optional argument + */ + public void sendCustomCommand(@NonNull Command command, @Nullable Bundle args) { + mProvider.sendCustomCommand_impl(command, args); + } + + /** + * Send custom command to a specific controller. + * + * @param command a command + * @param args optional argument + * @param receiver result receiver for the session + */ + public void sendCustomCommand(@NonNull ControllerInfo controller, @NonNull Command command, + @Nullable Bundle args, @Nullable ResultReceiver receiver) { + // Equivalent to the MediaController.sendCustomCommand(Action action, ResultReceiver r); + mProvider.sendCustomCommand_impl(controller, command, args, receiver); + } + + /** + * Play playback + */ + public void play() { + mProvider.play_impl(); + } + + /** + * Pause playback + */ + public void pause() { + mProvider.pause_impl(); + } + + /** + * Stop playback + */ + public void stop() { + mProvider.stop_impl(); + } + + /** + * Rewind playback + */ + public void skipToPrevious() { + mProvider.skipToPrevious_impl(); + } + + /** + * Rewind playback + */ + public void skipToNext() { + mProvider.skipToNext_impl(); + } + + /** + * Request that the player prepare its playback. In other words, other sessions can continue + * to play during the preparation of this session. This method can be used to speed up the + * start of the playback. Once the preparation is done, the session will change its playback + * state to {@link PlaybackState#STATE_PAUSED}. Afterwards, {@link #play} can be called to + * start playback. + */ + public void prepare() { + mProvider.prepare_impl(); + } + + /** + * Start fast forwarding. If playback is already fast forwarding this may increase the rate. + */ + public void fastForward() { + mProvider.fastForward_impl(); + } + + /** + * Start rewinding. If playback is already rewinding this may increase the rate. + */ + public void rewind() { + mProvider.rewind_impl(); + } + + /** + * Move to a new location in the media stream. + * + * @param pos Position to move to, in milliseconds. + */ + public void seekTo(long pos) { + mProvider.seekTo_impl(pos); + } + + /** + * Sets the index of current DataSourceDesc in the play list to be played. + * + * @param index the index of DataSourceDesc in the play list you want to play + * @throws IllegalArgumentException if the play list is null + * @throws NullPointerException if index is outside play list range + */ + public void setCurrentPlaylistItem(int index) { + mProvider.setCurrentPlaylistItem_impl(index); + } + + /** + * @hide + */ + public void skipForward() { + // To match with KEYCODE_MEDIA_SKIP_FORWARD + } + + /** + * @hide + */ + public void skipBackward() { + // To match with KEYCODE_MEDIA_SKIP_BACKWARD + } + + public void setPlaylist(@NonNull List playlist, @NonNull PlaylistParam param) { + mProvider.setPlaylist_impl(playlist, param); + } +} diff --git a/android/media/MediaSession2Test.java b/android/media/MediaSession2Test.java new file mode 100644 index 00000000..045dcd5a --- /dev/null +++ b/android/media/MediaSession2Test.java @@ -0,0 +1,273 @@ +/* + * Copyright 2018 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.media.MediaPlayerBase.PlaybackListener; +import android.media.MediaSession2.Builder; +import android.media.MediaSession2.ControllerInfo; +import android.media.MediaSession2.SessionCallback; +import android.media.session.PlaybackState; +import android.os.Process; +import android.os.Looper; +import android.support.annotation.NonNull; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; + +import java.util.ArrayList; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import static android.media.TestUtils.createPlaybackState; +import static org.junit.Assert.*; + +/** + * Tests {@link MediaSession2}. + */ +@RunWith(AndroidJUnit4.class) +@SmallTest +public class MediaSession2Test extends MediaSession2TestBase { + private static final String TAG = "MediaSession2Test"; + + private MediaSession2 mSession; + private MockPlayer mPlayer; + + @Before + @Override + public void setUp() throws Exception { + super.setUp(); + sHandler.postAndSync(() -> { + mPlayer = new MockPlayer(0); + mSession = new MediaSession2.Builder(mContext, mPlayer).build(); + }); + } + + @After + @Override + public void cleanUp() throws Exception { + super.cleanUp(); + sHandler.postAndSync(() -> { + mSession.close(); + }); + } + + @Test + public void testBuilder() throws Exception { + try { + MediaSession2.Builder builder = new Builder(mContext, null); + fail("null player shouldn't be allowed"); + } catch (IllegalArgumentException e) { + // expected. pass-through + } + MediaSession2.Builder builder = new Builder(mContext, mPlayer); + try { + builder.setId(null); + fail("null id shouldn't be allowed"); + } catch (IllegalArgumentException e) { + // expected. pass-through + } + } + + @Test + public void testSetPlayer() throws Exception { + sHandler.postAndSync(() -> { + MockPlayer player = new MockPlayer(0); + // Test if setPlayer doesn't crash with various situations. + mSession.setPlayer(mPlayer); + mSession.setPlayer(player); + mSession.close(); + }); + } + + @Test + public void testPlay() throws Exception { + sHandler.postAndSync(() -> { + mSession.play(); + assertTrue(mPlayer.mPlayCalled); + }); + } + + @Test + public void testPause() throws Exception { + sHandler.postAndSync(() -> { + mSession.pause(); + assertTrue(mPlayer.mPauseCalled); + }); + } + + @Test + public void testStop() throws Exception { + sHandler.postAndSync(() -> { + mSession.stop(); + assertTrue(mPlayer.mStopCalled); + }); + } + + @Test + public void testSkipToNext() throws Exception { + sHandler.postAndSync(() -> { + mSession.skipToNext(); + assertTrue(mPlayer.mSkipToNextCalled); + }); + } + + @Test + public void testSkipToPrevious() throws Exception { + sHandler.postAndSync(() -> { + mSession.skipToPrevious(); + assertTrue(mPlayer.mSkipToPreviousCalled); + }); + } + + @Test + public void testPlaybackStateChangedListener() throws InterruptedException { + // TODO(jaewan): Add equivalent tests again + /* + final CountDownLatch latch = new CountDownLatch(2); + final MockPlayer player = new MockPlayer(0); + final PlaybackListener listener = (state) -> { + assertEquals(sHandler.getLooper(), Looper.myLooper()); + assertNotNull(state); + switch ((int) latch.getCount()) { + case 2: + assertEquals(PlaybackState.STATE_PLAYING, state.getState()); + break; + case 1: + assertEquals(PlaybackState.STATE_PAUSED, state.getState()); + break; + case 0: + fail(); + } + latch.countDown(); + }; + player.notifyPlaybackState(createPlaybackState(PlaybackState.STATE_PLAYING)); + sHandler.postAndSync(() -> { + mSession.addPlaybackListener(listener, sHandler); + // When the player is set, listeners will be notified about the player's current state. + mSession.setPlayer(player); + }); + player.notifyPlaybackState(createPlaybackState(PlaybackState.STATE_PAUSED)); + assertTrue(latch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); + */ + } + + @Test + public void testBadPlayer() throws InterruptedException { + // TODO(jaewan): Add equivalent tests again + /* + final CountDownLatch latch = new CountDownLatch(3); // expected call + 1 + final BadPlayer player = new BadPlayer(0); + sHandler.postAndSync(() -> { + mSession.addPlaybackListener((state) -> { + // This will be called for every setPlayer() calls, but no more. + assertNull(state); + latch.countDown(); + }, sHandler); + mSession.setPlayer(player); + mSession.setPlayer(mPlayer); + }); + player.notifyPlaybackState(createPlaybackState(PlaybackState.STATE_PAUSED)); + assertFalse(latch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS)); + */ + } + + private static class BadPlayer extends MockPlayer { + public BadPlayer(int count) { + super(count); + } + + @Override + public void removePlaybackListener(@NonNull PlaybackListener listener) { + // No-op. This bad player will keep push notification to the listener that is previously + // registered by session.setPlayer(). + } + } + + @Test + public void testOnCommandCallback() throws InterruptedException { + final MockOnCommandCallback callback = new MockOnCommandCallback(); + sHandler.postAndSync(() -> { + mSession.close(); + mPlayer = new MockPlayer(1); + mSession = new MediaSession2.Builder(mContext, mPlayer) + .setSessionCallback(sHandlerExecutor, callback).build(); + }); + MediaController2 controller = createController(mSession.getToken()); + controller.pause(); + assertFalse(mPlayer.mCountDownLatch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS)); + assertFalse(mPlayer.mPauseCalled); + assertEquals(1, callback.commands.size()); + assertEquals(MediaSession2.COMMAND_CODE_PLAYBACK_PAUSE, + (long) callback.commands.get(0).getCommandCode()); + controller.skipToNext(); + assertTrue(mPlayer.mCountDownLatch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS)); + assertTrue(mPlayer.mSkipToNextCalled); + assertFalse(mPlayer.mPauseCalled); + assertEquals(2, callback.commands.size()); + assertEquals(MediaSession2.COMMAND_CODE_PLAYBACK_SKIP_NEXT_ITEM, + (long) callback.commands.get(1).getCommandCode()); + } + + @Test + public void testOnConnectCallback() throws InterruptedException { + final MockOnConnectCallback sessionCallback = new MockOnConnectCallback(); + sHandler.postAndSync(() -> { + mSession.close(); + mSession = new MediaSession2.Builder(mContext, mPlayer) + .setSessionCallback(sHandlerExecutor, sessionCallback).build(); + }); + MediaController2 controller = + createController(mSession.getToken(), false, null); + assertNotNull(controller); + waitForConnect(controller, false); + waitForDisconnect(controller, true); + } + + public class MockOnConnectCallback extends SessionCallback { + @Override + public MediaSession2.CommandGroup onConnect(ControllerInfo controllerInfo) { + if (Process.myUid() != controllerInfo.getUid()) { + return null; + } + assertEquals(mContext.getPackageName(), controllerInfo.getPackageName()); + assertEquals(Process.myUid(), controllerInfo.getUid()); + assertFalse(controllerInfo.isTrusted()); + // Reject all + return null; + } + } + + public class MockOnCommandCallback extends SessionCallback { + public final ArrayList commands = new ArrayList<>(); + + @Override + public boolean onCommandRequest(ControllerInfo controllerInfo, MediaSession2.Command command) { + assertEquals(mContext.getPackageName(), controllerInfo.getPackageName()); + assertEquals(Process.myUid(), controllerInfo.getUid()); + assertFalse(controllerInfo.isTrusted()); + commands.add(command); + if (command.getCommandCode() == MediaSession2.COMMAND_CODE_PLAYBACK_PAUSE) { + return false; + } + return true; + } + } +} diff --git a/android/media/MediaSession2TestBase.java b/android/media/MediaSession2TestBase.java new file mode 100644 index 00000000..96afcb90 --- /dev/null +++ b/android/media/MediaSession2TestBase.java @@ -0,0 +1,210 @@ +/* + * Copyright 2018 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 static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertTrue; + +import android.content.Context; +import android.media.MediaController2.ControllerCallback; +import android.media.MediaSession2.CommandGroup; +import android.os.Bundle; +import android.os.HandlerThread; +import android.support.annotation.CallSuper; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.test.InstrumentationRegistry; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executor; +import java.util.concurrent.TimeUnit; +import org.junit.AfterClass; +import org.junit.BeforeClass; + +/** + * Base class for session test. + */ +abstract class MediaSession2TestBase { + // Expected success + static final int WAIT_TIME_MS = 1000; + + // Expected timeout + static final int TIMEOUT_MS = 500; + + static TestUtils.SyncHandler sHandler; + static Executor sHandlerExecutor; + + Context mContext; + private List mControllers = new ArrayList<>(); + + interface TestControllerInterface { + ControllerCallback getCallback(); + } + + interface TestControllerCallbackInterface { + // Currently empty. Add methods in ControllerCallback/BrowserCallback that you want to test. + + // Browser specific callbacks + default void onGetRootResult(Bundle rootHints, String rootMediaId, Bundle rootExtra) {} + } + + interface WaitForConnectionInterface { + void waitForConnect(boolean expect) throws InterruptedException; + void waitForDisconnect(boolean expect) throws InterruptedException; + } + + @BeforeClass + public static void setUpThread() { + if (sHandler == null) { + HandlerThread handlerThread = new HandlerThread("MediaSession2TestBase"); + handlerThread.start(); + sHandler = new TestUtils.SyncHandler(handlerThread.getLooper()); + sHandlerExecutor = (runnable) -> { + sHandler.post(runnable); + }; + } + } + + @AfterClass + public static void cleanUpThread() { + if (sHandler != null) { + sHandler.getLooper().quitSafely(); + sHandler = null; + sHandlerExecutor = null; + } + } + + @CallSuper + public void setUp() throws Exception { + mContext = InstrumentationRegistry.getTargetContext(); + } + + @CallSuper + public void cleanUp() throws Exception { + for (int i = 0; i < mControllers.size(); i++) { + mControllers.get(i).close(); + } + } + + final MediaController2 createController(SessionToken2 token) throws InterruptedException { + return createController(token, true, null); + } + + final MediaController2 createController(@NonNull SessionToken2 token, + boolean waitForConnect, @Nullable TestControllerCallbackInterface callback) + throws InterruptedException { + TestControllerInterface instance = onCreateController(token, callback); + if (!(instance instanceof MediaController2)) { + throw new RuntimeException("Test has a bug. Expected MediaController2 but returned " + + instance); + } + MediaController2 controller = (MediaController2) instance; + mControllers.add(controller); + if (waitForConnect) { + waitForConnect(controller, true); + } + return controller; + } + + private static WaitForConnectionInterface getWaitForConnectionInterface( + MediaController2 controller) { + if (!(controller instanceof TestControllerInterface)) { + throw new RuntimeException("Test has a bug. Expected controller implemented" + + " TestControllerInterface but got " + controller); + } + ControllerCallback callback = ((TestControllerInterface) controller).getCallback(); + if (!(callback instanceof WaitForConnectionInterface)) { + throw new RuntimeException("Test has a bug. Expected controller with callback " + + " implemented WaitForConnectionInterface but got " + controller); + } + return (WaitForConnectionInterface) callback; + } + + public static void waitForConnect(MediaController2 controller, boolean expected) + throws InterruptedException { + getWaitForConnectionInterface(controller).waitForConnect(expected); + } + + public static void waitForDisconnect(MediaController2 controller, boolean expected) + throws InterruptedException { + getWaitForConnectionInterface(controller).waitForDisconnect(expected); + } + + TestControllerInterface onCreateController(@NonNull SessionToken2 token, + @NonNull TestControllerCallbackInterface callback) { + return new TestMediaController(mContext, token, new TestControllerCallback(callback)); + } + + public static class TestControllerCallback extends MediaController2.ControllerCallback + implements WaitForConnectionInterface { + public final TestControllerCallbackInterface mCallbackProxy; + public final CountDownLatch connectLatch = new CountDownLatch(1); + public final CountDownLatch disconnectLatch = new CountDownLatch(1); + + TestControllerCallback(TestControllerCallbackInterface callbackProxy) { + mCallbackProxy = callbackProxy; + } + + @CallSuper + @Override + public void onConnected(CommandGroup commands) { + super.onConnected(commands); + connectLatch.countDown(); + } + + @CallSuper + @Override + public void onDisconnected() { + super.onDisconnected(); + disconnectLatch.countDown(); + } + + @Override + public void waitForConnect(boolean expect) throws InterruptedException { + if (expect) { + assertTrue(connectLatch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS)); + } else { + assertFalse(connectLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); + } + } + + @Override + public void waitForDisconnect(boolean expect) throws InterruptedException { + if (expect) { + assertTrue(disconnectLatch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS)); + } else { + assertFalse(disconnectLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)); + } + } + } + + public class TestMediaController extends MediaController2 implements TestControllerInterface { + private final ControllerCallback mCallback; + + public TestMediaController(@NonNull Context context, @NonNull SessionToken2 token, + @NonNull ControllerCallback callback) { + super(context, token, callback, sHandlerExecutor); + mCallback = callback; + } + + @Override + public ControllerCallback getCallback() { + return mCallback; + } + } +} diff --git a/android/media/MediaSessionManager_MediaSession2.java b/android/media/MediaSessionManager_MediaSession2.java new file mode 100644 index 00000000..192cbc2b --- /dev/null +++ b/android/media/MediaSessionManager_MediaSession2.java @@ -0,0 +1,223 @@ +/* + * Copyright 2018 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.content.Context; +import android.media.MediaSession2.ControllerInfo; +import android.media.MediaSession2.SessionCallback; +import android.media.session.MediaSessionManager; +import android.media.session.PlaybackState; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; + +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +import static android.media.TestUtils.createPlaybackState; +import static org.junit.Assert.*; + +/** + * Tests {@link MediaSessionManager} with {@link MediaSession2} specific APIs. + */ +@RunWith(AndroidJUnit4.class) +@SmallTest +@Ignore +// TODO(jaewan): Reenable test when the media session service detects newly installed sesison +// service app. +public class MediaSessionManager_MediaSession2 extends MediaSession2TestBase { + private static final String TAG = "MediaSessionManager_MediaSession2"; + + private MediaSessionManager mManager; + private MediaSession2 mSession; + + @Before + @Override + public void setUp() throws Exception { + super.setUp(); + mManager = (MediaSessionManager) mContext.getSystemService(Context.MEDIA_SESSION_SERVICE); + + // Specify TAG here so {@link MediaSession2.getInstance()} doesn't complaint about + // per test thread differs across the {@link MediaSession2} with the same TAG. + final MockPlayer player = new MockPlayer(1); + sHandler.postAndSync(() -> { + mSession = new MediaSession2.Builder(mContext, player).setId(TAG).build(); + }); + ensureChangeInSession(); + } + + @After + @Override + public void cleanUp() throws Exception { + super.cleanUp(); + sHandler.removeCallbacksAndMessages(null); + sHandler.postAndSync(() -> { + mSession.close(); + }); + } + + // TODO(jaewan): Make this host-side test to see per-user behavior. + @Test + public void testGetMediaSession2Tokens_hasMediaController() throws InterruptedException { + final MockPlayer player = (MockPlayer) mSession.getPlayer(); + player.notifyPlaybackState(createPlaybackState(PlaybackState.STATE_STOPPED)); + + MediaController2 controller = null; + List tokens = mManager.getActiveSessionTokens(); + assertNotNull(tokens); + for (int i = 0; i < tokens.size(); i++) { + SessionToken2 token = tokens.get(i); + if (mContext.getPackageName().equals(token.getPackageName()) + && TAG.equals(token.getId())) { + assertNotNull(token.getSessionBinder()); + assertNull(controller); + controller = createController(token); + } + } + assertNotNull(controller); + + // Test if the found controller is correct one. + assertEquals(PlaybackState.STATE_STOPPED, controller.getPlaybackState().getState()); + controller.play(); + + assertTrue(player.mCountDownLatch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS)); + assertTrue(player.mPlayCalled); + } + + /** + * Test if server recognizes session even if session refuses the connection from server. + * + * @throws InterruptedException + */ + @Test + public void testGetSessionTokens_sessionRejected() throws InterruptedException { + sHandler.postAndSync(() -> { + mSession.close(); + mSession = new MediaSession2.Builder(mContext, new MockPlayer(0)).setId(TAG) + .setSessionCallback(sHandlerExecutor, new SessionCallback() { + @Override + public MediaSession2.CommandGroup onConnect(ControllerInfo controller) { + // Reject all connection request. + return null; + } + }).build(); + }); + ensureChangeInSession(); + + boolean foundSession = false; + List tokens = mManager.getActiveSessionTokens(); + assertNotNull(tokens); + for (int i = 0; i < tokens.size(); i++) { + SessionToken2 token = tokens.get(i); + if (mContext.getPackageName().equals(token.getPackageName()) + && TAG.equals(token.getId())) { + assertFalse(foundSession); + foundSession = true; + } + } + assertTrue(foundSession); + } + + @Test + public void testGetMediaSession2Tokens_playerRemoved() throws InterruptedException { + // Release + sHandler.postAndSync(() -> { + mSession.close(); + }); + ensureChangeInSession(); + + // When the mSession's player becomes null, it should lose binder connection between server. + // So server will forget the session. + List tokens = mManager.getActiveSessionTokens(); + for (int i = 0; i < tokens.size(); i++) { + SessionToken2 token = tokens.get(i); + assertFalse(mContext.getPackageName().equals(token.getPackageName()) + && TAG.equals(token.getId())); + } + } + + @Test + public void testGetMediaSessionService2Token() throws InterruptedException { + boolean foundTestSessionService = false; + boolean foundTestLibraryService = false; + List tokens = mManager.getSessionServiceTokens(); + for (int i = 0; i < tokens.size(); i++) { + SessionToken2 token = tokens.get(i); + if (mContext.getPackageName().equals(token.getPackageName()) + && MockMediaSessionService2.ID.equals(token.getId())) { + assertFalse(foundTestSessionService); + assertEquals(SessionToken2.TYPE_SESSION_SERVICE, token.getType()); + assertNull(token.getSessionBinder()); + foundTestSessionService = true; + } else if (mContext.getPackageName().equals(token.getPackageName()) + && MockMediaLibraryService2.ID.equals(token.getId())) { + assertFalse(foundTestLibraryService); + assertEquals(SessionToken2.TYPE_LIBRARY_SERVICE, token.getType()); + assertNull(token.getSessionBinder()); + foundTestLibraryService = true; + } + } + assertTrue(foundTestSessionService); + assertTrue(foundTestLibraryService); + } + + @Test + public void testGetAllSessionTokens() throws InterruptedException { + boolean foundTestSession = false; + boolean foundTestSessionService = false; + boolean foundTestLibraryService = false; + List tokens = mManager.getAllSessionTokens(); + for (int i = 0; i < tokens.size(); i++) { + SessionToken2 token = tokens.get(i); + if (!mContext.getPackageName().equals(token.getPackageName())) { + continue; + } + switch (token.getId()) { + case TAG: + assertFalse(foundTestSession); + foundTestSession = true; + break; + case MockMediaSessionService2.ID: + assertFalse(foundTestSessionService); + foundTestSessionService = true; + assertEquals(SessionToken2.TYPE_SESSION_SERVICE, token.getType()); + break; + case MockMediaLibraryService2.ID: + assertFalse(foundTestLibraryService); + assertEquals(SessionToken2.TYPE_LIBRARY_SERVICE, token.getType()); + foundTestLibraryService = true; + break; + default: + fail("Unexpected session " + token + " exists in the package"); + } + } + assertTrue(foundTestSession); + assertTrue(foundTestSessionService); + assertTrue(foundTestLibraryService); + } + + // Ensures if the session creation/release is notified to the server. + private void ensureChangeInSession() throws InterruptedException { + // TODO(jaewan): Wait by listener. + Thread.sleep(WAIT_TIME_MS); + } +} diff --git a/android/media/MediaSessionService2.java b/android/media/MediaSessionService2.java new file mode 100644 index 00000000..19814f04 --- /dev/null +++ b/android/media/MediaSessionService2.java @@ -0,0 +1,247 @@ +/* + * Copyright 2018 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.CallSuper; +import android.annotation.NonNull; +import android.annotation.Nullable; +import android.app.Notification; +import android.app.Service; +import android.content.Intent; +import android.media.MediaSession2.ControllerInfo; +import android.media.session.PlaybackState; +import android.media.update.ApiLoader; +import android.media.update.MediaSessionService2Provider; +import android.os.IBinder; + +/** + * Base class for media session services, which is the service version of the {@link MediaSession2}. + *

      + * It's highly recommended for an app to use this instead of {@link MediaSession2} if it wants + * to keep media playback in the background. + *

      + * Here's the benefits of using {@link MediaSessionService2} instead of + * {@link MediaSession2}. + *

        + *
      • Another app can know that your app supports {@link MediaSession2} even when your app + * isn't running. + *
      • Another app can start playback of your app even when your app isn't running. + *
      + * For example, user's voice command can start playback of your app even when it's not running. + *

      + * To extend this class, adding followings directly to your {@code AndroidManifest.xml}. + *

      + * <service android:name="component_name_of_your_implementation" >
      + *   <intent-filter>
      + *     <action android:name="android.media.MediaSessionService2" />
      + *   </intent-filter>
      + * </service>
      + *

      + * A {@link MediaSessionService2} is another form of {@link MediaSession2}. IDs shouldn't + * be shared between the {@link MediaSessionService2} and {@link MediaSession2}. By + * default, an empty string will be used for ID of the service. If you want to specify an ID, + * declare metadata in the manifest as follows. + *

      + * <service android:name="component_name_of_your_implementation" >
      + *   <intent-filter>
      + *     <action android:name="android.media.MediaSessionService2" />
      + *   </intent-filter>
      + *   <meta-data android:name="android.media.session"
      + *       android:value="session_id"/>
      + * </service>
      + *

      + * It's recommended for an app to have a single {@link MediaSessionService2} declared in the + * manifest. Otherwise, your app might be shown twice in the list of the Auto/Wearable, or another + * app fails to pick the right session service when it wants to start the playback this app. + *

      + * If there's conflicts with the session ID among the services, services wouldn't be available for + * any controllers. + *

      + * Topic covered here: + *

        + *
      1. Service Lifecycle + *
      2. Permissions + *
      + *
      + * + *

      Service Lifecycle

      + *

      + * Session service is bounded service. When a {@link MediaController2} is created for the + * session service, the controller binds to the session service. {@link #onCreateSession(String)} + * may be called after the {@link #onCreate} if the service hasn't created yet. + *

      + * After the binding, session's {@link MediaSession2.SessionCallback#onConnect(ControllerInfo)} + * will be called to accept or reject connection request from a controller. If the connection is + * rejected, the controller will unbind. If it's accepted, the controller will be available to use + * and keep binding. + *

      + * When playback is started for this session service, {@link #onUpdateNotification(PlaybackState)} + * is called and service would become a foreground service. It's needed to keep playback after the + * controller is destroyed. The session service becomes background service when the playback is + * stopped. + * + *

      Permissions

      + *

      + * Any app can bind to the session service with controller, but the controller can be used only if + * the session service accepted the connection request through + * {@link MediaSession2.SessionCallback#onConnect(ControllerInfo)}. + * + * @hide + */ +// TODO(jaewan): Unhide +// TODO(jaewan): Can we clean up sessions in onDestroy() automatically instead? +// What about currently running SessionCallback when the onDestroy() is called? +// TODO(jaewan): Protect this with system|privilleged permission - Q. +// TODO(jaewan): Add permission check for the service to know incoming connection request. +// Follow-up questions: What about asking a XML for list of white/black packages for +// allowing enumeration? +// We can read the information even when the service is started, +// so SessionManager.getXXXXService() can only return apps +// TODO(jaewan): Will be the black/white listing persistent? +// In other words, can we cache the rejection? +public abstract class MediaSessionService2 extends Service { + private final MediaSessionService2Provider mProvider; + + /** + * This is the interface name that a service implementing a session service should say that it + * support -- that is, this is the action it uses for its intent filter. + */ + public static final String SERVICE_INTERFACE = "android.media.MediaSessionService2"; + + /** + * Name under which a MediaSessionService2 component publishes information about itself. + * This meta-data must provide a string value for the ID. + */ + public static final String SERVICE_META_DATA = "android.media.session"; + + public MediaSessionService2() { + super(); + mProvider = createProvider(); + } + + MediaSessionService2Provider createProvider() { + return ApiLoader.getProvider(this).createMediaSessionService2(this); + } + + /** + * Default implementation for {@link MediaSessionService2} to initialize session service. + *

      + * Override this method if you need your own initialization. Derived classes MUST call through + * to the super class's implementation of this method. + */ + @CallSuper + @Override + public void onCreate() { + super.onCreate(); + mProvider.onCreate_impl(); + } + + /** + * Called when another app requested to start this service to get {@link MediaSession2}. + *

      + * Session service will accept or reject the connection with the + * {@link MediaSession2.SessionCallback} in the created session. + *

      + * Service wouldn't run if {@code null} is returned or session's ID doesn't match with the + * expected ID that you've specified through the AndroidManifest.xml. + *

      + * This method will be called on the main thread. + * + * @param sessionId session id written in the AndroidManifest.xml. + * @return a new session + * @see MediaSession2.Builder + * @see #getSession() + */ + public @NonNull abstract MediaSession2 onCreateSession(String sessionId); + + /** + * Called when the playback state of this session is changed, and notification needs update. + * Override this method to show your own notification UI. + *

      + * With the notification returned here, the service become foreground service when the playback + * is started. It becomes background service after the playback is stopped. + * + * @param state playback state + * @return a {@link MediaNotification}. If it's {@code null}, notification wouldn't be shown. + */ + // TODO(jaewan): Also add metadata + public MediaNotification onUpdateNotification(PlaybackState2 state) { + return mProvider.onUpdateNotification_impl(state); + } + + /** + * Get instance of the {@link MediaSession2} that you've previously created with the + * {@link #onCreateSession} for this service. + * + * @return created session + */ + public final MediaSession2 getSession() { + return mProvider.getSession_impl(); + } + + /** + * Default implementation for {@link MediaSessionService2} to handle incoming binding + * request. If the request is for getting the session, the intent will have action + * {@link #SERVICE_INTERFACE}. + *

      + * Override this method if this service also needs to handle binder requests other than + * {@link #SERVICE_INTERFACE}. Derived classes MUST call through to the super class's + * implementation of this method. + * + * @param intent + * @return Binder + */ + @CallSuper + @Nullable + @Override + public IBinder onBind(Intent intent) { + return mProvider.onBind_impl(intent); + } + + /** + * Returned by {@link #onUpdateNotification(PlaybackState)} for making session service + * foreground service to keep playback running in the background. It's highly recommended to + * show media style notification here. + */ + // TODO(jaewan): Should we also move this to updatable? + public static class MediaNotification { + public final int id; + public final Notification notification; + + private MediaNotification(int id, @NonNull Notification notification) { + this.id = id; + this.notification = notification; + } + + /** + * Create a {@link MediaNotification}. + * + * @param notificationId notification id to be used for + * {@link android.app.NotificationManager#notify(int, Notification)}. + * @param notification a notification to make session service foreground service. Media + * style notification is recommended here. + * @return + */ + public static MediaNotification create(int notificationId, + @NonNull Notification notification) { + if (notification == null) { + throw new IllegalArgumentException("Notification cannot be null"); + } + return new MediaNotification(notificationId, notification); + } + } +} diff --git a/android/media/MockMediaLibraryService2.java b/android/media/MockMediaLibraryService2.java new file mode 100644 index 00000000..14cf2577 --- /dev/null +++ b/android/media/MockMediaLibraryService2.java @@ -0,0 +1,98 @@ +/* +* Copyright 2018 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 static junit.framework.Assert.fail; + +import android.content.Context; +import android.media.MediaSession2.CommandGroup; +import android.media.MediaSession2.ControllerInfo; +import android.media.TestUtils.SyncHandler; +import android.os.Bundle; +import android.os.Process; + +import javax.annotation.concurrent.GuardedBy; + +/** + * Mock implementation of {@link MediaLibraryService2} for testing. + */ +public class MockMediaLibraryService2 extends MediaLibraryService2 { + // Keep in sync with the AndroidManifest.xml + public static final String ID = "TestLibrary"; + + public static final String ROOT_ID = "rootId"; + public static final Bundle EXTRA = new Bundle(); + static { + EXTRA.putString(ROOT_ID, ROOT_ID); + } + @GuardedBy("MockMediaLibraryService2.class") + private static SessionToken2 sToken; + + private MediaLibrarySession mSession; + + @Override + public MediaLibrarySession onCreateSession(String sessionId) { + final MockPlayer player = new MockPlayer(1); + final SyncHandler handler = (SyncHandler) TestServiceRegistry.getInstance().getHandler(); + try { + handler.postAndSync(() -> { + TestLibrarySessionCallback callback = new TestLibrarySessionCallback(); + mSession = new MediaLibrarySessionBuilder(MockMediaLibraryService2.this, + player, (runnable) -> handler.post(runnable), callback) + .setId(sessionId).build(); + }); + } catch (InterruptedException e) { + fail(e.toString()); + } + return mSession; + } + + @Override + public void onDestroy() { + TestServiceRegistry.getInstance().cleanUp(); + super.onDestroy(); + } + + public static SessionToken2 getToken(Context context) { + synchronized (MockMediaLibraryService2.class) { + if (sToken == null) { + sToken = new SessionToken2(SessionToken2.TYPE_LIBRARY_SERVICE, + context.getPackageName(), ID, + MockMediaLibraryService2.class.getName(), null); + } + return sToken; + } + } + + private class TestLibrarySessionCallback extends MediaLibrarySessionCallback { + @Override + public CommandGroup onConnect(ControllerInfo controller) { + if (Process.myUid() != controller.getUid()) { + // It's system app wants to listen changes. Ignore. + return super.onConnect(controller); + } + TestServiceRegistry.getInstance().setServiceInstance( + MockMediaLibraryService2.this, controller); + return super.onConnect(controller); + } + + @Override + public BrowserRoot onGetRoot(ControllerInfo controller, Bundle rootHints) { + return new BrowserRoot(ROOT_ID, EXTRA); + } + } +} \ No newline at end of file diff --git a/android/media/MockMediaSessionService2.java b/android/media/MockMediaSessionService2.java new file mode 100644 index 00000000..b0581170 --- /dev/null +++ b/android/media/MockMediaSessionService2.java @@ -0,0 +1,102 @@ +/* + * Copyright 2018 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 static junit.framework.Assert.fail; + +import android.app.Notification; +import android.app.NotificationChannel; +import android.app.NotificationManager; +import android.content.Context; +import android.media.MediaSession2.ControllerInfo; +import android.media.MediaSession2.SessionCallback; +import android.media.TestUtils.SyncHandler; +import android.media.session.PlaybackState; +import android.os.Process; + +/** + * Mock implementation of {@link android.media.MediaSessionService2} for testing. + */ +public class MockMediaSessionService2 extends MediaSessionService2 { + // Keep in sync with the AndroidManifest.xml + public static final String ID = "TestSession"; + + private static final String DEFAULT_MEDIA_NOTIFICATION_CHANNEL_ID = "media_session_service"; + private static final int DEFAULT_MEDIA_NOTIFICATION_ID = 1001; + + private NotificationChannel mDefaultNotificationChannel; + private MediaSession2 mSession; + private NotificationManager mNotificationManager; + + @Override + public MediaSession2 onCreateSession(String sessionId) { + final MockPlayer player = new MockPlayer(1); + final SyncHandler handler = (SyncHandler) TestServiceRegistry.getInstance().getHandler(); + try { + handler.postAndSync(() -> { + mSession = new MediaSession2.Builder(MockMediaSessionService2.this, player) + .setId(sessionId).setSessionCallback((runnable)->handler.post(runnable), + new MySessionCallback()).build(); + }); + } catch (InterruptedException e) { + fail(e.toString()); + } + return mSession; + } + + @Override + public void onCreate() { + super.onCreate(); + mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + } + + @Override + public void onDestroy() { + TestServiceRegistry.getInstance().cleanUp(); + super.onDestroy(); + } + + @Override + public MediaNotification onUpdateNotification(PlaybackState2 state) { + if (mDefaultNotificationChannel == null) { + mDefaultNotificationChannel = new NotificationChannel( + DEFAULT_MEDIA_NOTIFICATION_CHANNEL_ID, + DEFAULT_MEDIA_NOTIFICATION_CHANNEL_ID, + NotificationManager.IMPORTANCE_DEFAULT); + mNotificationManager.createNotificationChannel(mDefaultNotificationChannel); + } + Notification notification = new Notification.Builder( + this, DEFAULT_MEDIA_NOTIFICATION_CHANNEL_ID) + .setContentTitle(getPackageName()) + .setContentText("Playback state: " + state.getState()) + .setSmallIcon(android.R.drawable.sym_def_app_icon).build(); + return MediaNotification.create(DEFAULT_MEDIA_NOTIFICATION_ID, notification); + } + + private class MySessionCallback extends SessionCallback { + @Override + public MediaSession2.CommandGroup onConnect(ControllerInfo controller) { + if (Process.myUid() != controller.getUid()) { + // It's system app wants to listen changes. Ignore. + return super.onConnect(controller); + } + TestServiceRegistry.getInstance().setServiceInstance( + MockMediaSessionService2.this, controller); + return super.onConnect(controller); + } + } +} diff --git a/android/media/MockPlayer.java b/android/media/MockPlayer.java new file mode 100644 index 00000000..fd693092 --- /dev/null +++ b/android/media/MockPlayer.java @@ -0,0 +1,146 @@ +/* + * Copyright 2018 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.media.MediaSession2.PlaylistParam; +import android.media.session.PlaybackState; +import android.os.Handler; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executor; + +/** + * A mock implementation of {@link MediaPlayerBase} for testing. + */ +public class MockPlayer extends MediaPlayerBase { + public final CountDownLatch mCountDownLatch; + + public boolean mPlayCalled; + public boolean mPauseCalled; + public boolean mStopCalled; + public boolean mSkipToPreviousCalled; + public boolean mSkipToNextCalled; + public List mListeners = new ArrayList<>(); + private PlaybackState2 mLastPlaybackState; + + public MockPlayer(int count) { + mCountDownLatch = (count > 0) ? new CountDownLatch(count) : null; + } + + @Override + public void play() { + mPlayCalled = true; + if (mCountDownLatch != null) { + mCountDownLatch.countDown(); + } + } + + @Override + public void pause() { + mPauseCalled = true; + if (mCountDownLatch != null) { + mCountDownLatch.countDown(); + } + } + + @Override + public void stop() { + mStopCalled = true; + if (mCountDownLatch != null) { + mCountDownLatch.countDown(); + } + } + + @Override + public void skipToPrevious() { + mSkipToPreviousCalled = true; + if (mCountDownLatch != null) { + mCountDownLatch.countDown(); + } + } + + @Override + public void skipToNext() { + mSkipToNextCalled = true; + if (mCountDownLatch != null) { + mCountDownLatch.countDown(); + } + } + + + + @Nullable + @Override + public PlaybackState2 getPlaybackState() { + return mLastPlaybackState; + } + + @Override + public void addPlaybackListener(@NonNull Executor executor, + @NonNull PlaybackListener listener) { + mListeners.add(new PlaybackListenerHolder(executor, listener)); + } + + @Override + public void removePlaybackListener(@NonNull PlaybackListener listener) { + int index = PlaybackListenerHolder.indexOf(mListeners, listener); + if (index >= 0) { + mListeners.remove(index); + } + } + + public void notifyPlaybackState(final PlaybackState2 state) { + mLastPlaybackState = state; + for (int i = 0; i < mListeners.size(); i++) { + mListeners.get(i).postPlaybackChange(state); + } + } + + // No-op. Should be added for test later. + @Override + public void prepare() { + } + + @Override + public void seekTo(long pos) { + } + + @Override + public void fastFoward() { + } + + @Override + public void rewind() { + } + + @Override + public AudioAttributes getAudioAttributes() { + return null; + } + + @Override + public void setPlaylist(List item, PlaylistParam param) { + } + + @Override + public void setCurrentPlaylistItem(int index) { + } +} diff --git a/android/media/PlaybackListenerHolder.java b/android/media/PlaybackListenerHolder.java new file mode 100644 index 00000000..4e19d4de --- /dev/null +++ b/android/media/PlaybackListenerHolder.java @@ -0,0 +1,73 @@ +/* + * Copyright 2018 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.media.MediaPlayerBase.PlaybackListener; +import android.media.session.PlaybackState; +import android.os.Handler; +import android.os.Message; +import android.support.annotation.NonNull; + +import java.util.List; +import java.util.concurrent.Executor; + +/** + * Holds {@link PlaybackListener} with the {@link Handler}. + */ +public class PlaybackListenerHolder { + public final Executor executor; + public final PlaybackListener listener; + + public PlaybackListenerHolder(Executor executor, @NonNull PlaybackListener listener) { + this.executor = executor; + this.listener = listener; + } + + public void postPlaybackChange(final PlaybackState2 state) { + executor.execute(() -> listener.onPlaybackChanged(state)); + } + + /** + * Returns {@code true} if the given list contains a {@link PlaybackListenerHolder} that holds + * the given listener. + * + * @param list list to check + * @param listener listener to check + * @return {@code true} if the given list contains listener. {@code false} otherwise. + */ + public static boolean contains( + @NonNull List list, PlaybackListener listener) { + return indexOf(list, listener) >= 0; + } + + /** + * Returns the index of the {@link PlaybackListenerHolder} that contains the given listener. + * + * @param list list to check + * @param listener listener to check + * @return {@code index} of item if the given list contains listener. {@code -1} otherwise. + */ + public static int indexOf( + @NonNull List list, PlaybackListener listener) { + for (int i = 0; i < list.size(); i++) { + if (list.get(i).listener == listener) { + return i; + } + } + return -1; + } +} diff --git a/android/media/PlaybackState2.java b/android/media/PlaybackState2.java new file mode 100644 index 00000000..46d6f45a --- /dev/null +++ b/android/media/PlaybackState2.java @@ -0,0 +1,216 @@ +/* + * Copyright 2018 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.os.Bundle; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * Playback state for a {@link MediaPlayerBase}, to be shared between {@link MediaSession2} and + * {@link MediaController2}. This includes a playback state {@link #STATE_PLAYING}, + * the current playback position and extra. + * @hide + */ +// TODO(jaewan): Move to updatable +public final class PlaybackState2 { + private static final String TAG = "PlaybackState2"; + + private static final String KEY_STATE = "android.media.playbackstate2.state"; + + // TODO(jaewan): Replace states from MediaPlayer2 + /** + * @hide + */ + @IntDef({STATE_NONE, STATE_STOPPED, STATE_PREPARED, STATE_PAUSED, STATE_PLAYING, + STATE_FINISH, STATE_BUFFERING, STATE_ERROR}) + @Retention(RetentionPolicy.SOURCE) + public @interface State {} + + /** + * This is the default playback state and indicates that no media has been + * added yet, or the performer has been reset and has no content to play. + */ + public final static int STATE_NONE = 0; + + /** + * State indicating this item is currently stopped. + */ + public final static int STATE_STOPPED = 1; + + /** + * State indicating this item is currently prepared + */ + public final static int STATE_PREPARED = 2; + + /** + * State indicating this item is currently paused. + */ + public final static int STATE_PAUSED = 3; + + /** + * State indicating this item is currently playing. + */ + public final static int STATE_PLAYING = 4; + + /** + * State indicating the playback reaches the end of the item. + */ + public final static int STATE_FINISH = 5; + + /** + * State indicating this item is currently buffering and will begin playing + * when enough data has buffered. + */ + public final static int STATE_BUFFERING = 6; + + /** + * State indicating this item is currently in an error state. The error + * message should also be set when entering this state. + */ + public final static int STATE_ERROR = 7; + + /** + * Use this value for the position to indicate the position is not known. + */ + public final static long PLAYBACK_POSITION_UNKNOWN = -1; + + private final int mState; + private final long mPosition; + private final long mBufferedPosition; + private final float mSpeed; + private final CharSequence mErrorMessage; + private final long mUpdateTime; + private final long mActiveItemId; + + public PlaybackState2(int state, long position, long updateTime, float speed, + long bufferedPosition, long activeItemId, CharSequence error) { + mState = state; + mPosition = position; + mSpeed = speed; + mUpdateTime = updateTime; + mBufferedPosition = bufferedPosition; + mActiveItemId = activeItemId; + mErrorMessage = error; + } + + @Override + public String toString() { + StringBuilder bob = new StringBuilder("PlaybackState {"); + bob.append("state=").append(mState); + bob.append(", position=").append(mPosition); + bob.append(", buffered position=").append(mBufferedPosition); + bob.append(", speed=").append(mSpeed); + bob.append(", updated=").append(mUpdateTime); + bob.append(", active item id=").append(mActiveItemId); + bob.append(", error=").append(mErrorMessage); + bob.append("}"); + return bob.toString(); + } + + /** + * Get the current state of playback. One of the following: + *

        + *
      • {@link PlaybackState2#STATE_NONE}
      • + *
      • {@link PlaybackState2#STATE_STOPPED}
      • + *
      • {@link PlaybackState2#STATE_PLAYING}
      • + *
      • {@link PlaybackState2#STATE_PAUSED}
      • + *
      • {@link PlaybackState2#STATE_BUFFERING}
      • + *
      • {@link PlaybackState2#STATE_ERROR}
      • + *
      + */ + @State + public int getState() { + return mState; + } + + /** + * Get the current playback position in ms. + */ + public long getPosition() { + return mPosition; + } + + /** + * Get the current buffered position in ms. This is the farthest playback + * point that can be reached from the current position using only buffered + * content. + */ + public long getBufferedPosition() { + return mBufferedPosition; + } + + /** + * Get the current playback speed as a multiple of normal playback. This + * should be negative when rewinding. A value of 1 means normal playback and + * 0 means paused. + * + * @return The current speed of playback. + */ + public float getPlaybackSpeed() { + return mSpeed; + } + + /** + * Get a user readable error message. This should be set when the state is + * {@link PlaybackState2#STATE_ERROR}. + */ + public CharSequence getErrorMessage() { + return mErrorMessage; + } + + /** + * Get the elapsed real time at which position was last updated. If the + * position has never been set this will return 0; + * + * @return The last time the position was updated. + */ + public long getLastPositionUpdateTime() { + return mUpdateTime; + } + + /** + * Get the id of the currently active item in the playlist. + * + * @return The id of the currently active item in the queue + */ + public long getCurrentPlaylistItemIndex() { + return mActiveItemId; + } + + /** + * @return Bundle object for this to share between processes. + */ + public Bundle toBundle() { + // TODO(jaewan): Include other variables. + Bundle bundle = new Bundle(); + bundle.putInt(KEY_STATE, mState); + return bundle; + } + + /** + * @param bundle input + * @return + */ + public static PlaybackState2 fromBundle(Bundle bundle) { + // TODO(jaewan): Include other variables. + final int state = bundle.getInt(KEY_STATE); + return new PlaybackState2(state, 0, 0, 0, 0, 0, null); + } +} \ No newline at end of file diff --git a/android/media/Rating2.java b/android/media/Rating2.java new file mode 100644 index 00000000..67e5e728 --- /dev/null +++ b/android/media/Rating2.java @@ -0,0 +1,304 @@ +/* + * Copyright 2018 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.os.Bundle; +import android.util.Log; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * A class to encapsulate rating information used as content metadata. + * A rating is defined by its rating style (see {@link #RATING_HEART}, + * {@link #RATING_THUMB_UP_DOWN}, {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, + * {@link #RATING_5_STARS} or {@link #RATING_PERCENTAGE}) and the actual rating value (which may + * be defined as "unrated"), both of which are defined when the rating instance is constructed + * through one of the factory methods. + * @hide + */ +// TODO(jaewan): Move this to updatable +public final class Rating2 { + private static final String TAG = "Rating2"; + + private static final String KEY_STYLE = "android.media.rating2.style"; + private static final String KEY_VALUE = "android.media.rating2.value"; + + /** + * @hide + */ + @IntDef({RATING_NONE, RATING_HEART, RATING_THUMB_UP_DOWN, RATING_3_STARS, RATING_4_STARS, + RATING_5_STARS, RATING_PERCENTAGE}) + @Retention(RetentionPolicy.SOURCE) + public @interface Style {} + + /** + * @hide + */ + @IntDef({RATING_3_STARS, RATING_4_STARS, RATING_5_STARS}) + @Retention(RetentionPolicy.SOURCE) + public @interface StarStyle {} + + /** + * Indicates a rating style is not supported. A Rating2 will never have this + * type, but can be used by other classes to indicate they do not support + * Rating2. + */ + public final static int RATING_NONE = 0; + + /** + * A rating style with a single degree of rating, "heart" vs "no heart". Can be used to + * indicate the content referred to is a favorite (or not). + */ + public final static int RATING_HEART = 1; + + /** + * A rating style for "thumb up" vs "thumb down". + */ + public final static int RATING_THUMB_UP_DOWN = 2; + + /** + * A rating style with 0 to 3 stars. + */ + public final static int RATING_3_STARS = 3; + + /** + * A rating style with 0 to 4 stars. + */ + public final static int RATING_4_STARS = 4; + + /** + * A rating style with 0 to 5 stars. + */ + public final static int RATING_5_STARS = 5; + + /** + * A rating style expressed as a percentage. + */ + public final static int RATING_PERCENTAGE = 6; + + private final static float RATING_NOT_RATED = -1.0f; + + private final int mRatingStyle; + + private final float mRatingValue; + + private Rating2(@Style int ratingStyle, float rating) { + mRatingStyle = ratingStyle; + mRatingValue = rating; + } + + @Override + public String toString() { + return "Rating2:style=" + mRatingStyle + " rating=" + + (mRatingValue < 0.0f ? "unrated" : String.valueOf(mRatingValue)); + } + + /** + * Create an instance from bundle object, previoulsy created by {@link #toBundle()} + * + * @param bundle bundle + * @return new Rating2 instance + */ + public static Rating2 fromBundle(Bundle bundle) { + return new Rating2(bundle.getInt(KEY_STYLE), bundle.getFloat(KEY_VALUE)); + } + + /** + * Return bundle for this object to share across the process. + * @return bundle of this object + */ + public Bundle toBundle() { + Bundle bundle = new Bundle(); + bundle.putInt(KEY_STYLE, mRatingStyle); + bundle.putFloat(KEY_VALUE, mRatingValue); + return bundle; + } + + /** + * Return a Rating2 instance with no rating. + * Create and return a new Rating2 instance with no rating known for the given + * rating style. + * @param ratingStyle one of {@link #RATING_HEART}, {@link #RATING_THUMB_UP_DOWN}, + * {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, {@link #RATING_5_STARS}, + * or {@link #RATING_PERCENTAGE}. + * @return null if an invalid rating style is passed, a new Rating2 instance otherwise. + */ + public static Rating2 newUnratedRating(@Style int ratingStyle) { + switch(ratingStyle) { + case RATING_HEART: + case RATING_THUMB_UP_DOWN: + case RATING_3_STARS: + case RATING_4_STARS: + case RATING_5_STARS: + case RATING_PERCENTAGE: + return new Rating2(ratingStyle, RATING_NOT_RATED); + default: + return null; + } + } + + /** + * Return a Rating2 instance with a heart-based rating. + * Create and return a new Rating2 instance with a rating style of {@link #RATING_HEART}, + * and a heart-based rating. + * @param hasHeart true for a "heart selected" rating, false for "heart unselected". + * @return a new Rating2 instance. + */ + public static Rating2 newHeartRating(boolean hasHeart) { + return new Rating2(RATING_HEART, hasHeart ? 1.0f : 0.0f); + } + + /** + * Return a Rating2 instance with a thumb-based rating. + * Create and return a new Rating2 instance with a {@link #RATING_THUMB_UP_DOWN} + * rating style, and a "thumb up" or "thumb down" rating. + * @param thumbIsUp true for a "thumb up" rating, false for "thumb down". + * @return a new Rating2 instance. + */ + public static Rating2 newThumbRating(boolean thumbIsUp) { + return new Rating2(RATING_THUMB_UP_DOWN, thumbIsUp ? 1.0f : 0.0f); + } + + /** + * Return a Rating2 instance with a star-based rating. + * Create and return a new Rating2 instance with one of the star-base rating styles + * and the given integer or fractional number of stars. Non integer values can for instance + * be used to represent an average rating value, which might not be an integer number of stars. + * @param starRatingStyle one of {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, + * {@link #RATING_5_STARS}. + * @param starRating a number ranging from 0.0f to 3.0f, 4.0f or 5.0f according to + * the rating style. + * @return null if the rating style is invalid, or the rating is out of range, + * a new Rating2 instance otherwise. + */ + public static Rating2 newStarRating(@StarStyle int starRatingStyle, float starRating) { + float maxRating = -1.0f; + switch(starRatingStyle) { + case RATING_3_STARS: + maxRating = 3.0f; + break; + case RATING_4_STARS: + maxRating = 4.0f; + break; + case RATING_5_STARS: + maxRating = 5.0f; + break; + default: + Log.e(TAG, "Invalid rating style (" + starRatingStyle + ") for a star rating"); + return null; + } + if ((starRating < 0.0f) || (starRating > maxRating)) { + Log.e(TAG, "Trying to set out of range star-based rating"); + return null; + } + return new Rating2(starRatingStyle, starRating); + } + + /** + * Return a Rating2 instance with a percentage-based rating. + * Create and return a new Rating2 instance with a {@link #RATING_PERCENTAGE} + * rating style, and a rating of the given percentage. + * @param percent the value of the rating + * @return null if the rating is out of range, a new Rating2 instance otherwise. + */ + public static Rating2 newPercentageRating(float percent) { + if ((percent < 0.0f) || (percent > 100.0f)) { + Log.e(TAG, "Invalid percentage-based rating value"); + return null; + } else { + return new Rating2(RATING_PERCENTAGE, percent); + } + } + + /** + * Return whether there is a rating value available. + * @return true if the instance was not created with {@link #newUnratedRating(int)}. + */ + public boolean isRated() { + return mRatingValue >= 0.0f; + } + + /** + * Return the rating style. + * @return one of {@link #RATING_HEART}, {@link #RATING_THUMB_UP_DOWN}, + * {@link #RATING_3_STARS}, {@link #RATING_4_STARS}, {@link #RATING_5_STARS}, + * or {@link #RATING_PERCENTAGE}. + */ + @Style + public int getRatingStyle() { + return mRatingStyle; + } + + /** + * Return whether the rating is "heart selected". + * @return true if the rating is "heart selected", false if the rating is "heart unselected", + * if the rating style is not {@link #RATING_HEART} or if it is unrated. + */ + public boolean hasHeart() { + if (mRatingStyle != RATING_HEART) { + return false; + } else { + return (mRatingValue == 1.0f); + } + } + + /** + * Return whether the rating is "thumb up". + * @return true if the rating is "thumb up", false if the rating is "thumb down", + * if the rating style is not {@link #RATING_THUMB_UP_DOWN} or if it is unrated. + */ + public boolean isThumbUp() { + if (mRatingStyle != RATING_THUMB_UP_DOWN) { + return false; + } else { + return (mRatingValue == 1.0f); + } + } + + /** + * Return the star-based rating value. + * @return a rating value greater or equal to 0.0f, or a negative value if the rating style is + * not star-based, or if it is unrated. + */ + public float getStarRating() { + switch (mRatingStyle) { + case RATING_3_STARS: + case RATING_4_STARS: + case RATING_5_STARS: + if (isRated()) { + return mRatingValue; + } + default: + return -1.0f; + } + } + + /** + * Return the percentage-based rating value. + * @return a rating value greater or equal to 0.0f, or a negative value if the rating style is + * not percentage-based, or if it is unrated. + */ + public float getPercentRating() { + if ((mRatingStyle != RATING_PERCENTAGE) || !isRated()) { + return -1.0f; + } else { + return mRatingValue; + } + } +} diff --git a/android/media/SessionToken2.java b/android/media/SessionToken2.java new file mode 100644 index 00000000..697a5a87 --- /dev/null +++ b/android/media/SessionToken2.java @@ -0,0 +1,225 @@ +/* + * Copyright 2018 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.Nullable; +import android.media.session.MediaSessionManager; +import android.os.Bundle; +import android.os.IBinder; +import android.text.TextUtils; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * Represents an ongoing {@link MediaSession2} or a {@link MediaSessionService2}. + * If it's representing a session service, it may not be ongoing. + *

      + * This may be passed to apps by the session owner to allow them to create a + * {@link MediaController2} to communicate with the session. + *

      + * It can be also obtained by {@link MediaSessionManager}. + * @hide + */ +// TODO(jaewan): Unhide. SessionToken2? +// TODO(jaewan): Move Token to updatable! +// TODO(jaewan): Find better name for this (SessionToken or Session2Token) +public final class SessionToken2 { + @Retention(RetentionPolicy.SOURCE) + @IntDef(value = {TYPE_SESSION, TYPE_SESSION_SERVICE, TYPE_LIBRARY_SERVICE}) + public @interface TokenType { + } + + public static final int TYPE_SESSION = 0; + public static final int TYPE_SESSION_SERVICE = 1; + public static final int TYPE_LIBRARY_SERVICE = 2; + + private static final String KEY_TYPE = "android.media.token.type"; + private static final String KEY_PACKAGE_NAME = "android.media.token.package_name"; + private static final String KEY_SERVICE_NAME = "android.media.token.service_name"; + private static final String KEY_ID = "android.media.token.id"; + private static final String KEY_SESSION_BINDER = "android.media.token.session_binder"; + + private final @TokenType int mType; + private final String mPackageName; + private final String mServiceName; + private final String mId; + private final IMediaSession2 mSessionBinder; + + /** + * Constructor for the token. + * + * @hide + * @param type type + * @param packageName package name + * @param id id + * @param serviceName name of service. Can be {@code null} if it's not an service. + * @param sessionBinder binder for this session. Can be {@code null} if it's service. + * @hide + */ + // TODO(jaewan): UID is also needed. + // TODO(jaewan): Unhide + public SessionToken2(@TokenType int type, @NonNull String packageName, @NonNull String id, + @Nullable String serviceName, @Nullable IMediaSession2 sessionBinder) { + // TODO(jaewan): Add sanity check. + mType = type; + mPackageName = packageName; + mId = id; + mServiceName = serviceName; + mSessionBinder = sessionBinder; + } + + public int hashCode() { + final int prime = 31; + return mType + + prime * (mPackageName.hashCode() + + prime * (mId.hashCode() + + prime * ((mServiceName != null ? mServiceName.hashCode() : 0) + + prime * (mSessionBinder != null ? mSessionBinder.asBinder().hashCode() : 0)))); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + SessionToken2 other = (SessionToken2) obj; + if (!mPackageName.equals(other.getPackageName()) + || !mServiceName.equals(other.getServiceName()) + || !mId.equals(other.getId()) + || mType != other.getType()) { + return false; + } + if (mSessionBinder == other.getSessionBinder()) { + return true; + } else if (mSessionBinder == null || other.getSessionBinder() == null) { + return false; + } + return mSessionBinder.asBinder().equals(other.getSessionBinder().asBinder()); + } + + @Override + public String toString() { + return "SessionToken {pkg=" + mPackageName + " id=" + mId + " type=" + mType + + " service=" + mServiceName + " binder=" + mSessionBinder + "}"; + } + + /** + * @return package name + */ + public String getPackageName() { + return mPackageName; + } + + /** + * @return id + */ + public String getId() { + return mId; + } + + /** + * @return type of the token + * @see #TYPE_SESSION + * @see #TYPE_SESSION_SERVICE + */ + public @TokenType int getType() { + return mType; + } + + /** + * @return session binder. + * @hide + */ + public @Nullable IMediaSession2 getSessionBinder() { + return mSessionBinder; + } + + /** + * @return service name if it's session service. + * @hide + */ + public @Nullable String getServiceName() { + return mServiceName; + } + + /** + * Create a token from the bundle, exported by {@link #toBundle()}. + * + * @param bundle + * @return + */ + public static SessionToken2 fromBundle(@NonNull Bundle bundle) { + if (bundle == null) { + return null; + } + final @TokenType int type = bundle.getInt(KEY_TYPE, -1); + final String packageName = bundle.getString(KEY_PACKAGE_NAME); + final String serviceName = bundle.getString(KEY_SERVICE_NAME); + final String id = bundle.getString(KEY_ID); + final IBinder sessionBinder = bundle.getBinder(KEY_SESSION_BINDER); + + // Sanity check. + switch (type) { + case TYPE_SESSION: + if (!(sessionBinder instanceof IMediaSession2)) { + throw new IllegalArgumentException("Session needs sessionBinder"); + } + break; + case TYPE_SESSION_SERVICE: + if (TextUtils.isEmpty(serviceName)) { + throw new IllegalArgumentException("Session service needs service name"); + } + if (sessionBinder != null && !(sessionBinder instanceof IMediaSession2)) { + throw new IllegalArgumentException("Invalid session binder"); + } + break; + default: + throw new IllegalArgumentException("Invalid type"); + } + if (TextUtils.isEmpty(packageName) || id == null) { + throw new IllegalArgumentException("Package name nor ID cannot be null."); + } + // TODO(jaewan): Revisit here when we add connection callback to the session for individual + // controller's permission check. With it, sessionBinder should be available + // if and only if for session, not session service. + return new SessionToken2(type, packageName, id, serviceName, + sessionBinder != null ? IMediaSession2.Stub.asInterface(sessionBinder) : null); + } + + /** + * Create a {@link Bundle} from this token to share it across processes. + * + * @return Bundle + * @hide + */ + public Bundle toBundle() { + Bundle bundle = new Bundle(); + bundle.putString(KEY_PACKAGE_NAME, mPackageName); + bundle.putString(KEY_SERVICE_NAME, mServiceName); + bundle.putString(KEY_ID, mId); + bundle.putInt(KEY_TYPE, mType); + bundle.putBinder(KEY_SESSION_BINDER, + mSessionBinder != null ? mSessionBinder.asBinder() : null); + return bundle; + } +} diff --git a/android/media/TestServiceRegistry.java b/android/media/TestServiceRegistry.java new file mode 100644 index 00000000..6f5512ef --- /dev/null +++ b/android/media/TestServiceRegistry.java @@ -0,0 +1,135 @@ +/* + * Copyright 2018 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 static org.junit.Assert.fail; + +import android.media.MediaSession2.ControllerInfo; +import android.media.TestUtils.SyncHandler; +import android.os.Handler; +import android.os.Looper; +import android.support.annotation.GuardedBy; + +/** + * Keeps the instance of currently running {@link MockMediaSessionService2}. And also provides + * a way to control them in one place. + *

      + * It only support only one service at a time. + */ +public class TestServiceRegistry { + public interface ServiceInstanceChangedCallback { + void OnServiceInstanceChanged(MediaSessionService2 service); + } + + @GuardedBy("TestServiceRegistry.class") + private static TestServiceRegistry sInstance; + @GuardedBy("TestServiceRegistry.class") + private MediaSessionService2 mService; + @GuardedBy("TestServiceRegistry.class") + private SyncHandler mHandler; + @GuardedBy("TestServiceRegistry.class") + private ControllerInfo mOnConnectControllerInfo; + @GuardedBy("TestServiceRegistry.class") + private ServiceInstanceChangedCallback mCallback; + + public static TestServiceRegistry getInstance() { + synchronized (TestServiceRegistry.class) { + if (sInstance == null) { + sInstance = new TestServiceRegistry(); + } + return sInstance; + } + } + + public void setHandler(Handler handler) { + synchronized (TestServiceRegistry.class) { + mHandler = new SyncHandler(handler.getLooper()); + } + } + + public void setServiceInstanceChangedCallback(ServiceInstanceChangedCallback callback) { + synchronized (TestServiceRegistry.class) { + mCallback = callback; + } + } + + public Handler getHandler() { + synchronized (TestServiceRegistry.class) { + return mHandler; + } + } + + public void setServiceInstance(MediaSessionService2 service, ControllerInfo controller) { + synchronized (TestServiceRegistry.class) { + if (mService != null) { + fail("Previous service instance is still running. Clean up manually to ensure" + + " previoulsy running service doesn't break current test"); + } + mService = service; + mOnConnectControllerInfo = controller; + if (mCallback != null) { + mCallback.OnServiceInstanceChanged(service); + } + } + } + + public MediaSessionService2 getServiceInstance() { + synchronized (TestServiceRegistry.class) { + return mService; + } + } + + public ControllerInfo getOnConnectControllerInfo() { + synchronized (TestServiceRegistry.class) { + return mOnConnectControllerInfo; + } + } + + + public void cleanUp() { + synchronized (TestServiceRegistry.class) { + final ServiceInstanceChangedCallback callback = mCallback; + if (mService != null) { + try { + if (mHandler.getLooper() == Looper.myLooper()) { + mService.getSession().close(); + } else { + mHandler.postAndSync(() -> { + mService.getSession().close(); + }); + } + } catch (InterruptedException e) { + // No-op. Service containing session will die, but shouldn't be a huge issue. + } + // stopSelf() would not kill service while the binder connection established by + // bindService() exists, and close() above will do the job instead. + // So stopSelf() isn't really needed, but just for sure. + mService.stopSelf(); + mService = null; + } + if (mHandler != null) { + mHandler.removeCallbacksAndMessages(null); + } + mCallback = null; + mOnConnectControllerInfo = null; + + if (callback != null) { + callback.OnServiceInstanceChanged(null); + } + } + } +} diff --git a/android/media/TestUtils.java b/android/media/TestUtils.java new file mode 100644 index 00000000..9a1fa100 --- /dev/null +++ b/android/media/TestUtils.java @@ -0,0 +1,124 @@ +/* + * Copyright 2018 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.content.Context; +import android.media.session.MediaSessionManager; +import android.media.session.PlaybackState; +import android.os.Bundle; +import android.os.Handler; + +import android.os.Looper; + +import java.util.List; +import java.util.Objects; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** + * Utilities for tests. + */ +public final class TestUtils { + private static final int WAIT_TIME_MS = 1000; + private static final int WAIT_SERVICE_TIME_MS = 5000; + + /** + * Creates a {@link android.media.session.PlaybackState} with the given state. + * + * @param state one of the PlaybackState.STATE_xxx. + * @return a PlaybackState + */ + public static PlaybackState2 createPlaybackState(int state) { + return new PlaybackState2(state, 0, 0, 1.0f, + 0, 0, null); + } + + /** + * Finds the session with id in this test package. + * + * @param context + * @param id + * @return + */ + // TODO(jaewan): Currently not working. + public static SessionToken2 getServiceToken(Context context, String id) { + MediaSessionManager manager = + (MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE); + List tokens = manager.getSessionServiceTokens(); + for (int i = 0; i < tokens.size(); i++) { + SessionToken2 token = tokens.get(i); + if (context.getPackageName().equals(token.getPackageName()) + && id.equals(token.getId())) { + return token; + } + } + fail("Failed to find service"); + return null; + } + + /** + * Compares contents of two bundles. + * + * @param a a bundle + * @param b another bundle + * @return {@code true} if two bundles are the same. {@code false} otherwise. This may be + * incorrect if any bundle contains a bundle. + */ + public static boolean equals(Bundle a, Bundle b) { + if (a == b) { + return true; + } + if (a == null || b == null) { + return false; + } + if (!a.keySet().containsAll(b.keySet()) + || !b.keySet().containsAll(a.keySet())) { + return false; + } + for (String key : a.keySet()) { + if (!Objects.equals(a.get(key), b.get(key))) { + return false; + } + } + return true; + } + + /** + * Handler that always waits until the Runnable finishes. + */ + public static class SyncHandler extends Handler { + public SyncHandler(Looper looper) { + super(looper); + } + + public void postAndSync(Runnable runnable) throws InterruptedException { + final CountDownLatch latch = new CountDownLatch(1); + if (getLooper() == Looper.myLooper()) { + runnable.run(); + } else { + post(()->{ + runnable.run(); + latch.countDown(); + }); + assertTrue(latch.await(WAIT_TIME_MS, TimeUnit.MILLISECONDS)); + } + } + } +} diff --git a/android/media/session/MediaSessionManager.java b/android/media/session/MediaSessionManager.java index b215825c..81b4603e 100644 --- a/android/media/session/MediaSessionManager.java +++ b/android/media/session/MediaSessionManager.java @@ -24,8 +24,12 @@ import android.annotation.SystemService; import android.content.ComponentName; import android.content.Context; import android.media.AudioManager; +import android.media.IMediaSession2; import android.media.IRemoteVolumeController; -import android.media.session.ISessionManager; +import android.media.MediaSession2; +import android.media.MediaSessionService2; +import android.media.SessionToken2; +import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.RemoteException; @@ -38,6 +42,7 @@ import android.util.Log; import android.view.KeyEvent; import java.util.ArrayList; +import java.util.Collections; import java.util.List; /** @@ -330,6 +335,101 @@ public final class MediaSessionManager { } } + /** + * Called when a {@link MediaSession2} is created. + * + * @hide + */ + // TODO(jaewan): System API + public SessionToken2 createSessionToken(@NonNull String callingPackage, @NonNull String id, + @NonNull IMediaSession2 binder) { + try { + Bundle bundle = mService.createSessionToken(callingPackage, id, binder); + return SessionToken2.fromBundle(bundle); + } catch (RemoteException e) { + Log.wtf(TAG, "Cannot communicate with the service.", e); + } + return null; + } + + /** + * Get {@link List} of {@link SessionToken2} whose sessions are active now. This list represents + * active sessions regardless of whether they're {@link MediaSession2} or + * {@link MediaSessionService2}. + * + * @return list of Tokens + * @hide + */ + // TODO(jaewan): Unhide + // TODO(jaewan): Protect this with permission. + // TODO(jaewna): Add listener for change in lists. + public List getActiveSessionTokens() { + try { + List bundles = mService.getSessionTokens( + /* activeSessionOnly */ true, /* sessionServiceOnly */ false); + return toTokenList(bundles); + } catch (RemoteException e) { + Log.wtf(TAG, "Cannot communicate with the service.", e); + return Collections.emptyList(); + } + } + + /** + * Get {@link List} of {@link SessionToken2} for {@link MediaSessionService2} regardless of their + * activeness. This list represents media apps that support background playback. + * + * @return list of Tokens + * @hide + */ + // TODO(jaewan): Unhide + // TODO(jaewna): Add listener for change in lists. + public List getSessionServiceTokens() { + try { + List bundles = mService.getSessionTokens( + /* activeSessionOnly */ false, /* sessionServiceOnly */ true); + return toTokenList(bundles); + } catch (RemoteException e) { + Log.wtf(TAG, "Cannot communicate with the service.", e); + return Collections.emptyList(); + } + } + + /** + * Get all {@link SessionToken2}s. This is the combined list of {@link #getActiveSessionTokens()} + * and {@link #getSessionServiceTokens}. + * + * @return list of Tokens + * @see #getActiveSessionTokens + * @see #getSessionServiceTokens + * @hide + */ + // TODO(jaewan): Unhide + // TODO(jaewan): Protect this with permission. + // TODO(jaewna): Add listener for change in lists. + public List getAllSessionTokens() { + try { + List bundles = mService.getSessionTokens( + /* activeSessionOnly */ false, /* sessionServiceOnly */ false); + return toTokenList(bundles); + } catch (RemoteException e) { + Log.wtf(TAG, "Cannot communicate with the service.", e); + return Collections.emptyList(); + } + } + + private static List toTokenList(List bundles) { + List tokens = new ArrayList<>(); + if (bundles != null) { + for (int i = 0; i < bundles.size(); i++) { + SessionToken2 token = SessionToken2.fromBundle(bundles.get(i)); + if (token != null) { + tokens.add(token); + } + } + } + return tokens; + } + /** * Check if the global priority session is currently active. This can be * used to decide if media keys should be sent to the session or to the app. diff --git a/android/media/update/ApiLoader.java b/android/media/update/ApiLoader.java new file mode 100644 index 00000000..b928e931 --- /dev/null +++ b/android/media/update/ApiLoader.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2017 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.update; + +import android.content.res.Resources; +import android.content.Context; +import android.content.pm.PackageManager; +import android.os.Build; + +/** + * @hide + */ +public final class ApiLoader { + private static Object sMediaLibrary; + + private static final String UPDATE_PACKAGE = "com.android.media.update"; + private static final String UPDATE_CLASS = "com.android.media.update.ApiFactory"; + private static final String UPDATE_METHOD = "initialize"; + + private ApiLoader() { } + + public static StaticProvider getProvider(Context context) { + try { + return (StaticProvider) getMediaLibraryImpl(context); + } catch (PackageManager.NameNotFoundException | ReflectiveOperationException e) { + throw new RuntimeException(e); + } + } + + // TODO This method may do I/O; Ensure it does not violate (emit warnings in) strict mode. + private static synchronized Object getMediaLibraryImpl(Context context) + throws PackageManager.NameNotFoundException, ReflectiveOperationException { + if (sMediaLibrary != null) return sMediaLibrary; + + // TODO Figure out when to use which package (query media update service) + int flags = Build.IS_DEBUGGABLE ? 0 : PackageManager.MATCH_FACTORY_ONLY; + Context libContext = context.createApplicationContext( + context.getPackageManager().getPackageInfo(UPDATE_PACKAGE, flags).applicationInfo, + Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY); + sMediaLibrary = libContext.getClassLoader() + .loadClass(UPDATE_CLASS) + .getMethod(UPDATE_METHOD, Resources.class, Resources.Theme.class) + .invoke(null, libContext.getResources(), libContext.getTheme()); + return sMediaLibrary; + } +} diff --git a/android/media/update/MediaBrowser2Provider.java b/android/media/update/MediaBrowser2Provider.java new file mode 100644 index 00000000..e48711d9 --- /dev/null +++ b/android/media/update/MediaBrowser2Provider.java @@ -0,0 +1,33 @@ +/* + * Copyright 2018 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.update; + +import android.os.Bundle; + +/** + * @hide + */ +public interface MediaBrowser2Provider extends MediaController2Provider { + void getBrowserRoot_impl(Bundle rootHints); + + void subscribe_impl(String parentId, Bundle options); + void unsubscribe_impl(String parentId, Bundle options); + + void getItem_impl(String mediaId); + void getChildren_impl(String parentId, int page, int pageSize, Bundle options); + void search_impl(String query, int page, int pageSize, Bundle extras); +} diff --git a/android/media/update/MediaControlView2Provider.java b/android/media/update/MediaControlView2Provider.java new file mode 100644 index 00000000..6b38c926 --- /dev/null +++ b/android/media/update/MediaControlView2Provider.java @@ -0,0 +1,47 @@ +/* + * Copyright 2017 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.update; + +import android.annotation.SystemApi; +import android.media.session.MediaController; +import android.view.View; + +/** + * Interface for connecting the public API to an updatable implementation. + * + * Each instance object is connected to one corresponding updatable object which implements the + * runtime behavior of that class. There should a corresponding provider method for all public + * methods. + * + * All methods behave as per their namesake in the public API. + * + * @see android.widget.MediaControlView2 + * + * @hide + */ +// TODO @SystemApi +public interface MediaControlView2Provider extends ViewProvider { + void setController_impl(MediaController controller); + void show_impl(); + void show_impl(int timeout); + boolean isShowing_impl(); + void hide_impl(); + void showSubtitle_impl(); + void hideSubtitle_impl(); + void setPrevNextListeners_impl(View.OnClickListener next, View.OnClickListener prev); + void setButtonVisibility_impl(int button, boolean visible); +} diff --git a/android/media/update/MediaController2Provider.java b/android/media/update/MediaController2Provider.java new file mode 100644 index 00000000..c5f6b963 --- /dev/null +++ b/android/media/update/MediaController2Provider.java @@ -0,0 +1,64 @@ +/* + * Copyright 2018 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.update; + +import android.app.PendingIntent; +import android.media.MediaController2.PlaybackInfo; +import android.media.MediaItem2; +import android.media.MediaSession2.Command; +import android.media.MediaSession2.PlaylistParam; +import android.media.PlaybackState2; +import android.media.Rating2; +import android.media.SessionToken2; +import android.net.Uri; +import android.os.Bundle; +import android.os.ResultReceiver; + +import java.util.List; + +/** + * @hide + */ +public interface MediaController2Provider extends TransportControlProvider { + void close_impl(); + SessionToken2 getSessionToken_impl(); + boolean isConnected_impl(); + + PendingIntent getSessionActivity_impl(); + int getRatingType_impl(); + + void setVolumeTo_impl(int value, int flags); + void adjustVolume_impl(int direction, int flags); + PlaybackInfo getPlaybackInfo_impl(); + + void prepareFromUri_impl(Uri uri, Bundle extras); + void prepareFromSearch_impl(String query, Bundle extras); + void prepareMediaId_impl(String mediaId, Bundle extras); + void playFromSearch_impl(String query, Bundle extras); + void playFromUri_impl(String uri, Bundle extras); + void playFromMediaId_impl(String mediaId, Bundle extras); + + void setRating_impl(Rating2 rating); + void sendCustomCommand_impl(Command command, Bundle args, ResultReceiver cb); + List getPlaylist_impl(); + + void removePlaylistItem_impl(MediaItem2 index); + void addPlaylistItem_impl(int index, MediaItem2 item); + + PlaylistParam getPlaylistParam_impl(); + PlaybackState2 getPlaybackState_impl(); +} diff --git a/android/media/update/MediaLibraryService2Provider.java b/android/media/update/MediaLibraryService2Provider.java new file mode 100644 index 00000000..dac57841 --- /dev/null +++ b/android/media/update/MediaLibraryService2Provider.java @@ -0,0 +1,30 @@ +/* + * Copyright 2018 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.update; + +import android.media.MediaSession2.ControllerInfo; +import android.os.Bundle; /** + * @hide + */ +public interface MediaLibraryService2Provider extends MediaSessionService2Provider { + // Nothing new for now + + interface MediaLibrarySessionProvider extends MediaSession2Provider { + void notifyChildrenChanged_impl(ControllerInfo controller, String parentId, Bundle options); + void notifyChildrenChanged_impl(String parentId, Bundle options); + } +} diff --git a/android/media/update/MediaSession2Provider.java b/android/media/update/MediaSession2Provider.java new file mode 100644 index 00000000..2a68ad1d --- /dev/null +++ b/android/media/update/MediaSession2Provider.java @@ -0,0 +1,65 @@ +/* + * Copyright 2018 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.update; + +import android.media.AudioAttributes; +import android.media.MediaItem2; +import android.media.MediaPlayerBase; +import android.media.MediaSession2; +import android.media.MediaSession2.Command; +import android.media.MediaSession2.CommandButton; +import android.media.MediaSession2.CommandGroup; +import android.media.MediaSession2.ControllerInfo; +import android.media.SessionToken2; +import android.media.VolumeProvider; +import android.os.Bundle; +import android.os.ResultReceiver; + +import java.util.List; + +/** + * @hide + */ +public interface MediaSession2Provider extends TransportControlProvider { + void close_impl(); + void setPlayer_impl(MediaPlayerBase player); + void setPlayer_impl(MediaPlayerBase player, VolumeProvider volumeProvider); + MediaPlayerBase getPlayer_impl(); + SessionToken2 getToken_impl(); + List getConnectedControllers_impl(); + void setCustomLayout_impl(ControllerInfo controller, List layout); + void setAudioAttributes_impl(AudioAttributes attributes); + void setAudioFocusRequest_impl(int focusGain); + + void setAllowedCommands_impl(ControllerInfo controller, CommandGroup commands); + void notifyMetadataChanged_impl(); + void sendCustomCommand_impl(ControllerInfo controller, Command command, Bundle args, + ResultReceiver receiver); + void sendCustomCommand_impl(Command command, Bundle args); + void setPlaylist_impl(List playlist, MediaSession2.PlaylistParam param); + + /** + * @hide + */ + interface ControllerInfoProvider { + String getPackageName_impl(); + int getUid_impl(); + boolean isTrusted_impl(); + int hashCode_impl(); + boolean equals_impl(ControllerInfoProvider obj); + } +} diff --git a/android/media/update/MediaSessionService2Provider.java b/android/media/update/MediaSessionService2Provider.java new file mode 100644 index 00000000..a6b462b8 --- /dev/null +++ b/android/media/update/MediaSessionService2Provider.java @@ -0,0 +1,35 @@ +/* + * Copyright 2018 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.update; + +import android.content.Intent; +import android.media.MediaSession2; +import android.media.MediaSessionService2.MediaNotification; +import android.media.PlaybackState2; +import android.os.IBinder; + +/** + * @hide + */ +public interface MediaSessionService2Provider { + MediaSession2 getSession_impl(); + MediaNotification onUpdateNotification_impl(PlaybackState2 state); + + // Service + void onCreate_impl(); + IBinder onBind_impl(Intent intent); +} diff --git a/android/media/update/StaticProvider.java b/android/media/update/StaticProvider.java new file mode 100644 index 00000000..7c222c3c --- /dev/null +++ b/android/media/update/StaticProvider.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2017 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.update; + +import android.annotation.Nullable; +import android.app.PendingIntent; +import android.content.Context; +import android.media.IMediaSession2Callback; +import android.media.MediaBrowser2; +import android.media.MediaBrowser2.BrowserCallback; +import android.media.MediaController2; +import android.media.MediaController2.ControllerCallback; +import android.media.MediaLibraryService2; +import android.media.MediaLibraryService2.MediaLibrarySession; +import android.media.MediaLibraryService2.MediaLibrarySessionCallback; +import android.media.MediaPlayerBase; +import android.media.MediaSession2; +import android.media.MediaSession2.SessionCallback; +import android.media.MediaSessionService2; +import android.media.SessionToken2; +import android.media.VolumeProvider; +import android.media.update.MediaLibraryService2Provider.MediaLibrarySessionProvider; +import android.media.update.MediaSession2Provider.ControllerInfoProvider; +import android.util.AttributeSet; +import android.widget.MediaControlView2; +import android.widget.VideoView2; + +import java.util.concurrent.Executor; + +/** + * Interface for connecting the public API to an updatable implementation. + * + * This interface provides access to constructors and static methods that are otherwise not directly + * accessible via an implementation object. + * + * @hide + */ +// TODO @SystemApi +public interface StaticProvider { + MediaControlView2Provider createMediaControlView2( + MediaControlView2 instance, ViewProvider superProvider); + VideoView2Provider createVideoView2( + VideoView2 instance, ViewProvider superProvider, + @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes); + + MediaSession2Provider createMediaSession2(MediaSession2 mediaSession2, Context context, + MediaPlayerBase player, String id, Executor callbackExecutor, SessionCallback callback, + VolumeProvider volumeProvider, int ratingType, + PendingIntent sessionActivity); + ControllerInfoProvider createMediaSession2ControllerInfoProvider( + MediaSession2.ControllerInfo instance, Context context, int uid, int pid, + String packageName, IMediaSession2Callback callback); + MediaController2Provider createMediaController2( + MediaController2 instance, Context context, SessionToken2 token, + ControllerCallback callback, Executor executor); + MediaBrowser2Provider createMediaBrowser2( + MediaBrowser2 instance, Context context, SessionToken2 token, + BrowserCallback callback, Executor executor); + MediaSessionService2Provider createMediaSessionService2( + MediaSessionService2 instance); + MediaSessionService2Provider createMediaLibraryService2( + MediaLibraryService2 instance); + MediaLibrarySessionProvider createMediaLibraryService2MediaLibrarySession( + MediaLibrarySession instance, Context context, MediaPlayerBase player, String id, + Executor callbackExecutor, MediaLibrarySessionCallback callback, + VolumeProvider volumeProvider, int ratingType, PendingIntent sessionActivity); +} diff --git a/android/media/update/TransportControlProvider.java b/android/media/update/TransportControlProvider.java new file mode 100644 index 00000000..5217a9d9 --- /dev/null +++ b/android/media/update/TransportControlProvider.java @@ -0,0 +1,39 @@ +/* + * Copyright 2018 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.update; + +import android.media.MediaPlayerBase; +import android.media.session.PlaybackState; +import android.os.Handler; + +/** + * @hide + */ +// TODO(jaewan): SystemApi +public interface TransportControlProvider { + void play_impl(); + void pause_impl(); + void stop_impl(); + void skipToPrevious_impl(); + void skipToNext_impl(); + + void prepare_impl(); + void fastForward_impl(); + void rewind_impl(); + void seekTo_impl(long pos); + void setCurrentPlaylistItem_impl(int index); +} diff --git a/android/media/update/VideoView2Provider.java b/android/media/update/VideoView2Provider.java new file mode 100644 index 00000000..416ea98d --- /dev/null +++ b/android/media/update/VideoView2Provider.java @@ -0,0 +1,73 @@ +/* + * Copyright 2018 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.update; + +import android.media.AudioAttributes; +import android.media.MediaPlayerBase; +import android.net.Uri; +import android.widget.MediaControlView2; +import android.widget.VideoView2; + +import java.util.List; +import java.util.Map; + +/** + * Interface for connecting the public API to an updatable implementation. + * + * Each instance object is connected to one corresponding updatable object which implements the + * runtime behavior of that class. There should a corresponding provider method for all public + * methods. + * + * All methods behave as per their namesake in the public API. + * + * @see android.widget.VideoView2 + * + * @hide + */ +// TODO @SystemApi +public interface VideoView2Provider extends ViewProvider { + void setMediaControlView2_impl(MediaControlView2 mediaControlView); + MediaControlView2 getMediaControlView2_impl(); + void start_impl(); + void pause_impl(); + int getDuration_impl(); + int getCurrentPosition_impl(); + void seekTo_impl(int msec); + boolean isPlaying_impl(); + int getBufferPercentage_impl(); + int getAudioSessionId_impl(); + void showSubtitle_impl(); + void hideSubtitle_impl(); + void setFullScreen_impl(boolean fullScreen); + void setSpeed_impl(float speed); + float getSpeed_impl(); + void setAudioFocusRequest_impl(int focusGain); + void setAudioAttributes_impl(AudioAttributes attributes); + void setRouteAttributes_impl(List routeCategories, MediaPlayerBase player); + void setVideoPath_impl(String path); + void setVideoURI_impl(Uri uri); + void setVideoURI_impl(Uri uri, Map headers); + void setViewType_impl(int viewType); + int getViewType_impl(); + void stopPlayback_impl(); + void setOnPreparedListener_impl(VideoView2.OnPreparedListener l); + void setOnCompletionListener_impl(VideoView2.OnCompletionListener l); + void setOnErrorListener_impl(VideoView2.OnErrorListener l); + void setOnInfoListener_impl(VideoView2.OnInfoListener l); + void setOnViewTypeChangedListener_impl(VideoView2.OnViewTypeChangedListener l); + void setFullScreenChangedListener_impl(VideoView2.OnFullScreenChangedListener l); +} diff --git a/android/media/update/ViewProvider.java b/android/media/update/ViewProvider.java new file mode 100644 index 00000000..78c5b36f --- /dev/null +++ b/android/media/update/ViewProvider.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2017 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.update; + +import android.annotation.SystemApi; +import android.graphics.Canvas; +import android.view.KeyEvent; +import android.view.MotionEvent; + +/** + * Interface for connecting the public API to an updatable implementation. + * + * Each instance object is connected to one corresponding updatable object which implements the + * runtime behavior of that class. There should a corresponding provider method for all public + * methods. + * + * All methods behave as per their namesake in the public API. + * + * @see android.view.View + * + * @hide + */ +// TODO @SystemApi +public interface ViewProvider { + // TODO Add more (all?) methods from View + void onAttachedToWindow_impl(); + void onDetachedFromWindow_impl(); + CharSequence getAccessibilityClassName_impl(); + boolean onTouchEvent_impl(MotionEvent ev); + boolean onTrackballEvent_impl(MotionEvent ev); + boolean onKeyDown_impl(int keyCode, KeyEvent event); + void onFinishInflate_impl(); + boolean dispatchKeyEvent_impl(KeyEvent event); + void setEnabled_impl(boolean enabled); +} -- cgit v1.2.3