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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
/* 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 org.mozilla.gecko.AppConstants;
import android.media.MediaCrypto;
import android.util.Log;
final class LocalMediaDrmBridge implements GeckoMediaDrm {
private static final String LOGTAG = "GeckoLocalMediaDrmBridge";
private static final boolean DEBUG = false;
private GeckoMediaDrm mBridge = null;
private CallbacksForwarder mCallbacksFwd;
// Forward the callback calls from GeckoMediaDrmBridgeV{21,23}
// to the callback MediaDrmProxy.Callbacks.
private class CallbacksForwarder implements GeckoMediaDrm.Callbacks {
private final GeckoMediaDrm.Callbacks mProxyCallbacks;
CallbacksForwarder(GeckoMediaDrm.Callbacks callbacks) {
assertTrue(callbacks != null);
mProxyCallbacks = callbacks;
}
@Override
public void onSessionCreated(int createSessionToken,
int promiseId,
byte[] sessionId,
byte[] request) {
assertTrue(mProxyCallbacks != null);
mProxyCallbacks.onSessionCreated(createSessionToken,
promiseId,
sessionId,
request);
}
@Override
public void onSessionUpdated(int promiseId, byte[] sessionId) {
assertTrue(mProxyCallbacks != null);
mProxyCallbacks.onSessionUpdated(promiseId, sessionId);
}
@Override
public void onSessionClosed(int promiseId, byte[] sessionId) {
assertTrue(mProxyCallbacks != null);
mProxyCallbacks.onSessionClosed(promiseId, sessionId);
}
@Override
public void onSessionMessage(byte[] sessionId,
int sessionMessageType,
byte[] request) {
assertTrue(mProxyCallbacks != null);
mProxyCallbacks.onSessionMessage(sessionId, sessionMessageType, request);
}
@Override
public void onSessionError(byte[] sessionId,
String message) {
assertTrue(mProxyCallbacks != null);
mProxyCallbacks.onSessionError(sessionId, message);
}
@Override
public void onSessionBatchedKeyChanged(byte[] sessionId,
SessionKeyInfo[] keyInfos) {
assertTrue(mProxyCallbacks != null);
mProxyCallbacks.onSessionBatchedKeyChanged(sessionId, keyInfos);
}
@Override
public void onRejectPromise(int promiseId, String message) {
if (DEBUG) Log.d(LOGTAG, message);
assertTrue(mProxyCallbacks != null);
mProxyCallbacks.onRejectPromise(promiseId, message);
}
} // CallbacksForwarder
private static void assertTrue(boolean condition) {
if (DEBUG && !condition) {
throw new AssertionError("Expected condition to be true");
}
}
LocalMediaDrmBridge(String keySystem) throws Exception {
if (AppConstants.Versions.preLollipop) {
mBridge = null;
} else if (AppConstants.Versions.feature21Plus &&
AppConstants.Versions.preMarshmallow) {
mBridge = new GeckoMediaDrmBridgeV21(keySystem);
} else {
mBridge = new GeckoMediaDrmBridgeV23(keySystem);
}
}
@Override
public synchronized void setCallbacks(Callbacks callbacks) {
if (DEBUG) Log.d(LOGTAG, "setCallbacks()");
mCallbacksFwd = new CallbacksForwarder(callbacks);
assertTrue(mBridge != null);
mBridge.setCallbacks(mCallbacksFwd);
}
@Override
public synchronized void createSession(int createSessionToken,
int promiseId,
String initDataType,
byte[] initData) {
if (DEBUG) Log.d(LOGTAG, "createSession()");
assertTrue(mCallbacksFwd != null);
try {
mBridge.createSession(createSessionToken, promiseId, initDataType, initData);
} catch (Exception e) {
Log.e(LOGTAG, "Failed to createSession.", e);
mCallbacksFwd.onRejectPromise(promiseId, "Failed to createSession.");
}
}
@Override
public synchronized void updateSession(int promiseId, String sessionId, byte[] response) {
if (DEBUG) Log.d(LOGTAG, "updateSession()");
assertTrue(mCallbacksFwd != null);
try {
mBridge.updateSession(promiseId, sessionId, response);
} catch (Exception e) {
Log.e(LOGTAG, "Failed to updateSession.", e);
mCallbacksFwd.onRejectPromise(promiseId, "Failed to updateSession.");
}
}
@Override
public synchronized void closeSession(int promiseId, String sessionId) {
if (DEBUG) Log.d(LOGTAG, "closeSession()");
assertTrue(mCallbacksFwd != null);
try {
mBridge.closeSession(promiseId, sessionId);
} catch (Exception e) {
Log.e(LOGTAG, "Failed to closeSession.", e);
mCallbacksFwd.onRejectPromise(promiseId, "Failed to closeSession.");
}
}
@Override
public synchronized void release() {
if (DEBUG) Log.d(LOGTAG, "release()");
try {
mBridge.release();
mBridge = null;
mCallbacksFwd = null;
} catch (Exception e) {
Log.e(LOGTAG, "Failed to release", e);
}
}
@Override
public synchronized MediaCrypto getMediaCrypto() {
if (DEBUG) Log.d(LOGTAG, "getMediaCrypto()");
return mBridge != null ? mBridge.getMediaCrypto() : null;
}
}
|