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 implements BaseHlsPlayer.ResourceCallbacks {
20         @WrapForJNI(calledFrom = "gecko")
Callbacks()21         Callbacks() {}
22 
23         @Override
24         @WrapForJNI
onLoad(String mediaUrl)25         public native void onLoad(String mediaUrl);
26 
27         @Override
28         @WrapForJNI
onDataArrived()29         public native void onDataArrived();
30 
31         @Override
32         @WrapForJNI
onError(int errorCode)33         public native void onError(int errorCode);
34 
35         @Override // JNIObject
disposeNative()36         protected void disposeNative() {
37             throw new UnsupportedOperationException();
38         }
39     } // Callbacks
40 
GeckoHLSResourceWrapper(final String url, final BaseHlsPlayer.ResourceCallbacks callback)41     private GeckoHLSResourceWrapper(final String url,
42                                     final BaseHlsPlayer.ResourceCallbacks callback) {
43         if (DEBUG) Log.d(LOGTAG, "GeckoHLSResourceWrapper created with url = " + url);
44         assertTrue(callback != null);
45 
46         mPlayer = GeckoPlayerFactory.getPlayer();
47         try {
48             mPlayer.init(url, callback);
49         } catch (final Exception e) {
50             Log.e(LOGTAG, "Failed to create GeckoHlsResourceWrapper !", e);
51             callback.onError(BaseHlsPlayer.ResourceError.UNKNOWN.code());
52         }
53     }
54 
55     @WrapForJNI(calledFrom = "gecko")
create(final String url, final BaseHlsPlayer.ResourceCallbacks callback)56     public static GeckoHLSResourceWrapper create(final String url,
57                                                  final BaseHlsPlayer.ResourceCallbacks callback) {
58         return new GeckoHLSResourceWrapper(url, callback);
59     }
60 
61     @WrapForJNI(calledFrom = "gecko")
getPlayerId()62     public int getPlayerId() {
63         // GeckoHLSResourceWrapper should always be created before others
64         assertTrue(!mDestroy);
65         assertTrue(mPlayer != null);
66         return mPlayer.getId();
67     }
68 
69     @WrapForJNI(calledFrom = "gecko")
suspend()70     public void suspend() {
71         if (DEBUG) Log.d(LOGTAG, "GeckoHLSResourceWrapper suspend");
72         if (mPlayer != null) {
73             mPlayer.suspend();
74         }
75     }
76 
77     @WrapForJNI(calledFrom = "gecko")
resume()78     public void resume() {
79         if (DEBUG) Log.d(LOGTAG, "GeckoHLSResourceWrapper resume");
80         if (mPlayer != null) {
81             mPlayer.resume();
82         }
83     }
84 
85     @WrapForJNI(calledFrom = "gecko")
play()86     public void play() {
87         if (DEBUG) Log.d(LOGTAG, "GeckoHLSResourceWrapper mediaelement played");
88         if (mPlayer != null) {
89             mPlayer.play();
90         }
91     }
92 
93     @WrapForJNI(calledFrom = "gecko")
pause()94     public void pause() {
95         if (DEBUG) Log.d(LOGTAG, "GeckoHLSResourceWrapper mediaelement paused");
96         if (mPlayer != null) {
97             mPlayer.pause();
98         }
99     }
100 
assertTrue(final boolean condition)101     private static void assertTrue(final boolean condition) {
102         if (DEBUG && !condition) {
103             throw new AssertionError("Expected condition to be true");
104         }
105     }
106 
107     @WrapForJNI // Called when native object is mDestroy.
destroy()108     private void destroy() {
109         if (DEBUG) Log.d(LOGTAG, "destroy!! Native object is destroyed.");
110         if (mDestroy) {
111             return;
112         }
113         mDestroy = true;
114         if (mPlayer != null) {
115             GeckoPlayerFactory.removePlayer(mPlayer);
116             mPlayer.release();
117             mPlayer = null;
118         }
119     }
120 }
121