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 #include <vector> 24 25 #include <base/files/file.h> 26 #include <base/memory/weak_ptr.h> 27 #include <brillo/binder_watcher.h> 28 #include <brillo/daemons/daemon.h> 29 #include <media/IAudioPolicyService.h> 30 31 #include "audio_device_handler.h" 32 #include "audio_volume_handler.h" 33 #include "brillo_audio_service.h" 34 35 namespace brillo { 36 37 class AudioDaemon : public Daemon { 38 public: AudioDaemon()39 AudioDaemon() {} 40 virtual ~AudioDaemon(); 41 42 protected: 43 // Initialize the audio daemon handlers and start pollig the files in 44 // /dev/input. 45 int OnInit() override; 46 47 private: 48 friend class AudioDaemonTest; 49 FRIEND_TEST(AudioDaemonTest, RegisterService); 50 FRIEND_TEST(AudioDaemonTest, TestAPSConnectInitializesHandlersOnlyOnce); 51 FRIEND_TEST(AudioDaemonTest, TestDeviceCallbackInitializesBASIfNULL); 52 53 // Callback function for input events. Events are handled by the audio device 54 // handler. 55 void EventCallback(base::File* file); 56 57 // Callback function for device state changes. Events are handler by the 58 // audio service. 59 // 60 // |mode| is kDevicesConnected when |devices| are connected. 61 // |devices| is a vector of integers representing audio_devices_t. 62 void DeviceCallback(AudioDeviceHandler::DeviceConnectionState, 63 const std::vector<int>& devices); 64 65 // Callback function when volume changes. 66 // 67 // |stream| is an audio_stream_type_t representing the stream. 68 // |previous_index| is the volume index before the key press. 69 // |current_index| is the volume index after the key press. 70 void VolumeCallback(audio_stream_type_t stream, 71 int previous_index, 72 int current_index); 73 74 // Callback function for audio policy service death notification. 75 void OnAPSDisconnected(); 76 77 // Connect to the audio policy service and register a callback to be invoked 78 // if the audio policy service dies. 79 void ConnectToAPS(); 80 81 // Register the brillo audio service with the service manager. 82 void InitializeBrilloAudioService(); 83 84 // Initialize all audio daemon handlers. 85 // 86 // Note: This can only occur after we have connected to the audio policy 87 // service. 88 virtual void InitializeHandlers(); 89 90 // Store the file objects that are created during initialization for the files 91 // being polled. This is done so these objects can be freed when the 92 // AudioDaemon object is destroyed. 93 std::stack<base::File> files_; 94 // Handler for audio device input events. 95 std::shared_ptr<AudioDeviceHandler> audio_device_handler_; 96 // Handler for volume key press input events. 97 std::shared_ptr<AudioVolumeHandler> audio_volume_handler_; 98 // Used to generate weak_ptr to AudioDaemon for use in base::Bind. 99 base::WeakPtrFactory<AudioDaemon> weak_ptr_factory_{this}; 100 // Pointer to the audio policy service. 101 android::sp<android::IAudioPolicyService> aps_; 102 // Flag to indicate whether the handlers have been initialized. 103 bool handlers_initialized_ = false; 104 // Binder watcher to watch for binder messages. 105 BinderWatcher binder_watcher_; 106 // Brillo audio service. Used for scheduling callbacks to clients. 107 android::sp<BrilloAudioService> brillo_audio_service_; 108 }; 109 110 } // namespace brillo 111 112 #endif // BRILLO_AUDIO_AUDIOSERVICE_AUDIO_DAEMON_H_ 113