gomobile/gobind failing to build a library with interface argument

89 views
Skip to first unread message

Kevin Wang

unread,
Nov 28, 2023, 10:31:55 AM11/28/23
to golang-nuts
I'm trying to build https://github.com/pion/webrtc with gomobile but I'm running into this error. To reproduce, clone and then run gomobile bind .

gobind/go_webrtcmain.go:86:19: cannot use (*proxywebrtc_TrackLocal)(_param_track_ref) (value of type *proxywebrtc_TrackLocal) as webrtc.TrackLocal value in assignment: *proxywebrtc_TrackLocal does not implement webrtc.TrackLocal (missing method Bind)

Specifically, I've tracked it down to this function signature: https://github.com/pion/webrtc/blob/master/rtpsender.go#L61 where it seems like gobind generates some code that looks like:

func proxywebrtc_API_NewRTPSender(refnum C.int32_t, param_track C.int32_t, param_transport C.int32_t) (C.int32_t, C.int32_t) {
        ref := _seq.FromRefNum(int32(refnum))
        v := ref.Get().(*webrtc.API)
        var _param_track webrtc.TrackLocal
        _param_track_ref := _seq.FromRefNum(int32(param_track))
        if _param_track_ref != nil {
                if param_track < 0 { // go object
                        _param_track = _param_track_ref.Get().(webrtc.TrackLocal)
                } else { // foreign object
                        _param_track = (*proxywebrtc_TrackLocal)(_param_track_ref)
                }
        }


A little bit up, it seems that it's indeed creating a proxy TrackLocal type but doesn't implement the interface completely

type proxywebrtc_TrackLocal _seq.Ref

func (p *proxywebrtc_TrackLocal) Bind_proxy_refnum__() int32 {
        return (*_seq.Ref)(p).Bind_IncNum()
}

// skipped method TrackLocal.Bind with unsupported parameter or result types

So I'm not surprised this can't be assigned. I guess the consequence is that this API can't be bound?

Is it possible to have gobind skip the non-working functions with a comment or something? I'm not sure why gobind doesn't reject the API and instead crashes.

Bruno Albuquerque

unread,
Nov 28, 2023, 11:43:40 AM11/28/23
to Kevin Wang, golang-nuts
The way around that is to usually wrap the API you want to use and only expose what you actually need. When you need something with some type of unsupported parameter or return type, write some code around it to convert to sensible supported types. Here is an example of what I end up doing instead of fighting gomobile at every turn:

https://github.com/brunoga/robomaster/tree/main/sdk2/mobile

-Bruno


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/a8351e46-9595-4693-bc6c-03f52ec1a945n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages