A few of my favorite Win7 Sound features – Stream Switching


Way back when I was in college, I learned Lisp using a derivative of Lisp called MACLISP (named for MIT’s project MAC, not for anything that came from a fruity company in Cupertino).  One of the coolest features that MACLISP offered was the (DWIM) command – basically if you had a typo when entering an s-list into MACLISP, you could type (DWIM) and the MACLISP interpreter would fix your code for you (and yeah, it usually got it somewhat wrong :)).

Stream Switching is a DWIM feature in the audio stack.  If an application is rendering to a default audio device and the device is removed, the audio stream will automatically switch to the new device.  The same happens if the default device changes for other reasons (if the user changes the default device for example) or if the sample rate on the device changes (this can happen with certain types of audio hardware allow external controls to change the sample rate for the device).

We were able to figure out how to implement the stream switching logic in a fashion that causes it to work without requiring changes from 3rd party applications, which is really cool because it allows us to enable new scenarios without breaking appcompat – as long as the application is rendering to a default endpoint, we’ll stream switch the application without the app being notified.

If an application is rendering to a specific endpoint, we’re not going to stream switch when the endpoint is removed – we don’t know the reasons for the application choosing that particular endpoint so we don’t attempt to second guess the applications intent (maybe the user has asked that their music player only play through the headphones and not through the speakers because playing through the speakers would disturb the baby).

We also don’t support stream switching if the application is using WASAPI (the low level audio rendering APIs) to render audio.  That’s for a number of reasons, but mostly it’s because the presumption is that any application that is using WASAPI is using a low level rendering API and thus doesn’t want this kind of behavior.

 

The stream switching logic is really cool in action, especially if you’ve got a machine which supports dynamic jack detection – when you’re watching a DVD in windows media player and you plug in a pair of headphones, poof – the audio gets redirected to the headphones just like you’d expect it to.

Comments (29)

  1. Alexandre Grigoriev says:

    So that’s another thing Windows will do behind our backs, and sometimes get it really really wrong, as it often happens. I hope there’s an option to disable it?

  2. LarryOsterman says:

    Alexandre: There’s no way of disabling stream switching.  And I don’t see how it could get it "really wrong".

  3. Dean Harding says:

    Sounds like a pretty neat feature to me!

  4. Jason says:

    Excellent! I loved the new Vista port detection stuff, but always hated the "oops, I have headphones plugged in, so stop windows media player, exit it, unplug, and try again."

    Another thing to look forward to tomorrow when I grab it from MSDN!

  5. Phaeron says:

    This is definitely a good thing. Vista’s new audio stack introduced a problem where you could cause a device switch just by unplugging headphones, presumably because Vista drivers expose the headphone jack as a separate endpoint when the XP drivers didn’t. This exposed a new class of bugs where programs would hang or crash when headphones were inserted or pulled during audio playback. I ended up having to debug this, and it was fun to find out that DirectSound kept returning success on Restore() and DEVICELOST on anything else. This was a rare exception to the audio stack’s usual It Just Works(tm) behavior, and I was surprised that it didn’t seem to be covered in the stacks of Vista preparedness literature.

    Some newer programs will attempt to reinit sound when this occurs, but it sounds like Windows 7 will let the older programs continue working smoothly, too.

  6. tsilb says:

    Wow, almost nothing for nearly a year, then half a dozen posts in 5 days.  I could get used to this!

  7. yeah, i remember seeing a demo of this one in channel 9. very useful feature indeed and i’m sure going to use it.

    still, i’d appreciate it if we could get some kind of ui to switch streams selectively, i.e. for specific apps only and from any stream to any other stream (rather than for all apps from the default stream but only when that stream change). oh well, i guess we’ll see it win7sp1… ;)

  8. Robert says:

    I noticed this in the RC and it really is an awesome feature :)

    However, the same thing doesn’t really work for microphones as far as I can tell.

    For example, Im playing a game, I want to plug in my headset the audio switches over to it fine, however the microphone will not work until I exit and restart the game.

    Im unsure if this is just the app, or if input devices are not counted in "stream switching".

  9. Alexandre Grigoriev says:

    Does it work with Bluetooth headphones?

  10. LarryOsterman says:

    Alexandre: It should work on any headphones.  It doesn’t work with remote desktop endpoints though.

    Robert: I don’t believe we stream switch input devices.  I’ll ask the stream switching folks.

  11. Robert says:

    Thanks for the reply Larry, maybe for Windows 8 :)

  12. LarryOsterman says:

    Robert: Actually, stream switching capture devices has its own set of challenges – for instance the microphone volume is typically different on each device and mic tuning / agc is only done at the start of the stream.  This makes switching automatically highly likely to cause a negative experience for customers.  But maybe.

  13. Mike Dimmick says:

    Now, any ideas why the Headphones/Line Out port on my Latitude D820’s docking station doesn’t work under Windows 7 RC? I have to use the one on the laptop itself. Not critical, but slightly annoying that it’s something else to plug in or unplug when docking or undocking.

    Given Dell’s unbelievable slowness in getting working drivers just for Vista x64 – the video driver on their site for the nVidia Quadro NVS 110M chip is STILL the January 2007 version, known to be very buggy – I don’t hold out hope for Windows 7 x64 drivers that actually work, and I’m a bit wary of trying Windows Vista drivers!

  14. Mark Sowul says:

    Mike – try going to nvidia’s site; these days they offer laptop drivers.  It works fine for my D620.

  15. Matt Ellis says:

    I’m a bit confused by the comment about audio being automatically redirected to headphones when they’re inserted. Is this a new feature? Because this has worked for me for years, on various versions of Windows (even Windows Server 2003).

    I get the idea of the stream switching, but don’t know how it relates to headphones.

    Cheers

    Matt

  16. LarryOsterman says:

    Mike: Are you running the in-box driver or the dell driver?  The inbox driver depends on hints (called the pinconfig) in the system BIOS that let it know what ports are what, on many laptops this information doesn’t include some of the jacks (and it almost never has docking station stuff).  The OEM driver typically handles this.  When I get into work, I’ll look to see if we have info on the D820 docking station.

    Matt: It depends on how your headphone jacks are wired.   If the headphone is a separately addressable endpoint (like it is on my desktop machines) it won’t work without support from the audio stack.  If the headphone is wired in series with the speakers with a mechanical interlock it doesn’t need hardware support.

  17. Robert says:

    In reply to Mike: Depends if you are using a normal 3.5mm audio jack like you seem to be, or like me, you have a USB headset.

    It never used to work with a USB device, now it does :)

  18. Joshua says:

    I just tried out the stream switching just today with doing a voice chat before reading this blog. I noticed that the audio was sent to the headphones but I did have to exit and reopen to get the headset mic to work (which is no problem :) ).

    @Mike: You can download the drivers from http://www.laptopvideo2go.com and use the "hacked" INF for the chipset. I did that all the time when I had a D820 and D630c. The drivers Dell releases isn’t any different than the ones from nvidia except for the device id string (which is why the default nvidia ones don’t pick them up). The only thing I’ve seen is extra Dell branding on the control panels.

    Of course, YMMV with this, but I’ve always been pleased using the inf from the site.

  19. Ben Voigt says:

    @Joshua: Mike said Vista x64.  x64 won’t load hacked INFs, you *must* have a digital signature traceable back to Microsoft to load a driver following a normal boot.

  20. MSDNArchive says:

    Just found out about stream switching when I unplugged my USB headset while watching Hulu and it started playing through the speakers – woo hoo!

    Although later on, and after a suspend/resume cycle, the volume mixer would hang, and even explorer.exe hung up on restart when attempting to play the logoff sound. If I can get it to repro I’ll get you more info.

  21. Matt says:

    it is possible to assign different applications to differnt sound devices or outputs manually? as when i change the default sound in control pannel all my audio get switched to that device but i only want a specific one to do that. I think this is due to the auto switching feature, maybe a way to disable it or an manual option for audio routes?

  22. LarryOsterman says:

    Matt: It’s up to the application to decide what it should use as its output device.  We can’t control how the application choses a particular device, we just give them the option of choosing either the default device or a specific device.

  23. Matt says:

    The reason i ask that is because previously i was using windows xp, which does not support this auto switching feature and all sound get routed to the default device, thus i was able to assign a program to a specific device by changing the default device before i start a new application as the old application’s outputs will "stick" to what ever it was using. I was wondering if this can be implemented, so the program "thinks" it is getting assigned to the default device but in reality the "default" device is changes. So in the point of view of the application it still "thinks" it is outputing to the default device.

  24. LarryOsterman says:

    Matt: In XP, apps also chose between using the default device and a specific device.  Many apps choose not to use specific devices (and some apps chose to always use the first device, regardless of whether there are more than one device available).  It all depends on the app.

    Windows media player, for example uses the default device by default but allows you to override that default to use any other device.  Other media players and applications have similar settings.

  25. KW says:

    Stream switching in Win7 is incredibly annoying. When I play a video on, say, YouTube, and fast-forward through the video, it will randomly switch to my USB headset. For some videos, it *starts* playing on the headset, while other videos never have this issue.

    I’ve run into the same thing on Hulu and other flash-video players.

  26. LarryOsterman says:

    Stream switching should only kick in if the device is removed or if the default device changes.  You can see which of those is happening by bringing up the playback devices control panel and looking at it while fast forwarding.

  27. On Win7 on a Lenovo x61t, I have intermittent problems that *seem* to be related to stream switching.

    The configuration is a dock where the headphones are plugged into the 3.5mm speaker out jack on the base. It normally works just fine across un-docking/redocking (and I’m normally pretty careful to press the undock button first).

    But sometimes, even after a ‘safe’ undock, the sound doesn’t play after re-docking. It’s not tied to an app – even the Windows sounds and the sound tests/etc. stop playing. What’s even stranger is that if I unplug the headphones, the internal speaker works! It’s almost like the detection logic is reversed.

    So far, the only workaround I’ve had has been to reboot. Any other ideas as to the cause and/or solutions/workarounds?

  28. Vlad says:

    Hello ( first of all ,sorry of my english).

    I like de Stream Switching because i have installed the SRS audio sandbox from srs labs(kind of virtual soundcard with drivers ".sys" and a software), and when it crash i disable it and listen forward on real hardware souncard.

    If anyone can help me please. Under xp the "srs audio sandbox" work fine , in vista and seven after listening a hour or two the sound of the last second repeats and i need to disable audiodg service and start again to fix this(sometimes is not working , and needs restarting the OS to stop the repeating sound of the last second).In other forums i see that this problem is happening only in x64 version , on my PC in both. The program srs… have installed drivers and software, if software is not running- all efects(trusurrond etc) not applied but still using drivers from srs and it sooner or later will crash !!!   I tried to replace the file CHDART.sys(driver of real sound card conexant) with file from XP driver of soundcard and after this, the sound doesn`t crash but  I hear crackling in the sound (especialy when i refresh the desktop F5, or plug an USB device or playing games). My soundcard is conexant hd venice 5045(dev_5045) , and i also tried all drivers found on the net(latest and not ,with windows update and all the same). I don`t now if that is a problem of  "srs sandbox" ,i think  is a compatibility issue with new audio engine in vista and seven, someting that is doing automaticaly and it does in a wrong way .I also tried all formats for sound all variant to disable the mic or spdif or audio enhancement and nothing , also i find, with one driver, that in sound apear noise of crackling whe i navigate on audio proprieties (the problem don`t depend of player because i used wmp and winamp and kmplayer with ac3filter and the same efect ).I would to know  if I can to disable  smoeting  in win7 , modify de registry  or anything(i don`t know maybe emulate a XP audio engine) to  fix this problem.Thanks