From a907cc6e9db47d39a48e566bde8e9ca541bf598f Mon Sep 17 00:00:00 2001 From: danil_e71 Date: Fri, 20 May 2022 09:20:37 +0300 Subject: [PATCH] fix build for linux/windows --- decoder.go | 28 ++-------------------------- ffmpeg.h | 8 +++++++- stream.go | 16 +++++++--------- types_linux.go | 34 ++++++++++++++++++++++++++++++++++ types_windows.go | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 36 deletions(-) create mode 100644 types_linux.go create mode 100644 types_windows.go diff --git a/decoder.go b/decoder.go index e842648..388e523 100644 --- a/decoder.go +++ b/decoder.go @@ -124,19 +124,7 @@ func (decoder *decoder) Decode(packet *C.AVPacket) (pkt *Packet, err error) { switch frame.format { case C.AV_SAMPLE_FMT_FLTP: if decoder.swrContext == nil { - layout := uint64(frame.channel_layout) - - decoder.swrContext = C.swr_alloc_set_opts(nil, // we're allocating a new context - C.longlong(layout), // out_ch_layout - C.AV_SAMPLE_FMT_S16, // out_sample_fmt - frame.sample_rate, // out_sample_rate - - C.longlong(layout), // in_ch_layout - decoder.codecCtx.sample_fmt, // in_sample_fmt - frame.sample_rate, // in_sample_rate - - 0, // log_offset - nil) // log_ctx + decoder.swrContext = swrAllocSetOpts(uint64(frame.channel_layout), frame.sample_rate, decoder.codecCtx.sample_fmt) if cerr = C.swr_init(decoder.swrContext); cerr < C.int(0) { decoder.swrContext = nil @@ -166,19 +154,7 @@ func (decoder *decoder) Decode(packet *C.AVPacket) (pkt *Packet, err error) { case C.AV_SAMPLE_FMT_S32: if decoder.swrContext == nil { - layout := uint64(frame.channel_layout) - - decoder.swrContext = C.swr_alloc_set_opts(nil, // we're allocating a new context - C.longlong(layout), // out_ch_layout - C.AV_SAMPLE_FMT_S16, // out_sample_fmt - frame.sample_rate, // out_sample_rate - - C.longlong(layout), // in_ch_layout - decoder.codecCtx.sample_fmt, // in_sample_fmt - frame.sample_rate, // in_sample_rate - - 0, // log_offset - nil) // log_ctx + decoder.swrContext = swrAllocSetOpts(uint64(frame.channel_layout), frame.sample_rate, decoder.codecCtx.sample_fmt) if cerr = C.swr_init(decoder.swrContext); cerr < C.int(0) { decoder.swrContext = nil diff --git a/ffmpeg.h b/ffmpeg.h index 1667c94..67cdeb7 100644 --- a/ffmpeg.h +++ b/ffmpeg.h @@ -1,3 +1,7 @@ +#pragma once +#ifndef GO_FFMPEG_H +#define GO_FFMPEG_H + #include #include #include @@ -15,4 +19,6 @@ int rtsp_avcodec_encode_jpeg_nv12(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVP struct AVStream * stream_at(struct AVFormatContext *c, int idx); void ffmpeginit(); int rtsp_avcodec_encode_wav(AVCodecContext *pCodecCtx, AVFrame *pFrame,AVPacket *packet); -int rtsp_avcodec_encode_resample_wav(AVCodecContext *pCodecCtx,SwrContext *swr_ctx, AVFrame *pFrame,AVPacket *packet); \ No newline at end of file +int rtsp_avcodec_encode_resample_wav(AVCodecContext *pCodecCtx,SwrContext *swr_ctx, AVFrame *pFrame,AVPacket *packet); + +#endif /* GO_FFMPEG_H */ \ No newline at end of file diff --git a/stream.go b/stream.go index a512a29..5506034 100644 --- a/stream.go +++ b/stream.go @@ -7,6 +7,7 @@ import "C" import ( "fmt" "io" + "os" "runtime" "sync" "unsafe" @@ -62,14 +63,6 @@ func (e ErrTimeout) Error() string { return e.err.Error() } -func CErr2Str(code C.int) string { - buf := make([]byte, 64) - - C.av_strerror(code, (*C.char)(unsafe.Pointer(&buf[0])), C.ulonglong(len(buf))) - - return string(buf) -} - // Setup transport (tcp or udp) func (stream *Stream) Setup(t Type) (err error) { transport := C.CString("rtsp_transport") @@ -84,7 +77,12 @@ func (stream *Stream) Setup(t Type) (err error) { timeoutKey := C.CString("timeout") defer C.free(unsafe.Pointer(timeoutKey)) - timeout := C.CString("10000000") + goTimeout := os.Getenv("FFMPEG_TIMEOUT") + if goTimeout == "" { + goTimeout = "10000000" + } + + timeout := C.CString(goTimeout) defer C.free(unsafe.Pointer(timeout)) C.av_dict_set(&stream.dictionary, timeoutKey, timeout, 0) diff --git a/types_linux.go b/types_linux.go new file mode 100644 index 0000000..a848802 --- /dev/null +++ b/types_linux.go @@ -0,0 +1,34 @@ +// +build linux +package rtsp + +/* +#cgo LDFLAGS: -lavformat -lavutil -lavcodec -lswresample -lswscale -lm +#include "ffmpeg.h" +*/ +import "C" +import ( + "unsafe" +) + +func CErr2Str(code C.int) string { + buf := make([]byte, 64) + + C.av_strerror(code, (*C.char)(unsafe.Pointer(&buf[0])), C.ulong(len(buf))) + + return string(buf) +} + +func swrAllocSetOpts(layout uint64, sampleRate C.int, sampleFmt int32) *C.SwrContext { + swrContext := C.swr_alloc_set_opts(nil, // we're allocating a new context + C.long(layout), // out_ch_layout + C.AV_SAMPLE_FMT_S16, // out_sample_fmt + sampleRate, // out_sample_rate + + C.long(layout), // in_ch_layout + sampleFmt, // in_sample_fmt + sampleRate, // in_sample_rate + + 0, // log_offset + nil) // log_ctx + return swrContext +} diff --git a/types_windows.go b/types_windows.go new file mode 100644 index 0000000..c224987 --- /dev/null +++ b/types_windows.go @@ -0,0 +1,34 @@ +// +build windows +package rtsp + +/* +#cgo LDFLAGS: -lavformat -lavutil -lavcodec -lswresample -lswscale -lm +#include "ffmpeg.h" +*/ +import "C" +import ( + "unsafe" +) + +func CErr2Str(code C.int) string { + buf := make([]byte, 64) + + C.av_strerror(code, (*C.char)(unsafe.Pointer(&buf[0])), C.ulonglong(len(buf))) + + return string(buf) +} + +func swrAllocSetOpts(layout uint64, sampleRate C.int, sampleFmt int32) *C.SwrContext { + swrContext := C.swr_alloc_set_opts(nil, // we're allocating a new context + C.longlong(layout), // out_ch_layout + C.AV_SAMPLE_FMT_S16, // out_sample_fmt + sampleRate, // out_sample_rate + + C.longlong(layout), // in_ch_layout + sampleFmt, // in_sample_fmt + sampleRate, // in_sample_rate + + 0, // log_offset + nil) // log_ctx + return swrContext +}