1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.media; 18 19 import android.annotation.IntDef; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.annotation.TestApi; 23 import android.app.ActivityThread; 24 import android.content.ContentProvider; 25 import android.content.ContentResolver; 26 import android.content.Context; 27 import android.content.res.AssetFileDescriptor; 28 import android.net.Uri; 29 import android.os.Bundle; 30 import android.os.Handler; 31 import android.os.HandlerThread; 32 import android.os.IBinder; 33 import android.os.Looper; 34 import android.os.Message; 35 import android.os.Parcel; 36 import android.os.Parcelable; 37 import android.os.PersistableBundle; 38 import android.os.Process; 39 import android.os.PowerManager; 40 import android.os.SystemProperties; 41 import android.provider.Settings; 42 import android.system.ErrnoException; 43 import android.system.Os; 44 import android.system.OsConstants; 45 import android.util.Log; 46 import android.util.Pair; 47 import android.util.ArrayMap; 48 import android.view.Surface; 49 import android.view.SurfaceHolder; 50 import android.widget.VideoView; 51 import android.graphics.SurfaceTexture; 52 import android.media.AudioManager; 53 import android.media.MediaDrm; 54 import android.media.MediaFormat; 55 import android.media.MediaTimeProvider; 56 import android.media.PlaybackParams; 57 import android.media.SubtitleController; 58 import android.media.SubtitleController.Anchor; 59 import android.media.SubtitleData; 60 import android.media.SubtitleTrack.RenderingWidget; 61 import android.media.SyncParams; 62 63 import com.android.internal.annotations.GuardedBy; 64 import com.android.internal.util.Preconditions; 65 66 import libcore.io.IoBridge; 67 import libcore.io.Streams; 68 69 import java.io.ByteArrayOutputStream; 70 import java.io.File; 71 import java.io.FileDescriptor; 72 import java.io.FileInputStream; 73 import java.io.IOException; 74 import java.io.InputStream; 75 import java.lang.Runnable; 76 import java.lang.annotation.Retention; 77 import java.lang.annotation.RetentionPolicy; 78 import java.lang.ref.WeakReference; 79 import java.net.CookieHandler; 80 import java.net.CookieManager; 81 import java.net.HttpCookie; 82 import java.net.HttpURLConnection; 83 import java.net.InetSocketAddress; 84 import java.net.URL; 85 import java.nio.ByteOrder; 86 import java.util.Arrays; 87 import java.util.BitSet; 88 import java.util.HashMap; 89 import java.util.List; 90 import java.util.Map; 91 import java.util.Scanner; 92 import java.util.Set; 93 import java.util.UUID; 94 import 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 <uses-permission>} 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 */ 580 public 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"); native_init()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 */ MediaPlayer()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 */ _setVideoSurface(Surface surface)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 */ newRequest()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 */ invoke(Parcel request, Parcel reply)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 */ setDisplay(SurfaceHolder sh)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 */ setSurface(Surface surface)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 */ setVideoScalingMode(int mode)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 */ create(Context context, Uri uri)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 */ create(Context context, Uri uri, SurfaceHolder holder)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 */ create(Context context, Uri uri, SurfaceHolder holder, AudioAttributes audioAttributes, int audioSessionId)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.<something></var>) for 949 * the resource to use as the datasource 950 * @return a MediaPlayer object, or null if creation failed 951 */ create(Context context, int resid)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.<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 */ create(Context context, int resid, AudioAttributes audioAttributes, int audioSessionId)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 */ setDataSource(@onNull Context context, @NonNull Uri uri)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 */ setDataSource(@onNull Context context, @NonNull Uri uri, @Nullable Map<String, String> headers, @Nullable List<HttpCookie> cookies)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 */ setDataSource(@onNull Context context, @NonNull Uri uri, @Nullable Map<String, String> headers)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 attemptDataSource(ContentResolver resolver, Uri uri)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 */ setDataSource(String path)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 */ setDataSource(String path, Map<String, String> headers)1142 public void setDataSource(String path, Map<String, String> headers) 1143 throws IOException, IllegalArgumentException, SecurityException, IllegalStateException { 1144 setDataSource(path, headers, null); 1145 } 1146 setDataSource(String path, Map<String, String> headers, List<HttpCookie> cookies)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 setDataSource(String path, String[] keys, String[] values, List<HttpCookie> cookies)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 nativeSetDataSource( IBinder httpServiceBinder, String path, String[] keys, String[] values)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 */ setDataSource(@onNull AssetFileDescriptor afd)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 */ setDataSource(FileDescriptor fd)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 */ setDataSource(FileDescriptor fd, long offset, long length)1249 public void setDataSource(FileDescriptor fd, long offset, long length) 1250 throws IOException, IllegalArgumentException, IllegalStateException { 1251 _setDataSource(fd, offset, length); 1252 } 1253 _setDataSource(FileDescriptor fd, long offset, long length)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 */ setDataSource(MediaDataSource dataSource)1264 public void setDataSource(MediaDataSource dataSource) 1265 throws IllegalArgumentException, IllegalStateException { 1266 _setDataSource(dataSource); 1267 } 1268 _setDataSource(MediaDataSource dataSource)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 */ prepare()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 _prepare()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 */ prepareAsync()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 */ start()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 startImpl()1339 private void startImpl() { 1340 baseStart(); 1341 stayAwake(true); 1342 _start(); 1343 } 1344 _start()1345 private native void _start() throws IllegalStateException; 1346 1347 getAudioStreamType()1348 private int getAudioStreamType() { 1349 if (mStreamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { 1350 mStreamType = _getAudioStreamType(); 1351 } 1352 return mStreamType; 1353 } 1354 _getAudioStreamType()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 */ stop()1363 public void stop() throws IllegalStateException { 1364 stayAwake(false); 1365 _stop(); 1366 baseStop(); 1367 } 1368 _stop()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 */ pause()1377 public void pause() throws IllegalStateException { 1378 stayAwake(false); 1379 _pause(); 1380 basePause(); 1381 } 1382 _pause()1383 private native void _pause() throws IllegalStateException; 1384 1385 @Override playerStart()1386 void playerStart() { 1387 start(); 1388 } 1389 1390 @Override playerPause()1391 void playerPause() { 1392 pause(); 1393 } 1394 1395 @Override playerStop()1396 void playerStop() { 1397 stop(); 1398 } 1399 1400 @Override playerApplyVolumeShaper( @onNull VolumeShaper.Configuration configuration, @NonNull VolumeShaper.Operation operation)1401 /* package */ int playerApplyVolumeShaper( 1402 @NonNull VolumeShaper.Configuration configuration, 1403 @NonNull VolumeShaper.Operation operation) { 1404 return native_applyVolumeShaper(configuration, operation); 1405 } 1406 1407 @Override playerGetVolumeShaperState(int id)1408 /* package */ @Nullable VolumeShaper.State playerGetVolumeShaperState(int id) { 1409 return native_getVolumeShaperState(id); 1410 } 1411 1412 @Override createVolumeShaper( @onNull VolumeShaper.Configuration configuration)1413 public @NonNull VolumeShaper createVolumeShaper( 1414 @NonNull VolumeShaper.Configuration configuration) { 1415 return new VolumeShaper(configuration, this); 1416 } 1417 native_applyVolumeShaper( @onNull VolumeShaper.Configuration configuration, @NonNull VolumeShaper.Operation operation)1418 private native int native_applyVolumeShaper( 1419 @NonNull VolumeShaper.Configuration configuration, 1420 @NonNull VolumeShaper.Operation operation); 1421 native_getVolumeShaperState(int id)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 setPreferredDevice(AudioDeviceInfo deviceInfo)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 getPreferredDevice()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 getRoutedDevice()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") enableNativeRoutingCallbacksLocked(boolean enabled)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 addOnRoutingChangedListener(AudioRouting.OnRoutingChangedListener listener, Handler handler)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 removeOnRoutingChangedListener(AudioRouting.OnRoutingChangedListener listener)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 native_setOutputDevice(int deviceId)1541 private native final boolean native_setOutputDevice(int deviceId); native_getRoutedDeviceId()1542 private native final int native_getRoutedDeviceId(); native_enableDeviceCallback(boolean enabled)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 */ setWakeMode(Context context, int mode)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 */ setScreenOnWhilePlaying(boolean screenOn)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 stayAwake(boolean awake)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 updateSurfaceScreenOn()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 */ getVideoWidth()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 */ getVideoHeight()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 */ getMetrics()1657 public PersistableBundle getMetrics() { 1658 PersistableBundle bundle = native_getMetrics(); 1659 return bundle; 1660 } 1661 native_getMetrics()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 */ isPlaying()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 getBufferingParams()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 setBufferingParams(@onNull BufferingParams params)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 easyPlaybackParams(float rate, @PlaybackRateAudioMode int audioMode)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 */ setPlaybackParams(@onNull PlaybackParams params)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 getPlaybackParams()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 */ setSyncParams(@onNull SyncParams params)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 getSyncParams()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 _seekTo(long msec, int mode)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 */ seekTo(long msec, @SeekMode int mode)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 */ seekTo(int msec)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 getTimestamp()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 */ getCurrentPosition()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 */ getDuration()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 */ getMetadata(final boolean update_only, final boolean apply_filter)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 */ setMetadataFilter(Set<Integer> allow, Set<Integer> block)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 */ setNextMediaPlayer(MediaPlayer next)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 */ release()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 _release()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 */ reset()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 _reset()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 */ notifyAt(long mediaTimeUs)2202 public void notifyAt(long mediaTimeUs) { 2203 _notifyAt(mediaTimeUs); 2204 } 2205 _notifyAt(long mediaTimeUs)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 */ setAudioStreamType(int streamtype)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 _setAudioStreamType(int streamtype)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 */ setParameter(int key, Parcel value)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 */ setAudioAttributes(AudioAttributes attributes)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 */ setLooping(boolean looping)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 */ isLooping()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 */ setVolume(float leftVolume, float rightVolume)2292 public void setVolume(float leftVolume, float rightVolume) { 2293 baseSetVolume(leftVolume, rightVolume); 2294 } 2295 2296 @Override playerSetVolume(boolean muting, float leftVolume, float rightVolume)2297 void playerSetVolume(boolean muting, float leftVolume, float rightVolume) { 2298 _setVolume(muting ? 0.0f : leftVolume, muting ? 0.0f : rightVolume); 2299 } 2300 _setVolume(float leftVolume, float rightVolume)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 */ setVolume(float volume)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 */ setAudioSessionId(int sessionId)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 */ getAudioSessionId()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 */ attachAuxEffect(int effectId)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 */ setAuxEffectSendLevel(float level)2366 public void setAuxEffectSendLevel(float level) { 2367 baseSetAuxEffectSendLevel(level); 2368 } 2369 2370 @Override playerSetAuxEffectSendLevel(boolean muting, float level)2371 int playerSetAuxEffectSendLevel(boolean muting, float level) { 2372 _setAuxEffectSendLevel(muting ? 0.0f : level); 2373 return AudioSystem.SUCCESS; 2374 } 2375 _setAuxEffectSendLevel(float level)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 */ native_invoke(Parcel request, Parcel reply)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 */ native_getMetadata(boolean update_only, boolean apply_filter, Parcel reply)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 */ native_setMetadataFilter(Parcel request)2412 private native final int native_setMetadataFilter(Parcel request); 2413 native_init()2414 private static native final void native_init(); native_setup(Object mediaplayer_this)2415 private native final void native_setup(Object mediaplayer_this); native_finalize()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 */ getTrackType()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 */ getLanguage()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 */ getFormat()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 TrackInfo(Parcel in)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 */ TrackInfo(int type, MediaFormat format)2494 TrackInfo(int type, MediaFormat format) { 2495 mTrackType = type; 2496 mFormat = format; 2497 } 2498 2499 /** 2500 * {@inheritDoc} 2501 */ 2502 @Override describeContents()2503 public int describeContents() { 2504 return 0; 2505 } 2506 2507 /** 2508 * {@inheritDoc} 2509 */ 2510 @Override writeToParcel(Parcel dest, int flags)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 toString()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 */ getTrackInfo()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 getInbandTrackInfo()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 */ availableMimeTypeForExternalSource(String mimeType)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 */ setSubtitleAnchor( SubtitleController controller, SubtitleController.Anchor anchor)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 */ setSubtitleAnchor()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 onSubtitleTrackSelected(SubtitleTrack track)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 */ addSubtitleSource(InputStream is, MediaFormat format)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 scanInternalSubtitleTracks()2831 private void scanInternalSubtitleTracks() { 2832 setSubtitleAnchor(); 2833 2834 populateInbandTracks(); 2835 2836 if (mSubtitleController != null) { 2837 mSubtitleController.selectDefaultTrack(); 2838 } 2839 } 2840 populateInbandTracks()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 if (tracks[i] == null) { 2852 Log.w(TAG, "unexpected NULL track at index " + i); 2853 } 2854 // newly appeared inband track 2855 if (tracks[i] != null 2856 && tracks[i].getTrackType() == TrackInfo.MEDIA_TRACK_TYPE_SUBTITLE) { 2857 SubtitleTrack track = mSubtitleController.addTrack( 2858 tracks[i].getFormat()); 2859 mIndexTrackPairs.add(Pair.create(i, track)); 2860 } else { 2861 mIndexTrackPairs.add(Pair.<Integer, SubtitleTrack>create(i, null)); 2862 } 2863 } 2864 } 2865 } 2866 2867 /* TODO: Limit the total number of external timed text source to a reasonable number. 2868 */ 2869 /** 2870 * Adds an external timed text source file. 2871 * 2872 * Currently supported format is SubRip with the file extension .srt, case insensitive. 2873 * Note that a single external timed text source may contain multiple tracks in it. 2874 * One can find the total number of available tracks using {@link #getTrackInfo()} to see what 2875 * additional tracks become available after this method call. 2876 * 2877 * @param path The file path of external timed text source file. 2878 * @param mimeType The mime type of the file. Must be one of the mime types listed above. 2879 * @throws IOException if the file cannot be accessed or is corrupted. 2880 * @throws IllegalArgumentException if the mimeType is not supported. 2881 * @throws IllegalStateException if called in an invalid state. 2882 */ addTimedTextSource(String path, String mimeType)2883 public void addTimedTextSource(String path, String mimeType) 2884 throws IOException, IllegalArgumentException, IllegalStateException { 2885 if (!availableMimeTypeForExternalSource(mimeType)) { 2886 final String msg = "Illegal mimeType for timed text source: " + mimeType; 2887 throw new IllegalArgumentException(msg); 2888 } 2889 2890 File file = new File(path); 2891 if (file.exists()) { 2892 FileInputStream is = new FileInputStream(file); 2893 FileDescriptor fd = is.getFD(); 2894 addTimedTextSource(fd, mimeType); 2895 is.close(); 2896 } else { 2897 // We do not support the case where the path is not a file. 2898 throw new IOException(path); 2899 } 2900 } 2901 2902 /** 2903 * Adds an external timed text source file (Uri). 2904 * 2905 * Currently supported format is SubRip with the file extension .srt, case insensitive. 2906 * Note that a single external timed text source may contain multiple tracks in it. 2907 * One can find the total number of available tracks using {@link #getTrackInfo()} to see what 2908 * additional tracks become available after this method call. 2909 * 2910 * @param context the Context to use when resolving the Uri 2911 * @param uri the Content URI of the data you want to play 2912 * @param mimeType The mime type of the file. Must be one of the mime types listed above. 2913 * @throws IOException if the file cannot be accessed or is corrupted. 2914 * @throws IllegalArgumentException if the mimeType is not supported. 2915 * @throws IllegalStateException if called in an invalid state. 2916 */ addTimedTextSource(Context context, Uri uri, String mimeType)2917 public void addTimedTextSource(Context context, Uri uri, String mimeType) 2918 throws IOException, IllegalArgumentException, IllegalStateException { 2919 String scheme = uri.getScheme(); 2920 if(scheme == null || scheme.equals("file")) { 2921 addTimedTextSource(uri.getPath(), mimeType); 2922 return; 2923 } 2924 2925 AssetFileDescriptor fd = null; 2926 try { 2927 ContentResolver resolver = context.getContentResolver(); 2928 fd = resolver.openAssetFileDescriptor(uri, "r"); 2929 if (fd == null) { 2930 return; 2931 } 2932 addTimedTextSource(fd.getFileDescriptor(), mimeType); 2933 return; 2934 } catch (SecurityException ex) { 2935 } catch (IOException ex) { 2936 } finally { 2937 if (fd != null) { 2938 fd.close(); 2939 } 2940 } 2941 } 2942 2943 /** 2944 * Adds an external timed text source file (FileDescriptor). 2945 * 2946 * It is the caller's responsibility to close the file descriptor. 2947 * It is safe to do so as soon as this call returns. 2948 * 2949 * Currently supported format is SubRip. Note that a single external timed text source may 2950 * contain multiple tracks in it. One can find the total number of available tracks 2951 * using {@link #getTrackInfo()} to see what additional tracks become available 2952 * after this method call. 2953 * 2954 * @param fd the FileDescriptor for the file you want to play 2955 * @param mimeType The mime type of the file. Must be one of the mime types listed above. 2956 * @throws IllegalArgumentException if the mimeType is not supported. 2957 * @throws IllegalStateException if called in an invalid state. 2958 */ addTimedTextSource(FileDescriptor fd, String mimeType)2959 public void addTimedTextSource(FileDescriptor fd, String mimeType) 2960 throws IllegalArgumentException, IllegalStateException { 2961 // intentionally less than LONG_MAX 2962 addTimedTextSource(fd, 0, 0x7ffffffffffffffL, mimeType); 2963 } 2964 2965 /** 2966 * Adds an external timed text file (FileDescriptor). 2967 * 2968 * It is the caller's responsibility to close the file descriptor. 2969 * It is safe to do so as soon as this call returns. 2970 * 2971 * Currently supported format is SubRip. Note that a single external timed text source may 2972 * contain multiple tracks in it. One can find the total number of available tracks 2973 * using {@link #getTrackInfo()} to see what additional tracks become available 2974 * after this method call. 2975 * 2976 * @param fd the FileDescriptor for the file you want to play 2977 * @param offset the offset into the file where the data to be played starts, in bytes 2978 * @param length the length in bytes of the data to be played 2979 * @param mime The mime type of the file. Must be one of the mime types listed above. 2980 * @throws IllegalArgumentException if the mimeType is not supported. 2981 * @throws IllegalStateException if called in an invalid state. 2982 */ addTimedTextSource(FileDescriptor fd, long offset, long length, String mime)2983 public void addTimedTextSource(FileDescriptor fd, long offset, long length, String mime) 2984 throws IllegalArgumentException, IllegalStateException { 2985 if (!availableMimeTypeForExternalSource(mime)) { 2986 throw new IllegalArgumentException("Illegal mimeType for timed text source: " + mime); 2987 } 2988 2989 final FileDescriptor dupedFd; 2990 try { 2991 dupedFd = Os.dup(fd); 2992 } catch (ErrnoException ex) { 2993 Log.e(TAG, ex.getMessage(), ex); 2994 throw new RuntimeException(ex); 2995 } 2996 2997 final MediaFormat fFormat = new MediaFormat(); 2998 fFormat.setString(MediaFormat.KEY_MIME, mime); 2999 fFormat.setInteger(MediaFormat.KEY_IS_TIMED_TEXT, 1); 3000 3001 // A MediaPlayer created by a VideoView should already have its mSubtitleController set. 3002 if (mSubtitleController == null) { 3003 setSubtitleAnchor(); 3004 } 3005 3006 if (!mSubtitleController.hasRendererFor(fFormat)) { 3007 // test and add not atomic 3008 Context context = ActivityThread.currentApplication(); 3009 mSubtitleController.registerRenderer(new SRTRenderer(context, mEventHandler)); 3010 } 3011 final SubtitleTrack track = mSubtitleController.addTrack(fFormat); 3012 synchronized (mIndexTrackPairs) { 3013 mIndexTrackPairs.add(Pair.<Integer, SubtitleTrack>create(null, track)); 3014 } 3015 3016 getMediaTimeProvider(); 3017 3018 final long offset2 = offset; 3019 final long length2 = length; 3020 final HandlerThread thread = new HandlerThread( 3021 "TimedTextReadThread", 3022 Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_MORE_FAVORABLE); 3023 thread.start(); 3024 Handler handler = new Handler(thread.getLooper()); 3025 handler.post(new Runnable() { 3026 private int addTrack() { 3027 final ByteArrayOutputStream bos = new ByteArrayOutputStream(); 3028 try { 3029 Os.lseek(dupedFd, offset2, OsConstants.SEEK_SET); 3030 byte[] buffer = new byte[4096]; 3031 for (long total = 0; total < length2;) { 3032 int bytesToRead = (int) Math.min(buffer.length, length2 - total); 3033 int bytes = IoBridge.read(dupedFd, buffer, 0, bytesToRead); 3034 if (bytes < 0) { 3035 break; 3036 } else { 3037 bos.write(buffer, 0, bytes); 3038 total += bytes; 3039 } 3040 } 3041 Handler h = mTimeProvider.mEventHandler; 3042 int what = TimeProvider.NOTIFY; 3043 int arg1 = TimeProvider.NOTIFY_TRACK_DATA; 3044 Pair<SubtitleTrack, byte[]> trackData = Pair.create(track, bos.toByteArray()); 3045 Message m = h.obtainMessage(what, arg1, 0, trackData); 3046 h.sendMessage(m); 3047 return MEDIA_INFO_EXTERNAL_METADATA_UPDATE; 3048 } catch (Exception e) { 3049 Log.e(TAG, e.getMessage(), e); 3050 return MEDIA_INFO_TIMED_TEXT_ERROR; 3051 } finally { 3052 try { 3053 Os.close(dupedFd); 3054 } catch (ErrnoException e) { 3055 Log.e(TAG, e.getMessage(), e); 3056 } 3057 } 3058 } 3059 3060 public void run() { 3061 int res = addTrack(); 3062 if (mEventHandler != null) { 3063 Message m = mEventHandler.obtainMessage(MEDIA_INFO, res, 0, null); 3064 mEventHandler.sendMessage(m); 3065 } 3066 thread.getLooper().quitSafely(); 3067 } 3068 }); 3069 } 3070 3071 /** 3072 * Returns the index of the audio, video, or subtitle track currently selected for playback, 3073 * The return value is an index into the array returned by {@link #getTrackInfo()}, and can 3074 * be used in calls to {@link #selectTrack(int)} or {@link #deselectTrack(int)}. 3075 * 3076 * @param trackType should be one of {@link TrackInfo#MEDIA_TRACK_TYPE_VIDEO}, 3077 * {@link TrackInfo#MEDIA_TRACK_TYPE_AUDIO}, or 3078 * {@link TrackInfo#MEDIA_TRACK_TYPE_SUBTITLE} 3079 * @return index of the audio, video, or subtitle track currently selected for playback; 3080 * a negative integer is returned when there is no selected track for {@code trackType} or 3081 * when {@code trackType} is not one of audio, video, or subtitle. 3082 * @throws IllegalStateException if called after {@link #release()} 3083 * 3084 * @see #getTrackInfo() 3085 * @see #selectTrack(int) 3086 * @see #deselectTrack(int) 3087 */ getSelectedTrack(int trackType)3088 public int getSelectedTrack(int trackType) throws IllegalStateException { 3089 if (mSubtitleController != null 3090 && (trackType == TrackInfo.MEDIA_TRACK_TYPE_SUBTITLE 3091 || trackType == TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT)) { 3092 SubtitleTrack subtitleTrack = mSubtitleController.getSelectedTrack(); 3093 if (subtitleTrack != null) { 3094 synchronized (mIndexTrackPairs) { 3095 for (int i = 0; i < mIndexTrackPairs.size(); i++) { 3096 Pair<Integer, SubtitleTrack> p = mIndexTrackPairs.get(i); 3097 if (p.second == subtitleTrack && subtitleTrack.getTrackType() == trackType) { 3098 return i; 3099 } 3100 } 3101 } 3102 } 3103 } 3104 3105 Parcel request = Parcel.obtain(); 3106 Parcel reply = Parcel.obtain(); 3107 try { 3108 request.writeInterfaceToken(IMEDIA_PLAYER); 3109 request.writeInt(INVOKE_ID_GET_SELECTED_TRACK); 3110 request.writeInt(trackType); 3111 invoke(request, reply); 3112 int inbandTrackIndex = reply.readInt(); 3113 synchronized (mIndexTrackPairs) { 3114 for (int i = 0; i < mIndexTrackPairs.size(); i++) { 3115 Pair<Integer, SubtitleTrack> p = mIndexTrackPairs.get(i); 3116 if (p.first != null && p.first == inbandTrackIndex) { 3117 return i; 3118 } 3119 } 3120 } 3121 return -1; 3122 } finally { 3123 request.recycle(); 3124 reply.recycle(); 3125 } 3126 } 3127 3128 /** 3129 * Selects a track. 3130 * <p> 3131 * If a MediaPlayer is in invalid state, it throws an IllegalStateException exception. 3132 * If a MediaPlayer is in <em>Started</em> state, the selected track is presented immediately. 3133 * If a MediaPlayer is not in Started state, it just marks the track to be played. 3134 * </p> 3135 * <p> 3136 * In any valid state, if it is called multiple times on the same type of track (ie. Video, 3137 * Audio, Timed Text), the most recent one will be chosen. 3138 * </p> 3139 * <p> 3140 * The first audio and video tracks are selected by default if available, even though 3141 * this method is not called. However, no timed text track will be selected until 3142 * this function is called. 3143 * </p> 3144 * <p> 3145 * Currently, only timed text, subtitle or audio tracks can be selected via this method. 3146 * In addition, the support for selecting an audio track at runtime is pretty limited 3147 * in that an audio track can only be selected in the <em>Prepared</em> state. 3148 * </p> 3149 * @param index the index of the track to be selected. The valid range of the index 3150 * is 0..total number of track - 1. The total number of tracks as well as the type of 3151 * each individual track can be found by calling {@link #getTrackInfo()} method. 3152 * @throws IllegalStateException if called in an invalid state. 3153 * 3154 * @see android.media.MediaPlayer#getTrackInfo 3155 */ selectTrack(int index)3156 public void selectTrack(int index) throws IllegalStateException { 3157 selectOrDeselectTrack(index, true /* select */); 3158 } 3159 3160 /** 3161 * Deselect a track. 3162 * <p> 3163 * Currently, the track must be a timed text track and no audio or video tracks can be 3164 * deselected. If the timed text track identified by index has not been 3165 * selected before, it throws an exception. 3166 * </p> 3167 * @param index the index of the track to be deselected. The valid range of the index 3168 * is 0..total number of tracks - 1. The total number of tracks as well as the type of 3169 * each individual track can be found by calling {@link #getTrackInfo()} method. 3170 * @throws IllegalStateException if called in an invalid state. 3171 * 3172 * @see android.media.MediaPlayer#getTrackInfo 3173 */ deselectTrack(int index)3174 public void deselectTrack(int index) throws IllegalStateException { 3175 selectOrDeselectTrack(index, false /* select */); 3176 } 3177 selectOrDeselectTrack(int index, boolean select)3178 private void selectOrDeselectTrack(int index, boolean select) 3179 throws IllegalStateException { 3180 // handle subtitle track through subtitle controller 3181 populateInbandTracks(); 3182 3183 Pair<Integer,SubtitleTrack> p = null; 3184 try { 3185 p = mIndexTrackPairs.get(index); 3186 } catch (ArrayIndexOutOfBoundsException e) { 3187 // ignore bad index 3188 return; 3189 } 3190 3191 SubtitleTrack track = p.second; 3192 if (track == null) { 3193 // inband (de)select 3194 selectOrDeselectInbandTrack(p.first, select); 3195 return; 3196 } 3197 3198 if (mSubtitleController == null) { 3199 return; 3200 } 3201 3202 if (!select) { 3203 // out-of-band deselect 3204 if (mSubtitleController.getSelectedTrack() == track) { 3205 mSubtitleController.selectTrack(null); 3206 } else { 3207 Log.w(TAG, "trying to deselect track that was not selected"); 3208 } 3209 return; 3210 } 3211 3212 // out-of-band select 3213 if (track.getTrackType() == TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT) { 3214 int ttIndex = getSelectedTrack(TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT); 3215 synchronized (mIndexTrackPairs) { 3216 if (ttIndex >= 0 && ttIndex < mIndexTrackPairs.size()) { 3217 Pair<Integer,SubtitleTrack> p2 = mIndexTrackPairs.get(ttIndex); 3218 if (p2.first != null && p2.second == null) { 3219 // deselect inband counterpart 3220 selectOrDeselectInbandTrack(p2.first, false); 3221 } 3222 } 3223 } 3224 } 3225 mSubtitleController.selectTrack(track); 3226 } 3227 selectOrDeselectInbandTrack(int index, boolean select)3228 private void selectOrDeselectInbandTrack(int index, boolean select) 3229 throws IllegalStateException { 3230 Parcel request = Parcel.obtain(); 3231 Parcel reply = Parcel.obtain(); 3232 try { 3233 request.writeInterfaceToken(IMEDIA_PLAYER); 3234 request.writeInt(select? INVOKE_ID_SELECT_TRACK: INVOKE_ID_DESELECT_TRACK); 3235 request.writeInt(index); 3236 invoke(request, reply); 3237 } finally { 3238 request.recycle(); 3239 reply.recycle(); 3240 } 3241 } 3242 3243 3244 /** 3245 * @param reply Parcel with audio/video duration info for battery 3246 tracking usage 3247 * @return The status code. 3248 * {@hide} 3249 */ native_pullBatteryData(Parcel reply)3250 public native static int native_pullBatteryData(Parcel reply); 3251 3252 /** 3253 * Sets the target UDP re-transmit endpoint for the low level player. 3254 * Generally, the address portion of the endpoint is an IP multicast 3255 * address, although a unicast address would be equally valid. When a valid 3256 * retransmit endpoint has been set, the media player will not decode and 3257 * render the media presentation locally. Instead, the player will attempt 3258 * to re-multiplex its media data using the Android@Home RTP profile and 3259 * re-transmit to the target endpoint. Receiver devices (which may be 3260 * either the same as the transmitting device or different devices) may 3261 * instantiate, prepare, and start a receiver player using a setDataSource 3262 * URL of the form... 3263 * 3264 * aahRX://<multicastIP>:<port> 3265 * 3266 * to receive, decode and render the re-transmitted content. 3267 * 3268 * setRetransmitEndpoint may only be called before setDataSource has been 3269 * called; while the player is in the Idle state. 3270 * 3271 * @param endpoint the address and UDP port of the re-transmission target or 3272 * null if no re-transmission is to be performed. 3273 * @throws IllegalStateException if it is called in an invalid state 3274 * @throws IllegalArgumentException if the retransmit endpoint is supplied, 3275 * but invalid. 3276 * 3277 * {@hide} pending API council 3278 */ setRetransmitEndpoint(InetSocketAddress endpoint)3279 public void setRetransmitEndpoint(InetSocketAddress endpoint) 3280 throws IllegalStateException, IllegalArgumentException 3281 { 3282 String addrString = null; 3283 int port = 0; 3284 3285 if (null != endpoint) { 3286 addrString = endpoint.getAddress().getHostAddress(); 3287 port = endpoint.getPort(); 3288 } 3289 3290 int ret = native_setRetransmitEndpoint(addrString, port); 3291 if (ret != 0) { 3292 throw new IllegalArgumentException("Illegal re-transmit endpoint; native ret " + ret); 3293 } 3294 } 3295 native_setRetransmitEndpoint(String addrString, int port)3296 private native final int native_setRetransmitEndpoint(String addrString, int port); 3297 3298 @Override finalize()3299 protected void finalize() { 3300 baseRelease(); 3301 native_finalize(); 3302 } 3303 3304 /* Do not change these values without updating their counterparts 3305 * in include/media/mediaplayer.h! 3306 */ 3307 private static final int MEDIA_NOP = 0; // interface test message 3308 private static final int MEDIA_PREPARED = 1; 3309 private static final int MEDIA_PLAYBACK_COMPLETE = 2; 3310 private static final int MEDIA_BUFFERING_UPDATE = 3; 3311 private static final int MEDIA_SEEK_COMPLETE = 4; 3312 private static final int MEDIA_SET_VIDEO_SIZE = 5; 3313 private static final int MEDIA_STARTED = 6; 3314 private static final int MEDIA_PAUSED = 7; 3315 private static final int MEDIA_STOPPED = 8; 3316 private static final int MEDIA_SKIPPED = 9; 3317 private static final int MEDIA_NOTIFY_TIME = 98; 3318 private static final int MEDIA_TIMED_TEXT = 99; 3319 private static final int MEDIA_ERROR = 100; 3320 private static final int MEDIA_INFO = 200; 3321 private static final int MEDIA_SUBTITLE_DATA = 201; 3322 private static final int MEDIA_META_DATA = 202; 3323 private static final int MEDIA_DRM_INFO = 210; 3324 private static final int MEDIA_TIME_DISCONTINUITY = 211; 3325 private static final int MEDIA_AUDIO_ROUTING_CHANGED = 10000; 3326 3327 private TimeProvider mTimeProvider; 3328 3329 /** @hide */ getMediaTimeProvider()3330 public MediaTimeProvider getMediaTimeProvider() { 3331 if (mTimeProvider == null) { 3332 mTimeProvider = new TimeProvider(this); 3333 } 3334 return mTimeProvider; 3335 } 3336 3337 private class EventHandler extends Handler 3338 { 3339 private MediaPlayer mMediaPlayer; 3340 EventHandler(MediaPlayer mp, Looper looper)3341 public EventHandler(MediaPlayer mp, Looper looper) { 3342 super(looper); 3343 mMediaPlayer = mp; 3344 } 3345 3346 @Override handleMessage(Message msg)3347 public void handleMessage(Message msg) { 3348 if (mMediaPlayer.mNativeContext == 0) { 3349 Log.w(TAG, "mediaplayer went away with unhandled events"); 3350 return; 3351 } 3352 switch(msg.what) { 3353 case MEDIA_PREPARED: 3354 try { 3355 scanInternalSubtitleTracks(); 3356 } catch (RuntimeException e) { 3357 // send error message instead of crashing; 3358 // send error message instead of inlining a call to onError 3359 // to avoid code duplication. 3360 Message msg2 = obtainMessage( 3361 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, MEDIA_ERROR_UNSUPPORTED, null); 3362 sendMessage(msg2); 3363 } 3364 3365 OnPreparedListener onPreparedListener = mOnPreparedListener; 3366 if (onPreparedListener != null) 3367 onPreparedListener.onPrepared(mMediaPlayer); 3368 return; 3369 3370 case MEDIA_DRM_INFO: 3371 Log.v(TAG, "MEDIA_DRM_INFO " + mOnDrmInfoHandlerDelegate); 3372 3373 if (msg.obj == null) { 3374 Log.w(TAG, "MEDIA_DRM_INFO msg.obj=NULL"); 3375 } else if (msg.obj instanceof Parcel) { 3376 // The parcel was parsed already in postEventFromNative 3377 DrmInfo drmInfo = null; 3378 3379 OnDrmInfoHandlerDelegate onDrmInfoHandlerDelegate; 3380 synchronized (mDrmLock) { 3381 if (mOnDrmInfoHandlerDelegate != null && mDrmInfo != null) { 3382 drmInfo = mDrmInfo.makeCopy(); 3383 } 3384 // local copy while keeping the lock 3385 onDrmInfoHandlerDelegate = mOnDrmInfoHandlerDelegate; 3386 } 3387 3388 // notifying the client outside the lock 3389 if (onDrmInfoHandlerDelegate != null) { 3390 onDrmInfoHandlerDelegate.notifyClient(drmInfo); 3391 } 3392 } else { 3393 Log.w(TAG, "MEDIA_DRM_INFO msg.obj of unexpected type " + msg.obj); 3394 } 3395 return; 3396 3397 case MEDIA_PLAYBACK_COMPLETE: 3398 { 3399 mOnCompletionInternalListener.onCompletion(mMediaPlayer); 3400 OnCompletionListener onCompletionListener = mOnCompletionListener; 3401 if (onCompletionListener != null) 3402 onCompletionListener.onCompletion(mMediaPlayer); 3403 } 3404 stayAwake(false); 3405 return; 3406 3407 case MEDIA_STOPPED: 3408 { 3409 TimeProvider timeProvider = mTimeProvider; 3410 if (timeProvider != null) { 3411 timeProvider.onStopped(); 3412 } 3413 } 3414 break; 3415 3416 case MEDIA_STARTED: 3417 case MEDIA_PAUSED: 3418 { 3419 TimeProvider timeProvider = mTimeProvider; 3420 if (timeProvider != null) { 3421 timeProvider.onPaused(msg.what == MEDIA_PAUSED); 3422 } 3423 } 3424 break; 3425 3426 case MEDIA_BUFFERING_UPDATE: 3427 OnBufferingUpdateListener onBufferingUpdateListener = mOnBufferingUpdateListener; 3428 if (onBufferingUpdateListener != null) 3429 onBufferingUpdateListener.onBufferingUpdate(mMediaPlayer, msg.arg1); 3430 return; 3431 3432 case MEDIA_SEEK_COMPLETE: 3433 OnSeekCompleteListener onSeekCompleteListener = mOnSeekCompleteListener; 3434 if (onSeekCompleteListener != null) { 3435 onSeekCompleteListener.onSeekComplete(mMediaPlayer); 3436 } 3437 // fall through 3438 3439 case MEDIA_SKIPPED: 3440 { 3441 TimeProvider timeProvider = mTimeProvider; 3442 if (timeProvider != null) { 3443 timeProvider.onSeekComplete(mMediaPlayer); 3444 } 3445 } 3446 return; 3447 3448 case MEDIA_SET_VIDEO_SIZE: 3449 OnVideoSizeChangedListener onVideoSizeChangedListener = mOnVideoSizeChangedListener; 3450 if (onVideoSizeChangedListener != null) { 3451 onVideoSizeChangedListener.onVideoSizeChanged( 3452 mMediaPlayer, msg.arg1, msg.arg2); 3453 } 3454 return; 3455 3456 case MEDIA_ERROR: 3457 Log.e(TAG, "Error (" + msg.arg1 + "," + msg.arg2 + ")"); 3458 boolean error_was_handled = false; 3459 OnErrorListener onErrorListener = mOnErrorListener; 3460 if (onErrorListener != null) { 3461 error_was_handled = onErrorListener.onError(mMediaPlayer, msg.arg1, msg.arg2); 3462 } 3463 { 3464 mOnCompletionInternalListener.onCompletion(mMediaPlayer); 3465 OnCompletionListener onCompletionListener = mOnCompletionListener; 3466 if (onCompletionListener != null && ! error_was_handled) { 3467 onCompletionListener.onCompletion(mMediaPlayer); 3468 } 3469 } 3470 stayAwake(false); 3471 return; 3472 3473 case MEDIA_INFO: 3474 switch (msg.arg1) { 3475 case MEDIA_INFO_VIDEO_TRACK_LAGGING: 3476 Log.i(TAG, "Info (" + msg.arg1 + "," + msg.arg2 + ")"); 3477 break; 3478 case MEDIA_INFO_METADATA_UPDATE: 3479 try { 3480 scanInternalSubtitleTracks(); 3481 } catch (RuntimeException e) { 3482 Message msg2 = obtainMessage( 3483 MEDIA_ERROR, MEDIA_ERROR_UNKNOWN, MEDIA_ERROR_UNSUPPORTED, null); 3484 sendMessage(msg2); 3485 } 3486 // fall through 3487 3488 case MEDIA_INFO_EXTERNAL_METADATA_UPDATE: 3489 msg.arg1 = MEDIA_INFO_METADATA_UPDATE; 3490 // update default track selection 3491 if (mSubtitleController != null) { 3492 mSubtitleController.selectDefaultTrack(); 3493 } 3494 break; 3495 case MEDIA_INFO_BUFFERING_START: 3496 case MEDIA_INFO_BUFFERING_END: 3497 TimeProvider timeProvider = mTimeProvider; 3498 if (timeProvider != null) { 3499 timeProvider.onBuffering(msg.arg1 == MEDIA_INFO_BUFFERING_START); 3500 } 3501 break; 3502 } 3503 3504 OnInfoListener onInfoListener = mOnInfoListener; 3505 if (onInfoListener != null) { 3506 onInfoListener.onInfo(mMediaPlayer, msg.arg1, msg.arg2); 3507 } 3508 // No real default action so far. 3509 return; 3510 3511 case MEDIA_NOTIFY_TIME: 3512 TimeProvider timeProvider = mTimeProvider; 3513 if (timeProvider != null) { 3514 timeProvider.onNotifyTime(); 3515 } 3516 return; 3517 3518 case MEDIA_TIMED_TEXT: 3519 OnTimedTextListener onTimedTextListener = mOnTimedTextListener; 3520 if (onTimedTextListener == null) 3521 return; 3522 if (msg.obj == null) { 3523 onTimedTextListener.onTimedText(mMediaPlayer, null); 3524 } else { 3525 if (msg.obj instanceof Parcel) { 3526 Parcel parcel = (Parcel)msg.obj; 3527 TimedText text = new TimedText(parcel); 3528 parcel.recycle(); 3529 onTimedTextListener.onTimedText(mMediaPlayer, text); 3530 } 3531 } 3532 return; 3533 3534 case MEDIA_SUBTITLE_DATA: 3535 final OnSubtitleDataListener extSubtitleListener; 3536 final Handler extSubtitleHandler; 3537 synchronized(this) { 3538 if (mSubtitleDataListenerDisabled) { 3539 return; 3540 } 3541 extSubtitleListener = mExtSubtitleDataListener; 3542 extSubtitleHandler = mExtSubtitleDataHandler; 3543 } 3544 if (msg.obj instanceof Parcel) { 3545 Parcel parcel = (Parcel) msg.obj; 3546 final SubtitleData data = new SubtitleData(parcel); 3547 parcel.recycle(); 3548 3549 mIntSubtitleDataListener.onSubtitleData(mMediaPlayer, data); 3550 3551 if (extSubtitleListener != null) { 3552 if (extSubtitleHandler == null) { 3553 extSubtitleListener.onSubtitleData(mMediaPlayer, data); 3554 } else { 3555 extSubtitleHandler.post(new Runnable() { 3556 @Override 3557 public void run() { 3558 extSubtitleListener.onSubtitleData(mMediaPlayer, data); 3559 } 3560 }); 3561 } 3562 } 3563 } 3564 return; 3565 3566 case MEDIA_META_DATA: 3567 OnTimedMetaDataAvailableListener onTimedMetaDataAvailableListener = 3568 mOnTimedMetaDataAvailableListener; 3569 if (onTimedMetaDataAvailableListener == null) { 3570 return; 3571 } 3572 if (msg.obj instanceof Parcel) { 3573 Parcel parcel = (Parcel) msg.obj; 3574 TimedMetaData data = TimedMetaData.createTimedMetaDataFromParcel(parcel); 3575 parcel.recycle(); 3576 onTimedMetaDataAvailableListener.onTimedMetaDataAvailable(mMediaPlayer, data); 3577 } 3578 return; 3579 3580 case MEDIA_NOP: // interface test message - ignore 3581 break; 3582 3583 case MEDIA_AUDIO_ROUTING_CHANGED: 3584 AudioManager.resetAudioPortGeneration(); 3585 synchronized (mRoutingChangeListeners) { 3586 for (NativeRoutingEventHandlerDelegate delegate 3587 : mRoutingChangeListeners.values()) { 3588 delegate.notifyClient(); 3589 } 3590 } 3591 return; 3592 3593 case MEDIA_TIME_DISCONTINUITY: 3594 final OnMediaTimeDiscontinuityListener mediaTimeListener; 3595 final Handler mediaTimeHandler; 3596 synchronized(this) { 3597 mediaTimeListener = mOnMediaTimeDiscontinuityListener; 3598 mediaTimeHandler = mOnMediaTimeDiscontinuityHandler; 3599 } 3600 if (mediaTimeListener == null) { 3601 return; 3602 } 3603 if (msg.obj instanceof Parcel) { 3604 Parcel parcel = (Parcel) msg.obj; 3605 parcel.setDataPosition(0); 3606 long anchorMediaUs = parcel.readLong(); 3607 long anchorRealUs = parcel.readLong(); 3608 float playbackRate = parcel.readFloat(); 3609 parcel.recycle(); 3610 final MediaTimestamp timestamp; 3611 if (anchorMediaUs != -1 && anchorRealUs != -1) { 3612 timestamp = new MediaTimestamp( 3613 anchorMediaUs /*Us*/, anchorRealUs * 1000 /*Ns*/, playbackRate); 3614 } else { 3615 timestamp = MediaTimestamp.TIMESTAMP_UNKNOWN; 3616 } 3617 if (mediaTimeHandler == null) { 3618 mediaTimeListener.onMediaTimeDiscontinuity(mMediaPlayer, timestamp); 3619 } else { 3620 mediaTimeHandler.post(new Runnable() { 3621 @Override 3622 public void run() { 3623 mediaTimeListener.onMediaTimeDiscontinuity(mMediaPlayer, timestamp); 3624 } 3625 }); 3626 } 3627 } 3628 return; 3629 3630 default: 3631 Log.e(TAG, "Unknown message type " + msg.what); 3632 return; 3633 } 3634 } 3635 } 3636 3637 /* 3638 * Called from native code when an interesting event happens. This method 3639 * just uses the EventHandler system to post the event back to the main app thread. 3640 * We use a weak reference to the original MediaPlayer object so that the native 3641 * code is safe from the object disappearing from underneath it. (This is 3642 * the cookie passed to native_setup().) 3643 */ postEventFromNative(Object mediaplayer_ref, int what, int arg1, int arg2, Object obj)3644 private static void postEventFromNative(Object mediaplayer_ref, 3645 int what, int arg1, int arg2, Object obj) 3646 { 3647 final MediaPlayer mp = (MediaPlayer)((WeakReference)mediaplayer_ref).get(); 3648 if (mp == null) { 3649 return; 3650 } 3651 3652 switch (what) { 3653 case MEDIA_INFO: 3654 if (arg1 == MEDIA_INFO_STARTED_AS_NEXT) { 3655 new Thread(new Runnable() { 3656 @Override 3657 public void run() { 3658 // this acquires the wakelock if needed, and sets the client side state 3659 mp.start(); 3660 } 3661 }).start(); 3662 Thread.yield(); 3663 } 3664 break; 3665 3666 case MEDIA_DRM_INFO: 3667 // We need to derive mDrmInfo before prepare() returns so processing it here 3668 // before the notification is sent to EventHandler below. EventHandler runs in the 3669 // notification looper so its handleMessage might process the event after prepare() 3670 // has returned. 3671 Log.v(TAG, "postEventFromNative MEDIA_DRM_INFO"); 3672 if (obj instanceof Parcel) { 3673 Parcel parcel = (Parcel)obj; 3674 DrmInfo drmInfo = new DrmInfo(parcel); 3675 synchronized (mp.mDrmLock) { 3676 mp.mDrmInfo = drmInfo; 3677 } 3678 } else { 3679 Log.w(TAG, "MEDIA_DRM_INFO msg.obj of unexpected type " + obj); 3680 } 3681 break; 3682 3683 case MEDIA_PREPARED: 3684 // By this time, we've learned about DrmInfo's presence or absence. This is meant 3685 // mainly for prepareAsync() use case. For prepare(), this still can run to a race 3686 // condition b/c MediaPlayerNative releases the prepare() lock before calling notify 3687 // so we also set mDrmInfoResolved in prepare(). 3688 synchronized (mp.mDrmLock) { 3689 mp.mDrmInfoResolved = true; 3690 } 3691 break; 3692 3693 } 3694 3695 if (mp.mEventHandler != null) { 3696 Message m = mp.mEventHandler.obtainMessage(what, arg1, arg2, obj); 3697 mp.mEventHandler.sendMessage(m); 3698 } 3699 } 3700 3701 /** 3702 * Interface definition for a callback to be invoked when the media 3703 * source is ready for playback. 3704 */ 3705 public interface OnPreparedListener 3706 { 3707 /** 3708 * Called when the media file is ready for playback. 3709 * 3710 * @param mp the MediaPlayer that is ready for playback 3711 */ onPrepared(MediaPlayer mp)3712 void onPrepared(MediaPlayer mp); 3713 } 3714 3715 /** 3716 * Register a callback to be invoked when the media source is ready 3717 * for playback. 3718 * 3719 * @param listener the callback that will be run 3720 */ setOnPreparedListener(OnPreparedListener listener)3721 public void setOnPreparedListener(OnPreparedListener listener) 3722 { 3723 mOnPreparedListener = listener; 3724 } 3725 3726 private OnPreparedListener mOnPreparedListener; 3727 3728 /** 3729 * Interface definition for a callback to be invoked when playback of 3730 * a media source has completed. 3731 */ 3732 public interface OnCompletionListener 3733 { 3734 /** 3735 * Called when the end of a media source is reached during playback. 3736 * 3737 * @param mp the MediaPlayer that reached the end of the file 3738 */ onCompletion(MediaPlayer mp)3739 void onCompletion(MediaPlayer mp); 3740 } 3741 3742 /** 3743 * Register a callback to be invoked when the end of a media source 3744 * has been reached during playback. 3745 * 3746 * @param listener the callback that will be run 3747 */ setOnCompletionListener(OnCompletionListener listener)3748 public void setOnCompletionListener(OnCompletionListener listener) 3749 { 3750 mOnCompletionListener = listener; 3751 } 3752 3753 private OnCompletionListener mOnCompletionListener; 3754 3755 /** 3756 * @hide 3757 * Internal completion listener to update PlayerBase of the play state. Always "registered". 3758 */ 3759 private final OnCompletionListener mOnCompletionInternalListener = new OnCompletionListener() { 3760 @Override 3761 public void onCompletion(MediaPlayer mp) { 3762 baseStop(); 3763 } 3764 }; 3765 3766 /** 3767 * Interface definition of a callback to be invoked indicating buffering 3768 * status of a media resource being streamed over the network. 3769 */ 3770 public interface OnBufferingUpdateListener 3771 { 3772 /** 3773 * Called to update status in buffering a media stream received through 3774 * progressive HTTP download. The received buffering percentage 3775 * indicates how much of the content has been buffered or played. 3776 * For example a buffering update of 80 percent when half the content 3777 * has already been played indicates that the next 30 percent of the 3778 * content to play has been buffered. 3779 * 3780 * @param mp the MediaPlayer the update pertains to 3781 * @param percent the percentage (0-100) of the content 3782 * that has been buffered or played thus far 3783 */ onBufferingUpdate(MediaPlayer mp, int percent)3784 void onBufferingUpdate(MediaPlayer mp, int percent); 3785 } 3786 3787 /** 3788 * Register a callback to be invoked when the status of a network 3789 * stream's buffer has changed. 3790 * 3791 * @param listener the callback that will be run. 3792 */ setOnBufferingUpdateListener(OnBufferingUpdateListener listener)3793 public void setOnBufferingUpdateListener(OnBufferingUpdateListener listener) 3794 { 3795 mOnBufferingUpdateListener = listener; 3796 } 3797 3798 private OnBufferingUpdateListener mOnBufferingUpdateListener; 3799 3800 /** 3801 * Interface definition of a callback to be invoked indicating 3802 * the completion of a seek operation. 3803 */ 3804 public interface OnSeekCompleteListener 3805 { 3806 /** 3807 * Called to indicate the completion of a seek operation. 3808 * 3809 * @param mp the MediaPlayer that issued the seek operation 3810 */ onSeekComplete(MediaPlayer mp)3811 public void onSeekComplete(MediaPlayer mp); 3812 } 3813 3814 /** 3815 * Register a callback to be invoked when a seek operation has been 3816 * completed. 3817 * 3818 * @param listener the callback that will be run 3819 */ setOnSeekCompleteListener(OnSeekCompleteListener listener)3820 public void setOnSeekCompleteListener(OnSeekCompleteListener listener) 3821 { 3822 mOnSeekCompleteListener = listener; 3823 } 3824 3825 private OnSeekCompleteListener mOnSeekCompleteListener; 3826 3827 /** 3828 * Interface definition of a callback to be invoked when the 3829 * video size is first known or updated 3830 */ 3831 public interface OnVideoSizeChangedListener 3832 { 3833 /** 3834 * Called to indicate the video size 3835 * 3836 * The video size (width and height) could be 0 if there was no video, 3837 * no display surface was set, or the value was not determined yet. 3838 * 3839 * @param mp the MediaPlayer associated with this callback 3840 * @param width the width of the video 3841 * @param height the height of the video 3842 */ onVideoSizeChanged(MediaPlayer mp, int width, int height)3843 public void onVideoSizeChanged(MediaPlayer mp, int width, int height); 3844 } 3845 3846 /** 3847 * Register a callback to be invoked when the video size is 3848 * known or updated. 3849 * 3850 * @param listener the callback that will be run 3851 */ setOnVideoSizeChangedListener(OnVideoSizeChangedListener listener)3852 public void setOnVideoSizeChangedListener(OnVideoSizeChangedListener listener) 3853 { 3854 mOnVideoSizeChangedListener = listener; 3855 } 3856 3857 private OnVideoSizeChangedListener mOnVideoSizeChangedListener; 3858 3859 /** 3860 * Interface definition of a callback to be invoked when a 3861 * timed text is available for display. 3862 */ 3863 public interface OnTimedTextListener 3864 { 3865 /** 3866 * Called to indicate an avaliable timed text 3867 * 3868 * @param mp the MediaPlayer associated with this callback 3869 * @param text the timed text sample which contains the text 3870 * needed to be displayed and the display format. 3871 */ onTimedText(MediaPlayer mp, TimedText text)3872 public void onTimedText(MediaPlayer mp, TimedText text); 3873 } 3874 3875 /** 3876 * Register a callback to be invoked when a timed text is available 3877 * for display. 3878 * 3879 * @param listener the callback that will be run 3880 */ setOnTimedTextListener(OnTimedTextListener listener)3881 public void setOnTimedTextListener(OnTimedTextListener listener) 3882 { 3883 mOnTimedTextListener = listener; 3884 } 3885 3886 private OnTimedTextListener mOnTimedTextListener; 3887 3888 /** 3889 * Interface definition of a callback to be invoked when a player subtitle track has new 3890 * subtitle data available. 3891 * See the {@link MediaPlayer#setOnSubtitleDataListener(OnSubtitleDataListener, Handler)} 3892 * method for the description of which track will report data through this listener. 3893 */ 3894 public interface OnSubtitleDataListener { 3895 /** 3896 * Method called when new subtitle data is available 3897 * @param mp the player that reports the new subtitle data 3898 * @param data the subtitle data 3899 */ onSubtitleData(@onNull MediaPlayer mp, @NonNull SubtitleData data)3900 public void onSubtitleData(@NonNull MediaPlayer mp, @NonNull SubtitleData data); 3901 } 3902 3903 /** 3904 * Sets the listener to be invoked when a subtitle track has new data available. 3905 * The subtitle data comes from a subtitle track previously selected with 3906 * {@link #selectTrack(int)}. Use {@link #getTrackInfo()} to determine which tracks are 3907 * subtitles (of type {@link TrackInfo#MEDIA_TRACK_TYPE_SUBTITLE}), Subtitle track encodings 3908 * can be determined by {@link TrackInfo#getFormat()}).<br> 3909 * See {@link SubtitleData} for an example of querying subtitle encoding. 3910 * @param listener the listener called when new data is available 3911 * @param handler the {@link Handler} that receives the listener events 3912 */ setOnSubtitleDataListener(@onNull OnSubtitleDataListener listener, @NonNull Handler handler)3913 public void setOnSubtitleDataListener(@NonNull OnSubtitleDataListener listener, 3914 @NonNull Handler handler) { 3915 if (listener == null) { 3916 throw new IllegalArgumentException("Illegal null listener"); 3917 } 3918 if (handler == null) { 3919 throw new IllegalArgumentException("Illegal null handler"); 3920 } 3921 setOnSubtitleDataListenerInt(listener, handler); 3922 } 3923 /** 3924 * Sets the listener to be invoked when a subtitle track has new data available. 3925 * The subtitle data comes from a subtitle track previously selected with 3926 * {@link #selectTrack(int)}. Use {@link #getTrackInfo()} to determine which tracks are 3927 * subtitles (of type {@link TrackInfo#MEDIA_TRACK_TYPE_SUBTITLE}), Subtitle track encodings 3928 * can be determined by {@link TrackInfo#getFormat()}).<br> 3929 * See {@link SubtitleData} for an example of querying subtitle encoding.<br> 3930 * The listener will be called on the same thread as the one in which the MediaPlayer was 3931 * created. 3932 * @param listener the listener called when new data is available 3933 */ setOnSubtitleDataListener(@onNull OnSubtitleDataListener listener)3934 public void setOnSubtitleDataListener(@NonNull OnSubtitleDataListener listener) 3935 { 3936 if (listener == null) { 3937 throw new IllegalArgumentException("Illegal null listener"); 3938 } 3939 setOnSubtitleDataListenerInt(listener, null); 3940 } 3941 3942 /** 3943 * Clears the listener previously set with 3944 * {@link #setOnSubtitleDataListener(OnSubtitleDataListener)} or 3945 * {@link #setOnSubtitleDataListener(OnSubtitleDataListener, Handler)}. 3946 */ clearOnSubtitleDataListener()3947 public void clearOnSubtitleDataListener() { 3948 setOnSubtitleDataListenerInt(null, null); 3949 } 3950 setOnSubtitleDataListenerInt( @ullable OnSubtitleDataListener listener, @Nullable Handler handler)3951 private void setOnSubtitleDataListenerInt( 3952 @Nullable OnSubtitleDataListener listener, @Nullable Handler handler) { 3953 synchronized (this) { 3954 mExtSubtitleDataListener = listener; 3955 mExtSubtitleDataHandler = handler; 3956 } 3957 } 3958 3959 private boolean mSubtitleDataListenerDisabled; 3960 /** External OnSubtitleDataListener, the one set by {@link #setOnSubtitleDataListener}. */ 3961 private OnSubtitleDataListener mExtSubtitleDataListener; 3962 private Handler mExtSubtitleDataHandler; 3963 3964 /** 3965 * Interface definition of a callback to be invoked when discontinuity in the normal progression 3966 * of the media time is detected. 3967 * The "normal progression" of media time is defined as the expected increase of the playback 3968 * position when playing media, relative to the playback speed (for instance every second, media 3969 * time increases by two seconds when playing at 2x).<br> 3970 * Discontinuities are encountered in the following cases: 3971 * <ul> 3972 * <li>when the player is starved for data and cannot play anymore</li> 3973 * <li>when the player encounters a playback error</li> 3974 * <li>when the a seek operation starts, and when it's completed</li> 3975 * <li>when the playback speed changes</li> 3976 * <li>when the playback state changes</li> 3977 * <li>when the player is reset</li> 3978 * </ul> 3979 * See the 3980 * {@link MediaPlayer#setOnMediaTimeDiscontinuityListener(OnMediaTimeDiscontinuityListener, Handler)} 3981 * method to set a listener for these events. 3982 */ 3983 public interface OnMediaTimeDiscontinuityListener { 3984 /** 3985 * Called to indicate a time discontinuity has occured. 3986 * @param mp the MediaPlayer for which the discontinuity has occured. 3987 * @param mts the timestamp that correlates media time, system time and clock rate, 3988 * or {@link MediaTimestamp#TIMESTAMP_UNKNOWN} in an error case. 3989 */ onMediaTimeDiscontinuity(@onNull MediaPlayer mp, @NonNull MediaTimestamp mts)3990 public void onMediaTimeDiscontinuity(@NonNull MediaPlayer mp, @NonNull MediaTimestamp mts); 3991 } 3992 3993 /** 3994 * Sets the listener to be invoked when a media time discontinuity is encountered. 3995 * @param listener the listener called after a discontinuity 3996 * @param handler the {@link Handler} that receives the listener events 3997 */ setOnMediaTimeDiscontinuityListener( @onNull OnMediaTimeDiscontinuityListener listener, @NonNull Handler handler)3998 public void setOnMediaTimeDiscontinuityListener( 3999 @NonNull OnMediaTimeDiscontinuityListener listener, @NonNull Handler handler) { 4000 if (listener == null) { 4001 throw new IllegalArgumentException("Illegal null listener"); 4002 } 4003 if (handler == null) { 4004 throw new IllegalArgumentException("Illegal null handler"); 4005 } 4006 setOnMediaTimeDiscontinuityListenerInt(listener, handler); 4007 } 4008 4009 /** 4010 * Sets the listener to be invoked when a media time discontinuity is encountered. 4011 * The listener will be called on the same thread as the one in which the MediaPlayer was 4012 * created. 4013 * @param listener the listener called after a discontinuity 4014 */ setOnMediaTimeDiscontinuityListener( @onNull OnMediaTimeDiscontinuityListener listener)4015 public void setOnMediaTimeDiscontinuityListener( 4016 @NonNull OnMediaTimeDiscontinuityListener listener) 4017 { 4018 if (listener == null) { 4019 throw new IllegalArgumentException("Illegal null listener"); 4020 } 4021 setOnMediaTimeDiscontinuityListenerInt(listener, null); 4022 } 4023 4024 /** 4025 * Clears the listener previously set with 4026 * {@link #setOnMediaTimeDiscontinuityListener(OnMediaTimeDiscontinuityListener)} 4027 * or {@link #setOnMediaTimeDiscontinuityListener(OnMediaTimeDiscontinuityListener, Handler)} 4028 */ clearOnMediaTimeDiscontinuityListener()4029 public void clearOnMediaTimeDiscontinuityListener() { 4030 setOnMediaTimeDiscontinuityListenerInt(null, null); 4031 } 4032 setOnMediaTimeDiscontinuityListenerInt( @ullable OnMediaTimeDiscontinuityListener listener, @Nullable Handler handler)4033 private void setOnMediaTimeDiscontinuityListenerInt( 4034 @Nullable OnMediaTimeDiscontinuityListener listener, @Nullable Handler handler) { 4035 synchronized (this) { 4036 mOnMediaTimeDiscontinuityListener = listener; 4037 mOnMediaTimeDiscontinuityHandler = handler; 4038 } 4039 } 4040 4041 private OnMediaTimeDiscontinuityListener mOnMediaTimeDiscontinuityListener; 4042 private Handler mOnMediaTimeDiscontinuityHandler; 4043 4044 /** 4045 * Interface definition of a callback to be invoked when a 4046 * track has timed metadata available. 4047 * 4048 * @see MediaPlayer#setOnTimedMetaDataAvailableListener(OnTimedMetaDataAvailableListener) 4049 */ 4050 public interface OnTimedMetaDataAvailableListener 4051 { 4052 /** 4053 * Called to indicate avaliable timed metadata 4054 * <p> 4055 * This method will be called as timed metadata is extracted from the media, 4056 * in the same order as it occurs in the media. The timing of this event is 4057 * not controlled by the associated timestamp. 4058 * 4059 * @param mp the MediaPlayer associated with this callback 4060 * @param data the timed metadata sample associated with this event 4061 */ onTimedMetaDataAvailable(MediaPlayer mp, TimedMetaData data)4062 public void onTimedMetaDataAvailable(MediaPlayer mp, TimedMetaData data); 4063 } 4064 4065 /** 4066 * Register a callback to be invoked when a selected track has timed metadata available. 4067 * <p> 4068 * Currently only HTTP live streaming data URI's embedded with timed ID3 tags generates 4069 * {@link TimedMetaData}. 4070 * 4071 * @see MediaPlayer#selectTrack(int) 4072 * @see MediaPlayer.OnTimedMetaDataAvailableListener 4073 * @see TimedMetaData 4074 * 4075 * @param listener the callback that will be run 4076 */ setOnTimedMetaDataAvailableListener(OnTimedMetaDataAvailableListener listener)4077 public void setOnTimedMetaDataAvailableListener(OnTimedMetaDataAvailableListener listener) 4078 { 4079 mOnTimedMetaDataAvailableListener = listener; 4080 } 4081 4082 private OnTimedMetaDataAvailableListener mOnTimedMetaDataAvailableListener; 4083 4084 /* Do not change these values without updating their counterparts 4085 * in include/media/mediaplayer.h! 4086 */ 4087 /** Unspecified media player error. 4088 * @see android.media.MediaPlayer.OnErrorListener 4089 */ 4090 public static final int MEDIA_ERROR_UNKNOWN = 1; 4091 4092 /** Media server died. In this case, the application must release the 4093 * MediaPlayer object and instantiate a new one. 4094 * @see android.media.MediaPlayer.OnErrorListener 4095 */ 4096 public static final int MEDIA_ERROR_SERVER_DIED = 100; 4097 4098 /** The video is streamed and its container is not valid for progressive 4099 * playback i.e the video's index (e.g moov atom) is not at the start of the 4100 * file. 4101 * @see android.media.MediaPlayer.OnErrorListener 4102 */ 4103 public static final int MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK = 200; 4104 4105 /** File or network related operation errors. */ 4106 public static final int MEDIA_ERROR_IO = -1004; 4107 /** Bitstream is not conforming to the related coding standard or file spec. */ 4108 public static final int MEDIA_ERROR_MALFORMED = -1007; 4109 /** Bitstream is conforming to the related coding standard or file spec, but 4110 * the media framework does not support the feature. */ 4111 public static final int MEDIA_ERROR_UNSUPPORTED = -1010; 4112 /** Some operation takes too long to complete, usually more than 3-5 seconds. */ 4113 public static final int MEDIA_ERROR_TIMED_OUT = -110; 4114 4115 /** Unspecified low-level system error. This value originated from UNKNOWN_ERROR in 4116 * system/core/include/utils/Errors.h 4117 * @see android.media.MediaPlayer.OnErrorListener 4118 * @hide 4119 */ 4120 public static final int MEDIA_ERROR_SYSTEM = -2147483648; 4121 4122 /** 4123 * Interface definition of a callback to be invoked when there 4124 * has been an error during an asynchronous operation (other errors 4125 * will throw exceptions at method call time). 4126 */ 4127 public interface OnErrorListener 4128 { 4129 /** 4130 * Called to indicate an error. 4131 * 4132 * @param mp the MediaPlayer the error pertains to 4133 * @param what the type of error that has occurred: 4134 * <ul> 4135 * <li>{@link #MEDIA_ERROR_UNKNOWN} 4136 * <li>{@link #MEDIA_ERROR_SERVER_DIED} 4137 * </ul> 4138 * @param extra an extra code, specific to the error. Typically 4139 * implementation dependent. 4140 * <ul> 4141 * <li>{@link #MEDIA_ERROR_IO} 4142 * <li>{@link #MEDIA_ERROR_MALFORMED} 4143 * <li>{@link #MEDIA_ERROR_UNSUPPORTED} 4144 * <li>{@link #MEDIA_ERROR_TIMED_OUT} 4145 * <li><code>MEDIA_ERROR_SYSTEM (-2147483648)</code> - low-level system error. 4146 * </ul> 4147 * @return True if the method handled the error, false if it didn't. 4148 * Returning false, or not having an OnErrorListener at all, will 4149 * cause the OnCompletionListener to be called. 4150 */ onError(MediaPlayer mp, int what, int extra)4151 boolean onError(MediaPlayer mp, int what, int extra); 4152 } 4153 4154 /** 4155 * Register a callback to be invoked when an error has happened 4156 * during an asynchronous operation. 4157 * 4158 * @param listener the callback that will be run 4159 */ setOnErrorListener(OnErrorListener listener)4160 public void setOnErrorListener(OnErrorListener listener) 4161 { 4162 mOnErrorListener = listener; 4163 } 4164 4165 private OnErrorListener mOnErrorListener; 4166 4167 4168 /* Do not change these values without updating their counterparts 4169 * in include/media/mediaplayer.h! 4170 */ 4171 /** Unspecified media player info. 4172 * @see android.media.MediaPlayer.OnInfoListener 4173 */ 4174 public static final int MEDIA_INFO_UNKNOWN = 1; 4175 4176 /** The player was started because it was used as the next player for another 4177 * player, which just completed playback. 4178 * @see android.media.MediaPlayer#setNextMediaPlayer(MediaPlayer) 4179 * @see android.media.MediaPlayer.OnInfoListener 4180 */ 4181 public static final int MEDIA_INFO_STARTED_AS_NEXT = 2; 4182 4183 /** The player just pushed the very first video frame for rendering. 4184 * @see android.media.MediaPlayer.OnInfoListener 4185 */ 4186 public static final int MEDIA_INFO_VIDEO_RENDERING_START = 3; 4187 4188 /** The video is too complex for the decoder: it can't decode frames fast 4189 * enough. Possibly only the audio plays fine at this stage. 4190 * @see android.media.MediaPlayer.OnInfoListener 4191 */ 4192 public static final int MEDIA_INFO_VIDEO_TRACK_LAGGING = 700; 4193 4194 /** MediaPlayer is temporarily pausing playback internally in order to 4195 * buffer more data. 4196 * @see android.media.MediaPlayer.OnInfoListener 4197 */ 4198 public static final int MEDIA_INFO_BUFFERING_START = 701; 4199 4200 /** MediaPlayer is resuming playback after filling buffers. 4201 * @see android.media.MediaPlayer.OnInfoListener 4202 */ 4203 public static final int MEDIA_INFO_BUFFERING_END = 702; 4204 4205 /** Estimated network bandwidth information (kbps) is available; currently this event fires 4206 * simultaneously as {@link #MEDIA_INFO_BUFFERING_START} and {@link #MEDIA_INFO_BUFFERING_END} 4207 * when playing network files. 4208 * @see android.media.MediaPlayer.OnInfoListener 4209 * @hide 4210 */ 4211 public static final int MEDIA_INFO_NETWORK_BANDWIDTH = 703; 4212 4213 /** Bad interleaving means that a media has been improperly interleaved or 4214 * not interleaved at all, e.g has all the video samples first then all the 4215 * audio ones. Video is playing but a lot of disk seeks may be happening. 4216 * @see android.media.MediaPlayer.OnInfoListener 4217 */ 4218 public static final int MEDIA_INFO_BAD_INTERLEAVING = 800; 4219 4220 /** The media cannot be seeked (e.g live stream) 4221 * @see android.media.MediaPlayer.OnInfoListener 4222 */ 4223 public static final int MEDIA_INFO_NOT_SEEKABLE = 801; 4224 4225 /** A new set of metadata is available. 4226 * @see android.media.MediaPlayer.OnInfoListener 4227 */ 4228 public static final int MEDIA_INFO_METADATA_UPDATE = 802; 4229 4230 /** A new set of external-only metadata is available. Used by 4231 * JAVA framework to avoid triggering track scanning. 4232 * @hide 4233 */ 4234 public static final int MEDIA_INFO_EXTERNAL_METADATA_UPDATE = 803; 4235 4236 /** Informs that audio is not playing. Note that playback of the video 4237 * is not interrupted. 4238 * @see android.media.MediaPlayer.OnInfoListener 4239 */ 4240 public static final int MEDIA_INFO_AUDIO_NOT_PLAYING = 804; 4241 4242 /** Informs that video is not playing. Note that playback of the audio 4243 * is not interrupted. 4244 * @see android.media.MediaPlayer.OnInfoListener 4245 */ 4246 public static final int MEDIA_INFO_VIDEO_NOT_PLAYING = 805; 4247 4248 /** Failed to handle timed text track properly. 4249 * @see android.media.MediaPlayer.OnInfoListener 4250 * 4251 * {@hide} 4252 */ 4253 public static final int MEDIA_INFO_TIMED_TEXT_ERROR = 900; 4254 4255 /** Subtitle track was not supported by the media framework. 4256 * @see android.media.MediaPlayer.OnInfoListener 4257 */ 4258 public static final int MEDIA_INFO_UNSUPPORTED_SUBTITLE = 901; 4259 4260 /** Reading the subtitle track takes too long. 4261 * @see android.media.MediaPlayer.OnInfoListener 4262 */ 4263 public static final int MEDIA_INFO_SUBTITLE_TIMED_OUT = 902; 4264 4265 /** 4266 * Interface definition of a callback to be invoked to communicate some 4267 * info and/or warning about the media or its playback. 4268 */ 4269 public interface OnInfoListener 4270 { 4271 /** 4272 * Called to indicate an info or a warning. 4273 * 4274 * @param mp the MediaPlayer the info pertains to. 4275 * @param what the type of info or warning. 4276 * <ul> 4277 * <li>{@link #MEDIA_INFO_UNKNOWN} 4278 * <li>{@link #MEDIA_INFO_VIDEO_TRACK_LAGGING} 4279 * <li>{@link #MEDIA_INFO_VIDEO_RENDERING_START} 4280 * <li>{@link #MEDIA_INFO_BUFFERING_START} 4281 * <li>{@link #MEDIA_INFO_BUFFERING_END} 4282 * <li><code>MEDIA_INFO_NETWORK_BANDWIDTH (703)</code> - 4283 * bandwidth information is available (as <code>extra</code> kbps) 4284 * <li>{@link #MEDIA_INFO_BAD_INTERLEAVING} 4285 * <li>{@link #MEDIA_INFO_NOT_SEEKABLE} 4286 * <li>{@link #MEDIA_INFO_METADATA_UPDATE} 4287 * <li>{@link #MEDIA_INFO_UNSUPPORTED_SUBTITLE} 4288 * <li>{@link #MEDIA_INFO_SUBTITLE_TIMED_OUT} 4289 * </ul> 4290 * @param extra an extra code, specific to the info. Typically 4291 * implementation dependent. 4292 * @return True if the method handled the info, false if it didn't. 4293 * Returning false, or not having an OnInfoListener at all, will 4294 * cause the info to be discarded. 4295 */ onInfo(MediaPlayer mp, int what, int extra)4296 boolean onInfo(MediaPlayer mp, int what, int extra); 4297 } 4298 4299 /** 4300 * Register a callback to be invoked when an info/warning is available. 4301 * 4302 * @param listener the callback that will be run 4303 */ setOnInfoListener(OnInfoListener listener)4304 public void setOnInfoListener(OnInfoListener listener) 4305 { 4306 mOnInfoListener = listener; 4307 } 4308 4309 private OnInfoListener mOnInfoListener; 4310 4311 // Modular DRM begin 4312 4313 /** 4314 * Interface definition of a callback to be invoked when the app 4315 * can do DRM configuration (get/set properties) before the session 4316 * is opened. This facilitates configuration of the properties, like 4317 * 'securityLevel', which has to be set after DRM scheme creation but 4318 * before the DRM session is opened. 4319 * 4320 * The only allowed DRM calls in this listener are {@code getDrmPropertyString} 4321 * and {@code setDrmPropertyString}. 4322 * 4323 */ 4324 public interface OnDrmConfigHelper 4325 { 4326 /** 4327 * Called to give the app the opportunity to configure DRM before the session is created 4328 * 4329 * @param mp the {@code MediaPlayer} associated with this callback 4330 */ onDrmConfig(MediaPlayer mp)4331 public void onDrmConfig(MediaPlayer mp); 4332 } 4333 4334 /** 4335 * Register a callback to be invoked for configuration of the DRM object before 4336 * the session is created. 4337 * The callback will be invoked synchronously during the execution 4338 * of {@link #prepareDrm(UUID uuid)}. 4339 * 4340 * @param listener the callback that will be run 4341 */ setOnDrmConfigHelper(OnDrmConfigHelper listener)4342 public void setOnDrmConfigHelper(OnDrmConfigHelper listener) 4343 { 4344 synchronized (mDrmLock) { 4345 mOnDrmConfigHelper = listener; 4346 } // synchronized 4347 } 4348 4349 private OnDrmConfigHelper mOnDrmConfigHelper; 4350 4351 /** 4352 * Interface definition of a callback to be invoked when the 4353 * DRM info becomes available 4354 */ 4355 public interface OnDrmInfoListener 4356 { 4357 /** 4358 * Called to indicate DRM info is available 4359 * 4360 * @param mp the {@code MediaPlayer} associated with this callback 4361 * @param drmInfo DRM info of the source including PSSH, and subset 4362 * of crypto schemes supported by this device 4363 */ onDrmInfo(MediaPlayer mp, DrmInfo drmInfo)4364 public void onDrmInfo(MediaPlayer mp, DrmInfo drmInfo); 4365 } 4366 4367 /** 4368 * Register a callback to be invoked when the DRM info is 4369 * known. 4370 * 4371 * @param listener the callback that will be run 4372 */ setOnDrmInfoListener(OnDrmInfoListener listener)4373 public void setOnDrmInfoListener(OnDrmInfoListener listener) 4374 { 4375 setOnDrmInfoListener(listener, null); 4376 } 4377 4378 /** 4379 * Register a callback to be invoked when the DRM info is 4380 * known. 4381 * 4382 * @param listener the callback that will be run 4383 */ setOnDrmInfoListener(OnDrmInfoListener listener, Handler handler)4384 public void setOnDrmInfoListener(OnDrmInfoListener listener, Handler handler) 4385 { 4386 synchronized (mDrmLock) { 4387 if (listener != null) { 4388 mOnDrmInfoHandlerDelegate = new OnDrmInfoHandlerDelegate(this, listener, handler); 4389 } else { 4390 mOnDrmInfoHandlerDelegate = null; 4391 } 4392 } // synchronized 4393 } 4394 4395 private OnDrmInfoHandlerDelegate mOnDrmInfoHandlerDelegate; 4396 4397 4398 /** 4399 * The status codes for {@link OnDrmPreparedListener#onDrmPrepared} listener. 4400 * <p> 4401 * 4402 * DRM preparation has succeeded. 4403 */ 4404 public static final int PREPARE_DRM_STATUS_SUCCESS = 0; 4405 4406 /** 4407 * The device required DRM provisioning but couldn't reach the provisioning server. 4408 */ 4409 public static final int PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR = 1; 4410 4411 /** 4412 * The device required DRM provisioning but the provisioning server denied the request. 4413 */ 4414 public static final int PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR = 2; 4415 4416 /** 4417 * The DRM preparation has failed . 4418 */ 4419 public static final int PREPARE_DRM_STATUS_PREPARATION_ERROR = 3; 4420 4421 4422 /** @hide */ 4423 @IntDef({ 4424 PREPARE_DRM_STATUS_SUCCESS, 4425 PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR, 4426 PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR, 4427 PREPARE_DRM_STATUS_PREPARATION_ERROR, 4428 }) 4429 @Retention(RetentionPolicy.SOURCE) 4430 public @interface PrepareDrmStatusCode {} 4431 4432 /** 4433 * Interface definition of a callback to notify the app when the 4434 * DRM is ready for key request/response 4435 */ 4436 public interface OnDrmPreparedListener 4437 { 4438 /** 4439 * Called to notify the app that prepareDrm is finished and ready for key request/response 4440 * 4441 * @param mp the {@code MediaPlayer} associated with this callback 4442 * @param status the result of DRM preparation which can be 4443 * {@link #PREPARE_DRM_STATUS_SUCCESS}, 4444 * {@link #PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR}, 4445 * {@link #PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR}, or 4446 * {@link #PREPARE_DRM_STATUS_PREPARATION_ERROR}. 4447 */ onDrmPrepared(MediaPlayer mp, @PrepareDrmStatusCode int status)4448 public void onDrmPrepared(MediaPlayer mp, @PrepareDrmStatusCode int status); 4449 } 4450 4451 /** 4452 * Register a callback to be invoked when the DRM object is prepared. 4453 * 4454 * @param listener the callback that will be run 4455 */ setOnDrmPreparedListener(OnDrmPreparedListener listener)4456 public void setOnDrmPreparedListener(OnDrmPreparedListener listener) 4457 { 4458 setOnDrmPreparedListener(listener, null); 4459 } 4460 4461 /** 4462 * Register a callback to be invoked when the DRM object is prepared. 4463 * 4464 * @param listener the callback that will be run 4465 * @param handler the Handler that will receive the callback 4466 */ setOnDrmPreparedListener(OnDrmPreparedListener listener, Handler handler)4467 public void setOnDrmPreparedListener(OnDrmPreparedListener listener, Handler handler) 4468 { 4469 synchronized (mDrmLock) { 4470 if (listener != null) { 4471 mOnDrmPreparedHandlerDelegate = new OnDrmPreparedHandlerDelegate(this, 4472 listener, handler); 4473 } else { 4474 mOnDrmPreparedHandlerDelegate = null; 4475 } 4476 } // synchronized 4477 } 4478 4479 private OnDrmPreparedHandlerDelegate mOnDrmPreparedHandlerDelegate; 4480 4481 4482 private class OnDrmInfoHandlerDelegate { 4483 private MediaPlayer mMediaPlayer; 4484 private OnDrmInfoListener mOnDrmInfoListener; 4485 private Handler mHandler; 4486 OnDrmInfoHandlerDelegate(MediaPlayer mp, OnDrmInfoListener listener, Handler handler)4487 OnDrmInfoHandlerDelegate(MediaPlayer mp, OnDrmInfoListener listener, Handler handler) { 4488 mMediaPlayer = mp; 4489 mOnDrmInfoListener = listener; 4490 4491 // find the looper for our new event handler 4492 if (handler != null) { 4493 mHandler = handler; 4494 } else { 4495 // handler == null 4496 // Will let OnDrmInfoListener be called in mEventHandler similar to other 4497 // legacy notifications. This is because MEDIA_DRM_INFO's notification has to be 4498 // sent before MEDIA_PREPARED's (i.e., in the same order they are issued by 4499 // mediaserver). As a result, the callback has to be called directly by 4500 // EventHandler.handleMessage similar to onPrepared. 4501 } 4502 } 4503 notifyClient(DrmInfo drmInfo)4504 void notifyClient(DrmInfo drmInfo) { 4505 if (mHandler != null) { 4506 mHandler.post(new Runnable() { 4507 @Override 4508 public void run() { 4509 mOnDrmInfoListener.onDrmInfo(mMediaPlayer, drmInfo); 4510 } 4511 }); 4512 } 4513 else { // no handler: direct call by mEventHandler 4514 mOnDrmInfoListener.onDrmInfo(mMediaPlayer, drmInfo); 4515 } 4516 } 4517 } 4518 4519 private class OnDrmPreparedHandlerDelegate { 4520 private MediaPlayer mMediaPlayer; 4521 private OnDrmPreparedListener mOnDrmPreparedListener; 4522 private Handler mHandler; 4523 OnDrmPreparedHandlerDelegate(MediaPlayer mp, OnDrmPreparedListener listener, Handler handler)4524 OnDrmPreparedHandlerDelegate(MediaPlayer mp, OnDrmPreparedListener listener, 4525 Handler handler) { 4526 mMediaPlayer = mp; 4527 mOnDrmPreparedListener = listener; 4528 4529 // find the looper for our new event handler 4530 if (handler != null) { 4531 mHandler = handler; 4532 } else if (mEventHandler != null) { 4533 // Otherwise, use mEventHandler 4534 mHandler = mEventHandler; 4535 } else { 4536 Log.e(TAG, "OnDrmPreparedHandlerDelegate: Unexpected null mEventHandler"); 4537 } 4538 } 4539 notifyClient(int status)4540 void notifyClient(int status) { 4541 if (mHandler != null) { 4542 mHandler.post(new Runnable() { 4543 @Override 4544 public void run() { 4545 mOnDrmPreparedListener.onDrmPrepared(mMediaPlayer, status); 4546 } 4547 }); 4548 } else { 4549 Log.e(TAG, "OnDrmPreparedHandlerDelegate:notifyClient: Unexpected null mHandler"); 4550 } 4551 } 4552 } 4553 4554 /** 4555 * Retrieves the DRM Info associated with the current source 4556 * 4557 * @throws IllegalStateException if called before prepare() 4558 */ getDrmInfo()4559 public DrmInfo getDrmInfo() 4560 { 4561 DrmInfo drmInfo = null; 4562 4563 // there is not much point if the app calls getDrmInfo within an OnDrmInfoListenet; 4564 // regardless below returns drmInfo anyway instead of raising an exception 4565 synchronized (mDrmLock) { 4566 if (!mDrmInfoResolved && mDrmInfo == null) { 4567 final String msg = "The Player has not been prepared yet"; 4568 Log.v(TAG, msg); 4569 throw new IllegalStateException(msg); 4570 } 4571 4572 if (mDrmInfo != null) { 4573 drmInfo = mDrmInfo.makeCopy(); 4574 } 4575 } // synchronized 4576 4577 return drmInfo; 4578 } 4579 4580 4581 /** 4582 * Prepares the DRM for the current source 4583 * <p> 4584 * If {@code OnDrmConfigHelper} is registered, it will be called during 4585 * preparation to allow configuration of the DRM properties before opening the 4586 * DRM session. Note that the callback is called synchronously in the thread that called 4587 * {@code prepareDrm}. It should be used only for a series of {@code getDrmPropertyString} 4588 * and {@code setDrmPropertyString} calls and refrain from any lengthy operation. 4589 * <p> 4590 * If the device has not been provisioned before, this call also provisions the device 4591 * which involves accessing the provisioning server and can take a variable time to 4592 * complete depending on the network connectivity. 4593 * If {@code OnDrmPreparedListener} is registered, prepareDrm() runs in non-blocking 4594 * mode by launching the provisioning in the background and returning. The listener 4595 * will be called when provisioning and preparation has finished. If a 4596 * {@code OnDrmPreparedListener} is not registered, prepareDrm() waits till provisioning 4597 * and preparation has finished, i.e., runs in blocking mode. 4598 * <p> 4599 * If {@code OnDrmPreparedListener} is registered, it is called to indicate the DRM 4600 * session being ready. The application should not make any assumption about its call 4601 * sequence (e.g., before or after prepareDrm returns), or the thread context that will 4602 * execute the listener (unless the listener is registered with a handler thread). 4603 * <p> 4604 * 4605 * @param uuid The UUID of the crypto scheme. If not known beforehand, it can be retrieved 4606 * from the source through {@code getDrmInfo} or registering a {@code onDrmInfoListener}. 4607 * 4608 * @throws IllegalStateException if called before prepare(), or the DRM was 4609 * prepared already 4610 * @throws UnsupportedSchemeException if the crypto scheme is not supported 4611 * @throws ResourceBusyException if required DRM resources are in use 4612 * @throws ProvisioningNetworkErrorException if provisioning is required but failed due to a 4613 * network error 4614 * @throws ProvisioningServerErrorException if provisioning is required but failed due to 4615 * the request denied by the provisioning server 4616 */ prepareDrm(@onNull UUID uuid)4617 public void prepareDrm(@NonNull UUID uuid) 4618 throws UnsupportedSchemeException, ResourceBusyException, 4619 ProvisioningNetworkErrorException, ProvisioningServerErrorException 4620 { 4621 Log.v(TAG, "prepareDrm: uuid: " + uuid + " mOnDrmConfigHelper: " + mOnDrmConfigHelper); 4622 4623 boolean allDoneWithoutProvisioning = false; 4624 // get a snapshot as we'll use them outside the lock 4625 OnDrmPreparedHandlerDelegate onDrmPreparedHandlerDelegate = null; 4626 4627 synchronized (mDrmLock) { 4628 4629 // only allowing if tied to a protected source; might relax for releasing offline keys 4630 if (mDrmInfo == null) { 4631 final String msg = "prepareDrm(): Wrong usage: The player must be prepared and " + 4632 "DRM info be retrieved before this call."; 4633 Log.e(TAG, msg); 4634 throw new IllegalStateException(msg); 4635 } 4636 4637 if (mActiveDrmScheme) { 4638 final String msg = "prepareDrm(): Wrong usage: There is already " + 4639 "an active DRM scheme with " + mDrmUUID; 4640 Log.e(TAG, msg); 4641 throw new IllegalStateException(msg); 4642 } 4643 4644 if (mPrepareDrmInProgress) { 4645 final String msg = "prepareDrm(): Wrong usage: There is already " + 4646 "a pending prepareDrm call."; 4647 Log.e(TAG, msg); 4648 throw new IllegalStateException(msg); 4649 } 4650 4651 if (mDrmProvisioningInProgress) { 4652 final String msg = "prepareDrm(): Unexpectd: Provisioning is already in progress."; 4653 Log.e(TAG, msg); 4654 throw new IllegalStateException(msg); 4655 } 4656 4657 // shouldn't need this; just for safeguard 4658 cleanDrmObj(); 4659 4660 mPrepareDrmInProgress = true; 4661 // local copy while the lock is held 4662 onDrmPreparedHandlerDelegate = mOnDrmPreparedHandlerDelegate; 4663 4664 try { 4665 // only creating the DRM object to allow pre-openSession configuration 4666 prepareDrm_createDrmStep(uuid); 4667 } catch (Exception e) { 4668 Log.w(TAG, "prepareDrm(): Exception ", e); 4669 mPrepareDrmInProgress = false; 4670 throw e; 4671 } 4672 4673 mDrmConfigAllowed = true; 4674 } // synchronized 4675 4676 4677 // call the callback outside the lock 4678 if (mOnDrmConfigHelper != null) { 4679 mOnDrmConfigHelper.onDrmConfig(this); 4680 } 4681 4682 synchronized (mDrmLock) { 4683 mDrmConfigAllowed = false; 4684 boolean earlyExit = false; 4685 4686 try { 4687 prepareDrm_openSessionStep(uuid); 4688 4689 mDrmUUID = uuid; 4690 mActiveDrmScheme = true; 4691 4692 allDoneWithoutProvisioning = true; 4693 } catch (IllegalStateException e) { 4694 final String msg = "prepareDrm(): Wrong usage: The player must be " + 4695 "in the prepared state to call prepareDrm()."; 4696 Log.e(TAG, msg); 4697 earlyExit = true; 4698 throw new IllegalStateException(msg); 4699 } catch (NotProvisionedException e) { 4700 Log.w(TAG, "prepareDrm: NotProvisionedException"); 4701 4702 // handle provisioning internally; it'll reset mPrepareDrmInProgress 4703 int result = HandleProvisioninig(uuid); 4704 4705 // if blocking mode, we're already done; 4706 // if non-blocking mode, we attempted to launch background provisioning 4707 if (result != PREPARE_DRM_STATUS_SUCCESS) { 4708 earlyExit = true; 4709 String msg; 4710 4711 switch (result) { 4712 case PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR: 4713 msg = "prepareDrm: Provisioning was required but failed " + 4714 "due to a network error."; 4715 Log.e(TAG, msg); 4716 throw new ProvisioningNetworkErrorException(msg); 4717 4718 case PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR: 4719 msg = "prepareDrm: Provisioning was required but the request " + 4720 "was denied by the server."; 4721 Log.e(TAG, msg); 4722 throw new ProvisioningServerErrorException(msg); 4723 4724 case PREPARE_DRM_STATUS_PREPARATION_ERROR: 4725 default: // default for safeguard 4726 msg = "prepareDrm: Post-provisioning preparation failed."; 4727 Log.e(TAG, msg); 4728 throw new IllegalStateException(msg); 4729 } 4730 } 4731 // nothing else to do; 4732 // if blocking or non-blocking, HandleProvisioninig does the re-attempt & cleanup 4733 } catch (Exception e) { 4734 Log.e(TAG, "prepareDrm: Exception " + e); 4735 earlyExit = true; 4736 throw e; 4737 } finally { 4738 if (!mDrmProvisioningInProgress) {// if early exit other than provisioning exception 4739 mPrepareDrmInProgress = false; 4740 } 4741 if (earlyExit) { // cleaning up object if didn't succeed 4742 cleanDrmObj(); 4743 } 4744 } // finally 4745 } // synchronized 4746 4747 4748 // if finished successfully without provisioning, call the callback outside the lock 4749 if (allDoneWithoutProvisioning) { 4750 if (onDrmPreparedHandlerDelegate != null) 4751 onDrmPreparedHandlerDelegate.notifyClient(PREPARE_DRM_STATUS_SUCCESS); 4752 } 4753 4754 } 4755 4756 _releaseDrm()4757 private native void _releaseDrm(); 4758 4759 /** 4760 * Releases the DRM session 4761 * <p> 4762 * The player has to have an active DRM session and be in stopped, or prepared 4763 * state before this call is made. 4764 * A {@code reset()} call will release the DRM session implicitly. 4765 * 4766 * @throws NoDrmSchemeException if there is no active DRM session to release 4767 */ releaseDrm()4768 public void releaseDrm() 4769 throws NoDrmSchemeException 4770 { 4771 Log.v(TAG, "releaseDrm:"); 4772 4773 synchronized (mDrmLock) { 4774 if (!mActiveDrmScheme) { 4775 Log.e(TAG, "releaseDrm(): No active DRM scheme to release."); 4776 throw new NoDrmSchemeException("releaseDrm: No active DRM scheme to release."); 4777 } 4778 4779 try { 4780 // we don't have the player's state in this layer. The below call raises 4781 // exception if we're in a non-stopped/prepared state. 4782 4783 // for cleaning native/mediaserver crypto object 4784 _releaseDrm(); 4785 4786 // for cleaning client-side MediaDrm object; only called if above has succeeded 4787 cleanDrmObj(); 4788 4789 mActiveDrmScheme = false; 4790 } catch (IllegalStateException e) { 4791 Log.w(TAG, "releaseDrm: Exception ", e); 4792 throw new IllegalStateException("releaseDrm: The player is not in a valid state."); 4793 } catch (Exception e) { 4794 Log.e(TAG, "releaseDrm: Exception ", e); 4795 } 4796 } // synchronized 4797 } 4798 4799 4800 /** 4801 * A key request/response exchange occurs between the app and a license server 4802 * to obtain or release keys used to decrypt encrypted content. 4803 * <p> 4804 * getKeyRequest() is used to obtain an opaque key request byte array that is 4805 * delivered to the license server. The opaque key request byte array is returned 4806 * in KeyRequest.data. The recommended URL to deliver the key request to is 4807 * returned in KeyRequest.defaultUrl. 4808 * <p> 4809 * After the app has received the key request response from the server, 4810 * it should deliver to the response to the DRM engine plugin using the method 4811 * {@link #provideKeyResponse}. 4812 * 4813 * @param keySetId is the key-set identifier of the offline keys being released when keyType is 4814 * {@link MediaDrm#KEY_TYPE_RELEASE}. It should be set to null for other key requests, when 4815 * keyType is {@link MediaDrm#KEY_TYPE_STREAMING} or {@link MediaDrm#KEY_TYPE_OFFLINE}. 4816 * 4817 * @param initData is the container-specific initialization data when the keyType is 4818 * {@link MediaDrm#KEY_TYPE_STREAMING} or {@link MediaDrm#KEY_TYPE_OFFLINE}. Its meaning is 4819 * interpreted based on the mime type provided in the mimeType parameter. It could 4820 * contain, for example, the content ID, key ID or other data obtained from the content 4821 * metadata that is required in generating the key request. 4822 * When the keyType is {@link MediaDrm#KEY_TYPE_RELEASE}, it should be set to null. 4823 * 4824 * @param mimeType identifies the mime type of the content 4825 * 4826 * @param keyType specifies the type of the request. The request may be to acquire 4827 * keys for streaming, {@link MediaDrm#KEY_TYPE_STREAMING}, or for offline content 4828 * {@link MediaDrm#KEY_TYPE_OFFLINE}, or to release previously acquired 4829 * keys ({@link MediaDrm#KEY_TYPE_RELEASE}), which are identified by a keySetId. 4830 * 4831 * @param optionalParameters are included in the key request message to 4832 * allow a client application to provide additional message parameters to the server. 4833 * This may be {@code null} if no additional parameters are to be sent. 4834 * 4835 * @throws NoDrmSchemeException if there is no active DRM session 4836 */ 4837 @NonNull getKeyRequest(@ullable byte[] keySetId, @Nullable byte[] initData, @Nullable String mimeType, @MediaDrm.KeyType int keyType, @Nullable Map<String, String> optionalParameters)4838 public MediaDrm.KeyRequest getKeyRequest(@Nullable byte[] keySetId, @Nullable byte[] initData, 4839 @Nullable String mimeType, @MediaDrm.KeyType int keyType, 4840 @Nullable Map<String, String> optionalParameters) 4841 throws NoDrmSchemeException 4842 { 4843 Log.v(TAG, "getKeyRequest: " + 4844 " keySetId: " + keySetId + " initData:" + initData + " mimeType: " + mimeType + 4845 " keyType: " + keyType + " optionalParameters: " + optionalParameters); 4846 4847 synchronized (mDrmLock) { 4848 if (!mActiveDrmScheme) { 4849 Log.e(TAG, "getKeyRequest NoDrmSchemeException"); 4850 throw new NoDrmSchemeException("getKeyRequest: Has to set a DRM scheme first."); 4851 } 4852 4853 try { 4854 byte[] scope = (keyType != MediaDrm.KEY_TYPE_RELEASE) ? 4855 mDrmSessionId : // sessionId for KEY_TYPE_STREAMING/OFFLINE 4856 keySetId; // keySetId for KEY_TYPE_RELEASE 4857 4858 HashMap<String, String> hmapOptionalParameters = 4859 (optionalParameters != null) ? 4860 new HashMap<String, String>(optionalParameters) : 4861 null; 4862 4863 MediaDrm.KeyRequest request = mDrmObj.getKeyRequest(scope, initData, mimeType, 4864 keyType, hmapOptionalParameters); 4865 Log.v(TAG, "getKeyRequest: --> request: " + request); 4866 4867 return request; 4868 4869 } catch (NotProvisionedException e) { 4870 Log.w(TAG, "getKeyRequest NotProvisionedException: " + 4871 "Unexpected. Shouldn't have reached here."); 4872 throw new IllegalStateException("getKeyRequest: Unexpected provisioning error."); 4873 } catch (Exception e) { 4874 Log.w(TAG, "getKeyRequest Exception " + e); 4875 throw e; 4876 } 4877 4878 } // synchronized 4879 } 4880 4881 4882 /** 4883 * A key response is received from the license server by the app, then it is 4884 * provided to the DRM engine plugin using provideKeyResponse. When the 4885 * response is for an offline key request, a key-set identifier is returned that 4886 * can be used to later restore the keys to a new session with the method 4887 * {@ link # restoreKeys}. 4888 * When the response is for a streaming or release request, null is returned. 4889 * 4890 * @param keySetId When the response is for a release request, keySetId identifies 4891 * the saved key associated with the release request (i.e., the same keySetId 4892 * passed to the earlier {@ link # getKeyRequest} call. It MUST be null when the 4893 * response is for either streaming or offline key requests. 4894 * 4895 * @param response the byte array response from the server 4896 * 4897 * @throws NoDrmSchemeException if there is no active DRM session 4898 * @throws DeniedByServerException if the response indicates that the 4899 * server rejected the request 4900 */ provideKeyResponse(@ullable byte[] keySetId, @NonNull byte[] response)4901 public byte[] provideKeyResponse(@Nullable byte[] keySetId, @NonNull byte[] response) 4902 throws NoDrmSchemeException, DeniedByServerException 4903 { 4904 Log.v(TAG, "provideKeyResponse: keySetId: " + keySetId + " response: " + response); 4905 4906 synchronized (mDrmLock) { 4907 4908 if (!mActiveDrmScheme) { 4909 Log.e(TAG, "getKeyRequest NoDrmSchemeException"); 4910 throw new NoDrmSchemeException("getKeyRequest: Has to set a DRM scheme first."); 4911 } 4912 4913 try { 4914 byte[] scope = (keySetId == null) ? 4915 mDrmSessionId : // sessionId for KEY_TYPE_STREAMING/OFFLINE 4916 keySetId; // keySetId for KEY_TYPE_RELEASE 4917 4918 byte[] keySetResult = mDrmObj.provideKeyResponse(scope, response); 4919 4920 Log.v(TAG, "provideKeyResponse: keySetId: " + keySetId + " response: " + response + 4921 " --> " + keySetResult); 4922 4923 4924 return keySetResult; 4925 4926 } catch (NotProvisionedException e) { 4927 Log.w(TAG, "provideKeyResponse NotProvisionedException: " + 4928 "Unexpected. Shouldn't have reached here."); 4929 throw new IllegalStateException("provideKeyResponse: " + 4930 "Unexpected provisioning error."); 4931 } catch (Exception e) { 4932 Log.w(TAG, "provideKeyResponse Exception " + e); 4933 throw e; 4934 } 4935 } // synchronized 4936 } 4937 4938 4939 /** 4940 * Restore persisted offline keys into a new session. keySetId identifies the 4941 * keys to load, obtained from a prior call to {@link #provideKeyResponse}. 4942 * 4943 * @param keySetId identifies the saved key set to restore 4944 */ restoreKeys(@onNull byte[] keySetId)4945 public void restoreKeys(@NonNull byte[] keySetId) 4946 throws NoDrmSchemeException 4947 { 4948 Log.v(TAG, "restoreKeys: keySetId: " + keySetId); 4949 4950 synchronized (mDrmLock) { 4951 4952 if (!mActiveDrmScheme) { 4953 Log.w(TAG, "restoreKeys NoDrmSchemeException"); 4954 throw new NoDrmSchemeException("restoreKeys: Has to set a DRM scheme first."); 4955 } 4956 4957 try { 4958 mDrmObj.restoreKeys(mDrmSessionId, keySetId); 4959 } catch (Exception e) { 4960 Log.w(TAG, "restoreKeys Exception " + e); 4961 throw e; 4962 } 4963 4964 } // synchronized 4965 } 4966 4967 4968 /** 4969 * Read a DRM engine plugin String property value, given the property name string. 4970 * <p> 4971 * @param propertyName the property name 4972 * 4973 * Standard fields names are: 4974 * {@link MediaDrm#PROPERTY_VENDOR}, {@link MediaDrm#PROPERTY_VERSION}, 4975 * {@link MediaDrm#PROPERTY_DESCRIPTION}, {@link MediaDrm#PROPERTY_ALGORITHMS} 4976 */ 4977 @NonNull getDrmPropertyString(@onNull @ediaDrm.StringProperty String propertyName)4978 public String getDrmPropertyString(@NonNull @MediaDrm.StringProperty String propertyName) 4979 throws NoDrmSchemeException 4980 { 4981 Log.v(TAG, "getDrmPropertyString: propertyName: " + propertyName); 4982 4983 String value; 4984 synchronized (mDrmLock) { 4985 4986 if (!mActiveDrmScheme && !mDrmConfigAllowed) { 4987 Log.w(TAG, "getDrmPropertyString NoDrmSchemeException"); 4988 throw new NoDrmSchemeException("getDrmPropertyString: Has to prepareDrm() first."); 4989 } 4990 4991 try { 4992 value = mDrmObj.getPropertyString(propertyName); 4993 } catch (Exception e) { 4994 Log.w(TAG, "getDrmPropertyString Exception " + e); 4995 throw e; 4996 } 4997 } // synchronized 4998 4999 Log.v(TAG, "getDrmPropertyString: propertyName: " + propertyName + " --> value: " + value); 5000 5001 return value; 5002 } 5003 5004 5005 /** 5006 * Set a DRM engine plugin String property value. 5007 * <p> 5008 * @param propertyName the property name 5009 * @param value the property value 5010 * 5011 * Standard fields names are: 5012 * {@link MediaDrm#PROPERTY_VENDOR}, {@link MediaDrm#PROPERTY_VERSION}, 5013 * {@link MediaDrm#PROPERTY_DESCRIPTION}, {@link MediaDrm#PROPERTY_ALGORITHMS} 5014 */ setDrmPropertyString(@onNull @ediaDrm.StringProperty String propertyName, @NonNull String value)5015 public void setDrmPropertyString(@NonNull @MediaDrm.StringProperty String propertyName, 5016 @NonNull String value) 5017 throws NoDrmSchemeException 5018 { 5019 Log.v(TAG, "setDrmPropertyString: propertyName: " + propertyName + " value: " + value); 5020 5021 synchronized (mDrmLock) { 5022 5023 if ( !mActiveDrmScheme && !mDrmConfigAllowed ) { 5024 Log.w(TAG, "setDrmPropertyString NoDrmSchemeException"); 5025 throw new NoDrmSchemeException("setDrmPropertyString: Has to prepareDrm() first."); 5026 } 5027 5028 try { 5029 mDrmObj.setPropertyString(propertyName, value); 5030 } catch ( Exception e ) { 5031 Log.w(TAG, "setDrmPropertyString Exception " + e); 5032 throw e; 5033 } 5034 } // synchronized 5035 } 5036 5037 /** 5038 * Encapsulates the DRM properties of the source. 5039 */ 5040 public static final class DrmInfo { 5041 private Map<UUID, byte[]> mapPssh; 5042 private UUID[] supportedSchemes; 5043 5044 /** 5045 * Returns the PSSH info of the data source for each supported DRM scheme. 5046 */ getPssh()5047 public Map<UUID, byte[]> getPssh() { 5048 return mapPssh; 5049 } 5050 5051 /** 5052 * Returns the intersection of the data source and the device DRM schemes. 5053 * It effectively identifies the subset of the source's DRM schemes which 5054 * are supported by the device too. 5055 */ getSupportedSchemes()5056 public UUID[] getSupportedSchemes() { 5057 return supportedSchemes; 5058 } 5059 DrmInfo(Map<UUID, byte[]> Pssh, UUID[] SupportedSchemes)5060 private DrmInfo(Map<UUID, byte[]> Pssh, UUID[] SupportedSchemes) { 5061 mapPssh = Pssh; 5062 supportedSchemes = SupportedSchemes; 5063 } 5064 DrmInfo(Parcel parcel)5065 private DrmInfo(Parcel parcel) { 5066 Log.v(TAG, "DrmInfo(" + parcel + ") size " + parcel.dataSize()); 5067 5068 int psshsize = parcel.readInt(); 5069 byte[] pssh = new byte[psshsize]; 5070 parcel.readByteArray(pssh); 5071 5072 Log.v(TAG, "DrmInfo() PSSH: " + arrToHex(pssh)); 5073 mapPssh = parsePSSH(pssh, psshsize); 5074 Log.v(TAG, "DrmInfo() PSSH: " + mapPssh); 5075 5076 int supportedDRMsCount = parcel.readInt(); 5077 supportedSchemes = new UUID[supportedDRMsCount]; 5078 for (int i = 0; i < supportedDRMsCount; i++) { 5079 byte[] uuid = new byte[16]; 5080 parcel.readByteArray(uuid); 5081 5082 supportedSchemes[i] = bytesToUUID(uuid); 5083 5084 Log.v(TAG, "DrmInfo() supportedScheme[" + i + "]: " + 5085 supportedSchemes[i]); 5086 } 5087 5088 Log.v(TAG, "DrmInfo() Parcel psshsize: " + psshsize + 5089 " supportedDRMsCount: " + supportedDRMsCount); 5090 } 5091 makeCopy()5092 private DrmInfo makeCopy() { 5093 return new DrmInfo(this.mapPssh, this.supportedSchemes); 5094 } 5095 arrToHex(byte[] bytes)5096 private String arrToHex(byte[] bytes) { 5097 String out = "0x"; 5098 for (int i = 0; i < bytes.length; i++) { 5099 out += String.format("%02x", bytes[i]); 5100 } 5101 5102 return out; 5103 } 5104 bytesToUUID(byte[] uuid)5105 private UUID bytesToUUID(byte[] uuid) { 5106 long msb = 0, lsb = 0; 5107 for (int i = 0; i < 8; i++) { 5108 msb |= ( ((long)uuid[i] & 0xff) << (8 * (7 - i)) ); 5109 lsb |= ( ((long)uuid[i+8] & 0xff) << (8 * (7 - i)) ); 5110 } 5111 5112 return new UUID(msb, lsb); 5113 } 5114 parsePSSH(byte[] pssh, int psshsize)5115 private Map<UUID, byte[]> parsePSSH(byte[] pssh, int psshsize) { 5116 Map<UUID, byte[]> result = new HashMap<UUID, byte[]>(); 5117 5118 final int UUID_SIZE = 16; 5119 final int DATALEN_SIZE = 4; 5120 5121 int len = psshsize; 5122 int numentries = 0; 5123 int i = 0; 5124 5125 while (len > 0) { 5126 if (len < UUID_SIZE) { 5127 Log.w(TAG, String.format("parsePSSH: len is too short to parse " + 5128 "UUID: (%d < 16) pssh: %d", len, psshsize)); 5129 return null; 5130 } 5131 5132 byte[] subset = Arrays.copyOfRange(pssh, i, i + UUID_SIZE); 5133 UUID uuid = bytesToUUID(subset); 5134 i += UUID_SIZE; 5135 len -= UUID_SIZE; 5136 5137 // get data length 5138 if (len < 4) { 5139 Log.w(TAG, String.format("parsePSSH: len is too short to parse " + 5140 "datalen: (%d < 4) pssh: %d", len, psshsize)); 5141 return null; 5142 } 5143 5144 subset = Arrays.copyOfRange(pssh, i, i+DATALEN_SIZE); 5145 int datalen = (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) ? 5146 ((subset[3] & 0xff) << 24) | ((subset[2] & 0xff) << 16) | 5147 ((subset[1] & 0xff) << 8) | (subset[0] & 0xff) : 5148 ((subset[0] & 0xff) << 24) | ((subset[1] & 0xff) << 16) | 5149 ((subset[2] & 0xff) << 8) | (subset[3] & 0xff) ; 5150 i += DATALEN_SIZE; 5151 len -= DATALEN_SIZE; 5152 5153 if (len < datalen) { 5154 Log.w(TAG, String.format("parsePSSH: len is too short to parse " + 5155 "data: (%d < %d) pssh: %d", len, datalen, psshsize)); 5156 return null; 5157 } 5158 5159 byte[] data = Arrays.copyOfRange(pssh, i, i+datalen); 5160 5161 // skip the data 5162 i += datalen; 5163 len -= datalen; 5164 5165 Log.v(TAG, String.format("parsePSSH[%d]: <%s, %s> pssh: %d", 5166 numentries, uuid, arrToHex(data), psshsize)); 5167 numentries++; 5168 result.put(uuid, data); 5169 } 5170 5171 return result; 5172 } 5173 5174 }; // DrmInfo 5175 5176 /** 5177 * Thrown when a DRM method is called before preparing a DRM scheme through prepareDrm(). 5178 * Extends MediaDrm.MediaDrmException 5179 */ 5180 public static final class NoDrmSchemeException extends MediaDrmException { NoDrmSchemeException(String detailMessage)5181 public NoDrmSchemeException(String detailMessage) { 5182 super(detailMessage); 5183 } 5184 } 5185 5186 /** 5187 * Thrown when the device requires DRM provisioning but the provisioning attempt has 5188 * failed due to a network error (Internet reachability, timeout, etc.). 5189 * Extends MediaDrm.MediaDrmException 5190 */ 5191 public static final class ProvisioningNetworkErrorException extends MediaDrmException { ProvisioningNetworkErrorException(String detailMessage)5192 public ProvisioningNetworkErrorException(String detailMessage) { 5193 super(detailMessage); 5194 } 5195 } 5196 5197 /** 5198 * Thrown when the device requires DRM provisioning but the provisioning attempt has 5199 * failed due to the provisioning server denying the request. 5200 * Extends MediaDrm.MediaDrmException 5201 */ 5202 public static final class ProvisioningServerErrorException extends MediaDrmException { ProvisioningServerErrorException(String detailMessage)5203 public ProvisioningServerErrorException(String detailMessage) { 5204 super(detailMessage); 5205 } 5206 } 5207 5208 _prepareDrm(@onNull byte[] uuid, @NonNull byte[] drmSessionId)5209 private native void _prepareDrm(@NonNull byte[] uuid, @NonNull byte[] drmSessionId); 5210 5211 // Modular DRM helpers 5212 prepareDrm_createDrmStep(@onNull UUID uuid)5213 private void prepareDrm_createDrmStep(@NonNull UUID uuid) 5214 throws UnsupportedSchemeException { 5215 Log.v(TAG, "prepareDrm_createDrmStep: UUID: " + uuid); 5216 5217 try { 5218 mDrmObj = new MediaDrm(uuid); 5219 Log.v(TAG, "prepareDrm_createDrmStep: Created mDrmObj=" + mDrmObj); 5220 } catch (Exception e) { // UnsupportedSchemeException 5221 Log.e(TAG, "prepareDrm_createDrmStep: MediaDrm failed with " + e); 5222 throw e; 5223 } 5224 } 5225 prepareDrm_openSessionStep(@onNull UUID uuid)5226 private void prepareDrm_openSessionStep(@NonNull UUID uuid) 5227 throws NotProvisionedException, ResourceBusyException { 5228 Log.v(TAG, "prepareDrm_openSessionStep: uuid: " + uuid); 5229 5230 // TODO: don't need an open session for a future specialKeyReleaseDrm mode but we should do 5231 // it anyway so it raises provisioning error if needed. We'd rather handle provisioning 5232 // at prepareDrm/openSession rather than getKeyRequest/provideKeyResponse 5233 try { 5234 mDrmSessionId = mDrmObj.openSession(); 5235 Log.v(TAG, "prepareDrm_openSessionStep: mDrmSessionId=" + mDrmSessionId); 5236 5237 // Sending it down to native/mediaserver to create the crypto object 5238 // This call could simply fail due to bad player state, e.g., after start(). 5239 _prepareDrm(getByteArrayFromUUID(uuid), mDrmSessionId); 5240 Log.v(TAG, "prepareDrm_openSessionStep: _prepareDrm/Crypto succeeded"); 5241 5242 } catch (Exception e) { //ResourceBusyException, NotProvisionedException 5243 Log.e(TAG, "prepareDrm_openSessionStep: open/crypto failed with " + e); 5244 throw e; 5245 } 5246 5247 } 5248 5249 private class ProvisioningThread extends Thread 5250 { 5251 public static final int TIMEOUT_MS = 60000; 5252 5253 private UUID uuid; 5254 private String urlStr; 5255 private Object drmLock; 5256 private OnDrmPreparedHandlerDelegate onDrmPreparedHandlerDelegate; 5257 private MediaPlayer mediaPlayer; 5258 private int status; 5259 private boolean finished; status()5260 public int status() { 5261 return status; 5262 } 5263 initialize(MediaDrm.ProvisionRequest request, UUID uuid, MediaPlayer mediaPlayer)5264 public ProvisioningThread initialize(MediaDrm.ProvisionRequest request, 5265 UUID uuid, MediaPlayer mediaPlayer) { 5266 // lock is held by the caller 5267 drmLock = mediaPlayer.mDrmLock; 5268 onDrmPreparedHandlerDelegate = mediaPlayer.mOnDrmPreparedHandlerDelegate; 5269 this.mediaPlayer = mediaPlayer; 5270 5271 urlStr = request.getDefaultUrl() + "&signedRequest=" + new String(request.getData()); 5272 this.uuid = uuid; 5273 5274 status = PREPARE_DRM_STATUS_PREPARATION_ERROR; 5275 5276 Log.v(TAG, "HandleProvisioninig: Thread is initialised url: " + urlStr); 5277 return this; 5278 } 5279 run()5280 public void run() { 5281 5282 byte[] response = null; 5283 boolean provisioningSucceeded = false; 5284 try { 5285 URL url = new URL(urlStr); 5286 final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 5287 try { 5288 connection.setRequestMethod("POST"); 5289 connection.setDoOutput(false); 5290 connection.setDoInput(true); 5291 connection.setConnectTimeout(TIMEOUT_MS); 5292 connection.setReadTimeout(TIMEOUT_MS); 5293 5294 connection.connect(); 5295 response = Streams.readFully(connection.getInputStream()); 5296 5297 Log.v(TAG, "HandleProvisioninig: Thread run: response " + 5298 response.length + " " + response); 5299 } catch (Exception e) { 5300 status = PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR; 5301 Log.w(TAG, "HandleProvisioninig: Thread run: connect " + e + " url: " + url); 5302 } finally { 5303 connection.disconnect(); 5304 } 5305 } catch (Exception e) { 5306 status = PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR; 5307 Log.w(TAG, "HandleProvisioninig: Thread run: openConnection " + e); 5308 } 5309 5310 if (response != null) { 5311 try { 5312 mDrmObj.provideProvisionResponse(response); 5313 Log.v(TAG, "HandleProvisioninig: Thread run: " + 5314 "provideProvisionResponse SUCCEEDED!"); 5315 5316 provisioningSucceeded = true; 5317 } catch (Exception e) { 5318 status = PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR; 5319 Log.w(TAG, "HandleProvisioninig: Thread run: " + 5320 "provideProvisionResponse " + e); 5321 } 5322 } 5323 5324 boolean succeeded = false; 5325 5326 // non-blocking mode needs the lock 5327 if (onDrmPreparedHandlerDelegate != null) { 5328 5329 synchronized (drmLock) { 5330 // continuing with prepareDrm 5331 if (provisioningSucceeded) { 5332 succeeded = mediaPlayer.resumePrepareDrm(uuid); 5333 status = (succeeded) ? 5334 PREPARE_DRM_STATUS_SUCCESS : 5335 PREPARE_DRM_STATUS_PREPARATION_ERROR; 5336 } 5337 mediaPlayer.mDrmProvisioningInProgress = false; 5338 mediaPlayer.mPrepareDrmInProgress = false; 5339 if (!succeeded) { 5340 cleanDrmObj(); // cleaning up if it hasn't gone through while in the lock 5341 } 5342 } // synchronized 5343 5344 // calling the callback outside the lock 5345 onDrmPreparedHandlerDelegate.notifyClient(status); 5346 } else { // blocking mode already has the lock 5347 5348 // continuing with prepareDrm 5349 if (provisioningSucceeded) { 5350 succeeded = mediaPlayer.resumePrepareDrm(uuid); 5351 status = (succeeded) ? 5352 PREPARE_DRM_STATUS_SUCCESS : 5353 PREPARE_DRM_STATUS_PREPARATION_ERROR; 5354 } 5355 mediaPlayer.mDrmProvisioningInProgress = false; 5356 mediaPlayer.mPrepareDrmInProgress = false; 5357 if (!succeeded) { 5358 cleanDrmObj(); // cleaning up if it hasn't gone through 5359 } 5360 } 5361 5362 finished = true; 5363 } // run() 5364 5365 } // ProvisioningThread 5366 HandleProvisioninig(UUID uuid)5367 private int HandleProvisioninig(UUID uuid) 5368 { 5369 // the lock is already held by the caller 5370 5371 if (mDrmProvisioningInProgress) { 5372 Log.e(TAG, "HandleProvisioninig: Unexpected mDrmProvisioningInProgress"); 5373 return PREPARE_DRM_STATUS_PREPARATION_ERROR; 5374 } 5375 5376 MediaDrm.ProvisionRequest provReq = mDrmObj.getProvisionRequest(); 5377 if (provReq == null) { 5378 Log.e(TAG, "HandleProvisioninig: getProvisionRequest returned null."); 5379 return PREPARE_DRM_STATUS_PREPARATION_ERROR; 5380 } 5381 5382 Log.v(TAG, "HandleProvisioninig provReq " + 5383 " data: " + provReq.getData() + " url: " + provReq.getDefaultUrl()); 5384 5385 // networking in a background thread 5386 mDrmProvisioningInProgress = true; 5387 5388 mDrmProvisioningThread = new ProvisioningThread().initialize(provReq, uuid, this); 5389 mDrmProvisioningThread.start(); 5390 5391 int result; 5392 5393 // non-blocking: this is not the final result 5394 if (mOnDrmPreparedHandlerDelegate != null) { 5395 result = PREPARE_DRM_STATUS_SUCCESS; 5396 } else { 5397 // if blocking mode, wait till provisioning is done 5398 try { 5399 mDrmProvisioningThread.join(); 5400 } catch (Exception e) { 5401 Log.w(TAG, "HandleProvisioninig: Thread.join Exception " + e); 5402 } 5403 result = mDrmProvisioningThread.status(); 5404 // no longer need the thread 5405 mDrmProvisioningThread = null; 5406 } 5407 5408 return result; 5409 } 5410 resumePrepareDrm(UUID uuid)5411 private boolean resumePrepareDrm(UUID uuid) 5412 { 5413 Log.v(TAG, "resumePrepareDrm: uuid: " + uuid); 5414 5415 // mDrmLock is guaranteed to be held 5416 boolean success = false; 5417 try { 5418 // resuming 5419 prepareDrm_openSessionStep(uuid); 5420 5421 mDrmUUID = uuid; 5422 mActiveDrmScheme = true; 5423 5424 success = true; 5425 } catch (Exception e) { 5426 Log.w(TAG, "HandleProvisioninig: Thread run _prepareDrm resume failed with " + e); 5427 // mDrmObj clean up is done by the caller 5428 } 5429 5430 return success; 5431 } 5432 resetDrmState()5433 private void resetDrmState() 5434 { 5435 synchronized (mDrmLock) { 5436 Log.v(TAG, "resetDrmState: " + 5437 " mDrmInfo=" + mDrmInfo + 5438 " mDrmProvisioningThread=" + mDrmProvisioningThread + 5439 " mPrepareDrmInProgress=" + mPrepareDrmInProgress + 5440 " mActiveDrmScheme=" + mActiveDrmScheme); 5441 5442 mDrmInfoResolved = false; 5443 mDrmInfo = null; 5444 5445 if (mDrmProvisioningThread != null) { 5446 // timeout; relying on HttpUrlConnection 5447 try { 5448 mDrmProvisioningThread.join(); 5449 } 5450 catch (InterruptedException e) { 5451 Log.w(TAG, "resetDrmState: ProvThread.join Exception " + e); 5452 } 5453 mDrmProvisioningThread = null; 5454 } 5455 5456 mPrepareDrmInProgress = false; 5457 mActiveDrmScheme = false; 5458 5459 cleanDrmObj(); 5460 } // synchronized 5461 } 5462 cleanDrmObj()5463 private void cleanDrmObj() 5464 { 5465 // the caller holds mDrmLock 5466 Log.v(TAG, "cleanDrmObj: mDrmObj=" + mDrmObj + " mDrmSessionId=" + mDrmSessionId); 5467 5468 if (mDrmSessionId != null) { 5469 mDrmObj.closeSession(mDrmSessionId); 5470 mDrmSessionId = null; 5471 } 5472 if (mDrmObj != null) { 5473 mDrmObj.release(); 5474 mDrmObj = null; 5475 } 5476 } 5477 getByteArrayFromUUID(@onNull UUID uuid)5478 private static final byte[] getByteArrayFromUUID(@NonNull UUID uuid) { 5479 long msb = uuid.getMostSignificantBits(); 5480 long lsb = uuid.getLeastSignificantBits(); 5481 5482 byte[] uuidBytes = new byte[16]; 5483 for (int i = 0; i < 8; ++i) { 5484 uuidBytes[i] = (byte)(msb >>> (8 * (7 - i))); 5485 uuidBytes[8 + i] = (byte)(lsb >>> (8 * (7 - i))); 5486 } 5487 5488 return uuidBytes; 5489 } 5490 5491 // Modular DRM end 5492 5493 /* 5494 * Test whether a given video scaling mode is supported. 5495 */ isVideoScalingModeSupported(int mode)5496 private boolean isVideoScalingModeSupported(int mode) { 5497 return (mode == VIDEO_SCALING_MODE_SCALE_TO_FIT || 5498 mode == VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING); 5499 } 5500 5501 /** @hide */ 5502 static class TimeProvider implements MediaPlayer.OnSeekCompleteListener, 5503 MediaTimeProvider { 5504 private static final String TAG = "MTP"; 5505 private static final long MAX_NS_WITHOUT_POSITION_CHECK = 5000000000L; 5506 private static final long MAX_EARLY_CALLBACK_US = 1000; 5507 private static final long TIME_ADJUSTMENT_RATE = 2; /* meaning 1/2 */ 5508 private long mLastTimeUs = 0; 5509 private MediaPlayer mPlayer; 5510 private boolean mPaused = true; 5511 private boolean mStopped = true; 5512 private boolean mBuffering; 5513 private long mLastReportedTime; 5514 // since we are expecting only a handful listeners per stream, there is 5515 // no need for log(N) search performance 5516 private MediaTimeProvider.OnMediaTimeListener mListeners[]; 5517 private long mTimes[]; 5518 private Handler mEventHandler; 5519 private boolean mRefresh = false; 5520 private boolean mPausing = false; 5521 private boolean mSeeking = false; 5522 private static final int NOTIFY = 1; 5523 private static final int NOTIFY_TIME = 0; 5524 private static final int NOTIFY_STOP = 2; 5525 private static final int NOTIFY_SEEK = 3; 5526 private static final int NOTIFY_TRACK_DATA = 4; 5527 private HandlerThread mHandlerThread; 5528 5529 /** @hide */ 5530 public boolean DEBUG = false; 5531 TimeProvider(MediaPlayer mp)5532 public TimeProvider(MediaPlayer mp) { 5533 mPlayer = mp; 5534 try { 5535 getCurrentTimeUs(true, false); 5536 } catch (IllegalStateException e) { 5537 // we assume starting position 5538 mRefresh = true; 5539 } 5540 5541 Looper looper; 5542 if ((looper = Looper.myLooper()) == null && 5543 (looper = Looper.getMainLooper()) == null) { 5544 // Create our own looper here in case MP was created without one 5545 mHandlerThread = new HandlerThread("MediaPlayerMTPEventThread", 5546 Process.THREAD_PRIORITY_FOREGROUND); 5547 mHandlerThread.start(); 5548 looper = mHandlerThread.getLooper(); 5549 } 5550 mEventHandler = new EventHandler(looper); 5551 5552 mListeners = new MediaTimeProvider.OnMediaTimeListener[0]; 5553 mTimes = new long[0]; 5554 mLastTimeUs = 0; 5555 } 5556 scheduleNotification(int type, long delayUs)5557 private void scheduleNotification(int type, long delayUs) { 5558 // ignore time notifications until seek is handled 5559 if (mSeeking && type == NOTIFY_TIME) { 5560 return; 5561 } 5562 5563 if (DEBUG) Log.v(TAG, "scheduleNotification " + type + " in " + delayUs); 5564 mEventHandler.removeMessages(NOTIFY); 5565 Message msg = mEventHandler.obtainMessage(NOTIFY, type, 0); 5566 mEventHandler.sendMessageDelayed(msg, (int) (delayUs / 1000)); 5567 } 5568 5569 /** @hide */ close()5570 public void close() { 5571 mEventHandler.removeMessages(NOTIFY); 5572 if (mHandlerThread != null) { 5573 mHandlerThread.quitSafely(); 5574 mHandlerThread = null; 5575 } 5576 } 5577 5578 /** @hide */ finalize()5579 protected void finalize() { 5580 if (mHandlerThread != null) { 5581 mHandlerThread.quitSafely(); 5582 } 5583 } 5584 5585 /** @hide */ onNotifyTime()5586 public void onNotifyTime() { 5587 synchronized (this) { 5588 if (DEBUG) Log.d(TAG, "onNotifyTime: "); 5589 scheduleNotification(NOTIFY_TIME, 0 /* delay */); 5590 } 5591 } 5592 5593 /** @hide */ onPaused(boolean paused)5594 public void onPaused(boolean paused) { 5595 synchronized(this) { 5596 if (DEBUG) Log.d(TAG, "onPaused: " + paused); 5597 if (mStopped) { // handle as seek if we were stopped 5598 mStopped = false; 5599 mSeeking = true; 5600 scheduleNotification(NOTIFY_SEEK, 0 /* delay */); 5601 } else { 5602 mPausing = paused; // special handling if player disappeared 5603 mSeeking = false; 5604 scheduleNotification(NOTIFY_TIME, 0 /* delay */); 5605 } 5606 } 5607 } 5608 5609 /** @hide */ onBuffering(boolean buffering)5610 public void onBuffering(boolean buffering) { 5611 synchronized (this) { 5612 if (DEBUG) Log.d(TAG, "onBuffering: " + buffering); 5613 mBuffering = buffering; 5614 scheduleNotification(NOTIFY_TIME, 0 /* delay */); 5615 } 5616 } 5617 5618 /** @hide */ onStopped()5619 public void onStopped() { 5620 synchronized(this) { 5621 if (DEBUG) Log.d(TAG, "onStopped"); 5622 mPaused = true; 5623 mStopped = true; 5624 mSeeking = false; 5625 mBuffering = false; 5626 scheduleNotification(NOTIFY_STOP, 0 /* delay */); 5627 } 5628 } 5629 5630 /** @hide */ 5631 @Override onSeekComplete(MediaPlayer mp)5632 public void onSeekComplete(MediaPlayer mp) { 5633 synchronized(this) { 5634 mStopped = false; 5635 mSeeking = true; 5636 scheduleNotification(NOTIFY_SEEK, 0 /* delay */); 5637 } 5638 } 5639 5640 /** @hide */ onNewPlayer()5641 public void onNewPlayer() { 5642 if (mRefresh) { 5643 synchronized(this) { 5644 mStopped = false; 5645 mSeeking = true; 5646 mBuffering = false; 5647 scheduleNotification(NOTIFY_SEEK, 0 /* delay */); 5648 } 5649 } 5650 } 5651 notifySeek()5652 private synchronized void notifySeek() { 5653 mSeeking = false; 5654 try { 5655 long timeUs = getCurrentTimeUs(true, false); 5656 if (DEBUG) Log.d(TAG, "onSeekComplete at " + timeUs); 5657 5658 for (MediaTimeProvider.OnMediaTimeListener listener: mListeners) { 5659 if (listener == null) { 5660 break; 5661 } 5662 listener.onSeek(timeUs); 5663 } 5664 } catch (IllegalStateException e) { 5665 // we should not be there, but at least signal pause 5666 if (DEBUG) Log.d(TAG, "onSeekComplete but no player"); 5667 mPausing = true; // special handling if player disappeared 5668 notifyTimedEvent(false /* refreshTime */); 5669 } 5670 } 5671 notifyTrackData(Pair<SubtitleTrack, byte[]> trackData)5672 private synchronized void notifyTrackData(Pair<SubtitleTrack, byte[]> trackData) { 5673 SubtitleTrack track = trackData.first; 5674 byte[] data = trackData.second; 5675 track.onData(data, true /* eos */, ~0 /* runID: keep forever */); 5676 } 5677 notifyStop()5678 private synchronized void notifyStop() { 5679 for (MediaTimeProvider.OnMediaTimeListener listener: mListeners) { 5680 if (listener == null) { 5681 break; 5682 } 5683 listener.onStop(); 5684 } 5685 } 5686 registerListener(MediaTimeProvider.OnMediaTimeListener listener)5687 private int registerListener(MediaTimeProvider.OnMediaTimeListener listener) { 5688 int i = 0; 5689 for (; i < mListeners.length; i++) { 5690 if (mListeners[i] == listener || mListeners[i] == null) { 5691 break; 5692 } 5693 } 5694 5695 // new listener 5696 if (i >= mListeners.length) { 5697 MediaTimeProvider.OnMediaTimeListener[] newListeners = 5698 new MediaTimeProvider.OnMediaTimeListener[i + 1]; 5699 long[] newTimes = new long[i + 1]; 5700 System.arraycopy(mListeners, 0, newListeners, 0, mListeners.length); 5701 System.arraycopy(mTimes, 0, newTimes, 0, mTimes.length); 5702 mListeners = newListeners; 5703 mTimes = newTimes; 5704 } 5705 5706 if (mListeners[i] == null) { 5707 mListeners[i] = listener; 5708 mTimes[i] = MediaTimeProvider.NO_TIME; 5709 } 5710 return i; 5711 } 5712 notifyAt( long timeUs, MediaTimeProvider.OnMediaTimeListener listener)5713 public void notifyAt( 5714 long timeUs, MediaTimeProvider.OnMediaTimeListener listener) { 5715 synchronized(this) { 5716 if (DEBUG) Log.d(TAG, "notifyAt " + timeUs); 5717 mTimes[registerListener(listener)] = timeUs; 5718 scheduleNotification(NOTIFY_TIME, 0 /* delay */); 5719 } 5720 } 5721 scheduleUpdate(MediaTimeProvider.OnMediaTimeListener listener)5722 public void scheduleUpdate(MediaTimeProvider.OnMediaTimeListener listener) { 5723 synchronized(this) { 5724 if (DEBUG) Log.d(TAG, "scheduleUpdate"); 5725 int i = registerListener(listener); 5726 5727 if (!mStopped) { 5728 mTimes[i] = 0; 5729 scheduleNotification(NOTIFY_TIME, 0 /* delay */); 5730 } 5731 } 5732 } 5733 cancelNotifications( MediaTimeProvider.OnMediaTimeListener listener)5734 public void cancelNotifications( 5735 MediaTimeProvider.OnMediaTimeListener listener) { 5736 synchronized(this) { 5737 int i = 0; 5738 for (; i < mListeners.length; i++) { 5739 if (mListeners[i] == listener) { 5740 System.arraycopy(mListeners, i + 1, 5741 mListeners, i, mListeners.length - i - 1); 5742 System.arraycopy(mTimes, i + 1, 5743 mTimes, i, mTimes.length - i - 1); 5744 mListeners[mListeners.length - 1] = null; 5745 mTimes[mTimes.length - 1] = NO_TIME; 5746 break; 5747 } else if (mListeners[i] == null) { 5748 break; 5749 } 5750 } 5751 5752 scheduleNotification(NOTIFY_TIME, 0 /* delay */); 5753 } 5754 } 5755 notifyTimedEvent(boolean refreshTime)5756 private synchronized void notifyTimedEvent(boolean refreshTime) { 5757 // figure out next callback 5758 long nowUs; 5759 try { 5760 nowUs = getCurrentTimeUs(refreshTime, true); 5761 } catch (IllegalStateException e) { 5762 // assume we paused until new player arrives 5763 mRefresh = true; 5764 mPausing = true; // this ensures that call succeeds 5765 nowUs = getCurrentTimeUs(refreshTime, true); 5766 } 5767 long nextTimeUs = nowUs; 5768 5769 if (mSeeking) { 5770 // skip timed-event notifications until seek is complete 5771 return; 5772 } 5773 5774 if (DEBUG) { 5775 StringBuilder sb = new StringBuilder(); 5776 sb.append("notifyTimedEvent(").append(mLastTimeUs).append(" -> ") 5777 .append(nowUs).append(") from {"); 5778 boolean first = true; 5779 for (long time: mTimes) { 5780 if (time == NO_TIME) { 5781 continue; 5782 } 5783 if (!first) sb.append(", "); 5784 sb.append(time); 5785 first = false; 5786 } 5787 sb.append("}"); 5788 Log.d(TAG, sb.toString()); 5789 } 5790 5791 Vector<MediaTimeProvider.OnMediaTimeListener> activatedListeners = 5792 new Vector<MediaTimeProvider.OnMediaTimeListener>(); 5793 for (int ix = 0; ix < mTimes.length; ix++) { 5794 if (mListeners[ix] == null) { 5795 break; 5796 } 5797 if (mTimes[ix] <= NO_TIME) { 5798 // ignore, unless we were stopped 5799 } else if (mTimes[ix] <= nowUs + MAX_EARLY_CALLBACK_US) { 5800 activatedListeners.add(mListeners[ix]); 5801 if (DEBUG) Log.d(TAG, "removed"); 5802 mTimes[ix] = NO_TIME; 5803 } else if (nextTimeUs == nowUs || mTimes[ix] < nextTimeUs) { 5804 nextTimeUs = mTimes[ix]; 5805 } 5806 } 5807 5808 if (nextTimeUs > nowUs && !mPaused) { 5809 // schedule callback at nextTimeUs 5810 if (DEBUG) Log.d(TAG, "scheduling for " + nextTimeUs + " and " + nowUs); 5811 mPlayer.notifyAt(nextTimeUs); 5812 } else { 5813 mEventHandler.removeMessages(NOTIFY); 5814 // no more callbacks 5815 } 5816 5817 for (MediaTimeProvider.OnMediaTimeListener listener: activatedListeners) { 5818 listener.onTimedEvent(nowUs); 5819 } 5820 } 5821 getCurrentTimeUs(boolean refreshTime, boolean monotonic)5822 public long getCurrentTimeUs(boolean refreshTime, boolean monotonic) 5823 throws IllegalStateException { 5824 synchronized (this) { 5825 // we always refresh the time when the paused-state changes, because 5826 // we expect to have received the pause-change event delayed. 5827 if (mPaused && !refreshTime) { 5828 return mLastReportedTime; 5829 } 5830 5831 try { 5832 mLastTimeUs = mPlayer.getCurrentPosition() * 1000L; 5833 mPaused = !mPlayer.isPlaying() || mBuffering; 5834 if (DEBUG) Log.v(TAG, (mPaused ? "paused" : "playing") + " at " + mLastTimeUs); 5835 } catch (IllegalStateException e) { 5836 if (mPausing) { 5837 // if we were pausing, get last estimated timestamp 5838 mPausing = false; 5839 if (!monotonic || mLastReportedTime < mLastTimeUs) { 5840 mLastReportedTime = mLastTimeUs; 5841 } 5842 mPaused = true; 5843 if (DEBUG) Log.d(TAG, "illegal state, but pausing: estimating at " + mLastReportedTime); 5844 return mLastReportedTime; 5845 } 5846 // TODO get time when prepared 5847 throw e; 5848 } 5849 if (monotonic && mLastTimeUs < mLastReportedTime) { 5850 /* have to adjust time */ 5851 if (mLastReportedTime - mLastTimeUs > 1000000) { 5852 // schedule seeked event if time jumped significantly 5853 // TODO: do this properly by introducing an exception 5854 mStopped = false; 5855 mSeeking = true; 5856 scheduleNotification(NOTIFY_SEEK, 0 /* delay */); 5857 } 5858 } else { 5859 mLastReportedTime = mLastTimeUs; 5860 } 5861 5862 return mLastReportedTime; 5863 } 5864 } 5865 5866 private class EventHandler extends Handler { EventHandler(Looper looper)5867 public EventHandler(Looper looper) { 5868 super(looper); 5869 } 5870 5871 @Override handleMessage(Message msg)5872 public void handleMessage(Message msg) { 5873 if (msg.what == NOTIFY) { 5874 switch (msg.arg1) { 5875 case NOTIFY_TIME: 5876 notifyTimedEvent(true /* refreshTime */); 5877 break; 5878 case NOTIFY_STOP: 5879 notifyStop(); 5880 break; 5881 case NOTIFY_SEEK: 5882 notifySeek(); 5883 break; 5884 case NOTIFY_TRACK_DATA: 5885 notifyTrackData((Pair<SubtitleTrack, byte[]>)msg.obj); 5886 break; 5887 } 5888 } 5889 } 5890 } 5891 } 5892 5893 public final static class MetricsConstants 5894 { MetricsConstants()5895 private MetricsConstants() {} 5896 5897 /** 5898 * Key to extract the MIME type of the video track 5899 * from the {@link MediaPlayer#getMetrics} return value. 5900 * The value is a String. 5901 */ 5902 public static final String MIME_TYPE_VIDEO = "android.media.mediaplayer.video.mime"; 5903 5904 /** 5905 * Key to extract the codec being used to decode the video track 5906 * from the {@link MediaPlayer#getMetrics} return value. 5907 * The value is a String. 5908 */ 5909 public static final String CODEC_VIDEO = "android.media.mediaplayer.video.codec"; 5910 5911 /** 5912 * Key to extract the width (in pixels) of the video track 5913 * from the {@link MediaPlayer#getMetrics} return value. 5914 * The value is an integer. 5915 */ 5916 public static final String WIDTH = "android.media.mediaplayer.width"; 5917 5918 /** 5919 * Key to extract the height (in pixels) of the video track 5920 * from the {@link MediaPlayer#getMetrics} return value. 5921 * The value is an integer. 5922 */ 5923 public static final String HEIGHT = "android.media.mediaplayer.height"; 5924 5925 /** 5926 * Key to extract the count of video frames played 5927 * from the {@link MediaPlayer#getMetrics} return value. 5928 * The value is an integer. 5929 */ 5930 public static final String FRAMES = "android.media.mediaplayer.frames"; 5931 5932 /** 5933 * Key to extract the count of video frames dropped 5934 * from the {@link MediaPlayer#getMetrics} return value. 5935 * The value is an integer. 5936 */ 5937 public static final String FRAMES_DROPPED = "android.media.mediaplayer.dropped"; 5938 5939 /** 5940 * Key to extract the MIME type of the audio track 5941 * from the {@link MediaPlayer#getMetrics} return value. 5942 * The value is a String. 5943 */ 5944 public static final String MIME_TYPE_AUDIO = "android.media.mediaplayer.audio.mime"; 5945 5946 /** 5947 * Key to extract the codec being used to decode the audio track 5948 * from the {@link MediaPlayer#getMetrics} return value. 5949 * The value is a String. 5950 */ 5951 public static final String CODEC_AUDIO = "android.media.mediaplayer.audio.codec"; 5952 5953 /** 5954 * Key to extract the duration (in milliseconds) of the 5955 * media being played 5956 * from the {@link MediaPlayer#getMetrics} return value. 5957 * The value is a long. 5958 */ 5959 public static final String DURATION = "android.media.mediaplayer.durationMs"; 5960 5961 /** 5962 * Key to extract the playing time (in milliseconds) of the 5963 * media being played 5964 * from the {@link MediaPlayer#getMetrics} return value. 5965 * The value is a long. 5966 */ 5967 public static final String PLAYING = "android.media.mediaplayer.playingMs"; 5968 5969 /** 5970 * Key to extract the count of errors encountered while 5971 * playing the media 5972 * from the {@link MediaPlayer#getMetrics} return value. 5973 * The value is an integer. 5974 */ 5975 public static final String ERRORS = "android.media.mediaplayer.err"; 5976 5977 /** 5978 * Key to extract an (optional) error code detected while 5979 * playing the media 5980 * from the {@link MediaPlayer#getMetrics} return value. 5981 * The value is an integer. 5982 */ 5983 public static final String ERROR_CODE = "android.media.mediaplayer.errcode"; 5984 5985 } 5986 } 5987