opensles_output.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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#include <SLES/OpenSLES_AndroidConfiguration.h> 11 12#include "base/compiler_specific.h" 13#include "media/audio/android/opensles_util.h" 14#include "media/audio/audio_io.h" 15#include "media/audio/audio_parameters.h" 16 17namespace media { 18 19class AudioManagerAndroid; 20 21// Implements PCM audio output support for Android using the OpenSLES API. 22class OpenSLESOutputStream : public AudioOutputStream { 23 public: 24 static const int kNumOfQueuesInBuffer = 2; 25 26 OpenSLESOutputStream(AudioManagerAndroid* manager, 27 const AudioParameters& params); 28 29 virtual ~OpenSLESOutputStream(); 30 31 // Implementation of AudioOutputStream. 32 virtual bool Open() OVERRIDE; 33 virtual void Close() OVERRIDE; 34 virtual void Start(AudioSourceCallback* callback) OVERRIDE; 35 virtual void Stop() OVERRIDE; 36 virtual void SetVolume(double volume) OVERRIDE; 37 virtual void GetVolume(double* volume) OVERRIDE; 38 39 private: 40 bool CreatePlayer(); 41 42 static void SimpleBufferQueueCallback( 43 SLAndroidSimpleBufferQueueItf buffer_queue, void* instance); 44 45 void FillBufferQueue(); 46 47 // Called in Open(); 48 void SetupAudioBuffer(); 49 50 // Called in Close(); 51 void ReleaseAudioBuffer(); 52 53 // If OpenSLES reports an error this function handles it and passes it to 54 // the attached AudioOutputCallback::OnError(). 55 void HandleError(SLresult error); 56 57 AudioManagerAndroid* audio_manager_; 58 59 AudioSourceCallback* callback_; 60 61 // Shared engine interfaces for the app. 62 media::ScopedSLObjectItf engine_object_; 63 media::ScopedSLObjectItf player_object_; 64 media::ScopedSLObjectItf output_mixer_; 65 66 SLPlayItf player_; 67 68 // Buffer queue recorder interface. 69 SLAndroidSimpleBufferQueueItf simple_buffer_queue_; 70 71 SLDataFormat_PCM format_; 72 73 // Audio buffer arrays that are allocated in the constructor. 74 uint8* audio_data_[kNumOfQueuesInBuffer]; 75 76 int active_queue_; 77 size_t buffer_size_bytes_; 78 79 bool started_; 80 81 // Volume level from 0 to 1. 82 float volume_; 83 84 // Container for retrieving data from AudioSourceCallback::OnMoreData(). 85 scoped_ptr<AudioBus> audio_bus_; 86 87 DISALLOW_COPY_AND_ASSIGN(OpenSLESOutputStream); 88}; 89 90} // namespace media 91 92#endif // MEDIA_AUDIO_ANDROID_OPENSLES_INPUT_H_ 93