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

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_AUDIO_BUFFER_SOURCE_HANDLER_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_AUDIO_BUFFER_SOURCE_HANDLER_H_

#include <atomic>
#include <memory>

#include "base/containers/heap_array.h"
#include "base/memory/raw_span.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/modules/webaudio/audio_buffer.h"
#include "third_party/blink/renderer/modules/webaudio/audio_param.h"
#include "third_party/blink/renderer/modules/webaudio/audio_scheduled_source_node.h"
#include "third_party/blink/renderer/modules/webaudio/panner_node.h"
#include "third_party/blink/renderer/platform/audio/audio_bus.h"
#include "third_party/blink/renderer/platform/wtf/threading.h"

namespace blink {

class AudioBufferSourceOptions;
class BaseAudioContext;

class MODULES_EXPORT AudioBufferSourceHandler final
    : public AudioScheduledSourceHandler {
 public:
  static scoped_refptr<AudioBufferSourceHandler> Create(
      AudioNode&,
      float sample_rate,
      AudioParamHandler& playback_rate,
      AudioParamHandler& detune);
  ~AudioBufferSourceHandler() override;

  // AudioHandler
  void Process(uint32_t frames_to_process) override;

  // setBuffer() is called on the main thread. This is the buffer we use for
  // playback.
  void SetBuffer(AudioBuffer*, ExceptionState&);
  SharedAudioBuffer* Buffer() { return shared_buffer_.get(); }

  // numberOfChannels() returns the number of output channels.  This value
  // equals the number of channels from the buffer.  If a new buffer is set with
  // a different number of channels, then this value will dynamically change.
  unsigned NumberOfChannels();

  // Play-state
  void Start(double when, ExceptionState&);
  void Start(double when, double grain_offset, ExceptionState&);
  void Start(double when,
             double grain_offset,
             double grain_duration,
             ExceptionState&);

  bool Loop() const { return is_looping_; }
  void SetLoop(bool looping);

  // Loop times in seconds.
  double LoopStart() const { return loop_start_; }
  double LoopEnd() const { return loop_end_; }
  void SetLoopStart(double loop_start);
  void SetLoopEnd(double loop_end);

  double GetVirtualReadIndexForTesting() const { return virtual_read_index_; }

  // If we are no longer playing, propagate silence ahead to downstream nodes.
  bool PropagatesSilence() const override;

  void HandleStoppableSourceNode() override;

 private:
  struct ProcessResult {
    unsigned write_index;
    double virtual_read_index;
  };

  AudioBufferSourceHandler(AudioNode&,
                           float sample_rate,
                           AudioParamHandler& playback_rate,
                           AudioParamHandler& detune);
  void StartSource(double when,
                   double grain_offset,
                   double grain_duration,
                   bool is_duration_given,
                   ExceptionState&);

  // Render audio directly from the buffer to the audio bus. Returns true on
  // success, i.e., audio was written to the output bus because all the internal
  // checks passed.
  //
  //   output_bus -
  //     AudioBus where the rendered audio goes.
  //   destination_frame_offset -
  //     Index into the output bus where the first frame should be written.
  //   number_of_frames -
  //     Maximum number of frames to process; this can be less that a render
  //     quantum.
  //   start_time_offset -
  //     Actual start time relative to the `destination_frame_offset`.  This
  //     should be the `start_time_offset` value returned by
  //     `UpdateSchedulingInfo`.
  bool RenderFromBuffer(AudioBus* output_bus,
                        unsigned destination_frame_offset,
                        uint32_t number_of_frames,
                        double start_time_offset);

  ProcessResult ProcessFastPath(double virtual_delta_frames,
                                double virtual_end_frame,
                                uint32_t buffer_length,
                                size_t destination_length,
                                unsigned number_of_channels,
                                int frames_to_process,
                                unsigned write_index,
                                double virtual_read_index);

  ProcessResult ProcessInterpolatedPath(double virtual_start_frame,
                                        double virtual_delta_frames,
                                        double virtual_end_frame,
                                        uint32_t buffer_length,
                                        unsigned number_of_channels,
                                        double computed_playback_rate,
                                        int frames_to_process,
                                        unsigned write_index,
                                        double virtual_read_index);

  // Render silence starting from `index` frame in AudioBus, then call
  // `Finish()`.
  inline void RenderSilenceAndFinish(size_t index, size_t frames_to_process);

  // Clamps grain parameters to the duration of the given AudioBuffer.
  void ClampGrainParameters(const SharedAudioBuffer*)
      EXCLUSIVE_LOCKS_REQUIRED(process_lock_);

  // Updates effective loop start and end points.
  void UpdateEffectiveLoopPoints() EXCLUSIVE_LOCKS_REQUIRED(process_lock_);

  bool DidSetLooping() const { return did_set_looping_; }
  void SetDidSetLooping(bool loop) {
    if (loop) {
      did_set_looping_ = true;
    }
  }

  base::WeakPtr<AudioScheduledSourceHandler> AsWeakPtr() override;

  // Compute playback rate (k-rate) by incorporating the sample rate
  // conversion factor, and the value of playbackRate and detune AudioParams.
  double ComputePlaybackRate();

  double GetMinPlaybackRate();

  // Sample data for the outputs of this node. The shared buffer can safely be
  // accessed from the audio thread.
  std::unique_ptr<SharedAudioBuffer> shared_buffer_;

  // Channel views for the source buffer and render destination.
  base::HeapArray<base::raw_span<const float>> source_channels_;
  base::HeapArray<base::raw_span<float>> destination_channels_;

  scoped_refptr<AudioParamHandler> playback_rate_;
  scoped_refptr<AudioParamHandler> detune_;

  // If `is_looping_` is false, then this node will be done playing and become
  // inactive after it reaches the end of the sample data in the buffer.  If
  // true, it will wrap around to the start of the buffer each time it reaches
  // the end.
  //
  // A process lock must be used to protect access.
  bool is_looping_ = false;

  // True if the source .loop attribute was ever set.
  // A process lock must be used to protect access.
  bool did_set_looping_ = false;

  // A process lock must be used to protect access to both `loop_start_` and
  // `loop_end_`.
  double loop_start_ = 0;
  double loop_end_ = 0;

  // Effective loop points resolved under `process_lock_`.
  double effective_loop_start_ = 0;
  double effective_loop_end_ = 0;

  // `virtual_read_index_` is a sample-frame index into our buffer representing
  // the current playback position.  Since it's floating-point, it has
  // sub-sample accuracy.
  double virtual_read_index_ = 0;

  // Granular playback
  bool is_grain_ = false;
  double grain_offset_ = 0.0;  // in seconds
  double grain_duration_;      // in seconds
  // True if `grain_duration_` is given explicitly (via 3 arg start method).
  bool is_duration_given_ = false;

  // The minimum playbackRate value ever used for this source.
  double min_playback_rate_ = 1.0;

  // The number of source frames currently output by this node.
  // This is used for keeping track of the rate invariate duration of the node.
  double buffer_played_frames_ = 0.0;

  // True if the `buffer` attribute has ever been set to a non-null
  // value.  Defaults to false.
  bool buffer_has_been_set_ = false;

  base::WeakPtrFactory<AudioScheduledSourceHandler> weak_ptr_factory_{this};
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBAUDIO_AUDIO_BUFFER_SOURCE_HANDLER_H_
