audio_daemon.h revision b3d2a54300808bfe93b5dadfcc2b75b6347e5ada
15e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan// Copyright 2016 The Android Open Source Project 25e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan// 35e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan// Licensed under the Apache License, Version 2.0 (the "License"); 45e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan// you may not use this file except in compliance with the License. 55e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan// You may obtain a copy of the License at 65e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan// 75e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan// http://www.apache.org/licenses/LICENSE-2.0 85e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan// 95e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan// Unless required by applicable law or agreed to in writing, software 105e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan// distributed under the License is distributed on an "AS IS" BASIS, 115e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 125e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan// See the License for the specific language governing permissions and 135e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan// limitations under the License. 145e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan// 155e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan 165e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan// Main loop of the brillo audio service. 175e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan 185e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan#ifndef BRILLO_AUDIO_AUDIOSERVICE_AUDIO_DAEMON_H_ 195e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan#define BRILLO_AUDIO_AUDIOSERVICE_AUDIO_DAEMON_H_ 205e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan 215e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan#include <memory> 225e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan#include <stack> 23b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan#include <vector> 245e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan 255e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan#include <base/files/file.h> 265e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan#include <base/memory/weak_ptr.h> 275e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan#include <brillo/binder_watcher.h> 285e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan#include <brillo/daemons/daemon.h> 295e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan#include <media/IAudioPolicyService.h> 305e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan 315e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan#include "audio_device_handler.h" 32b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan#include "brillo_audio_service.h" 335e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan 345e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathannamespace brillo { 355e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan 365e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathanclass AudioDaemon : public Daemon { 375e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan public: 385e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan AudioDaemon() {} 39b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan virtual ~AudioDaemon(); 405e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan 415e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan protected: 425e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan // Initialize the audio device handler and start pollig the files in 435e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan // /dev/input. 445e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan int OnInit() override; 455e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan 465e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan private: 47b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan friend class AudioDaemonTest; 48b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan FRIEND_TEST(AudioDaemonTest, RegisterService); 49b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan FRIEND_TEST(AudioDaemonTest, TestAPSConnectInitializesHandlerOnlyOnce); 50b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan FRIEND_TEST(AudioDaemonTest, TestDeviceCallbackInitializesBASIfNULL); 51b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan 525e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan // Callback function for input events. Events are handled by the audio device 535e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan // handler. 54b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan void EventCallback(base::File* file); 55b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan 56b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan // Callback function for device state changes. Events are handler by the 57b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan // audio service. 58b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan // 59b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan // |mode| is kDevicesConnected when |devices| are connected. 60b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan // |devices| is a vector of integers representing audio_devices_t. 61b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan void DeviceCallback(AudioDeviceHandler::DeviceConnectionState, 62b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan const std::vector<int>& devices); 635e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan 645e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan // Callback function for audio policy service death notification. 655e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan void OnAPSDisconnected(); 665e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan 675e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan // Connect to the audio policy service and register a callback to be invoked 685e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan // if the audio policy service dies. 695e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan void ConnectToAPS(); 705e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan 71b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan // Register the brillo audio service with the service manager. 72b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan void InitializeBrilloAudioService(); 73b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan 745e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan // Initialize the audio_device_handler_. 755e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan // 765e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan // Note: This can only occur after we have connected to the audio policy 775e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan // service. 78b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan virtual void InitializeHandler(); 795e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan 805e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan // Store the file objects that are created during initialization for the files 815e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan // being polled. This is done so these objects can be freed when the 825e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan // AudioDaemon object is destroyed. 835e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan std::stack<base::File> files_; 845e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan // Handler for audio device input events. 85b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan std::shared_ptr<AudioDeviceHandler> audio_device_handler_; 865e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan // Used to generate weak_ptr to AudioDaemon for use in base::Bind. 875e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan base::WeakPtrFactory<AudioDaemon> weak_ptr_factory_{this}; 885e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan // Pointer to the audio policy service. 895e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan android::sp<android::IAudioPolicyService> aps_; 905e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan // Flag to indicate whether the handler has been initialized. 915e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan bool handler_initialized_ = false; 925e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan // Binder watcher to watch for binder messages. 93b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan BinderWatcher binder_watcher_; 94b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan // Brillo audio service. Used for scheduling callbacks to clients. 95b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan android::sp<BrilloAudioService> brillo_audio_service_; 965e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan}; 975e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan 985e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan} // namespace brillo 995e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan 1005e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan#endif // BRILLO_AUDIO_AUDIOSERVICE_AUDIO_DAEMON_H_ 101