mirror of
https://github.com/langhuihui/monibuca.git
synced 2025-09-26 23:05:55 +08:00
doc: optimize resuse
This commit is contained in:
@@ -321,27 +321,40 @@ func (r *VideoFrame) Demux() error {
|
|||||||
```
|
```
|
||||||
|
|
||||||
**Scenario 2: SEI Insertion Processing**
|
**Scenario 2: SEI Insertion Processing**
|
||||||
|
|
||||||
|
SEI insertion achieves efficient processing through object reuse:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (t *Transformer) processVideo(video *format.H26xFrame) {
|
func (t *Transformer) Run() (err error) {
|
||||||
nalus := writer.VideoFrame.GetNalus()
|
allocator := util.NewScalableMemoryAllocator(1 << util.MinPowerOf2)
|
||||||
|
defer allocator.Recycle()
|
||||||
|
writer := m7s.NewPublisherWriter[*format.RawAudio, *format.H26xFrame](pub, allocator)
|
||||||
|
|
||||||
for nalu := range video.Raw.(*pkg.Nalus).RangePoint {
|
return m7s.PlayBlock(t.TransformJob.Subscriber,
|
||||||
// Get reused NALU object
|
func(video *format.H26xFrame) (err error) {
|
||||||
p := nalus.GetNextPointer() // Reuse object, auto Reset()
|
nalus := writer.VideoFrame.GetNalus() // Reuse NALU array
|
||||||
mem := writer.VideoFrame.NextN(nalu.Size)
|
|
||||||
nalu.CopyTo(mem)
|
// Process each NALU, reuse NALU objects
|
||||||
|
for nalu := range video.Raw.(*pkg.Nalus).RangePoint {
|
||||||
// Handle SEI insertion
|
p := nalus.GetNextPointer() // Reuse object, auto Reset()
|
||||||
if seiCount > 0 {
|
mem := writer.VideoFrame.NextN(nalu.Size)
|
||||||
for _, sei := range seis {
|
nalu.CopyTo(mem)
|
||||||
p.Push(append([]byte{byte(codec.NALU_SEI)}, sei...))
|
|
||||||
|
// Insert SEI data
|
||||||
|
if len(seis) > 0 {
|
||||||
|
for _, sei := range seis {
|
||||||
|
p.Push(append([]byte{byte(codec.NALU_SEI)}, sei...))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
p.PushOne(mem)
|
||||||
}
|
}
|
||||||
}
|
return writer.NextVideo() // Reuse VideoFrame object
|
||||||
p.PushOne(mem)
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**Key Advantage**: Through `nalus.GetNextPointer()` reusing NALU objects, avoiding creating new objects for each NALU, significantly reducing GC pressure.
|
||||||
|
|
||||||
**Scenario 3: RTP Packet Processing**
|
**Scenario 3: RTP Packet Processing**
|
||||||
```go
|
```go
|
||||||
func (r *VideoFrame) Demux() error {
|
func (r *VideoFrame) Demux() error {
|
||||||
|
@@ -321,27 +321,40 @@ func (r *VideoFrame) Demux() error {
|
|||||||
```
|
```
|
||||||
|
|
||||||
**场景2:SEI插入处理**
|
**场景2:SEI插入处理**
|
||||||
|
|
||||||
|
SEI插入通过对象复用实现高效处理:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func (t *Transformer) processVideo(video *format.H26xFrame) {
|
func (t *Transformer) Run() (err error) {
|
||||||
nalus := writer.VideoFrame.GetNalus()
|
allocator := util.NewScalableMemoryAllocator(1 << util.MinPowerOf2)
|
||||||
|
defer allocator.Recycle()
|
||||||
|
writer := m7s.NewPublisherWriter[*format.RawAudio, *format.H26xFrame](pub, allocator)
|
||||||
|
|
||||||
for nalu := range video.Raw.(*pkg.Nalus).RangePoint {
|
return m7s.PlayBlock(t.TransformJob.Subscriber,
|
||||||
// 获取复用的NALU对象
|
func(video *format.H26xFrame) (err error) {
|
||||||
p := nalus.GetNextPointer() // 复用对象,自动Reset()
|
nalus := writer.VideoFrame.GetNalus() // 复用NALU数组
|
||||||
mem := writer.VideoFrame.NextN(nalu.Size)
|
|
||||||
nalu.CopyTo(mem)
|
// 处理每个NALU,复用NALU对象
|
||||||
|
for nalu := range video.Raw.(*pkg.Nalus).RangePoint {
|
||||||
// 处理SEI插入
|
p := nalus.GetNextPointer() // 复用对象,自动Reset()
|
||||||
if seiCount > 0 {
|
mem := writer.VideoFrame.NextN(nalu.Size)
|
||||||
for _, sei := range seis {
|
nalu.CopyTo(mem)
|
||||||
p.Push(append([]byte{byte(codec.NALU_SEI)}, sei...))
|
|
||||||
|
// 插入SEI数据
|
||||||
|
if len(seis) > 0 {
|
||||||
|
for _, sei := range seis {
|
||||||
|
p.Push(append([]byte{byte(codec.NALU_SEI)}, sei...))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
p.PushOne(mem)
|
||||||
}
|
}
|
||||||
}
|
return writer.NextVideo() // 复用VideoFrame对象
|
||||||
p.PushOne(mem)
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**关键优势**:通过`nalus.GetNextPointer()`复用NALU对象,避免为每个NALU创建新对象,显著降低GC压力。
|
||||||
|
|
||||||
**场景3:RTP包处理**
|
**场景3:RTP包处理**
|
||||||
```go
|
```go
|
||||||
func (r *VideoFrame) Demux() error {
|
func (r *VideoFrame) Demux() error {
|
||||||
@@ -697,7 +710,6 @@ func pullFLVNew(publisher *Publisher, file *os.File) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## 7. 总结
|
## 7. 总结
|
||||||
|
|
||||||
### 7.1 核心优势
|
### 7.1 核心优势
|
||||||
@@ -724,4 +736,4 @@ func pullFLVNew(publisher *Publisher, file *os.File) {
|
|||||||
- 高频数据处理
|
- 高频数据处理
|
||||||
- 对延迟敏感的应用
|
- 对延迟敏感的应用
|
||||||
|
|
||||||
通过合理应用这些技术,可以显著提升系统的性能和稳定性,为高并发、低延迟的流媒体应用提供坚实的技术基础。
|
通过合理应用这些技术,可以显著提升系统的性能和稳定性,为高并发、低延迟的流媒体应用提供坚实的技术基础。
|
||||||
|
Reference in New Issue
Block a user