Home | History | Annotate | Download | only in android
      1 // Copyright (c) 2012 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 MEDIA_AUDIO_ANDROID_OPENSLES_OUTPUT_H_
      6 #define MEDIA_AUDIO_ANDROID_OPENSLES_OUTPUT_H_
      7 
      8 #include <SLES/OpenSLES.h>
      9 #include <SLES/OpenSLES_Android.h>
     10 
     11 #include "base/compiler_specific.h"
     12 #include "base/synchronization/lock.h"
     13 #include "base/threading/thread_checker.h"
     14 #include "media/audio/android/opensles_util.h"
     15 #include "media/audio/audio_io.h"
     16 #include "media/audio/audio_parameters.h"
     17 
     18 namespace media {
     19 
     20 class AudioManagerAndroid;
     21 
     22 // Implements PCM audio output support for Android using the OpenSLES API.
     23 // This class is created and lives on the Audio Manager thread but recorded
     24 // audio buffers are given to us from an internal OpenSLES audio thread.
     25 // All public methods should be called on the Audio Manager thread.
     26 class OpenSLESOutputStream : public AudioOutputStream {
     27  public:
     28   static const int kMaxNumOfBuffersInQueue = 2;
     29 
     30   OpenSLESOutputStream(AudioManagerAndroid* manager,
     31                        const AudioParameters& params);
     32 
     33   virtual ~OpenSLESOutputStream();
     34 
     35   // Implementation of AudioOutputStream.
     36   virtual bool Open() OVERRIDE;
     37   virtual void Close() OVERRIDE;
     38   virtual void Start(AudioSourceCallback* callback) OVERRIDE;
     39   virtual void Stop() OVERRIDE;
     40   virtual void SetVolume(double volume) OVERRIDE;
     41   virtual void GetVolume(double* volume) OVERRIDE;
     42 
     43   // Set the value of |muted_|. It does not affect |volume_| which can be
     44   // got by calling GetVolume(). See comments for |muted_| below.
     45   void SetMute(bool muted);
     46 
     47  private:
     48   bool CreatePlayer();
     49 
     50   // Called from OpenSLES specific audio worker thread.
     51   static void SimpleBufferQueueCallback(
     52       SLAndroidSimpleBufferQueueItf buffer_queue,
     53       void* instance);
     54 
     55   // Fills up one buffer by asking the registered source for data.
     56   // Called from OpenSLES specific audio worker thread.
     57   void FillBufferQueue();
     58 
     59   // Called from the audio manager thread.
     60   void FillBufferQueueNoLock();
     61 
     62   // Called in Open();
     63   void SetupAudioBuffer();
     64 
     65   // Called in Close();
     66   void ReleaseAudioBuffer();
     67 
     68   // If OpenSLES reports an error this function handles it and passes it to
     69   // the attached AudioOutputCallback::OnError().
     70   void HandleError(SLresult error);
     71 
     72   base::ThreadChecker thread_checker_;
     73 
     74   // Protects |callback_|, |active_buffer_index_|, |audio_data_|,
     75   // |buffer_size_bytes_| and |simple_buffer_queue_|.
     76   base::Lock lock_;
     77 
     78   AudioManagerAndroid* audio_manager_;
     79 
     80   AudioSourceCallback* callback_;
     81 
     82   // Shared engine interfaces for the app.
     83   media::ScopedSLObjectItf engine_object_;
     84   media::ScopedSLObjectItf player_object_;
     85   media::ScopedSLObjectItf output_mixer_;
     86 
     87   SLPlayItf player_;
     88 
     89   // Buffer queue recorder interface.
     90   SLAndroidSimpleBufferQueueItf simple_buffer_queue_;
     91 
     92   SLDataFormat_PCM format_;
     93 
     94   // Audio buffers that are allocated in the constructor based on
     95   // info from audio parameters.
     96   uint8* audio_data_[kMaxNumOfBuffersInQueue];
     97 
     98   int active_buffer_index_;
     99   size_t buffer_size_bytes_;
    100 
    101   bool started_;
    102 
    103   // Volume control coming from hardware. It overrides |volume_| when it's
    104   // true. Otherwise, use |volume_| for scaling.
    105   // This is needed because platform voice volume never goes to zero in
    106   // COMMUNICATION mode on Android.
    107   bool muted_;
    108 
    109   // Volume level from 0 to 1.
    110   float volume_;
    111 
    112   // Container for retrieving data from AudioSourceCallback::OnMoreData().
    113   scoped_ptr<AudioBus> audio_bus_;
    114 
    115   DISALLOW_COPY_AND_ASSIGN(OpenSLESOutputStream);
    116 };
    117 
    118 }  // namespace media
    119 
    120 #endif  // MEDIA_AUDIO_ANDROID_OPENSLES_OUTPUT_H_
    121