Skip to content

Commit

Permalink
Update pion to v3, add some background logging for webrtc messages + …
Browse files Browse the repository at this point in the history
…stats, add initial contrib and issue reporting info to README
  • Loading branch information
LouisT committed Jan 9, 2021
1 parent 82746b3 commit 116ee7b
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 356 deletions.
77 changes: 0 additions & 77 deletions .devcontainer/Dockerfile

This file was deleted.

29 changes: 0 additions & 29 deletions .devcontainer/devcontainer.json

This file was deleted.

10 changes: 4 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,12 @@ build-crosscompile-dev: initial
install: initial
go install -i -ldflags "${LDFLAGS}"

install-upx: initial install
make upx-installed
install-upx: initial install upx-installed

install-dev: initial
go build -i -o $(GOPATH)/bin/$(APP_NAME)-dev -v -ldflags "${LDFLAGS} ${DEVLDFLAGS}"

install-dev-upx: initial install-dev
make upx-installed
install-dev-upx: initial install-dev upx-installed

uninstall:
ifneq (,$(shell which termbacktime))
Expand All @@ -97,14 +95,14 @@ ifndef UPX
endif

upx: upx-check
upx --brute ./builds/*
upx -5 ./builds/*

upx-installed: upx-check
upx -5 $(GOPATH)/bin/$(APP_NAME)*

# upx does not support freebsd
upx-crosscompile: upx-check
upx --brute ./builds/$(BINARY_DARWIN)* ./builds/$(BINARY_UNIX)*
upx -5 ./builds/$(BINARY_DARWIN)* ./builds/$(BINARY_UNIX)*

initial:
go clean
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ A STUN server is used to detect network addresses. Please see https://en.wikiped

A TURN server is used to relay WebRTC data between clients. Please see https://webrtc.org/getting-started/turn-server for more information.

## Contributing
Please feel free to open a PR or an issue on GitHub. Convention for this repository is to fork it, create a branch `feature/<feature name>`
in your fork based on `master` branch, and commit changes there. Once your changes are ready to merge back into this repository, squash your feature
branch into one commit and open a PR from your repository's feature branch into the `dev` branch of this repository. Please make sure your `master`
branch is up to date with `master` in this repo, and rebase your feature onto the tip of `master`.

## Reporting Issues
> Please do not report security related issues, vulnerabilities or bugs to the issue tracker, or elsewhere in public. Instead report sensitive bugs by email to <[email protected]>.
- Open an [Issue](issues/new) with a clear and descriptive title.
- Until we can discover whether it is a bug or not, we ask that you label it with `possible-bug`.
- Explain the behavior you would expect and the actual behavior.
- Please provide as much context as possible and describe the *reproduction steps* that someone else can follow to recreate the issue on their own.
- Describe the current behavior and explain which behavior you expected to see instead and why.
- Any availble stack traces or screenshots.
- OS, platform and version. (Windows, Linux, macOS, x86, ARM)
- If possible, please provide the `termbacktime --version` string.
- If manually compiling please provide `go version`, termbacktime version located in the `VERSION` file, and the revision with `git rev-parse --short HEAD`.

## Development
You can build your own development builds via `make build-dev` or `make build-crosscompile-dev`.
I provide development server endpoints for playback + live terminal, login, and WebRTC signaling at:
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.0.0-rc.4
v1.0.0-rc.5
10 changes: 10 additions & 0 deletions cmd/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"io"
"io/ioutil"
"log"
"math/bits"
"net/http"
"os"
"strings"
Expand Down Expand Up @@ -351,3 +352,12 @@ func fileExists(file string) bool {
}
return !info.IsDir()
}

// bytesToSize converts byte count to human readable string
func bytesToSize(bytes uint64) string {
if bytes < 1024 {
return fmt.Sprintf("%dB", bytes)
}
base := uint(bits.Len64(bytes) / 10)
return fmt.Sprintf("%.1f%cB", float64(bytes)/float64(uint64(1<<(base*10))), " KMGTPE"[base])
}
92 changes: 76 additions & 16 deletions cmd/live.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
Expand All @@ -36,9 +35,11 @@ import (
"github.com/creack/pty"
"github.com/gorilla/websocket"
au "github.com/logrusorgru/aurora"
"github.com/pion/webrtc/v2"
"github.com/pion/webrtc/v3"
"github.com/spf13/cobra"
terminal "golang.org/x/term"
"golang.org/x/text/language"
"golang.org/x/text/message"
)

var liveCmd = &cobra.Command{
Expand Down Expand Up @@ -134,12 +135,24 @@ var liveCmd = &cobra.Command{
}
defer client.Close()

peerConnection, err := webrtc.NewPeerConnection(webrtcConfig)
peerConnection, err = webrtc.NewPeerConnection(webrtcConfig)
if err != nil {
fmt.Println(au.Sprintf(au.Red("\nError: %v\n"), err))
os.Exit(1)
}

peerConnection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
connected = state <= 3
GetLogger().Write(fmt.Sprintf("[Peer] State changed: %s", state))
if !connected {
if dataChannel.ReadyState() == 2 {
if err := dataChannel.Close(); err != nil {
GetLogger().Write(fmt.Sprintf("[ERROR] %s", err))
}
}
}
})

dataChannel, err = peerConnection.CreateDataChannel("live-terminal", nil)
if err != nil {
fmt.Println(au.Sprintf(au.Red("\nError: %v\n"), err))
Expand All @@ -152,23 +165,36 @@ var liveCmd = &cobra.Command{
os.Exit(1)
}

err = peerConnection.SetLocalDescription(offer)
if err != nil {
gatherComplete := webrtc.GatheringCompletePromise(peerConnection)
if err := peerConnection.SetLocalDescription(offer); err != nil {
fmt.Println(au.Sprintf(au.Red("\nError: %v\n"), err))
os.Exit(1)
}
<-gatherComplete

encoded, err := EncodeOffer(offer)
encoded, err := EncodeOffer(*peerConnection.LocalDescription())
if err != nil {
fmt.Println(au.Sprintf(au.Red("\nError: %v\n"), err))
os.Exit(1)
}

dataChannel.OnOpen(func() {
if streaming == false {
GetLogger().Write("[Data] Remote peer connected!")
startpty()
}
})
dataChannel.OnError(func(err error) {
GetLogger().Write(fmt.Sprintf("[Data] Error: %s", err))
})
dataChannel.OnClose(func() {
GetLogger().Write("[Data] Remote peer disconnected!")
})
dataChannel.OnMessage(func(msg webrtc.DataChannelMessage) {
if !msg.IsString {
GetLogger().Write("[Data] Warning: unexpected binary message!")
}
})

spinner.Stop()
fmt.Println(au.Sprintf(au.Bold("Live playback: %s\r"), fmt.Sprintf("%s/%s", LiveURL, chn)))
Expand Down Expand Up @@ -271,8 +297,13 @@ func startpty() {
go func() {
for range ch {
if err := pty.InheritSize(os.Stdin, ptmx); err == nil {
if Width, Height, err := terminal.GetSize(int(os.Stdout.Fd())); err == nil {
_ = dataChannel.SendText(ToJSON(LiveLine{
Width, Height, err := terminal.GetSize(int(os.Stdout.Fd()))
if err != nil {
GetLogger().Write(fmt.Sprintf("[ERROR] %s", err))
continue
}
if connected {
dataChannel.SendText(ToJSON(LiveLine{
Command: "s",
Sizes: []int{Width, Height},
}))
Expand All @@ -288,7 +319,7 @@ func startpty() {
fmt.Println(au.Sprintf(au.Red("\nError: %v\n"), err))
os.Exit(1)
}
defer func() { _ = terminal.Restore(int(os.Stdin.Fd()), oldState) }()
defer func() { terminal.Restore(int(os.Stdin.Fd()), oldState) }()

// Write STDIN to PTY.
go func() {
Expand All @@ -299,20 +330,25 @@ func startpty() {
if err == io.EOF {
err = nil
} else {
log.Println(err)
GetLogger().Write(fmt.Sprintf("[ERROR] %s", err))
}
break
}
if !Closed {
if _, err = ptmx.Write(bufin[0:nr]); err != nil {
GetLogger().Write(fmt.Sprintf("[ERROR] %s", err))
break
}
}
}
}()

// XXX: Send this over the data channel?
fmt.Println(au.Green(au.Bold("Live streaming started!\r\n")))
if connected {
fmt.Println(au.Green(au.Bold("Live streaming started!\r\n")))
dataChannel.SendText(ToJSON(LiveLine{
Lines: []string{fmt.Sprint(au.Green(au.Bold("Live streaming started!\r\n")))},
}))
}

// Read from the PTY into a buffer.
bufout := make([]byte, 4096)
Expand All @@ -322,9 +358,11 @@ func startpty() {
line := string(bufout[0:nr])

// Send the line over the data channel
dataChannel.SendText(ToJSON(LiveLine{
Lines: []string{line},
}))
if connected {
dataChannel.SendText(ToJSON(LiveLine{
Lines: []string{line},
}))
}

// Write to STDOUT
os.Stdout.WriteString(line)
Expand All @@ -334,8 +372,30 @@ func startpty() {
break
}

_ = terminal.Restore(int(os.Stdin.Fd()), oldState)
if err := terminal.Restore(int(os.Stdin.Fd()), oldState); err != nil {
GetLogger().Write(fmt.Sprintf("[ERROR] %s", err))
}
fmt.Println(au.Bold(au.Green("\r\nLive streaming ended!")))

if dataChannel.ReadyState() == 2 {
if err := dataChannel.Close(); err != nil {
GetLogger().Write(fmt.Sprintf("[ERROR] %s", err))
}
}

if stats, ok := peerConnection.GetStats().GetDataChannelStats(dataChannel); ok {
formatter := message.NewPrinter(language.English)
GetLogger().Write(fmt.Sprintf("[Stats] Sent: %s / Recieved: %s -- %s",
formatter.Sprintf("%d", stats.MessagesSent),
formatter.Sprintf("%d", stats.MessagesReceived),
bytesToSize(stats.BytesSent+stats.BytesReceived),
))
}

if _, err := GetLogger().Dump().Remove(); err != nil {
fmt.Println(au.Sprintf(au.Red("\nError: failed to remove log: %s\n"), GetLogger().Name()))
}

os.Exit(0)
}

Expand Down
Loading

0 comments on commit 116ee7b

Please sign in to comment.