WebRTC IPv6 P2P Connection(视频流通过P2P点对点传输)

在视频播放时自动建立WebRTC连接以实现P2P传输

目前为 2025-03-11 提交的版本。查看 最新版本

// ==UserScript==
// @name         WebRTC IPv6 P2P Connection(视频流通过P2P点对点传输)
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  在视频播放时自动建立WebRTC连接以实现P2P传输
// @author       KiwiFruit
// @match        *://*/*
// @grant        none
// @license MIT
// ==/UserScript==
(function() {
    'use strict';

    // 初始化RTCPeerConnection配置
    const configuration = {
        iceServers: [], // 不使用任何STUN/TURN服务器
        iceTransportPolicy: "all" // 允许所有类型的传输,包括IPv6
    };

    function createPeerConnections(videoElement) {
        const localPeerConnection = new RTCPeerConnection(configuration);
        const remotePeerConnection = new RTCPeerConnection(configuration);

        // 监听ICE候选事件
        localPeerConnection.onicecandidate = event => {
            if (event.candidate) {
                console.log('Local ICE candidate:', event.candidate.address);
                remotePeerConnection.addIceCandidate(event.candidate)
                    .catch(error => console.error('Error adding received ICE candidate', error));
            }
        };

        remotePeerConnection.onicecandidate = event => {
            if (event.candidate) {
                console.log('Remote ICE candidate:', event.candidate.address);
                localPeerConnection.addIceCandidate(event.candidate)
                    .catch(error => console.error('Error adding received ICE candidate', error));
            }
        };

        // 处理远程视频流
        remotePeerConnection.ontrack = event => {
            if (videoElement.srcObject !== event.streams[0]) {
                videoElement.srcObject = event.streams[0];
            }
        };

        return { localPeerConnection, remotePeerConnection };
    }

    // 创建和交换SDP offer/answer
    async function createOffer(localPeerConnection, remotePeerConnection) {
        try {
            const offer = await localPeerConnection.createOffer();
            await localPeerConnection.setLocalDescription(offer);
            await remotePeerConnection.setRemoteDescription(offer);
            const answer = await remotePeerConnection.createAnswer();
            await remotePeerConnection.setLocalDescription(answer);
            await localPeerConnection.setRemoteDescription(answer);
        } catch (error) {
            console.error('Error during SDP exchange', error);
        }
    }

    // 并行缓存加载逻辑
    function setupBuffering(videoElement) {
        const threshold = 90; // 缓冲阈值(秒)
        setInterval(() => {
            if (!videoElement.buffered.length) return; // 如果没有缓冲区数据,直接返回
            const bufferedEnd = videoElement.buffered.end(videoElement.buffered.length - 1);
            const currentTime = videoElement.currentTime;
            const bufferLength = bufferedEnd - currentTime;
            if (bufferLength < threshold) {
                console.log('Buffer is low, fetching more data...');
                // 这里可以添加更多逻辑,例如预加载更多片段或优化下载策略
            } else {
                console.log('Buffer is sufficient.');
            }
        }, 5000); // 每5秒检查一次缓冲状态
    }

    // 当视频开始播放时启动WebRTC连接
    function onVideoPlay(videoElement) {
        const { localPeerConnection, remotePeerConnection } = createPeerConnections(videoElement);
        createOffer(localPeerConnection, remotePeerConnection);
        setupBuffering(videoElement);
    }

    // 检测页面中是否存在视频元素,并为其添加监听器
    document.addEventListener('play', function(e) {
        if (e.target.tagName === 'VIDEO') {
            console.log('Detected video element start playing.');
            onVideoPlay(e.target);
        }
    }, true);
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址