Skip to content

Commit

Permalink
Fix all lint errors in examples/
Browse files Browse the repository at this point in the history
Resolves #531 and Resolves #470
  • Loading branch information
Sean-Der committed Mar 21, 2019
1 parent 869df92 commit 9fe44cd
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 87 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ before_script:

script:
- golangci-lint run ./...
- cd examples && golangci-lint run ./...
- cd examples && golangci-lint run ./... -D gochecknoinits
- cd ..
- rm -rf examples # Remove examples, no test coverage for them
- go test -coverpkg=$(go list ./... | tr '\n' ',') -coverprofile=cover.out -v -race -covermode=atomic ./...
Expand Down
14 changes: 7 additions & 7 deletions examples/data-channels-close/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ func main() {

// Register data channel creation handling
peerConnection.OnDataChannel(func(d *webrtc.DataChannel) {
fmt.Printf("New DataChannel %s %d\n", d.Label, d.ID)
fmt.Printf("New DataChannel %s %d\n", d.Label(), d.ID())

// Register channel opening handling
d.OnOpen(func() {
fmt.Printf("Data channel '%s'-'%d' open. Random messages will now be sent to any connected DataChannels every 5 seconds\n", d.Label, d.ID)
fmt.Printf("Data channel '%s'-'%d' open. Random messages will now be sent to any connected DataChannels every 5 seconds\n", d.Label(), d.ID())

ticker := time.NewTicker(5 * time.Second)

d.OnClose(func() {
fmt.Printf("Data channel '%s'-'%d' closed.\n", d.Label, d.ID)
fmt.Printf("Data channel '%s'-'%d' closed.\n", d.Label(), d.ID())
ticker.Stop()
})

Expand All @@ -58,14 +58,14 @@ func main() {
fmt.Printf("Sending '%s'\n", message)

// Send the message as text
err := d.SendText(message)
if err != nil {
sendErr := d.SendText(message)
if sendErr != nil {
panic(err)
}

cnt--
if cnt < 0 {
fmt.Printf("Sent %d times. Closing data channel '%s'-'%d'.\n", *closeAfter, d.Label, d.ID)
fmt.Printf("Sent %d times. Closing data channel '%s'-'%d'.\n", *closeAfter, d.Label(), d.ID())
ticker.Stop()
err = d.Close()
if err != nil {
Expand All @@ -77,7 +77,7 @@ func main() {

// Register message handling
d.OnMessage(func(msg webrtc.DataChannelMessage) {
fmt.Printf("Message from DataChannel '%s': '%s'\n", d.Label, string(msg.Data))
fmt.Printf("Message from DataChannel '%s': '%s'\n", d.Label(), string(msg.Data))
})
})

Expand Down
8 changes: 4 additions & 4 deletions examples/data-channels-create/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ func main() {

// Register channel opening handling
dataChannel.OnOpen(func() {
fmt.Printf("Data channel '%s'-'%d' open. Random messages will now be sent to any connected DataChannels every 5 seconds\n", dataChannel.Label, dataChannel.ID)
fmt.Printf("Data channel '%s'-'%d' open. Random messages will now be sent to any connected DataChannels every 5 seconds\n", dataChannel.Label(), dataChannel.ID())

for range time.NewTicker(5 * time.Second).C {
message := signal.RandSeq(15)
fmt.Printf("Sending '%s'\n", message)

// Send the message as text
err := dataChannel.SendText(message)
if err != nil {
sendErr := dataChannel.SendText(message)
if sendErr != nil {
panic(err)
}
}
})

// Register text message handling
dataChannel.OnMessage(func(msg webrtc.DataChannelMessage) {
fmt.Printf("Message from DataChannel '%s': '%s'\n", dataChannel.Label, string(msg.Data))
fmt.Printf("Message from DataChannel '%s': '%s'\n", dataChannel.Label(), string(msg.Data))
})

// Create an offer to send to the browser
Expand Down
5 changes: 3 additions & 2 deletions examples/data-channels-detach-create/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"io"
"time"

"github.com/pions/datachannel"
Expand Down Expand Up @@ -55,7 +56,7 @@ func main() {

// Register channel opening handling
dataChannel.OnOpen(func() {
fmt.Printf("Data channel '%s'-'%d' open.\n", dataChannel.Label, dataChannel.ID)
fmt.Printf("Data channel '%s'-'%d' open.\n", dataChannel.Label(), dataChannel.ID())

// Detach the data channel
raw, dErr := dataChannel.Detach()
Expand Down Expand Up @@ -114,7 +115,7 @@ func ReadLoop(d *datachannel.DataChannel) {
}

// WriteLoop shows how to write to the datachannel directly
func WriteLoop(d *datachannel.DataChannel) {
func WriteLoop(d io.Writer) {
for range time.NewTicker(5 * time.Second).C {
message := signal.RandSeq(messageSize)
fmt.Printf("Sending %s \n", message)
Expand Down
7 changes: 4 additions & 3 deletions examples/data-channels-detach/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"io"
"time"

"github.com/pions/datachannel"
Expand Down Expand Up @@ -49,11 +50,11 @@ func main() {

// Register data channel creation handling
peerConnection.OnDataChannel(func(d *webrtc.DataChannel) {
fmt.Printf("New DataChannel %s %d\n", d.Label, d.ID)
fmt.Printf("New DataChannel %s %d\n", d.Label(), d.ID())

// Register channel opening handling
d.OnOpen(func() {
fmt.Printf("Data channel '%s'-'%d' open.\n", d.Label, d.ID)
fmt.Printf("Data channel '%s'-'%d' open.\n", d.Label(), d.ID())

// Detach the data channel
raw, dErr := d.Detach()
Expand Down Expand Up @@ -113,7 +114,7 @@ func ReadLoop(d *datachannel.DataChannel) {
}

// WriteLoop shows how to write to the datachannel directly
func WriteLoop(d *datachannel.DataChannel) {
func WriteLoop(d io.Writer) {
for range time.NewTicker(5 * time.Second).C {
message := signal.RandSeq(messageSize)
fmt.Printf("Sending %s \n", message)
Expand Down
10 changes: 5 additions & 5 deletions examples/data-channels/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,27 @@ func main() {

// Register data channel creation handling
peerConnection.OnDataChannel(func(d *webrtc.DataChannel) {
fmt.Printf("New DataChannel %s %d\n", d.Label, d.ID)
fmt.Printf("New DataChannel %s %d\n", d.Label(), d.ID())

// Register channel opening handling
d.OnOpen(func() {
fmt.Printf("Data channel '%s'-'%d' open. Random messages will now be sent to any connected DataChannels every 5 seconds\n", d.Label, d.ID)
fmt.Printf("Data channel '%s'-'%d' open. Random messages will now be sent to any connected DataChannels every 5 seconds\n", d.Label(), d.ID())

for range time.NewTicker(5 * time.Second).C {
message := signal.RandSeq(15)
fmt.Printf("Sending '%s'\n", message)

// Send the message as text
err := d.SendText(message)
if err != nil {
sendErr := d.SendText(message)
if sendErr != nil {
panic(err)
}
}
})

// Register text message handling
d.OnMessage(func(msg webrtc.DataChannelMessage) {
fmt.Printf("Message from DataChannel '%s': '%s'\n", d.Label, string(msg.Data))
fmt.Printf("Message from DataChannel '%s': '%s'\n", d.Label(), string(msg.Data))
})
})

Expand Down
17 changes: 14 additions & 3 deletions examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,19 @@ func serve(addr string) error {
exampleType == "js",
}

temp.Execute(w, data)
err = temp.Execute(w, data)
if err != nil {
panic(err)
}
return
}
}

// Serve the main page
homeTemplate.Execute(w, examples)
err = homeTemplate.Execute(w, examples)
if err != nil {
panic(err)
}
})

// Start the server
Expand All @@ -109,7 +115,12 @@ func getExamples() (*Examples, error) {
if err != nil {
return nil, fmt.Errorf("failed to list examples (please run in the examples folder): %v", err)
}
defer file.Close()
defer func() {
closeErr := file.Close()
if closeErr != nil {
panic(closeErr)
}
}()

var examples Examples
err = json.NewDecoder(file).Decode(&examples)
Expand Down
10 changes: 5 additions & 5 deletions examples/gstreamer-receive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func gstreamerReceiveMain() {
go func() {
ticker := time.NewTicker(time.Second * 3)
for range ticker.C {
err := peerConnection.SendRTCP(&rtcp.PictureLossIndication{MediaSSRC: track.SSRC()})
if err != nil {
fmt.Println(err)
rtcpSendErr := peerConnection.SendRTCP(&rtcp.PictureLossIndication{MediaSSRC: track.SSRC()})
if rtcpSendErr != nil {
fmt.Println(rtcpSendErr)
}
}
}()
Expand All @@ -53,8 +53,8 @@ func gstreamerReceiveMain() {
pipeline.Start()
buf := make([]byte, 1400)
for {
i, err := track.Read(buf)
if err != nil {
i, readErr := track.Read(buf)
if readErr != nil {
panic(err)
}

Expand Down
2 changes: 1 addition & 1 deletion examples/internal/gstreamer-sink/gst.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ func (p *Pipeline) Stop() {
// Push pushes a buffer on the appsrc of the GStreamer Pipeline
func (p *Pipeline) Push(buffer []byte) {
b := C.CBytes(buffer)
defer C.free(unsafe.Pointer(b))
defer C.free(b)
C.gstreamer_receive_push_buffer(p.Pipeline, b, C.int(len(buffer)))
}
15 changes: 2 additions & 13 deletions examples/internal/gstreamer-src/gst.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ func init() {

// Pipeline is a wrapper for a GStreamer Pipeline
type Pipeline struct {
Pipeline *C.GstElement
track *webrtc.Track
// stop acts as a signal that this pipeline is stopped
// any pending sends to Pipeline.in should be cancelled
stop chan interface{}
Pipeline *C.GstElement
track *webrtc.Track
id int
codecName string
}
Expand Down Expand Up @@ -71,17 +68,11 @@ func CreatePipeline(codecName string, track *webrtc.Track, pipelineSrc string) *

// Start starts the GStreamer Pipeline
func (p *Pipeline) Start() {
// This will signal to goHandlePipelineBuffer
// and provide a method for cancelling sends.
p.stop = make(chan interface{})
C.gstreamer_send_start_pipeline(p.Pipeline, C.int(p.id))
}

// Stop stops the GStreamer Pipeline
func (p *Pipeline) Stop() {
// To run gstreamer_send_stop_pipeline we need to make sure
// that appsink isn't being hung by any goHandlePipelineBuffers
close(p.stop)
C.gstreamer_send_stop_pipeline(p.Pipeline)
}

Expand All @@ -103,8 +94,6 @@ func goHandlePipelineBuffer(buffer unsafe.Pointer, bufferLen C.int, duration C.i
} else {
samples = uint32(videoClockRate * (float32(duration) / 1000000000))
}
// We need to be able to cancel this function even f pipeline.in isn't being serviced
// When pipeline.stop is closed the sending of data will be cancelled.
if err := pipeline.track.WriteSample(media.Sample{Data: C.GoBytes(buffer, bufferLen), Samples: samples}); err != nil {
panic(err)
}
Expand Down
18 changes: 9 additions & 9 deletions examples/janus-gateway/streaming/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,16 @@ func main() {
codec := track.Codec()
if codec.Name == webrtc.Opus {
fmt.Println("Got Opus track, saving to disk as output.opus")
i, err := opuswriter.New("output.opus", codec.ClockRate, codec.Channels)
if err != nil {
panic(err)
i, opusNewErr := opuswriter.New("output.opus", codec.ClockRate, codec.Channels)
if opusNewErr != nil {
panic(opusNewErr)
}
saveToDisk(i, track)
} else if codec.Name == webrtc.VP8 {
fmt.Println("Got VP8 track, saving to disk as output.ivf")
i, err := ivfwriter.New("output.ivf")
if err != nil {
panic(err)
i, ivfNewErr := ivfwriter.New("output.ivf")
if ivfNewErr != nil {
panic(ivfNewErr)
}
saveToDisk(i, track)
}
Expand Down Expand Up @@ -137,9 +137,9 @@ func main() {
panic(err)
}

answer, err := peerConnection.CreateAnswer(nil)
if err != nil {
panic(err)
answer, answerErr := peerConnection.CreateAnswer(nil)
if answerErr != nil {
panic(answerErr)
}

err = peerConnection.SetLocalDescription(answer)
Expand Down
8 changes: 4 additions & 4 deletions examples/ortc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ func main() {

// Handle incoming data channels
sctp.OnDataChannel(func(channel *webrtc.DataChannel) {
fmt.Printf("New DataChannel %s %d\n", channel.Label, channel.ID)
fmt.Printf("New DataChannel %s %d\n", channel.Label(), channel.ID())

// Register the handlers
channel.OnOpen(handleOnOpen(channel))
channel.OnMessage(func(msg webrtc.DataChannelMessage) {
fmt.Printf("Message from DataChannel '%s': '%s'\n", channel.Label, string(msg.Data))
fmt.Printf("Message from DataChannel '%s': '%s'\n", channel.Label(), string(msg.Data))
})
})

Expand Down Expand Up @@ -131,7 +131,7 @@ func main() {
// channel.OnOpen(handleOnOpen(channel)) // TODO: OnOpen on handle ChannelAck
go handleOnOpen(channel)() // Temporary alternative
channel.OnMessage(func(msg webrtc.DataChannelMessage) {
fmt.Printf("Message from DataChannel '%s': '%s'\n", channel.Label, string(msg.Data))
fmt.Printf("Message from DataChannel '%s': '%s'\n", channel.Label(), string(msg.Data))
})
}

Expand All @@ -150,7 +150,7 @@ type Signal struct {

func handleOnOpen(channel *webrtc.DataChannel) func() {
return func() {
fmt.Printf("Data channel '%s'-'%d' open. Random messages will now be sent to any connected DataChannels every 5 seconds\n", channel.Label, channel.ID)
fmt.Printf("Data channel '%s'-'%d' open. Random messages will now be sent to any connected DataChannels every 5 seconds\n", channel.Label(), channel.ID())

for range time.NewTicker(5 * time.Second).C {
message := signal.RandSeq(15)
Expand Down
16 changes: 9 additions & 7 deletions examples/pion-to-pion/answer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,27 @@ func main() {

// Register data channel creation handling
peerConnection.OnDataChannel(func(d *webrtc.DataChannel) {
fmt.Printf("New DataChannel %s %d\n", d.Label, d.ID)
fmt.Printf("New DataChannel %s %d\n", d.Label(), d.ID())

// Register channel opening handling
d.OnOpen(func() {
fmt.Printf("Data channel '%s'-'%d' open. Random messages will now be sent to any connected DataChannels every 5 seconds\n", d.Label, d.ID)
fmt.Printf("Data channel '%s'-'%d' open. Random messages will now be sent to any connected DataChannels every 5 seconds\n", d.Label(), d.ID())

for range time.NewTicker(5 * time.Second).C {
message := signal.RandSeq(15)
fmt.Printf("Sending '%s'\n", message)

// Send the message as text
err := d.SendText(message)
if err != nil {
panic(err)
sendTextErr := d.SendText(message)
if sendTextErr != nil {
panic(sendTextErr)
}
}
})

// Register text message handling
d.OnMessage(func(msg webrtc.DataChannelMessage) {
fmt.Printf("Message from DataChannel '%s': '%s'\n", d.Label, string(msg.Data))
fmt.Printf("Message from DataChannel '%s': '%s'\n", d.Label(), string(msg.Data))
})
})

Expand Down Expand Up @@ -117,7 +117,9 @@ func mustSignalViaHTTP(address string) (offerOut chan webrtc.SessionDescription,

})

go http.ListenAndServe(address, nil)
go func() {
panic(http.ListenAndServe(address, nil))
}()
fmt.Println("Listening on", address)

return
Expand Down

0 comments on commit 9fe44cd

Please sign in to comment.