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 // Main loop of the brillo audio service.
17 
18 #ifndef BRILLO_AUDIO_AUDIOSERVICE_AUDIO_DAEMON_H_
19 #define BRILLO_AUDIO_AUDIOSERVICE_AUDIO_DAEMON_H_
20 
21 #include <memory>
22 #include <stack>
23 
24 #include <base/files/file.h>
25 #include <base/memory/weak_ptr.h>
26 #include <brillo/binder_watcher.h>
27 #include <brillo/daemons/daemon.h>
28 #include <media/IAudioPolicyService.h>
29 
30 #include "audio_device_handler.h"
31 
32 namespace brillo {
33 
34 class AudioDaemon : public Daemon {
35  public:
AudioDaemon()36   AudioDaemon() {}
37 
38  protected:
39   // Initialize the audio device handler and start pollig the files in
40   // /dev/input.
41   int OnInit() override;
42 
43  private:
44   // Callback function for input events. Events are handled by the audio device
45   // handler.
46   void Callback(base::File* file);
47 
48   // Callback function for audio policy service death notification.
49   void OnAPSDisconnected();
50 
51   // Connect to the audio policy service and register a callback to be invoked
52   // if the audio policy service dies.
53   void ConnectToAPS();
54 
55   // Initialize the audio_device_handler_.
56   //
57   // Note: This can only occur after we have connected to the audio policy
58   // service.
59   void InitializeHandler();
60 
61   // Store the file objects that are created during initialization for the files
62   // being polled. This is done so these objects can be freed when the
63   // AudioDaemon object is destroyed.
64   std::stack<base::File> files_;
65   // Handler for audio device input events.
66   std::unique_ptr<AudioDeviceHandler> audio_device_handler_;
67   // Used to generate weak_ptr to AudioDaemon for use in base::Bind.
68   base::WeakPtrFactory<AudioDaemon> weak_ptr_factory_{this};
69   // Pointer to the audio policy service.
70   android::sp<android::IAudioPolicyService> aps_;
71   // Flag to indicate whether the handler has been initialized.
72   bool handler_initialized_ = false;
73   // Binder watcher to watch for binder messages.
74   brillo::BinderWatcher binder_watcher_;
75 };
76 
77 }  // namespace brillo
78 
79 #endif  // BRILLO_AUDIO_AUDIOSERVICE_AUDIO_DAEMON_H_
80