mirror of
https://github.com/Danile71/go-rtsp.git
synced 2025-10-05 16:16:54 +08:00
split finalizer for stream and decoder
This commit is contained in:
18
decoder.go
18
decoder.go
@@ -7,6 +7,7 @@ package rtsp
|
|||||||
import "C"
|
import "C"
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"runtime"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -26,6 +27,8 @@ func decodeAVPacket(packet *C.AVPacket) (data []byte) {
|
|||||||
|
|
||||||
func newDecoder(cstream *C.AVStream) (*decoder, error) {
|
func newDecoder(cstream *C.AVStream) (*decoder, error) {
|
||||||
decoder := &decoder{index: int(cstream.index)}
|
decoder := &decoder{index: int(cstream.index)}
|
||||||
|
runtime.SetFinalizer(decoder, freeDecoder)
|
||||||
|
|
||||||
decoder.swrContext = nil
|
decoder.swrContext = nil
|
||||||
decoder.codecCtx = C.avcodec_alloc_context3(nil)
|
decoder.codecCtx = C.avcodec_alloc_context3(nil)
|
||||||
C.avcodec_parameters_to_context(decoder.codecCtx, cstream.codecpar)
|
C.avcodec_parameters_to_context(decoder.codecCtx, cstream.codecpar)
|
||||||
@@ -41,6 +44,21 @@ func newDecoder(cstream *C.AVStream) (*decoder, error) {
|
|||||||
return decoder, nil
|
return decoder, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func freeDecoder(decoder *decoder) {
|
||||||
|
if decoder.codecCtx != nil {
|
||||||
|
C.avcodec_close(decoder.codecCtx)
|
||||||
|
C.av_free(unsafe.Pointer(decoder.codecCtx))
|
||||||
|
decoder.codecCtx = nil
|
||||||
|
}
|
||||||
|
if decoder.codec != nil {
|
||||||
|
decoder.codec = nil
|
||||||
|
}
|
||||||
|
if decoder.swrContext != nil {
|
||||||
|
C.swr_close(decoder.swrContext)
|
||||||
|
C.swr_free(&decoder.swrContext)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (decoder *decoder) Decode(packet *C.AVPacket) (pkt *Packet, err error) {
|
func (decoder *decoder) Decode(packet *C.AVPacket) (pkt *Packet, err error) {
|
||||||
pkt = &Packet{}
|
pkt = &Packet{}
|
||||||
|
|
||||||
|
22
stream.go
22
stream.go
@@ -41,34 +41,12 @@ func free(stream *Stream) {
|
|||||||
if stream.formatCtx != nil {
|
if stream.formatCtx != nil {
|
||||||
C.avformat_free_context(stream.formatCtx)
|
C.avformat_free_context(stream.formatCtx)
|
||||||
stream.formatCtx = nil
|
stream.formatCtx = nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if stream.dictionary != nil {
|
if stream.dictionary != nil {
|
||||||
C.av_dict_free(&stream.dictionary)
|
C.av_dict_free(&stream.dictionary)
|
||||||
stream.dictionary = nil
|
stream.dictionary = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, decoder := range stream.decoders {
|
|
||||||
if decoder != nil {
|
|
||||||
if decoder.codecCtx != nil {
|
|
||||||
C.avcodec_close(decoder.codecCtx)
|
|
||||||
C.av_free(unsafe.Pointer(decoder.codecCtx))
|
|
||||||
decoder.codecCtx = nil
|
|
||||||
}
|
|
||||||
if decoder.codec != nil {
|
|
||||||
decoder.codec = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if decoder.swrContext != nil {
|
|
||||||
C.swr_close(decoder.swrContext)
|
|
||||||
C.swr_free(&decoder.swrContext)
|
|
||||||
}
|
|
||||||
|
|
||||||
decoder = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
stream.decoders = nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (stream *Stream) Setup(t Type) (err error) {
|
func (stream *Stream) Setup(t Type) (err error) {
|
||||||
|
Reference in New Issue
Block a user