Update streamer NewTrack function

This commit is contained in:
Alexey Khit
2023-03-04 06:17:04 +03:00
parent c2cdf60ffc
commit 5aa20f0845
17 changed files with 73 additions and 24 deletions
+11 -2
View File
@@ -78,8 +78,17 @@ func (m *Media) MarshalJSON() ([]byte, error) {
}
func (m *Media) Clone() *Media {
clone := *m
return &clone
clone := &Media{
Kind: m.Kind,
Direction: m.Direction,
Codecs: make([]*Codec, len(m.Codecs)),
MID: m.MID,
Control: m.Control,
}
for i, codec := range m.Codecs {
clone.Codecs[i] = codec.Clone()
}
return clone
}
func (m *Media) AV() bool {
+21
View File
@@ -1,8 +1,10 @@
package streamer
import (
"fmt"
"github.com/pion/sdp/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/url"
"testing"
)
@@ -40,3 +42,22 @@ func TestParseQuery(t *testing.T) {
}, medias)
}
}
func TestClone(t *testing.T) {
media1 := &Media{
Kind: KindVideo,
Direction: DirectionRecvonly,
Codecs: []*Codec{
{Name: CodecPCMU, ClockRate: 8000},
},
}
media2 := media1.Clone()
p1 := fmt.Sprintf("%p", media1)
p2 := fmt.Sprintf("%p", media2)
require.NotEqualValues(t, p1, p2)
p3 := fmt.Sprintf("%p", media1.Codecs[0])
p4 := fmt.Sprintf("%p", media2.Codecs[0])
require.NotEqualValues(t, p3, p4)
}
+19
View File
@@ -1,3 +1,22 @@
// Package streamer
//
// 1. Consumer.GetMedias - return list of Media, that Consumer can play/load/consume:
// - Media with DirectionRecvonly for audio/video
// - Media with DirectionSendonly for backchannel
//
// 2. Producer.GetMedias - return list of Media, that Producer can generate/create/produce
// - Media with DirectionSendonly for audio/video
// - Media with DirectionRecvonly for backchannel
//
// 3. Producer.GetTrack - get Media from Producer and Codec from that Media return Track from Producer:
// - Media with DirectionSendonly should Track.WriteRTP after Producer.Start
// - Media with DirectionRecvonly should Track.Bind and wait Track.WriteRTP from Consumer
//
// 4. Consumer.AddTrack - takes Media from Consumer and Track from Producer:
// - Media with DirectionRecvonly should Track.WriteRTP
// - Media with DirectionSendonly should Track.Bind
//
// 5. Producer.Start - run loop with reading rtp.Packet from source
package streamer
// States, Queries and Events
+5 -5
View File
@@ -17,11 +17,7 @@ type Track struct {
sinkMu *sync.RWMutex
}
func NewTrack(codec *Codec, direction string) *Track {
return &Track{Codec: codec, Direction: direction, sinkMu: new(sync.RWMutex)}
}
func NewTrack2(media *Media, codec *Codec) *Track {
func NewTrack(media *Media, codec *Codec) *Track {
if codec == nil {
codec = media.Codecs[0]
}
@@ -55,6 +51,8 @@ func (t *Track) WriteRTP(p *rtp.Packet) error {
return nil
}
// Bind - attach WriterFunc (Consumer) for receiving rtp.Packet(s)
// and return new Track copy. Later you can run Unbind for new Track
func (t *Track) Bind(w WriterFunc) *Track {
t.sinkMu.Lock()
@@ -70,6 +68,8 @@ func (t *Track) Bind(w WriterFunc) *Track {
return &clone
}
// Unbind - detach WriterFunc that related to this Track from
// consuming track data
func (t *Track) Unbind() {
t.sinkMu.Lock()
delete(t.sink, t)