mirror of
https://github.com/lkmio/lkm.git
synced 2025-09-26 19:21:14 +08:00
feat: 使用引用计数器管理合并写切片的生命周期
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package rtsp
|
||||
|
||||
import (
|
||||
"github.com/lkmio/avformat/collections"
|
||||
"github.com/lkmio/avformat/utils"
|
||||
"github.com/lkmio/lkm/log"
|
||||
"github.com/lkmio/lkm/stream"
|
||||
@@ -79,7 +80,7 @@ func (s *Sink) AddSender(index int, tcp bool, ssrc uint32) (uint16, uint16, erro
|
||||
return rtpPort, rtcpPort, err
|
||||
}
|
||||
|
||||
func (s *Sink) Write(index int, data [][]byte, rtpTime int64) error {
|
||||
func (s *Sink) Write(index int, data []*collections.ReferenceCounter[[]byte], rtpTime int64) error {
|
||||
// 拉流方还没有连接上来
|
||||
if index >= cap(s.senders) || s.senders[index] == nil {
|
||||
return nil
|
||||
@@ -88,12 +89,12 @@ func (s *Sink) Write(index int, data [][]byte, rtpTime int64) error {
|
||||
for _, bytes := range data {
|
||||
sender := s.senders[index]
|
||||
sender.PktCount++
|
||||
sender.OctetCount += len(bytes)
|
||||
sender.OctetCount += len(bytes.Get())
|
||||
if s.TCPStreaming {
|
||||
s.Conn.Write(bytes)
|
||||
s.Conn.Write(bytes.Get())
|
||||
} else {
|
||||
//发送rtcp sr包
|
||||
sender.RtpConn.Write(bytes[OverTcpHeaderSize:])
|
||||
sender.RtpConn.Write(bytes.Get()[OverTcpHeaderSize:])
|
||||
|
||||
if sender.RtcpConn == nil || sender.PktCount%100 != 0 {
|
||||
continue
|
||||
|
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"github.com/lkmio/avformat"
|
||||
"github.com/lkmio/avformat/avc"
|
||||
"github.com/lkmio/avformat/collections"
|
||||
"github.com/lkmio/avformat/utils"
|
||||
"github.com/lkmio/lkm/stream"
|
||||
"github.com/lkmio/rtp"
|
||||
@@ -39,7 +40,7 @@ func (t *TransStream) OverTCP(data []byte, channel int) {
|
||||
binary.BigEndian.PutUint16(data[2:], uint16(len(data)-4))
|
||||
}
|
||||
|
||||
func (t *TransStream) Input(packet *avformat.AVPacket) ([][]byte, int64, bool, error) {
|
||||
func (t *TransStream) Input(packet *avformat.AVPacket) ([]*collections.ReferenceCounter[[]byte], int64, bool, error) {
|
||||
t.ClearOutStreamBuffer()
|
||||
|
||||
var ts uint32
|
||||
@@ -57,7 +58,9 @@ func (t *TransStream) Input(packet *avformat.AVPacket) ([][]byte, int64, bool, e
|
||||
return t.OutBuffer[:t.OutBufferSize], int64(ts), utils.AVMediaTypeVideo == packet.MediaType && packet.Key, nil
|
||||
}
|
||||
|
||||
func (t *TransStream) ReadExtraData(ts int64) ([][]byte, int64, error) {
|
||||
func (t *TransStream) ReadExtraData(ts int64) ([]*collections.ReferenceCounter[[]byte], int64, error) {
|
||||
t.ClearOutStreamBuffer()
|
||||
|
||||
// 返回视频编码数据的rtp包
|
||||
for _, track := range t.RtspTracks {
|
||||
if utils.AVMediaTypeVideo != track.MediaType {
|
||||
@@ -71,7 +74,11 @@ func (t *TransStream) ReadExtraData(ts int64) ([][]byte, int64, error) {
|
||||
binary.BigEndian.PutUint32(bytes[OverTcpHeaderSize+4:], uint32(ts))
|
||||
}
|
||||
|
||||
return track.ExtraDataBuffer, ts, nil
|
||||
for _, data := range track.ExtraDataBuffer {
|
||||
t.AppendOutStreamBuffer(collections.NewReferenceCounter(data))
|
||||
}
|
||||
|
||||
return t.OutBuffer[:t.OutBufferSize], ts, nil
|
||||
}
|
||||
|
||||
return nil, ts, nil
|
||||
@@ -91,7 +98,7 @@ func (t *TransStream) PackRtpPayload(track *Track, channel int, data []byte, tim
|
||||
|
||||
packet := t.buffer.Get(index)[:OverTcpHeaderSize+len(bytes)]
|
||||
t.OverTCP(packet, channel)
|
||||
t.AppendOutStreamBuffer(packet)
|
||||
t.AppendOutStreamBuffer(collections.NewReferenceCounter(packet))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -158,7 +165,7 @@ func (t *TransStream) AddTrack(track *stream.Track) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *TransStream) Close() ([][]byte, int64, error) {
|
||||
func (t *TransStream) Close() ([]*collections.ReferenceCounter[[]byte], int64, error) {
|
||||
for _, track := range t.RtspTracks {
|
||||
if track != nil {
|
||||
track.Close()
|
||||
|
Reference in New Issue
Block a user