PageRenderTime 64ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/content/public/android/java/src/org/chromium/content/browser/MediaThrottler.java

https://gitlab.com/jonnialva90/iridium-browser
Java | 218 lines | 140 code | 26 blank | 52 comment | 26 complexity | 0001c546a2b5455c7c3395b3657dfcfe MD5 | raw file
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. package org.chromium.content.browser;
  5. import android.content.Context;
  6. import android.media.MediaPlayer;
  7. import android.os.AsyncTask;
  8. import android.os.Handler;
  9. import android.os.Looper;
  10. import android.os.SystemClock;
  11. import org.chromium.base.Log;
  12. import org.chromium.base.annotations.CalledByNative;
  13. import org.chromium.base.annotations.JNINamespace;
  14. import org.chromium.base.metrics.RecordHistogram;
  15. import org.chromium.content.R;
  16. /**
  17. * Class for listening to Android MediaServer Crashes to throttle media decoding
  18. * when needed.
  19. */
  20. @JNINamespace("content")
  21. class MediaThrottler implements MediaPlayer.OnErrorListener {
  22. private static final String TAG = "cr_MediaThrottler";
  23. private static final long UNKNOWN_LAST_SERVER_CRASH_TIME = -1;
  24. // Number of active decode requests.
  25. private int mRequestCount;
  26. // Application context.
  27. private final Context mContext;
  28. // Watch dog player. Used to listen to all media server crashes.
  29. private MediaPlayer mPlayer;
  30. // The last media server crash time since Chrome lauches.
  31. private long mLastCrashTime = UNKNOWN_LAST_SERVER_CRASH_TIME;
  32. // Server crash count since last reset() call.
  33. private int mServerCrashCount;
  34. // Object for synchronized access to memeber variables.
  35. private final Object mLock = new Object();
  36. // Handler for posting delayed tasks.
  37. private Handler mHandler;
  38. // Intervals between media server crashes that are considered normal. It
  39. // takes about 5 seconds to restart the media server. So this value has to
  40. // be larger than 5 seconds.
  41. private static final long SERVER_CRASH_INTERVAL_THRESHOLD_IN_MILLIS = 60000;
  42. // Delay to keep the watch dog player alive When there are no decoding
  43. // requests. This is introduced to avoid recreating the watch dog over and
  44. // over if a burst of small decoding requests arrive.
  45. private static final int RELEASE_WATCH_DOG_PLAYER_DELAY_IN_MILLIS = 5000;
  46. // When |mServerCrashCount| reaches this threshold, throttling will start.
  47. // This is to prevent a page from loading a malformed video over and over
  48. // to crash the media server excessively.
  49. private static final int SERVER_CRASH_COUNT_THRESHOLD_FOR_THROTTLING = 4;
  50. /**
  51. * A background task to release the watch dog player.
  52. */
  53. private class ReleaseWatchDogTask extends AsyncTask<Void, Void, Void> {
  54. @Override
  55. protected Void doInBackground(Void... voids) {
  56. synchronized (mLock) {
  57. if (mRequestCount == 0 && mPlayer != null) {
  58. mPlayer.release();
  59. mPlayer = null;
  60. }
  61. }
  62. return null;
  63. }
  64. }
  65. private final Runnable mDelayedReleaseRunnable = new Runnable() {
  66. @Override
  67. public void run() {
  68. new ReleaseWatchDogTask().execute();
  69. }
  70. };
  71. @CalledByNative
  72. private static MediaThrottler create(Context context) {
  73. return new MediaThrottler(context);
  74. }
  75. private MediaThrottler(Context context) {
  76. mContext = context;
  77. mHandler = new Handler(Looper.getMainLooper());
  78. }
  79. /**
  80. * A background task to start the watch dog player.
  81. */
  82. private class StartWatchDogTask extends AsyncTask<Void, Void, Void> {
  83. @Override
  84. protected Void doInBackground(Void... voids) {
  85. synchronized (mLock) {
  86. if (mPlayer != null || mRequestCount == 0) return null;
  87. mPlayer = MediaPlayer.create(mContext, R.raw.empty);
  88. if (mPlayer == null) {
  89. Log.e(TAG, "Unable to create watch dog player, treat it as server crash.");
  90. onMediaServerCrash();
  91. } else {
  92. mPlayer.setOnErrorListener(MediaThrottler.this);
  93. }
  94. }
  95. return null;
  96. }
  97. }
  98. /**
  99. * Called to request the permission to decode media data.
  100. *
  101. * @return true if the request is permitted, or false otherwise.
  102. */
  103. @CalledByNative
  104. private boolean requestDecoderResources() {
  105. synchronized (mLock) {
  106. long currentTime = SystemClock.elapsedRealtime();
  107. if (mLastCrashTime != UNKNOWN_LAST_SERVER_CRASH_TIME
  108. && (currentTime - mLastCrashTime < SERVER_CRASH_INTERVAL_THRESHOLD_IN_MILLIS)
  109. && mServerCrashCount >= SERVER_CRASH_COUNT_THRESHOLD_FOR_THROTTLING) {
  110. Log.e(TAG, "Request to decode media data denied due to throttling.");
  111. return false;
  112. }
  113. mRequestCount++;
  114. if (mRequestCount == 1) {
  115. mHandler.removeCallbacks(mDelayedReleaseRunnable);
  116. mHandler.post(new Runnable() {
  117. @Override
  118. public void run() {
  119. new StartWatchDogTask().execute();
  120. }
  121. });
  122. }
  123. }
  124. return true;
  125. }
  126. /**
  127. * Called to signal that a decode request has been completed.
  128. */
  129. @CalledByNative
  130. private void onDecodeRequestFinished() {
  131. synchronized (mLock) {
  132. mRequestCount--;
  133. if (mRequestCount == 0) {
  134. // Don't release the watch dog immediately, there could be a
  135. // number of small requests coming together.
  136. prepareToStopWatchDog();
  137. }
  138. }
  139. }
  140. /**
  141. * Posts a delayed task to stop the watch dog player.
  142. */
  143. private void prepareToStopWatchDog() {
  144. mHandler.postDelayed(mDelayedReleaseRunnable, RELEASE_WATCH_DOG_PLAYER_DELAY_IN_MILLIS);
  145. }
  146. @Override
  147. public boolean onError(MediaPlayer mp, int what, int extra) {
  148. if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
  149. synchronized (mLock) {
  150. onMediaServerCrash();
  151. }
  152. }
  153. return true;
  154. }
  155. /**
  156. * Called when media server crashes.
  157. */
  158. private void onMediaServerCrash() {
  159. assert Thread.holdsLock(mLock);
  160. long currentTime = SystemClock.elapsedRealtime();
  161. if (mLastCrashTime != UNKNOWN_LAST_SERVER_CRASH_TIME
  162. && (currentTime - mLastCrashTime < SERVER_CRASH_INTERVAL_THRESHOLD_IN_MILLIS)) {
  163. mServerCrashCount++;
  164. } else {
  165. recordNumMediaServerCrashes();
  166. mServerCrashCount = 1;
  167. }
  168. mLastCrashTime = currentTime;
  169. }
  170. /**
  171. * Resets the MediaThrottler to its initial state so that subsequent requests
  172. * will not be throttled.
  173. */
  174. @CalledByNative
  175. private void reset() {
  176. synchronized (mLock) {
  177. recordNumMediaServerCrashes();
  178. mServerCrashCount = 0;
  179. mLastCrashTime = UNKNOWN_LAST_SERVER_CRASH_TIME;
  180. }
  181. }
  182. /**
  183. * Records the number of consecutive media server crashes in UMA.
  184. */
  185. private void recordNumMediaServerCrashes() {
  186. assert Thread.holdsLock(mLock);
  187. if (mServerCrashCount > 0) {
  188. RecordHistogram.recordCountHistogram(
  189. "Media.Android.NumMediaServerCrashes", mServerCrashCount);
  190. }
  191. }
  192. }