Skip to content
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

External peerconnection implementation #29

Draft
wants to merge 8 commits into
base: devel
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"webrtcperf": "app.min.js"
},
"scripts": {
"prepare": "yarn lint && tsc -b && webpack",
"prepare": "yarn lint && tsc -b && webpack && cd peer-connection-external && go build",
"prepublishOnly": "yarn lint",
"postversion": "git push && git push origin $(git tag | sort -V | tail -1)",
"release": "yarn && yarn docs && npm version patch && npm publish",
Expand Down
1 change: 1 addition & 0 deletions peer-connection-external/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
peer-connection-external
31 changes: 31 additions & 0 deletions peer-connection-external/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module peer-connection-external

go 1.18

require (
github.com/pion/example-webrtc-applications/v3 v3.0.5
github.com/pion/rtcp v1.2.10
github.com/pion/webrtc/v3 v3.1.50
)

require (
github.com/google/uuid v1.3.0 // indirect
github.com/pion/datachannel v1.5.5 // indirect
github.com/pion/dtls/v2 v2.1.5 // indirect
github.com/pion/ice/v2 v2.2.12 // indirect
github.com/pion/interceptor v0.1.11 // indirect
github.com/pion/logging v0.2.2 // indirect
github.com/pion/mdns v0.0.5 // indirect
github.com/pion/randutil v0.1.0 // indirect
github.com/pion/rtp v1.7.13 // indirect
github.com/pion/sctp v1.8.5 // indirect
github.com/pion/sdp/v3 v3.0.6 // indirect
github.com/pion/srtp/v2 v2.0.10 // indirect
github.com/pion/stun v0.3.5 // indirect
github.com/pion/transport v0.14.1 // indirect
github.com/pion/turn/v2 v2.0.8 // indirect
github.com/pion/udp v0.1.1 // indirect
golang.org/x/crypto v0.0.0-20221010152910-d6f0a8c073c2 // indirect
golang.org/x/net v0.3.0 // indirect
golang.org/x/sys v0.3.0 // indirect
)
216 changes: 216 additions & 0 deletions peer-connection-external/go.sum

Large diffs are not rendered by default.

215 changes: 215 additions & 0 deletions peer-connection-external/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package main

import (
"bufio"
"encoding/json"
"errors"
"fmt"
"os"
"strings"

"github.com/pion/interceptor"
"github.com/pion/webrtc/v3"
)

func readStdin(channel chan string) {
r := bufio.NewReader(os.Stdin)
for {
var in string
for {
var err error
in, err = r.ReadString('\n')
if err != nil {
os.Exit(0)
}
in = strings.TrimSpace(in)
if len(in) > 0 {
break
}
}
channel <- in
}
}

func onResult(id string, command string, value string) {
//println(fmt.Sprintf("[peer-connection-external] [%s] %s", id, command))
println(fmt.Sprintf("[peer-connection-external] [%s] %s result: '%s'", id, command, value))
fmt.Printf("r%s|%s|%s\n", id, command, value)
}

func onError(id string, command string, err error) {
println(fmt.Sprintf("[peer-connection-external] [%s] %s error: %s", id, command, err))
fmt.Printf("e%s|%s|%s\n", id, command, err.Error())
}

func main() {
m := &webrtc.MediaEngine{}
if err := m.RegisterDefaultCodecs(); err != nil {
panic(err)
}

i := &interceptor.Registry{}
if err := webrtc.RegisterDefaultInterceptors(m, i); err != nil {
panic(err)
}
settingEngine := webrtc.SettingEngine{}
// settingEngine.SetLite(true)
api := webrtc.NewAPI(webrtc.WithMediaEngine(m), webrtc.WithInterceptorRegistry(i), webrtc.WithSettingEngine(settingEngine))

config := webrtc.Configuration{}
err := json.Unmarshal([]byte(os.Args[1]), &config)
if err != nil {
panic(err)
}

peerConnection, err := api.NewPeerConnection(config)
if err != nil {
panic(err)
}

peerConnection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
onResult("ev", "connectionstatechange", state.String())
})

peerConnection.OnICEConnectionStateChange(func(state webrtc.ICEConnectionState) {
onResult("ev", "iceconnectionstatechange", state.String())
})

peerConnection.OnICEGatheringStateChange(func(state webrtc.ICEGathererState) {
onResult("ev", "icegatheringstatechange", state.String())
})

peerConnection.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
// Send a PLI on an interval so that the publisher is pushing a keyframe every rtcpPLIInterval
/* go func() {
ticker := time.NewTicker(time.Second * 5)
for range ticker.C {
rtcpSendErr := peerConnection.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: uint32(track.SSRC())}})
if rtcpSendErr != nil {
fmt.Println(rtcpSendErr)
}
}
}() */

codecName := strings.Split(track.Codec().RTPCodecCapability.MimeType, "/")[1]
println(fmt.Sprintf("[peer-connection-external] OnTrack type %d: %s", track.PayloadType(), codecName))

buf := make([]byte, 1400)
for {
i, _, readErr := track.Read(buf)
if readErr != nil {
panic(err)
}
println("[peer-connection-external] OnTrack data", i)
}
})

//
channel := make(chan string)
go readStdin(channel)

for msg := range channel {
commands := strings.SplitN(msg, "|", 3)
id := commands[0]
command := commands[1]
value := commands[2]

//println(fmt.Sprintf("[peer-connection-external] command [%s] %s: \"%s\"", id, command, value))

switch {
case command == "close":
onResult(id, command, "")
return

case command == "addTransceiver":
var args map[string]interface{}
err = json.Unmarshal([]byte(value), &args)
if err != nil {
onError(id, command, err)
continue
}
trackOrKind := args["trackOrKind"].(string)
_, err := peerConnection.AddTransceiverFromKind(webrtc.NewRTPCodecType(trackOrKind))
if err != nil {
onError(id, command, err)
continue
}
onResult(id, command, "")

case command == "createOffer":
offer, err := peerConnection.CreateOffer(nil)
if err != nil {
onError(id, command, err)
continue
}
b, err := json.Marshal(offer)
if err != nil {
onError(id, command, err)
continue
}
onResult(id, command, string(b))
/* err = peerConnection.SetLocalDescription(offer)
if err != nil {
onError(id, command, err)
continue
}
b, err := json.Marshal(offer)
if err != nil {
onError(id, command, err)
continue
}
onResult(id, command, string(b)) */

case command == "createAnswer":
answer, err := peerConnection.CreateAnswer(nil)
if err != nil {
onError(id, command, err)
continue
}
//gatherComplete := webrtc.GatheringCompletePromise(peerConnection)
err = peerConnection.SetLocalDescription(answer)
if err != nil {
onError(id, command, err)
continue
}
//<-gatherComplete
b, err := json.Marshal(*peerConnection.LocalDescription())
if err != nil {
onError(id, command, err)
continue
}
onResult(id, command, string(b))

case command == "setLocalDescription":
sdp := webrtc.SessionDescription{}
err = json.Unmarshal([]byte(value), &sdp)
if err != nil {
onError(id, command, err)
continue
}
/* if err = peerConnection.SetLocalDescription(sdp); err != nil {
onError(id, command, err)
continue
} */
onResult(id, command, "")

case command == "setRemoteDescription":
sdp := webrtc.SessionDescription{}
err = json.Unmarshal([]byte(value), &sdp)
if err != nil {
onError(id, command, err)
continue
}
err = peerConnection.SetRemoteDescription(sdp)
if err != nil {
onError(id, command, err)
continue
}
onResult(id, command, "")

default:
onError(id, command, errors.New("command not found"))
continue
}
}
}
Loading