Skip to content

Commit

Permalink
fix occasional access violation crash when switching audio devices
Browse files Browse the repository at this point in the history
  • Loading branch information
omriharel committed May 12, 2020
1 parent 6d19837 commit e05c903
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions session_finder_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ func (sf *wcaSessionFinder) GetAllSessions() ([]Session, error) {
}
defer defaultAudioEndpoint.Release()

// receive notifications whenever the default device changes
if err := sf.registerDefaultDeviceChangeCallback(); err != nil {
sf.logger.Warnw("Failed to register default device change callback", "error", err)
return nil, fmt.Errorf("register default device change callback: %w", err)
// receive notifications whenever the default device changes (only do this once)
if sf.mmNotificationClient == nil {
if err := sf.registerDefaultDeviceChangeCallback(); err != nil {
sf.logger.Warnw("Failed to register default device change callback", "error", err)
return nil, fmt.Errorf("register default device change callback: %w", err)
}
}

// get the master session
Expand Down Expand Up @@ -112,16 +114,18 @@ func (sf *wcaSessionFinder) Release() error {

func (sf *wcaSessionFinder) getDefaultAudioEndpoint() (*wca.IMMDevice, error) {

// get the IMMDeviceEnumerator
if err := wca.CoCreateInstance(
wca.CLSID_MMDeviceEnumerator,
0,
wca.CLSCTX_ALL,
wca.IID_IMMDeviceEnumerator,
&sf.mmDeviceEnumerator,
); err != nil {
sf.logger.Warnw("Failed to call CoCreateInstance", "error", err)
return nil, fmt.Errorf("call CoCreateInstance: %w", err)
// get the IMMDeviceEnumerator (only once)
if sf.mmDeviceEnumerator == nil {
if err := wca.CoCreateInstance(
wca.CLSID_MMDeviceEnumerator,
0,
wca.CLSCTX_ALL,
wca.IID_IMMDeviceEnumerator,
&sf.mmDeviceEnumerator,
); err != nil {
sf.logger.Warnw("Failed to call CoCreateInstance", "error", err)
return nil, fmt.Errorf("call CoCreateInstance: %w", err)
}
}

// get the default audio endpoint as an IMMDevice
Expand Down

0 comments on commit e05c903

Please sign in to comment.