• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 
16 // Client for the brilloaudioservice.
17 
18 #ifndef BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_CLIENT_H_
19 #define BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_CLIENT_H_
20 
21 #include <map>
22 #include <memory>
23 #include <vector>
24 
25 #include <base/bind.h>
26 #include <base/memory/weak_ptr.h>
27 #include <gtest/gtest_prod.h>
28 #include <media/IAudioPolicyService.h>
29 
30 #include "android/brillo/brilloaudioservice/IBrilloAudioService.h"
31 #include "audio_service_callback.h"
32 
33 using android::brillo::brilloaudioservice::IBrilloAudioService;
34 
35 namespace brillo {
36 
37 class BrilloAudioClient {
38  public:
39   virtual ~BrilloAudioClient();
40 
41   // Get or create a pointer to the client instance.
42   //
43   // Returns a weak_ptr to a BrilloAudioClient object.
44   static std::weak_ptr<BrilloAudioClient> GetClientInstance();
45 
46   // Query brillo audio service to get list of connected audio devices.
47   //
48   // |flag| is an int which is either GET_DEVICES_INPUTS or GET_DEVICES_OUTPUTS.
49   // |devices| is a reference to a vector of audio_devices_t.
50   //
51   // Returns 0 on success and errno on failure.
52   int GetDevices(int flag, std::vector<int>& devices);
53 
54   // Register a callback object with the service.
55   //
56   // |callback| is a ref pointer to a callback object to be register with the
57   // brillo audio service.
58   // |callback_id| is a pointer to an int that represents a callback id token on
59   // success and 0 on failure.
60   //
61   // Returns 0 on success and errno on failure.
62   int RegisterAudioCallback(android::sp<AudioServiceCallback> callback,
63                             int* callback_id);
64 
65   // Unregister a callback object with the service.
66   //
67   // |callback_id| is an int referring to the callback object.
68   //
69   // Returns 0 on success and errno on failure.
70   int UnregisterAudioCallback(int callback_id);
71 
72   // Set a device to be the default. This does not communicate with the brillo
73   // audio service but instead communicates directly with the audio policy
74   // service.
75   //
76   // Please see system/audio_policy.h for details on these arguments.
77   //
78   // Returns 0 on success and errno on failure.
79   int SetDevice(audio_policy_force_use_t usage,
80                 audio_policy_forced_cfg_t config);
81 
82   // Get the maximum number of steps for a given BAudioUsage.
83   //
84   // |usage| is an enum of type BAudioUsage.
85   // |max_steps| is a pointer to the maximum number of steps.
86   //
87   // Returns 0 on success and errno on failure.
88   int GetMaxVolumeSteps(BAudioUsage usage, int* max_steps);
89 
90   // Set the maximum number of steps to use for a given BAudioUsage.
91   //
92   // |usage| is an enum of type BAudioUsage.
93   // |max_steps| is an int between 0 and 100.
94   //
95   // Returns 0 on success and errno on failure.
96   int SetMaxVolumeSteps(BAudioUsage usage, int max_steps);
97 
98   // Set the volume index for a given BAudioUsage and device.
99   //
100   // |usage| is an enum of type BAudioUsage.
101   // |device| is of type audio_devices_t.
102   // |index| is an int representing the current index.
103   //
104   // Returns 0 on success and errno on failure.
105   int SetVolumeIndex(BAudioUsage usage, audio_devices_t device, int index);
106 
107   // Get the volume index for a given BAudioUsage and device.
108   //
109   // |usage| is an enum of type BAudioUsage.
110   // |device| is of type audio_devices_t.
111   // |index| is a pointer to an int representing the current index.
112   //
113   // Returns 0 on success and errno on failure.
114   int GetVolumeIndex(BAudioUsage usage, audio_devices_t device, int* index);
115 
116   // Get default stream to use for volume buttons.
117   //
118   // |usage| is a pointer to a BAudioUsage.
119   //
120   // Returns 0 on success and errno on failure.
121   int GetVolumeControlStream(BAudioUsage* usage);
122 
123   // Set default stream to use for volume buttons.
124   //
125   // |usage| is an enum of type BAudioUsage.
126   //
127   // Returns 0 on success and errno on failure.
128   int SetVolumeControlStream(BAudioUsage usage);
129 
130   // Increment the volume.
131   //
132   // Returns 0 on success and errno on failure.
133   int IncrementVolume();
134 
135   // Decrement the volume.
136   //
137   // Returns 0 on success and errno on failure.
138   int DecrementVolume();
139 
140  protected:
141   BrilloAudioClient() = default;
142 
143  private:
144   friend class BrilloAudioClientTest;
145   FRIEND_TEST(BrilloAudioClientTest, InitializeNoService);
146   FRIEND_TEST(BrilloAudioClientTest,
147               CheckInitializeRegistersForDeathNotifications);
148 
149   // Initialize the BrilloAudioClient object and connects to the brillo audio
150   // service and the audio policy service. It also registers for death
151   // notifications.
152   bool Initialize();
153 
154   // Callback to be triggered when the brillo audio service dies. It attempts to
155   // reconnect to the service.
156   virtual void OnBASDisconnect();
157 
158   // Helper method to connect to a service and register a callback to receive
159   // death notifications.
160   //
161   // |service_name| is a string representing the name of the service.
162   // |callback| is a base::Closure which will be called if the service dies.
163   android::sp<android::IBinder> ConnectToService(const std::string& service_name,
164                                                  const base::Closure& callback);
165 
166   // Pointer to the BrilloAudioClient object.
167   static std::shared_ptr<BrilloAudioClient> instance_;
168 
169   // Used to generate weak_ptr to BrilloAudioClient for use in base::Bind.
170   base::WeakPtrFactory<BrilloAudioClient> weak_ptr_factory_{this};
171   // Pointer to the brillo audio service.
172   android::sp<IBrilloAudioService> brillo_audio_service_;
173   // Counter for callback IDs.
174   static int callback_id_counter_;
175   // Map of callback ids to callback objects.
176   std::map<int, android::sp<AudioServiceCallback> > callback_map_;
177 
178   DISALLOW_COPY_AND_ASSIGN(BrilloAudioClient);
179 };
180 
181 }  // namespace brillo
182 
183 #endif  // BRILLO_AUDIO_AUDIOSERVICE_BRILLO_AUDIO_CLIENT_H_
184