Author Topic: BASS_Mixer_ChannelPlay hangs  (Read 222 times)

mhabegger

  • Posts: 5
BASS_Mixer_ChannelPlay hangs
« on: 19 Jun '18 - 20:38 »
I experience a strange and not consistently reproducible issue where a call to Bass hangs and freeze the .Net C# application.
Basically, I create a BassMixer and add channels to it with Sync points and start these channels consecutively.
Does anyone have any idea why the application would hang at the BASS_Mixer_ChannelPlay call?
I've tried with Bass 2.4.12 - 2.4.13.8 and BassMix 2.4.9.
Any help or insight appreciated.

Code: [Select]
var channel = BassAac.BASS_MP4_StreamCreateFile(Filename, 0L, 0L, BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_STREAM_PRESCAN);

[...]

Log.Debug($"Unpausing channel {channel}");
                   
var cI  = Bass.BASS_ChannelGetInfo(channel);
Log.Debug($"Channel {channel} info: {cI.chans} - {cI.freq} - {cI.flags} - {cI.ctype} - {cI.origres} - {cI.filename} - {cI}");

// The channel was created with
var result = BassMix.BASS_Mixer_ChannelPlay(channel); // <-- Hangs here forever
Log.Debug($"Unpaused channel {channel} - {result}");



Ian @ un4seen

  • Administrator
  • Posts: 20921
Re: BASS_Mixer_ChannelPlay hangs
« Reply #1 on: 20 Jun '18 - 13:48 »
That sounds like there is a deadlock between the thread making the BASS_Mixer_ChannelPlay call and some other thread, ie. those 2 threads are waiting for each other. Where are you making the BASS_Mixer_ChannelPlay call from? If it is in a SYNCPROC callback function, what type of sync is it and what channel was it set on?

mhabegger

  • Posts: 5
Re: BASS_Mixer_ChannelPlay hangs
« Reply #2 on: 20 Jun '18 - 15:53 »
Hi Ian thanks for helping.

Yes it's called from a SYNCPROC:

Code: [Select]
private void OnTrackSync(int handle, int channel, int data, IntPtr ptr){}

That we pass in as argument to

Code: [Select]
BassMix.BASS_Mixer_ChannelSetSync(channel, BASSSync.BASS_SYNC_POS, linkOutPos, OnTrackSync, _linkOutPtr);
Looking at our logs it seems that it's the same thread [14].

Code: [Select]
2018-06-20 15:18:53,038 [14] DEBUG AudioEngine.AEPlayer - Creating AE Track for 1185ffee-dac8-4f8b-b917-61915dd6c919.m4a (-2147480778)
// Here in between happens BassAac.BASS_AAC_StreamCreateFile
2018-06-20 15:18:53,039 [14] DEBUG AudioEngine.AEPlayer - Created  AE Track for 1185ffee-dac8-4f8b-b917-61915dd6c919.m4a (-2147480778) with channelId:-2147480778
2018-06-20 15:18:53,039 [14] DEBUG AudioEngine.AEPlayer - Adding END Sync for -2147480778
2018-06-20 15:18:53,039 [14] DEBUG AudioEngine.AEPlayer - Adding SLIDE END Sync for -2147480778
[...]
2018-06-20 15:19:03,037 [14] DEBUG AudioEngine.AEPlayer - Unpausing channel -2147480778 from Offset 00:00:00.6000000 1185ffee-dac8-4f8b-b917-61915dd6c919.m4a
2018-06-20 15:19:03,037 [14] DEBUG AudioEngine.AEPlayer - Channel -2147480778 info: 2 - 44100 - BASS_MUSIC_FLOAT, BASS_MUSIC_DECODE, BASS_UNICODE - BASS_CTYPE_STREAM_MP4 - 0 - 1185ffee-dac8-4f8b-b917-61915dd6c919.m4a - MP4, 44100Hz, Stereo, 32bit
// Stuck here

Ian @ un4seen

  • Administrator
  • Posts: 20921
Re: BASS_Mixer_ChannelPlay hangs
« Reply #3 on: 20 Jun '18 - 17:15 »
That looks like you aren't using a SYNCPROC delegate, or did you just leave that part out? If not, please see if adding that helps. The BASS_ChannelSetSync page in the BASS.Net documentation includes an example of how to use a SYNCPROC delegate. Also see the "Callbacks and Delegates" section of the "Interoperating with Unmanaged Code" page for more info.

If the problem still happens, please upload a dump file of it. You can use Task Manager's "Create Dump File" option to genreate the dump file, and upload it here:

   ftp.un4seen.com/incoming

mhabegger

  • Posts: 5
Re: BASS_Mixer_ChannelPlay hangs
« Reply #4 on: 20 Jun '18 - 22:22 »
Thank you Ian. That looks like it may be the source of issues.
I went back in time in the repository to see what happened. In fact, VisualStudio/Resharper is giving out a refactoring hint attached in the image below.
I guess that it got lost while refactoring the application.

Two, perhaps basic question though. Is VS/Resharper wrong about the requirement for `new SYNCPROC`?
Should we define one `private SYNCPROC` field for the entire application or should we create new SYNCPROC for every file?

We've change our code and are creating delegate in the constructor we later use when setting syncs. We've split the sync into three categories we don't want to be processed in parrallel. Is this the right approach or should there be one general single SYNCPROC?

Code: [Select]
    public ClassConstructor() {
            // [...]
            // ReSharper disable once RedundantDelegateCreation
            _mixerStallSync = new SYNCPROC(OnMixerStall);
            // ReSharper disable once RedundantDelegateCreation
            _trackSync = new SYNCPROC(OnTrackSync);
            // ReSharper disable once RedundantDelegateCreation
            _userSync = new SYNCPROC(OnUserSync);
            // [...]
    }

And thanks for the pointer to the `Interoperating with Unmanaged Code` page. In our managed code world we often forget what we've learned some years back... : )
« Last Edit: 20 Jun '18 - 22:41 by mhabegger »

mhabegger

  • Posts: 5
Re: BASS_Mixer_ChannelPlay hangs
« Reply #5 on: 21 Jun '18 - 19:59 »
Hi Ian, still experiencing the same issue with the hang on BASS_Mixer_ChannelPlay.
I've uploaded a Zip file called "mhabegger R1Player 20180621_Hang.zip" with the Dump and our logfile.

You can see from the log that it hangs on there with the following line:

Code: [Select]
2018-06-21 16:35:05,245 [13] DEBUG AudioEngine.AEPlayer - Unpausing channel -2147480598 ...
2018-06-21 16:35:05,245 [13] DEBUG AudioEngine.AEPlayer - Channel 13d554ed-b631-4960-9efa-b071745f05b5 info: 2 - 44100 - BASS_MUSIC_FLOAT, BASS_MUSIC_DECODE, BASS_UNICODE - BASS_CTYPE_STREAM_MP4 - 0 - MP4, 44100Hz, Stereo, 32bit
// BASS_Mixer_ChannelPlay <- happens right after here

Thanks again for you help.

radio42

  • Posts: 4603
Re: BASS_Mixer_ChannelPlay hangs
« Reply #6 on: 22 Jun '18 - 08:39 »
A SYNCPROC is a normal callback delegate. So in general you would not need a separate SYNCPROC instance for each Sync you are setting up; as you can differentiate the different Sync's being setup by the 'data' parameter provided in the sync.
However, I don't know the scope and lifetime of your class.

Can you maybe provide a bit more of your source code, as from the given fragments I cannot see:
- please provide the full source code of your OnTrackSync implementation
- how you create the source channel, the mixer channel and where you add the source to the mixer
- what is the scope and life time of your 'class' (e.g. is it a kind of 'player class' or do you create that class per track? and when is your class disposed?

What I also do not really understand is, that it looks like you are calling this:
    "var result = BassMix.BASS_Mixer_ChannelPlay(channel); // <-- Hangs here forever"
In case the 'channel' has actually ended?!
E.g. from your output it look like you are settings also an EndSync on the 'channel' and when the end is reached, you call BASS_Mixer_ChannelPlay for that 'channel' - this doesn't seem to make any sense to me (why play the channel when it already ended)?

Ian @ un4seen

  • Administrator
  • Posts: 20921
Re: BASS_Mixer_ChannelPlay hangs
« Reply #7 on: 22 Jun '18 - 18:17 »
Hi Ian, still experiencing the same issue with the hang on BASS_Mixer_ChannelPlay.
I've uploaded a Zip file called "mhabegger R1Player 20180621_Hang.zip" with the Dump and our logfile.

Thanks. The process had over 1000 threads when the dump file was generated. Rather than going through all of that, I think it will be simpler to get more info with a debug BASSmix version, so I will send you that.

mhabegger

  • Posts: 5
Re: BASS_Mixer_ChannelPlay hangs
« Reply #8 on: 23 Jun '18 - 08:28 »
Thanks.
About the architecture, we have an Engine class driving a Singleton Player instance and the Player handles all Bass. Calls.
Regarding the 1000 processes. The engine is trying over and over again to add files and syncs to player in different thread resulting in lots of threads since they all hang somewhere.

@Bernd: The logfile is showing the channel which is ending and calling for the next track to be started. The channel started is the next one not the one that has ended. The logfile may be a little confusing.

I've uploaded the code of our player to your ftp in case you wanted to check it and how we handle the SYNCPROC. I've changed the code to only have one single _trackSync method and use the IntPtr references to react to the callback properly.

The StreamCreateFile:
Code: [Select]
Channel = BassAac.BASS_AAC_StreamCreateFile(Filename, 0L, 0L, BASSFlag.BASS_SAMPLE_FLOAT | BASSFlag.BASS_STREAM_DECODE | BASSFlag.BASS_STREAM_PRESCAN);
The StreamAddChannel:
Code: [Select]
BassMix.BASS_Mixer_StreamAddChannel(_mixer, track.Channel, BASSFlag.BASS_MIXER_PAUSE | BASSFlag.BASS_MIXER_DOWNMIX | BASSFlag.BASS_STREAM_AUTOFREE);
The ChannelSetSync:
Code: [Select]
BassMix.BASS_Mixer_ChannelSetSync(track.Channel, BASSSync.BASS_SYNC_POS, linkOutPos, _trackSync, _linkOutPtr);
Happy to change to a debug BASSmix to see where and why we get stuck.
« Last Edit: 23 Jun '18 - 09:37 by mhabegger »