-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcamera.html
More file actions
509 lines (461 loc) · 20 KB
/
camera.html
File metadata and controls
509 lines (461 loc) · 20 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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>哈威SSR团队作品前端展示</title>
<!-- 样式与库 -->
<link href="css/sb-admin-2.min.css" rel="stylesheet">
<link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet">
<link href="vendor/toastr/toastr.min.css" rel="stylesheet">
<link href="https://web.sdk.qcloud.com/player/tcplayer/release/v4.7.1/tcplayer.min.css" rel="stylesheet">
<!-- <script src="https://cdn.jsdelivr.net/npm/rtsp-relay/browser/rtsp-relay.min.js"></script> -->
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
<!-- <script src="vendor/toastr/toastr.min.js"></script> -->
<script src="https://web.sdk.qcloud.com/player/tcplayer/release/v4.7.1/tcplayer.v4.7.1.min.js"></script>
<style>
.camera-container{padding:20px;max-width:800px;margin:0 auto;}
.camera-container video{width:100%;background:#000;}
#modeBtnGroup{position:fixed;bottom:20px;right:20px;z-index:10000;display:flex;gap:10px;}
#modeBtnGroup .btn{padding:1px 6px;min-width:60px;}
.rtsp-controls .btn{pointer-events:auto;cursor:pointer;}
.rtsp-controls .btn:disabled{pointer-events:none;cursor:not-allowed;}
.webrtc-controls .btn{pointer-events:auto;cursor:pointer;}
.webrtc-controls .btn:disabled{pointer-events:none;cursor:not-allowed;}
#webrtcVideoContainer{border-radius:0.35rem;overflow:hidden;}
#webrtcVideo{border-radius:0.35rem;}
</style>
</head>
<body id="page-top">
<!-- Page Wrapper -->
<div id="wrapper">
<!-- Sidebar -->
<ul class="navbar-nav bg-gradient-primary sidebar sidebar-dark accordion" id="accordionSidebar">
<!-- Sidebar Brand -->
<a class="sidebar-brand d-flex align-items-center justify-content-center" href="first_page.html">
<div class="sidebar-brand-icon rotate-n-15">
<i class="fas fa-laugh-wink"></i>
</div>
<div class="sidebar-brand-text mx-3">
<font _mstmutation="1" _msttexthash="10666266" _msthash="1">SSR团队</font>
</div>
</a>
<!-- Divider -->
<hr class="sidebar-divider my-0">
<!-- Nav Item - Dashboard -->
<li class="nav-item">
<a class="nav-link" href="first_page.html">
<i class="fas fa-fw fa-tachometer-alt"></i>
<span _msttexthash="8308430" _msthash="3">首页展示</span></a>
</li>
<hr class="sidebar-divider">
<div class="sidebar-heading" _msttexthash="4554719" _msthash="4">数据图表</div>
<li class="nav-item">
<a class="nav-link" href="text.html">
<i class="fas fa-fw fa-chart-area"></i>
<span _msttexthash="5658250" _msthash="24">图表</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="tables.html">
<i class="fas fa-fw fa-table"></i>
<span _msttexthash="3177720" _msthash="25">表格</span></a>
</li>
<!-- 你可以添加更多导航项 -->
<li class="nav-item">
<a class="nav-link collapsed" href="XBox.html">
<i class="fas fa-fw fa-cog"></i>
<span _msttexthash="5055388" _msthash="5">手柄效果</span>
</a>
</li>
<li class="nav-item active">
<a class="nav-link collapsed" href="camera.html">
<i class="fas fa-fw fa-camera"></i>
<span _msttexthash="5055388" _msthash="5">摄像头读取</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="http://localhost:8001/vendor/three.js-r161/examples/sinmple.html">
<i class="fas fa-fw fa-wrench"></i>
<span>模型展示</span>
</a>
</li>
</ul>
<!-- End of Sidebar -->
<!-- Content Wrapper -->
<div id="content-wrapper" class="d-flex flex-column">
<!-- Main Content -->
<div id="content">
<!-- Topbar -->
<nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">
<!-- Sidebar Toggle (Topbar) -->
<button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
<i class="fa fa-bars"></i>
</button>
<!-- Topbar Navbar -->
<ul class="navbar-nav ml-auto">
<!-- 可添加更多内容 -->
<li class="nav-item dropdown no-arrow">
<a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="mr-2 d-none d-lg-inline text-gray-600 small" _msttexthash="22648327"
_msthash="55">SSR团队研发</span>
<img class="img-profile rounded-circle" src="img/picture.png">
</a>
</li>
</ul>
</nav>
<!-- End of Topbar -->
<div id="content-wrapper" class="d-flex flex-column">
<div id="content">
<!-- ===== 顶栏略 ===== -->
<div class="container-fluid">
<!-- 本地摄像头 -->
<div id="cameraContainer" class="camera-container">
<div class="card shadow mb-4">
<div class="card-header py-3"><h6 class="m-0 font-weight-bold text-primary">本地摄像头</h6></div>
<div class="card-body text-center">
<video id="localVideo" autoplay playsinline muted></video>
<div class="camera-controls mt-4">
<button id="startCamera" class="btn btn-success"><i class="fas fa-video"></i> 开启摄像头</button>
<button id="takePhoto" class="btn btn-primary" disabled><i class="fas fa-camera"></i> 拍照</button>
<button id="switchCamera" class="btn btn-secondary"><i class="fas fa-sync-alt"></i> 切换摄像头</button>
</div>
<canvas id="canvas" width="640" height="480" style="display:none"></canvas>
<img id="photo" style="display:none;max-width:100%">
</div>
</div>
</div>
<!-- RTSP -->
<div id="rtspContainer" class="camera-container" style="display:none">
<div class="card shadow mb-4">
<div class="card-header py-3"><h6 class="m-0 font-weight-bold text-primary">RTSP 实时流</h6></div>
<div class="card-body text-center">
<video id="rtspVideo" controls autoplay muted playsinline></video>
<div class="rtsp-controls mt-4">
<button id="connectRtspBtn" class="btn btn-success mr-2">
<i class="fas fa-play"></i> 连接流
</button>
<button id="disconnectRtspBtn" class="btn btn-danger" disabled>
<i class="fas fa-stop"></i> 断开连接
</button>
<div class="mt-2">
<small class="text-muted">流地址: rtsp://stream.strba.sk:1935/strba/VYHLAD_JAZERO.stream</small>
</div>
</div>
</div>
</div>
</div>
<!-- WebRTC -->
<div id="webrtcContainer" class="camera-container" style="display:none">
<div class="card shadow mb-4">
<div class="card-header py-3"><h6 class="m-0 font-weight-bold text-primary">WebRTC 实时流</h6></div>
<div class="card-body text-center">
<div id="webrtcVideoContainer" style="width:100%;height:450px;background:#000;">
<video id="webrtcVideo" style="width:100%;height:100%;" controls autoplay muted playsinline></video>
</div>
<div class="webrtc-controls mt-4">
<button id="connectWebrtcBtn" class="btn btn-success mr-2">
<i class="fas fa-play"></i> 连接流
</button>
<!-- <button id="disconnectWebrtcBtn" class="btn btn-danger mr-2" disabled>
<i class="fas fa-stop"></i> 断开连接
</button>
<button id="pauseWebrtcBtn" class="btn btn-warning mr-2" disabled>
<i class="fas fa-pause"></i> 暂停
</button>
<button id="playWebrtcBtn" class="btn btn-info" disabled>
<i class="fas fa-play"></i> 播放
</button> -->
<div class="mt-2">
<small class="text-muted">WebRTC流地址: 腾讯云演示流</small>
</div>
</div>
</div>
</div>
</div>
</div><!-- /.container-fluid -->
</div><!-- /#content -->
<footer class="sticky-footer bg-white"><div class="container my-auto"><div class="copyright text-center my-auto">© 2025 哈威SSR团队研发</div></div></footer>
</div><!-- /#content-wrapper -->
</div><!-- /#wrapper -->
<!-- 三模式切换 -->
<div id="modeBtnGroup">
<button id="modeCameraBtn" class="btn btn-primary">本地摄像头</button>
<button id="modeRTSPBtn" class="btn btn-secondary">RTSP 流</button>
<button id="modeWebRTCBtn" class="btn btn-secondary">WebRTC 流</button>
</div>
<!-- 必要脚本 -->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="vendor/jquery-easing/jquery.easing.min.js"></script>
<script src="js/sb-admin-2.min.js"></script>
<script src="js/webrtcstreamer.js"></script>
<script src="js/rtsp-stream.js"></script>
<script src="js/webrtc-stream.js"></script>
<script>
/* ---------- 本地摄像头 ---------- */
let camStream=null,facing='user';
const localVideo=document.getElementById('localVideo');
$('#startCamera').on('click',async()=>{
try{
camStream=await navigator.mediaDevices.getUserMedia({video:{facingMode:facing}});
localVideo.srcObject=camStream;
$('#startCamera').prop('disabled',true);
$('#takePhoto').prop('disabled',false);
}catch(e){toastr.error('无法访问摄像头');}
});
$('#switchCamera').on('click',async()=>{
if(camStream)camStream.getTracks().forEach(t=>t.stop());
facing=facing==='user'?'environment':'user';
try{
camStream=await navigator.mediaDevices.getUserMedia({video:{facingMode:facing}});
localVideo.srcObject=camStream;
}catch{toastr.warning('切换失败');}
});
$('#takePhoto').on('click',()=>{
const c=document.getElementById('canvas'),ctx=c.getContext('2d');
ctx.drawImage(localVideo,0,0,c.width,c.height);
$('#photo').attr('src',c.toDataURL('image/png')).show();
});
function stopCamera(){if(camStream)camStream.getTracks().forEach(t=>t.stop());camStream=null;$('#startCamera').prop('disabled',false);$('#takePhoto').prop('disabled',true);}
/* ---------- RTSP ---------- */
async function startRTSP(){
if(window.rtspManager){
$('#connectRtspBtn').prop('disabled', true);
$('#disconnectRtspBtn').prop('disabled', false);
const success = await window.rtspManager.connectStream('rtspVideo');
if(!success){
// 如果WebRTC连接失败,回退到本地demo视频
const rtspVideo = document.getElementById('rtspVideo');
rtspVideo.src = './demo2.mp4';
rtspVideo.load();
rtspVideo.play();
console.log('RTSP连接失败,使用本地demo视频');
$('#connectRtspBtn').prop('disabled', false);
$('#disconnectRtspBtn').prop('disabled', true);
}
}
}
function stopRTSP(){
if(window.rtspManager){
window.rtspManager.disconnectStream();
}
const rtspVideo = document.getElementById('rtspVideo');
rtspVideo.src = '';
rtspVideo.srcObject = null;
// 重置按钮状态
$('#connectRtspBtn').prop('disabled', false);
$('#disconnectRtspBtn').prop('disabled', true);
}
/* ---------- WebRTC ---------- */
async function startWebRTC(){
if(window.webrtcManager){
$('#connectWebrtcBtn').prop('disabled', true);
$('#disconnectWebrtcBtn').prop('disabled', false);
$('#pauseWebrtcBtn').prop('disabled', false);
$('#playWebrtcBtn').prop('disabled', false);
const success = await window.webrtcManager.connectStream('webrtcVideo');
if(!success){
// 连接失败,回退到本地demo视频
console.log('WebRTC连接失败,使用本地demo视频');
const video = document.getElementById('webrtcVideo');
video.src = './demo2.mp4';
video.load();
video.play();
$('#connectWebrtcBtn').prop('disabled', false);
$('#disconnectWebrtcBtn').prop('disabled', true);
$('#pauseWebrtcBtn').prop('disabled', true);
$('#playWebrtcBtn').prop('disabled', true);
}
}
}
function stopWebRTC(){
if(window.webrtcManager){
window.webrtcManager.disconnectStream();
}
// 清理video元素
const video = document.getElementById('webrtcVideo');
video.src = '';
video.srcObject = null;
// 重置按钮状态
$('#connectWebrtcBtn').prop('disabled', false);
$('#disconnectWebrtcBtn').prop('disabled', true);
$('#pauseWebrtcBtn').prop('disabled', true);
$('#playWebrtcBtn').prop('disabled', true);
}
/* ---------- 模式切换 ---------- */
function setMode(show){
$('#cameraContainer,#rtspContainer,#webrtcContainer').hide();
$('#'+show).show();
$('#modeBtnGroup .btn').removeClass('btn-primary').addClass('btn-secondary');
$('#mode'+(show==='cameraContainer'?'Camera':show==='rtspContainer'?'RTSP':'WebRTC')+'Btn').removeClass('btn-secondary').addClass('btn-primary');
}
$('#modeCameraBtn').on('click',()=>{stopRTSP();stopWebRTC();setMode('cameraContainer');});
$('#modeRTSPBtn').on('click',()=>{
stopCamera();
stopWebRTC();
setMode('rtspContainer');
// 重置RTSP按钮状态,让用户手动控制连接
$('#connectRtspBtn').prop('disabled', false);
$('#disconnectRtspBtn').prop('disabled', true);
});
$('#modeWebRTCBtn').on('click',()=>{
stopCamera();
stopRTSP();
setMode('webrtcContainer');
// 重置WebRTC按钮状态,让用户手动控制连接
$('#connectWebrtcBtn').prop('disabled', false);
$('#disconnectWebrtcBtn').prop('disabled', true);
$('#pauseWebrtcBtn').prop('disabled', true);
$('#playWebrtcBtn').prop('disabled', true);
});
/* 默认本地摄像头 */
$('#modeCameraBtn').click();
/* 初始化流管理器 */
$(document).ready(async function(){
console.log('页面初始化开始');
// 初始化RTSP管理器
if(window.rtspManager){
console.log('RTSP管理器存在,开始初始化');
const rtspSuccess = await window.rtspManager.initialize();
console.log('RTSP管理器初始化结果:', rtspSuccess);
} else {
console.error('RTSP管理器未找到!请检查rtsp-stream.js是否正确加载');
}
// 初始化WebRTC管理器
if(window.webrtcManager){
console.log('WebRTC管理器存在,开始初始化');
const webrtcSuccess = await window.webrtcManager.initialize();
console.log('WebRTC管理器初始化结果:', webrtcSuccess);
} else {
console.error('WebRTC管理器未找到!请检查webrtc-stream.js是否正确加载');
}
// 确保按钮状态正确
$('#connectRtspBtn').prop('disabled', false);
$('#disconnectRtspBtn').prop('disabled', true);
$('#connectWebrtcBtn').prop('disabled', false);
$('#disconnectWebrtcBtn').prop('disabled', true);
$('#pauseWebrtcBtn').prop('disabled', true);
$('#playWebrtcBtn').prop('disabled', true);
console.log('所有按钮状态已重置');
});
/* RTSP控制按钮事件 */
$('#connectRtspBtn').on('click', async function(){
console.log('连接流按钮被点击');
if(!window.rtspManager) {
console.error('RTSP管理器未初始化');
if(typeof toastr !== 'undefined') {
toastr.error('RTSP管理器未初始化,请刷新页面重试');
}
return;
}
$('#connectRtspBtn').prop('disabled', true);
$('#disconnectRtspBtn').prop('disabled', false);
console.log('按钮状态已更新:连接按钮禁用,断开按钮启用');
try {
const success = await window.rtspManager.connectStream('rtspVideo');
console.log('RTSP连接结果:', success);
if(!success){
// 连接失败,回退到本地演示视频
console.log('RTSP连接失败,使用本地演示视频');
const rtspVideo = document.getElementById('rtspVideo');
rtspVideo.src = './demo2.mp4';
rtspVideo.load();
rtspVideo.play();
$('#connectRtspBtn').prop('disabled', false);
$('#disconnectRtspBtn').prop('disabled', true);
console.log('已回退到本地视频,按钮状态已重置');
}
} catch(error) {
console.error('连接RTSP时发生错误:', error);
$('#connectRtspBtn').prop('disabled', false);
$('#disconnectRtspBtn').prop('disabled', true);
if(typeof toastr !== 'undefined') {
toastr.error('连接失败: ' + error.message);
}
}
});
$('#disconnectRtspBtn').on('click', function(){
console.log('断开连接按钮被点击');
stopRTSP();
$('#connectRtspBtn').prop('disabled', false);
$('#disconnectRtspBtn').prop('disabled', true);
console.log('RTSP连接已断开,按钮状态已重置');
});
/* WebRTC控制按钮事件 */
$('#connectWebrtcBtn').on('click', async function(){
console.log('WebRTC连接流按钮被点击');
if(!window.webrtcManager) {
console.error('WebRTC管理器未初始化');
if(typeof toastr !== 'undefined') {
toastr.error('WebRTC管理器未初始化,请刷新页面重试');
}
return;
}
$('#connectWebrtcBtn').prop('disabled', true);
$('#disconnectWebrtcBtn').prop('disabled', false);
$('#pauseWebrtcBtn').prop('disabled', false);
$('#playWebrtcBtn').prop('disabled', false);
console.log('WebRTC按钮状态已更新');
try {
const success = await window.webrtcManager.connectStream('webrtcVideo');
console.log('WebRTC连接结果:', success);
if(!success){
// 连接失败,回退到本地演示视频
console.log('WebRTC连接失败,使用本地演示视频');
const video = document.getElementById('webrtcVideo');
video.src = './demo2.mp4';
video.load();
video.play();
$('#connectWebrtcBtn').prop('disabled', false);
$('#disconnectWebrtcBtn').prop('disabled', true);
$('#pauseWebrtcBtn').prop('disabled', true);
$('#playWebrtcBtn').prop('disabled', true);
console.log('已回退到本地视频,WebRTC按钮状态已重置');
}
} catch(error) {
console.error('连接WebRTC时发生错误:', error);
$('#connectWebrtcBtn').prop('disabled', false);
$('#disconnectWebrtcBtn').prop('disabled', true);
$('#pauseWebrtcBtn').prop('disabled', true);
$('#playWebrtcBtn').prop('disabled', true);
if(typeof toastr !== 'undefined') {
toastr.error('WebRTC连接失败: ' + error.message);
}
}
});
$('#disconnectWebrtcBtn').on('click', function(){
console.log('WebRTC断开连接按钮被点击');
stopWebRTC();
console.log('WebRTC连接已断开,按钮状态已重置');
});
$('#pauseWebrtcBtn').on('click', function(){
console.log('WebRTC暂停按钮被点击');
if(window.webrtcManager){
window.webrtcManager.pause();
if(typeof toastr !== 'undefined') {
toastr.info('WebRTC流已暂停');
}
}
});
$('#playWebrtcBtn').on('click', function(){
console.log('WebRTC播放按钮被点击');
if(window.webrtcManager){
window.webrtcManager.play();
if(typeof toastr !== 'undefined') {
toastr.info('WebRTC流已恢复播放');
}
}
});
/* ---------- demo.mp4 兼容 ---------- */
function playDemo(videoEl){
stopRTSP();stopWebRTC();
const v=document.getElementById(videoEl);
v.src='demo.mp4'; v.load(); v.play();
}
/* 若需要手动测试 demo,可打开下列语句 */
// playDemo('rtspVideo'); // 测 RTSP 区域
// playDemo('webrtcVideo'); // 测 WebRTC 区域
</script>
</body>
</html>