pion实现录制WebRTC流_pion webrtc 哪有教程

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)
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值