Author Topic: Dealing with device failures  (Read 263 times)

Chris Oakley

  • Posts: 180
Dealing with device failures
« on: 30 Apr '22 - 22:26 »
What's the best way to deal with a device that fails and disappears? For example if a device is unplugged whilst audio is outputting to it, how to move the audio to the default device?

What I've tried is creating a mixer to point to an output device and then opening a file as a decode file and attaching this to the mixer.

This all works fine and plays, and when I unplug the audio device it's outputting to and then I reconnect it, I can't then get the mixer to be assigned to the original device. It just doesn't work.

Ideally if a device is unplugged, even if there is no other device, the audio would still play, but obviously not being output, and then when the device is plugged in again then it would resume on that device.

For example Firefox does this. If you're on something like YouTube and you're watching a video, then you unplug the audio device, the sound is redirected to the default device. When you plug the device back in then it automatically goes back to that device.

Is this even possible?

Ian @ un4seen

  • Administrator
  • Posts: 24424
Re: Dealing with device failures
« Reply #1 on: 2 May '22 - 12:57 »
You can use a BASS_SYNC_DEV_FAIL sync to be informed when the device is removed/failed. Set that sync on the mixer (or any other stream on the same device) and then use BASS_ChannelSetDevice to move the stream(s) to a new device in the SYNCPROC callback. That new device could be the "No Sound" device is there are no other soundcards.

BASS won't automatically check when a device is available again, but you could periodically try calling BASS_Start on the old device to detect when it becomes available again, and then move the stream(s) back to it.

Chris Oakley

  • Posts: 180
Re: Dealing with device failures
« Reply #2 on: 2 May '22 - 19:21 »
Excellent Ian, this works perfectly - thanks so much :)

Chris Oakley

  • Posts: 180
Re: Dealing with device failures
« Reply #3 on: 3 May '22 - 12:08 »
Sorry Ian, quick one, is this the same procedure for stalled recording devices too?

Ian @ un4seen

  • Administrator
  • Posts: 24424
Re: Dealing with device failures
« Reply #4 on: 3 May '22 - 17:48 »
Good to hear that you've got it working well. You can set a BASS_SYNC_DEV_FAIL sync on a recording too, but it isn't possible to move a recording to another device with BASS_ChannelSetDevice, so you would need to start a new recording with BASS_RecordStart instead then.

Chris Oakley

  • Posts: 180
Re: Dealing with device failures
« Reply #5 on: 3 May '22 - 19:20 »
Got you. So aside from checking periodically, is there no other way to check if an audio device has been added or removed - regardless of device?

Ian @ un4seen

  • Administrator
  • Posts: 24424
Re: Dealing with device failures
« Reply #6 on: 4 May '22 - 12:39 »
Do you want to receive a notification (instead of polling it) when a device is added/removed? BASS doesn't have that option but BASSWASAPI does via its BASS_WASAPI_SetNotify function. You can use that while still using normal BASS output. Note that BASSWASAPI's device list is different to BASS's, so you can't use the same device numbers but you can find the corresponding devices by matching the BASS_WASAPI_DEVICEINFO "id" and BASS_DEVICEINFO "driver" values. For example:

Code: [Select]
void CALLBACK WasapiNotifyProc(DWORD notify, DWORD device, void *user)
{
BASS_WASAPI_DEVICEINFO wi;
BASS_WASAPI_GetDeviceInfo(device, &wi); // get info on changed WASAPI device
BASS_DEVICEINFO di;
for (int a=1; BASS_GetDeviceInfo(a, &di); a++) {
if (!strcmp(di.driver, wi.id)) { // found the corresponding BASS device
// do something with the notification here
break;
}
}
}

David_AVD

  • Posts: 54
Re: Dealing with device failures
« Reply #7 on: 5 May '22 - 22:12 »
I've tried BASS_WASAPI_SetNotify in my application, but found that the audio device is not gone from (or back into) the list of available devices for a couple of seconds after the notification.

Chris Oakley

  • Posts: 180
Re: Dealing with device failures
« Reply #8 on: 7 May '22 - 20:37 »
Interesting - what I'm actually after though is just a general device change notification, not for a specific device, just if any new audio device has been added / removed.

Ian @ un4seen

  • Administrator
  • Posts: 24424
Re: Dealing with device failures
« Reply #9 on: 9 May '22 - 17:07 »
BASS_WASAPI_SetNotify will give notification of any devices being added and removed, not only a specific device. The WASAPINOTIFYPROC "notify" parameter tells what it is that's changed about the device identified by the "device" parameter.

Chris Oakley

  • Posts: 180
Re: Dealing with device failures
« Reply #10 on: 11 May '22 - 21:09 »
Thanks for all this Ian. All working great :)