microsoft / Windows-universal-samples

API samples for the Universal Windows Platform.
MIT License
9.5k stars 7.98k forks source link

Windows Audio Session API (WASAPI) Sample does not render audio with hardware offload. #1325

Closed theyyg closed 2 years ago

theyyg commented 2 years ago

Which sample are you reporting a bug in?

WindowsAudioSession

Describe the bug Running scenario 2 (Audio Rendering with Hardware Offload), with the Hardware Offload enabled outputs silence.

After choosing a stereo wav test file (https://www.kozco.com/tech/piano2.wav), hardware offload is enabled using the slider. After pressing play, the status banner at the bottom of the application window indicates that playback has started, but no audio is heard. Watching the signal meters in the sound control panel (Win + R > mmsys.cpl) also indicates that no signal is output to the endpoint device.

A clear and concise description of what the bug is. Include links to specific lines of code where you think the bug is. (Click the line number, then click the "..." and select "Copy permalink.") If not sure, at least include a link to the scenario where the problem is.

Debugging the scenario shows that m_AudioClient->GetCurrentPadding( &PaddingFrames); always returns with PaddingFrames set to a 0 value https://github.com/microsoft/Windows-universal-samples/blob/f7bec1640bc2b773f02f102d13c12c04833a6c0e/Samples/WindowsAudioSession/cpp/WASAPIRenderer.cpp#L622

FramesAvailable is set from PaddingFrames. https://github.com/microsoft/Windows-universal-samples/blob/f7bec1640bc2b773f02f102d13c12c04833a6c0e/Samples/WindowsAudioSession/cpp/WASAPIRenderer.cpp#L633 The sample application works properly without hardware offload selected. That is to say that audio playback is successful.

FramesAvailable must be non-zero for any buffers to get filled. https://github.com/microsoft/Windows-universal-samples/blob/f7bec1640bc2b773f02f102d13c12c04833a6c0e/Samples/WindowsAudioSession/cpp/WASAPIRenderer.cpp#L643

To Reproduce Steps to reproduce the behavior:

  1. Build and Launch the Windows Audio Session API (WASAPI) Sample
  2. Click on "2) Audio Rendering with Hardware Offload" from the menu on the left. If the menu is not visible, click the three-lined "hamburger" icon to expand the menu first.
  3. Click the radial circle to select "File". Choose a file for playback and click OK to confirm your choice. This bug was discovered using https://www.kozco.com/tech/piano2.wav.
  4. Click the "Play" button, which sits under the tag "Media Controls" below the file text box.

Expected behavior Audio is expected to be heard from the default audio device, and the audio device meter in the control panel should represent the amplitude of audio being rendered.

Screenshots If applicable, add screenshots to help explain your problem.

Configuration

This was discovered using a Surface Book 2, and it was verified on several other computers which have the hardware offload capability.

PC Hardware Device name Redacted Processor Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz 2.11 GHz Installed RAM 16.0 GB Device ID C02F6CE2-3BDE-4BDB-8805-CA3DF7C4898D Product ID 00330-63028-85240-AAOEM System type 64-bit operating system, x64-based processor Pen and touch Pen and touch support with 10 touch points

Windows Version Edition Windows 11 Pro Version 21H2 Installed on ‎11/‎1/‎2021 OS build 22000.318 Serial number Redacted Experience Windows Feature Experience Pack 1000.22000.318.0

Build Configuration

Installed Version: Community

Visual C++ 2019 00435-60000-00000-AA343 Microsoft Visual C++ 2019

ASP.NET and Web Tools 2019 16.11.94.52318 ASP.NET and Web Tools 2019

Azure App Service Tools v3.0.0 16.11.94.52318 Azure App Service Tools v3.0.0

C# Tools 3.11.0-4.21403.6+ae1fff344d46976624e68ae17164e0607ab68b10 C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Debugging Tools for Windows 10.0.21381.1000 Integrates the Windows Debugger functionality (http://go.microsoft.com/fwlink/?linkid=223405) in Visual Studio.

Extensibility Message Bus 1.2.6 (master@34d6af2) Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

IntelliCode Extension 1.0 IntelliCode Visual Studio Extension Detailed Info

Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft MI-Based Debugger 1.0 Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards 1.0 Microsoft Visual C++ Wizards

Microsoft Visual Studio VC Package 1.0 Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio 16.10.15 (552afdf) Support for debugging Mono processes with Visual Studio.

NuGet Package Manager 5.11.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

ProjectServicesPackage Extension 1.0 ProjectServicesPackage Visual Studio Extension Detailed Info

TypeScript Tools 16.0.30526.2002 TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools 3.11.0-4.21403.6+ae1fff344d46976624e68ae17164e0607ab68b10 Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual C++ for Cross Platform Mobile Development (Android) 16.0.31829.152 Visual C++ for Cross Platform Mobile Development (Android)

Visual F# Tools 16.11.0-beta.21514.6+b6c2c4f53ea3a08fa603ea93d6d2f808a62a21d1 Microsoft Visual F# Tools

Visual Studio Code Debug Adapter Host Package 1.0 Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Tools for Unity 4.11.4.0 Visual Studio Tools for Unity

VisualStudio.DeviceLog 1.0 Information about my package

VisualStudio.Foo 1.0 Information about my package

VisualStudio.Mac 1.0 Mac Extension for Visual Studio

Windows Driver Kit 10.0.21381.1000 Headers, libraries, and tools needed to develop, debug, and test Windows drivers (msdn.microsoft.com/en-us/windows/hardware/gg487428.aspx)

Xamarin 16.11.000.197 (d16-11@6934992) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer 16.11.0.47 (remotes/origin/d16-11@e0d612363) Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates 16.10.5 (355b57a) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK 12.0.0.3 (d16-11/f0e3c2d) Xamarin.Android Reference Assemblies and MSBuild support. Mono: c633fe9 Java.Interop: xamarin/java.interop/d16-11@476bb5b ProGuard: Guardsquare/proguard/v7.0.1@912d149 SQLite: xamarin/sqlite/3.35.4@85460d3 Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-11@87af37b

Xamarin.iOS and Xamarin.Mac SDK 15.2.0.17 (738fde344) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

Additional context Playback using the Tone selection returns and error: ERROR: 0x8000ffff has occurred.

oldnewthing commented 2 years ago

I've asked the feature team to investigate.

theyyg commented 2 years ago

Thank you for investigating.

theyyg commented 2 years ago

Is there any news on this issue?

oldnewthing commented 2 years ago

Okay, a fix is coming next week. Here's the short summary:

https://github.com/microsoft/Windows-universal-samples/blob/7b34a9f7613be0c5316428426dfc6df8ce657f02/Samples/WindowsAudioSession/cpp/WASAPIRenderer.cpp#L141

Change to

       if (!m_DeviceProps.IsLowLatency || m_DeviceProps.IsHWOffload)

https://github.com/microsoft/Windows-universal-samples/blob/7b34a9f7613be0c5316428426dfc6df8ce657f02/Samples/WindowsAudioSession/cpp/WASAPIRenderer.cpp#L144

Change to

                AUDCLNT_STREAMFLAGS_EVENTCALLBACK,

(Remove the AUDCLNT_STREAMFLAGS_NOPERSIST flag.)

https://github.com/microsoft/Windows-universal-samples/blob/7b34a9f7613be0c5316428426dfc6df8ce657f02/Samples/WindowsAudioSession/cpp/WASAPIRenderer.cpp#L297-L303

Put this entire section inside

if (!audioProps.bIsOffload) { ... }

https://github.com/microsoft/Windows-universal-samples/blob/7b34a9f7613be0c5316428426dfc6df8ce657f02/Samples/WindowsAudioSession/cpp/WASAPIRenderer.cpp#L628-L640

Remove the if statement, leaving just the else branch.

        // GetCurrentPadding represents the number of queued frames
        // so we can subtract that from the overall number of frames we have
        FramesAvailable = m_BufferFrames - PaddingFrames;
theyyg commented 2 years ago

Thank you for the support and the update. I'll test it out and report my findings.

theyyg commented 2 years ago

I've tested 589f33b3d3930083debd21f54761269dc223445f and can verify that this issue has been fixed. Thank you, team.