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