1 // Copyright 2016 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 COMPONENTS_ARC_AUDIO_ARC_AUDIO_BRIDGE_H_
6 #define COMPONENTS_ARC_AUDIO_ARC_AUDIO_BRIDGE_H_
7 
8 #include <string>
9 
10 #include "base/macros.h"
11 #include "chromeos/audio/cras_audio_handler.h"
12 #include "components/arc/mojom/audio.mojom.h"
13 #include "components/arc/session/connection_observer.h"
14 #include "components/keyed_service/core/keyed_service.h"
15 
16 namespace content {
17 class BrowserContext;
18 }  // namespace content
19 
20 namespace arc {
21 
22 class ArcBridgeService;
23 
24 class ArcAudioBridge : public KeyedService,
25                        public ConnectionObserver<mojom::AudioInstance>,
26                        public mojom::AudioHost,
27                        public chromeos::CrasAudioHandler::AudioObserver {
28  public:
29   // Returns singleton instance for the given BrowserContext,
30   // or nullptr if the browser |context| is not allowed to use ARC.
31   static ArcAudioBridge* GetForBrowserContext(content::BrowserContext* context);
32 
33   ArcAudioBridge(content::BrowserContext* context,
34                  ArcBridgeService* bridge_service);
35   ~ArcAudioBridge() override;
36 
37   // ConnectionObserver<mojom::AudioInstance> overrides.
38   void OnConnectionReady() override;
39   void OnConnectionClosed() override;
40 
41   // mojom::AudioHost overrides.
42   void ShowVolumeControls() override;
43   void OnSystemVolumeUpdateRequest(int32_t percent) override;
44 
45  private:
46   // chromeos::CrasAudioHandler::AudioObserver overrides.
47   void OnAudioNodesChanged() override;
48   void OnOutputNodeVolumeChanged(uint64_t node_id, int volume) override;
49   void OnOutputMuteChanged(bool mute_on) override;
50 
51   void SendSwitchState(bool headphone_inserted, bool microphone_inserted);
52   void SendVolumeState();
53 
54   ArcBridgeService* const arc_bridge_service_;  // Owned by ArcServiceManager.
55 
56   chromeos::CrasAudioHandler* cras_audio_handler_;
57 
58   int volume_ = 0;  // Volume range: 0-100.
59   bool muted_ = false;
60 
61   // Avoids sending requests when the instance is unavailable.
62   // TODO(crbug.com/549195): Remove once the root cause is fixed.
63   bool available_ = false;
64 
65   DISALLOW_COPY_AND_ASSIGN(ArcAudioBridge);
66 };
67 
68 }  // namespace arc
69 
70 #endif  // COMPONENTS_ARC_AUDIO_ARC_AUDIO_BRIDGE_H_
71