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/sequenced_task_checker.h" |
| 21 #include "webrtc/base/task_queue.h" |
| 22 #include "webrtc/call.h" |
21 #include "webrtc/common_types.h" | 23 #include "webrtc/common_types.h" |
22 #include "webrtc/video_encoder.h" | |
23 #include "webrtc/media/base/videosinkinterface.h" | 24 #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" | 25 #include "webrtc/modules/video_coding/include/video_coding_defines.h" |
26 #include "webrtc/modules/video_coding/video_coding_impl.h" | 26 #include "webrtc/modules/video_coding/video_coding_impl.h" |
27 #include "webrtc/modules/video_processing/include/video_processing.h" | 27 #include "webrtc/modules/video_processing/include/video_processing.h" |
| 28 #include "webrtc/system_wrappers/include/atomic32.h" |
| 29 #include "webrtc/video/overuse_frame_detector.h" |
| 30 #include "webrtc/video_encoder.h" |
| 31 #include "webrtc/video_send_stream.h" |
28 #include "webrtc/typedefs.h" | 32 #include "webrtc/typedefs.h" |
29 | 33 |
30 namespace webrtc { | 34 namespace webrtc { |
31 | 35 |
32 class Config; | |
33 class EncodedImageCallback; | |
34 class OveruseFrameDetector; | |
35 class PacedSender; | |
36 class ProcessThread; | 36 class ProcessThread; |
37 class SendStatisticsProxy; | 37 class SendStatisticsProxy; |
38 class ViEBitrateObserver; | |
39 class ViEEffectFilter; | |
40 class VideoEncoder; | |
41 | 38 |
42 // VieEncoder represent a video encoder that accepts raw video frames as input | 39 // VieEncoder represent a video encoder that accepts raw video frames as input |
43 // and produces an encoded bit stream. | 40 // and produces an encoded bit stream. |
44 // Usage: | 41 // Usage: |
45 // 1. Instantiate | 42 // Instantiate. |
46 // 2. Call Init | 43 // Call SetStartRate and SetSink. |
47 // 3. Call RegisterExternalEncoder if available. | 44 // Call ConfigureEncoder with the codec settings. |
48 // 4. Call SetEncoder with the codec settings and the object that shall receive | 45 // Provide frames to encode by calling IncomingCapturedFrame. |
49 // the encoded bit stream. | 46 // Call Stop() when done. |
50 // 5. For each available raw video frame call EncodeVideoFrame. | 47 class ViEEncoder : public VideoCaptureInput, |
51 class ViEEncoder : public EncodedImageCallback, | 48 public EncodedImageCallback, |
52 public VCMSendStatisticsCallback { | 49 public VCMSendStatisticsCallback, |
| 50 public CpuOveruseObserver { |
53 public: | 51 public: |
54 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 has to happen on the thread where |
| 61 // |module_process_thread| was created (libjingle's 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(); |
55 | 65 |
56 ViEEncoder(uint32_t number_of_cores, | 66 void SetSink(EncodedImageCallback* sink); |
57 ProcessThread* module_process_thread, | |
58 SendStatisticsProxy* stats_proxy, | |
59 OveruseFrameDetector* overuse_detector, | |
60 EncodedImageCallback* sink); | |
61 ~ViEEncoder(); | |
62 | 67 |
63 vcm::VideoSender* video_sender(); | 68 // TODO(perkj): Can we remove VideoCodec.startBitrate ? |
| 69 void SetStartBitrate(int start_bitrate_bps); |
64 | 70 |
65 // Returns the id of the owning channel. | 71 void ConfigureEncoder(const VideoEncoderConfig& config, |
66 int Owner() const; | 72 size_t max_data_payload_length); |
67 | 73 |
68 // Codec settings. | 74 // Permanently stop encoding. After this method has returned, it is |
69 int32_t RegisterExternalEncoder(VideoEncoder* encoder, | 75 // guaranteed that no encoded frames will be delivered to the sink. |
70 uint8_t pl_type, | 76 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 | 77 |
76 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 |
77 void SendKeyFrame(); | 83 void SendKeyFrame(); |
78 | 84 |
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 EncodedImageCallback::Result OnEncodedImage( | |
86 const EncodedImage& encoded_image, | |
87 const CodecSpecificInfo* codec_specific_info, | |
88 const RTPFragmentationHeader* fragmentation) override; | |
89 | |
90 // Implements VideoSendStatisticsCallback. | |
91 void SendStatistics(uint32_t bit_rate, uint32_t frame_rate) override; | |
92 | |
93 // virtual to test EncoderStateFeedback with mocks. | 85 // virtual to test EncoderStateFeedback with mocks. |
94 virtual void OnReceivedIntraFrameRequest(size_t stream_index); | 86 virtual void OnReceivedIntraFrameRequest(size_t stream_index); |
95 virtual void OnReceivedSLI(uint8_t picture_id); | 87 virtual void OnReceivedSLI(uint8_t picture_id); |
96 virtual void OnReceivedRPSI(uint64_t picture_id); | 88 virtual void OnReceivedRPSI(uint64_t picture_id); |
97 | 89 |
98 void OnBitrateUpdated(uint32_t bitrate_bps, | 90 void OnBitrateUpdated(uint32_t bitrate_bps, |
99 uint8_t fraction_lost, | 91 uint8_t fraction_lost, |
100 int64_t round_trip_time_ms); | 92 int64_t round_trip_time_ms); |
101 | 93 |
102 private: | 94 private: |
103 bool EncoderPaused() const EXCLUSIVE_LOCKS_REQUIRED(data_cs_); | 95 class EncodeTask; |
104 void TraceFrameDropStart() EXCLUSIVE_LOCKS_REQUIRED(data_cs_); | 96 |
105 void TraceFrameDropEnd() EXCLUSIVE_LOCKS_REQUIRED(data_cs_); | 97 void ConfigureEncoderInternal(const VideoCodec& video_codec, |
| 98 size_t max_data_payload_length); |
| 99 |
| 100 // Implements VideoSendStatisticsCallback. |
| 101 void SendStatistics(uint32_t bit_rate, |
| 102 uint32_t frame_rate) override; |
| 103 |
| 104 void EncodeVideoFrame(const VideoFrame& frame); |
| 105 |
| 106 // Implements EncodedImageCallback. |
| 107 EncodedImageCallback::Result OnEncodedImage( |
| 108 const EncodedImage& encoded_image, |
| 109 const CodecSpecificInfo* codec_specific_info, |
| 110 const RTPFragmentationHeader* fragmentation) override; |
| 111 |
| 112 // webrtc::CpuOveruseObserver implementation. |
| 113 void OveruseDetected() override; |
| 114 void NormalUsage() override; |
| 115 |
| 116 bool EncoderPaused() const; |
| 117 void TraceFrameDropStart(); |
| 118 void TraceFrameDropEnd(); |
| 119 |
| 120 rtc::Event shutdown_event_; |
106 | 121 |
107 const uint32_t number_of_cores_; | 122 const uint32_t number_of_cores_; |
108 EncodedImageCallback* const sink_; | 123 EncodedImageCallback* sink_; |
| 124 const VideoSendStream::Config::EncoderSettings settings_; |
109 | 125 |
110 const std::unique_ptr<VideoProcessing> vp_; | 126 const std::unique_ptr<VideoProcessing> vp_; |
111 vcm::VideoSender video_sender_; | 127 vcm::VideoSender video_sender_ ACCESS_ON(&encoder_queue_); |
112 | 128 OveruseFrameDetector overuse_detector_; |
113 rtc::CriticalSection data_cs_; | 129 LoadObserver* const load_observer_ ACCESS_ON(&module_process_thread_checker_); |
114 | 130 |
115 SendStatisticsProxy* const stats_proxy_; | 131 SendStatisticsProxy* const stats_proxy_; |
116 OveruseFrameDetector* const overuse_detector_; | 132 rtc::VideoSinkInterface<VideoFrame>* const pre_encode_callback_; |
| 133 ProcessThread* module_process_thread_; |
| 134 rtc::ThreadChecker module_process_thread_checker_; |
117 | 135 |
118 // The time we last received an input frame or encoded frame. This is used to | 136 VideoCodec encoder_config_ ACCESS_ON(&encoder_queue_); |
119 // track when video is stopped long enough that we also want to stop sending | |
120 // padding. | |
121 int64_t time_of_last_frame_activity_ms_ GUARDED_BY(data_cs_); | |
122 VideoCodec encoder_config_ GUARDED_BY(data_cs_); | |
123 uint32_t last_observed_bitrate_bps_ GUARDED_BY(data_cs_); | |
124 bool encoder_paused_and_dropped_frame_ GUARDED_BY(data_cs_); | |
125 | 137 |
126 ProcessThread* module_process_thread_; | 138 int encoder_start_bitrate_bps_ ACCESS_ON(&encoder_queue_); |
| 139 uint32_t last_observed_bitrate_bps_ ACCESS_ON(&encoder_queue_); |
| 140 bool encoder_paused_and_dropped_frame_ ACCESS_ON(&encoder_queue_); |
| 141 bool has_received_sli_ ACCESS_ON(&encoder_queue_); |
| 142 uint8_t picture_id_sli_ ACCESS_ON(&encoder_queue_); |
| 143 bool has_received_rpsi_ ACCESS_ON(&encoder_queue_); |
| 144 uint64_t picture_id_rpsi_ ACCESS_ON(&encoder_queue_); |
| 145 Clock* const clock_; |
127 | 146 |
128 bool has_received_sli_ GUARDED_BY(data_cs_); | 147 rtc::RaceChecker incoming_frame_race_checker_; |
129 uint8_t picture_id_sli_ GUARDED_BY(data_cs_); | 148 Atomic32 posted_frames_waiting_for_encode_; |
130 bool has_received_rpsi_ GUARDED_BY(data_cs_); | 149 // Used to make sure incoming time stamp is increasing for every frame. |
131 uint64_t picture_id_rpsi_ GUARDED_BY(data_cs_); | 150 int64_t last_captured_timestamp_ GUARDED_BY(incoming_frame_race_checker_); |
| 151 // Delta used for translating between NTP and internal timestamps. |
| 152 const int64_t delta_ntp_internal_ms_; |
132 | 153 |
133 bool video_suspended_ GUARDED_BY(data_cs_); | 154 // All public methods are proxied to |encoder_queue_|. It must must be |
| 155 // destroyed first to make sure no tasks are run that use other members. |
| 156 rtc::TaskQueue encoder_queue_; |
134 }; | 157 }; |
135 | 158 |
136 } // namespace webrtc | 159 } // namespace webrtc |
137 | 160 |
138 #endif // WEBRTC_VIDEO_VIE_ENCODER_H_ | 161 #endif // WEBRTC_VIDEO_VIE_ENCODER_H_ |
OLD | NEW |