1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef REMOTING_HOST_AUDIO_CAPTURER_WIN_H_
6 #define REMOTING_HOST_AUDIO_CAPTURER_WIN_H_
7 
8 #include <audioclient.h>
9 #include <mmdeviceapi.h>
10 #include <wrl/client.h>
11 
12 #include <memory>
13 
14 #include "base/macros.h"
15 #include "base/threading/thread_checker.h"
16 #include "base/timer/timer.h"
17 #include "base/win/scoped_co_mem.h"
18 #include "remoting/host/audio_capturer.h"
19 #include "remoting/host/win/audio_volume_filter_win.h"
20 #include "remoting/proto/audio.pb.h"
21 
22 namespace remoting {
23 
24 class DefaultAudioDeviceChangeDetector;
25 
26 // An AudioCapturer implementation for Windows by using Windows Audio Session
27 // API, a.k.a. WASAPI. It supports up to 8 channels, but treats all layouts as
28 // a most commonly used one. E.g. 3.1 and surround layouts will both be marked
29 // as surround layout.
30 class AudioCapturerWin : public AudioCapturer {
31  public:
32   AudioCapturerWin();
33   ~AudioCapturerWin() override;
34 
35   // AudioCapturer interface.
36   bool Start(const PacketCapturedCallback& callback) override;
37 
38  private:
39   // Executes Deinitialize() and Initialize(). If Initialize() function call
40   // returns false, Deinitialize() will be called again to ensure we will
41   // initialize COM components again.
42   bool ResetAndInitialize();
43 
44   // Resets all COM components to nullptr, so is_initialized() will return
45   // false.
46   void Deinitialize();
47 
48   // Initializes default audio device related components. These components must
49   // be recreated once the default audio device changed. Returns false if
50   // initialization failed.
51   bool Initialize();
52 
53   // Whether all components are correctly initialized. If last
54   // Initialize() function call failed, this function will return false.
55   // Otherwise this function will return true.
56   bool is_initialized() const;
57 
58   // Receives all packets from the audio capture endpoint buffer and pushes them
59   // to the network.
60   void DoCapture();
61 
62   PacketCapturedCallback callback_;
63 
64   AudioPacket::SamplingRate sampling_rate_;
65 
66   std::unique_ptr<base::RepeatingTimer> capture_timer_;
67   base::TimeDelta audio_device_period_;
68 
69   AudioVolumeFilterWin volume_filter_;
70 
71   base::win::ScopedCoMem<WAVEFORMATEX> wave_format_ex_;
72   Microsoft::WRL::ComPtr<IAudioCaptureClient> audio_capture_client_;
73   Microsoft::WRL::ComPtr<IAudioClient> audio_client_;
74   Microsoft::WRL::ComPtr<IMMDevice> mm_device_;
75 
76   std::unique_ptr<DefaultAudioDeviceChangeDetector> default_device_detector_;
77 
78   HRESULT last_capture_error_;
79 
80   base::ThreadChecker thread_checker_;
81 
82   DISALLOW_COPY_AND_ASSIGN(AudioCapturerWin);
83 };
84 
85 }  // namespace remoting
86 
87 #endif  // REMOTING_HOST_AUDIO_CAPTURER_WIN_H_
88