http://www.perlmonks.org?node_id=349029

Jouke has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I'm trying to complete my Audio::BASSXS module by wrapping more of the functions supported by the BASS library.

One of the things I want to do is add support for callbacks. This turns out to be not so simple. Here's what I'm trying to do:

In bass.h there's the following function:
HSTREAM BASSDEF(BASS_StreamCreate)(DWORD freq, DWORD chans, DWORD flag +s, void *proc, DWORD user);
This proc parameter takes a STREAMPROC callback, which is defined like this:
typedef DWORD (CALLBACK STREAMPROC)(HSTREAM handle, void *buffer, DWOR +D length, DWORD user);
Obviously I want to achieve that the BASS_StreamCreate can be called from perl, taking a coderef as a parameter.
There's an example how to call this in C supplied with the BASS package, which looks like this (I stripped it down to the most important piece):
DWORD CALLBACK stream(HSTREAM handle, char *buffer, int length, DWORD +user) { int c; // check how much recorded data is buffered c=BASS_ChannelGetData(rchan,0,BASS_DATA_AVAILABLE); c-=length; if (c>2*chunk+1764) { // buffer has gotten pretty large so remove +some c-=chunk; // leave a single 'chunk' BASS_ChannelGetData(rchan,0,c); // remove it } // fetch recorded data into stream c=BASS_ChannelGetData(rchan,buffer,length); if (c<length) memset(buffer+c,0,length-c); // short of data return length; } // create a stream to play the recording data, and start it chan=BASS_StreamCreate(44100,2,0,(STREAMPROC*)stream,0); BASS_StreamPlay(chan,0,BASS_SAMPLE_LOOP);


My XS code (also stripped down to show relevant parts) looks like this:
#include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" #include <bass.h> #include "callback.c" #include "const-c.inc" MODULE = Audio::BASSXS PACKAGE = Audio::BASSXS INCLUDE: const-xs.inc PROTOTYPES: DISABLE HSTREAM BASS_StreamCreate(freq, chans, flags, proc, user) DWORD freq DWORD chans DWORD flags SV * proc DWORD user CODE: if (streamproccallback == (SV*)NULL) /* First time, so create a new SV */ streamproccallback = newSVsv(proc) ; else /* Been here before, so overwrite */ SvSetSV(streamproccallback, proc) ; RETVAL = BASS_StreamCreate(freq,chans,flags,(STREAMPROC*)MyStreamP +roc, user); OUTPUT: RETVAL
And the callback.c looks like this (not stripped down...this is all there is):
static SV * streamproccallback = (SV*)NULL; DWORD CALLBACK MyStreamProc(handle,buffer,length,user) HSTREAM handle; char *buffer; int length; DWORD user; { if (streamproccallback == (SV*)NULL) return 0; dSP ; int ret; SV * shandle; SV * sbuffer; SV * slength; SV * suser; ENTER; SAVETMPS; shandle = sv_2mortal(newSViv(handle)); sbuffer = sv_2mortal(newSVpv(buffer,length)); slength = sv_2mortal(newSViv(length)); suser = sv_2mortal(newSViv(user)); PUSHMARK(SP); XPUSHs(shandle); XPUSHs(sbuffer); XPUSHs(slength); XPUSHs(suser); PUTBACK; ret = call_sv(streamproccallback, G_SCALAR); buffer = SvPV(sbuffer,length); FREETMPS; LEAVE; return ret; }
This all compiles without a problem. However, when I run the following script:
#!/usr/bin/perl use strict; use warnings; use Audio::BASSXS; our $audio = 'test.mp3'; open(FH, "<", $audio) || die "Can't open $audio: $!"; binmode(FH); sub testsub { # We get 4 parameters in the @_: # ($handle,$buffer,$length,$user) = @_; my $read = sysread(FH, $_[1], $_[2]); unless ($read) { $read = $read|BASS_STREAMPROC_END; } warn "$read\n"; return $read; } BASS_Init(1,44100,0,0,0); my $stream = BASS_StreamCreate(44100,2,0,\&testsub, 0); warn BASS_ErrorString(BASS_ErrorGetCode) unless $stream; BASS_StreamPlay($stream,0,BASS_SAMPLE_LOOP); sleep(10); close(FH);
This however prints '35280' (so my callback is called once at least), and then ends with 'Unhandled exception in perl.exe (BASSXS.dll): 0xC0000005: Access violation' Then the MSVC debugger starts and shows me it has ended at the line     if (streamproccallback == (SV*)NULL) return 0; in callback.c. I've tried many, many different things (that if statement where it ends now wasn't there yesterday, and then it crashed on the dSP statement), but can't find anything that works. Does anyone have an idea?


Jouke Visser
Speaking at the 2004 O'Reilly Open Source Convention about pVoice

Replies are listed 'Best First'.
Re: callbacks in XS code
by hawtin (Prior) on Apr 29, 2004 at 07:58 UTC

    In this node I use have an example of a callback from C code to a code ref (and yes, you're right it is not as simple as you would have thought

Re: callbacks in XS code
by mpeppler (Vicar) on Apr 30, 2004 at 06:39 UTC
    From looking at my code and yours it seems that you are missing an SPAGAIN; directive after the perl_call_sv() call.

    Here's what I use in DBD::Sybase:

    if(imp_dbh->err_handler) { dSP; int retval, count; ENTER; SAVETMPS; PUSHMARK(sp); XPUSHs(sv_2mortal(newSViv(CS_NUMBER(errmsg->msgnumber)))); XPUSHs(sv_2mortal(newSViv(CS_SEVERITY(errmsg->msgnumber)))); XPUSHs(sv_2mortal(newSViv(0))); XPUSHs(sv_2mortal(newSViv(0))); XPUSHs(&sv_undef); XPUSHs(&sv_undef); XPUSHs(sv_2mortal(newSVpv(errmsg->msgstring, 0))); if(imp_dbh->sql) XPUSHs(sv_2mortal(newSVpv(imp_dbh->sql, 0))); else XPUSHs(&sv_undef); XPUSHs(sv_2mortal(newSVpv("client", 0))); PUTBACK; if((count = perl_call_sv(imp_dbh->err_handler, G_SCALAR | G_EV +AL)) != 1) croak("An error handler can't return a LIST."); SPAGAIN; if(SvTRUE(ERRSV)) { POPs; retval = 1; } else { retval = POPi; } PUTBACK; FREETMPS; LEAVE; /* If the called sub returns 0 then ignore this error */ if(retval == 0) return CS_SUCCEED; }
    This works fine for me (and I have similar code in Sybase::DBlib and Sybase::CTlib.)

    Michael