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