pion最简单webrtc例子

pion最简单webrtc例子,第1张

前言

pion是一个纯用golang写的开源webrtc项目,目前github上大部分webrtc开源都是使用c++写的。本人认为pion对于一个webrtc入门者来说golang的语法相对简单,你这样可以更好的关注webrtc协议。如果业务场景不是特别高,业务上只做sfu转发服务器的话,可以尝试用golang来做。
读这篇文章你需要稍微了解webrtc的协议。

例子

pion也有提供不少的例子,我这边写了一个我自认为最简单,只能接受一个流的例子。这个例子平铺直叙,走了一个完整的webrtc流程,需要配合前端https://jsfiddle.net/1jc4go7v/当然需要注意的是前端只有publish能用。

整体下来就是:

创建peerconnection,设置stun服务器设置对端的sdp创建自己协商的answer,设置answer等待candidate收集完成,输出序列化后的sdp信息

这边就是一个最简单的接受流的例子。


package main

import (
   "encoding/base64"
   "encoding/json"
   "fmt"
   "github.com/pion/webrtc/v3"
)

func main() {

   //创建pc, 并且指定stun服务器
   pc, err := webrtc.NewPeerConnection(webrtc.Configuration{
      ICEServers: []webrtc.ICEServer{
         {
            URLs: []string{"stun:stun.l.google.com:19302"},
         },
      },
   })

   if err != nil {
      panic(err.Error())
   }

   sdp := webrtc.SessionDescription{}
   //todo:这边需要你自己设置对端的sdp信息
   sdpStr := "eyJ0eXBlIjoib2ZmZXIiLCJzZHAiOiJ2PTBcclxubz0tIDE2OTQ4Nzk1NDc5OTk0ODE3MDYgMiBJTiBJUDQgMTI3LjAuMC4xXHJcbnM9LVxyXG50PTAgMFxyXG5hPWdyb3VwOkJVTkRMRSAwXHJcbmE9ZXh0bWFwLWFsbG93LW1peGVkXHJcbmE9bXNpZC1zZW1hbnRpYzogV01TIE1PQ0xmd21HT1hwRG41dlB5R3lyZU5nclJ0Y2xQR2xCVmdjSlxyXG5tPXZpZGVvIDUzMzUxIFVEUC9UTFMvUlRQL1NBVlBGIDk2IDk3IDk4IDk5IDEwMCAxMDEgMTAyIDEyMSAxMjcgMTIwIDEyNSAxMDcgMTA4IDEwOSAzNSAzNiAxMjQgMTE5IDEyMyAxMTggMTE0IDExNSA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G5hPXNzcmM6MTAzNjUzNjU5IG1zbGFiZWw6TU9DTGZ3bUdPWHBEbjV2UHlHeXJlTmdyUnRjbFBHbEJWZ2NKXHJcbmE9c3NyYzoxMDM2NTM2NTkgbGFiZWw6MzE1NjE5NzctODMyNy00ODM1LWJjZGItOTU5NDBmNGExOTVhXHJcbiJ9"

   //前端页面会对sdp进行base64的encode
   b, err := base64.StdEncoding.DecodeString(sdpStr)
   if err != nil {
      panic(err)
   }

   //反序列化为sdp
   if err = json.Unmarshal(b, &sdp); err != nil {
      panic(err.Error())
   }

   //设置远端的sdp
   if err = pc.SetRemoteDescription(sdp); err != nil {
      panic(err.Error())
   }


   //创建协商结果
   answer, err := pc.CreateAnswer(nil)
   if err != nil {
      panic(err.Error())
   }

   //设置自己的协商结果
   if err = pc.SetLocalDescription(answer); err != nil {
      panic(err)
   }

   //等待ice结束
   gatherCmp := webrtc.GatheringCompletePromise(pc)
   <-gatherCmp

   //将协商并且收集完candidate的answer,输出到控制台
   answerBytes, err := json.Marshal(*pc.LocalDescription())
   if err != nil {
      panic(err)
   }

   /*
   //这段代码可以省略,也可以放开。他可以清楚的让你知道你是否推流成功
   pc.OnTrack(func(remote *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
      fmt.Println("one track = ", remote.PayloadType())
      rtpBuf := make([]byte, 1400)
      for {
         n, as, err := remote.Read(rtpBuf)
         fmt.Println("n = ", n, "   as = ", as, " err = ", err)
      }
   })
    */
   
   fmt.Println(base64.StdEncoding.EncodeToString(answerBytes))

   select {
   }
}

流程 创建peerconnection

一开始我们需要创建一个peerconnection,这个会接受配置信息如下:

type Configuration struct {
    //stun或者turn服务器信息,为空只会有host
    ICEServers []ICEServer `json:"iceServers,omitempty"`
    //这个会规定
    //ICETransportPolicyAll 会收集所有的candidate,默认
    //ICETransportPolicyRelay 只会收集relay的candidate
    ICETransportPolicy ICETransportPolicy `json:"iceTransportPolicy,omitempty"`

    //BundlePolicyBalanced 相当于“balanced“,这种情况下音频和视频只会建立两个传输的通道进行传输。如果对端不支持这种方式,那么会将各个轨道的音频分开。默认
    //BundlePolicyMaxCompat 相当于“max-compat”,这种情况下会将所有的媒体轨道分开不同的通道做传输,音频可能会有多条通道传输,视频可能一条。如果对端不支持blanced会回退到这种模式。
    //BundlePolicyMaxBundle 相当于“max-bundle”,这种情况下会将所有的媒体只放到一个通道传输。如果对端不支持单通道,可能会有丢失媒体轨道。
    BundlePolicy BundlePolicy `json:"bundlePolicy,omitempty"`

    //RTCPMuxPolicyNegotiate rtp和rtcp是否复用一个candidate,可以经过协商后得到。如果对端支持端口复用,那么就共用一个candidate。如果对端不支持一个candidate,那么就分开。默认。
    //RTCPMuxPolicyRequire rtp和rtcp必须要复用一个candidate,否则会失败
    RTCPMuxPolicy RTCPMuxPolicy `json:"rtcpMuxPolicy,omitempty"`

    //对端的身份标示,默认为空。
    PeerIdentity string `json:"peerIdentity,omitempty"`

    //DTLS建立时的证书,如果没有填,会自己生成。
    Certificates []Certificate `json:"certificates,omitempty"`

    //当指定大小会提前去收集size个candidate供你使用,就不用等到setLocalDescription()再去收集。默认是0。
    ICECandidatePoolSize uint8 `json:"iceCandidatePoolSize,omitempty"`

    //sdp交互的格式,以下3种值。
    //SDPSemanticsUnifiedPlan sdp的格式为unified plan 
    //SDPSemanticsPlanB sdp格式为plan b
    //SDPSemanticsUnifiedPlanWithFallback 取决于offer的格式,如果unified plan会会unified plan,如果是plan b会回plan b
    SDPSemantics SDPSemantics `json:"sdpSemantics,omitempty"`
}

webrtc.NewPeerConnection函数会去初始化一些配置,做nack的处理等。

设置对端的sdp

sdp信息我们没有经过信令服务器做转发,做了一个最朴素的方法,直接写到代码里面,通过反序列化后直接SetRemoteDescription到pc中。pc会和默认支持的code做协商、设置remote Candidate、开始调用ICE的联通性检查等 *** 作。但是值得注意的是,真正开始连通性检查时需要双端的candidate都有。

创建、设置answer

这个时候会根据协商的内容,创建不同的媒体流,并且在设置的时候会开始收集信息自己的candidate。

输出最终answer

在SetLocalDescription的时候会开始收集,gatherCmp管道会在收集完成的时候收到内容。当收集完成我们就可以将本地的Candidate发送到远端。当然现在已经有ticker ice了,并不需要全部收集完成再发送,我们这边为了简单用的还是比较旧的方式。当我们收集完Candidate,这个时候就可以将本地的sdp经过序列化发送给远端了,剩下的就是连通性检查,然后推流了。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/994232.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存