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