-
Notifications
You must be signed in to change notification settings - Fork 14
/
player.ts
510 lines (463 loc) · 16.3 KB
/
player.ts
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
import { NativeModules, Platform } from 'react-native';
import NativeInstance from './nativeInstance';
import { Source, SourceConfig } from './source';
import { AudioTrack } from './audioTrack';
import { SubtitleTrack } from './subtitleTrack';
import { OfflineContentManager, OfflineSourceOptions } from './offline';
import { Thumbnail } from './thumbnail';
import { AnalyticsApi } from './analytics/player';
import { PlayerConfig } from './playerConfig';
import { AdItem } from './advertising';
import { BufferApi } from './bufferApi';
import { VideoQuality } from './media';
import { Network } from './network';
const PlayerModule = NativeModules.PlayerModule;
/**
* Loads, controls and renders audio and video content represented through {@link Source}s. A player
* instance can be created via the {@link usePlayer} hook and will idle until one or more {@link Source}s are
* loaded. Once {@link Player.load} or {@link Player.loadSource} is called, the player becomes active and initiates necessary downloads to
* start playback of the loaded source(s).
*
* Can be attached to {@link PlayerView} component in order to use Bitmovin's Player Web UI.
* @see PlayerView
*/
export class Player extends NativeInstance<PlayerConfig> {
private network?: Network;
/**
* Currently active source, or `null` if none is active.
*/
source?: Source;
/**
* Whether the native `Player` object has been created.
*/
isInitialized = false;
/**
* Whether the native `Player` object has been disposed.
*/
isDestroyed = false;
/**
* The `AnalyticsApi` for interactions regarding the `Player`'s analytics.
*
* `undefined` if the player was created without analytics support.
*/
analytics?: AnalyticsApi = undefined;
/**
* The {@link BufferApi} for interactions regarding the buffer.
*/
buffer: BufferApi = new BufferApi(this.nativeId);
/**
* Allocates the native `Player` instance and its resources natively.
*/
initialize = () => {
if (!this.isInitialized) {
if (this.config?.networkConfig) {
this.network = new Network(this.config.networkConfig);
this.network.initialize();
}
const analyticsConfig = this.config?.analyticsConfig;
if (analyticsConfig) {
PlayerModule.initWithAnalyticsConfig(
this.nativeId,
this.config,
this.network?.nativeId,
analyticsConfig
);
this.analytics = new AnalyticsApi(this.nativeId);
} else {
PlayerModule.initWithConfig(
this.nativeId,
this.config,
this.network?.nativeId
);
}
this.isInitialized = true;
}
};
/**
* Destroys the native `Player` and releases all of its allocated resources.
*/
destroy = () => {
if (!this.isDestroyed) {
PlayerModule.destroy(this.nativeId);
this.source?.destroy();
this.network?.destroy();
this.isDestroyed = true;
}
};
/**
* Loads a new {@link Source} from `sourceConfig` into the player.
*/
load = (sourceConfig: SourceConfig) => {
this.loadSource(new Source(sourceConfig));
};
/**
* Loads the downloaded content from {@link OfflineContentManager} into the player.
*/
loadOfflineContent = (
offlineContentManager: OfflineContentManager,
options?: OfflineSourceOptions
) => {
PlayerModule.loadOfflineContent(
this.nativeId,
offlineContentManager.nativeId,
options
);
};
/**
* Loads the given {@link Source} into the player.
*/
loadSource = (source: Source) => {
source.initialize();
this.source = source;
PlayerModule.loadSource(this.nativeId, source.nativeId);
};
/**
* Unloads all {@link Source}s from the player.
*/
unload = () => {
PlayerModule.unload(this.nativeId);
};
/**
* Starts or resumes playback after being paused. Has no effect if the player is already playing.
*/
play = () => {
PlayerModule.play(this.nativeId);
};
/**
* Pauses the video if it is playing. Has no effect if the player is already paused.
*/
pause = () => {
PlayerModule.pause(this.nativeId);
};
/**
* Seeks to the given playback time specified by the parameter `time` in seconds. Must not be
* greater than the total duration of the video. Has no effect when watching a live stream since
* seeking is not possible.
*
* @param time - The time to seek to in seconds.
*/
seek = (time: number) => {
PlayerModule.seek(this.nativeId, time);
};
/**
* Shifts the time to the given `offset` in seconds from the live edge. The resulting offset has to be within the
* timeShift window as specified by `maxTimeShift` (which is a negative value) and 0. When the provided `offset` is
* positive, it will be interpreted as a UNIX timestamp in seconds and converted to fit into the timeShift window.
* When the provided `offset` is negative, but lower than `maxTimeShift`, then it will be clamped to `maxTimeShift`.
* Has no effect for VoD.
*
* Has no effect if no sources are loaded.
*
* @param offset - Target offset from the live edge in seconds.
*/
timeShift = (offset: number) => {
PlayerModule.timeShift(this.nativeId, offset);
};
/**
* Mutes the player if an audio track is available. Has no effect if the player is already muted.
*/
mute = () => {
PlayerModule.mute(this.nativeId);
};
/**
* Unmutes the player if it is muted. Has no effect if the player is already unmuted.
*/
unmute = () => {
PlayerModule.unmute(this.nativeId);
};
/**
* Sets the player's volume between 0 (silent) and 100 (max volume).
*
* @param volume - The volume level to set.
*/
setVolume = (volume: number) => {
PlayerModule.setVolume(this.nativeId, volume);
};
/**
* @returns The player's current volume level.
*/
getVolume = async (): Promise<number> => {
return PlayerModule.getVolume(this.nativeId);
};
/**
* @returns The current playback time in seconds.
*
* For VoD streams the returned time ranges between 0 and the duration of the asset.
*
* For live streams it can be specified if an absolute UNIX timestamp or a value
* relative to the playback start should be returned.
*
* @param mode - The time mode to specify: an absolute UNIX timestamp ('absolute') or relative time ('relative').
*/
getCurrentTime = async (
mode: 'relative' | 'absolute' = 'absolute'
): Promise<number> => {
return PlayerModule.currentTime(this.nativeId, mode);
};
/**
* @returns The total duration in seconds of the current video or INFINITY if it’s a live stream.
*/
getDuration = async (): Promise<number> => {
return PlayerModule.duration(this.nativeId);
};
/**
* @returns `true` if the player is muted.
*/
isMuted = async (): Promise<boolean> => {
return PlayerModule.isMuted(this.nativeId);
};
/**
* @returns `true` if the player is currently playing, i.e. has started and is not paused.
*/
isPlaying = async (): Promise<boolean> => {
return PlayerModule.isPlaying(this.nativeId);
};
/**
* @returns `true` if the player has started playback but it's currently paused.
*/
isPaused = async (): Promise<boolean> => {
return PlayerModule.isPaused(this.nativeId);
};
/**
* @returns `true` if the displayed video is a live stream.
*/
isLive = async (): Promise<boolean> => {
return PlayerModule.isLive(this.nativeId);
};
/**
* @remarks Only available for iOS devices.
* @returns `true` when media is played externally using AirPlay.
*/
isAirPlayActive = async (): Promise<boolean> => {
if (Platform.OS === 'android') {
console.warn(
`[Player ${this.nativeId}] Method isAirPlayActive is not available for Android. Only iOS devices.`
);
return false;
}
return PlayerModule.isAirPlayActive(this.nativeId);
};
/**
* @remarks Only available for iOS devices.
* @returns `true` when AirPlay is available.
*/
isAirPlayAvailable = async (): Promise<boolean> => {
if (Platform.OS === 'android') {
console.warn(
`[Player ${this.nativeId}] Method isAirPlayAvailable is not available for Android. Only iOS devices.`
);
return false;
}
return PlayerModule.isAirPlayAvailable(this.nativeId);
};
/**
* @returns The currently selected audio track or `null`.
*/
getAudioTrack = async (): Promise<AudioTrack | null> => {
return PlayerModule.getAudioTrack(this.nativeId);
};
/**
* @returns An array containing {@link AudioTrack} objects for all available audio tracks.
*/
getAvailableAudioTracks = async (): Promise<AudioTrack[]> => {
return PlayerModule.getAvailableAudioTracks(this.nativeId);
};
/**
* Sets the audio track to the ID specified by trackIdentifier. A list can be retrieved by calling getAvailableAudioTracks.
*
* @param trackIdentifier - The {@link AudioTrack.identifier} to be set.
*/
setAudioTrack = async (trackIdentifier: string): Promise<void> => {
return PlayerModule.setAudioTrack(this.nativeId, trackIdentifier);
};
/**
* @returns The currently selected {@link SubtitleTrack} or `null`.
*/
getSubtitleTrack = async (): Promise<SubtitleTrack | null> => {
return PlayerModule.getSubtitleTrack(this.nativeId);
};
/**
* @returns An array containing SubtitleTrack objects for all available subtitle tracks.
*/
getAvailableSubtitles = async (): Promise<SubtitleTrack[]> => {
return PlayerModule.getAvailableSubtitles(this.nativeId);
};
/**
* Sets the subtitle track to the ID specified by trackIdentifier. A list can be retrieved by calling getAvailableSubtitles.
*
* @param trackIdentifier - The {@link SubtitleTrack.identifier} to be set.
*/
setSubtitleTrack = async (trackIdentifier?: string): Promise<void> => {
return PlayerModule.setSubtitleTrack(this.nativeId, trackIdentifier);
};
/**
* Dynamically schedules the {@link AdItem} for playback.
* Has no effect if there is no active playback session.
*
* @param adItem - Ad to be scheduled for playback.
*
* @platform iOS, Android
*/
scheduleAd = (adItem: AdItem) => {
PlayerModule.scheduleAd(this.nativeId, adItem);
};
/**
* Skips the current ad.
* Has no effect if the current ad is not skippable or if no ad is being played back.
*
* @platform iOS, Android
*/
skipAd = () => {
PlayerModule.skipAd(this.nativeId);
};
/**
* @returns `true` while an ad is being played back or when main content playback has been paused for ad playback.
* @platform iOS, Android
*/
isAd = async (): Promise<boolean> => {
return PlayerModule.isAd(this.nativeId);
};
/**
* The current time shift of the live stream in seconds. This value is always 0 if the active {@link Source} is not a
* live stream or no sources are loaded.
*/
getTimeShift = async (): Promise<number> => {
return PlayerModule.getTimeShift(this.nativeId);
};
/**
* The limit in seconds for time shifting. This value is either negative or 0 and it is always 0 if the active
* {@link Source} is not a live stream or no sources are loaded.
*/
getMaxTimeShift = async (): Promise<number> => {
return PlayerModule.getMaxTimeShift(this.nativeId);
};
/**
* Sets the upper bitrate boundary for video qualities. All qualities with a bitrate
* that is higher than this threshold will not be eligible for automatic quality selection.
*
* Can be set to `null` for no limitation.
*/
setMaxSelectableBitrate = (bitrate: number | null) => {
PlayerModule.setMaxSelectableBitrate(this.nativeId, bitrate || -1);
};
/**
* @returns a {@link Thumbnail} for the specified playback time for the currently active source if available.
* Supported thumbnail formats are:
* - `WebVtt` configured via {@link SourceConfig.thumbnailTrack}, on all supported platforms
* - HLS `Image Media Playlist` in the multivariant playlist, Android-only
* - DASH `Image Adaptation Set` as specified in DASH-IF IOP, Android-only
* If a `WebVtt` thumbnail track is provided, any potential in-manifest thumbnails are ignored on Android.
*
* @param time - The time in seconds for which to retrieve the thumbnail.
*/
getThumbnail = async (time: number): Promise<Thumbnail | null> => {
return PlayerModule.getThumbnail(this.nativeId, time);
};
/**
* Whether casting to a cast-compatible remote device is available. {@link CastAvailableEvent} signals when
* casting becomes available.
*
* @platform iOS, Android
*/
isCastAvailable = async (): Promise<boolean> => {
return PlayerModule.isCastAvailable(this.nativeId);
};
/**
* Whether video is currently being casted to a remote device and not played locally.
*
* @platform iOS, Android
*/
isCasting = async (): Promise<boolean> => {
return PlayerModule.isCasting(this.nativeId);
};
/**
* Initiates casting the current video to a cast-compatible remote device. The user has to choose to which device it
* should be sent.
*
* @platform iOS, Android
*/
castVideo = () => {
PlayerModule.castVideo(this.nativeId);
};
/**
* Stops casting the current video. Has no effect if {@link Player.isCasting} is `false`.
*
* @platform iOS, Android
*/
castStop = () => {
PlayerModule.castStop(this.nativeId);
};
/**
* Returns the currently selected video quality.
* @returns The currently selected video quality.
*/
getVideoQuality = async (): Promise<VideoQuality> => {
return PlayerModule.getVideoQuality(this.nativeId);
};
/**
* Returns an array containing all available video qualities the player can adapt between.
* @returns An array containing all available video qualities the player can adapt between.
*/
getAvailableVideoQualities = async (): Promise<VideoQuality[]> => {
return PlayerModule.getAvailableVideoQualities(this.nativeId);
};
/**
* Sets the video quality.
* @remarks Only available on Android.
* @platform Android
*
* @param qualityId value obtained from {@link VideoQuality}'s `id` property, which can be obtained via `Player.getAvailableVideoQualities()` to select a specific quality. To use automatic quality selection, 'auto' can be passed here.
*/
setVideoQuality = (qualityId: String) => {
if (Platform.OS !== 'android') {
console.warn(
`[Player ${this.nativeId}] Method setVideoQuality is not available for iOS and tvOS devices. Only Android devices.`
);
return;
}
PlayerModule.setVideoQuality(this.nativeId, qualityId);
};
/**
* Sets the playback speed of the player. Fast forward, slow motion and reverse playback are supported.
* @note
* - Slow motion is indicated by values between `0` and `1`.
* - Fast forward by values greater than `1`.
* - Slow reverse is used by values between `0` and `-1`, and fast reverse is used by values less than `-1`. iOS and tvOS only.
* @note
* Negative values are ignored during Casting and on Android.
* @note
* During reverse playback the playback will continue until the beginning of the active source is
* reached. When reaching the beginning of the source, playback will be paused and the playback
* speed will be reset to its default value of `1`. No {@link PlaybackFinishedEvent} will be
* emitted in this case.
*
* @param playbackSpeed - The playback speed to set.
*/
setPlaybackSpeed = (playbackSpeed: number) => {
PlayerModule.setPlaybackSpeed(this.nativeId, playbackSpeed);
};
/**
* @see {@link setPlaybackSpeed} for details on which values playback speed can assume.
* @returns The player's current playback speed.
*/
getPlaybackSpeed = async (): Promise<number> => {
return PlayerModule.getPlaybackSpeed(this.nativeId);
};
/**
* Checks the possibility to play the media at specified playback speed.
* @param playbackSpeed - The playback speed to check.
* @returns `true` if it's possible to play the media at the specified playback speed, otherwise `false`. On Android it always returns `undefined`.
* @platform iOS, tvOS
*/
canPlayAtPlaybackSpeed = async (
playbackSpeed: number
): Promise<boolean | undefined> => {
if (Platform.OS === 'android') {
console.warn(
`[Player ${this.nativeId}] Method canPlayAtPlaybackSpeed is not available for Android. Only iOS and tvOS devices.`
);
return undefined;
}
return PlayerModule.canPlayAtPlaybackSpeed(this.nativeId, playbackSpeed);
};
}