XPeer is a WebRTC based package for building Muti-Peer-to-Peer Frontend Application.

Overview

XPeer · GitHub license npm version PRs Welcome

install & usage

npm

npm i -s xpeer

yarn

yarn add xpeer

Data Model

// to be added

项目引入

MDN: RTCPeerConnection

import XPeer, { XPeerInit } from 'xpeer'
const options: XPeerInit = {
    signalServer: string, // 'ws://localhost:8080',
    peerConfig: RTCConfiguration
}
const xPeer = new XPeer(options)

浏览器引入

XPeer ">
>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    <script src="https://raw.githubusercontent.com/vaakian/XPeer/main/bundle/xpeer.bundle.min.js">script>
    <title>XPeertitle>
head>

<body>

body>
<script>
    // 直接使用
    const xPeer = new XPeer({
        signalServer: string, // 'ws://localhost:8080',
        peerConfig: RTCConfiguration
    })
script>

html>

METHODS

MDN: MediaStreamConstraints

// 加入房间
xPeer.join({roomId, nick}).then(localPeer => {
    xPeer.localPeer === localPeer // true
})
// 退出房间
xPeer.leave()
// 共享摄像头
xPeer.shareUser(constrants: MediaStreamConstraints)
// 共享屏幕
xPeer.shareDisplay(constrants: DisplayMediaStreamConstraints)
// 设置静音
xPeer.setMute(kind: 'audio'| 'video', enabled: boolean)

// 通过datachannel发送数据
xPeer.send(message: string)
// 通过datachannel发送二进制数据(类型为ArrayBuffer)
xPeer.sendBinary(binary: ArrayBuffer)

EVENTS

xPeer.on('join', (peer: Peer, type: boolean) => {
    // 在与该peer成功建立rtc连接时触发
    // type为false表示我进入房间时,该peer已经在房间内了。(可以用来区分是否提示有人加入)
})
xPeer.on('leave', (peerInfo: PeerInfo) => {

})
xPeer.on('roomInfo', (users: PeerInfo[]) => {

})
xPeer.on('signal:open', () => {
    
})
xPeer.on('signal:error', () => {

})
xPeer.on('signal:close', () => {

})
xPeer.on('stream:user', (peer: Peer) => {
    
})

xPeer.on('stream:display', (peer: Peer) => {
    
})
xPeer.on('streamStop:display', (peer: Peer)=> {
    
})


// datachannel收到文本数据
xPeer.on('message', (peer: Peer, message: string) => {
    
})
// datachannel收到二进制数据
xPeer.on('binary', (peer: Peer, binary: ArrayBuffer) => {

})

Draft

  • 断连,websocket心跳.

  • addTrack -> addEvent -> setRemote 顺序

  • 多人连接应该是多个PeerConnection

  • coturn

  • 先进入房间:negotiate,再分享新的源renegotiate

In short, in order to add video or audio to an existing connection, you need to renegotiate the connection every time you make a media change. Basically you register a listener:

pc.onnegotiationneeded

这个重新negotiate和初次建立的区别差不多,初次建立也会触发negotiationneeded事件。
在处理方式唯一不同的地方是:在接收offer时,需要区分是否已有PeerConnection,如果有,则不需要重新建立,只需要更新offer(CreateOffer)。

TODO: 前端

[]断连就退出界面,提示断连。

[]切换摄像头:先退出界面,再重新进入界面,或者重新nogitate

[]声音mute和unmute

[]屏幕共享:取消事件

https://www.w3.org/TR/webrtc/#dfn-update-the-negotiation-needed-flag

TODO: XPeer

tell screen and video

https://www.kevinmoreland.com/articles/03-22-20-webrtc-mediastream-tracks

datachannel一旦建立之后,再重新设置local和remote都没关系。 独立的sdp-信息可以在sdp中存储。 同理,track一旦接收到之后,再重新设置local和remote也没关系。 独立的sdp-信息可以在sdp中存储。

前端部分:ios自动播放问题¿

推流端:创建dc/推流(打上tag,存下来) -> 触发negotiationneeded(从本地读tag,放到sdp中) -> creaeOffer & setLocal & send -> receiveAnswer & setLocal -> icecandidate -> pc.ontrack/dc.onmessage 接收端:receiveOffer -> setRemote -> createAnswer & setLocal -> pc.ontrack / dc.onmessage

pc.ontrack: 读tag区分类别,并分别存储。

一次negotiation(offer-answer)可以推多个流:包括多个datachannel/多个track。 所以打tag,需要在sdp中一次打完。

单独推流无法匹配上ID,但此时只需要匹配display字段存在,而无需匹配id。

需要匹配trackId的情况只有一次发送多个track时。

每当negotiation完毕后,再createDatachannel/addTrack,都需要重新negotiation。

You might also like...

A utility package for Discord Bots!

A utility package for Discord Bots!

Nuggies A utility package for Discord Bots! For errors and questions you can join our support server Installation Table of content: - installation - d

Dec 17, 2022

An efficient package, which syncs ratelimits of your Discord Bot on all Machines/Clusters.

An efficient package, which syncs ratelimits of your Discord Bot on all Machines/Clusters.

Discord-cross-ratelimit An efficient package, which syncs ratelimits of your Discord Bot on all Machines/Clusters. Why? When Sharding/Scaling your Bot

Oct 23, 2022

A package to use Discord as a (horrible) database

DisDB A package to use Discord as a (horrible) database Features: A simple key value database Entirely hosted in Discord Horrible performance Dirt poo

Apr 12, 2022

This package will generate n numbers of thumbnails at different position in a given video file.

This package will generate n numbers of thumbnails at different position in a given video file.

About Generate n numbers of Image thumbnails of a video file. !Live Demo code sandbox example video-thumbnail-generate.mp4 This package can be used wi

Dec 20, 2022

Whatscode.js is a package to create Whatsapp bots easily and quickly

whatscode.js is a package to create Whatsapp bots easily and quickly, even coding experience is not needed...

Dec 30, 2022

A Simple Music Bot Made Using Discord-Player Package - (Beta)

A Simple Music Bot Made Using Discord-Player Package - (Beta)

Our Music Bot A Highly Powerful Music Bot Without Lavalink Which Is Gonna Have Setup, A Specific Text Channel For Bot Command Feature, And DJ System S

Dec 14, 2022

🧰 DiscordKit is a package that allows you to easily create a Discord bot.

DiscordKit Developed with ❤️ by Swôth What's DiscordKit? DiscordKit is a package that allows you to ✨ easily ✨ create a Discord bot. Installation Deta

Oct 29, 2022

A Discord bot project made with the npm package discord.js version 14

A Discord bot project made with the npm package discord.js version 14

A Discord bot project made with the npm package discord.js version 14 and it's job to manage mails on a server, and this project includes only one Database: Quick.db. This project also handles Slash commands.

Jan 6, 2023

A utility package for making discord-bot commands much easier to write with discord.js.

Cordcommand About A utility package for making discord-bot commands much easier to write with discord.js. Usage Example // initiate discord.js client

Sep 15, 2022
Comments
  • track事件笔记

    track事件笔记

    https://github.com/vaakian/XPeer/blob/0eea244fcdf48f5b05ba1b726ec0c96b45a2a3d2/src/index.ts#L123

    在通过修改sdp来辨别track的类型时,需要注意以下情况

    首次协商,推多个track(video/audio)

    次多个track仅在首次协商发生,此时trackId双端保持一致。

    类型匹配:不能单匹配display/user字段,因为同一个sdp中有多个tag被打上。此时需要类型/ID来匹配。

    后期变动产生新的协商,推单个track

    后期track有任何变化(添加、移除),都是单个track,且双端trackId不一致。

    类型匹配:只匹配display/user字段,不匹配id。

    MDN Official notes:

    Note: Adding a track to a connection triggers renegotiation by firing a negotiationneeded event. See Starting negotiation in Signaling and video calling for details.

    相关: How to handle removing and re-adding remote streams/tracks - possible ID collisions?

    opened by vaakian 3
Owner
Bio
null
WebRTC + WebXR Remote Desktop

WebRTC + WebXR RDP WebRTCとWebXRを使ったブラウザ上で動くリモートデスクトップです.WebXRではない通常表示も可能です. 最近の Chrome や Edge で動くはずです.VRモードは Oculus Quest 2 の Oculus Browser で動作確認していま

Kosuke Kawahira 28 Dec 27, 2022
SRS Player is a video streaming player, supports HLS/HTTP-FLV/WebRTC etc.

WordPress-Plugin-SrsPlayer SRS Player is a video streaming player, supports HLS/HTTP-FLV/WebRTC etc. Usage First, you should get your own video stream

ossrs 3 Jul 27, 2022
High-quality WebRTC SDK

腾讯云实时音视频 TRTC SDK 产品介绍 腾讯实时音视频(Tencent Real-Time Communication,TRTC),将腾讯多年来在网络与音视频技术上的深度积累,以多人音视频通话和低延时互动直播两大场景化方案,通过腾讯云服务向开发者开放,致力于帮助开发者快速搭建低成本、低延时、高

LiteAVSDK 58 Dec 18, 2022
SRS Player is a video streaming player, supports HLS/HTTP-FLV/WebRTC etc.

SRS Player is a video streaming player, supports HLS/HTTP-FLV/WebRTC etc.

ossrs 12 Oct 15, 2022
base on webrtc datachannel & ffmpeg wasm H265 player

1 简介 借助于WebAssembly(简称Wasm)技术,实现在浏览器端调用ffmpeg接口完成H.265码流到YUV数据的解码。 总体流程如下: 2 依赖 2.1 WebAssembly (Wasm) 按照官网的定义,WebAssembly (wasm) 是一个可移植、体积小、加载快并且兼容 W

null 66 Jan 1, 2023
A NodeJS package for voice channel interactions on Revolt. This package lets you join voice channels, play music and more!

Revoice.js - A Voice Module for Revolt This package is still in developement and lacks many features. You still are able to play sound to a voice chan

ShadowLp174 13 Dec 25, 2022
Frontend for a social audio & video app

LɪᴛᴛʟᴇAᴘᴇ littleape is a frontend for the free social audio & video social-media platform greatape. littleape, via greatape, is a Fediverse technology

Charles Iliya Krempeaux 2 Sep 7, 2022
Primitives for building user-friendly controls with SolidJS.

solid-knobs This library provides a set of utilities for building user-friendly controls for audio parameters and the like. The utilities come without

tahti 19 Jan 4, 2023
A simple package to download music tracks from spotify 🎵

A simple package to download music tracks from spotify ??

Alen Yohannan 26 Dec 25, 2022
An simple package to create an Activity in Discord Voice Channel using Discord.js

discordjs-activity An simple package to create an Activity in Discord Voice Channel using Discord.js ?? | Installation npm install discordjs-activity

Sudhan 55 Nov 15, 2022