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