I'm trying to repackage some go code that integrates with an existing C library.
The following works perfectly.
File 1:
package avcodec
type Codec C.struct_AVCodec
File 2:
package avformat
//#cgo pkg-config: libavformat libavcodec
//#include <libavformat/avformat.h>
//#include <libavcodec/avcodec.h>
import "C"
import (
"unsafe"
)
type Codec C.struct_AVCodec
func (s *FormatContext) AvFormatGetVideoCodec() *Codec {
result := C.av_format_get_video_codec((*C.struct_AVFormatContext)(s))
return (*Codec)(result) // <- This works. Codec is defined in this package.
}
If I try to reference or move Codec from File 2 into a separate package (eg. File 1) I get the error:
cannot convert (func literal)((*C.struct_AVFormatContext)(s)) (type *C.struct_AVCodec) to type *Codec
For example, this fails:
package avformat
//#cgo pkg-config: libavformat libavcodec
//#include <libavformat/avformat.h>
//#include <libavcodec/avcodec.h>
import "C"
import (
"avcodec"
"unsafe"
)
func (s *FormatContext) AvFormatGetVideoCodec() *avcodec.Codec {
result := C.av_format_get_video_codec((*C.struct_AVFormatContext)(s))
return (*avcodec.Codec)(result) // <- This fails. Codec defined in avcodec.
}
This also fails:
package avformat
//#cgo pkg-config: libavformat libavcodec
//#include <libavformat/avformat.h>
//#include <libavcodec/avcodec.h>
import "C"
import (
"avcodec"
"unsafe"
)
type Codec avcodec.Codec
func (s *FormatContext) AvFormatGetVideoCodec() *Codec {
result := C.av_format_get_video_codec((*C.struct_AVFormatContext)(s))
return (*Codec)(result) // <- This also fails. Codec is based on avcodec.Codec.
}
I'd like to:
- Understand why it fails, and
- Identify how I can change the packaging so that the function uses the
Codec
type that is defined in theavcodec
package.
Thanks in advance.