-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplayer.html
322 lines (288 loc) · 14.2 KB
/
player.html
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<!doctype html>
<!--
Copyright 2023 Ceeblue B.V.
This file is part of https://github.com/CeeblueTV/webrtc-client which is released under GNU Affero General Public License.
See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
-->
<html lang="en">
<head>
<title>Ceeblue WebRTC Player Example</title>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<link rel="icon" href="./assets/ceeblue-logo-32x32.png" sizes="32x32">
<link rel="icon" href="./assets/ceeblue-logo-192x192.png" sizes="192x192">
<link href="./css/bootstrap.css" rel="stylesheet">
<link href="./css/solid.css" rel="stylesheet">
<link href="./css/fontawesome.css" rel="stylesheet">
<style>
body {
margin-top: 1em;
margin-bottom: 3em;
}
video {
background-color: #333333;
max-width: 100%;
max-height: 60vh;
}
.nav .nav-item .nav-link {
padding: .5rem .8rem;
}
[v-cloak] {
display: none;
}
</style>
</head>
<body class="container">
<div class="row">
<div class="col-md-12">
<img alt="CEEBLUE Media Services" class="rounded mx-auto d-block" src="./assets/ceeblue-logo.png">
<h3 class="text-center">WebRTC Client Player Example</h3>
</div>
<div class="col-md-12" id="main">
<div class="card text-center border-0">
<div class="card-body">
<div class="alert alert-warning" role="alert" v-if="streamErrorMessage">
{{ streamErrorMessage }}
</div>
<video autoplay id="video" ref="video" playsinline></video>
</div>
</div>
<div class="form-row">
<div class="form-group col-sm">
<select id="video-track-selection" class="custom-select text-center"
:disabled="!player || !player.controller" v-model="videoTrackId" @change="onVideoTrackChange()">
<option :value="undefined" :key="videoTrack">AUTO - {{videoTrack}}</option>
<option v-for="[idx, track] of videoTracks" v-bind:value="idx">
{{ track }}
</option>
</select>
</div>
<div class="form-group col-sm">
<select id="audio-track-selection" class="custom-select text-center"
:disabled="!player || !player.controller" v-model="audioTrackId" @change="onAudioTrackChange()">
<option :value="undefined" :key="audioTrack">AUTO - {{audioTrack}}</option>
<option v-for="[idx, track] of audioTracks" v-bind:value="idx">
{{ track }}
</option>
</select>
</div>
</div>
<div class="form-row">
<div class="form-group col-2 col-sm-1">
<button class="btn btn-light" type="button" v-on:click="mute">
<i class="fas" v-bind:class="{ 'fa-volume-up': !muted, 'fa-volume-off': muted }"></i>
</button>
</div>
<div class="form-group col-10 col-sm">
<select class="custom-select"
:disabled="isPlaying()" v-model="connectorType">
<option v-bind:value="option" v-for="option in connectorTypes">
{{ option }}
</option>
</select>
</div>
<div class="form-group col-sm">
<input :disabled="isPlaying()" class="form-control"
placeholder="Stream name as+30189afc-541d-49b7-9ae0-468d15f62059" type="text"
v-model="streamName">
</div>
<div class="form-group col-sm">
<input :disabled="isPlaying()" class="form-control"
placeholder="Access token cc0f52bb-92bb-462b-8218-88c3febe8533" type="text" v-model="accessToken">
</div>
</div>
<div class="d-flex justify-content-center" v-cloak>
<button class="btn" type="button"
v-bind:class="{ 'btn-danger': isPlaying(), 'btn-success': isStopped(), 'btn-secondary': isStarting() }"
v-on:click="play">
<span>
{{ playButtonCaption() }} <i class="fas fa-spinner fa-pulse" v-if="isStarting()"></i>
</span>
</button>
</div>
</div>
</div>
<script type="module">
import { Player, HTTPConnector, WSController, VERSION, utils } from "../dist/webrtc-client.min.js";
// development version, includes helpful console warnings
// import { createApp } from 'https://cdn.jsdelivr.net/npm/vue@3/dist/vue.esm-browser.js';
// production version, optimized for size and speed
import { createApp } from 'https://cdn.jsdelivr.net/npm/vue@3/dist/vue.esm-browser.prod.js';
const { Util, NetAddress, log } = utils;
log('webrtc-client version:', VERSION).info();
log('web-utils version:', utils.VERSION).info();
const PlayState = {
PLAYING: 'PLAYING',
STARTING: 'STARTING',
STOPPED: 'STOPPED'
};
createApp({
data() {
return {
PlayState: PlayState,
playState: PlayState.STOPPED,
streamName: '',
accessToken: null,
muted: true,
host: null,
streamMetadata: null,
videoTracks: new Map(),
videoTrack: 'Video',
videoTrackId: undefined,
connectorType:'WebSocket (WS)',
connectorTypes:['WebSocket (WS)', 'WHEP (HTTP)'],
audioTracks: new Map(),
audioTrack: 'Audio',
audioTrackId: undefined,
streamErrorMessage: null,
player: null
}
},
created() {
const options = Util.options();
// init values
const host = options.host;
this.host = new NetAddress(host || location.host, 443);
this.streamName = options.stream;
this.accessToken = options.accessToken;
this.connectorType = options.whep || (host && host.toLowerCase().startsWith('http')) ? this.connectorTypes[1] : this.connectorTypes[0];
},
methods: {
play() {
if (this.player) {
this.player.stop();
return;
}
this.streamErrorMessage = null;
if (!this.streamName && !this.host.toString()) {
this.streamErrorMessage = "Enter a non-empty stream name to play";
return;
}
this.playState = PlayState.STARTING;
const videoElement = this.$refs.video;
videoElement.muted = this.muted;
videoElement.autoplay = true;
videoElement.onloadeddata = () => {
log('WebRTC loadeddata').info();
videoElement.play();
};
this.player = new Player(this.connectorType.startsWith('WebSocket') ? WSController : HTTPConnector);
this.player.onStart = this.onStart.bind(this);
this.player.onStop = this.onStop.bind(this);
this.player.onPlaying = this.onPlaying.bind(this);
this.player.onMetadata = this.onMetadata.bind(this);
this.player.start(
{
endPoint:this.host.toString(),
streamName: this.streamName,
accessToken: this.accessToken,
iceServer: {
urls: ['turn:' + this.host.domain + ':3478?transport=tcp', 'turn:' + this.host.domain + ':3478'],
username: 'ceeblue', credential: 'ceeblue'
}
});
},
onStart(stream) {
log('onStart').info();
this.playState = PlayState.PLAYING;
this.$refs.video.srcObject = stream;
},
onStop(error) {
if(error) {
log('onStop', error).error();
this.streamErrorMessage = error.name;
} else {
log('onStop').info();
}
const videoElement = this.$refs.video;
videoElement.srcObject = null;
videoElement.onloadeddata = null;
this.audioTracks = new Map();
this.audioTrack = 'Audio';
this.audioTrackId = undefined;
this.videoTracks = new Map();
this.videoTrack = 'Video';
this.videoTrackId = undefined;
this.player = null;
this.playState = PlayState.STOPPED;
},
onPlaying(playing) {
if(this.videoTrackId != null) {
this.videoTrackId = this.player.videoTrack;
}
this.videoTrack = this.videoTracks.get(this.player.videoTrack);
if(this.audioTrackId != null) {
this.audioTrackId = this.player.audioTrack;
}
this.audioTrack = this.audioTracks.get(this.player.audioTrack);
log(
'Position ' + playing.begin + '>>[' + playing.current + ']>>' + playing.end,
{
Buff:(playing.end - playing.current) + 'ms',
audioTrack: this.audioTrackId,
videoTrack: this.videoTrackId
}
).debug();
},
onMetadata(metadata) {
log(`onMetadata ${Util.stringify(metadata)}`).info();
this.audioTracks = new Map();
this.videoTracks = new Map();
for (const [id, track] of metadata.tracks) {
const readableBitrate = (bytes) => {
if (bytes === 0)
return '';
let i = Math.floor(Math.log(bytes) / Math.log(1000)),
sizes = ['bps', 'kbps', 'mbps', 'gbps'];
return (bytes / Math.pow(1000, i)).toFixed(1) * 1 + ' ' + sizes[i];
}
if (track.type === 'video') {
this.videoTracks.set(
id,
`${track.codec} ${track.width}x${track.height} ${readableBitrate(track.bps * 8)}`
);
} else if (track.type === 'audio') {
this.audioTracks.set(
id,
`${track.codec} ${track.channels}ch ${track.rate}Hz ${readableBitrate(track.bps * 8)}`
);
}
}
},
mute() {
this.muted = !this.muted;
log('MutePlayback').info();
this.$refs.video.muted = this.muted;
},
playButtonCaption() {
if (this.isStopped())
return 'Play';
if (this.isStarting())
return 'Starting... ';
if (this.isPlaying())
return 'Stop';
},
isPlaying() {
return this.playState === PlayState.PLAYING;
},
isStarting() {
return this.playState === PlayState.STARTING;
},
isStopped() {
return this.playState === PlayState.STOPPED;
},
onVideoTrackChange() {
if (this.player) {
this.player.videoTrack = this.videoTrackId;
}
},
onAudioTrackChange() {
if (this.player) {
this.player.audioTrack = this.audioTrackId;
}
}
},
}).mount('#main');
</script>
</body>
</html>