forked from react-native-webrtc/react-native-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MediaStreamTrack.js
126 lines (106 loc) · 2.92 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
119
120
121
122
123
124
125
126
'use strict';
import {NativeModules} from 'react-native';
import EventTarget from 'event-target-shim';
import MediaStreamErrorEvent from './MediaStreamErrorEvent';
import type MediaStreamError from './MediaStreamError';
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";
type SourceInfo = {
id: string;
label: string;
facing: string;
kind: string;
};
export default class MediaStreamTrack extends EventTarget(MEDIA_STREAM_TRACK_EVENTS) {
static getSources(success: (sources: Array<SourceInfo>) => void) {
const promise = WebRTCModule.mediaStreamTrackGetSources();
if (success) {
return promise.then(success);
}
return promise;
}
_enabled: boolean;
id: string;
kind: string;
label: string;
muted: boolean;
readonly: boolean; // how to decide?
// readyState in java: INITIALIZING, LIVE, ENDED, FAILED
readyState: MediaStreamTrackState;
remote: boolean;
onended: ?Function;
onmute: ?Function;
onunmute: ?Function;
overconstrained: ?Function;
constructor(info) {
super();
let _readyState = info.readyState.toLowerCase();
this._enabled = info.enabled;
this.id = info.id;
this.kind = info.kind;
this.label = info.label;
this.muted = false;
this.readonly = true; // how to decide?
this.remote = info.remote;
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() {
if (this.remote) {
return;
}
WebRTCModule.mediaStreamTrackStop(this.id);
this._enabled = false;
this.readyState = 'ended';
this.muted = !this._enabled;
}
/**
* 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() {
throw new Error('Not implemented.');
}
getSettings() {
throw new Error('Not implemented.');
}
}