forked from microsoft/Xbox-ATG-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WASAPIRenderer.h
92 lines (70 loc) · 2.75 KB
/
WASAPIRenderer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//--------------------------------------------------------------------------------------
// WASAPIRenderer.h
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include <Windows.h>
#include <wrl\implements.h>
#include <mfapi.h>
#include <AudioClient.h>
#include <mmdeviceapi.h>
#include <mutex>
#include "DeviceState.h"
#include "common.h"
#include "ToneSampleGenerator.h"
#pragma once
// User Configurable Arguments for Scenario
struct DEVICEPROPS
{
bool IsHWOffload;
bool IsBackground;
bool IsRawSupported;
bool IsRawChosen;
REFERENCE_TIME hnsBufferDuration;
unsigned long Frequency;
};
// Primary WASAPI Renderering Class
class WASAPIRenderer :
public Microsoft::WRL::RuntimeClass< Microsoft::WRL::RuntimeClassFlags< Microsoft::WRL::ClassicCom >, Microsoft::WRL::FtmBase, IActivateAudioInterfaceCompletionHandler >
{
public:
WASAPIRenderer();
HRESULT SetProperties( DEVICEPROPS props );
HRESULT InitializeAudioDeviceAsync();
HRESULT StartPlaybackAsync();
HRESULT StopPlaybackAsync();
HRESULT PausePlaybackAsync();
DeviceStateChangedEvent^ GetDeviceStateEvent() { return m_DeviceStateChanged; };
METHODASYNCCALLBACK( WASAPIRenderer, StartPlayback, OnStartPlayback );
METHODASYNCCALLBACK( WASAPIRenderer, StopPlayback, OnStopPlayback );
METHODASYNCCALLBACK( WASAPIRenderer, PausePlayback, OnPausePlayback );
METHODASYNCCALLBACK( WASAPIRenderer, SampleReady, OnSampleReady );
// IActivateAudioInterfaceCompletionHandler
STDMETHOD(ActivateCompleted)( IActivateAudioInterfaceAsyncOperation *operation );
private:
~WASAPIRenderer();
HRESULT OnStartPlayback( IMFAsyncResult* pResult );
HRESULT OnStopPlayback( IMFAsyncResult* pResult );
HRESULT OnPausePlayback( IMFAsyncResult* pResult );
HRESULT OnSampleReady( IMFAsyncResult* pResult );
HRESULT ConfigureDeviceInternal();
HRESULT ValidateBufferValue();
HRESULT OnAudioSampleRequested( bool IsSilence = false );
HRESULT ConfigureSource();
UINT32 GetBufferFramesPerPeriod();
HRESULT GetToneSample( unsigned int FramesAvailable );
private:
Platform::String^ m_DeviceIdString;
unsigned int m_BufferFrames;
HANDLE m_SampleReadyEvent;
MFWORKITEM_KEY m_SampleReadyKey;
std::mutex m_mutex;
WAVEFORMATEX *m_MixFormat;
Microsoft::WRL::ComPtr<IAudioClient2> m_AudioClient;
Microsoft::WRL::ComPtr<IAudioRenderClient> m_AudioRenderClient;
Microsoft::WRL::ComPtr<IMFAsyncResult> m_SampleReadyAsyncResult;
DeviceStateChangedEvent^ m_DeviceStateChanged;
DEVICEPROPS m_DeviceProps;
std::unique_ptr<ToneSampleGenerator> m_ToneSource;
};