1 // Copyright (c) 2013 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 CHROMEOS_DBUS_AUDIO_FAKE_CRAS_AUDIO_CLIENT_H_
6 #define CHROMEOS_DBUS_AUDIO_FAKE_CRAS_AUDIO_CLIENT_H_
7 
8 #include <stdint.h>
9 
10 #include <vector>
11 
12 #include "base/component_export.h"
13 #include "base/macros.h"
14 #include "chromeos/dbus/audio/cras_audio_client.h"
15 
16 namespace chromeos {
17 
18 // The CrasAudioClient implementation used on Linux desktop.
COMPONENT_EXPORT(DBUS_AUDIO)19 class COMPONENT_EXPORT(DBUS_AUDIO) FakeCrasAudioClient
20     : public CrasAudioClient {
21  public:
22   FakeCrasAudioClient();
23   ~FakeCrasAudioClient() override;
24 
25   static FakeCrasAudioClient* Get();
26 
27   // CrasAudioClient overrides:
28   void AddObserver(Observer* observer) override;
29   void RemoveObserver(Observer* observer) override;
30   bool HasObserver(const Observer* observer) const override;
31   void GetVolumeState(DBusMethodCallback<VolumeState> callback) override;
32   void GetDefaultOutputBufferSize(DBusMethodCallback<int> callback) override;
33   void GetSystemAecSupported(DBusMethodCallback<bool> callback) override;
34   void GetSystemAecGroupId(DBusMethodCallback<int32_t> callback) override;
35   void GetNodes(DBusMethodCallback<AudioNodeList> callback) override;
36   void GetNumberOfActiveOutputStreams(
37       DBusMethodCallback<int> callback) override;
38   void GetDeprioritizeBtWbsMic(DBusMethodCallback<bool> callback) override;
39   void SetOutputNodeVolume(uint64_t node_id, int32_t volume) override;
40   void SetOutputUserMute(bool mute_on) override;
41   void SetInputNodeGain(uint64_t node_id, int32_t gain) override;
42   void SetInputMute(bool mute_on) override;
43   void SetActiveOutputNode(uint64_t node_id) override;
44   void SetActiveInputNode(uint64_t node_id) override;
45   void SetHotwordModel(uint64_t node_id,
46                        const std::string& hotword_model,
47                        VoidDBusMethodCallback callback) override;
48   void SetFixA2dpPacketSize(bool enabled) override;
49   void AddActiveInputNode(uint64_t node_id) override;
50   void RemoveActiveInputNode(uint64_t node_id) override;
51   void AddActiveOutputNode(uint64_t node_id) override;
52   void RemoveActiveOutputNode(uint64_t node_id) override;
53   void SwapLeftRight(uint64_t node_id, bool swap) override;
54   void SetGlobalOutputChannelRemix(int32_t channels,
55                                    const std::vector<double>& mixer) override;
56   void SetPlayerPlaybackStatus(const std::string& playback_status) override;
57   void SetPlayerIdentity(const std::string& playback_identity) override;
58   void SetPlayerPosition(const int64_t& position) override;
59   void SetPlayerDuration(const int64_t& duration) override;
60   void SetPlayerMetadata(
61       const std::map<std::string, std::string>& metadata) override;
62   void ResendBluetoothBattery() override;
63   void WaitForServiceToBeAvailable(
64       WaitForServiceToBeAvailableCallback callback) override;
65 
66   // Modifies an AudioNode from |node_list_| based on |audio_node.id|.
67   // if the |audio_node.id| cannot be found in list, Add an
68   // AudioNode to |node_list_|
69   void InsertAudioNodeToList(const AudioNode& audio_node);
70 
71   // Removes an AudioNode from |node_list_| based on |node_id|.
72   void RemoveAudioNodeFromList(const uint64_t& node_id);
73 
74   // Updates |node_list_| to contain |audio_nodes|.
75   void SetAudioNodesForTesting(const AudioNodeList& audio_nodes);
76 
77   // Calls SetAudioNodesForTesting() and additionally notifies |observers_|.
78   void SetAudioNodesAndNotifyObserversForTesting(
79       const AudioNodeList& new_nodes);
80 
81   // Generates fake signal for OutputNodeVolumeChanged.
82   void NotifyOutputNodeVolumeChangedForTesting(uint64_t node_id, int volume);
83 
84   // Generates fake hotword signal for HotwordTriggered.
85   void NotifyHotwordTriggeredForTesting(uint64_t tv_sec, uint64_t tv_nsec);
86 
87   // Set a mock battery level for ResendBatteryLevel.
88   void SetBluetoothBattteryLevelForTesting(uint32_t level);
89 
90   const AudioNodeList& node_list() const { return node_list_; }
91   const uint64_t& active_input_node_id() const { return active_input_node_id_; }
92   const uint64_t& active_output_node_id() const {
93     return active_output_node_id_;
94   }
95   void set_notify_volume_change_with_delay(bool notify_with_delay) {
96     notify_volume_change_with_delay_ = notify_with_delay;
97   }
98 
99  private:
100   // Finds a node in the list based on the id.
101   AudioNodeList::iterator FindNode(uint64_t node_id);
102 
103   VolumeState volume_state_;
104   AudioNodeList node_list_;
105   uint64_t active_input_node_id_ = 0;
106   uint64_t active_output_node_id_ = 0;
107   // By default, immediately sends OutputNodeVolumeChange signal following the
108   // SetOutputNodeVolume fake dbus call.
109   bool notify_volume_change_with_delay_ = false;
110   uint32_t battery_level_ = 0;
111   base::ObserverList<Observer>::Unchecked observers_;
112 
113   DISALLOW_COPY_AND_ASSIGN(FakeCrasAudioClient);
114 };
115 
116 }  // namespace chromeos
117 
118 #endif  // CHROMEOS_DBUS_AUDIO_FAKE_CRAS_AUDIO_CLIENT_H_
119