1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 package org.mozilla.gecko.media; 6 7 import android.util.Log; 8 9 import org.mozilla.geckoview.BuildConfig; 10 import org.mozilla.gecko.annotation.WrapForJNI; 11 import org.mozilla.gecko.mozglue.JNIObject; 12 13 public class GeckoHLSResourceWrapper { 14 private static final String LOGTAG = "GeckoHLSResourceWrapper"; 15 private static final boolean DEBUG = !BuildConfig.MOZILLA_OFFICIAL; 16 private BaseHlsPlayer mPlayer = null; 17 private boolean mDestroy = false; 18 19 public static class Callbacks extends JNIObject 20 implements BaseHlsPlayer.ResourceCallbacks { 21 @WrapForJNI(calledFrom = "gecko") Callbacks()22 Callbacks() {} 23 24 @Override 25 @WrapForJNI onDataArrived()26 public native void onDataArrived(); 27 28 @Override 29 @WrapForJNI onError(int errorCode)30 public native void onError(int errorCode); 31 32 @Override // JNIObject disposeNative()33 protected void disposeNative() { 34 throw new UnsupportedOperationException(); 35 } 36 } // Callbacks 37 GeckoHLSResourceWrapper(String url, BaseHlsPlayer.ResourceCallbacks callback)38 private GeckoHLSResourceWrapper(String url, 39 BaseHlsPlayer.ResourceCallbacks callback) { 40 if (DEBUG) Log.d(LOGTAG, "GeckoHLSResourceWrapper created with url = " + url); 41 assertTrue(callback != null); 42 43 mPlayer = GeckoPlayerFactory.getPlayer(); 44 try { 45 mPlayer.init(url, callback); 46 } catch (Exception e) { 47 Log.e(LOGTAG, "Failed to create GeckoHlsResourceWrapper !", e); 48 callback.onError(BaseHlsPlayer.ResourceError.UNKNOWN.code()); 49 } 50 } 51 52 @WrapForJNI(calledFrom = "gecko") create(String url, BaseHlsPlayer.ResourceCallbacks callback)53 public static GeckoHLSResourceWrapper create(String url, 54 BaseHlsPlayer.ResourceCallbacks callback) { 55 return new GeckoHLSResourceWrapper(url, callback); 56 } 57 58 @WrapForJNI(calledFrom = "gecko") getPlayerId()59 public int getPlayerId() { 60 // GeckoHLSResourceWrapper should always be created before others 61 assertTrue(!mDestroy); 62 assertTrue(mPlayer != null); 63 return mPlayer.getId(); 64 } 65 66 @WrapForJNI(calledFrom = "gecko") suspend()67 public void suspend() { 68 if (DEBUG) Log.d(LOGTAG, "GeckoHLSResourceWrapper suspend"); 69 if (mPlayer != null) { 70 mPlayer.suspend(); 71 } 72 } 73 74 @WrapForJNI(calledFrom = "gecko") resume()75 public void resume() { 76 if (DEBUG) Log.d(LOGTAG, "GeckoHLSResourceWrapper resume"); 77 if (mPlayer != null) { 78 mPlayer.resume(); 79 } 80 } 81 82 @WrapForJNI(calledFrom = "gecko") play()83 public void play() { 84 if (DEBUG) Log.d(LOGTAG, "GeckoHLSResourceWrapper mediaelement played"); 85 if (mPlayer != null) { 86 mPlayer.play(); 87 } 88 } 89 90 @WrapForJNI(calledFrom = "gecko") pause()91 public void pause() { 92 if (DEBUG) Log.d(LOGTAG, "GeckoHLSResourceWrapper mediaelement paused"); 93 if (mPlayer != null) { 94 mPlayer.pause(); 95 } 96 } 97 assertTrue(boolean condition)98 private static void assertTrue(boolean condition) { 99 if (DEBUG && !condition) { 100 throw new AssertionError("Expected condition to be true"); 101 } 102 } 103 104 @WrapForJNI // Called when native object is mDestroy. destroy()105 private void destroy() { 106 if (DEBUG) Log.d(LOGTAG, "destroy!! Native object is destroyed."); 107 if (mDestroy) { 108 return; 109 } 110 mDestroy = true; 111 if (mPlayer != null) { 112 GeckoPlayerFactory.removePlayer(mPlayer); 113 mPlayer.release(); 114 mPlayer = null; 115 } 116 } 117 } 118