-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcircuit-call-stage.js
More file actions
147 lines (123 loc) · 3.82 KB
/
circuit-call-stage.js
File metadata and controls
147 lines (123 loc) · 3.82 KB
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
const template = document.createElement('template');
template.innerHTML = `
<style>
:host {
display: inline-block;
}
#container {
display: inline-flex;
height: inherit;
width: inherit;
position: relative;
background: black;
}
#remoteVideo {
height: inherit;
width: inherit;
}
#localVideo {
position: absolute;
}
</style>
<div id="container">
<video id="remoteVideo" autoplay playsinline></video>
<video id="localVideo" autoplay playsinline></video>
</div>
`;
export class CircuitCallStage extends HTMLElement {
// Attributes we care about getting values from.
static get observedAttributes() {
return ['overlay'];
}
set test(v) {
console.log('roger', v)
}
get call() {
return this._call;
}
get localVideo() {
return this._localVideoEl;
}
get remoteVideo() {
return this._remoteVideoEl;
}
set call(call) {
this._call = call;
this._render();
}
set _streaming(streaming) {
if (streaming) {
this.setAttribute('streaming', '');
} else {
this.removeAttribute('streaming');
}
}
constructor() {
super();
this._call = null;
this._overlay = 'bottom-right';
this.root = this.attachShadow({ mode: 'open' });
this.root.appendChild(template.content.cloneNode(true));
this._remoteVideoEl = this.root.querySelector('#remoteVideo');
this._localVideoEl = this.root.querySelector('#localVideo');
this._containerEl = this.root.querySelector('#container');
}
_render() {
try {
if (!this._call || !this._call.localStreams) {
this._localVideoEl.srcObject = null;
} else {
this._localVideoEl.srcObject = this._call.localStreams.video || this._call.localStreams.desktop;
}
// Only support single remote stream at this time
if (this._call) {
const remoteStreams = this._call && this._call.participants
.map(p => p.streams && (p.streams.video || p.streams.desktop))
.filter(s => !!s);
if (remoteStreams && remoteStreams.length) {
this._remoteVideoEl.srcObject = remoteStreams[0];
} else {
this._remoteVideoEl.srcObject = null;
}
} else {
this._remoteVideoEl.srcObject = null;
}
// Reflect 'streaming' attribute
this._streaming = !!this._remoteVideoEl.srcObject || !!this._localVideoEl.srcObject;
// Hide local video
if (this._overlay === 'hide') {
this._localVideoEl.srcObject = null;
return;
}
// Show local video in full
if (this._overlay === 'full') {
this._localVideoEl.style.top = 'unset';
this._localVideoEl.style.bottom = 'unset';
this._localVideoEl.style.left = 'unset';
this._localVideoEl.style.right = 'unset';
this._localVideoEl.style.width = 'inherit';
return;
}
// Show local video as positioned overlay
this._localVideoEl.style.width = Math.floor(this._containerEl.offsetWidth / 4) + 'px';
this._localVideoEl.style.top = this._overlay.startsWith('top-') ? '5px' : 'unset';
this._localVideoEl.style.bottom = this._overlay.startsWith('bottom-') ? '5px' : 'unset';
this._localVideoEl.style.left = this._overlay.endsWith('-left') ? '5px' : 'unset';
this._localVideoEl.style.right = this._overlay.endsWith('-right') ? '5px' : 'unset';
} catch (err) {
console.error('Error rendering video streams', err);
throw new Error('Error rendering video streams');
}
}
// Lifecycle hooks
attributeChangedCallback(attrName, oldValue, newValue) {
switch (attrName) {
case 'overlay':
/* Values: bottom-right (default), top-right, bottom-left, top-left, hide, full */
this._overlay = newValue || 'bottom-right';
this._render();
break;
}
}
}
customElements.define('circuit-call-stage', CircuitCallStage);