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:
@@ -7,7 +7,6 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/pion/randutil"
|
||||
"github.com/pion/webrtc/v3"
|
||||
"github.com/pion/webrtc/v3/examples/internal/signal"
|
||||
"github.com/pion/webrtc/v3/pkg/media"
|
||||
@@ -32,20 +31,8 @@ func main() {
|
||||
panic("Could not find `" + audioFileName + "` or `" + videoFileName + "`")
|
||||
}
|
||||
|
||||
// Wait for the offer to be pasted
|
||||
offer := webrtc.SessionDescription{}
|
||||
signal.Decode(signal.MustReadStdin(), &offer)
|
||||
|
||||
// We make our own mediaEngine so we can place the sender's codecs in it. This because we must use the
|
||||
// dynamic media type from the sender in our answer. This is not required if we are the offerer
|
||||
mediaEngine := webrtc.MediaEngine{}
|
||||
if err = mediaEngine.PopulateFromSDP(offer); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Create a new RTCPeerConnection
|
||||
api := webrtc.NewAPI(webrtc.WithMediaEngine(mediaEngine))
|
||||
peerConnection, err := api.NewPeerConnection(webrtc.Configuration{
|
||||
peerConnection, err := webrtc.NewPeerConnection(webrtc.Configuration{
|
||||
ICEServers: []webrtc.ICEServer{
|
||||
{
|
||||
URLs: []string{"stun:stun.l.google.com:19302"},
|
||||
@@ -59,12 +46,13 @@ func main() {
|
||||
|
||||
if haveVideoFile {
|
||||
// Create a video track
|
||||
videoTrack, addTrackErr := peerConnection.NewTrack(getPayloadType(mediaEngine, webrtc.RTPCodecTypeVideo, "VP8"), randutil.NewMathRandomGenerator().Uint32(), "video", "pion")
|
||||
if addTrackErr != nil {
|
||||
panic(addTrackErr)
|
||||
videoTrack, videoTrackErr := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: "video/vp8"}, "video", "pion")
|
||||
if videoTrackErr != nil {
|
||||
panic(videoTrackErr)
|
||||
}
|
||||
if _, addTrackErr = peerConnection.AddTrack(videoTrack); addTrackErr != nil {
|
||||
panic(addTrackErr)
|
||||
|
||||
if _, videoTrackErr = peerConnection.AddTrack(videoTrack); videoTrackErr != nil {
|
||||
panic(videoTrackErr)
|
||||
}
|
||||
|
||||
go func() {
|
||||
@@ -97,7 +85,7 @@ func main() {
|
||||
}
|
||||
|
||||
time.Sleep(sleepTime)
|
||||
if ivfErr = videoTrack.WriteSample(media.Sample{Data: frame, Samples: 90000}); ivfErr != nil {
|
||||
if ivfErr = videoTrack.WriteSample(media.Sample{Data: frame, Duration: time.Second}); ivfErr != nil {
|
||||
panic(ivfErr)
|
||||
}
|
||||
}
|
||||
@@ -106,12 +94,12 @@ func main() {
|
||||
|
||||
if haveAudioFile {
|
||||
// Create a audio track
|
||||
audioTrack, addTrackErr := peerConnection.NewTrack(getPayloadType(mediaEngine, webrtc.RTPCodecTypeAudio, "opus"), randutil.NewMathRandomGenerator().Uint32(), "audio", "pion")
|
||||
if addTrackErr != nil {
|
||||
panic(addTrackErr)
|
||||
audioTrack, audioTrackErr := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: "audio/opus"}, "audio", "pion")
|
||||
if audioTrackErr != nil {
|
||||
panic(audioTrackErr)
|
||||
}
|
||||
if _, addTrackErr = peerConnection.AddTrack(audioTrack); addTrackErr != nil {
|
||||
panic(addTrackErr)
|
||||
if _, audioTrackErr = peerConnection.AddTrack(audioTrack); audioTrackErr != nil {
|
||||
panic(audioTrackErr)
|
||||
}
|
||||
|
||||
go func() {
|
||||
@@ -146,13 +134,13 @@ func main() {
|
||||
// The amount of samples is the difference between the last and current timestamp
|
||||
sampleCount := float64(pageHeader.GranulePosition - lastGranule)
|
||||
lastGranule = pageHeader.GranulePosition
|
||||
sampleDuration := time.Duration((sampleCount/48000)*1000) * time.Millisecond
|
||||
|
||||
if oggErr = audioTrack.WriteSample(media.Sample{Data: pageData, Samples: uint32(sampleCount)}); oggErr != nil {
|
||||
if oggErr = audioTrack.WriteSample(media.Sample{Data: pageData, Duration: sampleDuration}); oggErr != nil {
|
||||
panic(oggErr)
|
||||
}
|
||||
|
||||
// Convert seconds to Milliseconds, Sleep doesn't accept floats
|
||||
time.Sleep(time.Duration((sampleCount/48000)*1000) * time.Millisecond)
|
||||
time.Sleep(sampleDuration)
|
||||
}
|
||||
}()
|
||||
}
|
||||
@@ -166,6 +154,10 @@ func main() {
|
||||
}
|
||||
})
|
||||
|
||||
// Wait for the offer to be pasted
|
||||
offer := webrtc.SessionDescription{}
|
||||
signal.Decode(signal.MustReadStdin(), &offer)
|
||||
|
||||
// Set the remote SessionDescription
|
||||
if err = peerConnection.SetRemoteDescription(offer); err != nil {
|
||||
panic(err)
|
||||
@@ -196,15 +188,3 @@ func main() {
|
||||
// Block forever
|
||||
select {}
|
||||
}
|
||||
|
||||
// Search for Codec PayloadType
|
||||
//
|
||||
// Since we are answering we need to match the remote PayloadType
|
||||
func getPayloadType(m webrtc.MediaEngine, codecType webrtc.RTPCodecType, codecName string) uint8 {
|
||||
for _, codec := range m.GetCodecsByKind(codecType) {
|
||||
if codec.Name == codecName {
|
||||
return codec.PayloadType
|
||||
}
|
||||
}
|
||||
panic(fmt.Sprintf("Remote peer does not support %s", codecName))
|
||||
}
|
||||
|
Reference in New Issue
Block a user