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