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