Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(336)

Side by Side Diff: webrtc/video/vie_encoder.h

Issue 2060403002: Add task queue to Call. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@move_getpadding
Patch Set: Revert fix for asan, protect instead. Added destruction observer to frames in ViEEncoder tests. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #ifndef WEBRTC_VIDEO_VIE_ENCODER_H_ 11 #ifndef WEBRTC_VIDEO_VIE_ENCODER_H_
12 #define WEBRTC_VIDEO_VIE_ENCODER_H_ 12 #define WEBRTC_VIDEO_VIE_ENCODER_H_
13 13
14 #include <memory> 14 #include <memory>
15 #include <string> 15 #include <string>
16 #include <vector> 16 #include <vector>
17 17
18 #include "webrtc/base/criticalsection.h" 18 #include "webrtc/base/criticalsection.h"
19 #include "webrtc/base/scoped_ref_ptr.h" 19 #include "webrtc/base/event.h"
20 #include "webrtc/base/thread_annotations.h" 20 #include "webrtc/base/task_queue.h"
21 #include "webrtc/call.h"
21 #include "webrtc/common_types.h" 22 #include "webrtc/common_types.h"
22 #include "webrtc/video_encoder.h"
23 #include "webrtc/media/base/videosinkinterface.h" 23 #include "webrtc/media/base/videosinkinterface.h"
24 #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
25 #include "webrtc/modules/video_coding/include/video_coding_defines.h" 24 #include "webrtc/modules/video_coding/include/video_coding_defines.h"
26 #include "webrtc/modules/video_coding/video_coding_impl.h" 25 #include "webrtc/modules/video_coding/video_coding_impl.h"
27 #include "webrtc/modules/video_processing/include/video_processing.h" 26 #include "webrtc/modules/video_processing/include/video_processing.h"
27 #include "webrtc/system_wrappers/include/atomic32.h"
28 #include "webrtc/video/overuse_frame_detector.h"
29 #include "webrtc/video_encoder.h"
30 #include "webrtc/video_send_stream.h"
28 #include "webrtc/typedefs.h" 31 #include "webrtc/typedefs.h"
29 32
30 namespace webrtc { 33 namespace webrtc {
31 34
32 class Config;
33 class EncodedImageCallback;
34 class OveruseFrameDetector;
35 class PacedSender;
36 class ProcessThread; 35 class ProcessThread;
37 class SendStatisticsProxy; 36 class SendStatisticsProxy;
38 class ViEBitrateObserver;
39 class ViEEffectFilter;
40 class VideoEncoder;
41 37
42 // VieEncoder represent a video encoder that accepts raw video frames as input 38 // VieEncoder represent a video encoder that accepts raw video frames as input
43 // and produces an encoded bit stream. 39 // and produces an encoded bit stream.
44 // Usage: 40 // Usage:
45 // 1. Instantiate 41 // Instantiate
46 // 2. Call Init 42 // Call SetStartRate and SetSink.
47 // 3. Call RegisterExternalEncoder if available. 43 // Call ConfigureEncoder with the codec settings.
48 // 4. Call SetEncoder with the codec settings and the object that shall receive 44 // Provide frames to encode by calling IncomingCapturedFrame.
49 // the encoded bit stream. 45 // Call Stop() when done.
50 // 5. For each available raw video frame call EncodeVideoFrame. 46 class ViEEncoder : public VideoCaptureInput,
51 class ViEEncoder : public VideoEncoderRateObserver, 47 public VideoEncoderRateObserver,
52 public EncodedImageCallback, 48 public EncodedImageCallback,
53 public VCMSendStatisticsCallback { 49 public VCMSendStatisticsCallback,
50 public CpuOveruseObserver {
54 public: 51 public:
55 friend class ViEBitrateObserver; 52 ViEEncoder(uint32_t number_of_cores,
53 SendStatisticsProxy* stats_proxy,
54 const webrtc::VideoSendStream::Config::EncoderSettings& settings,
55 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback,
56 LoadObserver* overuse_callback,
57 EncodedFrameObserver* encoder_timing);
58 ~ViEEncoder();
59 // RegisterProcessThread register |module_process_thread| with those objects
60 // that use it. Registration have to happen on the thread where
61 // |module_process_thread| where created (libjingles worker thread).
62 // TODO(perkj): Replace the use of |module_process_thread| with a TaskQueue.
63 void RegisterProcessThread(ProcessThread* module_process_thread);
64 void DeRegisterProcessThread();
56 65
57 ViEEncoder(uint32_t number_of_cores, 66 void SetSink(EncodedImageCallback* sink);
58 ProcessThread* module_process_thread,
59 SendStatisticsProxy* stats_proxy,
60 OveruseFrameDetector* overuse_detector,
61 EncodedImageCallback* sink);
62 ~ViEEncoder();
63 67
64 vcm::VideoSender* video_sender(); 68 // TODO(perkj): Can we remove VideoCodec.startBitrate ?
69 void SetStartBitrate(int start_bitrate_bps);
65 70
66 // Returns the id of the owning channel. 71 void ConfigureEncoder(const VideoEncoderConfig& config,
67 int Owner() const; 72 size_t max_data_payload_length);
68 73
69 // Codec settings. 74 // Permanently stop encoding. After this method has returned, it is
70 int32_t RegisterExternalEncoder(VideoEncoder* encoder, 75 // guaranteed that no encoded frames will be delivered to the sink.
71 uint8_t pl_type, 76 void Stop();
72 bool internal_source);
73 int32_t DeRegisterExternalEncoder(uint8_t pl_type);
74 void SetEncoder(const VideoCodec& video_codec,
75 size_t max_data_payload_length);
76 77
77 void EncodeVideoFrame(const VideoFrame& video_frame); 78 // Implements VideoCaptureInput.
79 // TODO(perkj): Refactor ViEEncoder to inherit rtc::VideoSink instead of
80 // VideoCaptureInput.
81 void IncomingCapturedFrame(const VideoFrame& video_frame) override;
82
78 void SendKeyFrame(); 83 void SendKeyFrame();
79 84
80 // Returns the time when the encoder last received an input frame or produced 85 // virtual to test EncoderStateFeedback with mocks.
81 // an encoded frame. 86 virtual void OnReceivedIntraFrameRequest(size_t stream_index);
82 int64_t time_of_last_frame_activity_ms(); 87 virtual void OnReceivedSLI(uint8_t picture_id);
88 virtual void OnReceivedRPSI(uint64_t picture_id);
89
90 void OnBitrateUpdated(uint32_t bitrate_bps,
91 uint8_t fraction_lost,
92 int64_t round_trip_time_ms);
93
94 private:
95 class EncodeTask;
96
97 void ConfigureEncoderInternal(const VideoCodec& video_codec,
98 size_t max_data_payload_length);
83 99
84 // Implements VideoEncoderRateObserver. 100 // Implements VideoEncoderRateObserver.
85 // TODO(perkj): Refactor VideoEncoderRateObserver. This is only used for 101 // TODO(perkj): Refactor VideoEncoderRateObserver. This is only used for
86 // stats. The stats should be set in VideoSendStream instead. 102 // stats. The stats should be set in VideoSendStream instead.
87 // |bitrate_bps| is the target bitrate and |framerate| is the input frame 103 // |bitrate_bps| is the target bitrate and |framerate| is the input frame
88 // rate so it has nothing to do with the actual encoder. 104 // rate so it has nothing to do with the actual encoder.
89 void OnSetRates(uint32_t bitrate_bps, int framerate) override; 105 void OnSetRates(uint32_t bitrate_bps, int framerate) override;
90 106
107 // Implements VideoSendStatisticsCallback.
108 void SendStatistics(uint32_t bit_rate,
109 uint32_t frame_rate,
110 const std::string& encoder_name) override;
111
112 void EncodeVideoFrame(const VideoFrame& frame);
113
91 // Implements EncodedImageCallback. 114 // Implements EncodedImageCallback.
92 int32_t Encoded(const EncodedImage& encoded_image, 115 int32_t Encoded(const EncodedImage& encoded_image,
93 const CodecSpecificInfo* codec_specific_info, 116 const CodecSpecificInfo* codec_specific_info,
94 const RTPFragmentationHeader* fragmentation) override; 117 const RTPFragmentationHeader* fragmentation) override;
95 118
96 // Implements VideoSendStatisticsCallback. 119 // webrtc::CpuOveruseObserver implementation.
97 void SendStatistics(uint32_t bit_rate, 120 void OveruseDetected() override;
98 uint32_t frame_rate, 121 void NormalUsage() override;
99 const std::string& encoder_name) override;
100 122
101 // virtual to test EncoderStateFeedback with mocks. 123 bool EncoderPaused() const;
102 virtual void OnReceivedIntraFrameRequest(size_t stream_index); 124 void TraceFrameDropStart();
103 virtual void OnReceivedSLI(uint8_t picture_id); 125 void TraceFrameDropEnd();
104 virtual void OnReceivedRPSI(uint64_t picture_id);
105 126
106 void OnBitrateUpdated(uint32_t bitrate_bps, 127 rtc::Event shutdown_event_;
107 uint8_t fraction_lost,
108 int64_t round_trip_time_ms);
109
110 private:
111 bool EncoderPaused() const EXCLUSIVE_LOCKS_REQUIRED(data_cs_);
112 void TraceFrameDropStart() EXCLUSIVE_LOCKS_REQUIRED(data_cs_);
113 void TraceFrameDropEnd() EXCLUSIVE_LOCKS_REQUIRED(data_cs_);
114 128
115 const uint32_t number_of_cores_; 129 const uint32_t number_of_cores_;
116 EncodedImageCallback* const sink_; 130 EncodedImageCallback* sink_;
131 const VideoSendStream::Config::EncoderSettings settings_;
117 132
118 const std::unique_ptr<VideoProcessing> vp_; 133 const std::unique_ptr<VideoProcessing> vp_;
119 vcm::VideoSender video_sender_; 134 vcm::VideoSender video_sender_;
120 135 OveruseFrameDetector overuse_detector_;
121 rtc::CriticalSection data_cs_; 136 LoadObserver* const load_observer_;
122 137
123 SendStatisticsProxy* const stats_proxy_; 138 SendStatisticsProxy* const stats_proxy_;
124 OveruseFrameDetector* const overuse_detector_; 139 rtc::VideoSinkInterface<VideoFrame>* const pre_encode_callback_;
140 ProcessThread* module_process_thread_;
141 rtc::ThreadChecker module_process_thread_checker_;
125 142
126 // The time we last received an input frame or encoded frame. This is used to 143 VideoCodec encoder_config_ ACCESS_ON(&encoder_queue_);
127 // track when video is stopped long enough that we also want to stop sending 144 Atomic32 posted_frames_waiting_for_encode_;
128 // padding. 145 int encoder_start_bitrate_bps_ ACCESS_ON(&encoder_queue_);
129 int64_t time_of_last_frame_activity_ms_ GUARDED_BY(data_cs_); 146 uint32_t last_observed_bitrate_bps_ ACCESS_ON(&encoder_queue_);
130 VideoCodec encoder_config_ GUARDED_BY(data_cs_); 147 bool encoder_paused_and_dropped_frame_ ACCESS_ON(&encoder_queue_);
131 uint32_t last_observed_bitrate_bps_ GUARDED_BY(data_cs_); 148 bool has_received_sli_ ACCESS_ON(&encoder_queue_);
132 bool encoder_paused_and_dropped_frame_ GUARDED_BY(data_cs_); 149 uint8_t picture_id_sli_ ACCESS_ON(&encoder_queue_);
150 bool has_received_rpsi_ ACCESS_ON(&encoder_queue_);
151 uint64_t picture_id_rpsi_ ACCESS_ON(&encoder_queue_);
152 Clock* const clock_;
133 153
134 ProcessThread* module_process_thread_; 154 // Used to make sure incoming time stamp is increasing for every frame.
155 int64_t last_captured_timestamp_;
156 // Delta used for translating between NTP and internal timestamps.
157 const int64_t delta_ntp_internal_ms_;
135 158
136 bool has_received_sli_ GUARDED_BY(data_cs_); 159 // All public methods are proxied to |encoder_queue_|. It must must be
137 uint8_t picture_id_sli_ GUARDED_BY(data_cs_); 160 // destroyed first to make sure no tasks are run that use other members.
138 bool has_received_rpsi_ GUARDED_BY(data_cs_); 161 rtc::TaskQueue encoder_queue_;
139 uint64_t picture_id_rpsi_ GUARDED_BY(data_cs_); 162 // |encoded_thread_checker_| check that frames are delivered from the real
140 163 // encoder implementation on a specific thread.
141 bool video_suspended_ GUARDED_BY(data_cs_); 164 rtc::ThreadChecker encoded_thread_checker_;
142 }; 165 };
143 166
144 } // namespace webrtc 167 } // namespace webrtc
145 168
146 #endif // WEBRTC_VIDEO_VIE_ENCODER_H_ 169 #endif // WEBRTC_VIDEO_VIE_ENCODER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698