• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.media;
18 
19 import static android.Manifest.permission.BIND_IMS_SERVICE;
20 import static android.content.pm.PackageManager.PERMISSION_GRANTED;
21 import static android.media.audio.Flags.FLAG_ROUTED_DEVICE_IDS;
22 
23 import android.annotation.FlaggedApi;
24 import android.annotation.IntDef;
25 import android.annotation.NonNull;
26 import android.annotation.Nullable;
27 import android.annotation.RequiresPermission;
28 import android.annotation.SystemApi;
29 import android.app.ActivityThread;
30 import android.compat.annotation.UnsupportedAppUsage;
31 import android.content.AttributionSource;
32 import android.content.AttributionSource.ScopedParcelState;
33 import android.content.ContentProvider;
34 import android.content.ContentResolver;
35 import android.content.Context;
36 import android.content.res.AssetFileDescriptor;
37 import android.graphics.SurfaceTexture;
38 import android.media.SubtitleController.Anchor;
39 import android.media.SubtitleTrack.RenderingWidget;
40 import android.net.Uri;
41 import android.os.Build;
42 import android.os.Bundle;
43 import android.os.FileUtils;
44 import android.os.Handler;
45 import android.os.HandlerThread;
46 import android.os.IBinder;
47 import android.os.Looper;
48 import android.os.Message;
49 import android.os.Parcel;
50 import android.os.ParcelFileDescriptor;
51 import android.os.Parcelable;
52 import android.os.PersistableBundle;
53 import android.os.PowerManager;
54 import android.os.Process;
55 import android.os.SystemProperties;
56 import android.provider.Settings;
57 import android.system.ErrnoException;
58 import android.system.Os;
59 import android.system.OsConstants;
60 import android.util.ArrayMap;
61 import android.util.Log;
62 import android.util.Pair;
63 import android.view.Surface;
64 import android.view.SurfaceHolder;
65 import android.widget.VideoView;
66 
67 import com.android.internal.annotations.GuardedBy;
68 import com.android.internal.util.Preconditions;
69 
70 import libcore.io.IoBridge;
71 import libcore.io.Streams;
72 
73 import java.io.ByteArrayOutputStream;
74 import java.io.File;
75 import java.io.FileDescriptor;
76 import java.io.FileInputStream;
77 import java.io.IOException;
78 import java.io.InputStream;
79 import java.lang.annotation.Retention;
80 import java.lang.annotation.RetentionPolicy;
81 import java.lang.ref.WeakReference;
82 import java.net.CookieHandler;
83 import java.net.CookieManager;
84 import java.net.HttpCookie;
85 import java.net.HttpURLConnection;
86 import java.net.InetSocketAddress;
87 import java.net.URL;
88 import java.nio.ByteOrder;
89 import java.util.ArrayList;
90 import java.util.Arrays;
91 import java.util.BitSet;
92 import java.util.HashMap;
93 import java.util.List;
94 import java.util.Map;
95 import java.util.Objects;
96 import java.util.Scanner;
97 import java.util.Set;
98 import java.util.UUID;
99 import java.util.Vector;
100 import java.util.concurrent.Executor;
101 
102 /**
103  * MediaPlayer class can be used to control playback of audio/video files and streams.
104  *
105  * <p>MediaPlayer is not thread-safe. Creation of and all access to player instances
106  * should be on the same thread. If registering <a href="#Callbacks">callbacks</a>,
107  * the thread must have a Looper.
108  *
109  * <p>Topics covered here are:
110  * <ol>
111  * <li><a href="#StateDiagram">State Diagram</a>
112  * <li><a href="#Valid_and_Invalid_States">Valid and Invalid States</a>
113  * <li><a href="#Permissions">Permissions</a>
114  * <li><a href="#Callbacks">Register informational and error callbacks</a>
115  * </ol>
116  *
117  * <div class="special reference">
118  * <h3>Developer Guides</h3>
119  * <p>For more information about how to use MediaPlayer, read the
120  * <a href="{@docRoot}guide/topics/media/mediaplayer.html">Media Playback</a> developer guide.</p>
121  * </div>
122  *
123  * <a name="StateDiagram"></a>
124  * <h3>State Diagram</h3>
125  *
126  * <p>Playback control of audio/video files and streams is managed as a state
127  * machine. The following diagram shows the life cycle and the states of a
128  * MediaPlayer object driven by the supported playback control operations.
129  * The ovals represent the states a MediaPlayer object may reside
130  * in. The arcs represent the playback control operations that drive the object
131  * state transition. There are two types of arcs. The arcs with a single arrow
132  * head represent synchronous method calls, while those with
133  * a double arrow head represent asynchronous method calls.</p>
134  *
135  * <p><img src="../../../images/mediaplayer_state_diagram.gif"
136  *         alt="MediaPlayer State diagram"
137  *         border="0" /></p>
138  *
139  * <p>From this state diagram, one can see that a MediaPlayer object has the
140  *    following states:</p>
141  * <ul>
142  *     <li>When a MediaPlayer object is just created using <code>new</code> or
143  *         after {@link #reset()} is called, it is in the <em>Idle</em> state; and after
144  *         {@link #release()} is called, it is in the <em>End</em> state. Between these
145  *         two states is the life cycle of the MediaPlayer object.
146  *         <ul>
147  *         <li>There is a subtle but important difference between a newly constructed
148  *         MediaPlayer object and the MediaPlayer object after {@link #reset()}
149  *         is called. It is a programming error to invoke methods such
150  *         as {@link #getCurrentPosition()},
151  *         {@link #getDuration()}, {@link #getVideoHeight()},
152  *         {@link #getVideoWidth()}, {@link #setAudioAttributes(AudioAttributes)},
153  *         {@link #setLooping(boolean)},
154  *         {@link #setVolume(float, float)}, {@link #pause()}, {@link #start()},
155  *         {@link #stop()}, {@link #seekTo(long, int)}, {@link #prepare()} or
156  *         {@link #prepareAsync()} in the <em>Idle</em> state for both cases. If any of these
157  *         methods is called right after a MediaPlayer object is constructed,
158  *         the user supplied callback method OnErrorListener.onError() won't be
159  *         called by the internal player engine and the object state remains
160  *         unchanged; but if these methods are called right after {@link #reset()},
161  *         the user supplied callback method OnErrorListener.onError() will be
162  *         invoked by the internal player engine and the object will be
163  *         transfered to the <em>Error</em> state. </li>
164  *         <li>You must keep a reference to a MediaPlayer instance to prevent it from being garbage
165  *         collected. If a MediaPlayer instance is garbage collected, {@link #release} will be
166  *         called, causing any ongoing playback to stop.
167  *         <li>You must call {@link #release()} once you have finished using an instance to release
168  *         acquired resources, such as memory and codecs. Once you have called {@link #release}, you
169  *         must no longer interact with the released instance.
170  *         <li>MediaPlayer objects created using <code>new</code> is in the
171  *         <em>Idle</em> state, while those created with one
172  *         of the overloaded convenient <code>create</code> methods are <em>NOT</em>
173  *         in the <em>Idle</em> state. In fact, the objects are in the <em>Prepared</em>
174  *         state if the creation using <code>create</code> method is successful.
175  *         </li>
176  *         </ul>
177  *         </li>
178  *     <li>In general, some playback control operation may fail due to various
179  *         reasons, such as unsupported audio/video format, poorly interleaved
180  *         audio/video, resolution too high, streaming timeout, and the like.
181  *         Thus, error reporting and recovery is an important concern under
182  *         these circumstances. Sometimes, due to programming errors, invoking a playback
183  *         control operation in an invalid state may also occur. Under all these
184  *         error conditions, the internal player engine invokes a user supplied
185  *         OnErrorListener.onError() method if an OnErrorListener has been
186  *         registered beforehand via
187  *         {@link #setOnErrorListener(android.media.MediaPlayer.OnErrorListener)}.
188  *         <ul>
189  *         <li>It is important to note that once an error occurs, the
190  *         MediaPlayer object enters the <em>Error</em> state (except as noted
191  *         above), even if an error listener has not been registered by the application.</li>
192  *         <li>In order to reuse a MediaPlayer object that is in the <em>
193  *         Error</em> state and recover from the error,
194  *         {@link #reset()} can be called to restore the object to its <em>Idle</em>
195  *         state.</li>
196  *         <li>It is good programming practice to have your application
197  *         register a OnErrorListener to look out for error notifications from
198  *         the internal player engine.</li>
199  *         <li>IllegalStateException is
200  *         thrown to prevent programming errors such as calling {@link #prepare()},
201  *         {@link #prepareAsync()}, or one of the overloaded <code>setDataSource
202  *         </code> methods in an invalid state. </li>
203  *         </ul>
204  *         </li>
205  *     <li>Calling
206  *         {@link #setDataSource(FileDescriptor)}, or
207  *         {@link #setDataSource(String)}, or
208  *         {@link #setDataSource(Context, Uri)}, or
209  *         {@link #setDataSource(FileDescriptor, long, long)}, or
210  *         {@link #setDataSource(MediaDataSource)} transfers a
211  *         MediaPlayer object in the <em>Idle</em> state to the
212  *         <em>Initialized</em> state.
213  *         <ul>
214  *         <li>An IllegalStateException is thrown if
215  *         setDataSource() is called in any other state.</li>
216  *         <li>It is good programming
217  *         practice to always look out for <code>IllegalArgumentException</code>
218  *         and <code>IOException</code> that may be thrown from the overloaded
219  *         <code>setDataSource</code> methods.</li>
220  *         </ul>
221  *         </li>
222  *     <li>A MediaPlayer object must first enter the <em>Prepared</em> state
223  *         before playback can be started.
224  *         <ul>
225  *         <li>There are two ways (synchronous vs.
226  *         asynchronous) that the <em>Prepared</em> state can be reached:
227  *         either a call to {@link #prepare()} (synchronous) which
228  *         transfers the object to the <em>Prepared</em> state once the method call
229  *         returns, or a call to {@link #prepareAsync()} (asynchronous) which
230  *         first transfers the object to the <em>Preparing</em> state after the
231  *         call returns (which occurs almost right away) while the internal
232  *         player engine continues working on the rest of preparation work
233  *         until the preparation work completes. When the preparation completes or when {@link #prepare()} call returns,
234  *         the internal player engine then calls a user supplied callback method,
235  *         onPrepared() of the OnPreparedListener interface, if an
236  *         OnPreparedListener is registered beforehand via {@link
237  *         #setOnPreparedListener(android.media.MediaPlayer.OnPreparedListener)}.</li>
238  *         <li>It is important to note that
239  *         the <em>Preparing</em> state is a transient state, and the behavior
240  *         of calling any method with side effect while a MediaPlayer object is
241  *         in the <em>Preparing</em> state is undefined.</li>
242  *         <li>An IllegalStateException is
243  *         thrown if {@link #prepare()} or {@link #prepareAsync()} is called in
244  *         any other state.</li>
245  *         <li>While in the <em>Prepared</em> state, properties
246  *         such as audio/sound volume, screenOnWhilePlaying, looping can be
247  *         adjusted by invoking the corresponding set methods.</li>
248  *         </ul>
249  *         </li>
250  *     <li>To start the playback, {@link #start()} must be called. After
251  *         {@link #start()} returns successfully, the MediaPlayer object is in the
252  *         <em>Started</em> state. {@link #isPlaying()} can be called to test
253  *         whether the MediaPlayer object is in the <em>Started</em> state.
254  *         <ul>
255  *         <li>While in the <em>Started</em> state, the internal player engine calls
256  *         a user supplied OnBufferingUpdateListener.onBufferingUpdate() callback
257  *         method if a OnBufferingUpdateListener has been registered beforehand
258  *         via {@link #setOnBufferingUpdateListener(OnBufferingUpdateListener)}.
259  *         This callback allows applications to keep track of the buffering status
260  *         while streaming audio/video.</li>
261  *         <li>Calling {@link #start()} has no effect
262  *         on a MediaPlayer object that is already in the <em>Started</em> state.</li>
263  *         </ul>
264  *         </li>
265  *     <li>Playback can be paused and stopped, and the current playback position
266  *         can be adjusted. Playback can be paused via {@link #pause()}. When the call to
267  *         {@link #pause()} returns, the MediaPlayer object enters the
268  *         <em>Paused</em> state. Note that the transition from the <em>Started</em>
269  *         state to the <em>Paused</em> state and vice versa happens
270  *         asynchronously in the player engine. It may take some time before
271  *         the state is updated in calls to {@link #isPlaying()}, and it can be
272  *         a number of seconds in the case of streamed content.
273  *         <ul>
274  *         <li>Calling {@link #start()} to resume playback for a paused
275  *         MediaPlayer object, and the resumed playback
276  *         position is the same as where it was paused. When the call to
277  *         {@link #start()} returns, the paused MediaPlayer object goes back to
278  *         the <em>Started</em> state.</li>
279  *         <li>Calling {@link #pause()} has no effect on
280  *         a MediaPlayer object that is already in the <em>Paused</em> state.</li>
281  *         </ul>
282  *         </li>
283  *     <li>Calling  {@link #stop()} stops playback and causes a
284  *         MediaPlayer in the <em>Started</em>, <em>Paused</em>, <em>Prepared
285  *         </em> or <em>PlaybackCompleted</em> state to enter the
286  *         <em>Stopped</em> state.
287  *         <ul>
288  *         <li>Once in the <em>Stopped</em> state, playback cannot be started
289  *         until {@link #prepare()} or {@link #prepareAsync()} are called to set
290  *         the MediaPlayer object to the <em>Prepared</em> state again.</li>
291  *         <li>Calling {@link #stop()} has no effect on a MediaPlayer
292  *         object that is already in the <em>Stopped</em> state.</li>
293  *         </ul>
294  *         </li>
295  *     <li>The playback position can be adjusted with a call to
296  *         {@link #seekTo(long, int)}.
297  *         <ul>
298  *         <li>Although the asynchronuous {@link #seekTo(long, int)}
299  *         call returns right away, the actual seek operation may take a while to
300  *         finish, especially for audio/video being streamed. When the actual
301  *         seek operation completes, the internal player engine calls a user
302  *         supplied OnSeekComplete.onSeekComplete() if an OnSeekCompleteListener
303  *         has been registered beforehand via
304  *         {@link #setOnSeekCompleteListener(OnSeekCompleteListener)}.</li>
305  *         <li>Please
306  *         note that {@link #seekTo(long, int)} can also be called in the other states,
307  *         such as <em>Prepared</em>, <em>Paused</em> and <em>PlaybackCompleted
308  *         </em> state. When {@link #seekTo(long, int)} is called in those states,
309  *         one video frame will be displayed if the stream has video and the requested
310  *         position is valid.
311  *         </li>
312  *         <li>Furthermore, the actual current playback position
313  *         can be retrieved with a call to {@link #getCurrentPosition()}, which
314  *         is helpful for applications such as a Music player that need to keep
315  *         track of the playback progress.</li>
316  *         </ul>
317  *         </li>
318  *     <li>When the playback reaches the end of stream, the playback completes.
319  *         <ul>
320  *         <li>If the looping mode was being set to <var>true</var> with
321  *         {@link #setLooping(boolean)}, the MediaPlayer object shall remain in
322  *         the <em>Started</em> state.</li>
323  *         <li>If the looping mode was set to <var>false
324  *         </var>, the player engine calls a user supplied callback method,
325  *         OnCompletion.onCompletion(), if a OnCompletionListener is registered
326  *         beforehand via {@link #setOnCompletionListener(OnCompletionListener)}.
327  *         The invoke of the callback signals that the object is now in the <em>
328  *         PlaybackCompleted</em> state.</li>
329  *         <li>While in the <em>PlaybackCompleted</em>
330  *         state, calling {@link #start()} can restart the playback from the
331  *         beginning of the audio/video source.</li>
332  * </ul>
333  *
334  *
335  * <a name="Valid_and_Invalid_States"></a>
336  * <h3>Valid and invalid states</h3>
337  *
338  * <table border="0" cellspacing="0" cellpadding="0">
339  * <tr><td>Method Name </p></td>
340  *     <td>Valid States </p></td>
341  *     <td>Invalid States </p></td>
342  *     <td>Comments </p></td></tr>
343  * <tr><td>attachAuxEffect </p></td>
344  *     <td>{Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} </p></td>
345  *     <td>{Idle, Error} </p></td>
346  *     <td>This method must be called after setDataSource.
347  *     Calling it does not change the object state. </p></td></tr>
348  * <tr><td>getAudioSessionId </p></td>
349  *     <td>any </p></td>
350  *     <td>{} </p></td>
351  *     <td>This method can be called in any state and calling it does not change
352  *         the object state. </p></td></tr>
353  * <tr><td>getCurrentPosition </p></td>
354  *     <td>{Idle, Initialized, Prepared, Started, Paused, Stopped,
355  *         PlaybackCompleted} </p></td>
356  *     <td>{Error}</p></td>
357  *     <td>Successful invoke of this method in a valid state does not change the
358  *         state. Calling this method in an invalid state transfers the object
359  *         to the <em>Error</em> state. </p></td></tr>
360  * <tr><td>getDuration </p></td>
361  *     <td>{Prepared, Started, Paused, Stopped, PlaybackCompleted} </p></td>
362  *     <td>{Idle, Initialized, Error} </p></td>
363  *     <td>Successful invoke of this method in a valid state does not change the
364  *         state. Calling this method in an invalid state transfers the object
365  *         to the <em>Error</em> state. </p></td></tr>
366  * <tr><td>getVideoHeight </p></td>
367  *     <td>{Idle, Initialized, Prepared, Started, Paused, Stopped,
368  *         PlaybackCompleted}</p></td>
369  *     <td>{Error}</p></td>
370  *     <td>Successful invoke of this method in a valid state does not change the
371  *         state. Calling this method in an invalid state transfers the object
372  *         to the <em>Error</em> state.  </p></td></tr>
373  * <tr><td>getVideoWidth </p></td>
374  *     <td>{Idle, Initialized, Prepared, Started, Paused, Stopped,
375  *         PlaybackCompleted}</p></td>
376  *     <td>{Error}</p></td>
377  *     <td>Successful invoke of this method in a valid state does not change
378  *         the state. Calling this method in an invalid state transfers the
379  *         object to the <em>Error</em> state. </p></td></tr>
380  * <tr><td>isPlaying </p></td>
381  *     <td>{Idle, Initialized, Prepared, Started, Paused, Stopped,
382  *          PlaybackCompleted}</p></td>
383  *     <td>{Error}</p></td>
384  *     <td>Successful invoke of this method in a valid state does not change
385  *         the state. Calling this method in an invalid state transfers the
386  *         object to the <em>Error</em> state. </p></td></tr>
387  * <tr><td>pause </p></td>
388  *     <td>{Started, Paused, PlaybackCompleted}</p></td>
389  *     <td>{Idle, Initialized, Prepared, Stopped, Error}</p></td>
390  *     <td>Successful invoke of this method in a valid state transfers the
391  *         object to the <em>Paused</em> state. Calling this method in an
392  *         invalid state transfers the object to the <em>Error</em> state.</p></td></tr>
393  * <tr><td>prepare </p></td>
394  *     <td>{Initialized, Stopped} </p></td>
395  *     <td>{Idle, Prepared, Started, Paused, PlaybackCompleted, Error} </p></td>
396  *     <td>Successful invoke of this method in a valid state transfers the
397  *         object to the <em>Prepared</em> state. Calling this method in an
398  *         invalid state throws an IllegalStateException.</p></td></tr>
399  * <tr><td>prepareAsync </p></td>
400  *     <td>{Initialized, Stopped} </p></td>
401  *     <td>{Idle, Prepared, Started, Paused, PlaybackCompleted, Error} </p></td>
402  *     <td>Successful invoke of this method in a valid state transfers the
403  *         object to the <em>Preparing</em> state. Calling this method in an
404  *         invalid state throws an IllegalStateException.</p></td></tr>
405  * <tr><td>release </p></td>
406  *     <td>any </p></td>
407  *     <td>{} </p></td>
408  *     <td>After {@link #release()}, you must not interact with the object. </p></td></tr>
409  * <tr><td>reset </p></td>
410  *     <td>{Idle, Initialized, Prepared, Started, Paused, Stopped,
411  *         PlaybackCompleted, Error}</p></td>
412  *     <td>{}</p></td>
413  *     <td>After {@link #reset()}, the object is like being just created.</p></td></tr>
414  * <tr><td>seekTo </p></td>
415  *     <td>{Prepared, Started, Paused, PlaybackCompleted} </p></td>
416  *     <td>{Idle, Initialized, Stopped, Error}</p></td>
417  *     <td>Successful invoke of this method in a valid state does not change
418  *         the state. Calling this method in an invalid state transfers the
419  *         object to the <em>Error</em> state. </p></td></tr>
420  * <tr><td>setAudioAttributes </p></td>
421  *     <td>{Idle, Initialized, Stopped, Prepared, Started, Paused,
422  *          PlaybackCompleted}</p></td>
423  *     <td>{Error}</p></td>
424  *     <td>Successful invoke of this method does not change the state. In order for the
425  *         target audio attributes type to become effective, this method must be called before
426  *         prepare() or prepareAsync().</p></td></tr>
427  * <tr><td>setAudioSessionId </p></td>
428  *     <td>{Idle} </p></td>
429  *     <td>{Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted,
430  *          Error} </p></td>
431  *     <td>This method must be called in idle state as the audio session ID must be known before
432  *         calling setDataSource. Calling it does not change the object state. </p></td></tr>
433  * <tr><td>setAudioStreamType (deprecated)</p></td>
434  *     <td>{Idle, Initialized, Stopped, Prepared, Started, Paused,
435  *          PlaybackCompleted}</p></td>
436  *     <td>{Error}</p></td>
437  *     <td>Successful invoke of this method does not change the state. In order for the
438  *         target audio stream type to become effective, this method must be called before
439  *         prepare() or prepareAsync().</p></td></tr>
440  * <tr><td>setAuxEffectSendLevel </p></td>
441  *     <td>any</p></td>
442  *     <td>{} </p></td>
443  *     <td>Calling this method does not change the object state. </p></td></tr>
444  * <tr><td>setDataSource </p></td>
445  *     <td>{Idle} </p></td>
446  *     <td>{Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted,
447  *          Error} </p></td>
448  *     <td>Successful invoke of this method in a valid state transfers the
449  *         object to the <em>Initialized</em> state. Calling this method in an
450  *         invalid state throws an IllegalStateException.</p></td></tr>
451  * <tr><td>setDisplay </p></td>
452  *     <td>any </p></td>
453  *     <td>{} </p></td>
454  *     <td>This method can be called in any state and calling it does not change
455  *         the object state. </p></td></tr>
456  * <tr><td>setSurface </p></td>
457  *     <td>any </p></td>
458  *     <td>{} </p></td>
459  *     <td>This method can be called in any state and calling it does not change
460  *         the object state. </p></td></tr>
461  * <tr><td>setVideoScalingMode </p></td>
462  *     <td>{Initialized, Prepared, Started, Paused, Stopped, PlaybackCompleted} </p></td>
463  *     <td>{Idle, Error}</p></td>
464  *     <td>Successful invoke of this method does not change the state.</p></td></tr>
465  * <tr><td>setLooping </p></td>
466  *     <td>{Idle, Initialized, Stopped, Prepared, Started, Paused,
467  *         PlaybackCompleted}</p></td>
468  *     <td>{Error}</p></td>
469  *     <td>Successful invoke of this method in a valid state does not change
470  *         the state. Calling this method in an
471  *         invalid state transfers the object to the <em>Error</em> state.</p></td></tr>
472  * <tr><td>isLooping </p></td>
473  *     <td>any </p></td>
474  *     <td>{} </p></td>
475  *     <td>This method can be called in any state and calling it does not change
476  *         the object state. </p></td></tr>
477  * <tr><td>setOnBufferingUpdateListener </p></td>
478  *     <td>any </p></td>
479  *     <td>{} </p></td>
480  *     <td>This method can be called in any state and calling it does not change
481  *         the object state. </p></td></tr>
482  * <tr><td>setOnCompletionListener </p></td>
483  *     <td>any </p></td>
484  *     <td>{} </p></td>
485  *     <td>This method can be called in any state and calling it does not change
486  *         the object state. </p></td></tr>
487  * <tr><td>setOnErrorListener </p></td>
488  *     <td>any </p></td>
489  *     <td>{} </p></td>
490  *     <td>This method can be called in any state and calling it does not change
491  *         the object state. </p></td></tr>
492  * <tr><td>setOnPreparedListener </p></td>
493  *     <td>any </p></td>
494  *     <td>{} </p></td>
495  *     <td>This method can be called in any state and calling it does not change
496  *         the object state. </p></td></tr>
497  * <tr><td>setOnSeekCompleteListener </p></td>
498  *     <td>any </p></td>
499  *     <td>{} </p></td>
500  *     <td>This method can be called in any state and calling it does not change
501  *         the object state. </p></td></tr>
502  * <tr><td>setPlaybackParams</p></td>
503  *     <td>{Initialized, Prepared, Started, Paused, PlaybackCompleted, Error}</p></td>
504  *     <td>{Idle, Stopped} </p></td>
505  *     <td>This method will change state in some cases, depending on when it's called.
506  *         </p></td></tr>
507  * <tr><td>setScreenOnWhilePlaying</></td>
508  *     <td>any </p></td>
509  *     <td>{} </p></td>
510  *     <td>This method can be called in any state and calling it does not change
511  *         the object state.  </p></td></tr>
512  * <tr><td>setVolume </p></td>
513  *     <td>{Idle, Initialized, Stopped, Prepared, Started, Paused,
514  *          PlaybackCompleted}</p></td>
515  *     <td>{Error}</p></td>
516  *     <td>Successful invoke of this method does not change the state.
517  * <tr><td>setWakeMode </p></td>
518  *     <td>any </p></td>
519  *     <td>{} </p></td>
520  *     <td>This method can be called in any state and calling it does not change
521  *         the object state.</p></td></tr>
522  * <tr><td>start </p></td>
523  *     <td>{Prepared, Started, Paused, PlaybackCompleted}</p></td>
524  *     <td>{Idle, Initialized, Stopped, Error}</p></td>
525  *     <td>Successful invoke of this method in a valid state transfers the
526  *         object to the <em>Started</em> state. Calling this method in an
527  *         invalid state transfers the object to the <em>Error</em> state.</p></td></tr>
528  * <tr><td>stop </p></td>
529  *     <td>{Prepared, Started, Stopped, Paused, PlaybackCompleted}</p></td>
530  *     <td>{Idle, Initialized, Error}</p></td>
531  *     <td>Successful invoke of this method in a valid state transfers the
532  *         object to the <em>Stopped</em> state. Calling this method in an
533  *         invalid state transfers the object to the <em>Error</em> state.</p></td></tr>
534  * <tr><td>getTrackInfo </p></td>
535  *     <td>{Prepared, Started, Stopped, Paused, PlaybackCompleted}</p></td>
536  *     <td>{Idle, Initialized, Error}</p></td>
537  *     <td>Successful invoke of this method does not change the state.</p></td></tr>
538  * <tr><td>addTimedTextSource </p></td>
539  *     <td>{Prepared, Started, Stopped, Paused, PlaybackCompleted}</p></td>
540  *     <td>{Idle, Initialized, Error}</p></td>
541  *     <td>Successful invoke of this method does not change the state.</p></td></tr>
542  * <tr><td>selectTrack </p></td>
543  *     <td>{Prepared, Started, Stopped, Paused, PlaybackCompleted}</p></td>
544  *     <td>{Idle, Initialized, Error}</p></td>
545  *     <td>Successful invoke of this method does not change the state.</p></td></tr>
546  * <tr><td>deselectTrack </p></td>
547  *     <td>{Prepared, Started, Stopped, Paused, PlaybackCompleted}</p></td>
548  *     <td>{Idle, Initialized, Error}</p></td>
549  *     <td>Successful invoke of this method does not change the state.</p></td></tr>
550  *
551  * </table>
552  *
553  * <a name="Permissions"></a>
554  * <h3>Permissions</h3>
555  * <p>One may need to declare a corresponding WAKE_LOCK permission {@link
556  * android.R.styleable#AndroidManifestUsesPermission &lt;uses-permission&gt;}
557  * element.
558  *
559  * <p>This class requires the {@link android.Manifest.permission#INTERNET} permission
560  * when used with network-based content.
561  *
562  * <a name="Callbacks"></a>
563  * <h3>Callbacks</h3>
564  * <p>Applications may want to register for informational and error
565  * events in order to be informed of some internal state update and
566  * possible runtime errors during playback or streaming. Registration for
567  * these events is done by properly setting the appropriate listeners (via calls
568  * to
569  * {@link #setOnPreparedListener(OnPreparedListener) setOnPreparedListener},
570  * {@link #setOnVideoSizeChangedListener(OnVideoSizeChangedListener) setOnVideoSizeChangedListener},
571  * {@link #setOnSeekCompleteListener(OnSeekCompleteListener) setOnSeekCompleteListener},
572  * {@link #setOnCompletionListener(OnCompletionListener) setOnCompletionListener},
573  * {@link #setOnBufferingUpdateListener(OnBufferingUpdateListener) setOnBufferingUpdateListener},
574  * {@link #setOnInfoListener(OnInfoListener) setOnInfoListener},
575  * {@link #setOnErrorListener(OnErrorListener) setOnErrorListener}, etc).
576  * In order to receive the respective callback
577  * associated with these listeners, applications are required to create
578  * MediaPlayer objects on a thread with its own Looper running (main UI
579  * thread by default has a Looper running).
580  *
581  */
582 public class MediaPlayer extends PlayerBase
583                          implements SubtitleController.Listener
584                                   , VolumeAutomation
585                                   , AudioRouting
586 {
587     /**
588        Constant to retrieve only the new metadata since the last
589        call.
590        // FIXME: unhide.
591        // FIXME: add link to getMetadata(boolean, boolean)
592        {@hide}
593      */
594     public static final boolean METADATA_UPDATE_ONLY = true;
595 
596     /**
597        Constant to retrieve all the metadata.
598        // FIXME: unhide.
599        // FIXME: add link to getMetadata(boolean, boolean)
600        {@hide}
601      */
602     @UnsupportedAppUsage
603     public static final boolean METADATA_ALL = false;
604 
605     /**
606        Constant to enable the metadata filter during retrieval.
607        // FIXME: unhide.
608        // FIXME: add link to getMetadata(boolean, boolean)
609        {@hide}
610      */
611     public static final boolean APPLY_METADATA_FILTER = true;
612 
613     /**
614        Constant to disable the metadata filter during retrieval.
615        // FIXME: unhide.
616        // FIXME: add link to getMetadata(boolean, boolean)
617        {@hide}
618      */
619     @UnsupportedAppUsage
620     public static final boolean BYPASS_METADATA_FILTER = false;
621 
622     static {
623         System.loadLibrary("media_jni");
native_init()624         native_init();
625     }
626 
627     private final static String TAG = "MediaPlayer";
628     // Name of the remote interface for the media player. Must be kept
629     // in sync with the 2nd parameter of the IMPLEMENT_META_INTERFACE
630     // macro invocation in IMediaPlayer.cpp
631     private final static String IMEDIA_PLAYER = "android.media.IMediaPlayer";
632 
633     private long mNativeContext; // accessed by native methods
634     private long mNativeSurfaceTexture;  // accessed by native methods
635     private int mListenerContext; // accessed by native methods
636     private SurfaceHolder mSurfaceHolder;
637     @UnsupportedAppUsage
638     private EventHandler mEventHandler;
639     private PowerManager.WakeLock mWakeLock = null;
640     private boolean mScreenOnWhilePlaying;
641     private boolean mStayAwake;
642     private int mStreamType = AudioManager.USE_DEFAULT_STREAM_TYPE;
643 
644     // Modular DRM
645     private UUID mDrmUUID;
646     private final Object mDrmLock = new Object();
647     private DrmInfo mDrmInfo;
648     private MediaDrm mDrmObj;
649     private byte[] mDrmSessionId;
650     private boolean mDrmInfoResolved;
651     private boolean mActiveDrmScheme;
652     private boolean mDrmConfigAllowed;
653     private boolean mDrmProvisioningInProgress;
654     private boolean mPrepareDrmInProgress;
655     private ProvisioningThread mDrmProvisioningThread;
656 
657     /**
658      * Default constructor.
659      *
660      * <p>Consider using one of the create() methods for synchronously instantiating a MediaPlayer
661      * from a Uri or resource.
662      *
663      * <p>You must call {@link #release()} when you are finished using the instantiated instance.
664      * Doing so frees any resources you have previously acquired.
665      */
MediaPlayer()666     public MediaPlayer() {
667         this(/*context=*/null, AudioSystem.AUDIO_SESSION_ALLOCATE);
668     }
669 
670 
671     /**
672      * Default constructor with context.
673      *
674      *  <p>Consider using one of the create() methods for synchronously instantiating a
675      *  MediaPlayer from a Uri or resource.
676      *
677      * @param context non-null context. This context will be used to pull information,
678      *  such as {@link android.content.AttributionSource} and device specific session ids, which
679      *  will be associated with the {@link MediaPlayer}.
680      *  However, the context itself will not be retained by the MediaPlayer.
681      */
MediaPlayer(@onNull Context context)682     public MediaPlayer(@NonNull Context context) {
683         this(Objects.requireNonNull(context), AudioSystem.AUDIO_SESSION_ALLOCATE);
684     }
685 
MediaPlayer(Context context, int sessionId)686     private MediaPlayer(Context context, int sessionId) {
687         super(new AudioAttributes.Builder().build(),
688                 AudioPlaybackConfiguration.PLAYER_TYPE_JAM_MEDIAPLAYER);
689 
690         Looper looper;
691         if ((looper = Looper.myLooper()) != null) {
692             mEventHandler = new EventHandler(this, looper);
693         } else if ((looper = Looper.getMainLooper()) != null) {
694             mEventHandler = new EventHandler(this, looper);
695         } else {
696             mEventHandler = null;
697         }
698 
699         mTimeProvider = new TimeProvider(this);
700         mOpenSubtitleSources = new Vector<InputStream>();
701 
702         AttributionSource attributionSource =
703                 context == null ? AttributionSource.myAttributionSource()
704                         : context.getAttributionSource();
705         // set the package name to empty if it was null
706         if (attributionSource.getPackageName() == null) {
707             attributionSource = attributionSource.withPackageName("");
708         }
709 
710         /* Native setup requires a weak reference to our object.
711          * It's easier to create it here than in C++.
712          */
713         try (ScopedParcelState attributionSourceState = attributionSource.asScopedParcelState()) {
714             native_setup(new WeakReference<>(this), attributionSourceState.getParcel(),
715                     resolvePlaybackSessionId(context, sessionId));
716         }
717         baseRegisterPlayer(getAudioSessionId());
718     }
719 
createPlayerIIdParcel()720     private Parcel createPlayerIIdParcel() {
721         Parcel parcel = newRequest();
722         parcel.writeInt(INVOKE_ID_SET_PLAYER_IID);
723         parcel.writeInt(mPlayerIId);
724         return parcel;
725     }
726 
727     /*
728      * Update the MediaPlayer SurfaceTexture.
729      * Call after setting a new display surface.
730      */
_setVideoSurface(Surface surface)731     private native void _setVideoSurface(Surface surface);
732 
733     /* Do not change these values (starting with INVOKE_ID) without updating
734      * their counterparts in include/media/mediaplayer.h!
735      */
736     private static final int INVOKE_ID_GET_TRACK_INFO = 1;
737     private static final int INVOKE_ID_ADD_EXTERNAL_SOURCE = 2;
738     private static final int INVOKE_ID_ADD_EXTERNAL_SOURCE_FD = 3;
739     private static final int INVOKE_ID_SELECT_TRACK = 4;
740     private static final int INVOKE_ID_DESELECT_TRACK = 5;
741     private static final int INVOKE_ID_SET_VIDEO_SCALE_MODE = 6;
742     private static final int INVOKE_ID_GET_SELECTED_TRACK = 7;
743     private static final int INVOKE_ID_SET_PLAYER_IID = 8;
744 
745     /**
746      * Create a request parcel which can be routed to the native media
747      * player using {@link #invoke(Parcel, Parcel)}. The Parcel
748      * returned has the proper InterfaceToken set. The caller should
749      * not overwrite that token, i.e it can only append data to the
750      * Parcel.
751      *
752      * @return A parcel suitable to hold a request for the native
753      * player.
754      * {@hide}
755      */
756     @UnsupportedAppUsage
newRequest()757     public Parcel newRequest() {
758         Parcel parcel = Parcel.obtain();
759         parcel.writeInterfaceToken(IMEDIA_PLAYER);
760         return parcel;
761     }
762 
763     /**
764      * Invoke a generic method on the native player using opaque
765      * parcels for the request and reply. Both payloads' format is a
766      * convention between the java caller and the native player.
767      * Must be called after setDataSource to make sure a native player
768      * exists. On failure, a RuntimeException is thrown.
769      *
770      * @param request Parcel with the data for the extension. The
771      * caller must use {@link #newRequest()} to get one.
772      *
773      * @param reply Output parcel with the data returned by the
774      * native player.
775      * {@hide}
776      */
777     @UnsupportedAppUsage
invoke(Parcel request, Parcel reply)778     public void invoke(Parcel request, Parcel reply) {
779         int retcode = native_invoke(request, reply);
780         reply.setDataPosition(0);
781         if (retcode != 0) {
782             throw new RuntimeException("failure code: " + retcode);
783         }
784     }
785 
786     /**
787      * Sets the {@link SurfaceHolder} to use for displaying the video
788      * portion of the media.
789      *
790      * Either a surface holder or surface must be set if a display or video sink
791      * is needed.  Not calling this method or {@link #setSurface(Surface)}
792      * when playing back a video will result in only the audio track being played.
793      * A null surface holder or surface will result in only the audio track being
794      * played.
795      *
796      * @param sh the SurfaceHolder to use for video display
797      * @throws IllegalStateException if the internal player engine has not been
798      * initialized or has been released.
799      */
setDisplay(SurfaceHolder sh)800     public void setDisplay(SurfaceHolder sh) {
801         mSurfaceHolder = sh;
802         Surface surface;
803         if (sh != null) {
804             surface = sh.getSurface();
805         } else {
806             surface = null;
807         }
808         _setVideoSurface(surface);
809         updateSurfaceScreenOn();
810     }
811 
812     /**
813      * Sets the {@link Surface} to be used as the sink for the video portion of
814      * the media. This is similar to {@link #setDisplay(SurfaceHolder)}, but
815      * does not support {@link #setScreenOnWhilePlaying(boolean)}.  Setting a
816      * Surface will un-set any Surface or SurfaceHolder that was previously set.
817      * A null surface will result in only the audio track being played.
818      *
819      * If the Surface sends frames to a {@link SurfaceTexture}, the timestamps
820      * returned from {@link SurfaceTexture#getTimestamp()} will have an
821      * unspecified zero point.  These timestamps cannot be directly compared
822      * between different media sources, different instances of the same media
823      * source, or multiple runs of the same program.  The timestamp is normally
824      * monotonically increasing and is unaffected by time-of-day adjustments,
825      * but it is reset when the position is set.
826      *
827      * @param surface The {@link Surface} to be used for the video portion of
828      * the media.
829      * @throws IllegalStateException if the internal player engine has not been
830      * initialized or has been released.
831      */
setSurface(Surface surface)832     public void setSurface(Surface surface) {
833         if (mScreenOnWhilePlaying && surface != null) {
834             Log.w(TAG, "setScreenOnWhilePlaying(true) is ineffective for Surface");
835         }
836         mSurfaceHolder = null;
837         _setVideoSurface(surface);
838         updateSurfaceScreenOn();
839     }
840 
841     /* Do not change these video scaling mode values below without updating
842      * their counterparts in system/window.h! Please do not forget to update
843      * {@link #isVideoScalingModeSupported} when new video scaling modes
844      * are added.
845      */
846     /**
847      * Specifies a video scaling mode. The content is stretched to the
848      * surface rendering area. When the surface has the same aspect ratio
849      * as the content, the aspect ratio of the content is maintained;
850      * otherwise, the aspect ratio of the content is not maintained when video
851      * is being rendered. Unlike {@link #VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING},
852      * there is no content cropping with this video scaling mode.
853      */
854     public static final int VIDEO_SCALING_MODE_SCALE_TO_FIT = 1;
855 
856     /**
857      * Specifies a video scaling mode. The content is scaled, maintaining
858      * its aspect ratio. The whole surface area is always used. When the
859      * aspect ratio of the content is the same as the surface, no content
860      * is cropped; otherwise, content is cropped to fit the surface.
861      */
862     public static final int VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING = 2;
863     /**
864      * Sets video scaling mode. To make the target video scaling mode
865      * effective during playback, this method must be called after
866      * data source is set. If not called, the default video
867      * scaling mode is {@link #VIDEO_SCALING_MODE_SCALE_TO_FIT}.
868      *
869      * <p> The supported video scaling modes are:
870      * <ul>
871      * <li> {@link #VIDEO_SCALING_MODE_SCALE_TO_FIT}
872      * <li> {@link #VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING}
873      * </ul>
874      *
875      * @param mode target video scaling mode. Must be one of the supported
876      * video scaling modes; otherwise, IllegalArgumentException will be thrown.
877      *
878      * @see MediaPlayer#VIDEO_SCALING_MODE_SCALE_TO_FIT
879      * @see MediaPlayer#VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING
880      */
setVideoScalingMode(int mode)881     public void setVideoScalingMode(int mode) {
882         if (!isVideoScalingModeSupported(mode)) {
883             final String msg = "Scaling mode " + mode + " is not supported";
884             throw new IllegalArgumentException(msg);
885         }
886         Parcel request = Parcel.obtain();
887         Parcel reply = Parcel.obtain();
888         try {
889             request.writeInterfaceToken(IMEDIA_PLAYER);
890             request.writeInt(INVOKE_ID_SET_VIDEO_SCALE_MODE);
891             request.writeInt(mode);
892             invoke(request, reply);
893         } finally {
894             request.recycle();
895             reply.recycle();
896         }
897     }
898 
899     /**
900      * Convenience method to create a MediaPlayer for a given Uri.
901      * On success, {@link #prepare()} will already have been called and must not be called again.
902      *
903      * <p>You must call {@link #release()} when you are finished using the created instance. Doing
904      * so frees any resources you have previously acquired.
905      *
906      * <p>Note that since {@link #prepare()} is called automatically in this method,
907      * you cannot change the audio
908      * session ID (see {@link #setAudioSessionId(int)}) or audio attributes
909      * (see {@link #setAudioAttributes(AudioAttributes)} of the new MediaPlayer.</p>
910      *
911      * @param context the Context to use
912      * @param uri the Uri from which to get the datasource
913      * @return a MediaPlayer object, or null if creation failed
914      */
create(Context context, Uri uri)915     public static MediaPlayer create(Context context, Uri uri) {
916         return create (context, uri, null);
917     }
918 
919     /**
920      * Convenience method to create a MediaPlayer for a given Uri.
921      * On success, {@link #prepare()} will already have been called and must not be called again.
922      *
923      * <p>You must call {@link #release()} when you are finished using the created instance. Doing
924      * so frees any resources you have previously acquired.
925      *
926      * <p>Note that since {@link #prepare()} is called automatically in this method,
927      * you cannot change the audio
928      * session ID (see {@link #setAudioSessionId(int)}) or audio attributes
929      * (see {@link #setAudioAttributes(AudioAttributes)} of the new MediaPlayer.</p>
930      *
931      * @param context the Context to use
932      * @param uri the Uri from which to get the datasource
933      * @param holder the SurfaceHolder to use for displaying the video
934      * @return a MediaPlayer object, or null if creation failed
935      */
create(Context context, Uri uri, SurfaceHolder holder)936     public static MediaPlayer create(Context context, Uri uri, SurfaceHolder holder) {
937         return create(context, uri, holder, null, AudioSystem.AUDIO_SESSION_ALLOCATE);
938     }
939 
940     /**
941      * Same factory method as {@link #create(Context, Uri, SurfaceHolder)} but that lets you specify
942      * the audio attributes and session ID to be used by the new MediaPlayer instance.
943      * @param context the Context to use
944      * @param uri the Uri from which to get the datasource
945      * @param holder the SurfaceHolder to use for displaying the video, may be null.
946      * @param audioAttributes the {@link AudioAttributes} to be used by the media player.
947      * @param audioSessionId the audio session ID to be used by the media player,
948      *     see {@link AudioManager#generateAudioSessionId()} to obtain a new session.
949      * @return a MediaPlayer object, or null if creation failed
950      */
create(Context context, Uri uri, SurfaceHolder holder, AudioAttributes audioAttributes, int audioSessionId)951     public static MediaPlayer create(Context context, Uri uri, SurfaceHolder holder,
952             AudioAttributes audioAttributes, int audioSessionId) {
953 
954         try {
955             MediaPlayer mp = new MediaPlayer(context, audioSessionId);
956             final AudioAttributes aa = audioAttributes != null ? audioAttributes :
957                 new AudioAttributes.Builder().build();
958             mp.setAudioAttributes(aa);
959             mp.setDataSource(context, uri);
960             if (holder != null) {
961                 mp.setDisplay(holder);
962             }
963             mp.prepare();
964             return mp;
965         } catch (IOException ex) {
966             Log.d(TAG, "create failed:", ex);
967             // fall through
968         } catch (IllegalArgumentException ex) {
969             Log.d(TAG, "create failed:", ex);
970             // fall through
971         } catch (SecurityException ex) {
972             Log.d(TAG, "create failed:", ex);
973             // fall through
974         }
975 
976         return null;
977     }
978 
979     // Note no convenience method to create a MediaPlayer with SurfaceTexture sink.
980 
981     /**
982      * Convenience method to create a MediaPlayer for a given resource id.
983      * On success, {@link #prepare()} will already have been called and must not be called again.
984      *
985      * <p>You must call {@link #release()} when you are finished using the created instance. Doing
986      * so frees any resources you have previously acquired.
987      *
988      * <p>Note that since {@link #prepare()} is called automatically in this method,
989      * you cannot change the audio
990      * session ID (see {@link #setAudioSessionId(int)}) or audio attributes
991      * (see {@link #setAudioAttributes(AudioAttributes)} of the new MediaPlayer.</p>
992      *
993      * @param context the Context to use
994      * @param resid the raw resource id (<var>R.raw.&lt;something></var>) for
995      *              the resource to use as the datasource
996      * @return a MediaPlayer object, or null if creation failed
997      */
create(Context context, int resid)998     public static MediaPlayer create(Context context, int resid) {
999         return create(context, resid, null, AudioSystem.AUDIO_SESSION_ALLOCATE);
1000     }
1001 
1002     /**
1003      * Same factory method as {@link #create(Context, int)} but that lets you specify the audio
1004      * attributes and session ID to be used by the new MediaPlayer instance.
1005      * @param context the Context to use
1006      * @param resid the raw resource id (<var>R.raw.&lt;something></var>) for
1007      *              the resource to use as the datasource
1008      * @param audioAttributes the {@link AudioAttributes} to be used by the media player.
1009      * @param audioSessionId the audio session ID to be used by the media player,
1010      *     see {@link AudioManager#generateAudioSessionId()} to obtain a new session.
1011      * @return a MediaPlayer object, or null if creation failed
1012      */
create(Context context, int resid, AudioAttributes audioAttributes, int audioSessionId)1013     public static MediaPlayer create(Context context, int resid,
1014             AudioAttributes audioAttributes, int audioSessionId) {
1015         try {
1016             AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
1017             if (afd == null) return null;
1018 
1019             MediaPlayer mp = new MediaPlayer(context, audioSessionId);
1020 
1021             final AudioAttributes aa = audioAttributes != null ? audioAttributes :
1022                 new AudioAttributes.Builder().build();
1023             mp.setAudioAttributes(aa);
1024             mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
1025             afd.close();
1026             mp.prepare();
1027             return mp;
1028         } catch (IOException ex) {
1029             Log.d(TAG, "create failed:", ex);
1030             // fall through
1031         } catch (IllegalArgumentException ex) {
1032             Log.d(TAG, "create failed:", ex);
1033            // fall through
1034         } catch (SecurityException ex) {
1035             Log.d(TAG, "create failed:", ex);
1036             // fall through
1037         }
1038         return null;
1039     }
1040 
1041     /**
1042      * Sets the data source as a content Uri.
1043      *
1044      * @param context the Context to use when resolving the Uri
1045      * @param uri the Content URI of the data you want to play
1046      * @throws IllegalStateException if it is called in an invalid state
1047      */
setDataSource(@onNull Context context, @NonNull Uri uri)1048     public void setDataSource(@NonNull Context context, @NonNull Uri uri)
1049             throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
1050         setDataSource(context, uri, null, null);
1051     }
1052 
1053     /**
1054      * Sets the data source as a content Uri.
1055      *
1056      * To provide cookies for the subsequent HTTP requests, you can install your own default cookie
1057      * handler and use other variants of setDataSource APIs instead. Alternatively, you can use
1058      * this API to pass the cookies as a list of HttpCookie. If the app has not installed
1059      * a CookieHandler already, this API creates a CookieManager and populates its CookieStore with
1060      * the provided cookies. If the app has installed its own handler already, this API requires the
1061      * handler to be of CookieManager type such that the API can update the manager's CookieStore.
1062      *
1063      * <p><strong>Note</strong> that the cross domain redirection is allowed by default,
1064      * but that can be changed with key/value pairs through the headers parameter with
1065      * "android-allow-cross-domain-redirect" as the key and "0" or "1" as the value to
1066      * disallow or allow cross domain redirection.
1067      *
1068      * @param context the Context to use when resolving the Uri
1069      * @param uri the Content URI of the data you want to play
1070      * @param headers the headers to be sent together with the request for the data
1071      *                The headers must not include cookies. Instead, use the cookies param.
1072      * @param cookies the cookies to be sent together with the request
1073      * @throws IllegalArgumentException if cookies are provided and the installed handler is not
1074      *                                  a CookieManager
1075      * @throws IllegalStateException    if it is called in an invalid state
1076      * @throws NullPointerException     if context or uri is null
1077      * @throws IOException              if uri has a file scheme and an I/O error occurs
1078      */
setDataSource(@onNull Context context, @NonNull Uri uri, @Nullable Map<String, String> headers, @Nullable List<HttpCookie> cookies)1079     public void setDataSource(@NonNull Context context, @NonNull Uri uri,
1080             @Nullable Map<String, String> headers, @Nullable List<HttpCookie> cookies)
1081             throws IOException {
1082         if (context == null) {
1083             throw new NullPointerException("context param can not be null.");
1084         }
1085 
1086         if (uri == null) {
1087             throw new NullPointerException("uri param can not be null.");
1088         }
1089 
1090         if (cookies != null) {
1091             CookieHandler cookieHandler = CookieHandler.getDefault();
1092             if (cookieHandler != null && !(cookieHandler instanceof CookieManager)) {
1093                 throw new IllegalArgumentException("The cookie handler has to be of CookieManager "
1094                         + "type when cookies are provided.");
1095             }
1096         }
1097 
1098         // The context and URI usually belong to the calling user. Get a resolver for that user
1099         // and strip out the userId from the URI if present.
1100         final ContentResolver resolver = context.getContentResolver();
1101         final String scheme = uri.getScheme();
1102         final String authority = ContentProvider.getAuthorityWithoutUserId(uri.getAuthority());
1103         if (ContentResolver.SCHEME_FILE.equals(scheme)) {
1104             setDataSource(uri.getPath());
1105             return;
1106         } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)
1107                 && Settings.AUTHORITY.equals(authority)) {
1108             // Try cached ringtone first since the actual provider may not be
1109             // encryption aware, or it may be stored on CE media storage
1110             final int type = RingtoneManager.getDefaultType(uri);
1111             final Uri cacheUri = RingtoneManager.getCacheForType(type, context.getUserId());
1112             final Uri actualUri = RingtoneManager.getActualDefaultRingtoneUri(context, type);
1113             if (attemptDataSource(resolver, cacheUri)) {
1114                 return;
1115             } else if (attemptDataSource(resolver, actualUri)) {
1116                 return;
1117             } else {
1118                 setDataSource(uri.toString(), headers, cookies);
1119             }
1120         } else {
1121             // Try requested Uri locally first, or fallback to media server
1122             if (attemptDataSource(resolver, uri)) {
1123                 return;
1124             } else {
1125                 setDataSource(uri.toString(), headers, cookies);
1126             }
1127         }
1128     }
1129 
1130     /**
1131      * Sets the data source as a content Uri.
1132      *
1133      * <p><strong>Note</strong> that the cross domain redirection is allowed by default,
1134      * but that can be changed with key/value pairs through the headers parameter with
1135      * "android-allow-cross-domain-redirect" as the key and "0" or "1" as the value to
1136      * disallow or allow cross domain redirection.
1137      *
1138      * @param context the Context to use when resolving the Uri
1139      * @param uri the Content URI of the data you want to play
1140      * @param headers the headers to be sent together with the request for the data
1141      * @throws IllegalStateException if it is called in an invalid state
1142      */
setDataSource(@onNull Context context, @NonNull Uri uri, @Nullable Map<String, String> headers)1143     public void setDataSource(@NonNull Context context, @NonNull Uri uri,
1144             @Nullable Map<String, String> headers)
1145             throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
1146         setDataSource(context, uri, headers, null);
1147     }
1148 
attemptDataSource(ContentResolver resolver, Uri uri)1149     private boolean attemptDataSource(ContentResolver resolver, Uri uri) {
1150         boolean optimize = SystemProperties.getBoolean("fuse.sys.transcode_player_optimize",
1151                 false);
1152         Bundle opts = new Bundle();
1153         opts.putBoolean("android.provider.extra.ACCEPT_ORIGINAL_MEDIA_FORMAT", true);
1154         try (AssetFileDescriptor afd = optimize
1155                 ? resolver.openTypedAssetFileDescriptor(uri, "*/*", opts)
1156                 : resolver.openAssetFileDescriptor(uri, "r")) {
1157             setDataSource(afd);
1158             return true;
1159         } catch (NullPointerException | SecurityException | IOException ex) {
1160             Log.w(TAG, "Error setting data source via ContentResolver", ex);
1161             return false;
1162         }
1163     }
1164 
1165     /**
1166      * Sets the data source (file-path or http/rtsp URL) to use.
1167      *
1168      * <p>When <code>path</code> refers to a local file, the file may actually be opened by a
1169      * process other than the calling application.  This implies that the pathname
1170      * should be an absolute path (as any other process runs with unspecified current working
1171      * directory), and that the pathname should reference a world-readable file.
1172      * As an alternative, the application could first open the file for reading,
1173      * and then use the file descriptor form {@link #setDataSource(FileDescriptor)}.
1174      *
1175      * @param path the path of the file, or the http/rtsp URL of the stream you want to play
1176      * @throws IllegalStateException if it is called in an invalid state
1177      */
setDataSource(String path)1178     public void setDataSource(String path)
1179             throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
1180         setDataSource(path, null, null);
1181     }
1182 
1183     /**
1184      * Sets the data source (file-path or http/rtsp URL) to use.
1185      *
1186      * @param path the path of the file, or the http/rtsp URL of the stream you want to play
1187      * @param headers the headers associated with the http request for the stream you want to play
1188      * @throws IllegalStateException if it is called in an invalid state
1189      * @hide pending API council
1190      */
1191     @UnsupportedAppUsage
setDataSource(String path, Map<String, String> headers)1192     public void setDataSource(String path, Map<String, String> headers)
1193             throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
1194         setDataSource(path, headers, null);
1195     }
1196 
1197     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
setDataSource(String path, Map<String, String> headers, List<HttpCookie> cookies)1198     private void setDataSource(String path, Map<String, String> headers, List<HttpCookie> cookies)
1199             throws IOException, IllegalArgumentException, SecurityException, IllegalStateException
1200     {
1201         String[] keys = null;
1202         String[] values = null;
1203 
1204         if (headers != null) {
1205             keys = new String[headers.size()];
1206             values = new String[headers.size()];
1207 
1208             int i = 0;
1209             for (Map.Entry<String, String> entry: headers.entrySet()) {
1210                 keys[i] = entry.getKey();
1211                 values[i] = entry.getValue();
1212                 ++i;
1213             }
1214         }
1215         setDataSource(path, keys, values, cookies);
1216     }
1217 
1218     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
setDataSource(String path, String[] keys, String[] values, List<HttpCookie> cookies)1219     private void setDataSource(String path, String[] keys, String[] values,
1220             List<HttpCookie> cookies)
1221             throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
1222         final Uri uri = Uri.parse(path);
1223         final String scheme = uri.getScheme();
1224         if ("file".equals(scheme)) {
1225             path = uri.getPath();
1226         } else if (scheme != null) {
1227             // handle non-file sources
1228             nativeSetDataSource(
1229                 MediaHTTPService.createHttpServiceBinderIfNecessary(path, cookies),
1230                 path,
1231                 keys,
1232                 values);
1233             return;
1234         }
1235 
1236         final File file = new File(path);
1237         try (FileInputStream is = new FileInputStream(file)) {
1238             setDataSource(is.getFD());
1239         }
1240     }
1241 
nativeSetDataSource( IBinder httpServiceBinder, String path, String[] keys, String[] values)1242     private native void nativeSetDataSource(
1243         IBinder httpServiceBinder, String path, String[] keys, String[] values)
1244         throws IOException, IllegalArgumentException, SecurityException, IllegalStateException;
1245 
1246     /**
1247      * Sets the data source (AssetFileDescriptor) to use. It is the caller's
1248      * responsibility to close the file descriptor. It is safe to do so as soon
1249      * as this call returns.
1250      *
1251      * @param afd the AssetFileDescriptor for the file you want to play
1252      * @throws IllegalStateException if it is called in an invalid state
1253      * @throws IllegalArgumentException if afd is not a valid AssetFileDescriptor
1254      * @throws IOException if afd can not be read
1255      */
setDataSource(@onNull AssetFileDescriptor afd)1256     public void setDataSource(@NonNull AssetFileDescriptor afd)
1257             throws IOException, IllegalArgumentException, IllegalStateException {
1258         Preconditions.checkNotNull(afd);
1259         // Note: using getDeclaredLength so that our behavior is the same
1260         // as previous versions when the content provider is returning
1261         // a full file.
1262         if (afd.getDeclaredLength() < 0) {
1263             setDataSource(afd.getFileDescriptor());
1264         } else {
1265             setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
1266         }
1267     }
1268 
1269     /**
1270      * Sets the data source (FileDescriptor) to use. It is the caller's responsibility
1271      * to close the file descriptor. It is safe to do so as soon as this call returns.
1272      *
1273      * @param fd the FileDescriptor for the file you want to play
1274      * @throws IllegalStateException if it is called in an invalid state
1275      * @throws IllegalArgumentException if fd is not a valid FileDescriptor
1276      * @throws IOException if fd can not be read
1277      */
setDataSource(FileDescriptor fd)1278     public void setDataSource(FileDescriptor fd)
1279             throws IOException, IllegalArgumentException, IllegalStateException {
1280         // intentionally less than LONG_MAX
1281         setDataSource(fd, 0, 0x7ffffffffffffffL);
1282     }
1283 
1284     /**
1285      * Sets the data source (FileDescriptor) to use.  The FileDescriptor must be
1286      * seekable (N.B. a LocalSocket is not seekable). It is the caller's responsibility
1287      * to close the file descriptor. It is safe to do so as soon as this call returns.
1288      *
1289      * @param fd the FileDescriptor for the file you want to play
1290      * @param offset the offset into the file where the data to be played starts, in bytes
1291      * @param length the length in bytes of the data to be played
1292      * @throws IllegalStateException if it is called in an invalid state
1293      * @throws IllegalArgumentException if fd is not a valid FileDescriptor
1294      * @throws IOException if fd can not be read
1295      */
setDataSource(FileDescriptor fd, long offset, long length)1296     public void setDataSource(FileDescriptor fd, long offset, long length)
1297             throws IOException, IllegalArgumentException, IllegalStateException {
1298         try (ParcelFileDescriptor modernFd = FileUtils.convertToModernFd(fd)) {
1299             if (modernFd == null) {
1300                 _setDataSource(fd, offset, length);
1301             } else {
1302                 _setDataSource(modernFd.getFileDescriptor(), offset, length);
1303             }
1304         } catch (IOException e) {
1305             Log.w(TAG, "Ignoring IO error while setting data source", e);
1306         }
1307     }
1308 
_setDataSource(FileDescriptor fd, long offset, long length)1309     private native void _setDataSource(FileDescriptor fd, long offset, long length)
1310             throws IOException, IllegalArgumentException, IllegalStateException;
1311 
1312     /**
1313      * Sets the data source (MediaDataSource) to use.
1314      *
1315      * @param dataSource the MediaDataSource for the media you want to play
1316      * @throws IllegalStateException if it is called in an invalid state
1317      * @throws IllegalArgumentException if dataSource is not a valid MediaDataSource
1318      */
setDataSource(MediaDataSource dataSource)1319     public void setDataSource(MediaDataSource dataSource)
1320             throws IllegalArgumentException, IllegalStateException {
1321         _setDataSource(dataSource);
1322     }
1323 
_setDataSource(MediaDataSource dataSource)1324     private native void _setDataSource(MediaDataSource dataSource)
1325           throws IllegalArgumentException, IllegalStateException;
1326 
1327     /**
1328      * Prepares the player for playback, synchronously.
1329      *
1330      * After setting the datasource and the display surface, you need to either
1331      * call prepare() or prepareAsync(). For files, it is OK to call prepare(),
1332      * which blocks until MediaPlayer is ready for playback.
1333      *
1334      * @throws IllegalStateException if it is called in an invalid state
1335      */
prepare()1336     public void prepare() throws IOException, IllegalStateException {
1337         Parcel piidParcel = createPlayerIIdParcel();
1338         try {
1339             int retCode = _prepare(piidParcel);
1340             if (retCode != 0) {
1341                 Log.w(TAG, "prepare(): could not set piid " + mPlayerIId);
1342             }
1343         } finally {
1344             piidParcel.recycle();
1345         }
1346         scanInternalSubtitleTracks();
1347 
1348         // DrmInfo, if any, has been resolved by now.
1349         synchronized (mDrmLock) {
1350             mDrmInfoResolved = true;
1351         }
1352 
1353     }
1354 
1355     /** Returns the result of sending the {@code piidParcel} to the MediaPlayerService. */
_prepare(Parcel piidParcel)1356     private native int _prepare(Parcel piidParcel) throws IOException, IllegalStateException;
1357 
1358     /**
1359      * Prepares the player for playback, asynchronously.
1360      *
1361      * After setting the datasource and the display surface, you need to either
1362      * call prepare() or prepareAsync(). For streams, you should call prepareAsync(),
1363      * which returns immediately, rather than blocking until enough data has been
1364      * buffered.
1365      *
1366      * @throws IllegalStateException if it is called in an invalid state
1367      */
prepareAsync()1368     public void prepareAsync() throws IllegalStateException {
1369         Parcel piidParcel = createPlayerIIdParcel();
1370         try {
1371             int retCode = _prepareAsync(piidParcel);
1372             if (retCode != 0) {
1373                 Log.w(TAG, "prepareAsync(): could not set piid " + mPlayerIId);
1374             }
1375         } finally {
1376             piidParcel.recycle();
1377         }
1378     }
1379 
1380     /** Returns the result of sending the {@code piidParcel} to the MediaPlayerService. */
_prepareAsync(Parcel piidParcel)1381     private native int _prepareAsync(Parcel piidParcel) throws IllegalStateException;
1382 
1383     /**
1384      * Starts or resumes playback. If playback had previously been paused,
1385      * playback will continue from where it was paused. If playback had
1386      * been stopped, or never started before, playback will start at the
1387      * beginning.
1388      *
1389      * @throws IllegalStateException if it is called in an invalid state
1390      */
start()1391     public void start() throws IllegalStateException {
1392         //FIXME use lambda to pass startImpl to superclass
1393         final int delay = getStartDelayMs();
1394         if (delay == 0) {
1395             startImpl();
1396         } else {
1397             new Thread() {
1398                 public void run() {
1399                     try {
1400                         Thread.sleep(delay);
1401                     } catch (InterruptedException e) {
1402                         e.printStackTrace();
1403                     }
1404                     baseSetStartDelayMs(0);
1405                     try {
1406                         startImpl();
1407                     } catch (IllegalStateException e) {
1408                         // fail silently for a state exception when it is happening after
1409                         // a delayed start, as the player state could have changed between the
1410                         // call to start() and the execution of startImpl()
1411                     }
1412                 }
1413             }.start();
1414         }
1415     }
1416 
startImpl()1417     private void startImpl() {
1418         baseStart(new int[0]); // unknown device at this point
1419         stayAwake(true);
1420         tryToEnableNativeRoutingCallback();
1421         _start();
1422     }
1423 
_start()1424     private native void _start() throws IllegalStateException;
1425 
1426 
getAudioStreamType()1427     private int getAudioStreamType() {
1428         if (mStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) {
1429             mStreamType = _getAudioStreamType();
1430         }
1431         return mStreamType;
1432     }
1433 
_getAudioStreamType()1434     private native int _getAudioStreamType() throws IllegalStateException;
1435 
1436     /**
1437      * Stops playback after playback has been started or paused.
1438      *
1439      * @throws IllegalStateException if the internal player engine has not been
1440      * initialized.
1441      */
stop()1442     public void stop() throws IllegalStateException {
1443         stayAwake(false);
1444         _stop();
1445         baseStop();
1446         tryToDisableNativeRoutingCallback();
1447     }
1448 
_stop()1449     private native void _stop() throws IllegalStateException;
1450 
1451     /**
1452      * Pauses playback. Call start() to resume.
1453      *
1454      * @throws IllegalStateException if the internal player engine has not been
1455      * initialized.
1456      */
pause()1457     public void pause() throws IllegalStateException {
1458         stayAwake(false);
1459         _pause();
1460         basePause();
1461     }
1462 
_pause()1463     private native void _pause() throws IllegalStateException;
1464 
1465     @Override
playerStart()1466     void playerStart() {
1467         start();
1468     }
1469 
1470     @Override
playerPause()1471     void playerPause() {
1472         pause();
1473     }
1474 
1475     @Override
playerStop()1476     void playerStop() {
1477         stop();
1478     }
1479 
1480     @Override
playerApplyVolumeShaper( @onNull VolumeShaper.Configuration configuration, @NonNull VolumeShaper.Operation operation)1481     /* package */ int playerApplyVolumeShaper(
1482             @NonNull VolumeShaper.Configuration configuration,
1483             @NonNull VolumeShaper.Operation operation) {
1484         return native_applyVolumeShaper(configuration, operation);
1485     }
1486 
1487     @Override
playerGetVolumeShaperState(int id)1488     /* package */ @Nullable VolumeShaper.State playerGetVolumeShaperState(int id) {
1489         return native_getVolumeShaperState(id);
1490     }
1491 
1492     @Override
createVolumeShaper( @onNull VolumeShaper.Configuration configuration)1493     public @NonNull VolumeShaper createVolumeShaper(
1494             @NonNull VolumeShaper.Configuration configuration) {
1495         return new VolumeShaper(configuration, this);
1496     }
1497 
native_applyVolumeShaper( @onNull VolumeShaper.Configuration configuration, @NonNull VolumeShaper.Operation operation)1498     private native int native_applyVolumeShaper(
1499             @NonNull VolumeShaper.Configuration configuration,
1500             @NonNull VolumeShaper.Operation operation);
1501 
native_getVolumeShaperState(int id)1502     private native @Nullable VolumeShaper.State native_getVolumeShaperState(int id);
1503 
1504     //--------------------------------------------------------------------------
1505     // Explicit Routing
1506     //--------------------
1507     private AudioDeviceInfo mPreferredDevice = null;
1508 
1509     /**
1510      * Specifies an audio device (via an {@link AudioDeviceInfo} object) to route
1511      * the output from this MediaPlayer.
1512      * @param deviceInfo The {@link AudioDeviceInfo} specifying the audio sink or source.
1513      *  If deviceInfo is null, default routing is restored.
1514      * @return true if succesful, false if the specified {@link AudioDeviceInfo} is non-null and
1515      * does not correspond to a valid audio device.
1516      */
1517     @Override
setPreferredDevice(AudioDeviceInfo deviceInfo)1518     public boolean setPreferredDevice(AudioDeviceInfo deviceInfo) {
1519         if (deviceInfo != null && !deviceInfo.isSink()) {
1520             return false;
1521         }
1522         int preferredDeviceId = deviceInfo != null ? deviceInfo.getId() : 0;
1523         boolean status = native_setOutputDevice(preferredDeviceId);
1524         if (status == true) {
1525             synchronized (this) {
1526                 mPreferredDevice = deviceInfo;
1527             }
1528         }
1529         return status;
1530     }
1531 
1532     /**
1533      * Returns the selected output specified by {@link #setPreferredDevice}. Note that this
1534      * is not guaranteed to correspond to the actual device being used for playback.
1535      */
1536     @Override
getPreferredDevice()1537     public AudioDeviceInfo getPreferredDevice() {
1538         synchronized (this) {
1539             return mPreferredDevice;
1540         }
1541     }
1542 
1543     /**
1544      * Internal API of getRoutedDevices(). We should not call flag APIs internally.
1545      */
getRoutedDevicesInternal()1546     private @NonNull List<AudioDeviceInfo> getRoutedDevicesInternal() {
1547         List<AudioDeviceInfo> audioDeviceInfos = new ArrayList<AudioDeviceInfo>();
1548         final int[] deviceIds = native_getRoutedDeviceIds();
1549         if (deviceIds == null || deviceIds.length == 0) {
1550             return audioDeviceInfos;
1551         }
1552 
1553         for (int i = 0; i < deviceIds.length; i++) {
1554             AudioDeviceInfo audioDeviceInfo = AudioManager.getDeviceForPortId(deviceIds[i],
1555                     AudioManager.GET_DEVICES_OUTPUTS);
1556             if (audioDeviceInfo != null) {
1557                 audioDeviceInfos.add(audioDeviceInfo);
1558             }
1559         }
1560         return audioDeviceInfos;
1561     }
1562 
1563     /**
1564      * Returns an {@link AudioDeviceInfo} identifying the current routing of this MediaPlayer
1565      * Note: The query is only valid if the MediaPlayer is currently playing.
1566      * If the player is not playing, the returned device can be null or correspond to previously
1567      * selected device when the player was last active.
1568      * Audio may play on multiple devices simultaneously (e.g. an alarm playing on headphones and
1569      * speaker on a phone), so prefer using {@link #getRoutedDevices}.
1570      */
1571     @Override
getRoutedDevice()1572     public AudioDeviceInfo getRoutedDevice() {
1573         final List<AudioDeviceInfo> audioDeviceInfos = getRoutedDevicesInternal();
1574         if (audioDeviceInfos.isEmpty()) {
1575             return null;
1576         }
1577         return audioDeviceInfos.get(0);
1578     }
1579 
1580     /**
1581      * Returns a List of {@link AudioDeviceInfo} identifying the current routing of this
1582      * MediaPlayer.
1583      * Note: The query is only valid if the MediaPlayer is currently playing.
1584      * If the player is not playing, the returned devices can be empty or correspond to previously
1585      * selected devices when the player was last active.
1586      */
1587     @Override
1588     @FlaggedApi(FLAG_ROUTED_DEVICE_IDS)
getRoutedDevices()1589     public @NonNull List<AudioDeviceInfo> getRoutedDevices() {
1590         return getRoutedDevicesInternal();
1591     }
1592 
1593     /**
1594      * Sends device list change notification to all listeners.
1595      */
broadcastRoutingChange()1596     private void broadcastRoutingChange() {
1597         AudioManager.resetAudioPortGeneration();
1598         synchronized (mRoutingChangeListeners) {
1599             // Prevent the case where an event is triggered by registering a routing change
1600             // listener via the media player.
1601             if (mEnableSelfRoutingMonitor) {
1602                 baseUpdateDeviceIds(getRoutedDevicesInternal());
1603             }
1604             for (NativeRoutingEventHandlerDelegate delegate
1605                     : mRoutingChangeListeners.values()) {
1606                 delegate.notifyClient();
1607             }
1608         }
1609     }
1610 
1611     /**
1612      * Call BEFORE adding a routing callback handler and when enabling self routing listener
1613      * @return returns true for success, false otherwise.
1614      */
1615     @GuardedBy("mRoutingChangeListeners")
testEnableNativeRoutingCallbacksLocked()1616     private boolean testEnableNativeRoutingCallbacksLocked() {
1617         if (mRoutingChangeListeners.size() == 0 && !mEnableSelfRoutingMonitor) {
1618             try {
1619                 native_enableDeviceCallback(true);
1620                 return true;
1621             } catch (IllegalStateException e) {
1622                 if (Log.isLoggable(TAG, Log.DEBUG)) {
1623                     Log.d(TAG, "testEnableNativeRoutingCallbacks failed", e);
1624                 }
1625             }
1626         }
1627         return false;
1628     }
1629 
tryToEnableNativeRoutingCallback()1630     private void  tryToEnableNativeRoutingCallback() {
1631         synchronized (mRoutingChangeListeners) {
1632             if (!mEnableSelfRoutingMonitor) {
1633                 mEnableSelfRoutingMonitor = testEnableNativeRoutingCallbacksLocked();
1634             }
1635         }
1636     }
1637 
tryToDisableNativeRoutingCallback()1638     private void tryToDisableNativeRoutingCallback() {
1639         synchronized (mRoutingChangeListeners) {
1640             if (mEnableSelfRoutingMonitor) {
1641                 mEnableSelfRoutingMonitor = false;
1642                 testDisableNativeRoutingCallbacksLocked();
1643             }
1644         }
1645     }
1646 
1647     /*
1648      * Call AFTER removing a routing callback handler and when disabling self routing listener
1649      */
1650     @GuardedBy("mRoutingChangeListeners")
testDisableNativeRoutingCallbacksLocked()1651     private void testDisableNativeRoutingCallbacksLocked() {
1652         if (mRoutingChangeListeners.size() == 0 && !mEnableSelfRoutingMonitor) {
1653             try {
1654                 native_enableDeviceCallback(false);
1655             } catch (IllegalStateException e) {
1656                 // Fail silently as media player state could have changed in between stop
1657                 // and disabling routing callback
1658             }
1659         }
1660     }
1661 
1662     /**
1663      * The list of AudioRouting.OnRoutingChangedListener interfaces added (with
1664      * {@link #addOnRoutingChangedListener(android.media.AudioRouting.OnRoutingChangedListener, Handler)}
1665      * by an app to receive (re)routing notifications.
1666      */
1667     @GuardedBy("mRoutingChangeListeners")
1668     private ArrayMap<AudioRouting.OnRoutingChangedListener,
1669             NativeRoutingEventHandlerDelegate> mRoutingChangeListeners = new ArrayMap<>();
1670 
1671     @GuardedBy("mRoutingChangeListeners")
1672     private boolean mEnableSelfRoutingMonitor;
1673 
1674     /**
1675      * Adds an {@link AudioRouting.OnRoutingChangedListener} to receive notifications of routing
1676      * changes on this MediaPlayer.
1677      * @param listener The {@link AudioRouting.OnRoutingChangedListener} interface to receive
1678      * notifications of rerouting events.
1679      * @param handler  Specifies the {@link Handler} object for the thread on which to execute
1680      * the callback. If <code>null</code>, the handler on the main looper will be used.
1681      */
1682     @Override
addOnRoutingChangedListener(AudioRouting.OnRoutingChangedListener listener, Handler handler)1683     public void addOnRoutingChangedListener(AudioRouting.OnRoutingChangedListener listener,
1684             Handler handler) {
1685         synchronized (mRoutingChangeListeners) {
1686             if (listener != null && !mRoutingChangeListeners.containsKey(listener)) {
1687                 mEnableSelfRoutingMonitor = testEnableNativeRoutingCallbacksLocked();
1688                 mRoutingChangeListeners.put(
1689                         listener, new NativeRoutingEventHandlerDelegate(this, listener,
1690                                 handler != null ? handler : mEventHandler));
1691             }
1692         }
1693     }
1694 
1695     /**
1696      * Removes an {@link AudioRouting.OnRoutingChangedListener} which has been previously added
1697      * to receive rerouting notifications.
1698      * @param listener The previously added {@link AudioRouting.OnRoutingChangedListener} interface
1699      * to remove.
1700      */
1701     @Override
removeOnRoutingChangedListener(AudioRouting.OnRoutingChangedListener listener)1702     public void removeOnRoutingChangedListener(AudioRouting.OnRoutingChangedListener listener) {
1703         synchronized (mRoutingChangeListeners) {
1704             if (mRoutingChangeListeners.containsKey(listener)) {
1705                 mRoutingChangeListeners.remove(listener);
1706             }
1707             testDisableNativeRoutingCallbacksLocked();
1708         }
1709     }
1710 
native_setOutputDevice(int deviceId)1711     private native final boolean native_setOutputDevice(int deviceId);
native_getRoutedDeviceIds()1712     private native int[] native_getRoutedDeviceIds();
native_enableDeviceCallback(boolean enabled)1713     private native final void native_enableDeviceCallback(boolean enabled);
1714 
1715     /**
1716      * Set the low-level power management behavior for this MediaPlayer.  This
1717      * can be used when the MediaPlayer is not playing through a SurfaceHolder
1718      * set with {@link #setDisplay(SurfaceHolder)} and thus can use the
1719      * high-level {@link #setScreenOnWhilePlaying(boolean)} feature.
1720      *
1721      * <p>This function has the MediaPlayer access the low-level power manager
1722      * service to control the device's power usage while playing is occurring.
1723      * The parameter is a combination of {@link android.os.PowerManager} wake flags.
1724      * Use of this method requires {@link android.Manifest.permission#WAKE_LOCK}
1725      * permission.
1726      * By default, no attempt is made to keep the device awake during playback.
1727      *
1728      * @param context the Context to use
1729      * @param mode    the power/wake mode to set
1730      * @see android.os.PowerManager
1731      */
setWakeMode(Context context, int mode)1732     public void setWakeMode(Context context, int mode) {
1733         boolean washeld = false;
1734 
1735         /* Disable persistant wakelocks in media player based on property */
1736         if (SystemProperties.getBoolean("audio.offload.ignore_setawake", false) == true) {
1737             Log.w(TAG, "IGNORING setWakeMode " + mode);
1738             return;
1739         }
1740 
1741         if (mWakeLock != null) {
1742             if (mWakeLock.isHeld()) {
1743                 washeld = true;
1744                 mWakeLock.release();
1745             }
1746             mWakeLock = null;
1747         }
1748 
1749         PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
1750         mWakeLock = pm.newWakeLock(mode|PowerManager.ON_AFTER_RELEASE, MediaPlayer.class.getName());
1751         mWakeLock.setReferenceCounted(false);
1752         if (washeld) {
1753             mWakeLock.acquire();
1754         }
1755     }
1756 
1757     /**
1758      * Control whether we should use the attached SurfaceHolder to keep the
1759      * screen on while video playback is occurring.  This is the preferred
1760      * method over {@link #setWakeMode} where possible, since it doesn't
1761      * require that the application have permission for low-level wake lock
1762      * access.
1763      *
1764      * @param screenOn Supply true to keep the screen on, false to allow it
1765      * to turn off.
1766      */
setScreenOnWhilePlaying(boolean screenOn)1767     public void setScreenOnWhilePlaying(boolean screenOn) {
1768         if (mScreenOnWhilePlaying != screenOn) {
1769             if (screenOn && mSurfaceHolder == null) {
1770                 Log.w(TAG, "setScreenOnWhilePlaying(true) is ineffective without a SurfaceHolder");
1771             }
1772             mScreenOnWhilePlaying = screenOn;
1773             updateSurfaceScreenOn();
1774         }
1775     }
1776 
stayAwake(boolean awake)1777     private void stayAwake(boolean awake) {
1778         if (mWakeLock != null) {
1779             if (awake && !mWakeLock.isHeld()) {
1780                 mWakeLock.acquire();
1781             } else if (!awake && mWakeLock.isHeld()) {
1782                 mWakeLock.release();
1783             }
1784         }
1785         mStayAwake = awake;
1786         updateSurfaceScreenOn();
1787     }
1788 
updateSurfaceScreenOn()1789     private void updateSurfaceScreenOn() {
1790         if (mSurfaceHolder != null) {
1791             mSurfaceHolder.setKeepScreenOn(mScreenOnWhilePlaying && mStayAwake);
1792         }
1793     }
1794 
1795     /**
1796      * Returns the width of the video.
1797      *
1798      * @return the width of the video, or 0 if there is no video,
1799      * no display surface was set, or the width has not been determined
1800      * yet. The OnVideoSizeChangedListener can be registered via
1801      * {@link #setOnVideoSizeChangedListener(OnVideoSizeChangedListener)}
1802      * to provide a notification when the width is available.
1803      */
getVideoWidth()1804     public native int getVideoWidth();
1805 
1806     /**
1807      * Returns the height of the video.
1808      *
1809      * @return the height of the video, or 0 if there is no video,
1810      * no display surface was set, or the height has not been determined
1811      * yet. The OnVideoSizeChangedListener can be registered via
1812      * {@link #setOnVideoSizeChangedListener(OnVideoSizeChangedListener)}
1813      * to provide a notification when the height is available.
1814      */
getVideoHeight()1815     public native int getVideoHeight();
1816 
1817     /**
1818      * Return Metrics data about the current player.
1819      *
1820      * @return a {@link PersistableBundle} containing the set of attributes and values
1821      * available for the media being handled by this instance of MediaPlayer
1822      * The attributes are descibed in {@link MetricsConstants}.
1823      *
1824      *  Additional vendor-specific fields may also be present in
1825      *  the return value.
1826      */
getMetrics()1827     public PersistableBundle getMetrics() {
1828         PersistableBundle bundle = native_getMetrics();
1829         return bundle;
1830     }
1831 
native_getMetrics()1832     private native PersistableBundle native_getMetrics();
1833 
1834     /**
1835      * Checks whether the MediaPlayer is playing.
1836      *
1837      * @return true if currently playing, false otherwise
1838      * @throws IllegalStateException if the internal player engine has not been
1839      * initialized or has been released.
1840      */
isPlaying()1841     public native boolean isPlaying();
1842 
1843     /**
1844      * Change playback speed of audio by resampling the audio.
1845      * <p>
1846      * Specifies resampling as audio mode for variable rate playback, i.e.,
1847      * resample the waveform based on the requested playback rate to get
1848      * a new waveform, and play back the new waveform at the original sampling
1849      * frequency.
1850      * When rate is larger than 1.0, pitch becomes higher.
1851      * When rate is smaller than 1.0, pitch becomes lower.
1852      *
1853      * @hide
1854      */
1855     public static final int PLAYBACK_RATE_AUDIO_MODE_RESAMPLE = 2;
1856 
1857     /**
1858      * Change playback speed of audio without changing its pitch.
1859      * <p>
1860      * Specifies time stretching as audio mode for variable rate playback.
1861      * Time stretching changes the duration of the audio samples without
1862      * affecting its pitch.
1863      * <p>
1864      * This mode is only supported for a limited range of playback speed factors,
1865      * e.g. between 1/2x and 2x.
1866      *
1867      * @hide
1868      */
1869     public static final int PLAYBACK_RATE_AUDIO_MODE_STRETCH = 1;
1870 
1871     /**
1872      * Change playback speed of audio without changing its pitch, and
1873      * possibly mute audio if time stretching is not supported for the playback
1874      * speed.
1875      * <p>
1876      * Try to keep audio pitch when changing the playback rate, but allow the
1877      * system to determine how to change audio playback if the rate is out
1878      * of range.
1879      *
1880      * @hide
1881      */
1882     public static final int PLAYBACK_RATE_AUDIO_MODE_DEFAULT = 0;
1883 
1884     /** @hide */
1885     @IntDef(
1886         value = {
1887             PLAYBACK_RATE_AUDIO_MODE_DEFAULT,
1888             PLAYBACK_RATE_AUDIO_MODE_STRETCH,
1889             PLAYBACK_RATE_AUDIO_MODE_RESAMPLE,
1890     })
1891     @Retention(RetentionPolicy.SOURCE)
1892     public @interface PlaybackRateAudioMode {}
1893 
1894     /**
1895      * Sets playback rate and audio mode.
1896      *
1897      * @param rate the ratio between desired playback rate and normal one.
1898      * @param audioMode audio playback mode. Must be one of the supported
1899      * audio modes.
1900      *
1901      * @throws IllegalStateException if the internal player engine has not been
1902      * initialized.
1903      * @throws IllegalArgumentException if audioMode is not supported.
1904      *
1905      * @hide
1906      */
1907     @NonNull
easyPlaybackParams(float rate, @PlaybackRateAudioMode int audioMode)1908     public PlaybackParams easyPlaybackParams(float rate, @PlaybackRateAudioMode int audioMode) {
1909         PlaybackParams params = new PlaybackParams();
1910         params.allowDefaults();
1911         switch (audioMode) {
1912         case PLAYBACK_RATE_AUDIO_MODE_DEFAULT:
1913             params.setSpeed(rate).setPitch(1.0f);
1914             break;
1915         case PLAYBACK_RATE_AUDIO_MODE_STRETCH:
1916             params.setSpeed(rate).setPitch(1.0f)
1917                     .setAudioFallbackMode(params.AUDIO_FALLBACK_MODE_FAIL);
1918             break;
1919         case PLAYBACK_RATE_AUDIO_MODE_RESAMPLE:
1920             params.setSpeed(rate).setPitch(rate);
1921             break;
1922         default:
1923             final String msg = "Audio playback mode " + audioMode + " is not supported";
1924             throw new IllegalArgumentException(msg);
1925         }
1926         return params;
1927     }
1928 
1929     /**
1930      * Sets playback rate using {@link PlaybackParams}. The object sets its internal
1931      * PlaybackParams to the input, except that the object remembers previous speed
1932      * when input speed is zero. This allows the object to resume at previous speed
1933      * when start() is called. Calling it before the object is prepared does not change
1934      * the object state. After the object is prepared, calling it with zero speed is
1935      * equivalent to calling pause(). After the object is prepared, calling it with
1936      * non-zero speed is equivalent to calling start().
1937      *
1938      * @param params the playback params.
1939      *
1940      * @throws IllegalStateException if the internal player engine has not been
1941      * initialized or has been released.
1942      * @throws IllegalArgumentException if params is not supported.
1943      */
setPlaybackParams(@onNull PlaybackParams params)1944     public native void setPlaybackParams(@NonNull PlaybackParams params);
1945 
1946     /**
1947      * Gets the playback params, containing the current playback rate.
1948      *
1949      * @return the playback params.
1950      * @throws IllegalStateException if the internal player engine has not been
1951      * initialized.
1952      */
1953     @NonNull
getPlaybackParams()1954     public native PlaybackParams getPlaybackParams();
1955 
1956     /**
1957      * Sets A/V sync mode.
1958      *
1959      * @param params the A/V sync params to apply
1960      *
1961      * @throws IllegalStateException if the internal player engine has not been
1962      * initialized.
1963      * @throws IllegalArgumentException if params are not supported.
1964      */
setSyncParams(@onNull SyncParams params)1965     public native void setSyncParams(@NonNull SyncParams params);
1966 
1967     /**
1968      * Gets the A/V sync mode.
1969      *
1970      * @return the A/V sync params
1971      *
1972      * @throws IllegalStateException if the internal player engine has not been
1973      * initialized.
1974      */
1975     @NonNull
getSyncParams()1976     public native SyncParams getSyncParams();
1977 
1978     /**
1979      * Seek modes used in method seekTo(long, int) to move media position
1980      * to a specified location.
1981      *
1982      * Do not change these mode values without updating their counterparts
1983      * in include/media/IMediaSource.h!
1984      */
1985     /**
1986      * This mode is used with {@link #seekTo(long, int)} to move media position to
1987      * a sync (or key) frame associated with a data source that is located
1988      * right before or at the given time.
1989      *
1990      * @see #seekTo(long, int)
1991      */
1992     public static final int SEEK_PREVIOUS_SYNC    = 0x00;
1993     /**
1994      * This mode is used with {@link #seekTo(long, int)} to move media position to
1995      * a sync (or key) frame associated with a data source that is located
1996      * right after or at the given time.
1997      *
1998      * @see #seekTo(long, int)
1999      */
2000     public static final int SEEK_NEXT_SYNC        = 0x01;
2001     /**
2002      * This mode is used with {@link #seekTo(long, int)} to move media position to
2003      * a sync (or key) frame associated with a data source that is located
2004      * closest to (in time) or at the given time.
2005      *
2006      * @see #seekTo(long, int)
2007      */
2008     public static final int SEEK_CLOSEST_SYNC     = 0x02;
2009     /**
2010      * This mode is used with {@link #seekTo(long, int)} to move media position to
2011      * a frame (not necessarily a key frame) associated with a data source that
2012      * is located closest to or at the given time.
2013      *
2014      * @see #seekTo(long, int)
2015      */
2016     public static final int SEEK_CLOSEST          = 0x03;
2017 
2018     /** @hide */
2019     @IntDef(
2020         value = {
2021             SEEK_PREVIOUS_SYNC,
2022             SEEK_NEXT_SYNC,
2023             SEEK_CLOSEST_SYNC,
2024             SEEK_CLOSEST,
2025     })
2026     @Retention(RetentionPolicy.SOURCE)
2027     public @interface SeekMode {}
2028 
_seekTo(long msec, int mode)2029     private native final void _seekTo(long msec, int mode);
2030 
2031     /**
2032      * Moves the media to specified time position by considering the given mode.
2033      * <p>
2034      * When seekTo is finished, the user will be notified via OnSeekComplete supplied by the user.
2035      * There is at most one active seekTo processed at any time. If there is a to-be-completed
2036      * seekTo, new seekTo requests will be queued in such a way that only the last request
2037      * is kept. When current seekTo is completed, the queued request will be processed if
2038      * that request is different from just-finished seekTo operation, i.e., the requested
2039      * position or mode is different.
2040      *
2041      * @param msec the offset in milliseconds from the start to seek to.
2042      * When seeking to the given time position, there is no guarantee that the data source
2043      * has a frame located at the position. When this happens, a frame nearby will be rendered.
2044      * If msec is negative, time position zero will be used.
2045      * If msec is larger than duration, duration will be used.
2046      * @param mode the mode indicating where exactly to seek to.
2047      * Use {@link #SEEK_PREVIOUS_SYNC} if one wants to seek to a sync frame
2048      * that has a timestamp earlier than or the same as msec. Use
2049      * {@link #SEEK_NEXT_SYNC} if one wants to seek to a sync frame
2050      * that has a timestamp later than or the same as msec. Use
2051      * {@link #SEEK_CLOSEST_SYNC} if one wants to seek to a sync frame
2052      * that has a timestamp closest to or the same as msec. Use
2053      * {@link #SEEK_CLOSEST} if one wants to seek to a frame that may
2054      * or may not be a sync frame but is closest to or the same as msec.
2055      * {@link #SEEK_CLOSEST} often has larger performance overhead compared
2056      * to the other options if there is no sync frame located at msec.
2057      * @throws IllegalStateException if the internal player engine has not been
2058      * initialized
2059      * @throws IllegalArgumentException if the mode is invalid.
2060      */
seekTo(long msec, @SeekMode int mode)2061     public void seekTo(long msec, @SeekMode int mode) {
2062         if (mode < SEEK_PREVIOUS_SYNC || mode > SEEK_CLOSEST) {
2063             final String msg = "Illegal seek mode: " + mode;
2064             throw new IllegalArgumentException(msg);
2065         }
2066         // TODO: pass long to native, instead of truncating here.
2067         if (msec > Integer.MAX_VALUE) {
2068             Log.w(TAG, "seekTo offset " + msec + " is too large, cap to " + Integer.MAX_VALUE);
2069             msec = Integer.MAX_VALUE;
2070         } else if (msec < Integer.MIN_VALUE) {
2071             Log.w(TAG, "seekTo offset " + msec + " is too small, cap to " + Integer.MIN_VALUE);
2072             msec = Integer.MIN_VALUE;
2073         }
2074         _seekTo(msec, mode);
2075     }
2076 
2077     /**
2078      * Seeks to specified time position.
2079      * Same as {@link #seekTo(long, int)} with {@code mode = SEEK_PREVIOUS_SYNC}.
2080      *
2081      * @param msec the offset in milliseconds from the start to seek to
2082      * @throws IllegalStateException if the internal player engine has not been
2083      * initialized
2084      */
seekTo(int msec)2085     public void seekTo(int msec) throws IllegalStateException {
2086         seekTo(msec, SEEK_PREVIOUS_SYNC /* mode */);
2087     }
2088 
2089     /**
2090      * Get current playback position as a {@link MediaTimestamp}.
2091      * <p>
2092      * The MediaTimestamp represents how the media time correlates to the system time in
2093      * a linear fashion using an anchor and a clock rate. During regular playback, the media
2094      * time moves fairly constantly (though the anchor frame may be rebased to a current
2095      * system time, the linear correlation stays steady). Therefore, this method does not
2096      * need to be called often.
2097      * <p>
2098      * To help users get current playback position, this method always anchors the timestamp
2099      * to the current {@link System#nanoTime system time}, so
2100      * {@link MediaTimestamp#getAnchorMediaTimeUs} can be used as current playback position.
2101      *
2102      * @return a MediaTimestamp object if a timestamp is available, or {@code null} if no timestamp
2103      *         is available, e.g. because the media player has not been initialized.
2104      *
2105      * @see MediaTimestamp
2106      */
2107     @Nullable
getTimestamp()2108     public MediaTimestamp getTimestamp()
2109     {
2110         try {
2111             // TODO: get the timestamp from native side
2112             return new MediaTimestamp(
2113                     getCurrentPosition() * 1000L,
2114                     System.nanoTime(),
2115                     isPlaying() ? getPlaybackParams().getSpeed() : 0.f);
2116         } catch (IllegalStateException e) {
2117             return null;
2118         }
2119     }
2120 
2121     /**
2122      * Gets the current playback position.
2123      *
2124      * @return the current position in milliseconds
2125      */
getCurrentPosition()2126     public native int getCurrentPosition();
2127 
2128     /**
2129      * Gets the duration of the file.
2130      *
2131      * @return the duration in milliseconds, if no duration is available
2132      *         (for example, if streaming live content), -1 is returned.
2133      */
getDuration()2134     public native int getDuration();
2135 
2136     /**
2137      * Gets the media metadata.
2138      *
2139      * @param update_only controls whether the full set of available
2140      * metadata is returned or just the set that changed since the
2141      * last call. See {@see #METADATA_UPDATE_ONLY} and {@see
2142      * #METADATA_ALL}.
2143      *
2144      * @param apply_filter if true only metadata that matches the
2145      * filter is returned. See {@see #APPLY_METADATA_FILTER} and {@see
2146      * #BYPASS_METADATA_FILTER}.
2147      *
2148      * @return The metadata, possibly empty. null if an error occured.
2149      // FIXME: unhide.
2150      * {@hide}
2151      */
2152     @UnsupportedAppUsage
getMetadata(final boolean update_only, final boolean apply_filter)2153     public Metadata getMetadata(final boolean update_only,
2154                                 final boolean apply_filter) {
2155         Parcel reply = Parcel.obtain();
2156         Metadata data = new Metadata();
2157 
2158         if (!native_getMetadata(update_only, apply_filter, reply)) {
2159             reply.recycle();
2160             return null;
2161         }
2162 
2163         // Metadata takes over the parcel, don't recycle it unless
2164         // there is an error.
2165         if (!data.parse(reply)) {
2166             reply.recycle();
2167             return null;
2168         }
2169         return data;
2170     }
2171 
2172     /**
2173      * Set a filter for the metadata update notification and update
2174      * retrieval. The caller provides 2 set of metadata keys, allowed
2175      * and blocked. The blocked set always takes precedence over the
2176      * allowed one.
2177      * Metadata.MATCH_ALL and Metadata.MATCH_NONE are 2 sets available as
2178      * shorthands to allow/block all or no metadata.
2179      *
2180      * By default, there is no filter set.
2181      *
2182      * @param allow Is the set of metadata the client is interested
2183      *              in receiving new notifications for.
2184      * @param block Is the set of metadata the client is not interested
2185      *              in receiving new notifications for.
2186      * @return The call status code.
2187      *
2188      // FIXME: unhide.
2189      * {@hide}
2190      */
setMetadataFilter(Set<Integer> allow, Set<Integer> block)2191     public int setMetadataFilter(Set<Integer> allow, Set<Integer> block) {
2192         // Do our serialization manually instead of calling
2193         // Parcel.writeArray since the sets are made of the same type
2194         // we avoid paying the price of calling writeValue (used by
2195         // writeArray) which burns an extra int per element to encode
2196         // the type.
2197         Parcel request =  newRequest();
2198 
2199         // The parcel starts already with an interface token. There
2200         // are 2 filters. Each one starts with a 4bytes number to
2201         // store the len followed by a number of int (4 bytes as well)
2202         // representing the metadata type.
2203         int capacity = request.dataSize() + 4 * (1 + allow.size() + 1 + block.size());
2204 
2205         if (request.dataCapacity() < capacity) {
2206             request.setDataCapacity(capacity);
2207         }
2208 
2209         request.writeInt(allow.size());
2210         for(Integer t: allow) {
2211             request.writeInt(t);
2212         }
2213         request.writeInt(block.size());
2214         for(Integer t: block) {
2215             request.writeInt(t);
2216         }
2217         return native_setMetadataFilter(request);
2218     }
2219 
2220     /**
2221      * Set the MediaPlayer to start when this MediaPlayer finishes playback
2222      * (i.e. reaches the end of the stream).
2223      * The media framework will attempt to transition from this player to
2224      * the next as seamlessly as possible. The next player can be set at
2225      * any time before completion, but shall be after setDataSource has been
2226      * called successfully. The next player must be prepared by the
2227      * app, and the application should not call start() on it.
2228      * The next MediaPlayer must be different from 'this'. An exception
2229      * will be thrown if next == this.
2230      * The application may call setNextMediaPlayer(null) to indicate no
2231      * next player should be started at the end of playback.
2232      * If the current player is looping, it will keep looping and the next
2233      * player will not be started.
2234      *
2235      * @param next the player to start after this one completes playback.
2236      *
2237      */
setNextMediaPlayer(MediaPlayer next)2238     public native void setNextMediaPlayer(MediaPlayer next);
2239 
2240     /**
2241      * Releases resources associated with this MediaPlayer object.
2242      *
2243      * <p>You must call this method once the instance is no longer required.
2244      */
release()2245     public void release() {
2246         baseRelease();
2247         stayAwake(false);
2248         updateSurfaceScreenOn();
2249         mOnPreparedListener = null;
2250         mOnBufferingUpdateListener = null;
2251         mOnCompletionListener = null;
2252         mOnSeekCompleteListener = null;
2253         mOnErrorListener = null;
2254         mOnInfoListener = null;
2255         mOnVideoSizeChangedListener = null;
2256         mOnTimedTextListener = null;
2257         mOnRtpRxNoticeListener = null;
2258         mOnRtpRxNoticeExecutor = null;
2259         synchronized (mTimeProviderLock) {
2260             if (mTimeProvider != null) {
2261                 mTimeProvider.close();
2262                 mTimeProvider = null;
2263             }
2264         }
2265         synchronized(this) {
2266             mSubtitleDataListenerDisabled = false;
2267             mExtSubtitleDataListener = null;
2268             mExtSubtitleDataHandler = null;
2269             mOnMediaTimeDiscontinuityListener = null;
2270             mOnMediaTimeDiscontinuityHandler = null;
2271         }
2272 
2273         // Modular DRM clean up
2274         mOnDrmConfigHelper = null;
2275         mOnDrmInfoHandlerDelegate = null;
2276         mOnDrmPreparedHandlerDelegate = null;
2277         resetDrmState();
2278 
2279         _release();
2280     }
2281 
_release()2282     private native void _release();
2283 
2284     /**
2285      * Resets the MediaPlayer to its uninitialized state. After calling
2286      * this method, you will have to initialize it again by setting the
2287      * data source and calling prepare().
2288      */
reset()2289     public void reset() {
2290         mSelectedSubtitleTrackIndex = -1;
2291         synchronized(mOpenSubtitleSources) {
2292             for (final InputStream is: mOpenSubtitleSources) {
2293                 try {
2294                     is.close();
2295                 } catch (IOException e) {
2296                 }
2297             }
2298             mOpenSubtitleSources.clear();
2299         }
2300         if (mSubtitleController != null) {
2301             mSubtitleController.reset();
2302         }
2303         synchronized (mTimeProviderLock) {
2304             if (mTimeProvider != null) {
2305                 mTimeProvider.close();
2306                 mTimeProvider = null;
2307             }
2308         }
2309 
2310         stayAwake(false);
2311         _reset();
2312         // make sure none of the listeners get called anymore
2313         if (mEventHandler != null) {
2314             mEventHandler.removeCallbacksAndMessages(null);
2315         }
2316 
2317         synchronized (mIndexTrackPairs) {
2318             mIndexTrackPairs.clear();
2319             mInbandTrackIndices.clear();
2320         };
2321 
2322         resetDrmState();
2323     }
2324 
_reset()2325     private native void _reset();
2326 
2327     /**
2328      * Set up a timer for {@link #TimeProvider}. {@link #TimeProvider} will be
2329      * notified when the presentation time reaches (becomes greater than or equal to)
2330      * the value specified.
2331      *
2332      * @param mediaTimeUs presentation time to get timed event callback at
2333      * @hide
2334      */
notifyAt(long mediaTimeUs)2335     public void notifyAt(long mediaTimeUs) {
2336         _notifyAt(mediaTimeUs);
2337     }
2338 
_notifyAt(long mediaTimeUs)2339     private native void _notifyAt(long mediaTimeUs);
2340 
2341     /**
2342      * Sets the audio stream type for this MediaPlayer. See {@link AudioManager}
2343      * for a list of stream types. Must call this method before prepare() or
2344      * prepareAsync() in order for the target stream type to become effective
2345      * thereafter.
2346      *
2347      * @param streamtype the audio stream type
2348      * @deprecated use {@link #setAudioAttributes(AudioAttributes)}
2349      * @see android.media.AudioManager
2350      */
setAudioStreamType(int streamtype)2351     public void setAudioStreamType(int streamtype) {
2352         deprecateStreamTypeForPlayback(streamtype, "MediaPlayer", "setAudioStreamType()");
2353         baseUpdateAudioAttributes(
2354                 new AudioAttributes.Builder().setInternalLegacyStreamType(streamtype).build());
2355         _setAudioStreamType(streamtype);
2356         mStreamType = streamtype;
2357     }
2358 
_setAudioStreamType(int streamtype)2359     private native void _setAudioStreamType(int streamtype);
2360 
2361     // Keep KEY_PARAMETER_* in sync with include/media/mediaplayer.h
2362     private final static int KEY_PARAMETER_AUDIO_ATTRIBUTES = 1400;
2363     /**
2364      * Sets the parameter indicated by key.
2365      * @param key key indicates the parameter to be set.
2366      * @param value value of the parameter to be set.
2367      * @return true if the parameter is set successfully, false otherwise
2368      * {@hide}
2369      */
2370     @UnsupportedAppUsage
setParameter(int key, Parcel value)2371     private native boolean setParameter(int key, Parcel value);
2372 
2373     /**
2374      * Sets the audio attributes for this MediaPlayer.
2375      * See {@link AudioAttributes} for how to build and configure an instance of this class.
2376      * You must call this method before {@link #prepare()} or {@link #prepareAsync()} in order
2377      * for the audio attributes to become effective thereafter.
2378      * @param attributes a non-null set of audio attributes
2379      */
setAudioAttributes(AudioAttributes attributes)2380     public void setAudioAttributes(AudioAttributes attributes) throws IllegalArgumentException {
2381         if (attributes == null) {
2382             final String msg = "Cannot set AudioAttributes to null";
2383             throw new IllegalArgumentException(msg);
2384         }
2385         baseUpdateAudioAttributes(attributes);
2386         Parcel pattributes = Parcel.obtain();
2387         attributes.writeToParcel(pattributes, AudioAttributes.FLATTEN_TAGS);
2388         setParameter(KEY_PARAMETER_AUDIO_ATTRIBUTES, pattributes);
2389         pattributes.recycle();
2390     }
2391 
2392     /**
2393      * Sets the player to be looping or non-looping.
2394      *
2395      * @param looping whether to loop or not
2396      */
setLooping(boolean looping)2397     public native void setLooping(boolean looping);
2398 
2399     /**
2400      * Checks whether the MediaPlayer is looping or non-looping.
2401      *
2402      * @return true if the MediaPlayer is currently looping, false otherwise
2403      */
isLooping()2404     public native boolean isLooping();
2405 
2406     /**
2407      * Sets the volume on this player.
2408      * This API is recommended for balancing the output of audio streams
2409      * within an application. Unless you are writing an application to
2410      * control user settings, this API should be used in preference to
2411      * {@link AudioManager#setStreamVolume(int, int, int)} which sets the volume of ALL streams of
2412      * a particular type. Note that the passed volume values are raw scalars in range 0.0 to 1.0.
2413      * UI controls should be scaled logarithmically.
2414      *
2415      * @param leftVolume left volume scalar
2416      * @param rightVolume right volume scalar
2417      */
2418     /*
2419      * FIXME: Merge this into javadoc comment above when setVolume(float) is not @hide.
2420      * The single parameter form below is preferred if the channel volumes don't need
2421      * to be set independently.
2422      */
setVolume(float leftVolume, float rightVolume)2423     public void setVolume(float leftVolume, float rightVolume) {
2424         baseSetVolume(leftVolume, rightVolume);
2425     }
2426 
2427     @Override
playerSetVolume(boolean muting, float leftVolume, float rightVolume)2428     void playerSetVolume(boolean muting, float leftVolume, float rightVolume) {
2429         _setVolume(muting ? 0.0f : leftVolume, muting ? 0.0f : rightVolume);
2430     }
2431 
_setVolume(float leftVolume, float rightVolume)2432     private native void _setVolume(float leftVolume, float rightVolume);
2433 
2434     /**
2435      * Similar, excepts sets volume of all channels to same value.
2436      * @hide
2437      */
setVolume(float volume)2438     public void setVolume(float volume) {
2439         setVolume(volume, volume);
2440     }
2441 
2442     /**
2443      * Sets the audio session ID.
2444      *
2445      * @param sessionId the audio session ID.
2446      * The audio session ID is a system wide unique identifier for the audio stream played by
2447      * this MediaPlayer instance.
2448      * The primary use of the audio session ID  is to associate audio effects to a particular
2449      * instance of MediaPlayer: if an audio session ID is provided when creating an audio effect,
2450      * this effect will be applied only to the audio content of media players within the same
2451      * audio session and not to the output mix.
2452      * When created, a MediaPlayer instance automatically generates its own audio session ID.
2453      * However, it is possible to force this player to be part of an already existing audio session
2454      * by calling this method.
2455      * This method must be called before one of the overloaded <code> setDataSource </code> methods.
2456      * Note that session id set using this method will override device-specific audio session id,
2457      * if the {@link MediaPlayer} was instantiated using device-specific {@link Context} -
2458      * see {@link MediaPlayer#MediaPlayer(Context)}.
2459      * @throws IllegalStateException if it is called in an invalid state
2460      */
setAudioSessionId(int sessionId)2461     public void setAudioSessionId(int sessionId)
2462             throws IllegalArgumentException, IllegalStateException {
2463         native_setAudioSessionId(sessionId);
2464         baseUpdateSessionId(sessionId);
2465     }
2466 
native_setAudioSessionId(int sessionId)2467     private native void native_setAudioSessionId(int sessionId);
2468 
2469     /**
2470      * Returns the audio session ID.
2471      *
2472      * @return the audio session ID. {@see #setAudioSessionId(int)}
2473      * Note that the audio session ID is 0 only if a problem occured when the MediaPlayer was contructed.
2474      */
getAudioSessionId()2475     public native int getAudioSessionId();
2476 
2477     /**
2478      * Attaches an auxiliary effect to the player. A typical auxiliary effect is a reverberation
2479      * effect which can be applied on any sound source that directs a certain amount of its
2480      * energy to this effect. This amount is defined by setAuxEffectSendLevel().
2481      * See {@link #setAuxEffectSendLevel(float)}.
2482      * <p>After creating an auxiliary effect (e.g.
2483      * {@link android.media.audiofx.EnvironmentalReverb}), retrieve its ID with
2484      * {@link android.media.audiofx.AudioEffect#getId()} and use it when calling this method
2485      * to attach the player to the effect.
2486      * <p>To detach the effect from the player, call this method with a null effect id.
2487      * <p>This method must be called after one of the overloaded <code> setDataSource </code>
2488      * methods.
2489      * @param effectId system wide unique id of the effect to attach
2490      */
attachAuxEffect(int effectId)2491     public native void attachAuxEffect(int effectId);
2492 
2493 
2494     /**
2495      * Sets the send level of the player to the attached auxiliary effect.
2496      * See {@link #attachAuxEffect(int)}. The level value range is 0 to 1.0.
2497      * <p>By default the send level is 0, so even if an effect is attached to the player
2498      * this method must be called for the effect to be applied.
2499      * <p>Note that the passed level value is a raw scalar. UI controls should be scaled
2500      * logarithmically: the gain applied by audio framework ranges from -72dB to 0dB,
2501      * so an appropriate conversion from linear UI input x to level is:
2502      * x == 0 -> level = 0
2503      * 0 < x <= R -> level = 10^(72*(x-R)/20/R)
2504      * @param level send level scalar
2505      */
setAuxEffectSendLevel(float level)2506     public void setAuxEffectSendLevel(float level) {
2507         baseSetAuxEffectSendLevel(level);
2508     }
2509 
2510     @Override
playerSetAuxEffectSendLevel(boolean muting, float level)2511     int playerSetAuxEffectSendLevel(boolean muting, float level) {
2512         _setAuxEffectSendLevel(muting ? 0.0f : level);
2513         return AudioSystem.SUCCESS;
2514     }
2515 
_setAuxEffectSendLevel(float level)2516     private native void _setAuxEffectSendLevel(float level);
2517 
2518     /*
2519      * @param request Parcel destinated to the media player. The
2520      *                Interface token must be set to the IMediaPlayer
2521      *                one to be routed correctly through the system.
2522      * @param reply[out] Parcel that will contain the reply.
2523      * @return The status code.
2524      */
native_invoke(Parcel request, Parcel reply)2525     private native final int native_invoke(Parcel request, Parcel reply);
2526 
2527 
2528     /*
2529      * @param update_only If true fetch only the set of metadata that have
2530      *                    changed since the last invocation of getMetadata.
2531      *                    The set is built using the unfiltered
2532      *                    notifications the native player sent to the
2533      *                    MediaPlayerService during that period of
2534      *                    time. If false, all the metadatas are considered.
2535      * @param apply_filter  If true, once the metadata set has been built based on
2536      *                     the value update_only, the current filter is applied.
2537      * @param reply[out] On return contains the serialized
2538      *                   metadata. Valid only if the call was successful.
2539      * @return The status code.
2540      */
native_getMetadata(boolean update_only, boolean apply_filter, Parcel reply)2541     private native final boolean native_getMetadata(boolean update_only,
2542                                                     boolean apply_filter,
2543                                                     Parcel reply);
2544 
2545     /*
2546      * @param request Parcel with the 2 serialized lists of allowed
2547      *                metadata types followed by the one to be
2548      *                dropped. Each list starts with an integer
2549      *                indicating the number of metadata type elements.
2550      * @return The status code.
2551      */
native_setMetadataFilter(Parcel request)2552     private native final int native_setMetadataFilter(Parcel request);
2553 
native_init()2554     private static native final void native_init();
native_setup(Object mediaplayerThis, @NonNull Parcel attributionSource, int audioSessionId)2555     private native void native_setup(Object mediaplayerThis,
2556             @NonNull Parcel attributionSource, int audioSessionId);
native_finalize()2557     private native final void native_finalize();
2558 
2559     /**
2560      * Class for MediaPlayer to return each audio/video/subtitle track's metadata.
2561      *
2562      * @see android.media.MediaPlayer#getTrackInfo
2563      */
2564     // The creator needs to be pulic, which requires removing the @UnsupportedAppUsage
2565     @SuppressWarnings("ParcelableCreator")
2566     static public class TrackInfo implements Parcelable {
2567         /**
2568          * Gets the track type.
2569          * @return TrackType which indicates if the track is video, audio, timed text.
2570          */
getTrackType()2571         public @TrackType int getTrackType() {
2572             return mTrackType;
2573         }
2574 
2575         /**
2576          * Gets the language code of the track.
2577          * @return a language code in either way of ISO-639-1 or ISO-639-2.
2578          * When the language is unknown or could not be determined,
2579          * ISO-639-2 language code, "und", is returned.
2580          */
getLanguage()2581         public String getLanguage() {
2582             String language = mFormat.getString(MediaFormat.KEY_LANGUAGE);
2583             return language == null ? "und" : language;
2584         }
2585 
2586         /**
2587          * Returns whether this track contains haptic channels in the audio track.
2588          * @hide
2589          */
hasHapticChannels()2590         public boolean hasHapticChannels() {
2591             return mFormat != null && mFormat.containsKey(MediaFormat.KEY_HAPTIC_CHANNEL_COUNT)
2592                     && mFormat.getInteger(MediaFormat.KEY_HAPTIC_CHANNEL_COUNT) > 0;
2593         }
2594 
2595         /**
2596          * Gets the {@link MediaFormat} of the track.  If the format is
2597          * unknown or could not be determined, null is returned.
2598          */
getFormat()2599         public MediaFormat getFormat() {
2600             // Note: The format isn't exposed for audio because it is incomplete.
2601             if (mTrackType == MEDIA_TRACK_TYPE_TIMEDTEXT
2602                     || mTrackType == MEDIA_TRACK_TYPE_SUBTITLE) {
2603                 return mFormat;
2604             }
2605             return null;
2606         }
2607 
2608         public static final int MEDIA_TRACK_TYPE_UNKNOWN = 0;
2609         public static final int MEDIA_TRACK_TYPE_VIDEO = 1;
2610         public static final int MEDIA_TRACK_TYPE_AUDIO = 2;
2611         public static final int MEDIA_TRACK_TYPE_TIMEDTEXT = 3;
2612         public static final int MEDIA_TRACK_TYPE_SUBTITLE = 4;
2613         public static final int MEDIA_TRACK_TYPE_METADATA = 5;
2614 
2615         /** @hide */
2616         @IntDef(flag = false, prefix = "MEDIA_TRACK_TYPE", value = {
2617                 MEDIA_TRACK_TYPE_UNKNOWN,
2618                 MEDIA_TRACK_TYPE_VIDEO,
2619                 MEDIA_TRACK_TYPE_AUDIO,
2620                 MEDIA_TRACK_TYPE_TIMEDTEXT,
2621                 MEDIA_TRACK_TYPE_SUBTITLE,
2622                 MEDIA_TRACK_TYPE_METADATA }
2623         )
2624         @Retention(RetentionPolicy.SOURCE)
2625         public @interface TrackType {}
2626 
2627 
2628         final int mTrackType;
2629         final MediaFormat mFormat;
2630 
TrackInfo(Parcel in)2631         TrackInfo(Parcel in) {
2632             mTrackType = in.readInt();
2633             // TODO: parcel in the full MediaFormat; currently we are using createSubtitleFormat
2634             // even for audio/video tracks, meaning we only set the mime and language.
2635             String mime = in.readString();
2636             String language = in.readString();
2637             mFormat = MediaFormat.createSubtitleFormat(mime, language);
2638 
2639             if (mTrackType == MEDIA_TRACK_TYPE_SUBTITLE) {
2640                 mFormat.setInteger(MediaFormat.KEY_IS_AUTOSELECT, in.readInt());
2641                 mFormat.setInteger(MediaFormat.KEY_IS_DEFAULT, in.readInt());
2642                 mFormat.setInteger(MediaFormat.KEY_IS_FORCED_SUBTITLE, in.readInt());
2643             } else if (mTrackType == MEDIA_TRACK_TYPE_AUDIO) {
2644                 boolean hasHapticChannels = in.readBoolean();
2645                 if (hasHapticChannels) {
2646                     mFormat.setInteger(MediaFormat.KEY_HAPTIC_CHANNEL_COUNT, in.readInt());
2647                 }
2648             }
2649         }
2650 
2651         /** @hide */
TrackInfo(int type, MediaFormat format)2652         TrackInfo(int type, MediaFormat format) {
2653             mTrackType = type;
2654             mFormat = format;
2655         }
2656 
2657         /**
2658          * {@inheritDoc}
2659          */
2660         @Override
describeContents()2661         public int describeContents() {
2662             return 0;
2663         }
2664 
2665         /**
2666          * {@inheritDoc}
2667          */
2668         @Override
writeToParcel(Parcel dest, int flags)2669         public void writeToParcel(Parcel dest, int flags) {
2670             dest.writeInt(mTrackType);
2671             dest.writeString(mFormat.getString(MediaFormat.KEY_MIME));
2672             dest.writeString(getLanguage());
2673 
2674             if (mTrackType == MEDIA_TRACK_TYPE_SUBTITLE) {
2675                 dest.writeInt(mFormat.getInteger(MediaFormat.KEY_IS_AUTOSELECT));
2676                 dest.writeInt(mFormat.getInteger(MediaFormat.KEY_IS_DEFAULT));
2677                 dest.writeInt(mFormat.getInteger(MediaFormat.KEY_IS_FORCED_SUBTITLE));
2678             } else if (mTrackType == MEDIA_TRACK_TYPE_AUDIO) {
2679                 boolean hasHapticChannels =
2680                         mFormat.containsKey(MediaFormat.KEY_HAPTIC_CHANNEL_COUNT);
2681                 dest.writeBoolean(hasHapticChannels);
2682                 if (hasHapticChannels) {
2683                     dest.writeInt(mFormat.getInteger(MediaFormat.KEY_HAPTIC_CHANNEL_COUNT));
2684                 }
2685             }
2686         }
2687 
2688         @Override
toString()2689         public String toString() {
2690             StringBuilder out = new StringBuilder(128);
2691             out.append(getClass().getName());
2692             out.append('{');
2693             switch (mTrackType) {
2694             case MEDIA_TRACK_TYPE_VIDEO:
2695                 out.append("VIDEO");
2696                 break;
2697             case MEDIA_TRACK_TYPE_AUDIO:
2698                 out.append("AUDIO");
2699                 break;
2700             case MEDIA_TRACK_TYPE_TIMEDTEXT:
2701                 out.append("TIMEDTEXT");
2702                 break;
2703             case MEDIA_TRACK_TYPE_SUBTITLE:
2704                 out.append("SUBTITLE");
2705                 break;
2706             default:
2707                 out.append("UNKNOWN");
2708                 break;
2709             }
2710             out.append(", " + mFormat.toString());
2711             out.append("}");
2712             return out.toString();
2713         }
2714 
2715         /**
2716          * Used to read a TrackInfo from a Parcel.
2717          */
2718         @UnsupportedAppUsage
2719         static final @android.annotation.NonNull Parcelable.Creator<TrackInfo> CREATOR
2720                 = new Parcelable.Creator<TrackInfo>() {
2721                     @Override
2722                     public TrackInfo createFromParcel(Parcel in) {
2723                         return new TrackInfo(in);
2724                     }
2725 
2726                     @Override
2727                     public TrackInfo[] newArray(int size) {
2728                         return new TrackInfo[size];
2729                     }
2730                 };
2731 
2732     };
2733 
2734     // We would like domain specific classes with more informative names than the `first` and `second`
2735     // in generic Pair, but we would also like to avoid creating new/trivial classes. As a compromise
2736     // we document the meanings of `first` and `second` here:
2737     //
2738     // Pair.first - inband track index; non-null iff representing an inband track.
2739     // Pair.second - a SubtitleTrack registered with mSubtitleController; non-null iff representing
2740     //               an inband subtitle track or any out-of-band track (subtitle or timedtext).
2741     private Vector<Pair<Integer, SubtitleTrack>> mIndexTrackPairs = new Vector<>();
2742     private BitSet mInbandTrackIndices = new BitSet();
2743 
2744     /**
2745      * Returns an array of track information.
2746      *
2747      * @return Array of track info. The total number of tracks is the array length.
2748      * Must be called again if an external timed text source has been added after any of the
2749      * addTimedTextSource methods are called.
2750      * @throws IllegalStateException if it is called in an invalid state.
2751      */
getTrackInfo()2752     public TrackInfo[] getTrackInfo() throws IllegalStateException {
2753         TrackInfo trackInfo[] = getInbandTrackInfo();
2754         // add out-of-band tracks
2755         synchronized (mIndexTrackPairs) {
2756             TrackInfo allTrackInfo[] = new TrackInfo[mIndexTrackPairs.size()];
2757             for (int i = 0; i < allTrackInfo.length; i++) {
2758                 Pair<Integer, SubtitleTrack> p = mIndexTrackPairs.get(i);
2759                 if (p.first != null) {
2760                     // inband track
2761                     allTrackInfo[i] = trackInfo[p.first];
2762                 } else {
2763                     SubtitleTrack track = p.second;
2764                     allTrackInfo[i] = new TrackInfo(track.getTrackType(), track.getFormat());
2765                 }
2766             }
2767             return allTrackInfo;
2768         }
2769     }
2770 
getInbandTrackInfo()2771     private TrackInfo[] getInbandTrackInfo() throws IllegalStateException {
2772         Parcel request = Parcel.obtain();
2773         Parcel reply = Parcel.obtain();
2774         try {
2775             request.writeInterfaceToken(IMEDIA_PLAYER);
2776             request.writeInt(INVOKE_ID_GET_TRACK_INFO);
2777             invoke(request, reply);
2778             TrackInfo trackInfo[] = reply.createTypedArray(TrackInfo.CREATOR);
2779             return trackInfo;
2780         } finally {
2781             request.recycle();
2782             reply.recycle();
2783         }
2784     }
2785 
2786     /* Do not change these values without updating their counterparts
2787      * in include/media/stagefright/MediaDefs.h and media/libstagefright/MediaDefs.cpp!
2788      */
2789     /**
2790      * MIME type for SubRip (SRT) container. Used in addTimedTextSource APIs.
2791      * @deprecated use {@link MediaFormat#MIMETYPE_TEXT_SUBRIP}
2792      */
2793     public static final String MEDIA_MIMETYPE_TEXT_SUBRIP = MediaFormat.MIMETYPE_TEXT_SUBRIP;
2794 
2795     /**
2796      * MIME type for WebVTT subtitle data.
2797      * @hide
2798      * @deprecated
2799      */
2800     public static final String MEDIA_MIMETYPE_TEXT_VTT = MediaFormat.MIMETYPE_TEXT_VTT;
2801 
2802     /**
2803      * MIME type for CEA-608 closed caption data.
2804      * @hide
2805      * @deprecated
2806      */
2807     public static final String MEDIA_MIMETYPE_TEXT_CEA_608 = MediaFormat.MIMETYPE_TEXT_CEA_608;
2808 
2809     /**
2810      * MIME type for CEA-708 closed caption data.
2811      * @hide
2812      * @deprecated
2813      */
2814     public static final String MEDIA_MIMETYPE_TEXT_CEA_708 = MediaFormat.MIMETYPE_TEXT_CEA_708;
2815 
2816     /*
2817      * A helper function to check if the mime type is supported by media framework.
2818      */
availableMimeTypeForExternalSource(String mimeType)2819     private static boolean availableMimeTypeForExternalSource(String mimeType) {
2820         if (MEDIA_MIMETYPE_TEXT_SUBRIP.equals(mimeType)) {
2821             return true;
2822         }
2823         return false;
2824     }
2825 
2826     private SubtitleController mSubtitleController;
2827 
2828     /** @hide */
2829     @UnsupportedAppUsage
setSubtitleAnchor( SubtitleController controller, SubtitleController.Anchor anchor)2830     public void setSubtitleAnchor(
2831             SubtitleController controller,
2832             SubtitleController.Anchor anchor) {
2833         // TODO: create SubtitleController in MediaPlayer
2834         mSubtitleController = controller;
2835         mSubtitleController.setAnchor(anchor);
2836     }
2837 
2838     /**
2839      * The private version of setSubtitleAnchor is used internally to set mSubtitleController if
2840      * necessary when clients don't provide their own SubtitleControllers using the public version
2841      * {@link #setSubtitleAnchor(SubtitleController, Anchor)} (e.g. {@link VideoView} provides one).
2842      */
setSubtitleAnchor()2843     private synchronized void setSubtitleAnchor() {
2844         if ((mSubtitleController == null) && (ActivityThread.currentApplication() != null)) {
2845             final TimeProvider timeProvider = (TimeProvider) getMediaTimeProvider();
2846             final HandlerThread thread = new HandlerThread("SetSubtitleAnchorThread");
2847             thread.start();
2848             Handler handler = new Handler(thread.getLooper());
2849             handler.post(new Runnable() {
2850                 @Override
2851                 public void run() {
2852                     Context context = ActivityThread.currentApplication();
2853                     mSubtitleController =
2854                             new SubtitleController(context, timeProvider, MediaPlayer.this);
2855                     mSubtitleController.setAnchor(new Anchor() {
2856                         @Override
2857                         public void setSubtitleWidget(RenderingWidget subtitleWidget) {
2858                         }
2859 
2860                         @Override
2861                         public Looper getSubtitleLooper() {
2862                             return timeProvider.mEventHandler.getLooper();
2863                         }
2864                     });
2865                     thread.getLooper().quitSafely();
2866                 }
2867             });
2868             try {
2869                 thread.join();
2870             } catch (InterruptedException e) {
2871                 Thread.currentThread().interrupt();
2872                 Log.w(TAG, "failed to join SetSubtitleAnchorThread");
2873             }
2874         }
2875     }
2876 
2877     private int mSelectedSubtitleTrackIndex = -1;
2878     private Vector<InputStream> mOpenSubtitleSources;
2879 
2880     private final OnSubtitleDataListener mIntSubtitleDataListener = new OnSubtitleDataListener() {
2881         @Override
2882         public void onSubtitleData(MediaPlayer mp, SubtitleData data) {
2883             int index = data.getTrackIndex();
2884             synchronized (mIndexTrackPairs) {
2885                 for (Pair<Integer, SubtitleTrack> p : mIndexTrackPairs) {
2886                     if (p.first != null && p.first == index && p.second != null) {
2887                         // inband subtitle track that owns data
2888                         SubtitleTrack track = p.second;
2889                         track.onData(data);
2890                     }
2891                 }
2892             }
2893         }
2894     };
2895 
2896     /** @hide */
2897     @Override
onSubtitleTrackSelected(SubtitleTrack track)2898     public void onSubtitleTrackSelected(SubtitleTrack track) {
2899         if (mSelectedSubtitleTrackIndex >= 0) {
2900             try {
2901                 selectOrDeselectInbandTrack(mSelectedSubtitleTrackIndex, false);
2902             } catch (IllegalStateException e) {
2903             }
2904             mSelectedSubtitleTrackIndex = -1;
2905         }
2906         synchronized (this) {
2907             mSubtitleDataListenerDisabled = true;
2908         }
2909         if (track == null) {
2910             return;
2911         }
2912 
2913         synchronized (mIndexTrackPairs) {
2914             for (Pair<Integer, SubtitleTrack> p : mIndexTrackPairs) {
2915                 if (p.first != null && p.second == track) {
2916                     // inband subtitle track that is selected
2917                     mSelectedSubtitleTrackIndex = p.first;
2918                     break;
2919                 }
2920             }
2921         }
2922 
2923         if (mSelectedSubtitleTrackIndex >= 0) {
2924             try {
2925                 selectOrDeselectInbandTrack(mSelectedSubtitleTrackIndex, true);
2926             } catch (IllegalStateException e) {
2927             }
2928             synchronized (this) {
2929                 mSubtitleDataListenerDisabled = false;
2930             }
2931         }
2932         // no need to select out-of-band tracks
2933     }
2934 
2935     /** @hide */
2936     @UnsupportedAppUsage
addSubtitleSource(InputStream is, MediaFormat format)2937     public void addSubtitleSource(InputStream is, MediaFormat format)
2938             throws IllegalStateException
2939     {
2940         final InputStream fIs = is;
2941         final MediaFormat fFormat = format;
2942 
2943         if (is != null) {
2944             // Ensure all input streams are closed.  It is also a handy
2945             // way to implement timeouts in the future.
2946             synchronized(mOpenSubtitleSources) {
2947                 mOpenSubtitleSources.add(is);
2948             }
2949         } else {
2950             Log.w(TAG, "addSubtitleSource called with null InputStream");
2951         }
2952 
2953         getMediaTimeProvider();
2954 
2955         // process each subtitle in its own thread
2956         final HandlerThread thread = new HandlerThread("SubtitleReadThread",
2957               Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_MORE_FAVORABLE);
2958         thread.start();
2959         Handler handler = new Handler(thread.getLooper());
2960         handler.post(new Runnable() {
2961             private int addTrack() {
2962                 if (fIs == null || mSubtitleController == null) {
2963                     return MEDIA_INFO_UNSUPPORTED_SUBTITLE;
2964                 }
2965 
2966                 SubtitleTrack track = mSubtitleController.addTrack(fFormat);
2967                 if (track == null) {
2968                     return MEDIA_INFO_UNSUPPORTED_SUBTITLE;
2969                 }
2970 
2971                 // TODO: do the conversion in the subtitle track
2972                 Scanner scanner = new Scanner(fIs, "UTF-8");
2973                 String contents = scanner.useDelimiter("\\A").next();
2974                 synchronized(mOpenSubtitleSources) {
2975                     mOpenSubtitleSources.remove(fIs);
2976                 }
2977                 scanner.close();
2978                 synchronized (mIndexTrackPairs) {
2979                     mIndexTrackPairs.add(Pair.<Integer, SubtitleTrack>create(null, track));
2980                 }
2981                 synchronized (mTimeProviderLock) {
2982                     if (mTimeProvider != null) {
2983                         Handler h = mTimeProvider.mEventHandler;
2984                         int what = TimeProvider.NOTIFY;
2985                         int arg1 = TimeProvider.NOTIFY_TRACK_DATA;
2986                         Pair<SubtitleTrack, byte[]> trackData =
2987                                 Pair.create(track, contents.getBytes());
2988                         Message m = h.obtainMessage(what, arg1, 0, trackData);
2989                         h.sendMessage(m);
2990                     }
2991                 }
2992                 return MEDIA_INFO_EXTERNAL_METADATA_UPDATE;
2993             }
2994 
2995             public void run() {
2996                 int res = addTrack();
2997                 if (mEventHandler != null) {
2998                     Message m = mEventHandler.obtainMessage(MEDIA_INFO, res, 0, null);
2999                     mEventHandler.sendMessage(m);
3000                 }
3001                 thread.getLooper().quitSafely();
3002             }
3003         });
3004     }
3005 
scanInternalSubtitleTracks()3006     private void scanInternalSubtitleTracks() {
3007         setSubtitleAnchor();
3008 
3009         populateInbandTracks();
3010 
3011         if (mSubtitleController != null) {
3012             mSubtitleController.selectDefaultTrack();
3013         }
3014     }
3015 
populateInbandTracks()3016     private void populateInbandTracks() {
3017         TrackInfo[] tracks = getInbandTrackInfo();
3018         synchronized (mIndexTrackPairs) {
3019             for (int i = 0; i < tracks.length; i++) {
3020                 if (mInbandTrackIndices.get(i)) {
3021                     continue;
3022                 } else {
3023                     mInbandTrackIndices.set(i);
3024                 }
3025 
3026                 if (tracks[i] == null) {
3027                     Log.w(TAG, "unexpected NULL track at index " + i);
3028                 }
3029                 // newly appeared inband track
3030                 if (tracks[i] != null
3031                         && tracks[i].getTrackType() == TrackInfo.MEDIA_TRACK_TYPE_SUBTITLE) {
3032                     SubtitleTrack track = mSubtitleController.addTrack(
3033                             tracks[i].getFormat());
3034                     mIndexTrackPairs.add(Pair.create(i, track));
3035                 } else {
3036                     mIndexTrackPairs.add(Pair.<Integer, SubtitleTrack>create(i, null));
3037                 }
3038             }
3039         }
3040     }
3041 
3042     /* TODO: Limit the total number of external timed text source to a reasonable number.
3043      */
3044     /**
3045      * Adds an external timed text source file.
3046      *
3047      * Currently supported format is SubRip with the file extension .srt, case insensitive.
3048      * Note that a single external timed text source may contain multiple tracks in it.
3049      * One can find the total number of available tracks using {@link #getTrackInfo()} to see what
3050      * additional tracks become available after this method call.
3051      *
3052      * @param path The file path of external timed text source file.
3053      * @param mimeType The mime type of the file. Must be one of the mime types listed above.
3054      * @throws IOException if the file cannot be accessed or is corrupted.
3055      * @throws IllegalArgumentException if the mimeType is not supported.
3056      * @throws IllegalStateException if called in an invalid state.
3057      */
addTimedTextSource(String path, String mimeType)3058     public void addTimedTextSource(String path, String mimeType)
3059             throws IOException, IllegalArgumentException, IllegalStateException {
3060         if (!availableMimeTypeForExternalSource(mimeType)) {
3061             final String msg = "Illegal mimeType for timed text source: " + mimeType;
3062             throw new IllegalArgumentException(msg);
3063         }
3064 
3065         final File file = new File(path);
3066         try (FileInputStream is = new FileInputStream(file)) {
3067             addTimedTextSource(is.getFD(), mimeType);
3068         }
3069     }
3070 
3071     /**
3072      * Adds an external timed text source file (Uri).
3073      *
3074      * Currently supported format is SubRip with the file extension .srt, case insensitive.
3075      * Note that a single external timed text source may contain multiple tracks in it.
3076      * One can find the total number of available tracks using {@link #getTrackInfo()} to see what
3077      * additional tracks become available after this method call.
3078      *
3079      * @param context the Context to use when resolving the Uri
3080      * @param uri the Content URI of the data you want to play
3081      * @param mimeType The mime type of the file. Must be one of the mime types listed above.
3082      * @throws IOException if the file cannot be accessed or is corrupted.
3083      * @throws IllegalArgumentException if the mimeType is not supported.
3084      * @throws IllegalStateException if called in an invalid state.
3085      */
addTimedTextSource(Context context, Uri uri, String mimeType)3086     public void addTimedTextSource(Context context, Uri uri, String mimeType)
3087             throws IOException, IllegalArgumentException, IllegalStateException {
3088         String scheme = uri.getScheme();
3089         if(scheme == null || scheme.equals("file")) {
3090             addTimedTextSource(uri.getPath(), mimeType);
3091             return;
3092         }
3093 
3094         AssetFileDescriptor fd = null;
3095         try {
3096             boolean optimize = SystemProperties.getBoolean("fuse.sys.transcode_player_optimize",
3097                     false);
3098             ContentResolver resolver = context.getContentResolver();
3099             Bundle opts = new Bundle();
3100             opts.putBoolean("android.provider.extra.ACCEPT_ORIGINAL_MEDIA_FORMAT", true);
3101             fd = optimize ? resolver.openTypedAssetFileDescriptor(uri, "*/*", opts)
3102                     : resolver.openAssetFileDescriptor(uri, "r");
3103             if (fd == null) {
3104                 return;
3105             }
3106             addTimedTextSource(fd.getFileDescriptor(), mimeType);
3107             return;
3108         } catch (SecurityException ex) {
3109         } catch (IOException ex) {
3110         } finally {
3111             if (fd != null) {
3112                 fd.close();
3113             }
3114         }
3115     }
3116 
3117     /**
3118      * Adds an external timed text source file (FileDescriptor).
3119      *
3120      * It is the caller's responsibility to close the file descriptor.
3121      * It is safe to do so as soon as this call returns.
3122      *
3123      * Currently supported format is SubRip. Note that a single external timed text source may
3124      * contain multiple tracks in it. One can find the total number of available tracks
3125      * using {@link #getTrackInfo()} to see what additional tracks become available
3126      * after this method call.
3127      *
3128      * @param fd the FileDescriptor for the file you want to play
3129      * @param mimeType The mime type of the file. Must be one of the mime types listed above.
3130      * @throws IllegalArgumentException if the mimeType is not supported.
3131      * @throws IllegalStateException if called in an invalid state.
3132      */
addTimedTextSource(FileDescriptor fd, String mimeType)3133     public void addTimedTextSource(FileDescriptor fd, String mimeType)
3134             throws IllegalArgumentException, IllegalStateException {
3135         // intentionally less than LONG_MAX
3136         addTimedTextSource(fd, 0, 0x7ffffffffffffffL, mimeType);
3137     }
3138 
3139     /**
3140      * Adds an external timed text file (FileDescriptor).
3141      *
3142      * It is the caller's responsibility to close the file descriptor.
3143      * It is safe to do so as soon as this call returns.
3144      *
3145      * Currently supported format is SubRip. Note that a single external timed text source may
3146      * contain multiple tracks in it. One can find the total number of available tracks
3147      * using {@link #getTrackInfo()} to see what additional tracks become available
3148      * after this method call.
3149      *
3150      * @param fd the FileDescriptor for the file you want to play
3151      * @param offset the offset into the file where the data to be played starts, in bytes
3152      * @param length the length in bytes of the data to be played
3153      * @param mime The mime type of the file. Must be one of the mime types listed above.
3154      * @throws IllegalArgumentException if the mimeType is not supported.
3155      * @throws IllegalStateException if called in an invalid state.
3156      */
addTimedTextSource(FileDescriptor fd, long offset, long length, String mime)3157     public void addTimedTextSource(FileDescriptor fd, long offset, long length, String mime)
3158             throws IllegalArgumentException, IllegalStateException {
3159         if (!availableMimeTypeForExternalSource(mime)) {
3160             throw new IllegalArgumentException("Illegal mimeType for timed text source: " + mime);
3161         }
3162 
3163         final FileDescriptor dupedFd;
3164         try {
3165             dupedFd = Os.dup(fd);
3166         } catch (ErrnoException ex) {
3167             Log.e(TAG, ex.getMessage(), ex);
3168             throw new RuntimeException(ex);
3169         }
3170 
3171         final MediaFormat fFormat = new MediaFormat();
3172         fFormat.setString(MediaFormat.KEY_MIME, mime);
3173         fFormat.setInteger(MediaFormat.KEY_IS_TIMED_TEXT, 1);
3174 
3175         // A MediaPlayer created by a VideoView should already have its mSubtitleController set.
3176         if (mSubtitleController == null) {
3177             setSubtitleAnchor();
3178         }
3179 
3180         if (!mSubtitleController.hasRendererFor(fFormat)) {
3181             // test and add not atomic
3182             Context context = ActivityThread.currentApplication();
3183             mSubtitleController.registerRenderer(new SRTRenderer(context, mEventHandler));
3184         }
3185         final SubtitleTrack track = mSubtitleController.addTrack(fFormat);
3186         synchronized (mIndexTrackPairs) {
3187             mIndexTrackPairs.add(Pair.<Integer, SubtitleTrack>create(null, track));
3188         }
3189 
3190         getMediaTimeProvider();
3191 
3192         final long offset2 = offset;
3193         final long length2 = length;
3194         final HandlerThread thread = new HandlerThread(
3195                 "TimedTextReadThread",
3196                 Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_MORE_FAVORABLE);
3197         thread.start();
3198         Handler handler = new Handler(thread.getLooper());
3199         handler.post(new Runnable() {
3200             private int addTrack() {
3201                 final ByteArrayOutputStream bos = new ByteArrayOutputStream();
3202                 try {
3203                     Os.lseek(dupedFd, offset2, OsConstants.SEEK_SET);
3204                     byte[] buffer = new byte[4096];
3205                     for (long total = 0; total < length2;) {
3206                         int bytesToRead = (int) Math.min(buffer.length, length2 - total);
3207                         int bytes = IoBridge.read(dupedFd, buffer, 0, bytesToRead);
3208                         if (bytes < 0) {
3209                             break;
3210                         } else {
3211                             bos.write(buffer, 0, bytes);
3212                             total += bytes;
3213                         }
3214                     }
3215                     synchronized (mTimeProviderLock) {
3216                         if (mTimeProvider != null) {
3217                             Handler h = mTimeProvider.mEventHandler;
3218                             int what = TimeProvider.NOTIFY;
3219                             int arg1 = TimeProvider.NOTIFY_TRACK_DATA;
3220                             Pair<SubtitleTrack, byte[]> trackData =
3221                                     Pair.create(track, bos.toByteArray());
3222                             Message m = h.obtainMessage(what, arg1, 0, trackData);
3223                             h.sendMessage(m);
3224                         }
3225                     }
3226                     return MEDIA_INFO_EXTERNAL_METADATA_UPDATE;
3227                 } catch (Exception e) {
3228                     Log.e(TAG, e.getMessage(), e);
3229                     return MEDIA_INFO_TIMED_TEXT_ERROR;
3230                 } finally {
3231                     try {
3232                         Os.close(dupedFd);
3233                     } catch (ErrnoException e) {
3234                         Log.e(TAG, e.getMessage(), e);
3235                     }
3236                 }
3237             }
3238 
3239             public void run() {
3240                 int res = addTrack();
3241                 if (mEventHandler != null) {
3242                     Message m = mEventHandler.obtainMessage(MEDIA_INFO, res, 0, null);
3243                     mEventHandler.sendMessage(m);
3244                 }
3245                 thread.getLooper().quitSafely();
3246             }
3247         });
3248     }
3249 
3250     /**
3251      * Returns the index of the audio, video, or subtitle track currently selected for playback,
3252      * The return value is an index into the array returned by {@link #getTrackInfo()}, and can
3253      * be used in calls to {@link #selectTrack(int)} or {@link #deselectTrack(int)}.
3254      *
3255      * @param trackType should be one of {@link TrackInfo#MEDIA_TRACK_TYPE_VIDEO},
3256      * {@link TrackInfo#MEDIA_TRACK_TYPE_AUDIO}, or
3257      * {@link TrackInfo#MEDIA_TRACK_TYPE_SUBTITLE}
3258      * @return index of the audio, video, or subtitle track currently selected for playback;
3259      * a negative integer is returned when there is no selected track for {@code trackType} or
3260      * when {@code trackType} is not one of audio, video, or subtitle.
3261      * @throws IllegalStateException if called after {@link #release()}
3262      *
3263      * @see #getTrackInfo()
3264      * @see #selectTrack(int)
3265      * @see #deselectTrack(int)
3266      */
getSelectedTrack(int trackType)3267     public int getSelectedTrack(int trackType) throws IllegalStateException {
3268         if (mSubtitleController != null
3269                 && (trackType == TrackInfo.MEDIA_TRACK_TYPE_SUBTITLE
3270                 || trackType == TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT)) {
3271             SubtitleTrack subtitleTrack = mSubtitleController.getSelectedTrack();
3272             if (subtitleTrack != null) {
3273                 synchronized (mIndexTrackPairs) {
3274                     for (int i = 0; i < mIndexTrackPairs.size(); i++) {
3275                         Pair<Integer, SubtitleTrack> p = mIndexTrackPairs.get(i);
3276                         if (p.second == subtitleTrack && subtitleTrack.getTrackType() == trackType) {
3277                             return i;
3278                         }
3279                     }
3280                 }
3281             }
3282         }
3283 
3284         Parcel request = Parcel.obtain();
3285         Parcel reply = Parcel.obtain();
3286         try {
3287             request.writeInterfaceToken(IMEDIA_PLAYER);
3288             request.writeInt(INVOKE_ID_GET_SELECTED_TRACK);
3289             request.writeInt(trackType);
3290             invoke(request, reply);
3291             int inbandTrackIndex = reply.readInt();
3292             synchronized (mIndexTrackPairs) {
3293                 for (int i = 0; i < mIndexTrackPairs.size(); i++) {
3294                     Pair<Integer, SubtitleTrack> p = mIndexTrackPairs.get(i);
3295                     if (p.first != null && p.first == inbandTrackIndex) {
3296                         return i;
3297                     }
3298                 }
3299             }
3300             return -1;
3301         } finally {
3302             request.recycle();
3303             reply.recycle();
3304         }
3305     }
3306 
3307     /**
3308      * Selects a track.
3309      * <p>
3310      * If a MediaPlayer is in invalid state, it throws an IllegalStateException exception.
3311      * If a MediaPlayer is in <em>Started</em> state, the selected track is presented immediately.
3312      * If a MediaPlayer is not in Started state, it just marks the track to be played.
3313      * </p>
3314      * <p>
3315      * In any valid state, if it is called multiple times on the same type of track (ie. Video,
3316      * Audio, Timed Text), the most recent one will be chosen.
3317      * </p>
3318      * <p>
3319      * The first audio and video tracks are selected by default if available, even though
3320      * this method is not called. However, no timed text track will be selected until
3321      * this function is called.
3322      * </p>
3323      * <p>
3324      * Currently, only timed text, subtitle or audio tracks can be selected via this method.
3325      * In addition, the support for selecting an audio track at runtime is pretty limited
3326      * in that an audio track can only be selected in the <em>Prepared</em> state.
3327      * </p>
3328      * @param index the index of the track to be selected. The valid range of the index
3329      * is 0..total number of track - 1. The total number of tracks as well as the type of
3330      * each individual track can be found by calling {@link #getTrackInfo()} method.
3331      * @throws IllegalStateException if called in an invalid state.
3332      *
3333      * @see android.media.MediaPlayer#getTrackInfo
3334      */
selectTrack(int index)3335     public void selectTrack(int index) throws IllegalStateException {
3336         selectOrDeselectTrack(index, true /* select */);
3337     }
3338 
3339     /**
3340      * Deselect a track.
3341      * <p>
3342      * Currently, the track must be a timed text track and no audio or video tracks can be
3343      * deselected. If the timed text track identified by index has not been
3344      * selected before, it throws an exception.
3345      * </p>
3346      * @param index the index of the track to be deselected. The valid range of the index
3347      * is 0..total number of tracks - 1. The total number of tracks as well as the type of
3348      * each individual track can be found by calling {@link #getTrackInfo()} method.
3349      * @throws IllegalStateException if called in an invalid state.
3350      *
3351      * @see android.media.MediaPlayer#getTrackInfo
3352      */
deselectTrack(int index)3353     public void deselectTrack(int index) throws IllegalStateException {
3354         selectOrDeselectTrack(index, false /* select */);
3355     }
3356 
selectOrDeselectTrack(int index, boolean select)3357     private void selectOrDeselectTrack(int index, boolean select)
3358             throws IllegalStateException {
3359         // handle subtitle track through subtitle controller
3360         populateInbandTracks();
3361 
3362         Pair<Integer,SubtitleTrack> p = null;
3363         try {
3364             p = mIndexTrackPairs.get(index);
3365         } catch (ArrayIndexOutOfBoundsException e) {
3366             // ignore bad index
3367             return;
3368         }
3369 
3370         SubtitleTrack track = p.second;
3371         if (track == null) {
3372             // inband (de)select
3373             selectOrDeselectInbandTrack(p.first, select);
3374             return;
3375         }
3376 
3377         if (mSubtitleController == null) {
3378             return;
3379         }
3380 
3381         if (!select) {
3382             // out-of-band deselect
3383             if (mSubtitleController.getSelectedTrack() == track) {
3384                 mSubtitleController.selectTrack(null);
3385             } else {
3386                 Log.w(TAG, "trying to deselect track that was not selected");
3387             }
3388             return;
3389         }
3390 
3391         // out-of-band select
3392         if (track.getTrackType() == TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT) {
3393             int ttIndex = getSelectedTrack(TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT);
3394             synchronized (mIndexTrackPairs) {
3395                 if (ttIndex >= 0 && ttIndex < mIndexTrackPairs.size()) {
3396                     Pair<Integer,SubtitleTrack> p2 = mIndexTrackPairs.get(ttIndex);
3397                     if (p2.first != null && p2.second == null) {
3398                         // deselect inband counterpart
3399                         selectOrDeselectInbandTrack(p2.first, false);
3400                     }
3401                 }
3402             }
3403         }
3404         mSubtitleController.selectTrack(track);
3405     }
3406 
selectOrDeselectInbandTrack(int index, boolean select)3407     private void selectOrDeselectInbandTrack(int index, boolean select)
3408             throws IllegalStateException {
3409         Parcel request = Parcel.obtain();
3410         Parcel reply = Parcel.obtain();
3411         try {
3412             request.writeInterfaceToken(IMEDIA_PLAYER);
3413             request.writeInt(select? INVOKE_ID_SELECT_TRACK: INVOKE_ID_DESELECT_TRACK);
3414             request.writeInt(index);
3415             invoke(request, reply);
3416         } finally {
3417             request.recycle();
3418             reply.recycle();
3419         }
3420     }
3421 
3422 
3423     /**
3424      * @param reply Parcel with audio/video duration info for battery
3425                     tracking usage
3426      * @return The status code.
3427      * {@hide}
3428      */
native_pullBatteryData(Parcel reply)3429     public native static int native_pullBatteryData(Parcel reply);
3430 
3431     /**
3432      * Sets the target UDP re-transmit endpoint for the low level player.
3433      * Generally, the address portion of the endpoint is an IP multicast
3434      * address, although a unicast address would be equally valid.  When a valid
3435      * retransmit endpoint has been set, the media player will not decode and
3436      * render the media presentation locally.  Instead, the player will attempt
3437      * to re-multiplex its media data using the Android@Home RTP profile and
3438      * re-transmit to the target endpoint.  Receiver devices (which may be
3439      * either the same as the transmitting device or different devices) may
3440      * instantiate, prepare, and start a receiver player using a setDataSource
3441      * URL of the form...
3442      *
3443      * aahRX://&lt;multicastIP&gt;:&lt;port&gt;
3444      *
3445      * to receive, decode and render the re-transmitted content.
3446      *
3447      * setRetransmitEndpoint may only be called before setDataSource has been
3448      * called; while the player is in the Idle state.
3449      *
3450      * @param endpoint the address and UDP port of the re-transmission target or
3451      * null if no re-transmission is to be performed.
3452      * @throws IllegalStateException if it is called in an invalid state
3453      * @throws IllegalArgumentException if the retransmit endpoint is supplied,
3454      * but invalid.
3455      *
3456      * {@hide} pending API council
3457      */
3458     @UnsupportedAppUsage
setRetransmitEndpoint(InetSocketAddress endpoint)3459     public void setRetransmitEndpoint(InetSocketAddress endpoint)
3460             throws IllegalStateException, IllegalArgumentException
3461     {
3462         String addrString = null;
3463         int port = 0;
3464 
3465         if (null != endpoint) {
3466             addrString = endpoint.getAddress().getHostAddress();
3467             port = endpoint.getPort();
3468         }
3469 
3470         int ret = native_setRetransmitEndpoint(addrString, port);
3471         if (ret != 0) {
3472             throw new IllegalArgumentException("Illegal re-transmit endpoint; native ret " + ret);
3473         }
3474     }
3475 
native_setRetransmitEndpoint(String addrString, int port)3476     private native final int native_setRetransmitEndpoint(String addrString, int port);
3477 
3478     @Override
finalize()3479     protected void finalize() {
3480         tryToDisableNativeRoutingCallback();
3481         baseRelease();
3482         native_finalize();
3483     }
3484 
3485     /* Do not change these values without updating their counterparts
3486      * in include/media/mediaplayer.h!
3487      */
3488     private static final int MEDIA_NOP = 0; // interface test message
3489     private static final int MEDIA_PREPARED = 1;
3490     private static final int MEDIA_PLAYBACK_COMPLETE = 2;
3491     private static final int MEDIA_BUFFERING_UPDATE = 3;
3492     private static final int MEDIA_SEEK_COMPLETE = 4;
3493     private static final int MEDIA_SET_VIDEO_SIZE = 5;
3494     private static final int MEDIA_STARTED = 6;
3495     private static final int MEDIA_PAUSED = 7;
3496     private static final int MEDIA_STOPPED = 8;
3497     private static final int MEDIA_SKIPPED = 9;
3498     private static final int MEDIA_NOTIFY_TIME = 98;
3499     private static final int MEDIA_TIMED_TEXT = 99;
3500     private static final int MEDIA_ERROR = 100;
3501     private static final int MEDIA_INFO = 200;
3502     private static final int MEDIA_SUBTITLE_DATA = 201;
3503     private static final int MEDIA_META_DATA = 202;
3504     private static final int MEDIA_DRM_INFO = 210;
3505     private static final int MEDIA_TIME_DISCONTINUITY = 211;
3506     private static final int MEDIA_RTP_RX_NOTICE = 300;
3507     private static final int MEDIA_AUDIO_ROUTING_CHANGED = 10000;
3508 
3509     private TimeProvider mTimeProvider;
3510     private final Object mTimeProviderLock = new Object();
3511 
3512     /** @hide */
3513     @UnsupportedAppUsage
getMediaTimeProvider()3514     public MediaTimeProvider getMediaTimeProvider() {
3515         synchronized (mTimeProviderLock) {
3516             if (mTimeProvider == null) {
3517                 mTimeProvider = new TimeProvider(this);
3518             }
3519             return mTimeProvider;
3520         }
3521     }
3522 
3523     private class EventHandler extends Handler
3524     {
3525         private MediaPlayer mMediaPlayer;
3526 
EventHandler(MediaPlayer mp, Looper looper)3527         public EventHandler(MediaPlayer mp, Looper looper) {
3528             super(looper);
3529             mMediaPlayer = mp;
3530         }
3531 
3532         @Override
handleMessage(Message msg)3533         public void handleMessage(Message msg) {
3534             if (mMediaPlayer.mNativeContext == 0) {
3535                 Log.w(TAG, "mediaplayer went away with unhandled events");
3536                 return;
3537             }
3538             switch(msg.what) {
3539             case MEDIA_PREPARED:
3540                 try {
3541                     scanInternalSubtitleTracks();
3542                 } catch (RuntimeException e) {
3543                     // send error message instead of crashing;
3544                     // send error message instead of inlining a call to onError
3545                     // to avoid code duplication.
3546                     Message msg2 = obtainMessage(
3547                             MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, MEDIA_ERROR_UNSUPPORTED, null);
3548                     sendMessage(msg2);
3549                 }
3550 
3551                 OnPreparedListener onPreparedListener = mOnPreparedListener;
3552                 if (onPreparedListener != null)
3553                     onPreparedListener.onPrepared(mMediaPlayer);
3554                 return;
3555 
3556             case MEDIA_DRM_INFO:
3557                 Log.v(TAG, "MEDIA_DRM_INFO " + mOnDrmInfoHandlerDelegate);
3558 
3559                 if (msg.obj == null) {
3560                     Log.w(TAG, "MEDIA_DRM_INFO msg.obj=NULL");
3561                 } else if (msg.obj instanceof Parcel) {
3562                     // The parcel was parsed already in postEventFromNative
3563                     DrmInfo drmInfo = null;
3564 
3565                     OnDrmInfoHandlerDelegate onDrmInfoHandlerDelegate;
3566                     synchronized (mDrmLock) {
3567                         if (mOnDrmInfoHandlerDelegate != null && mDrmInfo != null) {
3568                             drmInfo = mDrmInfo.makeCopy();
3569                         }
3570                         // local copy while keeping the lock
3571                         onDrmInfoHandlerDelegate = mOnDrmInfoHandlerDelegate;
3572                     }
3573 
3574                     // notifying the client outside the lock
3575                     if (onDrmInfoHandlerDelegate != null) {
3576                         onDrmInfoHandlerDelegate.notifyClient(drmInfo);
3577                     }
3578                 } else {
3579                     Log.w(TAG, "MEDIA_DRM_INFO msg.obj of unexpected type " + msg.obj);
3580                 }
3581                 return;
3582 
3583             case MEDIA_PLAYBACK_COMPLETE:
3584                 {
3585                     mOnCompletionInternalListener.onCompletion(mMediaPlayer);
3586                     OnCompletionListener onCompletionListener = mOnCompletionListener;
3587                     if (onCompletionListener != null)
3588                         onCompletionListener.onCompletion(mMediaPlayer);
3589                 }
3590                 stayAwake(false);
3591                 return;
3592 
3593             case MEDIA_STOPPED:
3594                 {
3595                     TimeProvider timeProvider = mTimeProvider;
3596                     if (timeProvider != null) {
3597                         timeProvider.onStopped();
3598                     }
3599                 }
3600                 break;
3601 
3602             case MEDIA_STARTED:
3603                 // fall through
3604             case MEDIA_PAUSED:
3605                 {
3606                     TimeProvider timeProvider = mTimeProvider;
3607                     if (timeProvider != null) {
3608                         timeProvider.onPaused(msg.what == MEDIA_PAUSED);
3609                     }
3610                 }
3611                 break;
3612 
3613             case MEDIA_BUFFERING_UPDATE:
3614                 OnBufferingUpdateListener onBufferingUpdateListener = mOnBufferingUpdateListener;
3615                 if (onBufferingUpdateListener != null)
3616                     onBufferingUpdateListener.onBufferingUpdate(mMediaPlayer, msg.arg1);
3617                 return;
3618 
3619             case MEDIA_SEEK_COMPLETE:
3620                 OnSeekCompleteListener onSeekCompleteListener = mOnSeekCompleteListener;
3621                 if (onSeekCompleteListener != null) {
3622                     onSeekCompleteListener.onSeekComplete(mMediaPlayer);
3623                 }
3624                 // fall through
3625 
3626             case MEDIA_SKIPPED:
3627                 {
3628                     TimeProvider timeProvider = mTimeProvider;
3629                     if (timeProvider != null) {
3630                         timeProvider.onSeekComplete(mMediaPlayer);
3631                     }
3632                 }
3633                 return;
3634 
3635             case MEDIA_SET_VIDEO_SIZE:
3636                 OnVideoSizeChangedListener onVideoSizeChangedListener = mOnVideoSizeChangedListener;
3637                 if (onVideoSizeChangedListener != null) {
3638                     onVideoSizeChangedListener.onVideoSizeChanged(
3639                         mMediaPlayer, msg.arg1, msg.arg2);
3640                 }
3641                 return;
3642 
3643             case MEDIA_ERROR:
3644                 Log.e(TAG, "Error (" + msg.arg1 + "," + msg.arg2 + ")");
3645                 boolean error_was_handled = false;
3646                 OnErrorListener onErrorListener = mOnErrorListener;
3647                 if (onErrorListener != null) {
3648                     error_was_handled = onErrorListener.onError(mMediaPlayer, msg.arg1, msg.arg2);
3649                 }
3650                 {
3651                     mOnCompletionInternalListener.onCompletion(mMediaPlayer);
3652                     OnCompletionListener onCompletionListener = mOnCompletionListener;
3653                     if (onCompletionListener != null && ! error_was_handled) {
3654                         onCompletionListener.onCompletion(mMediaPlayer);
3655                     }
3656                 }
3657                 stayAwake(false);
3658                 return;
3659 
3660             case MEDIA_INFO:
3661                 switch (msg.arg1) {
3662                 case MEDIA_INFO_VIDEO_TRACK_LAGGING:
3663                     Log.i(TAG, "Info (" + msg.arg1 + "," + msg.arg2 + ")");
3664                     break;
3665                 case MEDIA_INFO_METADATA_UPDATE:
3666                     try {
3667                         scanInternalSubtitleTracks();
3668                     } catch (RuntimeException e) {
3669                         Message msg2 = obtainMessage(
3670                                 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, MEDIA_ERROR_UNSUPPORTED, null);
3671                         sendMessage(msg2);
3672                     }
3673                     // fall through
3674 
3675                 case MEDIA_INFO_EXTERNAL_METADATA_UPDATE:
3676                     msg.arg1 = MEDIA_INFO_METADATA_UPDATE;
3677                     // update default track selection
3678                     if (mSubtitleController != null) {
3679                         mSubtitleController.selectDefaultTrack();
3680                     }
3681                     break;
3682                 case MEDIA_INFO_BUFFERING_START:
3683                 case MEDIA_INFO_BUFFERING_END:
3684                     TimeProvider timeProvider = mTimeProvider;
3685                     if (timeProvider != null) {
3686                         timeProvider.onBuffering(msg.arg1 == MEDIA_INFO_BUFFERING_START);
3687                     }
3688                     break;
3689                 }
3690 
3691                 OnInfoListener onInfoListener = mOnInfoListener;
3692                 if (onInfoListener != null) {
3693                     onInfoListener.onInfo(mMediaPlayer, msg.arg1, msg.arg2);
3694                 }
3695                 // No real default action so far.
3696                 return;
3697 
3698             case MEDIA_NOTIFY_TIME:
3699                     TimeProvider timeProvider = mTimeProvider;
3700                     if (timeProvider != null) {
3701                         timeProvider.onNotifyTime();
3702                     }
3703                 return;
3704 
3705             case MEDIA_TIMED_TEXT:
3706                 OnTimedTextListener onTimedTextListener = mOnTimedTextListener;
3707                 if (onTimedTextListener == null)
3708                     return;
3709                 if (msg.obj == null) {
3710                     onTimedTextListener.onTimedText(mMediaPlayer, null);
3711                 } else {
3712                     if (msg.obj instanceof Parcel) {
3713                         Parcel parcel = (Parcel)msg.obj;
3714                         TimedText text = new TimedText(parcel);
3715                         parcel.recycle();
3716                         onTimedTextListener.onTimedText(mMediaPlayer, text);
3717                     }
3718                 }
3719                 return;
3720 
3721             case MEDIA_SUBTITLE_DATA:
3722                 final OnSubtitleDataListener extSubtitleListener;
3723                 final Handler extSubtitleHandler;
3724                 synchronized(this) {
3725                     if (mSubtitleDataListenerDisabled) {
3726                         return;
3727                     }
3728                     extSubtitleListener = mExtSubtitleDataListener;
3729                     extSubtitleHandler = mExtSubtitleDataHandler;
3730                 }
3731                 if (msg.obj instanceof Parcel) {
3732                     Parcel parcel = (Parcel) msg.obj;
3733                     final SubtitleData data = new SubtitleData(parcel);
3734                     parcel.recycle();
3735 
3736                     mIntSubtitleDataListener.onSubtitleData(mMediaPlayer, data);
3737 
3738                     if (extSubtitleListener != null) {
3739                         if (extSubtitleHandler == null) {
3740                             extSubtitleListener.onSubtitleData(mMediaPlayer, data);
3741                         } else {
3742                             extSubtitleHandler.post(new Runnable() {
3743                                 @Override
3744                                 public void run() {
3745                                     extSubtitleListener.onSubtitleData(mMediaPlayer, data);
3746                                 }
3747                             });
3748                         }
3749                     }
3750                 }
3751                 return;
3752 
3753             case MEDIA_META_DATA:
3754                 OnTimedMetaDataAvailableListener onTimedMetaDataAvailableListener =
3755                     mOnTimedMetaDataAvailableListener;
3756                 if (onTimedMetaDataAvailableListener == null) {
3757                     return;
3758                 }
3759                 if (msg.obj instanceof Parcel) {
3760                     Parcel parcel = (Parcel) msg.obj;
3761                     TimedMetaData data = TimedMetaData.createTimedMetaDataFromParcel(parcel);
3762                     parcel.recycle();
3763                     onTimedMetaDataAvailableListener.onTimedMetaDataAvailable(mMediaPlayer, data);
3764                 }
3765                 return;
3766 
3767             case MEDIA_NOP: // interface test message - ignore
3768                 break;
3769 
3770             case MEDIA_AUDIO_ROUTING_CHANGED:
3771                     broadcastRoutingChange();
3772                     return;
3773 
3774             case MEDIA_TIME_DISCONTINUITY:
3775                 final OnMediaTimeDiscontinuityListener mediaTimeListener;
3776                 final Handler mediaTimeHandler;
3777                 synchronized(this) {
3778                     mediaTimeListener = mOnMediaTimeDiscontinuityListener;
3779                     mediaTimeHandler = mOnMediaTimeDiscontinuityHandler;
3780                 }
3781                 if (mediaTimeListener == null) {
3782                     return;
3783                 }
3784                 if (msg.obj instanceof Parcel) {
3785                     Parcel parcel = (Parcel) msg.obj;
3786                     parcel.setDataPosition(0);
3787                     long anchorMediaUs = parcel.readLong();
3788                     long anchorRealUs = parcel.readLong();
3789                     float playbackRate = parcel.readFloat();
3790                     parcel.recycle();
3791                     final MediaTimestamp timestamp;
3792                     if (anchorMediaUs != -1 && anchorRealUs != -1) {
3793                         timestamp = new MediaTimestamp(
3794                                 anchorMediaUs /*Us*/, anchorRealUs * 1000 /*Ns*/, playbackRate);
3795                     } else {
3796                         timestamp = MediaTimestamp.TIMESTAMP_UNKNOWN;
3797                     }
3798                     if (mediaTimeHandler == null) {
3799                         mediaTimeListener.onMediaTimeDiscontinuity(mMediaPlayer, timestamp);
3800                     } else {
3801                         mediaTimeHandler.post(new Runnable() {
3802                             @Override
3803                             public void run() {
3804                                 mediaTimeListener.onMediaTimeDiscontinuity(mMediaPlayer, timestamp);
3805                             }
3806                         });
3807                     }
3808                 }
3809                 return;
3810 
3811             case MEDIA_RTP_RX_NOTICE:
3812                 final OnRtpRxNoticeListener rtpRxNoticeListener = mOnRtpRxNoticeListener;
3813                 if (rtpRxNoticeListener == null) {
3814                     return;
3815                 }
3816                 if (msg.obj instanceof Parcel) {
3817                     Parcel parcel = (Parcel) msg.obj;
3818                     parcel.setDataPosition(0);
3819                     int noticeType;
3820                     int[] data;
3821                     try {
3822                         noticeType = parcel.readInt();
3823                         int numOfArgs = parcel.dataAvail() / 4;
3824                         data = new int[numOfArgs];
3825                         for (int i = 0; i < numOfArgs; i++) {
3826                             data[i] = parcel.readInt();
3827                         }
3828                     } finally {
3829                         parcel.recycle();
3830                     }
3831                     mOnRtpRxNoticeExecutor.execute(() ->
3832                             rtpRxNoticeListener
3833                                     .onRtpRxNotice(mMediaPlayer, noticeType, data));
3834                 }
3835                 return;
3836 
3837             default:
3838                 Log.e(TAG, "Unknown message type " + msg.what);
3839                 return;
3840             }
3841         }
3842     }
3843 
3844     /*
3845      * Called from native code when an interesting event happens.  This method
3846      * just uses the EventHandler system to post the event back to the main app thread.
3847      * We use a weak reference to the original MediaPlayer object so that the native
3848      * code is safe from the object disappearing from underneath it.  (This is
3849      * the cookie passed to native_setup().)
3850      */
postEventFromNative(Object mediaplayer_ref, int what, int arg1, int arg2, Object obj)3851     private static void postEventFromNative(Object mediaplayer_ref,
3852                                             int what, int arg1, int arg2, Object obj)
3853     {
3854         final MediaPlayer mp = (MediaPlayer)((WeakReference)mediaplayer_ref).get();
3855         if (mp == null) {
3856             return;
3857         }
3858 
3859         switch (what) {
3860         case MEDIA_INFO:
3861             if (arg1 == MEDIA_INFO_STARTED_AS_NEXT) {
3862                 new Thread(new Runnable() {
3863                     @Override
3864                     public void run() {
3865                         // this acquires the wakelock if needed, and sets the client side state
3866                         mp.start();
3867                     }
3868                 }).start();
3869                 Thread.yield();
3870             }
3871             break;
3872 
3873         case MEDIA_DRM_INFO:
3874             // We need to derive mDrmInfo before prepare() returns so processing it here
3875             // before the notification is sent to EventHandler below. EventHandler runs in the
3876             // notification looper so its handleMessage might process the event after prepare()
3877             // has returned.
3878             Log.v(TAG, "postEventFromNative MEDIA_DRM_INFO");
3879             if (obj instanceof Parcel) {
3880                 Parcel parcel = (Parcel)obj;
3881                 DrmInfo drmInfo = new DrmInfo(parcel);
3882                 synchronized (mp.mDrmLock) {
3883                     mp.mDrmInfo = drmInfo;
3884                 }
3885             } else {
3886                 Log.w(TAG, "MEDIA_DRM_INFO msg.obj of unexpected type " + obj);
3887             }
3888             break;
3889 
3890         case MEDIA_PREPARED:
3891             // By this time, we've learned about DrmInfo's presence or absence. This is meant
3892             // mainly for prepareAsync() use case. For prepare(), this still can run to a race
3893             // condition b/c MediaPlayerNative releases the prepare() lock before calling notify
3894             // so we also set mDrmInfoResolved in prepare().
3895             synchronized (mp.mDrmLock) {
3896                 mp.mDrmInfoResolved = true;
3897             }
3898             break;
3899 
3900         }
3901 
3902         if (mp.mEventHandler != null) {
3903             Message m = mp.mEventHandler.obtainMessage(what, arg1, arg2, obj);
3904             mp.mEventHandler.sendMessage(m);
3905         }
3906     }
3907 
3908     /**
3909      * Interface definition for a callback to be invoked when the media
3910      * source is ready for playback.
3911      */
3912     public interface OnPreparedListener
3913     {
3914         /**
3915          * Called when the media file is ready for playback.
3916          *
3917          * @param mp the MediaPlayer that is ready for playback
3918          */
onPrepared(MediaPlayer mp)3919         void onPrepared(MediaPlayer mp);
3920     }
3921 
3922     /**
3923      * Register a callback to be invoked when the media source is ready
3924      * for playback.
3925      *
3926      * @param listener the callback that will be run
3927      */
setOnPreparedListener(OnPreparedListener listener)3928     public void setOnPreparedListener(OnPreparedListener listener)
3929     {
3930         mOnPreparedListener = listener;
3931     }
3932 
3933     @UnsupportedAppUsage
3934     private OnPreparedListener mOnPreparedListener;
3935 
3936     /**
3937      * Interface definition for a callback to be invoked when playback of
3938      * a media source has completed.
3939      */
3940     public interface OnCompletionListener
3941     {
3942         /**
3943          * Called when the end of a media source is reached during playback.
3944          *
3945          * @param mp the MediaPlayer that reached the end of the file
3946          */
onCompletion(MediaPlayer mp)3947         void onCompletion(MediaPlayer mp);
3948     }
3949 
3950     /**
3951      * Register a callback to be invoked when the end of a media source
3952      * has been reached during playback.
3953      *
3954      * @param listener the callback that will be run
3955      */
setOnCompletionListener(OnCompletionListener listener)3956     public void setOnCompletionListener(OnCompletionListener listener)
3957     {
3958         mOnCompletionListener = listener;
3959     }
3960 
3961     @UnsupportedAppUsage
3962     private OnCompletionListener mOnCompletionListener;
3963 
3964     /**
3965      * @hide
3966      * Internal completion listener to update PlayerBase of the play state. Always "registered".
3967      */
3968     private final OnCompletionListener mOnCompletionInternalListener = new OnCompletionListener() {
3969         @Override
3970         public void onCompletion(MediaPlayer mp) {
3971             tryToDisableNativeRoutingCallback();
3972             baseStop();
3973         }
3974     };
3975 
3976     /**
3977      * Interface definition of a callback to be invoked indicating buffering
3978      * status of a media resource being streamed over the network.
3979      */
3980     public interface OnBufferingUpdateListener
3981     {
3982         /**
3983          * Called to update status in buffering a media stream received through
3984          * progressive HTTP download. The received buffering percentage
3985          * indicates how much of the content has been buffered or played.
3986          * For example a buffering update of 80 percent when half the content
3987          * has already been played indicates that the next 30 percent of the
3988          * content to play has been buffered.
3989          *
3990          * @param mp      the MediaPlayer the update pertains to
3991          * @param percent the percentage (0-100) of the content
3992          *                that has been buffered or played thus far
3993          */
onBufferingUpdate(MediaPlayer mp, int percent)3994         void onBufferingUpdate(MediaPlayer mp, int percent);
3995     }
3996 
3997     /**
3998      * Register a callback to be invoked when the status of a network
3999      * stream's buffer has changed.
4000      *
4001      * @param listener the callback that will be run.
4002      */
setOnBufferingUpdateListener(OnBufferingUpdateListener listener)4003     public void setOnBufferingUpdateListener(OnBufferingUpdateListener listener)
4004     {
4005         mOnBufferingUpdateListener = listener;
4006     }
4007 
4008     private OnBufferingUpdateListener mOnBufferingUpdateListener;
4009 
4010     /**
4011      * Interface definition of a callback to be invoked indicating
4012      * the completion of a seek operation.
4013      */
4014     public interface OnSeekCompleteListener
4015     {
4016         /**
4017          * Called to indicate the completion of a seek operation.
4018          *
4019          * @param mp the MediaPlayer that issued the seek operation
4020          */
onSeekComplete(MediaPlayer mp)4021         public void onSeekComplete(MediaPlayer mp);
4022     }
4023 
4024     /**
4025      * Register a callback to be invoked when a seek operation has been
4026      * completed.
4027      *
4028      * @param listener the callback that will be run
4029      */
setOnSeekCompleteListener(OnSeekCompleteListener listener)4030     public void setOnSeekCompleteListener(OnSeekCompleteListener listener)
4031     {
4032         mOnSeekCompleteListener = listener;
4033     }
4034 
4035     @UnsupportedAppUsage
4036     private OnSeekCompleteListener mOnSeekCompleteListener;
4037 
4038     /**
4039      * Interface definition of a callback to be invoked when the
4040      * video size is first known or updated
4041      */
4042     public interface OnVideoSizeChangedListener
4043     {
4044         /**
4045          * Called to indicate the video size
4046          *
4047          * The video size (width and height) could be 0 if there was no video,
4048          * no display surface was set, or the value was not determined yet.
4049          *
4050          * @param mp        the MediaPlayer associated with this callback
4051          * @param width     the width of the video
4052          * @param height    the height of the video
4053          */
onVideoSizeChanged(MediaPlayer mp, int width, int height)4054         public void onVideoSizeChanged(MediaPlayer mp, int width, int height);
4055     }
4056 
4057     /**
4058      * Register a callback to be invoked when the video size is
4059      * known or updated.
4060      *
4061      * @param listener the callback that will be run
4062      */
setOnVideoSizeChangedListener(OnVideoSizeChangedListener listener)4063     public void setOnVideoSizeChangedListener(OnVideoSizeChangedListener listener)
4064     {
4065         mOnVideoSizeChangedListener = listener;
4066     }
4067 
4068     private OnVideoSizeChangedListener mOnVideoSizeChangedListener;
4069 
4070     /**
4071      * Interface definition of a callback to be invoked when a
4072      * timed text is available for display.
4073      */
4074     public interface OnTimedTextListener
4075     {
4076         /**
4077          * Called to indicate an avaliable timed text
4078          *
4079          * @param mp             the MediaPlayer associated with this callback
4080          * @param text           the timed text sample which contains the text
4081          *                       needed to be displayed and the display format.
4082          */
onTimedText(MediaPlayer mp, TimedText text)4083         public void onTimedText(MediaPlayer mp, TimedText text);
4084     }
4085 
4086     /**
4087      * Register a callback to be invoked when a timed text is available
4088      * for display.
4089      *
4090      * @param listener the callback that will be run
4091      */
setOnTimedTextListener(OnTimedTextListener listener)4092     public void setOnTimedTextListener(OnTimedTextListener listener)
4093     {
4094         mOnTimedTextListener = listener;
4095     }
4096 
4097     @UnsupportedAppUsage
4098     private OnTimedTextListener mOnTimedTextListener;
4099 
4100     /**
4101      * Interface definition of a callback to be invoked when a player subtitle track has new
4102      * subtitle data available.
4103      * See the {@link MediaPlayer#setOnSubtitleDataListener(OnSubtitleDataListener, Handler)}
4104      * method for the description of which track will report data through this listener.
4105      */
4106     public interface OnSubtitleDataListener {
4107         /**
4108          * Method called when new subtitle data is available
4109          * @param mp the player that reports the new subtitle data
4110          * @param data the subtitle data
4111          */
onSubtitleData(@onNull MediaPlayer mp, @NonNull SubtitleData data)4112         public void onSubtitleData(@NonNull MediaPlayer mp, @NonNull SubtitleData data);
4113     }
4114 
4115     /**
4116      * Sets the listener to be invoked when a subtitle track has new data available.
4117      * The subtitle data comes from a subtitle track previously selected with
4118      * {@link #selectTrack(int)}. Use {@link #getTrackInfo()} to determine which tracks are
4119      * subtitles (of type {@link TrackInfo#MEDIA_TRACK_TYPE_SUBTITLE}), Subtitle track encodings
4120      * can be determined by {@link TrackInfo#getFormat()}).<br>
4121      * See {@link SubtitleData} for an example of querying subtitle encoding.
4122      * @param listener the listener called when new data is available
4123      * @param handler the {@link Handler} that receives the listener events
4124      */
setOnSubtitleDataListener(@onNull OnSubtitleDataListener listener, @NonNull Handler handler)4125     public void setOnSubtitleDataListener(@NonNull OnSubtitleDataListener listener,
4126             @NonNull Handler handler) {
4127         if (listener == null) {
4128             throw new IllegalArgumentException("Illegal null listener");
4129         }
4130         if (handler == null) {
4131             throw new IllegalArgumentException("Illegal null handler");
4132         }
4133         setOnSubtitleDataListenerInt(listener, handler);
4134     }
4135     /**
4136      * Sets the listener to be invoked when a subtitle track has new data available.
4137      * The subtitle data comes from a subtitle track previously selected with
4138      * {@link #selectTrack(int)}. Use {@link #getTrackInfo()} to determine which tracks are
4139      * subtitles (of type {@link TrackInfo#MEDIA_TRACK_TYPE_SUBTITLE}), Subtitle track encodings
4140      * can be determined by {@link TrackInfo#getFormat()}).<br>
4141      * See {@link SubtitleData} for an example of querying subtitle encoding.<br>
4142      * The listener will be called on the same thread as the one in which the MediaPlayer was
4143      * created.
4144      * @param listener the listener called when new data is available
4145      */
setOnSubtitleDataListener(@onNull OnSubtitleDataListener listener)4146     public void setOnSubtitleDataListener(@NonNull OnSubtitleDataListener listener)
4147     {
4148         if (listener == null) {
4149             throw new IllegalArgumentException("Illegal null listener");
4150         }
4151         setOnSubtitleDataListenerInt(listener, null);
4152     }
4153 
4154     /**
4155      * Clears the listener previously set with
4156      * {@link #setOnSubtitleDataListener(OnSubtitleDataListener)} or
4157      * {@link #setOnSubtitleDataListener(OnSubtitleDataListener, Handler)}.
4158      */
clearOnSubtitleDataListener()4159     public void clearOnSubtitleDataListener() {
4160         setOnSubtitleDataListenerInt(null, null);
4161     }
4162 
setOnSubtitleDataListenerInt( @ullable OnSubtitleDataListener listener, @Nullable Handler handler)4163     private void setOnSubtitleDataListenerInt(
4164             @Nullable OnSubtitleDataListener listener, @Nullable Handler handler) {
4165         synchronized (this) {
4166             mExtSubtitleDataListener = listener;
4167             mExtSubtitleDataHandler = handler;
4168         }
4169     }
4170 
4171     private boolean mSubtitleDataListenerDisabled;
4172     /** External OnSubtitleDataListener, the one set by {@link #setOnSubtitleDataListener}. */
4173     private OnSubtitleDataListener mExtSubtitleDataListener;
4174     private Handler mExtSubtitleDataHandler;
4175 
4176     /**
4177      * Interface definition of a callback to be invoked when discontinuity in the normal progression
4178      * of the media time is detected.
4179      * The "normal progression" of media time is defined as the expected increase of the playback
4180      * position when playing media, relative to the playback speed (for instance every second, media
4181      * time increases by two seconds when playing at 2x).<br>
4182      * Discontinuities are encountered in the following cases:
4183      * <ul>
4184      * <li>when the player is starved for data and cannot play anymore</li>
4185      * <li>when the player encounters a playback error</li>
4186      * <li>when the a seek operation starts, and when it's completed</li>
4187      * <li>when the playback speed changes</li>
4188      * <li>when the playback state changes</li>
4189      * <li>when the player is reset</li>
4190      * </ul>
4191      * See the
4192      * {@link MediaPlayer#setOnMediaTimeDiscontinuityListener(OnMediaTimeDiscontinuityListener, Handler)}
4193      * method to set a listener for these events.
4194      */
4195     public interface OnMediaTimeDiscontinuityListener {
4196         /**
4197          * Called to indicate a time discontinuity has occured.
4198          * @param mp the MediaPlayer for which the discontinuity has occured.
4199          * @param mts the timestamp that correlates media time, system time and clock rate,
4200          *     or {@link MediaTimestamp#TIMESTAMP_UNKNOWN} in an error case.
4201          */
onMediaTimeDiscontinuity(@onNull MediaPlayer mp, @NonNull MediaTimestamp mts)4202         public void onMediaTimeDiscontinuity(@NonNull MediaPlayer mp, @NonNull MediaTimestamp mts);
4203     }
4204 
4205     /**
4206      * Sets the listener to be invoked when a media time discontinuity is encountered.
4207      * @param listener the listener called after a discontinuity
4208      * @param handler the {@link Handler} that receives the listener events
4209      */
setOnMediaTimeDiscontinuityListener( @onNull OnMediaTimeDiscontinuityListener listener, @NonNull Handler handler)4210     public void setOnMediaTimeDiscontinuityListener(
4211             @NonNull OnMediaTimeDiscontinuityListener listener, @NonNull Handler handler) {
4212         if (listener == null) {
4213             throw new IllegalArgumentException("Illegal null listener");
4214         }
4215         if (handler == null) {
4216             throw new IllegalArgumentException("Illegal null handler");
4217         }
4218         setOnMediaTimeDiscontinuityListenerInt(listener, handler);
4219     }
4220 
4221     /**
4222      * Sets the listener to be invoked when a media time discontinuity is encountered.
4223      * The listener will be called on the same thread as the one in which the MediaPlayer was
4224      * created.
4225      * @param listener the listener called after a discontinuity
4226      */
setOnMediaTimeDiscontinuityListener( @onNull OnMediaTimeDiscontinuityListener listener)4227     public void setOnMediaTimeDiscontinuityListener(
4228             @NonNull OnMediaTimeDiscontinuityListener listener)
4229     {
4230         if (listener == null) {
4231             throw new IllegalArgumentException("Illegal null listener");
4232         }
4233         setOnMediaTimeDiscontinuityListenerInt(listener, null);
4234     }
4235 
4236     /**
4237      * Clears the listener previously set with
4238      * {@link #setOnMediaTimeDiscontinuityListener(OnMediaTimeDiscontinuityListener)}
4239      * or {@link #setOnMediaTimeDiscontinuityListener(OnMediaTimeDiscontinuityListener, Handler)}
4240      */
clearOnMediaTimeDiscontinuityListener()4241     public void clearOnMediaTimeDiscontinuityListener() {
4242         setOnMediaTimeDiscontinuityListenerInt(null, null);
4243     }
4244 
setOnMediaTimeDiscontinuityListenerInt( @ullable OnMediaTimeDiscontinuityListener listener, @Nullable Handler handler)4245     private void setOnMediaTimeDiscontinuityListenerInt(
4246             @Nullable OnMediaTimeDiscontinuityListener listener, @Nullable Handler handler) {
4247         synchronized (this) {
4248             mOnMediaTimeDiscontinuityListener = listener;
4249             mOnMediaTimeDiscontinuityHandler = handler;
4250         }
4251     }
4252 
4253     private OnMediaTimeDiscontinuityListener mOnMediaTimeDiscontinuityListener;
4254     private Handler mOnMediaTimeDiscontinuityHandler;
4255 
4256     /**
4257      * Interface definition of a callback to be invoked when a
4258      * track has timed metadata available.
4259      *
4260      * @see MediaPlayer#setOnTimedMetaDataAvailableListener(OnTimedMetaDataAvailableListener)
4261      */
4262     public interface OnTimedMetaDataAvailableListener
4263     {
4264         /**
4265          * Called to indicate avaliable timed metadata
4266          * <p>
4267          * This method will be called as timed metadata is extracted from the media,
4268          * in the same order as it occurs in the media. The timing of this event is
4269          * not controlled by the associated timestamp.
4270          *
4271          * @param mp             the MediaPlayer associated with this callback
4272          * @param data           the timed metadata sample associated with this event
4273          */
onTimedMetaDataAvailable(MediaPlayer mp, TimedMetaData data)4274         public void onTimedMetaDataAvailable(MediaPlayer mp, TimedMetaData data);
4275     }
4276 
4277     /**
4278      * Interface definition of a callback to be invoked when
4279      * RTP Rx connection has a notice.
4280      *
4281      * @see #setOnRtpRxNoticeListener
4282      *
4283      * @hide
4284      */
4285     @SystemApi
4286     public interface OnRtpRxNoticeListener
4287     {
4288         /**
4289          * Called when an RTP Rx connection has a notice.
4290          * <p>
4291          * Basic format. All TYPE and ARG are 4 bytes unsigned integer in native byte order.
4292          * <pre>{@code
4293          * 0                4               8                12
4294          * +----------------+---------------+----------------+----------------+
4295          * |      TYPE      |      ARG1     |      ARG2      |      ARG3      |
4296          * +----------------+---------------+----------------+----------------+
4297          * |      ARG4      |      ARG5     |      ...
4298          * +----------------+---------------+-------------
4299          * 16               20              24
4300          *
4301          *
4302          * TYPE 100 - A notice of the first rtp packet received. No ARGs.
4303          * 0
4304          * +----------------+
4305          * |      100       |
4306          * +----------------+
4307          *
4308          *
4309          * TYPE 101 - A notice of the first rtcp packet received. No ARGs.
4310          * 0
4311          * +----------------+
4312          * |      101       |
4313          * +----------------+
4314          *
4315          *
4316          * TYPE 102 - A periodic report of a RTP statistics.
4317          * TYPE 103 - An emergency report when serious packet loss has been detected
4318          *            in between TYPE 102 events.
4319          * 0                4               8                12
4320          * +----------------+---------------+----------------+----------------+
4321          * |   102 or 103   |   FB type=0   |    Bitrate     |   Top #.Seq    |
4322          * +----------------+---------------+----------------+----------------+
4323          * |   Base #.Seq   |Prev Expt #.Pkt|   Recv #.Pkt   |Prev Recv #.Pkt |
4324          * +----------------+---------------+----------------+----------------+
4325          * Feedback (FB) type
4326          *      - always 0.
4327          * Bitrate
4328          *      - amount of data received in this period.
4329          * Top number of sequence
4330          *      - highest RTP sequence number received in this period.
4331          *      - monotonically increasing value.
4332          * Base number of sequence
4333          *      - the first RTP sequence number of the media stream.
4334          * Previous Expected number of Packets
4335          *      - expected count of packets received in the previous report.
4336          * Received number of packet
4337          *      - actual count of packets received in this report.
4338          * Previous Received number of packet
4339          *      - actual count of packets received in the previous report.
4340          *
4341          *
4342          * TYPE 205 - Transport layer Feedback message. (RFC-5104 Sec.4.2)
4343          * 0                4               8                12
4344          * +----------------+---------------+----------------+----------------+
4345          * |      205       |FB type(1 or 3)|      SSRC      |      Value     |
4346          * +----------------+---------------+----------------+----------------+
4347          * Feedback (FB) type: determines the type of the event.
4348          *      - if 1, we received a NACK request from the remote side.
4349          *      - if 3, we received a TMMBR (Temporary Maximum Media Stream Bit Rate Request) from
4350          *        the remote side.
4351          * SSRC
4352          *      - Remote side's SSRC value of the media sender (RFC-3550 Sec.5.1)
4353          * Value: the FCI (Feedback Control Information) depending on the value of FB type
4354          *      - if FB type is 1, the Generic NACK as specified in RFC-4585 Sec.6.2.1
4355          *      - if FB type is 3, the TMMBR as specified in RFC-5104 Sec.4.2.1.1
4356          *
4357          *
4358          * TYPE 206 - Payload-specific Feedback message. (RFC-5104 Sec.4.3)
4359          * 0                4               8
4360          * +----------------+---------------+----------------+
4361          * |      206       |FB type(1 or 4)|      SSRC      |
4362          * +----------------+---------------+----------------+
4363          * Feedback (FB) type: determines the type of the event.
4364          *      - if 1, we received a PLI request from the remote side.
4365          *      - if 4, we received a FIR request from the remote side.
4366          * SSRC
4367          *      - Remote side's SSRC value of the media sender (RFC-3550 Sec.5.1)
4368          *
4369          *
4370          * TYPE 300 - CVO (RTP Extension) message.
4371          * 0                4
4372          * +----------------+---------------+
4373          * |      101       |     value     |
4374          * +----------------+---------------+
4375          * value
4376          *      - clockwise rotation degrees of a received video (6.2.3 of 3GPP R12 TS 26.114).
4377          *      - can be 0 (degree 0), 1 (degree 90), 2 (degree 180) or 3 (degree 270).
4378          *
4379          *
4380          * TYPE 400 - Socket failed during receive. No ARGs.
4381          * 0
4382          * +----------------+
4383          * |      400       |
4384          * +----------------+
4385          * }</pre>
4386          *
4387          * @param mp the {@code MediaPlayer} associated with this callback.
4388          * @param noticeType TYPE of the event.
4389          * @param params RTP Rx media data serialized as int[] array.
4390          */
onRtpRxNotice(@onNull MediaPlayer mp, int noticeType, @NonNull int[] params)4391         void onRtpRxNotice(@NonNull MediaPlayer mp, int noticeType, @NonNull int[] params);
4392     }
4393 
4394     /**
4395      * Sets the listener to be invoked when an RTP Rx connection has a notice.
4396      * The listener is required if MediaPlayer is configured for RTPSource by
4397      * MediaPlayer.setDataSource(String8 rtpParams) of mediaplayer.h.
4398      *
4399      * @see OnRtpRxNoticeListener
4400      *
4401      * @param listener the listener called after a notice from RTP Rx.
4402      * @param executor the {@link Executor} on which to post RTP Tx events.
4403      * @hide
4404      */
4405     @SystemApi
4406     @RequiresPermission(BIND_IMS_SERVICE)
setOnRtpRxNoticeListener( @onNull Context context, @NonNull Executor executor, @NonNull OnRtpRxNoticeListener listener)4407     public void setOnRtpRxNoticeListener(
4408             @NonNull Context context,
4409             @NonNull Executor executor,
4410             @NonNull OnRtpRxNoticeListener listener) {
4411         Objects.requireNonNull(context);
4412         Preconditions.checkArgument(
4413                 context.checkSelfPermission(BIND_IMS_SERVICE) == PERMISSION_GRANTED,
4414                 BIND_IMS_SERVICE + " permission not granted.");
4415         mOnRtpRxNoticeListener = Objects.requireNonNull(listener);
4416         mOnRtpRxNoticeExecutor = Objects.requireNonNull(executor);
4417     }
4418 
4419     private OnRtpRxNoticeListener mOnRtpRxNoticeListener;
4420     private Executor mOnRtpRxNoticeExecutor;
4421 
4422     /**
4423      * Register a callback to be invoked when a selected track has timed metadata available.
4424      * <p>
4425      * Currently only HTTP live streaming data URI's embedded with timed ID3 tags generates
4426      * {@link TimedMetaData}.
4427      *
4428      * @see MediaPlayer#selectTrack(int)
4429      * @see MediaPlayer.OnTimedMetaDataAvailableListener
4430      * @see TimedMetaData
4431      *
4432      * @param listener the callback that will be run
4433      */
setOnTimedMetaDataAvailableListener(OnTimedMetaDataAvailableListener listener)4434     public void setOnTimedMetaDataAvailableListener(OnTimedMetaDataAvailableListener listener)
4435     {
4436         mOnTimedMetaDataAvailableListener = listener;
4437     }
4438 
4439     private OnTimedMetaDataAvailableListener mOnTimedMetaDataAvailableListener;
4440 
4441     /* Do not change these values without updating their counterparts
4442      * in include/media/mediaplayer.h!
4443      */
4444     /** Unspecified media player error.
4445      * @see android.media.MediaPlayer.OnErrorListener
4446      */
4447     public static final int MEDIA_ERROR_UNKNOWN = 1;
4448 
4449     /** Media server died. In this case, the application must release the
4450      * MediaPlayer object and instantiate a new one.
4451      * @see android.media.MediaPlayer.OnErrorListener
4452      */
4453     public static final int MEDIA_ERROR_SERVER_DIED = 100;
4454 
4455     /** The video is streamed and its container is not valid for progressive
4456      * playback i.e the video's index (e.g moov atom) is not at the start of the
4457      * file.
4458      * @see android.media.MediaPlayer.OnErrorListener
4459      */
4460     public static final int MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK = 200;
4461 
4462     /** File or network related operation errors. */
4463     public static final int MEDIA_ERROR_IO = -1004;
4464     /** Bitstream is not conforming to the related coding standard or file spec. */
4465     public static final int MEDIA_ERROR_MALFORMED = -1007;
4466     /** Bitstream is conforming to the related coding standard or file spec, but
4467      * the media framework does not support the feature. */
4468     public static final int MEDIA_ERROR_UNSUPPORTED = -1010;
4469     /** Some operation takes too long to complete, usually more than 3-5 seconds. */
4470     public static final int MEDIA_ERROR_TIMED_OUT = -110;
4471 
4472     /** Unspecified low-level system error. This value originated from UNKNOWN_ERROR in
4473      * system/core/include/utils/Errors.h
4474      * @see android.media.MediaPlayer.OnErrorListener
4475      * @hide
4476      */
4477     public static final int MEDIA_ERROR_SYSTEM = -2147483648;
4478 
4479     /**
4480      * Interface definition of a callback to be invoked when there
4481      * has been an error during an asynchronous operation (other errors
4482      * will throw exceptions at method call time).
4483      */
4484     public interface OnErrorListener
4485     {
4486         /**
4487          * Called to indicate an error.
4488          *
4489          * @param mp      the MediaPlayer the error pertains to
4490          * @param what    the type of error that has occurred:
4491          * <ul>
4492          * <li>{@link #MEDIA_ERROR_UNKNOWN}
4493          * <li>{@link #MEDIA_ERROR_SERVER_DIED}
4494          * </ul>
4495          * @param extra an extra code, specific to the error. Typically
4496          * implementation dependent.
4497          * <ul>
4498          * <li>{@link #MEDIA_ERROR_IO}
4499          * <li>{@link #MEDIA_ERROR_MALFORMED}
4500          * <li>{@link #MEDIA_ERROR_UNSUPPORTED}
4501          * <li>{@link #MEDIA_ERROR_TIMED_OUT}
4502          * <li><code>MEDIA_ERROR_SYSTEM (-2147483648)</code> - low-level system error.
4503          * </ul>
4504          * @return True if the method handled the error, false if it didn't.
4505          * Returning false, or not having an OnErrorListener at all, will
4506          * cause the OnCompletionListener to be called.
4507          */
onError(MediaPlayer mp, int what, int extra)4508         boolean onError(MediaPlayer mp, int what, int extra);
4509     }
4510 
4511     /**
4512      * Register a callback to be invoked when an error has happened
4513      * during an asynchronous operation.
4514      *
4515      * @param listener the callback that will be run
4516      */
setOnErrorListener(OnErrorListener listener)4517     public void setOnErrorListener(OnErrorListener listener)
4518     {
4519         mOnErrorListener = listener;
4520     }
4521 
4522     @UnsupportedAppUsage
4523     private OnErrorListener mOnErrorListener;
4524 
4525 
4526     /* Do not change these values without updating their counterparts
4527      * in include/media/mediaplayer.h!
4528      */
4529     /** Unspecified media player info.
4530      * @see android.media.MediaPlayer.OnInfoListener
4531      */
4532     public static final int MEDIA_INFO_UNKNOWN = 1;
4533 
4534     /** The player was started because it was used as the next player for another
4535      * player, which just completed playback.
4536      * @see android.media.MediaPlayer#setNextMediaPlayer(MediaPlayer)
4537      * @see android.media.MediaPlayer.OnInfoListener
4538      */
4539     public static final int MEDIA_INFO_STARTED_AS_NEXT = 2;
4540 
4541     /** The player just pushed the very first video frame for rendering.
4542      * @see android.media.MediaPlayer.OnInfoListener
4543      */
4544     public static final int MEDIA_INFO_VIDEO_RENDERING_START = 3;
4545 
4546     /** The video is too complex for the decoder: it can't decode frames fast
4547      *  enough. Possibly only the audio plays fine at this stage.
4548      * @see android.media.MediaPlayer.OnInfoListener
4549      */
4550     public static final int MEDIA_INFO_VIDEO_TRACK_LAGGING = 700;
4551 
4552     /** MediaPlayer is temporarily pausing playback internally in order to
4553      * buffer more data.
4554      * @see android.media.MediaPlayer.OnInfoListener
4555      */
4556     public static final int MEDIA_INFO_BUFFERING_START = 701;
4557 
4558     /** MediaPlayer is resuming playback after filling buffers.
4559      * @see android.media.MediaPlayer.OnInfoListener
4560      */
4561     public static final int MEDIA_INFO_BUFFERING_END = 702;
4562 
4563     /** Estimated network bandwidth information (kbps) is available; currently this event fires
4564      * simultaneously as {@link #MEDIA_INFO_BUFFERING_START} and {@link #MEDIA_INFO_BUFFERING_END}
4565      * when playing network files.
4566      * @see android.media.MediaPlayer.OnInfoListener
4567      * @hide
4568      */
4569     public static final int MEDIA_INFO_NETWORK_BANDWIDTH = 703;
4570 
4571     /** Bad interleaving means that a media has been improperly interleaved or
4572      * not interleaved at all, e.g has all the video samples first then all the
4573      * audio ones. Video is playing but a lot of disk seeks may be happening.
4574      * @see android.media.MediaPlayer.OnInfoListener
4575      */
4576     public static final int MEDIA_INFO_BAD_INTERLEAVING = 800;
4577 
4578     /** The media cannot be seeked (e.g live stream)
4579      * @see android.media.MediaPlayer.OnInfoListener
4580      */
4581     public static final int MEDIA_INFO_NOT_SEEKABLE = 801;
4582 
4583     /** A new set of metadata is available.
4584      * @see android.media.MediaPlayer.OnInfoListener
4585      */
4586     public static final int MEDIA_INFO_METADATA_UPDATE = 802;
4587 
4588     /** A new set of external-only metadata is available.  Used by
4589      *  JAVA framework to avoid triggering track scanning.
4590      * @hide
4591      */
4592     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
4593     public static final int MEDIA_INFO_EXTERNAL_METADATA_UPDATE = 803;
4594 
4595     /** Informs that audio is not playing. Note that playback of the video
4596      * is not interrupted.
4597      * @see android.media.MediaPlayer.OnInfoListener
4598      */
4599     public static final int MEDIA_INFO_AUDIO_NOT_PLAYING = 804;
4600 
4601     /** Informs that video is not playing. Note that playback of the audio
4602      * is not interrupted.
4603      * @see android.media.MediaPlayer.OnInfoListener
4604      */
4605     public static final int MEDIA_INFO_VIDEO_NOT_PLAYING = 805;
4606 
4607     /** Failed to handle timed text track properly.
4608      * @see android.media.MediaPlayer.OnInfoListener
4609      *
4610      * {@hide}
4611      */
4612     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
4613     public static final int MEDIA_INFO_TIMED_TEXT_ERROR = 900;
4614 
4615     /** Subtitle track was not supported by the media framework.
4616      * @see android.media.MediaPlayer.OnInfoListener
4617      */
4618     public static final int MEDIA_INFO_UNSUPPORTED_SUBTITLE = 901;
4619 
4620     /** Reading the subtitle track takes too long.
4621      * @see android.media.MediaPlayer.OnInfoListener
4622      */
4623     public static final int MEDIA_INFO_SUBTITLE_TIMED_OUT = 902;
4624 
4625     /**
4626      * Interface definition of a callback to be invoked to communicate some
4627      * info and/or warning about the media or its playback.
4628      */
4629     public interface OnInfoListener
4630     {
4631         /**
4632          * Called to indicate an info or a warning.
4633          *
4634          * @param mp      the MediaPlayer the info pertains to.
4635          * @param what    the type of info or warning.
4636          * <ul>
4637          * <li>{@link #MEDIA_INFO_UNKNOWN}
4638          * <li>{@link #MEDIA_INFO_VIDEO_TRACK_LAGGING}
4639          * <li>{@link #MEDIA_INFO_VIDEO_RENDERING_START}
4640          * <li>{@link #MEDIA_INFO_BUFFERING_START}
4641          * <li>{@link #MEDIA_INFO_BUFFERING_END}
4642          * <li><code>MEDIA_INFO_NETWORK_BANDWIDTH (703)</code> -
4643          *     bandwidth information is available (as <code>extra</code> kbps)
4644          * <li>{@link #MEDIA_INFO_BAD_INTERLEAVING}
4645          * <li>{@link #MEDIA_INFO_NOT_SEEKABLE}
4646          * <li>{@link #MEDIA_INFO_METADATA_UPDATE}
4647          * <li>{@link #MEDIA_INFO_UNSUPPORTED_SUBTITLE}
4648          * <li>{@link #MEDIA_INFO_SUBTITLE_TIMED_OUT}
4649          * </ul>
4650          * @param extra an extra code, specific to the info. Typically
4651          * implementation dependent.
4652          * @return True if the method handled the info, false if it didn't.
4653          * Returning false, or not having an OnInfoListener at all, will
4654          * cause the info to be discarded.
4655          */
onInfo(MediaPlayer mp, int what, int extra)4656         boolean onInfo(MediaPlayer mp, int what, int extra);
4657     }
4658 
4659     /**
4660      * Register a callback to be invoked when an info/warning is available.
4661      *
4662      * @param listener the callback that will be run
4663      */
setOnInfoListener(OnInfoListener listener)4664     public void setOnInfoListener(OnInfoListener listener)
4665     {
4666         mOnInfoListener = listener;
4667     }
4668 
4669     @UnsupportedAppUsage
4670     private OnInfoListener mOnInfoListener;
4671 
4672     // Modular DRM begin
4673 
4674     /**
4675      * Interface definition of a callback to be invoked when the app
4676      * can do DRM configuration (get/set properties) before the session
4677      * is opened. This facilitates configuration of the properties, like
4678      * 'securityLevel', which has to be set after DRM scheme creation but
4679      * before the DRM session is opened.
4680      *
4681      * The only allowed DRM calls in this listener are {@code getDrmPropertyString}
4682      * and {@code setDrmPropertyString}.
4683      *
4684      */
4685     public interface OnDrmConfigHelper
4686     {
4687         /**
4688          * Called to give the app the opportunity to configure DRM before the session is created
4689          *
4690          * @param mp the {@code MediaPlayer} associated with this callback
4691          */
onDrmConfig(MediaPlayer mp)4692         public void onDrmConfig(MediaPlayer mp);
4693     }
4694 
4695     /**
4696      * Register a callback to be invoked for configuration of the DRM object before
4697      * the session is created.
4698      * The callback will be invoked synchronously during the execution
4699      * of {@link #prepareDrm(UUID uuid)}.
4700      *
4701      * @param listener the callback that will be run
4702      */
setOnDrmConfigHelper(OnDrmConfigHelper listener)4703     public void setOnDrmConfigHelper(OnDrmConfigHelper listener)
4704     {
4705         synchronized (mDrmLock) {
4706             mOnDrmConfigHelper = listener;
4707         } // synchronized
4708     }
4709 
4710     private OnDrmConfigHelper mOnDrmConfigHelper;
4711 
4712     /**
4713      * Interface definition of a callback to be invoked when the
4714      * DRM info becomes available
4715      */
4716     public interface OnDrmInfoListener
4717     {
4718         /**
4719          * Called to indicate DRM info is available
4720          *
4721          * @param mp the {@code MediaPlayer} associated with this callback
4722          * @param drmInfo DRM info of the source including PSSH, and subset
4723          *                of crypto schemes supported by this device
4724          */
onDrmInfo(MediaPlayer mp, DrmInfo drmInfo)4725         public void onDrmInfo(MediaPlayer mp, DrmInfo drmInfo);
4726     }
4727 
4728     /**
4729      * Register a callback to be invoked when the DRM info is
4730      * known.
4731      *
4732      * @param listener the callback that will be run
4733      */
setOnDrmInfoListener(OnDrmInfoListener listener)4734     public void setOnDrmInfoListener(OnDrmInfoListener listener)
4735     {
4736         setOnDrmInfoListener(listener, null);
4737     }
4738 
4739     /**
4740      * Register a callback to be invoked when the DRM info is
4741      * known.
4742      *
4743      * @param listener the callback that will be run
4744      */
setOnDrmInfoListener(OnDrmInfoListener listener, Handler handler)4745     public void setOnDrmInfoListener(OnDrmInfoListener listener, Handler handler)
4746     {
4747         synchronized (mDrmLock) {
4748             if (listener != null) {
4749                 mOnDrmInfoHandlerDelegate = new OnDrmInfoHandlerDelegate(this, listener, handler);
4750             } else {
4751                 mOnDrmInfoHandlerDelegate = null;
4752             }
4753         } // synchronized
4754     }
4755 
4756     private OnDrmInfoHandlerDelegate mOnDrmInfoHandlerDelegate;
4757 
4758 
4759     /**
4760      * The status codes for {@link OnDrmPreparedListener#onDrmPrepared} listener.
4761      * <p>
4762      *
4763      * DRM preparation has succeeded.
4764      */
4765     public static final int PREPARE_DRM_STATUS_SUCCESS = 0;
4766 
4767     /**
4768      * The device required DRM provisioning but couldn't reach the provisioning server.
4769      */
4770     public static final int PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR = 1;
4771 
4772     /**
4773      * The device required DRM provisioning but the provisioning server denied the request.
4774      */
4775     public static final int PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR = 2;
4776 
4777     /**
4778      * The DRM preparation has failed .
4779      */
4780     public static final int PREPARE_DRM_STATUS_PREPARATION_ERROR = 3;
4781 
4782 
4783     /** @hide */
4784     @IntDef({
4785         PREPARE_DRM_STATUS_SUCCESS,
4786         PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR,
4787         PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR,
4788         PREPARE_DRM_STATUS_PREPARATION_ERROR,
4789     })
4790     @Retention(RetentionPolicy.SOURCE)
4791     public @interface PrepareDrmStatusCode {}
4792 
4793     /**
4794      * Interface definition of a callback to notify the app when the
4795      * DRM is ready for key request/response
4796      */
4797     public interface OnDrmPreparedListener
4798     {
4799         /**
4800          * Called to notify the app that prepareDrm is finished and ready for key request/response
4801          *
4802          * @param mp the {@code MediaPlayer} associated with this callback
4803          * @param status the result of DRM preparation which can be
4804          * {@link #PREPARE_DRM_STATUS_SUCCESS},
4805          * {@link #PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR},
4806          * {@link #PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR}, or
4807          * {@link #PREPARE_DRM_STATUS_PREPARATION_ERROR}.
4808          */
onDrmPrepared(MediaPlayer mp, @PrepareDrmStatusCode int status)4809         public void onDrmPrepared(MediaPlayer mp, @PrepareDrmStatusCode int status);
4810     }
4811 
4812     /**
4813      * Register a callback to be invoked when the DRM object is prepared.
4814      *
4815      * @param listener the callback that will be run
4816      */
setOnDrmPreparedListener(OnDrmPreparedListener listener)4817     public void setOnDrmPreparedListener(OnDrmPreparedListener listener)
4818     {
4819         setOnDrmPreparedListener(listener, null);
4820     }
4821 
4822     /**
4823      * Register a callback to be invoked when the DRM object is prepared.
4824      *
4825      * @param listener the callback that will be run
4826      * @param handler the Handler that will receive the callback
4827      */
setOnDrmPreparedListener(OnDrmPreparedListener listener, Handler handler)4828     public void setOnDrmPreparedListener(OnDrmPreparedListener listener, Handler handler)
4829     {
4830         synchronized (mDrmLock) {
4831             if (listener != null) {
4832                 mOnDrmPreparedHandlerDelegate = new OnDrmPreparedHandlerDelegate(this,
4833                                                             listener, handler);
4834             } else {
4835                 mOnDrmPreparedHandlerDelegate = null;
4836             }
4837         } // synchronized
4838     }
4839 
4840     private OnDrmPreparedHandlerDelegate mOnDrmPreparedHandlerDelegate;
4841 
4842 
4843     private class OnDrmInfoHandlerDelegate {
4844         private MediaPlayer mMediaPlayer;
4845         private OnDrmInfoListener mOnDrmInfoListener;
4846         private Handler mHandler;
4847 
OnDrmInfoHandlerDelegate(MediaPlayer mp, OnDrmInfoListener listener, Handler handler)4848         OnDrmInfoHandlerDelegate(MediaPlayer mp, OnDrmInfoListener listener, Handler handler) {
4849             mMediaPlayer = mp;
4850             mOnDrmInfoListener = listener;
4851 
4852             // find the looper for our new event handler
4853             if (handler != null) {
4854                 mHandler = handler;
4855             } else {
4856                 // handler == null
4857                 // Will let OnDrmInfoListener be called in mEventHandler similar to other
4858                 // legacy notifications. This is because MEDIA_DRM_INFO's notification has to be
4859                 // sent before MEDIA_PREPARED's (i.e., in the same order they are issued by
4860                 // mediaserver). As a result, the callback has to be called directly by
4861                 // EventHandler.handleMessage similar to onPrepared.
4862             }
4863         }
4864 
notifyClient(DrmInfo drmInfo)4865         void notifyClient(DrmInfo drmInfo) {
4866             if (mHandler != null) {
4867                 mHandler.post(new Runnable() {
4868                     @Override
4869                     public void run() {
4870                        mOnDrmInfoListener.onDrmInfo(mMediaPlayer, drmInfo);
4871                     }
4872                 });
4873             }
4874             else {  // no handler: direct call by mEventHandler
4875                 mOnDrmInfoListener.onDrmInfo(mMediaPlayer, drmInfo);
4876             }
4877         }
4878     }
4879 
4880     private class OnDrmPreparedHandlerDelegate {
4881         private MediaPlayer mMediaPlayer;
4882         private OnDrmPreparedListener mOnDrmPreparedListener;
4883         private Handler mHandler;
4884 
OnDrmPreparedHandlerDelegate(MediaPlayer mp, OnDrmPreparedListener listener, Handler handler)4885         OnDrmPreparedHandlerDelegate(MediaPlayer mp, OnDrmPreparedListener listener,
4886                 Handler handler) {
4887             mMediaPlayer = mp;
4888             mOnDrmPreparedListener = listener;
4889 
4890             // find the looper for our new event handler
4891             if (handler != null) {
4892                 mHandler = handler;
4893             } else if (mEventHandler != null) {
4894                 // Otherwise, use mEventHandler
4895                 mHandler = mEventHandler;
4896             } else {
4897                 Log.e(TAG, "OnDrmPreparedHandlerDelegate: Unexpected null mEventHandler");
4898             }
4899         }
4900 
notifyClient(int status)4901         void notifyClient(int status) {
4902             if (mHandler != null) {
4903                 mHandler.post(new Runnable() {
4904                     @Override
4905                     public void run() {
4906                         mOnDrmPreparedListener.onDrmPrepared(mMediaPlayer, status);
4907                     }
4908                 });
4909             } else {
4910                 Log.e(TAG, "OnDrmPreparedHandlerDelegate:notifyClient: Unexpected null mHandler");
4911             }
4912         }
4913     }
4914 
4915     /**
4916      * Retrieves the DRM Info associated with the current source
4917      *
4918      * @throws IllegalStateException if called before prepare()
4919      */
getDrmInfo()4920     public DrmInfo getDrmInfo()
4921     {
4922         DrmInfo drmInfo = null;
4923 
4924         // there is not much point if the app calls getDrmInfo within an OnDrmInfoListenet;
4925         // regardless below returns drmInfo anyway instead of raising an exception
4926         synchronized (mDrmLock) {
4927             if (!mDrmInfoResolved && mDrmInfo == null) {
4928                 final String msg = "The Player has not been prepared yet";
4929                 Log.v(TAG, msg);
4930                 throw new IllegalStateException(msg);
4931             }
4932 
4933             if (mDrmInfo != null) {
4934                 drmInfo = mDrmInfo.makeCopy();
4935             }
4936         }   // synchronized
4937 
4938         return drmInfo;
4939     }
4940 
4941 
4942     /**
4943      * Prepares the DRM for the current source
4944      * <p>
4945      * If {@code OnDrmConfigHelper} is registered, it will be called during
4946      * preparation to allow configuration of the DRM properties before opening the
4947      * DRM session. Note that the callback is called synchronously in the thread that called
4948      * {@code prepareDrm}. It should be used only for a series of {@code getDrmPropertyString}
4949      * and {@code setDrmPropertyString} calls and refrain from any lengthy operation.
4950      * <p>
4951      * If the device has not been provisioned before, this call also provisions the device
4952      * which involves accessing the provisioning server and can take a variable time to
4953      * complete depending on the network connectivity.
4954      * If {@code OnDrmPreparedListener} is registered, prepareDrm() runs in non-blocking
4955      * mode by launching the provisioning in the background and returning. The listener
4956      * will be called when provisioning and preparation has finished. If a
4957      * {@code OnDrmPreparedListener} is not registered, prepareDrm() waits till provisioning
4958      * and preparation has finished, i.e., runs in blocking mode.
4959      * <p>
4960      * If {@code OnDrmPreparedListener} is registered, it is called to indicate the DRM
4961      * session being ready. The application should not make any assumption about its call
4962      * sequence (e.g., before or after prepareDrm returns), or the thread context that will
4963      * execute the listener (unless the listener is registered with a handler thread).
4964      * <p>
4965      *
4966      * @param uuid The UUID of the crypto scheme. If not known beforehand, it can be retrieved
4967      * from the source through {@code getDrmInfo} or registering a {@code onDrmInfoListener}.
4968      *
4969      * @throws IllegalStateException              if called before prepare(), or the DRM was
4970      *                                            prepared already
4971      * @throws UnsupportedSchemeException         if the crypto scheme is not supported
4972      * @throws ResourceBusyException              if required DRM resources are in use
4973      * @throws ProvisioningNetworkErrorException  if provisioning is required but failed due to a
4974      *                                            network error
4975      * @throws ProvisioningServerErrorException   if provisioning is required but failed due to
4976      *                                            the request denied by the provisioning server
4977      */
prepareDrm(@onNull UUID uuid)4978     public void prepareDrm(@NonNull UUID uuid)
4979             throws UnsupportedSchemeException, ResourceBusyException,
4980                    ProvisioningNetworkErrorException, ProvisioningServerErrorException
4981     {
4982         Log.v(TAG, "prepareDrm: uuid: " + uuid + " mOnDrmConfigHelper: " + mOnDrmConfigHelper);
4983 
4984         boolean allDoneWithoutProvisioning = false;
4985         // get a snapshot as we'll use them outside the lock
4986         OnDrmPreparedHandlerDelegate onDrmPreparedHandlerDelegate = null;
4987 
4988         synchronized (mDrmLock) {
4989 
4990             // only allowing if tied to a protected source; might relax for releasing offline keys
4991             if (mDrmInfo == null) {
4992                 final String msg = "prepareDrm(): Wrong usage: The player must be prepared and " +
4993                         "DRM info be retrieved before this call.";
4994                 Log.e(TAG, msg);
4995                 throw new IllegalStateException(msg);
4996             }
4997 
4998             if (mActiveDrmScheme) {
4999                 final String msg = "prepareDrm(): Wrong usage: There is already " +
5000                         "an active DRM scheme with " + mDrmUUID;
5001                 Log.e(TAG, msg);
5002                 throw new IllegalStateException(msg);
5003             }
5004 
5005             if (mPrepareDrmInProgress) {
5006                 final String msg = "prepareDrm(): Wrong usage: There is already " +
5007                         "a pending prepareDrm call.";
5008                 Log.e(TAG, msg);
5009                 throw new IllegalStateException(msg);
5010             }
5011 
5012             if (mDrmProvisioningInProgress) {
5013                 final String msg = "prepareDrm(): Unexpectd: Provisioning is already in progress.";
5014                 Log.e(TAG, msg);
5015                 throw new IllegalStateException(msg);
5016             }
5017 
5018             // shouldn't need this; just for safeguard
5019             cleanDrmObj();
5020 
5021             mPrepareDrmInProgress = true;
5022             // local copy while the lock is held
5023             onDrmPreparedHandlerDelegate = mOnDrmPreparedHandlerDelegate;
5024 
5025             try {
5026                 // only creating the DRM object to allow pre-openSession configuration
5027                 prepareDrm_createDrmStep(uuid);
5028             } catch (Exception e) {
5029                 Log.w(TAG, "prepareDrm(): Exception ", e);
5030                 mPrepareDrmInProgress = false;
5031                 throw e;
5032             }
5033 
5034             mDrmConfigAllowed = true;
5035         }   // synchronized
5036 
5037 
5038         // call the callback outside the lock
5039         if (mOnDrmConfigHelper != null)  {
5040             mOnDrmConfigHelper.onDrmConfig(this);
5041         }
5042 
5043         synchronized (mDrmLock) {
5044             mDrmConfigAllowed = false;
5045             boolean earlyExit = false;
5046 
5047             try {
5048                 prepareDrm_openSessionStep(uuid);
5049 
5050                 mDrmUUID = uuid;
5051                 mActiveDrmScheme = true;
5052 
5053                 allDoneWithoutProvisioning = true;
5054             } catch (IllegalStateException e) {
5055                 final String msg = "prepareDrm(): Wrong usage: The player must be " +
5056                         "in the prepared state to call prepareDrm().";
5057                 Log.e(TAG, msg);
5058                 earlyExit = true;
5059                 throw new IllegalStateException(msg);
5060             } catch (NotProvisionedException e) {
5061                 Log.w(TAG, "prepareDrm: NotProvisionedException");
5062 
5063                 // handle provisioning internally; it'll reset mPrepareDrmInProgress
5064                 int result = HandleProvisioninig(uuid);
5065 
5066                 // if blocking mode, we're already done;
5067                 // if non-blocking mode, we attempted to launch background provisioning
5068                 if (result != PREPARE_DRM_STATUS_SUCCESS) {
5069                     earlyExit = true;
5070                     String msg;
5071 
5072                     switch (result) {
5073                     case PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR:
5074                         msg = "prepareDrm: Provisioning was required but failed " +
5075                                 "due to a network error.";
5076                         Log.e(TAG, msg);
5077                         throw new ProvisioningNetworkErrorException(msg);
5078 
5079                     case PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR:
5080                         msg = "prepareDrm: Provisioning was required but the request " +
5081                                 "was denied by the server.";
5082                         Log.e(TAG, msg);
5083                         throw new ProvisioningServerErrorException(msg);
5084 
5085                     case PREPARE_DRM_STATUS_PREPARATION_ERROR:
5086                     default: // default for safeguard
5087                         msg = "prepareDrm: Post-provisioning preparation failed.";
5088                         Log.e(TAG, msg);
5089                         throw new IllegalStateException(msg);
5090                     }
5091                 }
5092                 // nothing else to do;
5093                 // if blocking or non-blocking, HandleProvisioninig does the re-attempt & cleanup
5094             } catch (Exception e) {
5095                 Log.e(TAG, "prepareDrm: Exception " + e);
5096                 earlyExit = true;
5097                 throw e;
5098             } finally {
5099                 if (!mDrmProvisioningInProgress) {// if early exit other than provisioning exception
5100                     mPrepareDrmInProgress = false;
5101                 }
5102                 if (earlyExit) {    // cleaning up object if didn't succeed
5103                     cleanDrmObj();
5104                 }
5105             } // finally
5106         }   // synchronized
5107 
5108 
5109         // if finished successfully without provisioning, call the callback outside the lock
5110         if (allDoneWithoutProvisioning) {
5111             if (onDrmPreparedHandlerDelegate != null)
5112                 onDrmPreparedHandlerDelegate.notifyClient(PREPARE_DRM_STATUS_SUCCESS);
5113         }
5114 
5115     }
5116 
5117 
_releaseDrm()5118     private native void _releaseDrm();
5119 
5120     /**
5121      * Releases the DRM session
5122      * <p>
5123      * The player has to have an active DRM session and be in stopped, or prepared
5124      * state before this call is made.
5125      * A {@code reset()} call will release the DRM session implicitly.
5126      *
5127      * @throws NoDrmSchemeException if there is no active DRM session to release
5128      */
releaseDrm()5129     public void releaseDrm()
5130             throws NoDrmSchemeException
5131     {
5132         Log.v(TAG, "releaseDrm:");
5133 
5134         synchronized (mDrmLock) {
5135             if (!mActiveDrmScheme) {
5136                 Log.e(TAG, "releaseDrm(): No active DRM scheme to release.");
5137                 throw new NoDrmSchemeException("releaseDrm: No active DRM scheme to release.");
5138             }
5139 
5140             try {
5141                 // we don't have the player's state in this layer. The below call raises
5142                 // exception if we're in a non-stopped/prepared state.
5143 
5144                 // for cleaning native/mediaserver crypto object
5145                 _releaseDrm();
5146 
5147                 // for cleaning client-side MediaDrm object; only called if above has succeeded
5148                 cleanDrmObj();
5149 
5150                 mActiveDrmScheme = false;
5151             } catch (IllegalStateException e) {
5152                 Log.w(TAG, "releaseDrm: Exception ", e);
5153                 throw new IllegalStateException("releaseDrm: The player is not in a valid state.");
5154             } catch (Exception e) {
5155                 Log.e(TAG, "releaseDrm: Exception ", e);
5156             }
5157         }   // synchronized
5158     }
5159 
5160 
5161     /**
5162      * A key request/response exchange occurs between the app and a license server
5163      * to obtain or release keys used to decrypt encrypted content.
5164      * <p>
5165      * getKeyRequest() is used to obtain an opaque key request byte array that is
5166      * delivered to the license server.  The opaque key request byte array is returned
5167      * in KeyRequest.data.  The recommended URL to deliver the key request to is
5168      * returned in KeyRequest.defaultUrl.
5169      * <p>
5170      * After the app has received the key request response from the server,
5171      * it should deliver to the response to the DRM engine plugin using the method
5172      * {@link #provideKeyResponse}.
5173      *
5174      * @param keySetId is the key-set identifier of the offline keys being released when keyType is
5175      * {@link MediaDrm#KEY_TYPE_RELEASE}. It should be set to null for other key requests, when
5176      * keyType is {@link MediaDrm#KEY_TYPE_STREAMING} or {@link MediaDrm#KEY_TYPE_OFFLINE}.
5177      *
5178      * @param initData is the container-specific initialization data when the keyType is
5179      * {@link MediaDrm#KEY_TYPE_STREAMING} or {@link MediaDrm#KEY_TYPE_OFFLINE}. Its meaning is
5180      * interpreted based on the mime type provided in the mimeType parameter.  It could
5181      * contain, for example, the content ID, key ID or other data obtained from the content
5182      * metadata that is required in generating the key request.
5183      * When the keyType is {@link MediaDrm#KEY_TYPE_RELEASE}, it should be set to null.
5184      *
5185      * @param mimeType identifies the mime type of the content
5186      *
5187      * @param keyType specifies the type of the request. The request may be to acquire
5188      * keys for streaming, {@link MediaDrm#KEY_TYPE_STREAMING}, or for offline content
5189      * {@link MediaDrm#KEY_TYPE_OFFLINE}, or to release previously acquired
5190      * keys ({@link MediaDrm#KEY_TYPE_RELEASE}), which are identified by a keySetId.
5191      *
5192      * @param optionalParameters are included in the key request message to
5193      * allow a client application to provide additional message parameters to the server.
5194      * This may be {@code null} if no additional parameters are to be sent.
5195      *
5196      * @throws NoDrmSchemeException if there is no active DRM session
5197      */
5198     @NonNull
getKeyRequest(@ullable byte[] keySetId, @Nullable byte[] initData, @Nullable String mimeType, @MediaDrm.KeyType int keyType, @Nullable Map<String, String> optionalParameters)5199     public MediaDrm.KeyRequest getKeyRequest(@Nullable byte[] keySetId, @Nullable byte[] initData,
5200             @Nullable String mimeType, @MediaDrm.KeyType int keyType,
5201             @Nullable Map<String, String> optionalParameters)
5202             throws NoDrmSchemeException
5203     {
5204         Log.v(TAG, "getKeyRequest: "
5205                 + " keySetId: " + Arrays.toString(keySetId)
5206                 + " initData:" + Arrays.toString(initData)
5207                 + " mimeType: " + mimeType
5208                 + " keyType: " + keyType
5209                 + " optionalParameters: " + optionalParameters);
5210 
5211         synchronized (mDrmLock) {
5212             if (!mActiveDrmScheme) {
5213                 Log.e(TAG, "getKeyRequest NoDrmSchemeException");
5214                 throw new NoDrmSchemeException("getKeyRequest: Has to set a DRM scheme first.");
5215             }
5216 
5217             try {
5218                 byte[] scope = (keyType != MediaDrm.KEY_TYPE_RELEASE) ?
5219                         mDrmSessionId : // sessionId for KEY_TYPE_STREAMING/OFFLINE
5220                         keySetId;       // keySetId for KEY_TYPE_RELEASE
5221 
5222                 HashMap<String, String> hmapOptionalParameters =
5223                                                 (optionalParameters != null) ?
5224                                                 new HashMap<String, String>(optionalParameters) :
5225                                                 null;
5226 
5227                 MediaDrm.KeyRequest request = mDrmObj.getKeyRequest(scope, initData, mimeType,
5228                                                               keyType, hmapOptionalParameters);
5229                 Log.v(TAG, "getKeyRequest:   --> request: " + request);
5230 
5231                 return request;
5232 
5233             } catch (NotProvisionedException e) {
5234                 Log.w(TAG, "getKeyRequest NotProvisionedException: " +
5235                         "Unexpected. Shouldn't have reached here.");
5236                 throw new IllegalStateException("getKeyRequest: Unexpected provisioning error.");
5237             } catch (Exception e) {
5238                 Log.w(TAG, "getKeyRequest Exception " + e);
5239                 throw e;
5240             }
5241 
5242         }   // synchronized
5243     }
5244 
5245 
5246     /**
5247      * A key response is received from the license server by the app, then it is
5248      * provided to the DRM engine plugin using provideKeyResponse. When the
5249      * response is for an offline key request, a key-set identifier is returned that
5250      * can be used to later restore the keys to a new session with the method
5251      * {@ link # restoreKeys}.
5252      * When the response is for a streaming or release request, null is returned.
5253      *
5254      * @param keySetId When the response is for a release request, keySetId identifies
5255      * the saved key associated with the release request (i.e., the same keySetId
5256      * passed to the earlier {@ link # getKeyRequest} call. It MUST be null when the
5257      * response is for either streaming or offline key requests.
5258      *
5259      * @param response the byte array response from the server
5260      *
5261      * @throws NoDrmSchemeException if there is no active DRM session
5262      * @throws DeniedByServerException if the response indicates that the
5263      * server rejected the request
5264      */
provideKeyResponse(@ullable byte[] keySetId, @NonNull byte[] response)5265     public byte[] provideKeyResponse(@Nullable byte[] keySetId, @NonNull byte[] response)
5266             throws NoDrmSchemeException, DeniedByServerException
5267     {
5268         Log.v(TAG, "provideKeyResponse: keySetId: " + Arrays.toString(keySetId)
5269                 + " response: " + Arrays.toString(response));
5270 
5271         synchronized (mDrmLock) {
5272 
5273             if (!mActiveDrmScheme) {
5274                 Log.e(TAG, "getKeyRequest NoDrmSchemeException");
5275                 throw new NoDrmSchemeException("getKeyRequest: Has to set a DRM scheme first.");
5276             }
5277 
5278             try {
5279                 byte[] scope = (keySetId == null) ?
5280                                 mDrmSessionId :     // sessionId for KEY_TYPE_STREAMING/OFFLINE
5281                                 keySetId;           // keySetId for KEY_TYPE_RELEASE
5282 
5283                 byte[] keySetResult = mDrmObj.provideKeyResponse(scope, response);
5284 
5285                 Log.v(TAG, "provideKeyResponse: keySetId: " + Arrays.toString(keySetId)
5286                         + " response: " + Arrays.toString(response)
5287                         + " --> " + Arrays.toString(keySetResult));
5288 
5289 
5290                 return keySetResult;
5291 
5292             } catch (NotProvisionedException e) {
5293                 Log.w(TAG, "provideKeyResponse NotProvisionedException: " +
5294                         "Unexpected. Shouldn't have reached here.");
5295                 throw new IllegalStateException("provideKeyResponse: " +
5296                         "Unexpected provisioning error.");
5297             } catch (Exception e) {
5298                 Log.w(TAG, "provideKeyResponse Exception " + e);
5299                 throw e;
5300             }
5301         }   // synchronized
5302     }
5303 
5304 
5305     /**
5306      * Restore persisted offline keys into a new session.  keySetId identifies the
5307      * keys to load, obtained from a prior call to {@link #provideKeyResponse}.
5308      *
5309      * @param keySetId identifies the saved key set to restore
5310      */
restoreKeys(@onNull byte[] keySetId)5311     public void restoreKeys(@NonNull byte[] keySetId)
5312             throws NoDrmSchemeException
5313     {
5314         Log.v(TAG, "restoreKeys: keySetId: " + Arrays.toString(keySetId));
5315 
5316         synchronized (mDrmLock) {
5317 
5318             if (!mActiveDrmScheme) {
5319                 Log.w(TAG, "restoreKeys NoDrmSchemeException");
5320                 throw new NoDrmSchemeException("restoreKeys: Has to set a DRM scheme first.");
5321             }
5322 
5323             try {
5324                 mDrmObj.restoreKeys(mDrmSessionId, keySetId);
5325             } catch (Exception e) {
5326                 Log.w(TAG, "restoreKeys Exception " + e);
5327                 throw e;
5328             }
5329 
5330         }   // synchronized
5331     }
5332 
5333 
5334     /**
5335      * Read a DRM engine plugin String property value, given the property name string.
5336      * <p>
5337      * @param propertyName the property name
5338      *
5339      * Standard fields names are:
5340      * {@link MediaDrm#PROPERTY_VENDOR}, {@link MediaDrm#PROPERTY_VERSION},
5341      * {@link MediaDrm#PROPERTY_DESCRIPTION}, {@link MediaDrm#PROPERTY_ALGORITHMS}
5342      */
5343     @NonNull
getDrmPropertyString(@onNull @ediaDrm.StringProperty String propertyName)5344     public String getDrmPropertyString(@NonNull @MediaDrm.StringProperty String propertyName)
5345             throws NoDrmSchemeException
5346     {
5347         Log.v(TAG, "getDrmPropertyString: propertyName: " + propertyName);
5348 
5349         String value;
5350         synchronized (mDrmLock) {
5351 
5352             if (!mActiveDrmScheme && !mDrmConfigAllowed) {
5353                 Log.w(TAG, "getDrmPropertyString NoDrmSchemeException");
5354                 throw new NoDrmSchemeException("getDrmPropertyString: Has to prepareDrm() first.");
5355             }
5356 
5357             try {
5358                 value = mDrmObj.getPropertyString(propertyName);
5359             } catch (Exception e) {
5360                 Log.w(TAG, "getDrmPropertyString Exception " + e);
5361                 throw e;
5362             }
5363         }   // synchronized
5364 
5365         Log.v(TAG, "getDrmPropertyString: propertyName: " + propertyName + " --> value: " + value);
5366 
5367         return value;
5368     }
5369 
5370 
5371     /**
5372      * Set a DRM engine plugin String property value.
5373      * <p>
5374      * @param propertyName the property name
5375      * @param value the property value
5376      *
5377      * Standard fields names are:
5378      * {@link MediaDrm#PROPERTY_VENDOR}, {@link MediaDrm#PROPERTY_VERSION},
5379      * {@link MediaDrm#PROPERTY_DESCRIPTION}, {@link MediaDrm#PROPERTY_ALGORITHMS}
5380      */
setDrmPropertyString(@onNull @ediaDrm.StringProperty String propertyName, @NonNull String value)5381     public void setDrmPropertyString(@NonNull @MediaDrm.StringProperty String propertyName,
5382                                      @NonNull String value)
5383             throws NoDrmSchemeException
5384     {
5385         Log.v(TAG, "setDrmPropertyString: propertyName: " + propertyName + " value: " + value);
5386 
5387         synchronized (mDrmLock) {
5388 
5389             if ( !mActiveDrmScheme && !mDrmConfigAllowed ) {
5390                 Log.w(TAG, "setDrmPropertyString NoDrmSchemeException");
5391                 throw new NoDrmSchemeException("setDrmPropertyString: Has to prepareDrm() first.");
5392             }
5393 
5394             try {
5395                 mDrmObj.setPropertyString(propertyName, value);
5396             } catch ( Exception e ) {
5397                 Log.w(TAG, "setDrmPropertyString Exception " + e);
5398                 throw e;
5399             }
5400         }   // synchronized
5401     }
5402 
5403     /**
5404      * Encapsulates the DRM properties of the source.
5405      */
5406     public static final class DrmInfo {
5407         private Map<UUID, byte[]> mapPssh;
5408         private UUID[] supportedSchemes;
5409 
5410         /**
5411          * Returns the PSSH info of the data source for each supported DRM scheme.
5412          */
getPssh()5413         public Map<UUID, byte[]> getPssh() {
5414             return mapPssh;
5415         }
5416 
5417         /**
5418          * Returns the intersection of the data source and the device DRM schemes.
5419          * It effectively identifies the subset of the source's DRM schemes which
5420          * are supported by the device too.
5421          */
getSupportedSchemes()5422         public UUID[] getSupportedSchemes() {
5423             return supportedSchemes;
5424         }
5425 
DrmInfo(Map<UUID, byte[]> Pssh, UUID[] SupportedSchemes)5426         private DrmInfo(Map<UUID, byte[]> Pssh, UUID[] SupportedSchemes) {
5427             mapPssh = Pssh;
5428             supportedSchemes = SupportedSchemes;
5429         }
5430 
DrmInfo(Parcel parcel)5431         private DrmInfo(Parcel parcel) {
5432             Log.v(TAG, "DrmInfo(" + parcel + ") size " + parcel.dataSize());
5433 
5434             int psshsize = parcel.readInt();
5435             byte[] pssh = new byte[psshsize];
5436             parcel.readByteArray(pssh);
5437 
5438             Log.v(TAG, "DrmInfo() PSSH: " + arrToHex(pssh));
5439             mapPssh = parsePSSH(pssh, psshsize);
5440             Log.v(TAG, "DrmInfo() PSSH: " + mapPssh);
5441 
5442             int supportedDRMsCount = parcel.readInt();
5443             supportedSchemes = new UUID[supportedDRMsCount];
5444             for (int i = 0; i < supportedDRMsCount; i++) {
5445                 byte[] uuid = new byte[16];
5446                 parcel.readByteArray(uuid);
5447 
5448                 supportedSchemes[i] = bytesToUUID(uuid);
5449 
5450                 Log.v(TAG, "DrmInfo() supportedScheme[" + i + "]: " +
5451                       supportedSchemes[i]);
5452             }
5453 
5454             Log.v(TAG, "DrmInfo() Parcel psshsize: " + psshsize +
5455                   " supportedDRMsCount: " + supportedDRMsCount);
5456         }
5457 
makeCopy()5458         private DrmInfo makeCopy() {
5459             return new DrmInfo(this.mapPssh, this.supportedSchemes);
5460         }
5461 
arrToHex(byte[] bytes)5462         private String arrToHex(byte[] bytes) {
5463             String out = "0x";
5464             for (int i = 0; i < bytes.length; i++) {
5465                 out += String.format("%02x", bytes[i]);
5466             }
5467 
5468             return out;
5469         }
5470 
bytesToUUID(byte[] uuid)5471         private UUID bytesToUUID(byte[] uuid) {
5472             long msb = 0, lsb = 0;
5473             for (int i = 0; i < 8; i++) {
5474                 msb |= ( ((long)uuid[i]   & 0xff) << (8 * (7 - i)) );
5475                 lsb |= ( ((long)uuid[i+8] & 0xff) << (8 * (7 - i)) );
5476             }
5477 
5478             return new UUID(msb, lsb);
5479         }
5480 
parsePSSH(byte[] pssh, int psshsize)5481         private Map<UUID, byte[]> parsePSSH(byte[] pssh, int psshsize) {
5482             Map<UUID, byte[]> result = new HashMap<UUID, byte[]>();
5483 
5484             final int UUID_SIZE = 16;
5485             final int DATALEN_SIZE = 4;
5486 
5487             int len = psshsize;
5488             int numentries = 0;
5489             int i = 0;
5490 
5491             while (len > 0) {
5492                 if (len < UUID_SIZE) {
5493                     Log.w(TAG, String.format("parsePSSH: len is too short to parse " +
5494                                              "UUID: (%d < 16) pssh: %d", len, psshsize));
5495                     return null;
5496                 }
5497 
5498                 byte[] subset = Arrays.copyOfRange(pssh, i, i + UUID_SIZE);
5499                 UUID uuid = bytesToUUID(subset);
5500                 i += UUID_SIZE;
5501                 len -= UUID_SIZE;
5502 
5503                 // get data length
5504                 if (len < 4) {
5505                     Log.w(TAG, String.format("parsePSSH: len is too short to parse " +
5506                                              "datalen: (%d < 4) pssh: %d", len, psshsize));
5507                     return null;
5508                 }
5509 
5510                 subset = Arrays.copyOfRange(pssh, i, i+DATALEN_SIZE);
5511                 int datalen = (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) ?
5512                     ((subset[3] & 0xff) << 24) | ((subset[2] & 0xff) << 16) |
5513                     ((subset[1] & 0xff) <<  8) |  (subset[0] & 0xff)          :
5514                     ((subset[0] & 0xff) << 24) | ((subset[1] & 0xff) << 16) |
5515                     ((subset[2] & 0xff) <<  8) |  (subset[3] & 0xff) ;
5516                 i += DATALEN_SIZE;
5517                 len -= DATALEN_SIZE;
5518 
5519                 if (len < datalen) {
5520                     Log.w(TAG, String.format("parsePSSH: len is too short to parse " +
5521                                              "data: (%d < %d) pssh: %d", len, datalen, psshsize));
5522                     return null;
5523                 }
5524 
5525                 byte[] data = Arrays.copyOfRange(pssh, i, i+datalen);
5526 
5527                 // skip the data
5528                 i += datalen;
5529                 len -= datalen;
5530 
5531                 Log.v(TAG, String.format("parsePSSH[%d]: <%s, %s> pssh: %d",
5532                                          numentries, uuid, arrToHex(data), psshsize));
5533                 numentries++;
5534                 result.put(uuid, data);
5535             }
5536 
5537             return result;
5538         }
5539 
5540     };  // DrmInfo
5541 
5542     /**
5543      * Thrown when a DRM method is called before preparing a DRM scheme through prepareDrm().
5544      * Extends MediaDrm.MediaDrmException
5545      */
5546     public static final class NoDrmSchemeException extends MediaDrmException {
NoDrmSchemeException(String detailMessage)5547         public NoDrmSchemeException(String detailMessage) {
5548             super(detailMessage);
5549         }
5550     }
5551 
5552     /**
5553      * Thrown when the device requires DRM provisioning but the provisioning attempt has
5554      * failed due to a network error (Internet reachability, timeout, etc.).
5555      * Extends MediaDrm.MediaDrmException
5556      */
5557     public static final class ProvisioningNetworkErrorException extends MediaDrmException {
ProvisioningNetworkErrorException(String detailMessage)5558         public ProvisioningNetworkErrorException(String detailMessage) {
5559             super(detailMessage);
5560         }
5561     }
5562 
5563     /**
5564      * Thrown when the device requires DRM provisioning but the provisioning attempt has
5565      * failed due to the provisioning server denying the request.
5566      * Extends MediaDrm.MediaDrmException
5567      */
5568     public static final class ProvisioningServerErrorException extends MediaDrmException {
ProvisioningServerErrorException(String detailMessage)5569         public ProvisioningServerErrorException(String detailMessage) {
5570             super(detailMessage);
5571         }
5572     }
5573 
5574 
_prepareDrm(@onNull byte[] uuid, @NonNull byte[] drmSessionId)5575     private native void _prepareDrm(@NonNull byte[] uuid, @NonNull byte[] drmSessionId);
5576 
5577         // Modular DRM helpers
5578 
prepareDrm_createDrmStep(@onNull UUID uuid)5579     private void prepareDrm_createDrmStep(@NonNull UUID uuid)
5580             throws UnsupportedSchemeException {
5581         Log.v(TAG, "prepareDrm_createDrmStep: UUID: " + uuid);
5582 
5583         try {
5584             mDrmObj = new MediaDrm(uuid);
5585             Log.v(TAG, "prepareDrm_createDrmStep: Created mDrmObj=" + mDrmObj);
5586         } catch (Exception e) { // UnsupportedSchemeException
5587             Log.e(TAG, "prepareDrm_createDrmStep: MediaDrm failed with " + e);
5588             throw e;
5589         }
5590     }
5591 
prepareDrm_openSessionStep(@onNull UUID uuid)5592     private void prepareDrm_openSessionStep(@NonNull UUID uuid)
5593             throws NotProvisionedException, ResourceBusyException {
5594         Log.v(TAG, "prepareDrm_openSessionStep: uuid: " + uuid);
5595 
5596         // TODO: don't need an open session for a future specialKeyReleaseDrm mode but we should do
5597         // it anyway so it raises provisioning error if needed. We'd rather handle provisioning
5598         // at prepareDrm/openSession rather than getKeyRequest/provideKeyResponse
5599         try {
5600             mDrmSessionId = mDrmObj.openSession();
5601             Log.v(TAG, "prepareDrm_openSessionStep: mDrmSessionId="
5602                     + Arrays.toString(mDrmSessionId));
5603 
5604             // Sending it down to native/mediaserver to create the crypto object
5605             // This call could simply fail due to bad player state, e.g., after start().
5606             _prepareDrm(getByteArrayFromUUID(uuid), mDrmSessionId);
5607             Log.v(TAG, "prepareDrm_openSessionStep: _prepareDrm/Crypto succeeded");
5608 
5609         } catch (Exception e) { //ResourceBusyException, NotProvisionedException
5610             Log.e(TAG, "prepareDrm_openSessionStep: open/crypto failed with " + e);
5611             throw e;
5612         }
5613 
5614     }
5615 
5616     private class ProvisioningThread extends Thread
5617     {
5618         public static final int TIMEOUT_MS = 60000;
5619 
5620         private UUID uuid;
5621         private String urlStr;
5622         private Object drmLock;
5623         private OnDrmPreparedHandlerDelegate onDrmPreparedHandlerDelegate;
5624         private MediaPlayer mediaPlayer;
5625         private int status;
5626         private boolean finished;
status()5627         public  int status() {
5628             return status;
5629         }
5630 
initialize(MediaDrm.ProvisionRequest request, UUID uuid, MediaPlayer mediaPlayer)5631         public ProvisioningThread initialize(MediaDrm.ProvisionRequest request,
5632                                           UUID uuid, MediaPlayer mediaPlayer) {
5633             // lock is held by the caller
5634             drmLock = mediaPlayer.mDrmLock;
5635             onDrmPreparedHandlerDelegate = mediaPlayer.mOnDrmPreparedHandlerDelegate;
5636             this.mediaPlayer = mediaPlayer;
5637 
5638             urlStr = request.getDefaultUrl() + "&signedRequest=" + new String(request.getData());
5639             this.uuid = uuid;
5640 
5641             status = PREPARE_DRM_STATUS_PREPARATION_ERROR;
5642 
5643             Log.v(TAG, "HandleProvisioninig: Thread is initialised url: " + urlStr);
5644             return this;
5645         }
5646 
run()5647         public void run() {
5648 
5649             byte[] response = null;
5650             boolean provisioningSucceeded = false;
5651             try {
5652                 URL url = new URL(urlStr);
5653                 final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
5654                 try {
5655                     connection.setRequestMethod("POST");
5656                     connection.setDoOutput(false);
5657                     connection.setDoInput(true);
5658                     connection.setConnectTimeout(TIMEOUT_MS);
5659                     connection.setReadTimeout(TIMEOUT_MS);
5660 
5661                     connection.connect();
5662                     response = Streams.readFully(connection.getInputStream());
5663 
5664                     Log.v(TAG, "HandleProvisioninig: Thread run: response " +
5665                             response.length + " " + Arrays.toString(response));
5666                 } catch (Exception e) {
5667                     status = PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR;
5668                     Log.w(TAG, "HandleProvisioninig: Thread run: connect " + e + " url: " + url);
5669                 } finally {
5670                     connection.disconnect();
5671                 }
5672             } catch (Exception e)   {
5673                 status = PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR;
5674                 Log.w(TAG, "HandleProvisioninig: Thread run: openConnection " + e);
5675             }
5676 
5677             if (response != null) {
5678                 try {
5679                     mDrmObj.provideProvisionResponse(response);
5680                     Log.v(TAG, "HandleProvisioninig: Thread run: " +
5681                             "provideProvisionResponse SUCCEEDED!");
5682 
5683                     provisioningSucceeded = true;
5684                 } catch (Exception e) {
5685                     status = PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR;
5686                     Log.w(TAG, "HandleProvisioninig: Thread run: " +
5687                             "provideProvisionResponse " + e);
5688                 }
5689             }
5690 
5691             boolean succeeded = false;
5692 
5693             // non-blocking mode needs the lock
5694             if (onDrmPreparedHandlerDelegate != null) {
5695 
5696                 synchronized (drmLock) {
5697                     // continuing with prepareDrm
5698                     if (provisioningSucceeded) {
5699                         succeeded = mediaPlayer.resumePrepareDrm(uuid);
5700                         status = (succeeded) ?
5701                                 PREPARE_DRM_STATUS_SUCCESS :
5702                                 PREPARE_DRM_STATUS_PREPARATION_ERROR;
5703                     }
5704                     mediaPlayer.mDrmProvisioningInProgress = false;
5705                     mediaPlayer.mPrepareDrmInProgress = false;
5706                     if (!succeeded) {
5707                         cleanDrmObj();  // cleaning up if it hasn't gone through while in the lock
5708                     }
5709                 } // synchronized
5710 
5711                 // calling the callback outside the lock
5712                 onDrmPreparedHandlerDelegate.notifyClient(status);
5713             } else {   // blocking mode already has the lock
5714 
5715                 // continuing with prepareDrm
5716                 if (provisioningSucceeded) {
5717                     succeeded = mediaPlayer.resumePrepareDrm(uuid);
5718                     status = (succeeded) ?
5719                             PREPARE_DRM_STATUS_SUCCESS :
5720                             PREPARE_DRM_STATUS_PREPARATION_ERROR;
5721                 }
5722                 mediaPlayer.mDrmProvisioningInProgress = false;
5723                 mediaPlayer.mPrepareDrmInProgress = false;
5724                 if (!succeeded) {
5725                     cleanDrmObj();  // cleaning up if it hasn't gone through
5726                 }
5727             }
5728 
5729             finished = true;
5730         }   // run()
5731 
5732     }   // ProvisioningThread
5733 
HandleProvisioninig(UUID uuid)5734     private int HandleProvisioninig(UUID uuid)
5735     {
5736         // the lock is already held by the caller
5737 
5738         if (mDrmProvisioningInProgress) {
5739             Log.e(TAG, "HandleProvisioninig: Unexpected mDrmProvisioningInProgress");
5740             return PREPARE_DRM_STATUS_PREPARATION_ERROR;
5741         }
5742 
5743         MediaDrm.ProvisionRequest provReq = mDrmObj.getProvisionRequest();
5744         if (provReq == null) {
5745             Log.e(TAG, "HandleProvisioninig: getProvisionRequest returned null.");
5746             return PREPARE_DRM_STATUS_PREPARATION_ERROR;
5747         }
5748 
5749         Log.v(TAG, "HandleProvisioninig provReq "
5750                 + " data: " + Arrays.toString(provReq.getData())
5751                 + " url: " + provReq.getDefaultUrl());
5752 
5753         // networking in a background thread
5754         mDrmProvisioningInProgress = true;
5755 
5756         mDrmProvisioningThread = new ProvisioningThread().initialize(provReq, uuid, this);
5757         mDrmProvisioningThread.start();
5758 
5759         int result;
5760 
5761         // non-blocking: this is not the final result
5762         if (mOnDrmPreparedHandlerDelegate != null) {
5763             result = PREPARE_DRM_STATUS_SUCCESS;
5764         } else {
5765             // if blocking mode, wait till provisioning is done
5766             try {
5767                 mDrmProvisioningThread.join();
5768             } catch (Exception e) {
5769                 Log.w(TAG, "HandleProvisioninig: Thread.join Exception " + e);
5770             }
5771             result = mDrmProvisioningThread.status();
5772             // no longer need the thread
5773             mDrmProvisioningThread = null;
5774         }
5775 
5776         return result;
5777     }
5778 
resumePrepareDrm(UUID uuid)5779     private boolean resumePrepareDrm(UUID uuid)
5780     {
5781         Log.v(TAG, "resumePrepareDrm: uuid: " + uuid);
5782 
5783         // mDrmLock is guaranteed to be held
5784         boolean success = false;
5785         try {
5786             // resuming
5787             prepareDrm_openSessionStep(uuid);
5788 
5789             mDrmUUID = uuid;
5790             mActiveDrmScheme = true;
5791 
5792             success = true;
5793         } catch (Exception e) {
5794             Log.w(TAG, "HandleProvisioninig: Thread run _prepareDrm resume failed with " + e);
5795             // mDrmObj clean up is done by the caller
5796         }
5797 
5798         return success;
5799     }
5800 
resetDrmState()5801     private void resetDrmState()
5802     {
5803         synchronized (mDrmLock) {
5804             Log.v(TAG, "resetDrmState: " +
5805                     " mDrmInfo=" + mDrmInfo +
5806                     " mDrmProvisioningThread=" + mDrmProvisioningThread +
5807                     " mPrepareDrmInProgress=" + mPrepareDrmInProgress +
5808                     " mActiveDrmScheme=" + mActiveDrmScheme);
5809 
5810             mDrmInfoResolved = false;
5811             mDrmInfo = null;
5812 
5813             if (mDrmProvisioningThread != null) {
5814                 // timeout; relying on HttpUrlConnection
5815                 try {
5816                     mDrmProvisioningThread.join();
5817                 }
5818                 catch (InterruptedException e) {
5819                     Log.w(TAG, "resetDrmState: ProvThread.join Exception " + e);
5820                 }
5821                 mDrmProvisioningThread = null;
5822             }
5823 
5824             mPrepareDrmInProgress = false;
5825             mActiveDrmScheme = false;
5826 
5827             cleanDrmObj();
5828         }   // synchronized
5829     }
5830 
cleanDrmObj()5831     private void cleanDrmObj()
5832     {
5833         // the caller holds mDrmLock
5834         Log.v(TAG, "cleanDrmObj: mDrmObj=" + mDrmObj
5835                 + " mDrmSessionId=" + Arrays.toString(mDrmSessionId));
5836 
5837         if (mDrmSessionId != null)    {
5838             mDrmObj.closeSession(mDrmSessionId);
5839             mDrmSessionId = null;
5840         }
5841         if (mDrmObj != null) {
5842             mDrmObj.release();
5843             mDrmObj = null;
5844         }
5845     }
5846 
getByteArrayFromUUID(@onNull UUID uuid)5847     private static final byte[] getByteArrayFromUUID(@NonNull UUID uuid) {
5848         long msb = uuid.getMostSignificantBits();
5849         long lsb = uuid.getLeastSignificantBits();
5850 
5851         byte[] uuidBytes = new byte[16];
5852         for (int i = 0; i < 8; ++i) {
5853             uuidBytes[i] = (byte)(msb >>> (8 * (7 - i)));
5854             uuidBytes[8 + i] = (byte)(lsb >>> (8 * (7 - i)));
5855         }
5856 
5857         return uuidBytes;
5858     }
5859 
5860     // Modular DRM end
5861 
5862     /*
5863      * Test whether a given video scaling mode is supported.
5864      */
isVideoScalingModeSupported(int mode)5865     private boolean isVideoScalingModeSupported(int mode) {
5866         return (mode == VIDEO_SCALING_MODE_SCALE_TO_FIT ||
5867                 mode == VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
5868     }
5869 
5870     /** @hide */
5871     static class TimeProvider implements MediaPlayer.OnSeekCompleteListener,
5872             MediaTimeProvider {
5873         private static final String TAG = "MTP";
5874         private static final long MAX_NS_WITHOUT_POSITION_CHECK = 5000000000L;
5875         private static final long MAX_EARLY_CALLBACK_US = 1000;
5876         private static final long TIME_ADJUSTMENT_RATE = 2;  /* meaning 1/2 */
5877         private long mLastTimeUs = 0;
5878         private MediaPlayer mPlayer;
5879         private boolean mPaused = true;
5880         private boolean mStopped = true;
5881         private boolean mBuffering;
5882         private long mLastReportedTime;
5883         // since we are expecting only a handful listeners per stream, there is
5884         // no need for log(N) search performance
5885         private MediaTimeProvider.OnMediaTimeListener mListeners[];
5886         private long mTimes[];
5887         private Handler mEventHandler;
5888         private boolean mRefresh = false;
5889         private boolean mPausing = false;
5890         private boolean mSeeking = false;
5891         private static final int NOTIFY = 1;
5892         private static final int NOTIFY_TIME = 0;
5893         private static final int NOTIFY_STOP = 2;
5894         private static final int NOTIFY_SEEK = 3;
5895         private static final int NOTIFY_TRACK_DATA = 4;
5896         private HandlerThread mHandlerThread;
5897 
5898         /** @hide */
5899         public boolean DEBUG = false;
5900 
TimeProvider(MediaPlayer mp)5901         public TimeProvider(MediaPlayer mp) {
5902             mPlayer = mp;
5903             try {
5904                 getCurrentTimeUs(true, false);
5905             } catch (IllegalStateException e) {
5906                 // we assume starting position
5907                 mRefresh = true;
5908             }
5909 
5910             Looper looper;
5911             if ((looper = Looper.myLooper()) == null &&
5912                 (looper = Looper.getMainLooper()) == null) {
5913                 // Create our own looper here in case MP was created without one
5914                 mHandlerThread = new HandlerThread("MediaPlayerMTPEventThread",
5915                       Process.THREAD_PRIORITY_FOREGROUND);
5916                 mHandlerThread.start();
5917                 looper = mHandlerThread.getLooper();
5918             }
5919             mEventHandler = new EventHandler(looper);
5920 
5921             mListeners = new MediaTimeProvider.OnMediaTimeListener[0];
5922             mTimes = new long[0];
5923             mLastTimeUs = 0;
5924         }
5925 
scheduleNotification(int type, long delayUs)5926         private void scheduleNotification(int type, long delayUs) {
5927             // ignore time notifications until seek is handled
5928             if (mSeeking && type == NOTIFY_TIME) {
5929                 return;
5930             }
5931 
5932             if (DEBUG) Log.v(TAG, "scheduleNotification " + type + " in " + delayUs);
5933             mEventHandler.removeMessages(NOTIFY);
5934             Message msg = mEventHandler.obtainMessage(NOTIFY, type, 0);
5935             mEventHandler.sendMessageDelayed(msg, (int) (delayUs / 1000));
5936         }
5937 
5938         /** @hide */
close()5939         public void close() {
5940             mEventHandler.removeMessages(NOTIFY);
5941             if (mHandlerThread != null) {
5942                 mHandlerThread.quitSafely();
5943                 mHandlerThread = null;
5944             }
5945         }
5946 
5947         /** @hide */
finalize()5948         protected void finalize() {
5949             if (mHandlerThread != null) {
5950                 mHandlerThread.quitSafely();
5951             }
5952         }
5953 
5954         /** @hide */
onNotifyTime()5955         public void onNotifyTime() {
5956             synchronized (this) {
5957                 if (DEBUG) Log.d(TAG, "onNotifyTime: ");
5958                 scheduleNotification(NOTIFY_TIME, 0 /* delay */);
5959             }
5960         }
5961 
5962         /** @hide */
onPaused(boolean paused)5963         public void onPaused(boolean paused) {
5964             synchronized(this) {
5965                 if (DEBUG) Log.d(TAG, "onPaused: " + paused);
5966                 if (mStopped) { // handle as seek if we were stopped
5967                     mStopped = false;
5968                     mSeeking = true;
5969                     scheduleNotification(NOTIFY_SEEK, 0 /* delay */);
5970                 } else {
5971                     mPausing = paused;  // special handling if player disappeared
5972                     mSeeking = false;
5973                     scheduleNotification(NOTIFY_TIME, 0 /* delay */);
5974                 }
5975             }
5976         }
5977 
5978         /** @hide */
onBuffering(boolean buffering)5979         public void onBuffering(boolean buffering) {
5980             synchronized (this) {
5981                 if (DEBUG) Log.d(TAG, "onBuffering: " + buffering);
5982                 mBuffering = buffering;
5983                 scheduleNotification(NOTIFY_TIME, 0 /* delay */);
5984             }
5985         }
5986 
5987         /** @hide */
onStopped()5988         public void onStopped() {
5989             synchronized(this) {
5990                 if (DEBUG) Log.d(TAG, "onStopped");
5991                 mPaused = true;
5992                 mStopped = true;
5993                 mSeeking = false;
5994                 mBuffering = false;
5995                 scheduleNotification(NOTIFY_STOP, 0 /* delay */);
5996             }
5997         }
5998 
5999         /** @hide */
6000         @Override
onSeekComplete(MediaPlayer mp)6001         public void onSeekComplete(MediaPlayer mp) {
6002             synchronized(this) {
6003                 mStopped = false;
6004                 mSeeking = true;
6005                 scheduleNotification(NOTIFY_SEEK, 0 /* delay */);
6006             }
6007         }
6008 
6009         /** @hide */
onNewPlayer()6010         public void onNewPlayer() {
6011             if (mRefresh) {
6012                 synchronized(this) {
6013                     mStopped = false;
6014                     mSeeking = true;
6015                     mBuffering = false;
6016                     scheduleNotification(NOTIFY_SEEK, 0 /* delay */);
6017                 }
6018             }
6019         }
6020 
notifySeek()6021         private synchronized void notifySeek() {
6022             mSeeking = false;
6023             try {
6024                 long timeUs = getCurrentTimeUs(true, false);
6025                 if (DEBUG) Log.d(TAG, "onSeekComplete at " + timeUs);
6026 
6027                 for (MediaTimeProvider.OnMediaTimeListener listener: mListeners) {
6028                     if (listener == null) {
6029                         break;
6030                     }
6031                     listener.onSeek(timeUs);
6032                 }
6033             } catch (IllegalStateException e) {
6034                 // we should not be there, but at least signal pause
6035                 if (DEBUG) Log.d(TAG, "onSeekComplete but no player");
6036                 mPausing = true;  // special handling if player disappeared
6037                 notifyTimedEvent(false /* refreshTime */);
6038             }
6039         }
6040 
notifyTrackData(Pair<SubtitleTrack, byte[]> trackData)6041         private synchronized void notifyTrackData(Pair<SubtitleTrack, byte[]> trackData) {
6042             SubtitleTrack track = trackData.first;
6043             byte[] data = trackData.second;
6044             track.onData(data, true /* eos */, ~0 /* runID: keep forever */);
6045         }
6046 
notifyStop()6047         private synchronized void notifyStop() {
6048             for (MediaTimeProvider.OnMediaTimeListener listener: mListeners) {
6049                 if (listener == null) {
6050                     break;
6051                 }
6052                 listener.onStop();
6053             }
6054         }
6055 
registerListener(MediaTimeProvider.OnMediaTimeListener listener)6056         private int registerListener(MediaTimeProvider.OnMediaTimeListener listener) {
6057             int i = 0;
6058             for (; i < mListeners.length; i++) {
6059                 if (mListeners[i] == listener || mListeners[i] == null) {
6060                     break;
6061                 }
6062             }
6063 
6064             // new listener
6065             if (i >= mListeners.length) {
6066                 MediaTimeProvider.OnMediaTimeListener[] newListeners =
6067                     new MediaTimeProvider.OnMediaTimeListener[i + 1];
6068                 long[] newTimes = new long[i + 1];
6069                 System.arraycopy(mListeners, 0, newListeners, 0, mListeners.length);
6070                 System.arraycopy(mTimes, 0, newTimes, 0, mTimes.length);
6071                 mListeners = newListeners;
6072                 mTimes = newTimes;
6073             }
6074 
6075             if (mListeners[i] == null) {
6076                 mListeners[i] = listener;
6077                 mTimes[i] = MediaTimeProvider.NO_TIME;
6078             }
6079             return i;
6080         }
6081 
notifyAt( long timeUs, MediaTimeProvider.OnMediaTimeListener listener)6082         public void notifyAt(
6083                 long timeUs, MediaTimeProvider.OnMediaTimeListener listener) {
6084             synchronized(this) {
6085                 if (DEBUG) Log.d(TAG, "notifyAt " + timeUs);
6086                 mTimes[registerListener(listener)] = timeUs;
6087                 scheduleNotification(NOTIFY_TIME, 0 /* delay */);
6088             }
6089         }
6090 
scheduleUpdate(MediaTimeProvider.OnMediaTimeListener listener)6091         public void scheduleUpdate(MediaTimeProvider.OnMediaTimeListener listener) {
6092             synchronized(this) {
6093                 if (DEBUG) Log.d(TAG, "scheduleUpdate");
6094                 int i = registerListener(listener);
6095 
6096                 if (!mStopped) {
6097                     mTimes[i] = 0;
6098                     scheduleNotification(NOTIFY_TIME, 0 /* delay */);
6099                 }
6100             }
6101         }
6102 
cancelNotifications( MediaTimeProvider.OnMediaTimeListener listener)6103         public void cancelNotifications(
6104                 MediaTimeProvider.OnMediaTimeListener listener) {
6105             synchronized(this) {
6106                 int i = 0;
6107                 for (; i < mListeners.length; i++) {
6108                     if (mListeners[i] == listener) {
6109                         System.arraycopy(mListeners, i + 1,
6110                                 mListeners, i, mListeners.length - i - 1);
6111                         System.arraycopy(mTimes, i + 1,
6112                                 mTimes, i, mTimes.length - i - 1);
6113                         mListeners[mListeners.length - 1] = null;
6114                         mTimes[mTimes.length - 1] = NO_TIME;
6115                         break;
6116                     } else if (mListeners[i] == null) {
6117                         break;
6118                     }
6119                 }
6120 
6121                 scheduleNotification(NOTIFY_TIME, 0 /* delay */);
6122             }
6123         }
6124 
notifyTimedEvent(boolean refreshTime)6125         private synchronized void notifyTimedEvent(boolean refreshTime) {
6126             // figure out next callback
6127             long nowUs;
6128             try {
6129                 nowUs = getCurrentTimeUs(refreshTime, true);
6130             } catch (IllegalStateException e) {
6131                 // assume we paused until new player arrives
6132                 mRefresh = true;
6133                 mPausing = true; // this ensures that call succeeds
6134                 nowUs = getCurrentTimeUs(refreshTime, true);
6135             }
6136             long nextTimeUs = nowUs;
6137 
6138             if (mSeeking) {
6139                 // skip timed-event notifications until seek is complete
6140                 return;
6141             }
6142 
6143             if (DEBUG) {
6144                 StringBuilder sb = new StringBuilder();
6145                 sb.append("notifyTimedEvent(").append(mLastTimeUs).append(" -> ")
6146                         .append(nowUs).append(") from {");
6147                 boolean first = true;
6148                 for (long time: mTimes) {
6149                     if (time == NO_TIME) {
6150                         continue;
6151                     }
6152                     if (!first) sb.append(", ");
6153                     sb.append(time);
6154                     first = false;
6155                 }
6156                 sb.append("}");
6157                 Log.d(TAG, sb.toString());
6158             }
6159 
6160             Vector<MediaTimeProvider.OnMediaTimeListener> activatedListeners =
6161                 new Vector<MediaTimeProvider.OnMediaTimeListener>();
6162             for (int ix = 0; ix < mTimes.length; ix++) {
6163                 if (mListeners[ix] == null) {
6164                     break;
6165                 }
6166                 if (mTimes[ix] <= NO_TIME) {
6167                     // ignore, unless we were stopped
6168                 } else if (mTimes[ix] <= nowUs + MAX_EARLY_CALLBACK_US) {
6169                     activatedListeners.add(mListeners[ix]);
6170                     if (DEBUG) Log.d(TAG, "removed");
6171                     mTimes[ix] = NO_TIME;
6172                 } else if (nextTimeUs == nowUs || mTimes[ix] < nextTimeUs) {
6173                     nextTimeUs = mTimes[ix];
6174                 }
6175             }
6176 
6177             if (nextTimeUs > nowUs && !mPaused) {
6178                 // schedule callback at nextTimeUs
6179                 if (DEBUG) Log.d(TAG, "scheduling for " + nextTimeUs + " and " + nowUs);
6180                 mPlayer.notifyAt(nextTimeUs);
6181             } else {
6182                 mEventHandler.removeMessages(NOTIFY);
6183                 // no more callbacks
6184             }
6185 
6186             for (MediaTimeProvider.OnMediaTimeListener listener: activatedListeners) {
6187                 listener.onTimedEvent(nowUs);
6188             }
6189         }
6190 
getCurrentTimeUs(boolean refreshTime, boolean monotonic)6191         public long getCurrentTimeUs(boolean refreshTime, boolean monotonic)
6192                 throws IllegalStateException {
6193             synchronized (this) {
6194                 // we always refresh the time when the paused-state changes, because
6195                 // we expect to have received the pause-change event delayed.
6196                 if (mPaused && !refreshTime) {
6197                     return mLastReportedTime;
6198                 }
6199 
6200                 try {
6201                     mLastTimeUs = mPlayer.getCurrentPosition() * 1000L;
6202                     mPaused = !mPlayer.isPlaying() || mBuffering;
6203                     if (DEBUG) Log.v(TAG, (mPaused ? "paused" : "playing") + " at " + mLastTimeUs);
6204                 } catch (IllegalStateException e) {
6205                     if (mPausing) {
6206                         // if we were pausing, get last estimated timestamp
6207                         mPausing = false;
6208                         if (!monotonic || mLastReportedTime < mLastTimeUs) {
6209                             mLastReportedTime = mLastTimeUs;
6210                         }
6211                         mPaused = true;
6212                         if (DEBUG) Log.d(TAG, "illegal state, but pausing: estimating at " + mLastReportedTime);
6213                         return mLastReportedTime;
6214                     }
6215                     // TODO get time when prepared
6216                     throw e;
6217                 }
6218                 if (monotonic && mLastTimeUs < mLastReportedTime) {
6219                     /* have to adjust time */
6220                     if (mLastReportedTime - mLastTimeUs > 1000000) {
6221                         // schedule seeked event if time jumped significantly
6222                         // TODO: do this properly by introducing an exception
6223                         mStopped = false;
6224                         mSeeking = true;
6225                         scheduleNotification(NOTIFY_SEEK, 0 /* delay */);
6226                     }
6227                 } else {
6228                     mLastReportedTime = mLastTimeUs;
6229                 }
6230 
6231                 return mLastReportedTime;
6232             }
6233         }
6234 
6235         private class EventHandler extends Handler {
EventHandler(Looper looper)6236             public EventHandler(Looper looper) {
6237                 super(looper);
6238             }
6239 
6240             @Override
handleMessage(Message msg)6241             public void handleMessage(Message msg) {
6242                 if (msg.what == NOTIFY) {
6243                     switch (msg.arg1) {
6244                     case NOTIFY_TIME:
6245                         notifyTimedEvent(true /* refreshTime */);
6246                         break;
6247                     case NOTIFY_STOP:
6248                         notifyStop();
6249                         break;
6250                     case NOTIFY_SEEK:
6251                         notifySeek();
6252                         break;
6253                     case NOTIFY_TRACK_DATA:
6254                         notifyTrackData((Pair<SubtitleTrack, byte[]>)msg.obj);
6255                         break;
6256                     }
6257                 }
6258             }
6259         }
6260     }
6261 
6262     public final static class MetricsConstants
6263     {
MetricsConstants()6264         private MetricsConstants() {}
6265 
6266         /**
6267          * Key to extract the MIME type of the video track
6268          * from the {@link MediaPlayer#getMetrics} return value.
6269          * The value is a String.
6270          */
6271         public static final String MIME_TYPE_VIDEO = "android.media.mediaplayer.video.mime";
6272 
6273         /**
6274          * Key to extract the codec being used to decode the video track
6275          * from the {@link MediaPlayer#getMetrics} return value.
6276          * The value is a String.
6277          */
6278         public static final String CODEC_VIDEO = "android.media.mediaplayer.video.codec";
6279 
6280         /**
6281          * Key to extract the width (in pixels) of the video track
6282          * from the {@link MediaPlayer#getMetrics} return value.
6283          * The value is an integer.
6284          */
6285         public static final String WIDTH = "android.media.mediaplayer.width";
6286 
6287         /**
6288          * Key to extract the height (in pixels) of the video track
6289          * from the {@link MediaPlayer#getMetrics} return value.
6290          * The value is an integer.
6291          */
6292         public static final String HEIGHT = "android.media.mediaplayer.height";
6293 
6294         /**
6295          * Key to extract the count of video frames played
6296          * from the {@link MediaPlayer#getMetrics} return value.
6297          * The value is an integer.
6298          */
6299         public static final String FRAMES = "android.media.mediaplayer.frames";
6300 
6301         /**
6302          * Key to extract the count of video frames dropped
6303          * from the {@link MediaPlayer#getMetrics} return value.
6304          * The value is an integer.
6305          */
6306         public static final String FRAMES_DROPPED = "android.media.mediaplayer.dropped";
6307 
6308         /**
6309          * Key to extract the MIME type of the audio track
6310          * from the {@link MediaPlayer#getMetrics} return value.
6311          * The value is a String.
6312          */
6313         public static final String MIME_TYPE_AUDIO = "android.media.mediaplayer.audio.mime";
6314 
6315         /**
6316          * Key to extract the codec being used to decode the audio track
6317          * from the {@link MediaPlayer#getMetrics} return value.
6318          * The value is a String.
6319          */
6320         public static final String CODEC_AUDIO = "android.media.mediaplayer.audio.codec";
6321 
6322         /**
6323          * Key to extract the duration (in milliseconds) of the
6324          * media being played
6325          * from the {@link MediaPlayer#getMetrics} return value.
6326          * The value is a long.
6327          */
6328         public static final String DURATION = "android.media.mediaplayer.durationMs";
6329 
6330         /**
6331          * Key to extract the playing time (in milliseconds) of the
6332          * media being played
6333          * from the {@link MediaPlayer#getMetrics} return value.
6334          * The value is a long.
6335          */
6336         public static final String PLAYING = "android.media.mediaplayer.playingMs";
6337 
6338         /**
6339          * Key to extract the count of errors encountered while
6340          * playing the media
6341          * from the {@link MediaPlayer#getMetrics} return value.
6342          * The value is an integer.
6343          */
6344         public static final String ERRORS = "android.media.mediaplayer.err";
6345 
6346         /**
6347          * Key to extract an (optional) error code detected while
6348          * playing the media
6349          * from the {@link MediaPlayer#getMetrics} return value.
6350          * The value is an integer.
6351          */
6352         public static final String ERROR_CODE = "android.media.mediaplayer.errcode";
6353 
6354     }
6355 }
6356