OLD | NEW |
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 EncodedImageCallback, | 47 public EncodedImageCallback, |
52 public VCMSendStatisticsCallback { | 48 public VCMSendStatisticsCallback, |
| 49 public CpuOveruseObserver { |
53 public: | 50 public: |
54 friend class ViEBitrateObserver; | 51 ViEEncoder(uint32_t number_of_cores, |
| 52 SendStatisticsProxy* stats_proxy, |
| 53 const webrtc::VideoSendStream::Config::EncoderSettings& settings, |
| 54 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, |
| 55 LoadObserver* overuse_callback, |
| 56 EncodedFrameObserver* encoder_timing); |
| 57 ~ViEEncoder(); |
| 58 // RegisterProcessThread register |module_process_thread| with those objects |
| 59 // that use it. Registration have to happen on the thread where |
| 60 // |module_process_thread| where created (libjingles worker thread). |
| 61 // TODO(perkj): Replace the use of |module_process_thread| with a TaskQueue. |
| 62 void RegisterProcessThread(ProcessThread* module_process_thread); |
| 63 void DeRegisterProcessThread(); |
55 | 64 |
56 ViEEncoder(uint32_t number_of_cores, | 65 void SetSink(EncodedImageCallback* sink); |
57 ProcessThread* module_process_thread, | |
58 SendStatisticsProxy* stats_proxy, | |
59 OveruseFrameDetector* overuse_detector, | |
60 EncodedImageCallback* sink); | |
61 ~ViEEncoder(); | |
62 | 66 |
63 vcm::VideoSender* video_sender(); | 67 // TODO(perkj): Can we remove VideoCodec.startBitrate ? |
| 68 void SetStartBitrate(int start_bitrate_bps); |
64 | 69 |
65 // Returns the id of the owning channel. | 70 void ConfigureEncoder(const VideoEncoderConfig& config, |
66 int Owner() const; | 71 size_t max_data_payload_length); |
67 | 72 |
68 // Codec settings. | 73 // Permanently stop encoding. After this method has returned, it is |
69 int32_t RegisterExternalEncoder(VideoEncoder* encoder, | 74 // guaranteed that no encoded frames will be delivered to the sink. |
70 uint8_t pl_type, | 75 void Stop(); |
71 bool internal_source); | |
72 int32_t DeRegisterExternalEncoder(uint8_t pl_type); | |
73 void SetEncoder(const VideoCodec& video_codec, | |
74 size_t max_data_payload_length); | |
75 | 76 |
76 void EncodeVideoFrame(const VideoFrame& video_frame); | 77 // Implements VideoCaptureInput. |
| 78 // TODO(perkj): Refactor ViEEncoder to inherit rtc::VideoSink instead of |
| 79 // VideoCaptureInput. |
| 80 void IncomingCapturedFrame(const VideoFrame& video_frame) override; |
| 81 |
77 void SendKeyFrame(); | 82 void SendKeyFrame(); |
78 | 83 |
79 // Returns the time when the encoder last received an input frame or produced | |
80 // an encoded frame. | |
81 int64_t time_of_last_frame_activity_ms(); | |
82 | |
83 | |
84 // Implements EncodedImageCallback. | |
85 int32_t Encoded(const EncodedImage& encoded_image, | |
86 const CodecSpecificInfo* codec_specific_info, | |
87 const RTPFragmentationHeader* fragmentation) override; | |
88 | |
89 // Implements VideoSendStatisticsCallback. | |
90 void SendStatistics(uint32_t bit_rate, | |
91 uint32_t frame_rate, | |
92 const std::string& encoder_name) override; | |
93 | |
94 // virtual to test EncoderStateFeedback with mocks. | 84 // virtual to test EncoderStateFeedback with mocks. |
95 virtual void OnReceivedIntraFrameRequest(size_t stream_index); | 85 virtual void OnReceivedIntraFrameRequest(size_t stream_index); |
96 virtual void OnReceivedSLI(uint8_t picture_id); | 86 virtual void OnReceivedSLI(uint8_t picture_id); |
97 virtual void OnReceivedRPSI(uint64_t picture_id); | 87 virtual void OnReceivedRPSI(uint64_t picture_id); |
98 | 88 |
99 void OnBitrateUpdated(uint32_t bitrate_bps, | 89 void OnBitrateUpdated(uint32_t bitrate_bps, |
100 uint8_t fraction_lost, | 90 uint8_t fraction_lost, |
101 int64_t round_trip_time_ms); | 91 int64_t round_trip_time_ms); |
102 | 92 |
103 private: | 93 private: |
104 bool EncoderPaused() const EXCLUSIVE_LOCKS_REQUIRED(data_cs_); | 94 class EncodeTask; |
105 void TraceFrameDropStart() EXCLUSIVE_LOCKS_REQUIRED(data_cs_); | 95 |
106 void TraceFrameDropEnd() EXCLUSIVE_LOCKS_REQUIRED(data_cs_); | 96 void ConfigureEncoderInternal(const VideoCodec& video_codec, |
| 97 size_t max_data_payload_length); |
| 98 |
| 99 // Implements VideoSendStatisticsCallback. |
| 100 void SendStatistics(uint32_t bit_rate, |
| 101 uint32_t frame_rate, |
| 102 const std::string& encoder_name) override; |
| 103 |
| 104 void EncodeVideoFrame(const VideoFrame& frame); |
| 105 |
| 106 // Implements EncodedImageCallback. |
| 107 int32_t Encoded(const EncodedImage& encoded_image, |
| 108 const CodecSpecificInfo* codec_specific_info, |
| 109 const RTPFragmentationHeader* fragmentation) override; |
| 110 |
| 111 // webrtc::CpuOveruseObserver implementation. |
| 112 void OveruseDetected() override; |
| 113 void NormalUsage() override; |
| 114 |
| 115 bool EncoderPaused() const; |
| 116 void TraceFrameDropStart(); |
| 117 void TraceFrameDropEnd(); |
| 118 |
| 119 rtc::Event shutdown_event_; |
107 | 120 |
108 const uint32_t number_of_cores_; | 121 const uint32_t number_of_cores_; |
109 EncodedImageCallback* const sink_; | 122 EncodedImageCallback* sink_; |
| 123 const VideoSendStream::Config::EncoderSettings settings_; |
110 | 124 |
111 const std::unique_ptr<VideoProcessing> vp_; | 125 const std::unique_ptr<VideoProcessing> vp_; |
112 vcm::VideoSender video_sender_; | 126 vcm::VideoSender video_sender_ ACCESS_ON(&encoder_queue_); |
113 | 127 OveruseFrameDetector overuse_detector_; |
114 rtc::CriticalSection data_cs_; | 128 LoadObserver* const load_observer_ ACCESS_ON(&module_process_thread_checker_); |
115 | 129 |
116 SendStatisticsProxy* const stats_proxy_; | 130 SendStatisticsProxy* const stats_proxy_; |
117 OveruseFrameDetector* const overuse_detector_; | 131 rtc::VideoSinkInterface<VideoFrame>* const pre_encode_callback_; |
| 132 ProcessThread* module_process_thread_; |
| 133 rtc::ThreadChecker module_process_thread_checker_; |
118 | 134 |
119 // The time we last received an input frame or encoded frame. This is used to | 135 VideoCodec encoder_config_ ACCESS_ON(&encoder_queue_); |
120 // track when video is stopped long enough that we also want to stop sending | |
121 // padding. | |
122 int64_t time_of_last_frame_activity_ms_ GUARDED_BY(data_cs_); | |
123 VideoCodec encoder_config_ GUARDED_BY(data_cs_); | |
124 uint32_t last_observed_bitrate_bps_ GUARDED_BY(data_cs_); | |
125 bool encoder_paused_and_dropped_frame_ GUARDED_BY(data_cs_); | |
126 | 136 |
127 ProcessThread* module_process_thread_; | 137 int encoder_start_bitrate_bps_ ACCESS_ON(&encoder_queue_); |
| 138 uint32_t last_observed_bitrate_bps_ ACCESS_ON(&encoder_queue_); |
| 139 bool encoder_paused_and_dropped_frame_ ACCESS_ON(&encoder_queue_); |
| 140 bool has_received_sli_ ACCESS_ON(&encoder_queue_); |
| 141 uint8_t picture_id_sli_ ACCESS_ON(&encoder_queue_); |
| 142 bool has_received_rpsi_ ACCESS_ON(&encoder_queue_); |
| 143 uint64_t picture_id_rpsi_ ACCESS_ON(&encoder_queue_); |
| 144 Clock* const clock_; |
128 | 145 |
129 bool has_received_sli_ GUARDED_BY(data_cs_); | 146 rtc::RaceChecker incoming_frame_race_checker_; |
130 uint8_t picture_id_sli_ GUARDED_BY(data_cs_); | 147 Atomic32 posted_frames_waiting_for_encode_; |
131 bool has_received_rpsi_ GUARDED_BY(data_cs_); | 148 // Used to make sure incoming time stamp is increasing for every frame. |
132 uint64_t picture_id_rpsi_ GUARDED_BY(data_cs_); | 149 int64_t last_captured_timestamp_ GUARDED_BY(incoming_frame_race_checker_); |
| 150 // Delta used for translating between NTP and internal timestamps. |
| 151 const int64_t delta_ntp_internal_ms_; |
133 | 152 |
134 bool video_suspended_ GUARDED_BY(data_cs_); | 153 // |encoded_thread_checker_| check that frames are delivered from the real |
| 154 // encoder implementation on a specific thread. |
| 155 rtc::ThreadChecker encoded_thread_checker_; |
| 156 |
| 157 // All public methods are proxied to |encoder_queue_|. It must must be |
| 158 // destroyed first to make sure no tasks are run that use other members. |
| 159 rtc::TaskQueue encoder_queue_; |
135 }; | 160 }; |
136 | 161 |
137 } // namespace webrtc | 162 } // namespace webrtc |
138 | 163 |
139 #endif // WEBRTC_VIDEO_VIE_ENCODER_H_ | 164 #endif // WEBRTC_VIDEO_VIE_ENCODER_H_ |
OLD | NEW |