Skip to content

Commit

Permalink
Remove SampleBuilder from rtp-to-webrtc
Browse files Browse the repository at this point in the history
SampleBuilder isn't able to properly handle H264. We have multiple
issues and until resolved we shouldn't suggest it.

Relates to pion#1652
  • Loading branch information
Sean-Der committed Jan 30, 2021
1 parent f4b3d98 commit daf27bd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 22 deletions.
6 changes: 6 additions & 0 deletions examples/rtp-to-webrtc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@ Copy the text that `rtp-to-webrtc` just emitted and copy into second text area
A video should start playing in your browser above the input boxes.

Congrats, you have used Pion WebRTC! Now start building something cool

## Dealing with broken/lossy inputs
Pion WebRTC also provides a [SampleBuilder](https://pkg.go.dev/github.com/pion/webrtc/v3@v3.0.4/pkg/media/samplebuilder). This consumes RTP packets and returns samples.
It can be used to re-order and delay for lossy streams. You can see its usage in this example in [e4da70](https://github.com/pion/webrtc/commit/e4da709754639871266ca5e2cc2f945178caeb12).

Currently it isn't working with H264, but is useful for VP8 and Opus. See [#1652](https://github.com/pion/webrtc/issues/1652) for the status of fixing for H264.
25 changes: 3 additions & 22 deletions examples/rtp-to-webrtc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import (
"fmt"
"net"

"github.com/pion/rtp"
"github.com/pion/rtp/codecs"
"github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3/examples/internal/signal"
"github.com/pion/webrtc/v3/pkg/media/samplebuilder"
)

func main() {
Expand All @@ -37,7 +34,7 @@ func main() {
}()

// Create a video track
videoTrack, err := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: "video/vp8"}, "video", "pion")
videoTrack, err := webrtc.NewTrackLocalStaticRTP(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8}, "video", "pion")
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -95,32 +92,16 @@ func main() {
// Output the answer in base64 so we can paste it in browser
fmt.Println(signal.Encode(*peerConnection.LocalDescription()))

videoBuilder := samplebuilder.New(10, &codecs.VP8Packet{}, 90000)

// Read RTP packets forever and send them to the WebRTC Client
inboundRTPPacket := make([]byte, 1600) // UDP MTU
for {
inboundRTPPacket := make([]byte, 1500) // UDP MTU
packet := &rtp.Packet{}

n, _, err := listener.ReadFrom(inboundRTPPacket)
if err != nil {
panic(fmt.Sprintf("error during read: %s", err))
}

if err = packet.Unmarshal(inboundRTPPacket[:n]); err != nil {
if _, err = videoTrack.Write(inboundRTPPacket[:n]); err != nil {
panic(err)
}

videoBuilder.Push(packet)
for {
sample := videoBuilder.Pop()
if sample == nil {
break
}

if writeErr := videoTrack.WriteSample(*sample); writeErr != nil {
panic(writeErr)
}
}
}
}

0 comments on commit daf27bd

Please sign in to comment.