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