/*
 *  Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef MODULES_VIDEO_CODING_VIDEO_RECEIVER2_H_
#define MODULES_VIDEO_CODING_VIDEO_RECEIVER2_H_

#include <cstdint>
#include <memory>

#include "absl/base/nullability.h"
#include "api/field_trials_view.h"
#include "api/sequence_checker.h"
#include "api/task_queue/task_queue_base.h"
#include "api/video/encoded_frame.h"
#include "api/video_codecs/video_decoder.h"
#include "common_video/include/corruption_score_calculator.h"
#include "modules/video_coding/decoder_database.h"
#include "modules/video_coding/generic_decoder.h"
#include "modules/video_coding/timing/timing.h"
#include "rtc_base/checks.h"
#include "rtc_base/system/no_unique_address.h"
#include "rtc_base/thread_annotations.h"
#include "system_wrappers/include/clock.h"

namespace webrtc {

// This class is a copy of vcm::VideoReceiver, trimmed down to what's used by
// VideoReceive stream, with the aim to incrementally trim it down further and
// ultimately delete it. It's difficult to do this incrementally with the
// original VideoReceiver class, since it is used by the legacy
// VideoCodingModule api.
class VideoReceiver2 {
 public:
  VideoReceiver2(Clock* clock,
                 VCMTiming* timing,
                 const FieldTrialsView& field_trials,
                 CorruptionScoreCalculator* corruption_score_calculator);
  ~VideoReceiver2();

  // Called on the worker thread. A non-null `decode_queue` is passed when the
  // stream starts, and `nullptr` is passed when it stops.
  void SetDecodeQueue(TaskQueueBase* absl_nullable decode_queue);

  void RegisterReceiveCodec(uint8_t payload_type,
                            const VideoDecoder::Settings& decoder_settings);
  void DeregisterReceiveCodec(uint8_t payload_type);
  void DeregisterReceiveCodecs();

  void RegisterExternalDecoder(std::unique_ptr<VideoDecoder> decoder,
                               uint8_t payload_type);

  bool IsExternalDecoderRegistered(uint8_t payload_type) const;
  int32_t RegisterReceiveCallback(VCMReceiveCallback* receive_callback);

  int32_t Decode(const EncodedFrame* frame);

 private:
  // Sequence checker that validates the calling thread context across stream
  // phases. Two threads/task queues are involved but only one thread is
  // active while the value of `active_queue_` is changed.
  //
  // Before we start decoding we allow changing the decoder config on the
  // worker thread (`SetDecodeQueue(decode_queue)` has not been called yet).
  // When decoding starts, `SetDecodeQueue(decode_queue)` is called on the
  // worker thread before posting tasks to `decode_queue`. The memory barrier in
  // `PostTask` ensures `active_queue_` is visible before tasks run on
  // `decode_queue`.
  //
  // When decoding stops, `SetDecodeQueue(nullptr)` is called on the worker
  // thread after ensuring `decode_queue` is drained/idle, avoiding races while
  // resetting. Once decoding is running, changing config must only be allowed
  // on the decoder queue.
  class RTC_LOCKABLE PhasedSequenceChecker {
   public:
    PhasedSequenceChecker();
    ~PhasedSequenceChecker() = default;
    bool IsCurrent() const;
    void SetDecodeQueue(TaskQueueBase* absl_nullable decode_queue);

   private:
#if RTC_DCHECK_IS_ON
    TaskQueueBase* const construction_queue_;
    TaskQueueBase* active_queue_;
#endif
  };

  PhasedSequenceChecker phased_sequence_checker_;
  RTC_NO_UNIQUE_ADDRESS SequenceChecker construction_sequence_checker_;
  RTC_NO_UNIQUE_ADDRESS SequenceChecker decoder_sequence_checker_;
  Clock* const clock_;
  VCMDecodedFrameCallback decoded_frame_callback_;
  // Callbacks are set before the decoder thread starts.
  // Once the decoder thread has been started, usage of `_codecDataBase` moves
  // over to the decoder thread.
  VCMDecoderDatabase codec_database_;
};

}  // namespace webrtc

#endif  // MODULES_VIDEO_CODING_VIDEO_RECEIVER2_H_
