@playkit-js/rapt 0.4.10-canary.0-878cdb7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/Kip.ts ADDED
@@ -0,0 +1,57 @@
1
+ import KalturaInteractiveVideo from "./KalturaInteractiveVideo";
2
+ import "./Kip.scss";
3
+ import "script-loader!../libs/engineWithNodeId.min.js";
4
+ import { VERSION } from "../version";
5
+ import { log } from "./helpers/logger";
6
+
7
+ declare var KalturaPlayer: any;
8
+ declare var __kalturaplayerdata: any;
9
+
10
+ export interface RaptConfig {
11
+ abr?: any;
12
+ ui?: any;
13
+ rapt: any;
14
+ targetId: string;
15
+ playback?: any;
16
+ sources?: { poster?: any };
17
+ provider?: any;
18
+ plugins?: any;
19
+ gaTrackId?: string;
20
+ }
21
+ function setup(config: RaptConfig): KalturaInteractiveVideo {
22
+ if (console && console.log) {
23
+ console.log("Path player " + VERSION);
24
+ }
25
+ // merge uiconf rapt data with current config (priority is local config) - this is a one-level object
26
+ // extract the uiconf JSON
27
+ try {
28
+ const uiconfData: any = __kalturaplayerdata.UIConf ? Object.values(__kalturaplayerdata.UIConf)[0] : __kalturaplayerdata;
29
+ const uiconfRaptData: any = uiconfData.rapt || {};
30
+ const uiconfPlaybackData: any =
31
+ (uiconfData.player && uiconfData.player.playback) || uiconfData.playback || {};
32
+ // local config will override uiconf properties.
33
+ config = {
34
+ ...config,
35
+ rapt: {
36
+ ...uiconfRaptData,
37
+ ...config.rapt
38
+ },
39
+ playback: {
40
+ ...uiconfPlaybackData,
41
+ ...config.playback
42
+ }
43
+ };
44
+ // detect Google Analytics
45
+ if (
46
+ uiconfData.player.plugins.googleAnalytics &&
47
+ uiconfData.player.plugins.googleAnalytics.trackingId
48
+ ) {
49
+ config.gaTrackId = uiconfData.player.plugins.googleAnalytics.trackingId;
50
+ log("log", "Kip", "Google Analytics exists ");
51
+ }
52
+ } catch (error) {
53
+ log("log", "Kip", "Fail to merge local config and uiconf", error);
54
+ }
55
+ return new KalturaInteractiveVideo(config, KalturaPlayer);
56
+ }
57
+ export { setup };
@@ -0,0 +1,250 @@
1
+ import {
2
+ KalturaAPIException,
3
+ KalturaClient,
4
+ KalturaClientException,
5
+ KalturaMultiRequest,
6
+ KalturaRequestOptions,
7
+ } from "kaltura-typescript-client";
8
+ import { Dispatcher } from "./helpers/Dispatcher";
9
+ import { KalturaPlaylist } from "kaltura-typescript-client/api/types/KalturaPlaylist";
10
+ import { KalturaPlaylistType } from "kaltura-typescript-client/api/types/KalturaPlaylistType";
11
+ import { KalturaFileAsset } from "../node_modules/kaltura-typescript-client/api/types/KalturaFileAsset";
12
+ import { PlaylistGetAction } from "../node_modules/kaltura-typescript-client/api/types/PlaylistGetAction";
13
+ import { FileAssetListAction } from "../node_modules/kaltura-typescript-client/api/types/FileAssetListAction";
14
+ import { KalturaFileAssetFilter } from "../node_modules/kaltura-typescript-client/api/types/KalturaFileAssetFilter";
15
+ import { KalturaInteractivityNodeFilter } from "kaltura-typescript-client/api/types/KalturaInteractivityNodeFilter";
16
+ import { KalturaInteractivityDataFilter } from "kaltura-typescript-client/api/types/KalturaInteractivityDataFilter";
17
+ import { KalturaInteractivityRootFilter } from "kaltura-typescript-client/api/types/KalturaInteractivityRootFilter";
18
+ import { InteractivityGetAction } from "../node_modules/kaltura-typescript-client/api/types/InteractivityGetAction";
19
+ import { KalturaFileAssetObjectType } from "../node_modules/kaltura-typescript-client/api/types/KalturaFileAssetObjectType";
20
+ import { SessionStartWidgetSessionAction } from "../node_modules/kaltura-typescript-client/api/types/SessionStartWidgetSessionAction";
21
+
22
+ interface ClientConfig {
23
+ ks?: string;
24
+ serviceUrl?: string;
25
+ partnerId?: string;
26
+ widgetId?: string;
27
+ capabilityCheck?: boolean;
28
+ }
29
+
30
+ /**
31
+ * This class handles all BE callbacks API
32
+ */
33
+
34
+ export class KipClient extends Dispatcher {
35
+ kClient: KalturaClient;
36
+ serviceUrl: string;
37
+ partnerId: string;
38
+ clientTag: string = "rapt-v3-app";
39
+ config: ClientConfig;
40
+ ks: string;
41
+ widgetId: string;
42
+ capabilityCheck: boolean;
43
+
44
+ constructor(config: ClientConfig) {
45
+ super();
46
+ this.serviceUrl = config.serviceUrl;
47
+ this.ks = config.ks;
48
+ this.partnerId = config.partnerId;
49
+ this.widgetId = config.widgetId;
50
+ this.capabilityCheck = config.capabilityCheck;
51
+ this.config = { ks: this.ks, serviceUrl: this.serviceUrl };
52
+ }
53
+
54
+ /**
55
+ * Initialize Kaltura client
56
+ * @param serviceUrl
57
+ * @param clientTag
58
+ * @param ks
59
+ */
60
+ initClient(serviceUrl: string, clientTag: string, ks: string): KalturaClient {
61
+ return new KalturaClient(
62
+ {
63
+ endpointUrl: serviceUrl,
64
+ clientTag: clientTag,
65
+ },
66
+ {
67
+ ks: ks,
68
+ }
69
+ );
70
+ }
71
+
72
+ /**
73
+ * Load the rapt data by playlist id (Promise)
74
+ * @param raptPlaylistId
75
+ */
76
+ loadRaptData(raptPlaylistId: string): Promise<object> {
77
+ return new Promise((resolve, reject) => {
78
+ this.kClient = this.initClient(
79
+ this.config.serviceUrl,
80
+ this.clientTag,
81
+ this.config.ks
82
+ );
83
+ const filter: KalturaFileAssetFilter = new KalturaFileAssetFilter();
84
+ filter.objectIdEqual = raptPlaylistId;
85
+ filter.fileAssetObjectTypeEqual = KalturaFileAssetObjectType.entry;
86
+
87
+ let multiRequest: KalturaMultiRequest;
88
+
89
+ // NO KS given - build a multi request with 1st action to retrieve a KS
90
+ if (!this.config.ks) {
91
+ // no given KS - we need to add a widget-session request
92
+ multiRequest = new KalturaMultiRequest(
93
+ new SessionStartWidgetSessionAction({
94
+ widgetId: this.widgetId || "_" + this.partnerId,
95
+ }),
96
+ new FileAssetListAction({
97
+ filter: filter,
98
+ }).setRequestOptions(
99
+ new KalturaRequestOptions({}).setDependency(["ks", 0, "ks"])
100
+ )
101
+ );
102
+ } else {
103
+ multiRequest = new KalturaMultiRequest(
104
+ new FileAssetListAction({
105
+ filter: filter,
106
+ }),
107
+ new PlaylistGetAction({
108
+ id: raptPlaylistId,
109
+ })
110
+ );
111
+ }
112
+ this.kClient.multiRequest(multiRequest).then(
113
+ (data: any) => {
114
+ // API error
115
+ if (data[0].error) {
116
+ console.log(data[0].error);
117
+ reject(data[0].error);
118
+ return;
119
+ }
120
+ // TODO - handle no-ks with new path data later.
121
+ if (
122
+ data.length === 2 &&
123
+ data[1].result instanceof KalturaPlaylist &&
124
+ (data[1].result.playlistType === KalturaPlaylistType.path || // path entry || capabilities check
125
+ (data[1].result.capabilities.includes(
126
+ "interactivity.interactivity"
127
+ ) &&
128
+ this.capabilityCheck))
129
+ ) {
130
+ // warn if capabilityCheck is set to true
131
+ if (
132
+ data[1].result.capabilities.includes(
133
+ "interactivity.interactivity"
134
+ ) &&
135
+ this.capabilityCheck
136
+ ) {
137
+ console.warn(
138
+ "Provided entry is hybrid Rapt entry. Running Rapt Player in test mode with interactivities"
139
+ );
140
+ }
141
+
142
+ const dataFilter = new KalturaInteractivityDataFilter();
143
+ dataFilter.rootFilter = new KalturaInteractivityRootFilter({
144
+ fields: "nodes,pathData",
145
+ });
146
+ dataFilter.nodeFilter = new KalturaInteractivityNodeFilter({
147
+ fields: "pathData,name,interactions,id,entryId",
148
+ });
149
+ const interactivityRequest = new InteractivityGetAction({
150
+ entryId: raptPlaylistId,
151
+ dataFilter: dataFilter,
152
+ });
153
+ this.kClient.request(interactivityRequest).then(
154
+ (data: any) => {
155
+ if (!data.data) {
156
+ reject(
157
+ "Missing data. Could not retrieve interactivity data for playlist " +
158
+ raptPlaylistId
159
+ );
160
+ }
161
+ resolve(JSON.parse(data.data));
162
+ },
163
+ () => {
164
+ reject(
165
+ "Got an error during getting interactivity data for playlist " +
166
+ raptPlaylistId
167
+ );
168
+ }
169
+ );
170
+ } else {
171
+ let fileAssetObjects: any;
172
+ if (
173
+ data.length === 2 &&
174
+ !(data[1].result instanceof KalturaPlaylist)
175
+ ) {
176
+ // this was a request with a KS request - extract the KS, set it to the client and then continue with data
177
+ if (data[0].error) {
178
+ reject(
179
+ "Error with generating widget session KS for pid " +
180
+ this.partnerId
181
+ );
182
+ }
183
+
184
+ if (!data[1].result.objects || !data[1].result.objects.length) {
185
+ reject(
186
+ "Missing data. Could not retrieve attached file assets for playlist " +
187
+ raptPlaylistId
188
+ );
189
+ }
190
+
191
+ this.ks = data[0].result.ks;
192
+ this.kClient.setDefaultRequestOptions({ ks: this.ks });
193
+ fileAssetObjects = data[1].result.objects;
194
+ } else {
195
+ // Just a FileAssetList request
196
+ fileAssetObjects = data[0].result.objects;
197
+ }
198
+ const graphDataFileAsset: any = fileAssetObjects.find(
199
+ (item: KalturaFileAsset) => item.systemName === "GRAPH_DATA"
200
+ );
201
+ // get the graph-data file content
202
+ this.serveAssetById(graphDataFileAsset.id).then((res) => {
203
+ resolve(JSON.parse(res));
204
+ });
205
+ }
206
+ },
207
+ (err) => {
208
+ if (err instanceof KalturaClientException) {
209
+ reject("Network/Client error");
210
+ } else if (err instanceof KalturaAPIException) {
211
+ reject(
212
+ "Multirequest API error, check your KS and Playlist data validation"
213
+ );
214
+ }
215
+ }
216
+ );
217
+ });
218
+ }
219
+
220
+ /**
221
+ * Load the content of the fileAsset (Promise)
222
+ * @param id
223
+ */
224
+ serveAssetById(id: number): Promise<string> {
225
+ return new Promise((resolve, reject) => {
226
+ const urlToLoad =
227
+ this.serviceUrl +
228
+ "/api_v3/index.php?service=fileAsset&apiVersion=3.1&expiry=86400&clientTag=" +
229
+ this.clientTag +
230
+ "&format=1&ignoreNull=1&action=serve&id=" +
231
+ id +
232
+ "&ks=" +
233
+ this.ks;
234
+ fetch(urlToLoad)
235
+ .then((response) => {
236
+ response
237
+ .text()
238
+ .then((text) => {
239
+ resolve(text);
240
+ })
241
+ .catch((error) => {
242
+ reject();
243
+ });
244
+ })
245
+ .catch((error) => {
246
+ reject();
247
+ });
248
+ });
249
+ }
250
+ }