Home | History | Annotate | Download | only in audioservice
      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 // Handler for input events in /dev/input. AudioDeviceHandler handles events
     17 // only for audio devices being plugged in/removed from the system. Implements
     18 // some of the functionality present in WiredAccessoryManager.java.
     19 
     20 #ifndef BRILLO_AUDIO_AUDIOSERVICE_AUDIO_DEVICE_HANDLER_H_
     21 #define BRILLO_AUDIO_AUDIOSERVICE_AUDIO_DEVICE_HANDLER_H_
     22 
     23 #include <set>
     24 #include <vector>
     25 
     26 #include <base/files/file_path.h>
     27 #include <gtest/gtest_prod.h>
     28 #include <linux/input.h>
     29 #include <media/IAudioPolicyService.h>
     30 #include <system/audio.h>
     31 #include <system/audio_policy.h>
     32 
     33 namespace brillo {
     34 
     35 class AudioDeviceHandler {
     36  public:
     37   AudioDeviceHandler();
     38   virtual ~AudioDeviceHandler();
     39 
     40   // Get the current state of the headset jack and update AudioSystem based on
     41   // the initial state.
     42   //
     43   // |aps| is a pointer to the binder object.
     44   void Init(android::sp<android::IAudioPolicyService> aps);
     45 
     46   // Process input events from the kernel. Connecting/disconnecting an audio
     47   // device will result in multiple calls to this method.
     48   //
     49   // |event| is a pointer to an input_event. This function should be able to
     50   // gracefully handle input events that are not relevant to the functionality
     51   // provided by this class.
     52   void ProcessEvent(const struct input_event& event);
     53 
     54   // Inform the handler that the audio policy service has been disconnected.
     55   void APSDisconnect();
     56 
     57   // Inform the handler that the audio policy service is reconnected.
     58   //
     59   // |aps| is a pointer to the binder object.
     60   void APSConnect(android::sp<android::IAudioPolicyService> aps);
     61 
     62  private:
     63   friend class AudioDeviceHandlerTest;
     64   FRIEND_TEST(AudioDeviceHandlerTest,
     65               DisconnectAllSupportedDevicesCallsDisconnect);
     66   FRIEND_TEST(AudioDeviceHandlerTest, InitCallsDisconnectAllSupportedDevices);
     67   FRIEND_TEST(AudioDeviceHandlerTest, InitialAudioStateMic);
     68   FRIEND_TEST(AudioDeviceHandlerTest, InitialAudioStateHeadphone);
     69   FRIEND_TEST(AudioDeviceHandlerTest, InitialAudioStateHeadset);
     70   FRIEND_TEST(AudioDeviceHandlerTest, InitialAudioStateNone);
     71   FRIEND_TEST(AudioDeviceHandlerTest, InitialAudioStateInvalid);
     72   FRIEND_TEST(AudioDeviceHandlerTest, ProcessEventEmpty);
     73   FRIEND_TEST(AudioDeviceHandlerTest, ProcessEventMicrophonePresent);
     74   FRIEND_TEST(AudioDeviceHandlerTest, ProcessEventHeadphonePresent);
     75   FRIEND_TEST(AudioDeviceHandlerTest, ProcessEventMicrophoneNotPresent);
     76   FRIEND_TEST(AudioDeviceHandlerTest, ProcessEventHeadphoneNotPresent);
     77   FRIEND_TEST(AudioDeviceHandlerTest, ProcessEventInvalid);
     78   FRIEND_TEST(AudioDeviceHandlerTest, UpdateAudioSystemNone);
     79   FRIEND_TEST(AudioDeviceHandlerTest, UpdateAudioSystemConnectMic);
     80   FRIEND_TEST(AudioDeviceHandlerTest, UpdateAudioSystemConnectHeadphone);
     81   FRIEND_TEST(AudioDeviceHandlerTest, UpdateAudioSystemConnectHeadset);
     82   FRIEND_TEST(AudioDeviceHandlerTest, UpdateAudioSystemDisconnectMic);
     83   FRIEND_TEST(AudioDeviceHandlerTest, UpdateAudioSystemDisconnectHeadphone);
     84   FRIEND_TEST(AudioDeviceHandlerTest, UpdateAudioSystemDisconnectHeadset);
     85   FRIEND_TEST(AudioDeviceHandlerTest, ConnectAudioDeviceInput);
     86   FRIEND_TEST(AudioDeviceHandlerTest, ConnectAudioDeviceOutput);
     87   FRIEND_TEST(AudioDeviceHandlerTest, DisconnectAudioDeviceInput);
     88   FRIEND_TEST(AudioDeviceHandlerTest, DisconnectAudioDeviceOutput);
     89 
     90   // Read the initial state of audio devices in /sys/class/* and update
     91   // the audio policy service.
     92   //
     93   // |path| is the file that contains the initial audio jack state.
     94   void GetInitialAudioDeviceState(const base::FilePath& path);
     95 
     96   // Update the audio policy service once an input_event has completed.
     97   //
     98   // |headphone| is true is headphones are connected.
     99   // |microphone| is true is microphones are connected.
    100   void UpdateAudioSystem(bool headphone, bool microphone);
    101 
    102   // Notify the audio policy service that this device has been removed.
    103   //
    104   // |device| is the audio device whose state is to be changed.
    105   // |state| is the current state of |device|.
    106   virtual void NotifyAudioPolicyService(audio_devices_t device,
    107                                         audio_policy_dev_state_t state);
    108 
    109   // Connect an audio device by calling aps and add it to the appropriate set
    110   // (either connected_input_devices_ or connected_output_devices_).
    111   //
    112   // |device| is the audio device that has been added.
    113   void ConnectAudioDevice(audio_devices_t device);
    114 
    115   // Disconnect an audio device by calling aps and remove it from the
    116   // appropriate set (either connected_input_devices_ or
    117   // connected_output_devices_).
    118   //
    119   // |device| is the audio device that has been disconnected.
    120   void DisconnectAudioDevice(audio_devices_t device);
    121 
    122   // Disconnected all connected audio devices.
    123   void DisconnectAllConnectedDevices();
    124 
    125   // Disconnect all supported audio devices.
    126   void DisconnectAllSupportedDevices();
    127 
    128   // All input devices currently supported by AudioDeviceHandler.
    129   std::vector<audio_devices_t> kSupportedInputDevices_{
    130       AUDIO_DEVICE_IN_WIRED_HEADSET};
    131   // All output devices currently supported by AudioDeviceHandler.
    132   std::vector<audio_devices_t> kSupportedOutputDevices_{
    133       AUDIO_DEVICE_OUT_WIRED_HEADSET, AUDIO_DEVICE_OUT_WIRED_HEADPHONE};
    134   // Pointer to the audio policy service.
    135   android::sp<android::IAudioPolicyService> aps_;
    136 
    137  protected:
    138   // Set of connected input devices.
    139   std::set<audio_devices_t> connected_input_devices_;
    140   // Set of connected output devices.
    141   std::set<audio_devices_t> connected_output_devices_;
    142   // Keeps track of whether a headphone has been connected. Used by ProcessEvent
    143   // and UpdateAudioSystem.
    144   bool headphone_;
    145   // Keeps track of whether a microphone has been connected. Used by
    146   // ProcessEvent and UpdateAudioSystem.
    147   bool microphone_;
    148 };
    149 
    150 }  // namespace brillo
    151 
    152 #endif  // BRILLO_AUDIO_AUDIOSERVICE_AUDIO_DEVICE_HANDLER_H_
    153