update to pion v3

This commit is contained in:
Marcel Battista 2021-02-14 16:30:24 +00:00
parent 00a785f4c5
commit a362df4976
14 changed files with 211 additions and 84 deletions

View File

@ -2,7 +2,7 @@ import EventEmitter from 'eventemitter3'
import { OPCODE } from './data' import { OPCODE } from './data'
import { EVENT, WebSocketEvents } from './events' import { EVENT, WebSocketEvents } from './events'
import { WebSocketMessages, WebSocketPayloads, SignalProvidePayload } from './messages' import { WebSocketMessages, WebSocketPayloads, SignalProvidePayload, SignalCandidatePayload } from './messages'
export interface BaseEvents { export interface BaseEvents {
info: (...message: any[]) => void info: (...message: any[]) => void
@ -211,8 +211,8 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
} }
this._peer.ontrack = this.onTrack.bind(this) this._peer.ontrack = this.onTrack.bind(this)
this._peer.addTransceiver('audio', { direction: 'recvonly' }) this._peer.addTransceiver('audio', { direction: 'sendrecv' })
this._peer.addTransceiver('video', { direction: 'recvonly' }) this._peer.addTransceiver('video', { direction: 'sendrecv' })
this._channel = this._peer.createDataChannel('data') this._channel = this._peer.createDataChannel('data')
this._channel.onerror = this.onError.bind(this) this._channel.onerror = this.onError.bind(this)
@ -246,6 +246,15 @@ export abstract class BaseClient extends EventEmitter<BaseEvents> {
this.createPeer(sdp, lite, ice) this.createPeer(sdp, lite, ice)
return return
} }
if (event === EVENT.SIGNAL.CANDIDATE) {
const { data } = payload as SignalCandidatePayload
let candidate: RTCIceCandidate = JSON.parse(data)
this._peer!.addIceCandidate(candidate)
return
}
// @ts-ignore // @ts-ignore
if (typeof this[event] === 'function') { if (typeof this[event] === 'function') {

View File

@ -14,6 +14,7 @@ export const EVENT = {
SIGNAL: { SIGNAL: {
ANSWER: 'signal/answer', ANSWER: 'signal/answer',
PROVIDE: 'signal/provide', PROVIDE: 'signal/provide',
CANDIDATE: 'signal/candidate'
}, },
MEMBER: { MEMBER: {
LIST: 'member/list', LIST: 'member/list',
@ -78,7 +79,7 @@ export type ControlEvents =
export type SystemEvents = typeof EVENT.SYSTEM.DISCONNECT export type SystemEvents = typeof EVENT.SYSTEM.DISCONNECT
export type MemberEvents = typeof EVENT.MEMBER.LIST | typeof EVENT.MEMBER.CONNECTED | typeof EVENT.MEMBER.DISCONNECTED export type MemberEvents = typeof EVENT.MEMBER.LIST | typeof EVENT.MEMBER.CONNECTED | typeof EVENT.MEMBER.DISCONNECTED
export type SignalEvents = typeof EVENT.SIGNAL.ANSWER | typeof EVENT.SIGNAL.PROVIDE export type SignalEvents = typeof EVENT.SIGNAL.ANSWER | typeof EVENT.SIGNAL.PROVIDE | typeof EVENT.SIGNAL.CANDIDATE
export type ChatEvents = typeof EVENT.CHAT.MESSAGE | typeof EVENT.CHAT.EMOTE export type ChatEvents = typeof EVENT.CHAT.MESSAGE | typeof EVENT.CHAT.EMOTE
export type ScreenEvents = typeof EVENT.SCREEN.CONFIGURATIONS | typeof EVENT.SCREEN.RESOLUTION | typeof EVENT.SCREEN.SET export type ScreenEvents = typeof EVENT.SCREEN.CONFIGURATIONS | typeof EVENT.SCREEN.RESOLUTION | typeof EVENT.SCREEN.SET

View File

@ -15,6 +15,7 @@ export type WebSocketMessages =
| WebSocketMessage | WebSocketMessage
| SignalProvideMessage | SignalProvideMessage
| SignalAnswerMessage | SignalAnswerMessage
| SignalCandidateMessage
| MemberListMessage | MemberListMessage
| MemberConnectMessage | MemberConnectMessage
| MemberDisconnectMessage | MemberDisconnectMessage
@ -26,6 +27,7 @@ export type WebSocketMessages =
export type WebSocketPayloads = export type WebSocketPayloads =
| SignalProvidePayload | SignalProvidePayload
| SignalAnswerPayload | SignalAnswerPayload
| SignalCandidatePayload
| MemberListPayload | MemberListPayload
| Member | Member
| ControlPayload | ControlPayload
@ -78,6 +80,14 @@ export interface SignalAnswerPayload {
displayname: string displayname: string
} }
// signal/candidate
export interface SignalCandidateMessage extends WebSocketMessage, SignalCandidatePayload {
event: typeof EVENT.SIGNAL.CANDIDATE
}
export interface SignalCandidatePayload {
data: string
}
/* /*
MEMBER MESSAGES/PAYLOADS MEMBER MESSAGES/PAYLOADS
*/ */

View File

@ -10,10 +10,9 @@ import "C"
import ( import (
"fmt" "fmt"
"sync" "sync"
"time"
"unsafe" "unsafe"
"github.com/pion/webrtc/v2"
"n.eko.moe/neko/internal/types" "n.eko.moe/neko/internal/types"
) )
@ -80,13 +79,13 @@ func CreateRTMPPipeline(pipelineDevice string, pipelineDisplay string, pipelineS
} }
// CreateAppPipeline creates a GStreamer Pipeline // CreateAppPipeline creates a GStreamer Pipeline
func CreateAppPipeline(codecName string, pipelineDevice string, pipelineSrc string) (*Pipeline, error) { func CreateAppPipeline(codecName string, pipelineDevice string, pipelineSrc string, bitrate string) (*Pipeline, error) {
pipelineStr := " ! appsink name=appsink" pipelineStr := " ! appsink name=appsink"
var clockRate float32 var clockRate float32
switch codecName { switch codecName {
case webrtc.VP8: case "VP8":
// https://gstreamer.freedesktop.org/documentation/vpx/vp8enc.html?gi-language=c // https://gstreamer.freedesktop.org/documentation/vpx/vp8enc.html?gi-language=c
// gstreamer1.0-plugins-good // gstreamer1.0-plugins-good
// vp8enc error-resilient=partitions keyframe-max-dist=10 auto-alt-ref=true cpu-used=5 deadline=1 // vp8enc error-resilient=partitions keyframe-max-dist=10 auto-alt-ref=true cpu-used=5 deadline=1
@ -99,9 +98,9 @@ func CreateAppPipeline(codecName string, pipelineDevice string, pipelineSrc stri
if pipelineSrc != "" { if pipelineSrc != "" {
pipelineStr = fmt.Sprintf(pipelineSrc+pipelineStr, pipelineDevice) pipelineStr = fmt.Sprintf(pipelineSrc+pipelineStr, pipelineDevice)
} else { } else {
pipelineStr = fmt.Sprintf(videoSrc+"vp8enc cpu-used=8 threads=2 deadline=1 error-resilient=partitions keyframe-max-dist=10 auto-alt-ref=true"+pipelineStr, pipelineDevice) pipelineStr = fmt.Sprintf(videoSrc+"vp8enc cpu-used=-5 threads=4 deadline=1 error-resilient=partitions keyframe-max-dist=30 auto-alt-ref=true"+pipelineStr, pipelineDevice)
} }
case webrtc.VP9: case "VP9":
// https://gstreamer.freedesktop.org/documentation/vpx/vp9enc.html?gi-language=c // https://gstreamer.freedesktop.org/documentation/vpx/vp9enc.html?gi-language=c
// gstreamer1.0-plugins-good // gstreamer1.0-plugins-good
// vp9enc // vp9enc
@ -117,7 +116,7 @@ func CreateAppPipeline(codecName string, pipelineDevice string, pipelineSrc stri
} else { } else {
pipelineStr = fmt.Sprintf(videoSrc+"vp9enc"+pipelineStr, pipelineDevice) pipelineStr = fmt.Sprintf(videoSrc+"vp9enc"+pipelineStr, pipelineDevice)
} }
case webrtc.H264: case "H264":
// https://gstreamer.freedesktop.org/documentation/openh264/openh264enc.html?gi-language=c#openh264enc // https://gstreamer.freedesktop.org/documentation/openh264/openh264enc.html?gi-language=c#openh264enc
// gstreamer1.0-plugins-bad // gstreamer1.0-plugins-bad
// openh264enc multi-thread=4 complexity=high bitrate=3072000 max-bitrate=4096000 // openh264enc multi-thread=4 complexity=high bitrate=3072000 max-bitrate=4096000
@ -132,12 +131,19 @@ func CreateAppPipeline(codecName string, pipelineDevice string, pipelineSrc stri
} else { } else {
var h264Str string var h264Str string
h264Str = "openh264enc multi-thread=4 complexity=high bitrate=3072000 max-bitrate=4096000 ! video/x-h264,stream-format=byte-stream" h264Str = "openh264enc multi-thread=4 complexity=high bitrate=3072000 max-bitrate=4096000 ! video/x-h264,stream-format=byte-stream"
if bitrate != "" {
h264Str = "openh264enc multi-thread=4 complexity=high bitrate=" + bitrate + "000 max-bitrate=" + bitrate + "999 ! video/x-h264,stream-format=byte-stream"
}
// https://gstreamer.freedesktop.org/documentation/x264/index.html?gi-language=c // https://gstreamer.freedesktop.org/documentation/x264/index.html?gi-language=c
// gstreamer1.0-plugins-ugly // gstreamer1.0-plugins-ugly
// video/x-raw,format=I420 ! x264enc bframes=0 key-int-max=60 byte-stream=true tune=zerolatency speed-preset=veryfast ! video/x-h264,stream-format=byte-stream // video/x-raw,format=I420 ! x264enc bframes=0 key-int-max=60 byte-stream=true tune=zerolatency speed-preset=veryfast ! video/x-h264,stream-format=byte-stream
if err := CheckPlugins([]string{"openh264"}); err != nil { if err := CheckPlugins([]string{"openh264"}); err != nil {
h264Str = "video/x-raw,format=I420 ! x264enc bframes=0 key-int-max=60 byte-stream=true tune=zerolatency speed-preset=veryfast ! video/x-h264,stream-format=byte-stream"
h264Str = "video/x-raw,format=I420 ! x264enc threads=4 byte-stream=true tune=zerolatency speed-preset=veryfast ! video/x-h264,stream-format=byte-stream"
if bitrate != "" {
h264Str = "video/x-raw,format=I420 ! x264enc threads=4 bitrate=" + bitrate + " byte-stream=true tune=zerolatency speed-preset=veryfast ! video/x-h264,stream-format=byte-stream"
}
if err := CheckPlugins([]string{"x264"}); err != nil { if err := CheckPlugins([]string{"x264"}); err != nil {
return nil, err return nil, err
@ -145,7 +151,7 @@ func CreateAppPipeline(codecName string, pipelineDevice string, pipelineSrc stri
} }
pipelineStr = fmt.Sprintf(videoSrc+h264Str+pipelineStr, pipelineDevice) pipelineStr = fmt.Sprintf(videoSrc+h264Str+pipelineStr, pipelineDevice)
} }
case webrtc.Opus: case "Opus":
// https://gstreamer.freedesktop.org/documentation/opus/opusenc.html // https://gstreamer.freedesktop.org/documentation/opus/opusenc.html
// gstreamer1.0-plugins-base // gstreamer1.0-plugins-base
// opusenc // opusenc
@ -160,7 +166,7 @@ func CreateAppPipeline(codecName string, pipelineDevice string, pipelineSrc stri
} else { } else {
pipelineStr = fmt.Sprintf(audioSrc+"opusenc"+pipelineStr, pipelineDevice) pipelineStr = fmt.Sprintf(audioSrc+"opusenc"+pipelineStr, pipelineDevice)
} }
case webrtc.G722: case "G722":
// https://gstreamer.freedesktop.org/documentation/libav/avenc_g722.html?gi-language=c // https://gstreamer.freedesktop.org/documentation/libav/avenc_g722.html?gi-language=c
// gstreamer1.0-libav // gstreamer1.0-libav
// avenc_g722 // avenc_g722
@ -175,7 +181,7 @@ func CreateAppPipeline(codecName string, pipelineDevice string, pipelineSrc stri
} else { } else {
pipelineStr = fmt.Sprintf(audioSrc+"avenc_g722"+pipelineStr, pipelineDevice) pipelineStr = fmt.Sprintf(audioSrc+"avenc_g722"+pipelineStr, pipelineDevice)
} }
case webrtc.PCMU: case "PCMU":
// https://gstreamer.freedesktop.org/documentation/mulaw/mulawenc.html?gi-language=c // https://gstreamer.freedesktop.org/documentation/mulaw/mulawenc.html?gi-language=c
// gstreamer1.0-plugins-good // gstreamer1.0-plugins-good
// audio/x-raw, rate=8000 ! mulawenc // audio/x-raw, rate=8000 ! mulawenc
@ -190,7 +196,7 @@ func CreateAppPipeline(codecName string, pipelineDevice string, pipelineSrc stri
} else { } else {
pipelineStr = fmt.Sprintf(audioSrc+"audio/x-raw, rate=8000 ! mulawenc"+pipelineStr, pipelineDevice) pipelineStr = fmt.Sprintf(audioSrc+"audio/x-raw, rate=8000 ! mulawenc"+pipelineStr, pipelineDevice)
} }
case webrtc.PCMA: case "PCMA":
// https://gstreamer.freedesktop.org/documentation/alaw/alawenc.html?gi-language=c // https://gstreamer.freedesktop.org/documentation/alaw/alawenc.html?gi-language=c
// gstreamer1.0-plugins-good // gstreamer1.0-plugins-good
// audio/x-raw, rate=8000 ! alawenc // audio/x-raw, rate=8000 ! alawenc
@ -270,8 +276,7 @@ func goHandlePipelineBuffer(buffer unsafe.Pointer, bufferLen C.int, duration C.i
pipelinesLock.Unlock() pipelinesLock.Unlock()
if ok { if ok {
samples := uint32(pipeline.ClockRate * (float32(duration) / 1000000000)) pipeline.Sample <- types.Sample{Data: C.GoBytes(buffer, bufferLen), Timestamp: time.Now(), Duration: time.Duration(duration)}
pipeline.Sample <- types.Sample{Data: C.GoBytes(buffer, bufferLen), Samples: samples}
} else { } else {
fmt.Printf("discarding buffer, no pipeline with id %d", int(pipelineID)) fmt.Printf("discarding buffer, no pipeline with id %d", int(pipelineID))
} }

View File

@ -135,6 +135,7 @@ func (manager *RemoteManager) createPipelines() {
manager.config.VideoCodec, manager.config.VideoCodec,
manager.config.Display, manager.config.Display,
manager.config.VideoParams, manager.config.VideoParams,
manager.config.Bitrate,
) )
if err != nil { if err != nil {
manager.logger.Panic().Err(err).Msg("unable to create video pipeline") manager.logger.Panic().Err(err).Msg("unable to create video pipeline")
@ -144,6 +145,7 @@ func (manager *RemoteManager) createPipelines() {
manager.config.AudioCodec, manager.config.AudioCodec,
manager.config.Device, manager.config.Device,
manager.config.AudioParams, manager.config.AudioParams,
"",
) )
if err != nil { if err != nil {
manager.logger.Panic().Err(err).Msg("unable to create audio pipeline") manager.logger.Panic().Err(err).Msg("unable to create audio pipeline")
@ -174,6 +176,7 @@ func (manager *RemoteManager) ChangeResolution(width int, height int, rate int)
manager.config.VideoCodec, manager.config.VideoCodec,
manager.config.Display, manager.config.Display,
manager.config.VideoParams, manager.config.VideoParams,
manager.config.Bitrate,
) )
if err != nil { if err != nil {
manager.logger.Panic().Err(err).Msg("unable to create new video pipeline") manager.logger.Panic().Err(err).Msg("unable to create new video pipeline")

View File

@ -120,6 +120,16 @@ func (session *Session) SignalAnswer(sdp string) error {
return session.peer.SignalAnswer(sdp) return session.peer.SignalAnswer(sdp)
} }
func (session *Session) SignalCandidate(data string) error {
if session.socket == nil {
return nil
}
return session.socket.Send(&message.SignalCandidate{
Event: event.SIGNAL_CANDIDATE,
Data: data,
});
}
func (session *Session) destroy() error { func (session *Session) destroy() error {
if session.socket != nil { if session.socket != nil {
if err := session.socket.Destroy(); err != nil { if err := session.socket.Destroy(); err != nil {

View File

@ -4,7 +4,6 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"github.com/pion/webrtc/v2"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -19,6 +18,7 @@ type Remote struct {
ScreenWidth int ScreenWidth int
ScreenHeight int ScreenHeight int
ScreenRate int ScreenRate int
Bitrate string
} }
func (Remote) Init(cmd *cobra.Command) error { func (Remote) Init(cmd *cobra.Command) error {
@ -47,6 +47,12 @@ func (Remote) Init(cmd *cobra.Command) error {
return err return err
} }
cmd.PersistentFlags().String("bitrate", "", "set this video bitrate when possible")
if err := viper.BindPFlag("bitrate", cmd.PersistentFlags().Lookup("bitrate")); err != nil {
return err
}
// video codecs // video codecs
cmd.PersistentFlags().Bool("vp8", false, "use VP8 video codec") cmd.PersistentFlags().Bool("vp8", false, "use VP8 video codec")
if err := viper.BindPFlag("vp8", cmd.PersistentFlags().Lookup("vp8")); err != nil { if err := viper.BindPFlag("vp8", cmd.PersistentFlags().Lookup("vp8")); err != nil {
@ -88,24 +94,24 @@ func (Remote) Init(cmd *cobra.Command) error {
} }
func (s *Remote) Set() { func (s *Remote) Set() {
videoCodec := webrtc.VP8 videoCodec := "VP8"
if viper.GetBool("vp8") { if viper.GetBool("vp8") {
videoCodec = webrtc.VP8 videoCodec = "VP8"
} else if viper.GetBool("vp9") { } else if viper.GetBool("vp9") {
videoCodec = webrtc.VP9 videoCodec = "VP9"
} else if viper.GetBool("h264") { } else if viper.GetBool("h264") {
videoCodec = webrtc.H264 videoCodec = "H264"
} }
audioCodec := webrtc.Opus audioCodec := "Opus"
if viper.GetBool("opus") { if viper.GetBool("opus") {
audioCodec = webrtc.Opus audioCodec = "Opus"
} else if viper.GetBool("g722") { } else if viper.GetBool("g722") {
audioCodec = webrtc.G722 audioCodec = "G722"
} else if viper.GetBool("pcmu") { } else if viper.GetBool("pcmu") {
audioCodec = webrtc.PCMU audioCodec = "PCMU"
} else if viper.GetBool("pcma") { } else if viper.GetBool("pcma") {
audioCodec = webrtc.PCMA audioCodec = "PCMA"
} }
s.Device = viper.GetString("device") s.Device = viper.GetString("device")
@ -114,6 +120,7 @@ func (s *Remote) Set() {
s.Display = viper.GetString("display") s.Display = viper.GetString("display")
s.VideoCodec = videoCodec s.VideoCodec = videoCodec
s.VideoParams = viper.GetString("video") s.VideoParams = viper.GetString("video")
s.Bitrate = viper.GetString("bitrate")
s.ScreenWidth = 1280 s.ScreenWidth = 1280
s.ScreenHeight = 720 s.ScreenHeight = 720

View File

@ -6,7 +6,9 @@ const (
const ( const (
SIGNAL_ANSWER = "signal/answer" SIGNAL_ANSWER = "signal/answer"
SIGNAL_OFFER = "signal/offer"
SIGNAL_PROVIDE = "signal/provide" SIGNAL_PROVIDE = "signal/provide"
SIGNAL_CANDIDATE = "signal/candidate"
) )
const ( const (

View File

@ -27,6 +27,11 @@ type SignalAnswer struct {
SDP string `json:"sdp"` SDP string `json:"sdp"`
} }
type SignalCandidate struct {
Event string `json:"event"`
Data string `json:"data"`
}
type MembersList struct { type MembersList struct {
Event string `json:"event"` Event string `json:"event"`
Memebers []*types.Member `json:"members"` Memebers []*types.Member `json:"members"`

View File

@ -24,6 +24,7 @@ type Session interface {
Write(v interface{}) error Write(v interface{}) error
Send(v interface{}) error Send(v interface{}) error
SignalAnswer(sdp string) error SignalAnswer(sdp string) error
SignalCandidate(data string) error
} }
type SessionManager interface { type SessionManager interface {

View File

@ -1,8 +1,13 @@
package types package types
import (
"time"
)
type Sample struct { type Sample struct {
Data []byte Data []byte
Samples uint32 Timestamp time.Time
Duration time.Duration
} }
type WebRTCManager interface { type WebRTCManager interface {

View File

@ -5,7 +5,7 @@ import (
"encoding/binary" "encoding/binary"
"strconv" "strconv"
"github.com/pion/webrtc/v2" "github.com/pion/webrtc/v3"
) )
const OP_MOVE = 0x01 const OP_MOVE = 0x01

View File

@ -3,7 +3,7 @@ package webrtc
import ( import (
"sync" "sync"
"github.com/pion/webrtc/v2" "github.com/pion/webrtc/v3"
) )
type Peer struct { type Peer struct {

View File

@ -1,13 +1,15 @@
package webrtc package webrtc
import ( import (
"encoding/json"
"fmt" "fmt"
"io" "io"
"math/rand"
"strings" "strings"
"github.com/pion/webrtc/v2" "github.com/pion/interceptor"
"github.com/pion/webrtc/v2/pkg/media" "github.com/pion/rtcp"
"github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3/pkg/media"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -26,10 +28,10 @@ func New(sessions types.SessionManager, remote types.RemoteManager, config *conf
type WebRTCManager struct { type WebRTCManager struct {
logger zerolog.Logger logger zerolog.Logger
videoTrack *webrtc.Track videoTrack *webrtc.TrackLocalStaticSample
audioTrack *webrtc.Track audioTrack *webrtc.TrackLocalStaticSample
videoCodec *webrtc.RTPCodec videoCodec webrtc.RTPCodecParameters
audioCodec *webrtc.RTPCodec audioCodec webrtc.RTPCodecParameters
sessions types.SessionManager sessions types.SessionManager
remote types.RemoteManager remote types.RemoteManager
config *config.WebRTC config *config.WebRTC
@ -97,39 +99,31 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri
settings.SetEphemeralUDPPortRange(manager.config.EphemeralMin, manager.config.EphemeralMax) settings.SetEphemeralUDPPortRange(manager.config.EphemeralMin, manager.config.EphemeralMax)
settings.SetNAT1To1IPs(manager.config.NAT1To1IPs, webrtc.ICECandidateTypeHost) settings.SetNAT1To1IPs(manager.config.NAT1To1IPs, webrtc.ICECandidateTypeHost)
settings.SetSRTPReplayProtectionWindow(512)
// Create MediaEngine based off sdp // Create MediaEngine based off sdp
engine := webrtc.MediaEngine{} engine := webrtc.MediaEngine{}
engine.RegisterCodec(manager.audioCodec) engine.RegisterCodec(manager.audioCodec, webrtc.RTPCodecTypeAudio)
engine.RegisterCodec(manager.videoCodec) engine.RegisterCodec(manager.videoCodec, webrtc.RTPCodecTypeVideo)
i := &interceptor.Registry{}
if err := webrtc.RegisterDefaultInterceptors(&engine, i); err != nil {
return "", manager.config.ICELite, manager.config.ICEServers, err
}
// Create API with MediaEngine and SettingEngine // Create API with MediaEngine and SettingEngine
api := webrtc.NewAPI(webrtc.WithMediaEngine(engine), webrtc.WithSettingEngine(settings)) api := webrtc.NewAPI(webrtc.WithMediaEngine(&engine), webrtc.WithSettingEngine(settings), webrtc.WithInterceptorRegistry(i))
// Create new peer connection // Create new peer connection
connection, err := api.NewPeerConnection(*configuration) connection, err := api.NewPeerConnection(*configuration)
if err != nil { if err != nil {
return "", manager.config.ICELite, manager.config.ICEServers, err return "", manager.config.ICELite, manager.config.ICEServers, err
} }
negotiated := true
if _, err = connection.AddTransceiverFromTrack(manager.videoTrack, webrtc.RtpTransceiverInit{ connection.CreateDataChannel("data", &webrtc.DataChannelInit{
Direction: webrtc.RTPTransceiverDirectionSendonly, Negotiated: &negotiated,
}); err != nil { })
return "", manager.config.ICELite, manager.config.ICEServers, err
}
if _, err = connection.AddTransceiverFromTrack(manager.audioTrack, webrtc.RtpTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionSendonly,
}); err != nil {
return "", manager.config.ICELite, manager.config.ICEServers, err
}
description, err := connection.CreateOffer(nil)
if err != nil {
return "", manager.config.ICELite, manager.config.ICEServers, err
}
connection.OnDataChannel(func(d *webrtc.DataChannel) { connection.OnDataChannel(func(d *webrtc.DataChannel) {
d.OnMessage(func(msg webrtc.DataChannelMessage) { d.OnMessage(func(msg webrtc.DataChannelMessage) {
if err = manager.handle(id, msg); err != nil { if err = manager.handle(id, msg); err != nil {
@ -138,7 +132,31 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri
}) })
}) })
connection.SetLocalDescription(description) // Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected
connection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("Connection State has changed %s \n", connectionState.String())
})
rtpSender, viderr := connection.AddTrack(manager.videoTrack)
if viderr != nil {
return "", manager.config.ICELite, manager.config.ICEServers, viderr
}
if _, err = connection.AddTrack(manager.audioTrack); err != nil {
return "", manager.config.ICELite, manager.config.ICEServers, err
}
description, err := connection.CreateOffer(nil)
if err != nil {
return "", manager.config.ICELite, manager.config.ICEServers, err
}
err = connection.SetLocalDescription(description)
if err != nil {
panic(err)
}
connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) { connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
switch state { switch state {
case webrtc.PeerConnectionStateDisconnected: case webrtc.PeerConnectionStateDisconnected:
@ -156,6 +174,47 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri
} }
}) })
connection.OnICECandidate(func(i *webrtc.ICECandidate) {
if i != nil {
candidateString, err := json.Marshal(i.ToJSON())
if err != nil {
manager.logger.Info().Msg("error")
return
}
if err = session.SignalCandidate(string(candidateString));err != nil {
manager.logger.Info().Msg("err")
return
}
}
})
// Read incoming RTCP packets
// Before these packets are retuned they are processed by interceptors. For things
// like NACK this needs to be called.
go func() {
rtcpBuf := make([]byte, 1500)
for {
n, _, rtcpErr := rtpSender.Read(rtcpBuf)
if rtcpErr != nil {
return
}
ps, err := rtcp.Unmarshal(rtcpBuf[:n])
if err != nil {
log.Printf("Unmarshal RTCP: %v", err)
continue
}
for _, p := range ps {
switch p.(type) {
case *rtcp.TransportLayerNack:
manager.logger.Info().Msg("got a nack")
}
}
}
}()
if err := session.SetPeer(&Peer{ if err := session.SetPeer(&Peer{
id: id, id: id,
api: api, api: api,
@ -171,30 +230,40 @@ func (manager *WebRTCManager) CreatePeer(id string, session types.Session) (stri
return description.SDP, manager.config.ICELite, manager.config.ICEServers, nil return description.SDP, manager.config.ICELite, manager.config.ICEServers, nil
} }
func (m *WebRTCManager) createTrack(codecName string) (*webrtc.Track, *webrtc.RTPCodec, error) { func (m *WebRTCManager) createTrack(codecName string) (*webrtc.TrackLocalStaticSample, webrtc.RTPCodecParameters, error) {
var codec *webrtc.RTPCodec var codec webrtc.RTPCodecParameters
var fb []webrtc.RTCPFeedback
var fba []webrtc.RTCPFeedback
fb = []webrtc.RTCPFeedback{
{"goog-remb", ""},
{"nack", ""},
{"nack", "pli"},
{"ccm", "fir"},
}
fba = []webrtc.RTCPFeedback{}
switch codecName { switch codecName {
case webrtc.VP8: case "VP8":
codec = webrtc.NewRTPVP8Codec(webrtc.DefaultPayloadTypeVP8, 90000) codec = webrtc.RTPCodecParameters{ RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "video/VP8", ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: fb}, PayloadType: 96,}
case webrtc.VP9: case "VP9":
codec = webrtc.NewRTPVP9Codec(webrtc.DefaultPayloadTypeVP9, 90000) codec = webrtc.RTPCodecParameters{ RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "video/VP9", ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: fb}, PayloadType: 98,}
case webrtc.H264: case "H264":
codec = webrtc.NewRTPH264Codec(webrtc.DefaultPayloadTypeH264, 90000) codec = webrtc.RTPCodecParameters{ RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "video/H264", ClockRate: 90000, Channels: 0, SDPFmtpLine: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42001f", RTCPFeedback: fb}, PayloadType: 102,}
case webrtc.Opus: case "Opus":
codec = webrtc.NewRTPOpusCodec(webrtc.DefaultPayloadTypeOpus, 48000) codec = webrtc.RTPCodecParameters{ RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "audio/opus", ClockRate: 48000, Channels: 2, SDPFmtpLine: "", RTCPFeedback: fba}, PayloadType: 111,}
case webrtc.G722: case "G722":
codec = webrtc.NewRTPG722Codec(webrtc.DefaultPayloadTypeG722, 8000) codec = webrtc.RTPCodecParameters{ RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "audio/G722", ClockRate: 8000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: fba}, PayloadType: 9,}
case webrtc.PCMU: case "PCMU":
codec = webrtc.NewRTPPCMUCodec(webrtc.DefaultPayloadTypePCMU, 8000) codec = webrtc.RTPCodecParameters{ RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "audio/PCMU", ClockRate: 8000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: fba}, PayloadType: 0,}
case webrtc.PCMA: case "PCMA":
codec = webrtc.NewRTPPCMACodec(webrtc.DefaultPayloadTypePCMA, 8000) codec = webrtc.RTPCodecParameters{ RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "audio/PCMA", ClockRate: 8000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: fba}, PayloadType: 8,}
default: default:
return nil, nil, fmt.Errorf("unknown codec %s", codecName) return nil, codec, fmt.Errorf("unknown codec %s", codecName)
} }
track, err := webrtc.NewTrack(codec.PayloadType, rand.Uint32(), "stream", "stream", codec) track, err := webrtc.NewTrackLocalStaticSample(codec.RTPCodecCapability, "stream", "stream")
if err != nil { if err != nil {
return nil, nil, err return nil, codec, err
} }
return track, codec, nil return track, codec, nil