go-pus/callbacks.c
Randy Reddig 25a249fbd5 feat: build without libopusfile using go build tag
This patch introduces a new build tag `nolibopusfile` that conditionally
compiles out the code that imports `libopusfile`. This enables a static
binary build on Alpine Linux, which doesn’t have a static `libopusfile`.

Tests still work:

```sh
go test -tags nolibopusfile ./...
go test ./...
```

We could also have moved all libopusfile related code (i.e. Stream) into
a separate sub-package, but that would break backwards compatibility.
This feature is too unpopular to justify introducing a new major
version.

See also: https://github.com/hraban/opus/pull/24
2020-07-10 13:54:23 +01:00

30 lines
1,008 B
C

// +build !nolibopusfile
// Copyright © Go Opus Authors (see AUTHORS file)
//
// License for use of this code is detailed in the LICENSE file
// Allocate callback struct in C to ensure it's not managed by the Go GC. This
// plays nice with the CGo rules and avoids any confusion.
#include <opusfile.h>
#include <stdint.h>
// Defined in Go. Uses the same signature as Go, no need for proxy function.
int go_readcallback(void *p, unsigned char *buf, int nbytes);
static struct OpusFileCallbacks callbacks = {
.read = go_readcallback,
};
// Proxy function for op_open_callbacks, because it takes a void * context but
// we want to pass it non-pointer data, namely an arbitrary uintptr_t
// value. This is legal C, but go test -race (-d=checkptr) complains anyway. So
// we have this wrapper function to shush it.
// https://groups.google.com/g/golang-nuts/c/995uZyRPKlU
OggOpusFile *
my_open_callbacks(uintptr_t p, int *error)
{
return op_open_callbacks((void *)p, &callbacks, NULL, 0, error);
}