Skip to content

Commit

Permalink
Core: fixing audio onComplete(#673)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggcrunchy authored Jan 16, 2024
1 parent 2459919 commit c2edbda
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
48 changes: 47 additions & 1 deletion librtt/Rtt_PlatformNotifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,37 @@ PlatformNotifier::ScheduleDispatch( VirtualEvent *e )
}
}

void
PlatformNotifier::ScheduleDispatch( VirtualEvent *e, int ref )
{
if ( Rtt_VERIFY( e ) )
{
bool scheduled = false;
Rtt_ASSERT( LUA_NOREF != ref && LUA_REFNIL != ref );

lua_State *L = GetLuaState();
Rtt_ASSERT( L );
if ( L )
{
Runtime *runtime = LuaContext::GetRuntime( L );

PlatformNotifierTask *task = Rtt_NEW( runtime->Allocator(), PlatformNotifierTask( * this, e ) );

task->SetReference( ref );

runtime->GetScheduler().Append( task );

scheduled = true;
}

// In case of error, we need to delete the event
if ( ! Rtt_VERIFY( scheduled ) )
{
Rtt_DELETE( e );
}
}
}

bool
PlatformNotifier::HasListener() const
{
Expand Down Expand Up @@ -163,7 +194,8 @@ PlatformNotifier::RawSetListenerRef(int ref)
PlatformNotifierTask::PlatformNotifierTask( PlatformNotifier& notifier, VirtualEvent *e )
: Super(),
fNotifier( notifier ),
fEvent( e )
fEvent( e ),
fLuaRef( LUA_NOREF )
{
Rtt_ASSERT( e );
}
Expand All @@ -176,7 +208,21 @@ PlatformNotifierTask::~PlatformNotifierTask()
void
PlatformNotifierTask::operator()( Scheduler & sender )
{
int oldRef = fNotifier.GetListenerRef();

if ( LUA_NOREF != fLuaRef )
{
fNotifier.RawSetListenerRef( fLuaRef );
}

fNotifier.CallListener( fEvent->Name(), *fEvent );

if ( LUA_NOREF != fLuaRef )
{
fNotifier.RawSetListenerRef( oldRef );

fLuaRef = LUA_NOREF;
}
}

// ----------------------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions librtt/Rtt_PlatformNotifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class PlatformNotifier
public:
// Caller must release ownership of event (it will be disposed of by the runtime)
void ScheduleDispatch( VirtualEvent *e );
void ScheduleDispatch( VirtualEvent *e, int ref );

public:
void SetListenerRef( int index );
Expand Down Expand Up @@ -86,6 +87,9 @@ class PlatformNotifierTask : public Task
protected:
PlatformNotifierTask( PlatformNotifier& notifier, VirtualEvent *e );

protected:
void SetReference( int ref ) { fLuaRef = ref; }

public:
~PlatformNotifierTask();

Expand All @@ -95,6 +99,7 @@ class PlatformNotifierTask : public Task
protected:
PlatformNotifier& fNotifier;
VirtualEvent *fEvent;
int fLuaRef;

friend class PlatformNotifier;
};
Expand Down
4 changes: 1 addition & 3 deletions librtt/Rtt_PlatformOpenALPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1124,9 +1124,7 @@ PlatformOpenALPlayer::NotificationCallback( int which_channel, unsigned int al_s
// Bug 5724: In addition to setting all the callback properties, we pass the notifier in to transfer ownership.
// We expect when the event is fired and deleted, it will also delete the notifier with it.
e->SetProperties( which_channel, al_source, almixer_data, finished_naturally );//notifier );
notifier->RawSetListenerRef( callback );
notifier->ScheduleDispatch( e );
notifier->RawSetListenerRef( LUA_NOREF );
notifier->ScheduleDispatch( e, callback );
}
}/*
else
Expand Down

0 comments on commit c2edbda

Please sign in to comment.