Author Topic: Strange delay occuring when casting  (Read 771 times)

Chris Oakley

  • Posts: 304
Strange delay occuring when casting
« on: 16 May '23 - 10:57 »
So I've got a weird issue happening with a mixer which has a recording channel plugged into it and then that mixer is being cast to a shoutcast stream.

First the mixer is created:
Code: [Select]
_Mixer = BassMix.BASS_Mixer_StreamCreate(44100, 2, BASSFlag.BASS_STREAM_DECODE Or BASSFlag.BASS_MIXER_NONSTOP)
Bass.BASS_ChannelSetAttribute(_Mixer, BASSAttribute.BASS_ATTRIB_BUFFER, 0F)
Bass.BASS_ChannelSetAttribute(_Mixer, BASSAttribute.BASS_ATTRIB_FREQ, 45000)

Then this to start the recording:
Code: [Select]
_RecordMonitorStream = Bass.BASS_RecordStart(44100, 2, BASSFlag.BASS_DEFAULT, Nothing, IntPtr.Zero) 'Start the recording

Then it's plugged into the mixer:
Code: [Select]
BassMix.BASS_Mixer_StreamAddChannel(_Mixer, _RecordMonitorStream, BASSFlag.BASS_DEFAULT)

Now, that on its own is fine, there is no delay build up. However, if I then start casting by starting an encoder:
Code: [Select]
_Encoder = BassEnc.BASS_Encode_Start(_Mixer, _EncoderString, BASSEncode.BASS_ENCODE_NOHEAD Or BASSEncode.BASS_ENCODE_FP_16BIT Or BASSEncode.BASS_ENCODE_LIMIT, Nothing, IntPtr.Zero)

Then cast:
Code: [Select]
BassEnc.BASS_Encode_CastInit(_Encoder, .FullStreamURL, If(.Username.IsNullOrEmpty, .Password.Trim, .Username.Trim & ":" & .Password.Trim), _EncoderContent, .Title, .URL, .Genre, .Description, Nothing, CInt(.EncodingBitrate), .Public)

After a while there is a delay build up on the record stream which you can see by attaching a DSP Peak level to the mixer. When there is a delay, which can be anywhere from 1 to 10 seconds, if you open a file and add it to the _Mixer as well, there is no delay on that. It's only on the _RecordMonitorStream.

The delay comes and goes too. It can be 10 seconds but then later it's back down to 1 second and this only happens when it's casting. Stop the casting and there is no delay build up.

Is there a flag I need to set somewhere?

Ian @ un4seen

  • Administrator
  • Posts: 26095
Re: Strange delay occuring when casting
« Reply #1 on: 16 May '23 - 17:26 »
It sounds like the mixer is processing data from the recording more slowly than it is arriving from the device, resulting in a build-up of data in the recording's buffer. I notice the mixer is a decoding channel, ie. has the BASS_STREAM_DECODE flag set. How are you processing/playing the mixer? The BASS_ATTRIB_BUFFER setting doesn't apply to decoding channels (there is no playback buffer), so that BASS_ChannelSetAttribute call will fail. The BASS_ATTRIB_FREQ setting will also have no effect, unless the mixer is played by another mixer.

One thing you could try is replacing the BASS_ENCODE_LIMIT flag with BASS_ENCODE_CAST_NOLIMIT in the BASS_Encode_Start call. That will prevent the encoder limitting the data flow to realtime speed, which isn't necessary when the source is limited to realtime speed itself, eg. by a live recording. You will then probably also need to add the BASS_MIXER_CHAN_LIMIT flag to the recording's BASS_Mixer_StreamAddChannel call.

Chris Oakley

  • Posts: 304
Re: Strange delay occuring when casting
« Reply #2 on: 16 May '23 - 20:07 »
Thanks Ian I'll try that and see what happens.

Chris Oakley

  • Posts: 304
Re: Strange delay occuring when casting
« Reply #3 on: 18 May '23 - 14:44 »
Hi Ian, unfortunately this hasn't helped. It's taken a few days, but it's finally now delayed by about 3 seconds.

I have 4 instances of the same app running on the same machine, however, the delay only happens on the one which is casting. The others remain in sync and bang on.

Ian @ un4seen

  • Administrator
  • Posts: 26095
Re: Strange delay occuring when casting
« Reply #4 on: 19 May '23 - 12:18 »
Please confirm how the mixer (with the encoder/caster on it) is being processed. If it's sensitive to delays (eg. it doesn't catch up afterwards) then it would be a good idea to include the BASS_ENCODE_QUEUE flag in the BASS_Encode_Start call, to prevent the encoding introducing any delays.