This reverts commit 8eb37a39e79fe1098d3503dcb8c8c2d196203fed. Chrome now have its own implementation of TaskQueues that is based on Chrome threads.

cl was originally reviewed here:
https://codereview.webrtc.org/2060403002/

- Add task queue to Call with the intent of replacing the use of one of the process threads.

- Split VideoSendStream in two. VideoSendStreamInternal is created and used on the new task queue.

- BitrateAllocator is now created on libjingle's worker thread but always used on the new task queue instead of both encoder threads and the process thread.

- VideoEncoderConfig and VideoSendStream::Config support move semantics.

- The encoder thread is moved from VideoSendStream to ViEEncoder. Frames are forwarded directly to ViEEncoder which is responsible for timestamping ? and encoding the frames.

TBR=mflodman@webrtc.org
BUG=webrtc:5687

Review-Url: https://codereview.webrtc.org/2250123002
Cr-Commit-Position: refs/heads/master@{#14014}
diff --git a/webrtc/video/vie_encoder.h b/webrtc/video/vie_encoder.h
index b302f9e..7408a53 100644
--- a/webrtc/video/vie_encoder.h
+++ b/webrtc/video/vie_encoder.h
@@ -16,80 +16,72 @@
 #include <vector>
 
 #include "webrtc/base/criticalsection.h"
-#include "webrtc/base/scoped_ref_ptr.h"
-#include "webrtc/base/thread_annotations.h"
+#include "webrtc/base/event.h"
+#include "webrtc/base/sequenced_task_checker.h"
+#include "webrtc/base/task_queue.h"
+#include "webrtc/call.h"
 #include "webrtc/common_types.h"
-#include "webrtc/video_encoder.h"
 #include "webrtc/media/base/videosinkinterface.h"
-#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
 #include "webrtc/modules/video_coding/include/video_coding_defines.h"
 #include "webrtc/modules/video_coding/video_coding_impl.h"
 #include "webrtc/modules/video_processing/include/video_processing.h"
+#include "webrtc/system_wrappers/include/atomic32.h"
+#include "webrtc/video/overuse_frame_detector.h"
+#include "webrtc/video_encoder.h"
+#include "webrtc/video_send_stream.h"
 #include "webrtc/typedefs.h"
 
 namespace webrtc {
 
-class Config;
-class EncodedImageCallback;
-class OveruseFrameDetector;
-class PacedSender;
 class ProcessThread;
 class SendStatisticsProxy;
-class ViEBitrateObserver;
-class ViEEffectFilter;
-class VideoEncoder;
 
 // VieEncoder represent a video encoder that accepts raw video frames as input
 // and produces an encoded bit stream.
 // Usage:
-// 1. Instantiate
-// 2. Call Init
-// 3. Call RegisterExternalEncoder if available.
-// 4. Call SetEncoder with the codec settings and the object that shall receive
-//    the encoded bit stream.
-// 5. For each available raw video frame call EncodeVideoFrame.
-class ViEEncoder : public EncodedImageCallback,
-                   public VCMSendStatisticsCallback {
+//  Instantiate.
+//  Call SetStartRate and SetSink.
+//  Call ConfigureEncoder with the codec settings.
+//  Provide frames to encode by calling IncomingCapturedFrame.
+//  Call Stop() when done.
+class ViEEncoder : public VideoCaptureInput,
+                   public EncodedImageCallback,
+                   public VCMSendStatisticsCallback,
+                   public CpuOveruseObserver {
  public:
-  friend class ViEBitrateObserver;
-
   ViEEncoder(uint32_t number_of_cores,
-             ProcessThread* module_process_thread,
              SendStatisticsProxy* stats_proxy,
-             OveruseFrameDetector* overuse_detector,
-             EncodedImageCallback* sink);
+             const webrtc::VideoSendStream::Config::EncoderSettings& settings,
+             rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback,
+             LoadObserver* overuse_callback,
+             EncodedFrameObserver* encoder_timing);
   ~ViEEncoder();
+  // RegisterProcessThread register |module_process_thread| with those objects
+  // that use it. Registration has to happen on the thread where
+  // |module_process_thread| was created (libjingle's worker thread).
+  // TODO(perkj): Replace the use of |module_process_thread| with a TaskQueue.
+  void RegisterProcessThread(ProcessThread* module_process_thread);
+  void DeRegisterProcessThread();
 
-  vcm::VideoSender* video_sender();
+  void SetSink(EncodedImageCallback* sink);
 
-  // Returns the id of the owning channel.
-  int Owner() const;
+  // TODO(perkj): Can we remove VideoCodec.startBitrate ?
+  void SetStartBitrate(int start_bitrate_bps);
 
-  // Codec settings.
-  int32_t RegisterExternalEncoder(VideoEncoder* encoder,
-                                  uint8_t pl_type,
-                                  bool internal_source);
-  int32_t DeRegisterExternalEncoder(uint8_t pl_type);
-  void SetEncoder(const VideoCodec& video_codec,
-                  size_t max_data_payload_length);
+  void ConfigureEncoder(const VideoEncoderConfig& config,
+                        size_t max_data_payload_length);
 
-  void EncodeVideoFrame(const VideoFrame& video_frame);
+  // Permanently stop encoding. After this method has returned, it is
+  // guaranteed that no encoded frames will be delivered to the sink.
+  void Stop();
+
+  // Implements VideoCaptureInput.
+  // TODO(perkj): Refactor ViEEncoder to inherit rtc::VideoSink instead of
+  // VideoCaptureInput.
+  void IncomingCapturedFrame(const VideoFrame& video_frame) override;
+
   void SendKeyFrame();
 
-  // Returns the time when the encoder last received an input frame or produced
-  // an encoded frame.
-  int64_t time_of_last_frame_activity_ms();
-
-
-  // Implements EncodedImageCallback.
-  EncodedImageCallback::Result OnEncodedImage(
-      const EncodedImage& encoded_image,
-      const CodecSpecificInfo* codec_specific_info,
-      const RTPFragmentationHeader* fragmentation) override;
-
-  // Implements VideoSendStatisticsCallback.
-  void SendStatistics(uint32_t bit_rate, uint32_t frame_rate) override;
-
   // virtual to test EncoderStateFeedback with mocks.
   virtual void OnReceivedIntraFrameRequest(size_t stream_index);
   virtual void OnReceivedSLI(uint8_t picture_id);
@@ -100,37 +92,68 @@
                         int64_t round_trip_time_ms);
 
  private:
-  bool EncoderPaused() const EXCLUSIVE_LOCKS_REQUIRED(data_cs_);
-  void TraceFrameDropStart() EXCLUSIVE_LOCKS_REQUIRED(data_cs_);
-  void TraceFrameDropEnd() EXCLUSIVE_LOCKS_REQUIRED(data_cs_);
+  class EncodeTask;
+
+  void ConfigureEncoderInternal(const VideoCodec& video_codec,
+                                size_t max_data_payload_length);
+
+  // Implements VideoSendStatisticsCallback.
+  void SendStatistics(uint32_t bit_rate,
+                      uint32_t frame_rate) override;
+
+  void EncodeVideoFrame(const VideoFrame& frame);
+
+  // Implements EncodedImageCallback.
+  EncodedImageCallback::Result OnEncodedImage(
+      const EncodedImage& encoded_image,
+      const CodecSpecificInfo* codec_specific_info,
+      const RTPFragmentationHeader* fragmentation) override;
+
+  // webrtc::CpuOveruseObserver implementation.
+  void OveruseDetected() override;
+  void NormalUsage() override;
+
+  bool EncoderPaused() const;
+  void TraceFrameDropStart();
+  void TraceFrameDropEnd();
+
+  rtc::Event shutdown_event_;
 
   const uint32_t number_of_cores_;
-  EncodedImageCallback* const sink_;
+  EncodedImageCallback* sink_;
+  const VideoSendStream::Config::EncoderSettings settings_;
 
   const std::unique_ptr<VideoProcessing> vp_;
-  vcm::VideoSender video_sender_;
-
-  rtc::CriticalSection data_cs_;
+  vcm::VideoSender video_sender_ ACCESS_ON(&encoder_queue_);
+  OveruseFrameDetector overuse_detector_;
+  LoadObserver* const load_observer_ ACCESS_ON(&module_process_thread_checker_);
 
   SendStatisticsProxy* const stats_proxy_;
-  OveruseFrameDetector* const overuse_detector_;
-
-  // The time we last received an input frame or encoded frame. This is used to
-  // track when video is stopped long enough that we also want to stop sending
-  // padding.
-  int64_t time_of_last_frame_activity_ms_ GUARDED_BY(data_cs_);
-  VideoCodec encoder_config_ GUARDED_BY(data_cs_);
-  uint32_t last_observed_bitrate_bps_ GUARDED_BY(data_cs_);
-  bool encoder_paused_and_dropped_frame_ GUARDED_BY(data_cs_);
-
+  rtc::VideoSinkInterface<VideoFrame>* const pre_encode_callback_;
   ProcessThread* module_process_thread_;
+  rtc::ThreadChecker module_process_thread_checker_;
 
-  bool has_received_sli_ GUARDED_BY(data_cs_);
-  uint8_t picture_id_sli_ GUARDED_BY(data_cs_);
-  bool has_received_rpsi_ GUARDED_BY(data_cs_);
-  uint64_t picture_id_rpsi_ GUARDED_BY(data_cs_);
+  VideoCodec encoder_config_ ACCESS_ON(&encoder_queue_);
 
-  bool video_suspended_ GUARDED_BY(data_cs_);
+  int encoder_start_bitrate_bps_ ACCESS_ON(&encoder_queue_);
+  uint32_t last_observed_bitrate_bps_ ACCESS_ON(&encoder_queue_);
+  bool encoder_paused_and_dropped_frame_ ACCESS_ON(&encoder_queue_);
+  bool has_received_sli_ ACCESS_ON(&encoder_queue_);
+  uint8_t picture_id_sli_ ACCESS_ON(&encoder_queue_);
+  bool has_received_rpsi_ ACCESS_ON(&encoder_queue_);
+  uint64_t picture_id_rpsi_ ACCESS_ON(&encoder_queue_);
+  Clock* const clock_;
+
+  rtc::RaceChecker incoming_frame_race_checker_;
+  Atomic32 posted_frames_waiting_for_encode_;
+  // Used to make sure incoming time stamp is increasing for every frame.
+  int64_t last_captured_timestamp_ GUARDED_BY(incoming_frame_race_checker_);
+  // Delta used for translating between NTP and internal timestamps.
+  const int64_t delta_ntp_internal_ms_;
+
+  // All public methods are proxied to |encoder_queue_|. It must must be
+  // destroyed first to make sure no tasks are run that use other members.
+  rtc::TaskQueue encoder_queue_;
 };
 
 }  // namespace webrtc