summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/main/java/org/mozilla/gecko/media/GeckoHLSResourceWrapper.java
blob: c21789fdd096af88a50e21a36d9ed3c0c7afb0d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.media;

import android.util.Log;
import org.mozilla.gecko.annotation.WrapForJNI;
import org.mozilla.gecko.mozglue.JNIObject;
import org.mozilla.geckoview.BuildConfig;

public class GeckoHLSResourceWrapper {
  private static final String LOGTAG = "GeckoHLSResourceWrapper";
  private static final boolean DEBUG = !BuildConfig.MOZILLA_OFFICIAL;
  private BaseHlsPlayer mPlayer = null;
  private boolean mDestroy = false;

  public static class Callbacks extends JNIObject implements BaseHlsPlayer.ResourceCallbacks {
    @WrapForJNI(calledFrom = "gecko")
    Callbacks() {}

    @Override
    @WrapForJNI
    public native void onLoad(String mediaUrl);

    @Override
    @WrapForJNI
    public native void onDataArrived();

    @Override
    @WrapForJNI
    public native void onError(int errorCode);

    @Override // JNIObject
    protected void disposeNative() {
      throw new UnsupportedOperationException();
    }
  } // Callbacks

  private GeckoHLSResourceWrapper(
      final String url, final BaseHlsPlayer.ResourceCallbacks callback) {
    if (DEBUG) Log.d(LOGTAG, "GeckoHLSResourceWrapper created with url = " + url);
    assertTrue(callback != null);

    mPlayer = GeckoPlayerFactory.getPlayer();
    try {
      mPlayer.init(url, callback);
    } catch (final Exception e) {
      Log.e(LOGTAG, "Failed to create GeckoHlsResourceWrapper !", e);
      callback.onError(BaseHlsPlayer.ResourceError.UNKNOWN.code());
    }
  }

  @WrapForJNI(calledFrom = "gecko")
  public static GeckoHLSResourceWrapper create(
      final String url, final BaseHlsPlayer.ResourceCallbacks callback) {
    return new GeckoHLSResourceWrapper(url, callback);
  }

  @WrapForJNI(calledFrom = "gecko")
  public int getPlayerId() {
    // GeckoHLSResourceWrapper should always be created before others
    assertTrue(!mDestroy);
    assertTrue(mPlayer != null);
    return mPlayer.getId();
  }

  @WrapForJNI(calledFrom = "gecko")
  public void suspend() {
    if (DEBUG) Log.d(LOGTAG, "GeckoHLSResourceWrapper suspend");
    if (mPlayer != null) {
      mPlayer.suspend();
    }
  }

  @WrapForJNI(calledFrom = "gecko")
  public void resume() {
    if (DEBUG) Log.d(LOGTAG, "GeckoHLSResourceWrapper resume");
    if (mPlayer != null) {
      mPlayer.resume();
    }
  }

  @WrapForJNI(calledFrom = "gecko")
  public void play() {
    if (DEBUG) Log.d(LOGTAG, "GeckoHLSResourceWrapper mediaelement played");
    if (mPlayer != null) {
      mPlayer.play();
    }
  }

  @WrapForJNI(calledFrom = "gecko")
  public void pause() {
    if (DEBUG) Log.d(LOGTAG, "GeckoHLSResourceWrapper mediaelement paused");
    if (mPlayer != null) {
      mPlayer.pause();
    }
  }

  private static void assertTrue(final boolean condition) {
    if (DEBUG && !condition) {
      throw new AssertionError("Expected condition to be true");
    }
  }

  @WrapForJNI // Called when native object is mDestroy.
  private void destroy() {
    if (DEBUG) Log.d(LOGTAG, "destroy!! Native object is destroyed.");
    if (mDestroy) {
      return;
    }
    mDestroy = true;
    if (mPlayer != null) {
      GeckoPlayerFactory.removePlayer(mPlayer);
      mPlayer.release();
      mPlayer = null;
    }
  }
}