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 |
(...skipping 22 matching lines...) Expand all Loading... |
33 | 33 |
34 namespace webrtc { | 34 namespace webrtc { |
35 | 35 |
36 class ProcessThread; | 36 class ProcessThread; |
37 class SendStatisticsProxy; | 37 class SendStatisticsProxy; |
38 | 38 |
39 // 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 |
40 // and produces an encoded bit stream. | 40 // and produces an encoded bit stream. |
41 // Usage: | 41 // Usage: |
42 // Instantiate. | 42 // Instantiate. |
43 // Call SetStartRate and SetSink. | 43 // Call SetSink. |
| 44 // Call SetSource. |
44 // Call ConfigureEncoder with the codec settings. | 45 // Call ConfigureEncoder with the codec settings. |
45 // Provide frames to encode by calling IncomingCapturedFrame. | |
46 // Call Stop() when done. | 46 // Call Stop() when done. |
47 class ViEEncoder : public VideoCaptureInput, | 47 class ViEEncoder : public rtc::VideoSinkInterface<VideoFrame>, |
48 public EncodedImageCallback, | 48 public EncodedImageCallback, |
49 public VCMSendStatisticsCallback, | 49 public VCMSendStatisticsCallback, |
50 public CpuOveruseObserver { | 50 public CpuOveruseObserver { |
51 public: | 51 public: |
52 ViEEncoder(uint32_t number_of_cores, | 52 ViEEncoder(uint32_t number_of_cores, |
53 SendStatisticsProxy* stats_proxy, | 53 SendStatisticsProxy* stats_proxy, |
54 const webrtc::VideoSendStream::Config::EncoderSettings& settings, | 54 const webrtc::VideoSendStream::Config::EncoderSettings& settings, |
55 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, | 55 rtc::VideoSinkInterface<VideoFrame>* pre_encode_callback, |
56 LoadObserver* overuse_callback, | 56 LoadObserver* overuse_callback, |
57 EncodedFrameObserver* encoder_timing); | 57 EncodedFrameObserver* encoder_timing); |
58 ~ViEEncoder(); | 58 ~ViEEncoder(); |
59 // RegisterProcessThread register |module_process_thread| with those objects | 59 // RegisterProcessThread register |module_process_thread| with those objects |
60 // that use it. Registration has to happen on the thread where | 60 // that use it. Registration has to happen on the thread where |
61 // |module_process_thread| was created (libjingle's worker thread). | 61 // |module_process_thread| was created (libjingle's worker thread). |
62 // TODO(perkj): Replace the use of |module_process_thread| with a TaskQueue. | 62 // TODO(perkj): Replace the use of |module_process_thread| with a TaskQueue. |
63 void RegisterProcessThread(ProcessThread* module_process_thread); | 63 void RegisterProcessThread(ProcessThread* module_process_thread); |
64 void DeRegisterProcessThread(); | 64 void DeRegisterProcessThread(); |
65 | 65 |
| 66 void SetSource(rtc::VideoSourceInterface<VideoFrame>* source); |
66 void SetSink(EncodedImageCallback* sink); | 67 void SetSink(EncodedImageCallback* sink); |
67 | 68 |
68 // TODO(perkj): Can we remove VideoCodec.startBitrate ? | 69 // TODO(perkj): Can we remove VideoCodec.startBitrate ? |
69 void SetStartBitrate(int start_bitrate_bps); | 70 void SetStartBitrate(int start_bitrate_bps); |
70 | 71 |
71 void ConfigureEncoder(const VideoEncoderConfig& config, | 72 void ConfigureEncoder(const VideoEncoderConfig& config, |
72 size_t max_data_payload_length); | 73 size_t max_data_payload_length); |
73 | 74 |
74 // Permanently stop encoding. After this method has returned, it is | 75 // Permanently stop encoding. After this method has returned, it is |
75 // guaranteed that no encoded frames will be delivered to the sink. | 76 // guaranteed that no encoded frames will be delivered to the sink. |
76 void Stop(); | 77 void Stop(); |
77 | 78 |
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 | |
83 void SendKeyFrame(); | 79 void SendKeyFrame(); |
84 | 80 |
85 // virtual to test EncoderStateFeedback with mocks. | 81 // virtual to test EncoderStateFeedback with mocks. |
86 virtual void OnReceivedIntraFrameRequest(size_t stream_index); | 82 virtual void OnReceivedIntraFrameRequest(size_t stream_index); |
87 virtual void OnReceivedSLI(uint8_t picture_id); | 83 virtual void OnReceivedSLI(uint8_t picture_id); |
88 virtual void OnReceivedRPSI(uint64_t picture_id); | 84 virtual void OnReceivedRPSI(uint64_t picture_id); |
89 | 85 |
90 void OnBitrateUpdated(uint32_t bitrate_bps, | 86 void OnBitrateUpdated(uint32_t bitrate_bps, |
91 uint8_t fraction_lost, | 87 uint8_t fraction_lost, |
92 int64_t round_trip_time_ms); | 88 int64_t round_trip_time_ms); |
93 | 89 |
94 private: | 90 private: |
95 class EncodeTask; | 91 class EncodeTask; |
| 92 class VideoSourceProxy; |
96 | 93 |
97 void ConfigureEncoderInternal(const VideoCodec& video_codec, | 94 void ConfigureEncoderInternal(const VideoCodec& video_codec, |
98 size_t max_data_payload_length); | 95 size_t max_data_payload_length); |
99 | 96 |
| 97 // Implements VideoSinkInterface. |
| 98 void OnFrame(const VideoFrame& video_frame) override; |
| 99 |
100 // Implements VideoSendStatisticsCallback. | 100 // Implements VideoSendStatisticsCallback. |
101 void SendStatistics(uint32_t bit_rate, | 101 void SendStatistics(uint32_t bit_rate, |
102 uint32_t frame_rate) override; | 102 uint32_t frame_rate) override; |
103 | 103 |
104 void EncodeVideoFrame(const VideoFrame& frame, | 104 void EncodeVideoFrame(const VideoFrame& frame, |
105 int64_t time_when_posted_in_ms); | 105 int64_t time_when_posted_in_ms); |
106 | 106 |
107 // Implements EncodedImageCallback. | 107 // Implements EncodedImageCallback. |
108 EncodedImageCallback::Result OnEncodedImage( | 108 EncodedImageCallback::Result OnEncodedImage( |
109 const EncodedImage& encoded_image, | 109 const EncodedImage& encoded_image, |
110 const CodecSpecificInfo* codec_specific_info, | 110 const CodecSpecificInfo* codec_specific_info, |
111 const RTPFragmentationHeader* fragmentation) override; | 111 const RTPFragmentationHeader* fragmentation) override; |
112 | 112 |
113 // webrtc::CpuOveruseObserver implementation. | 113 // webrtc::CpuOveruseObserver implementation. |
114 void OveruseDetected() override; | 114 void OveruseDetected() override; |
115 void NormalUsage() override; | 115 void NormalUsage() override; |
116 | 116 |
117 bool EncoderPaused() const; | 117 bool EncoderPaused() const; |
118 void TraceFrameDropStart(); | 118 void TraceFrameDropStart(); |
119 void TraceFrameDropEnd(); | 119 void TraceFrameDropEnd(); |
120 | 120 |
121 rtc::Event shutdown_event_; | 121 rtc::Event shutdown_event_; |
122 | 122 |
123 const uint32_t number_of_cores_; | 123 const uint32_t number_of_cores_; |
| 124 |
| 125 const std::unique_ptr<VideoSourceProxy> source_proxy_; |
124 EncodedImageCallback* sink_; | 126 EncodedImageCallback* sink_; |
125 const VideoSendStream::Config::EncoderSettings settings_; | 127 const VideoSendStream::Config::EncoderSettings settings_; |
126 | 128 |
127 const std::unique_ptr<VideoProcessing> vp_; | 129 const std::unique_ptr<VideoProcessing> vp_; |
128 vcm::VideoSender video_sender_ ACCESS_ON(&encoder_queue_); | 130 vcm::VideoSender video_sender_ ACCESS_ON(&encoder_queue_); |
129 | 131 |
130 OveruseFrameDetector overuse_detector_ ACCESS_ON(&encoder_queue_); | 132 OveruseFrameDetector overuse_detector_ ACCESS_ON(&encoder_queue_); |
131 LoadObserver* const load_observer_ ACCESS_ON(&encoder_queue_); | 133 LoadObserver* const load_observer_ ACCESS_ON(&encoder_queue_); |
132 | 134 |
133 SendStatisticsProxy* const stats_proxy_; | 135 SendStatisticsProxy* const stats_proxy_; |
134 rtc::VideoSinkInterface<VideoFrame>* const pre_encode_callback_; | 136 rtc::VideoSinkInterface<VideoFrame>* const pre_encode_callback_; |
135 ProcessThread* module_process_thread_; | 137 ProcessThread* module_process_thread_; |
136 rtc::ThreadChecker module_process_thread_checker_; | 138 rtc::ThreadChecker module_process_thread_checker_; |
| 139 // |thread_checker_| checks that public methods that are related to lifetime |
| 140 // of ViEEncoder are called on the same thread. |
| 141 rtc::ThreadChecker thread_checker_; |
137 | 142 |
138 VideoCodec encoder_config_ ACCESS_ON(&encoder_queue_); | 143 VideoCodec encoder_config_ ACCESS_ON(&encoder_queue_); |
139 | 144 |
140 int encoder_start_bitrate_bps_ ACCESS_ON(&encoder_queue_); | 145 int encoder_start_bitrate_bps_ ACCESS_ON(&encoder_queue_); |
141 uint32_t last_observed_bitrate_bps_ ACCESS_ON(&encoder_queue_); | 146 uint32_t last_observed_bitrate_bps_ ACCESS_ON(&encoder_queue_); |
142 bool encoder_paused_and_dropped_frame_ ACCESS_ON(&encoder_queue_); | 147 bool encoder_paused_and_dropped_frame_ ACCESS_ON(&encoder_queue_); |
143 bool has_received_sli_ ACCESS_ON(&encoder_queue_); | 148 bool has_received_sli_ ACCESS_ON(&encoder_queue_); |
144 uint8_t picture_id_sli_ ACCESS_ON(&encoder_queue_); | 149 uint8_t picture_id_sli_ ACCESS_ON(&encoder_queue_); |
145 bool has_received_rpsi_ ACCESS_ON(&encoder_queue_); | 150 bool has_received_rpsi_ ACCESS_ON(&encoder_queue_); |
146 uint64_t picture_id_rpsi_ ACCESS_ON(&encoder_queue_); | 151 uint64_t picture_id_rpsi_ ACCESS_ON(&encoder_queue_); |
147 Clock* const clock_; | 152 Clock* const clock_; |
148 | 153 |
149 rtc::RaceChecker incoming_frame_race_checker_; | 154 rtc::RaceChecker incoming_frame_race_checker_; |
150 Atomic32 posted_frames_waiting_for_encode_; | 155 Atomic32 posted_frames_waiting_for_encode_; |
151 // Used to make sure incoming time stamp is increasing for every frame. | 156 // Used to make sure incoming time stamp is increasing for every frame. |
152 int64_t last_captured_timestamp_ GUARDED_BY(incoming_frame_race_checker_); | 157 int64_t last_captured_timestamp_ GUARDED_BY(incoming_frame_race_checker_); |
153 // Delta used for translating between NTP and internal timestamps. | 158 // Delta used for translating between NTP and internal timestamps. |
154 const int64_t delta_ntp_internal_ms_; | 159 const int64_t delta_ntp_internal_ms_; |
155 | 160 |
156 int64_t last_frame_log_ms_ GUARDED_BY(incoming_frame_race_checker_); | 161 int64_t last_frame_log_ms_ GUARDED_BY(incoming_frame_race_checker_); |
157 int captured_frame_count_ ACCESS_ON(&encoder_queue_); | 162 int captured_frame_count_ ACCESS_ON(&encoder_queue_); |
158 int dropped_frame_count_ ACCESS_ON(&encoder_queue_); | 163 int dropped_frame_count_ ACCESS_ON(&encoder_queue_); |
159 | 164 |
160 // All public methods are proxied to |encoder_queue_|. It must must be | 165 // All public methods are proxied to |encoder_queue_|. It must must be |
161 // destroyed first to make sure no tasks are run that use other members. | 166 // destroyed first to make sure no tasks are run that use other members. |
162 rtc::TaskQueue encoder_queue_; | 167 rtc::TaskQueue encoder_queue_; |
| 168 |
| 169 RTC_DISALLOW_COPY_AND_ASSIGN(ViEEncoder); |
163 }; | 170 }; |
164 | 171 |
165 } // namespace webrtc | 172 } // namespace webrtc |
166 | 173 |
167 #endif // WEBRTC_VIDEO_VIE_ENCODER_H_ | 174 #endif // WEBRTC_VIDEO_VIE_ENCODER_H_ |
OLD | NEW |