1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71

media / audio / android / audio_track_output_stream.h [blame]

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_AUDIO_ANDROID_AUDIO_TRACK_OUTPUT_STREAM_H_
#define MEDIA_AUDIO_ANDROID_AUDIO_TRACK_OUTPUT_STREAM_H_

#include <memory>

#include "base/android/jni_android.h"
#include "base/memory/raw_ptr.h"
#include "base/time/tick_clock.h"
#include "media/audio/android/muteable_audio_output_stream.h"
#include "media/base/audio_parameters.h"

namespace media {

class AudioManagerBase;

// A MuteableAudioOutputStream implementation based on the Android AudioTrack
// API.
class MEDIA_EXPORT AudioTrackOutputStream : public MuteableAudioOutputStream {
 public:
  AudioTrackOutputStream(AudioManagerBase* manager,
                         const AudioParameters& params);

  AudioTrackOutputStream(const AudioTrackOutputStream&) = delete;
  AudioTrackOutputStream& operator=(const AudioTrackOutputStream&) = delete;

  ~AudioTrackOutputStream() override;

  // AudioOutputStream implementation.
  bool Open() override;
  void Start(AudioSourceCallback* callback) override;
  void Stop() override;
  void SetVolume(double volume) override;
  void GetVolume(double* volume) override;
  void Close() override;
  void Flush() override;

  // MuteableAudioOutputStream implementation.
  void SetMute(bool muted) override;

  // AudioOutputStream::SourceCallback implementation methods called from Java.
  base::android::ScopedJavaLocalRef<jobject> OnMoreData(JNIEnv* env,
                                                        jobject obj,
                                                        jobject audio_data,
                                                        jlong delay);
  void OnError(JNIEnv* env, jobject obj);
  jlong GetAddress(JNIEnv* env, jobject obj, jobject byte_buffer);

 private:
  const AudioParameters params_;

  raw_ptr<AudioManagerBase> audio_manager_;
  raw_ptr<AudioSourceCallback> callback_ = nullptr;
  bool muted_ = false;
  double volume_ = 1.0;

  // Extra buffer for PCM format.
  std::unique_ptr<AudioBus> audio_bus_;

  raw_ptr<const base::TickClock> tick_clock_;

  // Java AudioTrackOutputStream instance.
  base::android::ScopedJavaGlobalRef<jobject> j_audio_output_stream_;
};

}  // namespace media

#endif  // MEDIA_AUDIO_ANDROID_AUDIO_TRACK_OUTPUT_STREAM_H_