mirror of
https://github.com/pion/webrtc.git
synced 2025-10-05 15:16:52 +08:00
New Track API
The Pion WebRTC API has been dramatically redesigned. The design docs are located here [0] You can also read the release notes [1] on how to migrate your application. [0] https://github.com/pion/webrtc-v3-design [1] https://github.com/pion/webrtc/wiki/Release-WebRTC@v3.0.0
This commit is contained in:
@@ -13,7 +13,7 @@ import (
|
||||
"github.com/pion/webrtc/v3/pkg/media/oggwriter"
|
||||
)
|
||||
|
||||
func saveToDisk(i media.Writer, track *webrtc.Track) {
|
||||
func saveToDisk(i media.Writer, track *webrtc.TrackRemote) {
|
||||
defer func() {
|
||||
if err := i.Close(); err != nil {
|
||||
panic(err)
|
||||
@@ -32,19 +32,29 @@ func saveToDisk(i media.Writer, track *webrtc.Track) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Everything below is the Pion WebRTC API! Thanks for using it ❤️.
|
||||
|
||||
// Create a MediaEngine object to configure the supported codec
|
||||
m := webrtc.MediaEngine{}
|
||||
|
||||
// Setup the codecs you want to use.
|
||||
// We'll use a VP8 codec but you can also define your own
|
||||
m.RegisterCodec(webrtc.NewRTPOpusCodec(webrtc.DefaultPayloadTypeOpus, 48000))
|
||||
m.RegisterCodec(webrtc.NewRTPVP8Codec(webrtc.DefaultPayloadTypeVP8, 90000))
|
||||
// We'll use a VP8 and Opus but you can also define your own
|
||||
if err := m.RegisterCodec(webrtc.RTPCodecParameters{
|
||||
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "video/VP8", ClockRate: 90000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil},
|
||||
PayloadType: 96,
|
||||
}, webrtc.RTPCodecTypeVideo); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := m.RegisterCodec(webrtc.RTPCodecParameters{
|
||||
RTPCodecCapability: webrtc.RTPCodecCapability{MimeType: "audio/opus", ClockRate: 48000, Channels: 0, SDPFmtpLine: "", RTCPFeedback: nil},
|
||||
PayloadType: 111,
|
||||
}, webrtc.RTPCodecTypeAudio); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Create the API object with the MediaEngine
|
||||
api := webrtc.NewAPI(webrtc.WithMediaEngine(m))
|
||||
|
||||
// Everything below is the Pion WebRTC API! Thanks for using it ❤️.
|
||||
|
||||
// Prepare the configuration
|
||||
config := webrtc.Configuration{
|
||||
ICEServers: []webrtc.ICEServer{
|
||||
@@ -79,12 +89,12 @@ func main() {
|
||||
// Set a handler for when a new remote track starts, this handler saves buffers to disk as
|
||||
// an ivf file, since we could have multiple video tracks we provide a counter.
|
||||
// In your application this is where you would handle/process video
|
||||
peerConnection.OnTrack(func(track *webrtc.Track, receiver *webrtc.RTPReceiver) {
|
||||
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 * 3)
|
||||
for range ticker.C {
|
||||
errSend := peerConnection.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: track.SSRC()}})
|
||||
errSend := peerConnection.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: uint32(track.SSRC())}})
|
||||
if errSend != nil {
|
||||
fmt.Println(errSend)
|
||||
}
|
||||
@@ -92,10 +102,10 @@ func main() {
|
||||
}()
|
||||
|
||||
codec := track.Codec()
|
||||
if codec.Name == webrtc.Opus {
|
||||
if codec.MimeType == "audio/opus" {
|
||||
fmt.Println("Got Opus track, saving to disk as output.opus (48 kHz, 2 channels)")
|
||||
saveToDisk(oggFile, track)
|
||||
} else if codec.Name == webrtc.VP8 {
|
||||
} else if codec.MimeType == "video/VP8" {
|
||||
fmt.Println("Got VP8 track, saving to disk as output.ivf")
|
||||
saveToDisk(ivfFile, track)
|
||||
}
|
||||
|
Reference in New Issue
Block a user