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"
32831b44060007dc2e1e1716e4b2c3d4774e5f218aRalph Nathan#include "audio_volume_handler.h"
33b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan#include "brillo_audio_service.h"
345e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan
355e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathannamespace brillo {
365e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan
375e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathanclass AudioDaemon : public Daemon {
385e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan public:
395e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  AudioDaemon() {}
40b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan  virtual ~AudioDaemon();
415e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan
425e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan protected:
43c777981a7673bb43b594a39ab9beba7ec5827be4Ralph Nathan  // Initialize the audio daemon handlers and start pollig the files in
445e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  // /dev/input.
455e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  int OnInit() override;
465e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan
475e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan private:
48b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan  friend class AudioDaemonTest;
49b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan  FRIEND_TEST(AudioDaemonTest, RegisterService);
50831b44060007dc2e1e1716e4b2c3d4774e5f218aRalph Nathan  FRIEND_TEST(AudioDaemonTest, TestAPSConnectInitializesHandlersOnlyOnce);
51b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan  FRIEND_TEST(AudioDaemonTest, TestDeviceCallbackInitializesBASIfNULL);
52b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan
535e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  // Callback function for input events. Events are handled by the audio device
545e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  // handler.
55b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan  void EventCallback(base::File* file);
56b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan
57b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan  // Callback function for device state changes. Events are handler by the
58b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan  // audio service.
59b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan  //
60b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan  // |mode| is kDevicesConnected when |devices| are connected.
61b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan  // |devices| is a vector of integers representing audio_devices_t.
62b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan  void DeviceCallback(AudioDeviceHandler::DeviceConnectionState,
63b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan                      const std::vector<int>& devices);
645e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan
65c777981a7673bb43b594a39ab9beba7ec5827be4Ralph Nathan  // Callback function when volume changes.
66c777981a7673bb43b594a39ab9beba7ec5827be4Ralph Nathan  //
67c777981a7673bb43b594a39ab9beba7ec5827be4Ralph Nathan  // |stream| is an audio_stream_type_t representing the stream.
68c777981a7673bb43b594a39ab9beba7ec5827be4Ralph Nathan  // |previous_index| is the volume index before the key press.
69c777981a7673bb43b594a39ab9beba7ec5827be4Ralph Nathan  // |current_index| is the volume index after the key press.
70c777981a7673bb43b594a39ab9beba7ec5827be4Ralph Nathan  void VolumeCallback(audio_stream_type_t stream,
71c777981a7673bb43b594a39ab9beba7ec5827be4Ralph Nathan                      int previous_index,
72c777981a7673bb43b594a39ab9beba7ec5827be4Ralph Nathan                      int current_index);
73c777981a7673bb43b594a39ab9beba7ec5827be4Ralph Nathan
745e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  // Callback function for audio policy service death notification.
755e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  void OnAPSDisconnected();
765e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan
775e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  // Connect to the audio policy service and register a callback to be invoked
785e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  // if the audio policy service dies.
795e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  void ConnectToAPS();
805e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan
81b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan  // Register the brillo audio service with the service manager.
82b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan  void InitializeBrilloAudioService();
83b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan
84831b44060007dc2e1e1716e4b2c3d4774e5f218aRalph Nathan  // Initialize all audio daemon handlers.
855e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  //
865e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  // Note: This can only occur after we have connected to the audio policy
875e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  // service.
88831b44060007dc2e1e1716e4b2c3d4774e5f218aRalph Nathan  virtual void InitializeHandlers();
895e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan
905e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  // Store the file objects that are created during initialization for the files
915e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  // being polled. This is done so these objects can be freed when the
925e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  // AudioDaemon object is destroyed.
935e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  std::stack<base::File> files_;
945e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  // Handler for audio device input events.
95b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan  std::shared_ptr<AudioDeviceHandler> audio_device_handler_;
96831b44060007dc2e1e1716e4b2c3d4774e5f218aRalph Nathan  // Handler for volume key press input events.
97c777981a7673bb43b594a39ab9beba7ec5827be4Ralph Nathan  std::shared_ptr<AudioVolumeHandler> audio_volume_handler_;
985e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  // Used to generate weak_ptr to AudioDaemon for use in base::Bind.
995e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  base::WeakPtrFactory<AudioDaemon> weak_ptr_factory_{this};
1005e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  // Pointer to the audio policy service.
1015e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  android::sp<android::IAudioPolicyService> aps_;
102831b44060007dc2e1e1716e4b2c3d4774e5f218aRalph Nathan  // Flag to indicate whether the handlers have been initialized.
103831b44060007dc2e1e1716e4b2c3d4774e5f218aRalph Nathan  bool handlers_initialized_ = false;
1045e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan  // Binder watcher to watch for binder messages.
105b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan  BinderWatcher binder_watcher_;
106b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan  // Brillo audio service. Used for scheduling callbacks to clients.
107b3d2a54300808bfe93b5dadfcc2b75b6347e5adaRalph Nathan  android::sp<BrilloAudioService> brillo_audio_service_;
1085e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan};
1095e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan
1105e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan}  // namespace brillo
1115e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan
1125e788b5ea5a8d47bb96b3a0ab9fbac8a89e94174Ralph Nathan#endif  // BRILLO_AUDIO_AUDIOSERVICE_AUDIO_DAEMON_H_
113