mirror of
https://github.com/pion/mediadevices.git
synced 2025-10-27 18:40:51 +08:00
Add rtp-send example
This commit is contained in:
committed by
Lukas Herman
parent
6b32f6eac2
commit
f0cd710090
23
examples/rtp-send/README.md
Normal file
23
examples/rtp-send/README.md
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
## Instructions
|
||||||
|
|
||||||
|
### Download rtp-send example
|
||||||
|
|
||||||
|
```
|
||||||
|
go get github.com/pion/mediadevices/examples/rtp-send
|
||||||
|
```
|
||||||
|
|
||||||
|
### Listen RTP
|
||||||
|
|
||||||
|
Install GStreamer and run:
|
||||||
|
```
|
||||||
|
gst-launch-1.0 udpsrc port=5000 ! rtpvp8depay ! vp8dec ! videoconvert ! autovideosink
|
||||||
|
```
|
||||||
|
|
||||||
|
### Run rtp-send
|
||||||
|
|
||||||
|
Run `rtp-send localhost:5000`
|
||||||
|
|
||||||
|
A video should start playing in your GStreamer window.
|
||||||
|
It's not WebRTC, but pure RTP.
|
||||||
|
|
||||||
|
Congrats, you have used pion-MediaDevices! Now start building something cool
|
||||||
114
examples/rtp-send/main.go
Normal file
114
examples/rtp-send/main.go
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/pion/mediadevices"
|
||||||
|
_ "github.com/pion/mediadevices/pkg/codec/openh264" // This is required to register h264 video encoder
|
||||||
|
_ "github.com/pion/mediadevices/pkg/codec/vpx" // This is required to register VP8/VP9 video encoder
|
||||||
|
"github.com/pion/mediadevices/pkg/frame"
|
||||||
|
"github.com/pion/rtp"
|
||||||
|
"github.com/pion/webrtc/v2"
|
||||||
|
"github.com/pion/webrtc/v2/pkg/media"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
videoCodecName = webrtc.VP8
|
||||||
|
mtu = 1000
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if len(os.Args) != 2 {
|
||||||
|
fmt.Printf("usage: %s host:port\n", os.Args[0])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
md := mediadevices.NewMediaDevicesFromCodecs(
|
||||||
|
map[webrtc.RTPCodecType][]*webrtc.RTPCodec{
|
||||||
|
webrtc.RTPCodecTypeVideo: []*webrtc.RTPCodec{
|
||||||
|
webrtc.NewRTPVP8Codec(100, 90000),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
mediadevices.WithTrackGenerator(
|
||||||
|
func(_ uint8, _ uint32, id, _ string, codec *webrtc.RTPCodec) (
|
||||||
|
mediadevices.LocalTrack, error,
|
||||||
|
) {
|
||||||
|
return newTrack(codec, id, os.Args[1]), nil
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
_, err := md.GetUserMedia(mediadevices.MediaStreamConstraints{
|
||||||
|
Video: func(c *mediadevices.MediaTrackConstraints) {
|
||||||
|
c.CodecName = videoCodecName
|
||||||
|
c.FrameFormat = frame.FormatYUY2
|
||||||
|
c.Enabled = true
|
||||||
|
c.Width = 640
|
||||||
|
c.Height = 480
|
||||||
|
c.BitRate = 100000 // 100kbps
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
select {}
|
||||||
|
}
|
||||||
|
|
||||||
|
type track struct {
|
||||||
|
codec *webrtc.RTPCodec
|
||||||
|
packetizer rtp.Packetizer
|
||||||
|
id string
|
||||||
|
conn net.Conn
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTrack(codec *webrtc.RTPCodec, id, dest string) *track {
|
||||||
|
addr, err := net.ResolveUDPAddr("udp", dest)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
conn, err := net.DialUDP("udp", nil, addr)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return &track{
|
||||||
|
codec: codec,
|
||||||
|
packetizer: rtp.NewPacketizer(
|
||||||
|
mtu,
|
||||||
|
codec.PayloadType,
|
||||||
|
1,
|
||||||
|
codec.Payloader,
|
||||||
|
rtp.NewRandomSequencer(),
|
||||||
|
codec.ClockRate,
|
||||||
|
),
|
||||||
|
id: id,
|
||||||
|
conn: conn,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *track) WriteSample(s media.Sample) error {
|
||||||
|
buf := make([]byte, mtu)
|
||||||
|
pkts := t.packetizer.Packetize(s.Data, s.Samples)
|
||||||
|
for _, p := range pkts {
|
||||||
|
n, err := p.MarshalTo(buf)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
_, _ = t.conn.Write(buf[:n])
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *track) Codec() *webrtc.RTPCodec {
|
||||||
|
return t.codec
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *track) ID() string {
|
||||||
|
return t.id
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *track) Kind() webrtc.RTPCodecType {
|
||||||
|
return t.codec.Type
|
||||||
|
}
|
||||||
1
go.mod
1
go.mod
@@ -6,6 +6,7 @@ require (
|
|||||||
github.com/blackjack/webcam v0.0.0-20191123110216-08fa32efcb67
|
github.com/blackjack/webcam v0.0.0-20191123110216-08fa32efcb67
|
||||||
github.com/faiface/beep v1.0.2
|
github.com/faiface/beep v1.0.2
|
||||||
github.com/jfreymuth/pulse v0.0.0-20200118113426-7cf5f487291e
|
github.com/jfreymuth/pulse v0.0.0-20200118113426-7cf5f487291e
|
||||||
|
github.com/pion/rtp v1.2.0
|
||||||
github.com/pion/webrtc/v2 v2.1.19-0.20200106051345-726a16faa60d
|
github.com/pion/webrtc/v2 v2.1.19-0.20200106051345-726a16faa60d
|
||||||
github.com/satori/go.uuid v1.2.0
|
github.com/satori/go.uuid v1.2.0
|
||||||
gopkg.in/hraban/opus.v2 v2.0.0-20191117073431-57179dff69a6
|
gopkg.in/hraban/opus.v2 v2.0.0-20191117073431-57179dff69a6
|
||||||
|
|||||||
Reference in New Issue
Block a user