diff --git a/decoder.go b/decoder.go index 1c4f00a..72adb41 100644 --- a/decoder.go +++ b/decoder.go @@ -7,6 +7,7 @@ package rtsp import "C" import ( "fmt" + "runtime" "unsafe" ) @@ -26,6 +27,8 @@ func decodeAVPacket(packet *C.AVPacket) (data []byte) { func newDecoder(cstream *C.AVStream) (*decoder, error) { decoder := &decoder{index: int(cstream.index)} + runtime.SetFinalizer(decoder, freeDecoder) + decoder.swrContext = nil decoder.codecCtx = C.avcodec_alloc_context3(nil) C.avcodec_parameters_to_context(decoder.codecCtx, cstream.codecpar) @@ -41,6 +44,21 @@ func newDecoder(cstream *C.AVStream) (*decoder, error) { 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) { pkt = &Packet{} diff --git a/stream.go b/stream.go index 023c744..8e2b751 100644 --- a/stream.go +++ b/stream.go @@ -41,34 +41,12 @@ func free(stream *Stream) { if stream.formatCtx != nil { C.avformat_free_context(stream.formatCtx) stream.formatCtx = nil - } if stream.dictionary != nil { C.av_dict_free(&stream.dictionary) 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) {