Home | History | Annotate | Download | only in chromeos
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_CHROMEOS_AUDIO_HANDLER_H_
      6 #define CHROME_BROWSER_CHROMEOS_AUDIO_HANDLER_H_
      7 #pragma once
      8 
      9 #include "base/basictypes.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/threading/thread.h"
     12 
     13 class InProcessBrowserTest;
     14 template <typename T> struct DefaultSingletonTraits;
     15 
     16 namespace chromeos {
     17 
     18 class AudioMixer;
     19 
     20 class AudioHandler {
     21  public:
     22   static AudioHandler* GetInstance();
     23 
     24   // Get volume level in our internal 0-100% range, 0 being pure silence.
     25   // Returns default of 0 on error.  This function will block until the volume
     26   // is retrieved or fails.  Blocking call.
     27   double GetVolumePercent();
     28 
     29   // Set volume level from 0-100%.  Volumes above 100% are OK and boost volume,
     30   // although clipping will occur more at higher volumes.  Volume gets quieter
     31   // as the percentage gets lower, and then switches to silence at 0%.
     32   void SetVolumePercent(double volume_percent);
     33 
     34   // Adjust volume up (positive percentage) or down (negative percentage),
     35   // capping at 100%.  GetVolumePercent() will be accurate after this
     36   // blocking call.
     37   void AdjustVolumeByPercent(double adjust_by_percent);
     38 
     39   // Just returns true if mute, false if not or an error occurred.
     40   // Blocking call.
     41   bool IsMute();
     42 
     43   // Mutes all audio.  Non-blocking call.
     44   void SetMute(bool do_mute);
     45 
     46   // Disconnects from mixer.  Called during shutdown.
     47   void Disconnect();
     48 
     49  private:
     50   enum MixerType {
     51     MIXER_TYPE_ALSA = 0,
     52     MIXER_TYPE_NONE,
     53   };
     54 
     55   // Defines the delete on exit Singleton traits we like.  Best to have this
     56   // and constructor/destructor private as recommended for Singletons.
     57   friend struct DefaultSingletonTraits<AudioHandler>;
     58 
     59   friend class ::InProcessBrowserTest;
     60   // Disable audio in browser tests. This is a workaround for the bug
     61   // crosbug.com/17058. Remove this once it's fixed.
     62   static void Disable();
     63 
     64   // Connect to the current mixer_type_.
     65   bool TryToConnect(bool async);
     66 
     67   void OnMixerInitialized(bool success);
     68 
     69   AudioHandler();
     70   virtual ~AudioHandler();
     71   bool VerifyMixerConnection();
     72 
     73   // Conversion between our internal scaling (0-100%) and decibels.
     74   double VolumeDbToPercent(double volume_db) const;
     75   double PercentToVolumeDb(double volume_percent) const;
     76 
     77   scoped_ptr<AudioMixer> mixer_;
     78 
     79   bool connected_;
     80   int reconnect_tries_;
     81 
     82   // The min and max volume in decibels, limited to the maximum range of the
     83   // audio system being used.
     84   double max_volume_db_;
     85   double min_volume_db_;
     86 
     87   // Which mixer is being used, ALSA or none.
     88   MixerType mixer_type_;
     89 
     90   DISALLOW_COPY_AND_ASSIGN(AudioHandler);
     91 };
     92 
     93 }  // namespace chromeos
     94 
     95 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::AudioHandler);
     96 
     97 #endif  // CHROME_BROWSER_CHROMEOS_AUDIO_HANDLER_H_
     98