package stream
import (
"testing"
"time"
)
func TestStream(t \*testing.T) {
stream, _ := NewStream("172.24.116.214", "5314d4e", "8402180", "D:\\workspace\\biz\\record\\")
count := 0
for count < 10 {
count++
time.Sleep(1 \* time.Second)
}
stream.Stop()
}
stream.go
package stream
import (
"context"
"fmt"
"github.com/pion/interceptor"
"github.com/pion/sdp/v3"
"github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3/pkg/media"
"github.com/pion/webrtc/v3/pkg/media/h264writer"
"github.com/pion/webrtc/v3/pkg/media/oggwriter"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"os/exec"
"sync"
)
var Streams sync.Map
func Find(host, room string) bool {
key := "webrtc://" + host + "/" + room
if _, ok := Streams.Load(key); ok {
return true
}
return false
}
func LoadAndDelStream(host, room string) \*Stream {
key := "webrtc://" + host + "/" + room
if v, ok := Streams.LoadAndDelete(key); ok {
if stream, ok := v.(\*Stream); ok {
return stream
}
return nil
}
return nil
}
type Stream struct {
Host string
Room string
Display string
rtcUrl string
savePath string
ctx context.Context
cancel context.CancelFunc
pc \*webrtc.PeerConnection
hasAudioTrack bool
hasVideoTrack bool
videoFinish chan struct{}
audioFinish chan struct{}
}
func (self \*Stream) onTrack(track \*webrtc.TrackRemote, receiver \*webrtc.RTPReceiver) error {
// Send a PLI on an interval so that the publisher is pushing a keyframe
codec := track.Codec()
trackDesc := fmt.Sprintf("channels=%v", codec.Channels)
if track.Kind() == webrtc.RTPCodecTypeVideo {
trackDesc = fmt.Sprintf("fmtp=%v", codec.SDPFmtpLine)