Skip to content

Commit

Permalink
test: video
Browse files Browse the repository at this point in the history
  • Loading branch information
jiahui92 committed Sep 1, 2024
1 parent 40bea66 commit 6894bcb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
19 changes: 14 additions & 5 deletions web-rtc/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import './index.less';
import React from 'react';
import ReactDOM from 'react-dom';
import { startConnection, sendFile } from './web-rtc.ts';
import { startConnection, sendFile, startVideo } from './web-rtc.ts';

// class View extends React.Component {
// render() {
Expand All @@ -16,11 +16,20 @@ import { startConnection, sendFile } from './web-rtc.ts';
function Page () {
return (
<div>
<input type="file" id="fileInput" />
<button onClick={startConnection}>Start Connection</button>
<button onClick={sendFile}>Send File</button>
<div>
<div id="qrcode"></div>
<button onClick={startConnection}>Start Connection</button>
</div>
<div>
<input type="file" id="fileInput" />
<button onClick={sendFile}>Send File</button>
</div>
<div>
<video id='videoA' />
<video id='videoB' />
<button onClick={startVideo}>start video</button>
</div>
<pre id="log"></pre>
<div id="qrcode"></div>
</div>
)
}
Expand Down
28 changes: 26 additions & 2 deletions web-rtc/src/web-rtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ let sendChannel;
let receiveChannel;
let offer;
let answer;
let readSliceTimer;

const urlParams = new URLSearchParams(location.search)
const shareId = urlParams.get('shareId');
Expand Down Expand Up @@ -159,7 +160,7 @@ export function sendFile() {
fileReader.readAsArrayBuffer(slice);
} else {
console.log('DataChannel buffer is full, waiting to send more data');
setTimeout(() => readSlice(), 0);
readSliceTimer = setTimeout(() => readSlice(), 0);
}
};

Expand All @@ -173,10 +174,14 @@ export function handleDataChannel() {
sendChannel.onopen = () => {
logMessage('Data channel is open')
};
sendChannel.onclose = () => logMessage('Data channel is closed');
sendChannel.onclose = () => {
logMessage('Data channel is closed');
clearTimeout(readSliceTimer)
}

sendChannel.onerror = (event) => {
logMessage(`Data channel error: ${event.error.message}`)
clearTimeout(readSliceTimer)
}

// 处理接收端的数据通道
Expand Down Expand Up @@ -236,3 +241,22 @@ function downloadFile(fileData: File, contentBuffer: ArrayBuffer[]) {
// 释放 URL 对象
URL.revokeObjectURL(url);
}

export async function startVideo() {
if (!navigator.mediaDevices?.getUserMedia) {
logMessage('not support getUserMedia')
return
}

try {
const videoStream = await navigator.mediaDevices.getUserMedia({ audio: true, video: true })
const videoA = document.querySelector('#videoA');
videoA.srcObject = videoStream;
localConnection.onaddStream = (event) => {
const videoB = document.querySelector('#videoB');
videoB.srcObject = event.stram;
}
} catch (error) {
logMessage(error.message)
}
}

0 comments on commit 6894bcb

Please sign in to comment.