| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.media; | 5 package org.chromium.media; |
| 6 | 6 |
| 7 import android.content.Context; | |
| 8 import android.media.MediaPlayer; | 7 import android.media.MediaPlayer; |
| 9 import android.os.SystemClock; | 8 import android.os.SystemClock; |
| 10 | 9 |
| 10 import org.chromium.base.ContextUtils; |
| 11 import org.chromium.base.Log; | 11 import org.chromium.base.Log; |
| 12 import org.chromium.base.annotations.CalledByNative; | 12 import org.chromium.base.annotations.CalledByNative; |
| 13 import org.chromium.base.annotations.JNINamespace; | 13 import org.chromium.base.annotations.JNINamespace; |
| 14 import org.chromium.base.annotations.MainDex; | 14 import org.chromium.base.annotations.MainDex; |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * Class for listening to Android MediaServer crashes to throttle media decoding | 17 * Class for listening to Android MediaServer crashes to throttle media decoding |
| 18 * when needed. | 18 * when needed. |
| 19 */ | 19 */ |
| 20 @MainDex | 20 @MainDex |
| 21 @JNINamespace("media") | 21 @JNINamespace("media") |
| 22 public class MediaServerCrashListener implements MediaPlayer.OnErrorListener { | 22 public class MediaServerCrashListener implements MediaPlayer.OnErrorListener { |
| 23 private static final String TAG = "crMediaCrashListener"; | 23 private static final String TAG = "crMediaCrashListener"; |
| 24 private static final long UNKNOWN_TIME = -1; | 24 private static final long UNKNOWN_TIME = -1; |
| 25 | 25 |
| 26 // Watchdog player. Used to listen to all media server crashes. | 26 // Watchdog player. Used to listen to all media server crashes. |
| 27 private MediaPlayer mPlayer; | 27 private MediaPlayer mPlayer; |
| 28 private final Context mContext; | |
| 29 | 28 |
| 30 // Protecting the creation/release of the watchdog player. | 29 // Protecting the creation/release of the watchdog player. |
| 31 private final Object mLock = new Object(); | 30 private final Object mLock = new Object(); |
| 32 | 31 |
| 33 // Approximate time necessary for the MediaServer to restart after a crash. | 32 // Approximate time necessary for the MediaServer to restart after a crash. |
| 34 private static final int APPROX_MEDIA_SERVER_RESTART_TIME_IN_MS = 5000; | 33 private static final int APPROX_MEDIA_SERVER_RESTART_TIME_IN_MS = 5000; |
| 35 | 34 |
| 36 // The last time we reported a failure to create the watchdog as a server cr
ash. | 35 // The last time we reported a failure to create the watchdog as a server cr
ash. |
| 37 private long mLastReportedWatchdogCreationFailure = UNKNOWN_TIME; | 36 private long mLastReportedWatchdogCreationFailure = UNKNOWN_TIME; |
| 38 | 37 |
| 39 private long mNativeMediaServerCrashListener; | 38 private long mNativeMediaServerCrashListener; |
| 40 | 39 |
| 41 @CalledByNative | 40 @CalledByNative |
| 42 private static MediaServerCrashListener create( | 41 private static MediaServerCrashListener create(long nativeMediaServerCrashLi
stener) { |
| 43 Context context, long nativeMediaServerCrashListener) { | 42 return new MediaServerCrashListener(nativeMediaServerCrashListener); |
| 44 return new MediaServerCrashListener(context, nativeMediaServerCrashListe
ner); | |
| 45 } | 43 } |
| 46 | 44 |
| 47 private MediaServerCrashListener(Context context, long nativeMediaServerCras
hListener) { | 45 private MediaServerCrashListener(long nativeMediaServerCrashListener) { |
| 48 mContext = context; | |
| 49 mNativeMediaServerCrashListener = nativeMediaServerCrashListener; | 46 mNativeMediaServerCrashListener = nativeMediaServerCrashListener; |
| 50 } | 47 } |
| 51 | 48 |
| 52 @CalledByNative | 49 @CalledByNative |
| 53 public void releaseWatchdog() { | 50 public void releaseWatchdog() { |
| 54 if (mPlayer == null) return; | 51 if (mPlayer == null) return; |
| 55 | 52 |
| 56 mPlayer.release(); | 53 mPlayer.release(); |
| 57 mPlayer = null; | 54 mPlayer = null; |
| 58 } | 55 } |
| 59 | 56 |
| 60 @CalledByNative | 57 @CalledByNative |
| 61 public boolean startListening() { | 58 public boolean startListening() { |
| 62 if (mPlayer != null) return true; | 59 if (mPlayer != null) return true; |
| 63 | 60 |
| 64 try { | 61 try { |
| 65 mPlayer = MediaPlayer.create(mContext, R.raw.empty); | 62 mPlayer = MediaPlayer.create(ContextUtils.getApplicationContext(), R
.raw.empty); |
| 66 } catch (IllegalStateException e) { | 63 } catch (IllegalStateException e) { |
| 67 Log.e(TAG, "Exception while creating the watchdog player.", e); | 64 Log.e(TAG, "Exception while creating the watchdog player.", e); |
| 68 } catch (RuntimeException e) { | 65 } catch (RuntimeException e) { |
| 69 Log.e(TAG, "Exception while creating the watchdog player.", e); | 66 Log.e(TAG, "Exception while creating the watchdog player.", e); |
| 70 } | 67 } |
| 71 | 68 |
| 72 if (mPlayer != null) { | 69 if (mPlayer != null) { |
| 73 mPlayer.setOnErrorListener(MediaServerCrashListener.this); | 70 mPlayer.setOnErrorListener(MediaServerCrashListener.this); |
| 74 | 71 |
| 75 // Reset the reported creation failure time on successful | 72 // Reset the reported creation failure time on successful |
| (...skipping 23 matching lines...) Expand all Loading... |
| 99 if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) { | 96 if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) { |
| 100 nativeOnMediaServerCrashDetected(mNativeMediaServerCrashListener, tr
ue); | 97 nativeOnMediaServerCrashDetected(mNativeMediaServerCrashListener, tr
ue); |
| 101 releaseWatchdog(); | 98 releaseWatchdog(); |
| 102 } | 99 } |
| 103 return true; | 100 return true; |
| 104 } | 101 } |
| 105 | 102 |
| 106 private native void nativeOnMediaServerCrashDetected( | 103 private native void nativeOnMediaServerCrashDetected( |
| 107 long nativeMediaServerCrashListener, boolean watchdogNeedsRelease); | 104 long nativeMediaServerCrashListener, boolean watchdogNeedsRelease); |
| 108 } | 105 } |
| OLD | NEW |