forked from react-native-webrtc/react-native-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MediaStream.js
91 lines (73 loc) · 2.46 KB
/
MediaStream.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
'use strict';
import {NativeModules} from 'react-native';
import EventTarget from 'event-target-shim';
import MediaStreamTrackEvent from './MediaStreamTrackEvent';
import type MediaStreamTrack from './MediaStreamTrack';
const {WebRTCModule} = NativeModules;
const MEDIA_STREAM_EVENTS = [
'active',
'inactive',
'addtrack',
'removetrack',
];
export default class MediaStream extends EventTarget(MEDIA_STREAM_EVENTS) {
id: string;
active: boolean = true;
onactive: ?Function;
oninactive: ?Function;
onaddtrack: ?Function;
onremovetrack: ?Function;
_tracks: Array<MediaStreamTrack> = [];
/**
* The identifier of this MediaStream unique within the associated
* WebRTCModule instance. As the id of a remote MediaStream instance is unique
* only within the associated RTCPeerConnection, it is not sufficiently unique
* to identify this MediaStream across multiple RTCPeerConnections and to
* unambiguously differentiate it from a local MediaStream instance not added
* to an RTCPeerConnection.
*/
reactTag: string;
constructor(id, reactTag) {
super();
this.id = id;
// Local MediaStreams are created by WebRTCModule to have their id and
// reactTag equal because WebRTCModule follows the respective standard's
// recommendation for id generation i.e. uses UUID which is unique enough
// for the purposes of reactTag.
this.reactTag = (typeof reactTag === 'undefined') ? id : reactTag;
}
addTrack(track: MediaStreamTrack) {
this._tracks.push(track);
this.dispatchEvent(new MediaStreamTrackEvent('addtrack', {track}));
}
removeTrack(track: MediaStreamTrack) {
let index = this._tracks.indexOf(track);
if (index === -1) {
return;
}
WebRTCModule.mediaStreamTrackRelease(this.reactTag, track.id);
this._tracks.splice(index, 1);
this.dispatchEvent(new MediaStreamTrackEvent('removetrack', {track}));
}
getTracks(): Array<MediaStreamTrack> {
return this._tracks.slice();
}
getTrackById(trackId): ?MediaStreamTrack {
return this._tracks.find(track => track.id === trackId);
}
getAudioTracks(): Array<MediaStreamTrack> {
return this._tracks.filter(track => track.kind === 'audio');
}
getVideoTracks(): Array<MediaStreamTrack> {
return this._tracks.filter(track => track.kind === 'video');
}
clone() {
throw new Error('Not implemented.');
}
toURL() {
return this.reactTag;
}
release() {
WebRTCModule.mediaStreamRelease(this.reactTag);
}
}