opensles_output.h revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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 18namespace media { 19 20class 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. 26class 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 private: 44 bool CreatePlayer(); 45 46 // Called from OpenSLES specific audio worker thread. 47 static void SimpleBufferQueueCallback( 48 SLAndroidSimpleBufferQueueItf buffer_queue, 49 void* instance); 50 51 // Fills up one buffer by asking the registered source for data. 52 // Called from OpenSLES specific audio worker thread. 53 void FillBufferQueue(); 54 55 // Called from the audio manager thread. 56 void FillBufferQueueNoLock(); 57 58 // Called in Open(); 59 void SetupAudioBuffer(); 60 61 // Called in Close(); 62 void ReleaseAudioBuffer(); 63 64 // If OpenSLES reports an error this function handles it and passes it to 65 // the attached AudioOutputCallback::OnError(). 66 void HandleError(SLresult error); 67 68 base::ThreadChecker thread_checker_; 69 70 // Protects |callback_|, |active_buffer_index_|, |audio_data_|, 71 // |buffer_size_bytes_| and |simple_buffer_queue_|. 72 base::Lock lock_; 73 74 AudioManagerAndroid* audio_manager_; 75 76 AudioSourceCallback* callback_; 77 78 // Shared engine interfaces for the app. 79 media::ScopedSLObjectItf engine_object_; 80 media::ScopedSLObjectItf player_object_; 81 media::ScopedSLObjectItf output_mixer_; 82 83 SLPlayItf player_; 84 85 // Buffer queue recorder interface. 86 SLAndroidSimpleBufferQueueItf simple_buffer_queue_; 87 88 SLDataFormat_PCM format_; 89 90 // Audio buffers that are allocated in the constructor based on 91 // info from audio parameters. 92 uint8* audio_data_[kMaxNumOfBuffersInQueue]; 93 94 int active_buffer_index_; 95 size_t buffer_size_bytes_; 96 97 bool started_; 98 99 // Volume level from 0 to 1. 100 float volume_; 101 102 // Container for retrieving data from AudioSourceCallback::OnMoreData(). 103 scoped_ptr<AudioBus> audio_bus_; 104 105 DISALLOW_COPY_AND_ASSIGN(OpenSLESOutputStream); 106}; 107 108} // namespace media 109 110#endif // MEDIA_AUDIO_ANDROID_OPENSLES_OUTPUT_H_ 111