forked from ThaUnknown/simple-peer
-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add preferred codec support and real browser verification #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import common from './common.js' | ||
| import Peer from '../index.js' | ||
| import { test, expect } from 'vitest' | ||
|
|
||
| function getVideoTransceiver (peer: Peer): RTCRtpTransceiver | null { | ||
| const transceivers = peer._pc!.getTransceivers?.() | ||
| if (!transceivers) return null | ||
| return transceivers.find(transceiver => transceiver.receiver?.track?.kind === 'video') || null | ||
| } | ||
|
|
||
| function getCodecMimeTypeFromReport (report: RTCStatsReport, rtpType: 'outbound-rtp' | 'inbound-rtp'): string | null { | ||
| let rtpReport: RTCStats & { codecId?: string } | undefined | ||
| report.forEach(stat => { | ||
| if (rtpReport) return | ||
| if (stat.type !== rtpType) return | ||
| const mediaType = (stat as any).mediaType || (stat as any).kind | ||
| if (mediaType === 'video') rtpReport = stat as (RTCStats & { codecId?: string }) | ||
| }) | ||
|
|
||
| if (!rtpReport?.codecId) return null | ||
| const codecReport = report.get(rtpReport.codecId) as (RTCStats & { mimeType?: string }) | undefined | ||
| return codecReport?.mimeType?.toLowerCase() ?? null | ||
| } | ||
|
|
||
| function listCodecMimeTypes (report: RTCStatsReport): string[] { | ||
| const codecs: string[] = [] | ||
| report.forEach(stat => { | ||
| if (stat.type !== 'codec') return | ||
| const mime = (stat as RTCStats & { mimeType?: string }).mimeType?.toLowerCase() | ||
| if (mime && !codecs.includes(mime)) codecs.push(mime) | ||
| }) | ||
| return codecs | ||
| } | ||
|
|
||
| async function waitForReceiverVideoCodec (peer: Peer, timeoutMs = 8000): Promise<string> { | ||
| const start = Date.now() | ||
| let lastCodecs: string[] = [] | ||
| while (Date.now() - start < timeoutMs) { | ||
| const transceiver = getVideoTransceiver(peer) | ||
| if (!transceiver?.receiver?.getStats) break | ||
| const report = await transceiver.receiver.getStats() | ||
| const codec = getCodecMimeTypeFromReport(report, 'inbound-rtp') | ||
| if (codec) return codec | ||
| lastCodecs = listCodecMimeTypes(report) | ||
| await new Promise(resolve => setTimeout(resolve, 100)) | ||
| } | ||
| throw new Error(`Timed out waiting for inbound video codec. Seen codecs: ${lastCodecs.join(', ') || 'none'}`) | ||
| } | ||
|
|
||
| async function waitForSenderVideoCodec (peer: Peer, timeoutMs = 8000): Promise<string> { | ||
| const start = Date.now() | ||
| let lastCodecs: string[] = [] | ||
| while (Date.now() - start < timeoutMs) { | ||
| const transceiver = getVideoTransceiver(peer) | ||
| if (!transceiver?.sender?.getStats) break | ||
| const report = await transceiver.sender.getStats() | ||
| const codec = getCodecMimeTypeFromReport(report, 'outbound-rtp') | ||
| if (codec) return codec | ||
| lastCodecs = listCodecMimeTypes(report) | ||
| await new Promise(resolve => setTimeout(resolve, 100)) | ||
| } | ||
| throw new Error(`Timed out waiting for outbound video codec. Seen codecs: ${lastCodecs.join(', ') || 'none'}`) | ||
| } | ||
|
|
||
| async function waitForVideoCodec (peer: Peer): Promise<string> { | ||
| try { | ||
| return await waitForReceiverVideoCodec(peer, 4000) | ||
| } catch { | ||
| return await waitForSenderVideoCodec(peer, 4000) | ||
| } | ||
| } | ||
|
|
||
| async function attachStreamToVideo (stream: MediaStream): Promise<void> { | ||
| const video = document.createElement('video') | ||
| video.muted = true | ||
| video.autoplay = true | ||
| video.playsInline = true | ||
| video.srcObject = stream | ||
| document.body.appendChild(video) | ||
| try { | ||
| await video.play() | ||
| } catch { | ||
| // Ignore autoplay errors; stats should still populate. | ||
| } | ||
| } | ||
|
|
||
| async function getCameraStream (): Promise<MediaStream> { | ||
| if (!navigator.mediaDevices?.getUserMedia) { | ||
| throw new Error('getUserMedia is not available in this browser') | ||
| } | ||
| return await navigator.mediaDevices.getUserMedia({ video: true, audio: false }) | ||
| } | ||
|
|
||
| test('preferredCodecs influences negotiated video codec (getStats)', async function () { | ||
| if (!process.browser) return | ||
| if (common.isBrowser('ios')) return | ||
| // Playwright WebKit does not support starting the webcam | ||
| if (common.isBrowser('safari')) return | ||
| if (typeof RTCRtpTransceiver === 'undefined') return | ||
| if (typeof RTCRtpTransceiver.prototype.setCodecPreferences !== 'function') return | ||
| if (typeof RTCRtpSender === 'undefined' || typeof RTCRtpSender.getCapabilities !== 'function') return | ||
| if (typeof RTCRtpReceiver === 'undefined' || typeof RTCRtpReceiver.getCapabilities !== 'function') return | ||
| const preferred = ['video/vp9'] | ||
|
|
||
| const senderCaps = RTCRtpSender.getCapabilities('video') | ||
| const receiverCaps = RTCRtpReceiver.getCapabilities('video') | ||
| const supportsVp9 = senderCaps?.codecs?.some(codec => codec.mimeType?.toLowerCase() === 'video/vp9') && | ||
| receiverCaps?.codecs?.some(codec => codec.mimeType?.toLowerCase() === 'video/vp9') | ||
| if (!supportsVp9) return | ||
|
|
||
| const [stream1, stream2] = await Promise.all([getCameraStream(), getCameraStream()]) | ||
|
|
||
| const peer1 = new Peer({ | ||
| initiator: true, | ||
| streams: [stream1], | ||
| preferredCodecs: { video: preferred } | ||
| }) | ||
| const peer2 = new Peer({ | ||
| streams: [stream2], | ||
| preferredCodecs: { video: preferred } | ||
| }) | ||
|
|
||
| peer1.on('signal', data => peer2.signal(data)) | ||
| peer2.on('signal', data => peer1.signal(data)) | ||
|
|
||
| await new Promise<void>((resolve) => { | ||
| let streams = 0 | ||
| const onStream = (stream: MediaStream) => { | ||
| void attachStreamToVideo(stream) | ||
| streams++ | ||
| if (streams >= 2) resolve() | ||
| } | ||
| peer1.on('stream', onStream) | ||
| peer2.on('stream', onStream) | ||
| }) | ||
|
|
||
| await new Promise(resolve => setTimeout(resolve, 500)) | ||
|
|
||
| const [codec1, codec2] = await Promise.all([ | ||
| waitForVideoCodec(peer1), | ||
| waitForVideoCodec(peer2) | ||
| ]) | ||
|
|
||
| expect(codec1).toBe('video/vp9') | ||
| expect(codec2).toBe('video/vp9') | ||
|
|
||
| peer1.destroy() | ||
| peer2.destroy() | ||
| stream1.getTracks().forEach(track => track.stop()) | ||
| stream2.getTracks().forEach(track => track.stop()) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.