forked from react-native-webrtc/react-native-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MediaStream.js
76 lines (58 loc) · 1.66 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
'use strict';
const EventTarget = require('event-target-shim');
const WebRTCModule = require('react-native').NativeModules.WebRTCModule;
const MediaStreamTrackEvent = require('./MediaStreamTrackEvent');
import type MediaStreamTrack from './MediaStreamTrack';
const MEDIA_STREAM_EVENTS = [
'active',
'inactive',
'addtrack',
'removetrack',
];
class MediaStream extends EventTarget(MEDIA_STREAM_EVENTS) {
id: number; // NOTE: spec wants string here
active: boolean = true;
onactive: ?Function;
oninactive: ?Function;
onaddtrack: ?Function;
onremovetrack: ?Function;
_tracks: Array<MediaStreamTrack> = [];
constructor(streamId) {
super();
this.id = streamId;
}
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;
}
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.id;
}
release() {
WebRTCModule.mediaStreamRelease(this.id);
}
}
module.exports = MediaStream;