Skip to content

Commit

Permalink
Removed use of av_init_packet as it is deprecated in new FFMPEG versi…
Browse files Browse the repository at this point in the history
…on (#7)

* Removed use of av_init_packet as it is deprecated in new FFMPEG version
Changed LogLevel to AV_LOG_QUIET
Added socket TCP I/O timeout value 10s

* Fix type layout in swr_alloc_set_opts
  • Loading branch information
fadagus authored May 20, 2022
1 parent 48e165a commit b9ef7be
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
8 changes: 4 additions & 4 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ func (decoder *decoder) Decode(packet *C.AVPacket) (pkt *Packet, err error) {
layout := uint64(frame.channel_layout)

decoder.swrContext = C.swr_alloc_set_opts(nil, // we're allocating a new context
C.long(layout), // out_ch_layout
C.longlong(layout), // out_ch_layout
C.AV_SAMPLE_FMT_S16, // out_sample_fmt
frame.sample_rate, // out_sample_rate

C.long(layout), // in_ch_layout
C.longlong(layout), // in_ch_layout
decoder.codecCtx.sample_fmt, // in_sample_fmt
frame.sample_rate, // in_sample_rate

Expand Down Expand Up @@ -169,11 +169,11 @@ func (decoder *decoder) Decode(packet *C.AVPacket) (pkt *Packet, err error) {
layout := uint64(frame.channel_layout)

decoder.swrContext = C.swr_alloc_set_opts(nil, // we're allocating a new context
C.long(layout), // out_ch_layout
C.longlong(layout), // out_ch_layout
C.AV_SAMPLE_FMT_S16, // out_sample_fmt
frame.sample_rate, // out_sample_rate

C.long(layout), // in_ch_layout
C.longlong(layout), // in_ch_layout
decoder.codecCtx.sample_fmt, // in_sample_fmt
frame.sample_rate, // in_sample_rate

Expand Down
4 changes: 2 additions & 2 deletions ffmpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int rtsp_encode(AVCodecContext *avctx, AVPacket *pkt, int *got_packet, AVFrame *
}

int rtsp_avcodec_encode_wav(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket *packet) {
AVCodec *wavCodec = avcodec_find_encoder(AV_CODEC_ID_PCM_S16LE );
const AVCodec *wavCodec = avcodec_find_encoder(AV_CODEC_ID_PCM_S16LE );
int ret = -1;

if (!wavCodec) {
Expand Down Expand Up @@ -76,7 +76,7 @@ int rtsp_avcodec_encode_wav(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket
}

int rtsp_avcodec_encode_jpeg(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket *packet) {
AVCodec *jpegCodec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
const AVCodec *jpegCodec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
int ret = -1;

if (!jpegCodec) {
Expand Down
22 changes: 15 additions & 7 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func New(uri string) (stream *Stream) {
stream.decoders = make(map[int]*decoder)
stream.formatCtx = C.avformat_alloc_context()

C.av_log_set_level(C.AV_LOG_QUIET)

runtime.SetFinalizer(stream, free)
return
}
Expand Down Expand Up @@ -63,7 +65,7 @@ func (e ErrTimeout) Error() string {
func CErr2Str(code C.int) string {
buf := make([]byte, 64)

C.av_strerror(code, (*C.char)(unsafe.Pointer(&buf[0])), C.ulong(len(buf)))
C.av_strerror(code, (*C.char)(unsafe.Pointer(&buf[0])), C.ulonglong(len(buf)))

return string(buf)
}
Expand All @@ -79,6 +81,14 @@ func (stream *Stream) Setup(t Type) (err error) {
udp := C.CString("udp")
defer C.free(unsafe.Pointer(udp))

timeoutKey := C.CString("timeout")
defer C.free(unsafe.Pointer(timeoutKey))

timeout := C.CString("10000000")
defer C.free(unsafe.Pointer(timeout))

C.av_dict_set(&stream.dictionary, timeoutKey, timeout, 0)

switch t {
case Tcp:
C.av_dict_set(&stream.dictionary, transport, tcp, 0)
Expand Down Expand Up @@ -127,12 +137,10 @@ func (stream *Stream) Setup(t Type) (err error) {
}

func (stream *Stream) ReadPacket() (pkt *Packet, err error) {
var packet C.AVPacket
C.av_init_packet(&packet)

defer C.av_packet_unref(&packet)
packet := C.av_packet_alloc()
defer C.av_packet_free(&packet)

if cerr := C.av_read_frame(stream.formatCtx, &packet); int(cerr) != 0 {
if cerr := C.av_read_frame(stream.formatCtx, packet); int(cerr) != 0 {
if cerr == C.AVERROR_EOF {
err = io.EOF
} else if cerr == -C.ETIMEDOUT {
Expand All @@ -147,7 +155,7 @@ func (stream *Stream) ReadPacket() (pkt *Packet, err error) {
defer stream.mu.RUnlock()

if decoder, ok := stream.decoders[int(packet.stream_index)]; ok {
return decoder.Decode(&packet)
return decoder.Decode(packet)
}

err = fmt.Errorf("ffmpeg: decoder not found %d", int(packet.stream_index))
Expand Down

0 comments on commit b9ef7be

Please sign in to comment.