-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
DRMExample.tsx
231 lines (205 loc) · 6.44 KB
/
DRMExample.tsx
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import * as React from 'react';
import {
Text,
View,
StyleSheet,
Platform,
ScrollView,
TextInput,
Alert,
Button,
ActivityIndicator,
} from 'react-native';
import Video, {DRMType, ReactVideoSourceProperties} from 'react-native-video';
type SourceType = ReactVideoSourceProperties | null;
const DRMExample = () => {
const [loading, setLoading] = React.useState(false);
const [source, setSource] = React.useState<SourceType>(null);
const [hls, setHls] = React.useState(
'https://d2e67eijd6imrw.cloudfront.net/559c7a7e-960d-4cd8-9dba-bc4e59890177/assets/47cfca69-91b5-4311-bf6c-b9b1f297ed9b/videokit-720p-dash-hls-drm/hls/index.m3u8',
);
const [fairplayLicense, setFairplayLicense] = React.useState(
'https://thewidlarzgroup.la.drm.cloud/acquire-license/fairplay?brandGuid=559c7a7e-960d-4cd8-9dba-bc4e59890177',
);
const [fairplayCertificate, setFairplayCertificate] = React.useState(
'https://thewidlarzgroup.la.drm.cloud/certificate/fairplay?brandGuid=559c7a7e-960d-4cd8-9dba-bc4e59890177',
);
const [dash, setDash] = React.useState(
'https://d2e67eijd6imrw.cloudfront.net/559c7a7e-960d-4cd8-9dba-bc4e59890177/assets/47cfca69-91b5-4311-bf6c-b9b1f297ed9b/videokit-720p-dash-hls-drm/dash/index.mpd',
);
const [widevineLicense, setWidevineLicense] = React.useState(
'https://thewidlarzgroup.la.drm.cloud/acquire-license/widevine?brandGuid=559c7a7e-960d-4cd8-9dba-bc4e59890177',
);
// ------------- DMR Token -------------
// This token is used to authenticate the user and get the license
// To run example please go to https://www.thewidlarzgroup.com/services/free-drm-token-generator-for-video?utm_source=drm&utm_medium=code and complete the form to receive the token
// After you receive the token, please paste it here
const [token, setToken] = React.useState('<USER_TOKEN>');
const handlePlayStopVideo = () => {
if (source !== null) {
setSource(null);
return;
}
if (token === '<USER_TOKEN>') {
Alert.alert('Error', 'Please enter the token received from the website');
return;
}
setLoading(true);
const newSource: ReactVideoSourceProperties = {};
if (Platform.OS === 'ios') {
if (fairplayLicense && fairplayCertificate) {
newSource.uri = hls;
newSource.drm = {
type: DRMType.FAIRPLAY,
licenseServer: fairplayLicense,
certificateUrl: fairplayCertificate,
getLicense: (spcString, contentId, licenseUrl, loadedLicenseUrl) => {
const formData = new FormData();
formData.append('spc', spcString);
const resultURL = loadedLicenseUrl.replace('skd://', 'https://');
return fetch(`${resultURL}&userToken=${token}`, {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
Accept: 'application/json',
},
body: formData,
})
.then((response) => response.json())
.then((response) => {
return response.ckc;
})
.catch((error) => {
console.error('Error', error);
});
},
};
} else {
Alert.alert('Error', 'Please enter Fairplay License and Certificate');
setLoading(false);
}
}
if (Platform.OS === 'android') {
if (widevineLicense) {
newSource.drm = {
type: DRMType.WIDEVINE,
licenseServer: widevineLicense,
};
newSource.uri = dash;
} else {
Alert.alert('Error', 'Please enter Widevine License');
setLoading(false);
}
}
setSource(newSource);
};
if (Platform.OS !== 'ios' && Platform.OS !== 'android') {
return (
<View style={styles.container}>
<Text>DRM is not supported on this platform</Text>
</View>
);
}
return (
<ScrollView contentContainerStyle={styles.container}>
<Text style={styles.title}>DRM Protected Stream Player</Text>
{loading && <ActivityIndicator size="large" color="#0000ff" />}
{source && source.uri && (
<Video
key={source.uri}
onLoad={() => {
setLoading(false);
}}
onError={(e) => {
console.log('error', e);
Alert.alert('Error', e.error.localizedDescription);
setLoading(false);
}}
source={source}
resizeMode="contain"
style={styles.video}
controls
muted={false}
/>
)}
{Platform.OS === 'ios' && (
<>
<TextInput
style={styles.input}
placeholder="HLS URL"
value={hls}
onChangeText={(text) => setHls(text)}
/>
<TextInput
style={styles.input}
placeholder="Fairplay License URL"
value={fairplayLicense}
onChangeText={(text) => setFairplayLicense(text)}
/>
<TextInput
style={styles.input}
placeholder="Fairplay Certificate URL"
value={fairplayCertificate}
onChangeText={(text) => setFairplayCertificate(text)}
/>
</>
)}
{Platform.OS === 'android' && (
<>
<TextInput
style={styles.input}
placeholder="DASH URL"
value={dash}
onChangeText={(text) => setDash(text)}
/>
<TextInput
style={styles.input}
placeholder="Widevine License URL"
value={widevineLicense}
onChangeText={(text) => setWidevineLicense(text)}
/>
</>
)}
<TextInput
style={styles.input}
placeholder="Token"
value={token}
onChangeText={(text) => setToken(text)}
/>
<Button
title={`${source !== null ? 'Stop' : 'Play'} Video`}
onPress={handlePlayStopVideo}
/>
</ScrollView>
);
};
export default DRMExample;
const styles = StyleSheet.create({
container: {
flexGrow: 1,
justifyContent: 'center',
alignItems: 'center',
padding: 20,
backgroundColor: 'black',
},
title: {
fontSize: 24,
fontWeight: 'bold',
marginBottom: 20,
color: 'white',
},
video: {
width: '100%',
height: 200,
marginBottom: 80,
},
input: {
height: 40,
borderColor: 'gray',
borderWidth: 1,
marginBottom: 10,
paddingHorizontal: 10,
width: '100%',
color: 'white',
},
});