forked from react-native-webrtc/react-native-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MediaStreamTrack.js
118 lines (96 loc) · 2.75 KB
/
MediaStreamTrack.js
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
'use strict';
import {NativeModules} from 'react-native';
import EventTarget from 'event-target-shim';
import MediaStreamErrorEvent from './MediaStreamErrorEvent';
import type MediaStreamError from './MediaStreamError';
import { deepClone } from './RTCUtil';
const {WebRTCModule} = NativeModules;
const MEDIA_STREAM_TRACK_EVENTS = [
'ended',
'mute',
'unmute',
// see: https://www.w3.org/TR/mediacapture-streams/#constrainable-interface
'overconstrained',
];
type MediaStreamTrackState = "live" | "ended";
class MediaStreamTrack extends EventTarget(MEDIA_STREAM_TRACK_EVENTS) {
_constraints: Object;
_enabled: boolean;
_settings: Object;
id: string;
kind: string;
label: string;
muted: boolean;
// readyState in java: INITIALIZING, LIVE, ENDED, FAILED
readyState: MediaStreamTrackState;
remote: boolean;
onended: ?Function;
onmute: ?Function;
onunmute: ?Function;
overconstrained: ?Function;
constructor(info) {
super();
this._constraints = info.constraints || {};
this._enabled = info.enabled;
this._settings = info.settings || {};
this.id = info.id;
this.kind = info.kind;
this.label = info.label;
this.muted = false;
this.remote = info.remote;
const _readyState = info.readyState.toLowerCase();
this.readyState = (_readyState === "initializing"
|| _readyState === "live") ? "live" : "ended";
}
get enabled(): boolean {
return this._enabled;
}
set enabled(enabled: boolean): void {
if (enabled === this._enabled) {
return;
}
WebRTCModule.mediaStreamTrackSetEnabled(this.id, !this._enabled);
this._enabled = !this._enabled;
this.muted = !this._enabled;
}
stop() {
WebRTCModule.mediaStreamTrackSetEnabled(this.id, false);
this.readyState = 'ended';
// TODO: save some stopped flag?
}
/**
* Private / custom API for switching the cameras on the fly, without the
* need for adding / removing tracks or doing any SDP renegotiation.
*
* This is how the reference application (AppRTCMobile) implements camera
* switching.
*/
_switchCamera() {
if (this.remote) {
throw new Error('Not implemented for remote tracks');
}
if (this.kind !== 'video') {
throw new Error('Only implemented for video tracks');
}
WebRTCModule.mediaStreamTrackSwitchCamera(this.id);
}
applyConstraints() {
throw new Error('Not implemented.');
}
clone() {
throw new Error('Not implemented.');
}
getCapabilities() {
throw new Error('Not implemented.');
}
getConstraints() {
return deepClone(this._constraints);
}
getSettings() {
return deepClone(this._settings);
}
release() {
WebRTCModule.mediaStreamTrackRelease(this.id);
}
}
export default MediaStreamTrack;