andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_ |
| 12 | #define WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_ |
| 13 | |
mflodman@webrtc.org | b6d9cfc | 2012-10-25 11:30:29 | [diff] [blame] | 14 | #include <map> |
mflodman@webrtc.org | c51222c | 2015-02-06 13:10:19 | [diff] [blame] | 15 | #include <vector> |
mflodman@webrtc.org | b6d9cfc | 2012-10-25 11:30:29 | [diff] [blame] | 16 | |
mflodman@webrtc.org | 867d288 | 2015-02-10 08:58:16 | [diff] [blame] | 17 | #include "webrtc/base/scoped_ptr.h" |
Peter Boström | 623f2f0 | 2015-06-04 13:18:17 | [diff] [blame] | 18 | #include "webrtc/base/scoped_ref_ptr.h" |
pbos@webrtc.org | d54aa96 | 2014-09-24 06:05:00 | [diff] [blame] | 19 | #include "webrtc/base/thread_annotations.h" |
Henrik Kjellander | fe7633e | 2015-11-18 21:00:21 | [diff] [blame^] | 20 | #include "webrtc/call/bitrate_allocator.h" |
pbos@webrtc.org | 281cff8 | 2013-05-17 13:44:48 | [diff] [blame] | 21 | #include "webrtc/common_types.h" |
Peter Boström | 623f2f0 | 2015-06-04 13:18:17 | [diff] [blame] | 22 | #include "webrtc/frame_callback.h" |
Henrik Kjellander | 36a14b5 | 2015-11-04 07:31:52 | [diff] [blame] | 23 | #include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
Henrik Kjellander | fe7633e | 2015-11-18 21:00:21 | [diff] [blame^] | 24 | #include "webrtc/modules/video_coding/include/video_coding_defines.h" |
pbos@webrtc.org | 281cff8 | 2013-05-17 13:44:48 | [diff] [blame] | 25 | #include "webrtc/modules/video_processing/main/interface/video_processing.h" |
pbos@webrtc.org | 281cff8 | 2013-05-17 13:44:48 | [diff] [blame] | 26 | #include "webrtc/typedefs.h" |
Peter Boström | d5d98ba | 2015-06-26 04:58:16 | [diff] [blame] | 27 | #include "webrtc/video/video_capture_input.h" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 28 | |
| 29 | namespace webrtc { |
| 30 | |
mflodman | 1b78cc3 | 2015-11-13 05:02:42 | [diff] [blame] | 31 | class BitrateAllocator; |
| 32 | class BitrateObserver; |
andresp@webrtc.org | ac6d919 | 2013-05-13 10:50:50 | [diff] [blame] | 33 | class Config; |
mflodman@webrtc.org | bf76ae2 | 2013-07-23 11:35:00 | [diff] [blame] | 34 | class CriticalSectionWrapper; |
sprang@webrtc.org | 2e98d45 | 2013-11-26 11:41:59 | [diff] [blame] | 35 | class EncodedImageCallback; |
pwestin@webrtc.org | 5e87b5f | 2012-11-13 21:12:39 | [diff] [blame] | 36 | class PacedSender; |
mflodman@webrtc.org | c51222c | 2015-02-06 13:10:19 | [diff] [blame] | 37 | class PayloadRouter; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 38 | class ProcessThread; |
| 39 | class QMVideoSettingsCallback; |
pbos@webrtc.org | ee9497c | 2014-12-01 15:23:21 | [diff] [blame] | 40 | class SendStatisticsProxy; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 41 | class ViEBitrateObserver; |
| 42 | class ViEEffectFilter; |
mflodman@webrtc.org | bf76ae2 | 2013-07-23 11:35:00 | [diff] [blame] | 43 | class VideoCodingModule; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 44 | |
Peter Boström | d5d98ba | 2015-06-26 04:58:16 | [diff] [blame] | 45 | class ViEEncoder : public RtcpIntraFrameObserver, |
| 46 | public VideoEncoderRateObserver, |
| 47 | public VCMPacketizationCallback, |
| 48 | public VCMSendStatisticsCallback, |
| 49 | public VideoCaptureCallback { |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 50 | public: |
| 51 | friend class ViEBitrateObserver; |
| 52 | |
mflodman | fd3f2cd | 2015-10-19 15:12:12 | [diff] [blame] | 53 | ViEEncoder(uint32_t number_of_cores, |
Peter Boström | 39ac69b | 2015-09-22 14:28:51 | [diff] [blame] | 54 | ProcessThread* module_process_thread, |
| 55 | SendStatisticsProxy* stats_proxy, |
| 56 | I420FrameCallback* pre_encode_callback, |
Stefan Holmer | ca55fa1 | 2015-03-26 10:11:06 | [diff] [blame] | 57 | PacedSender* pacer, |
Peter Boström | eff34fc | 2015-09-15 13:08:03 | [diff] [blame] | 58 | BitrateAllocator* bitrate_allocator); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 59 | ~ViEEncoder(); |
| 60 | |
| 61 | bool Init(); |
| 62 | |
mflodman@webrtc.org | aca8c5e | 2015-02-17 10:15:06 | [diff] [blame] | 63 | // This function is assumed to be called before any frames are delivered and |
| 64 | // only once. |
| 65 | // Ideally this would be done in Init, but the dependencies between ViEEncoder |
| 66 | // and ViEChannel makes it really hard to do in a good way. |
mflodman@webrtc.org | 52be6c2 | 2015-02-25 13:50:10 | [diff] [blame] | 67 | void StartThreadsAndSetSharedMembers( |
Peter Boström | 623f2f0 | 2015-06-04 13:18:17 | [diff] [blame] | 68 | rtc::scoped_refptr<PayloadRouter> send_payload_router, |
mflodman@webrtc.org | 52be6c2 | 2015-02-25 13:50:10 | [diff] [blame] | 69 | VCMProtectionCallback* vcm_protection_callback); |
mflodman@webrtc.org | aca8c5e | 2015-02-17 10:15:06 | [diff] [blame] | 70 | |
| 71 | // This function must be called before the corresponding ViEChannel is |
| 72 | // deleted. |
mflodman@webrtc.org | 52be6c2 | 2015-02-25 13:50:10 | [diff] [blame] | 73 | void StopThreadsAndRemoveSharedMembers(); |
mflodman@webrtc.org | c51222c | 2015-02-06 13:10:19 | [diff] [blame] | 74 | |
stefan@webrtc.org | dca71b2 | 2013-03-27 16:36:01 | [diff] [blame] | 75 | void SetNetworkTransmissionState(bool is_transmitting); |
| 76 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 77 | // Returns the id of the owning channel. |
| 78 | int Owner() const; |
| 79 | |
| 80 | // Drops incoming packets before they get to the encoder. |
| 81 | void Pause(); |
| 82 | void Restart(); |
| 83 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 84 | // Codec settings. |
pbos@webrtc.org | 67879bc | 2013-04-09 13:41:51 | [diff] [blame] | 85 | uint8_t NumberOfCodecs(); |
| 86 | int32_t GetCodec(uint8_t list_index, VideoCodec* video_codec); |
| 87 | int32_t RegisterExternalEncoder(VideoEncoder* encoder, |
| 88 | uint8_t pl_type, |
| 89 | bool internal_source); |
| 90 | int32_t DeRegisterExternalEncoder(uint8_t pl_type); |
| 91 | int32_t SetEncoder(const VideoCodec& video_codec); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 92 | |
Peter Boström | d5d98ba | 2015-06-26 04:58:16 | [diff] [blame] | 93 | // Implementing VideoCaptureCallback. |
Miguel Casas-Sanchez | 3ca60f3 | 2015-05-30 00:21:40 | [diff] [blame] | 94 | void DeliverFrame(VideoFrame video_frame) override; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 95 | |
pbos@webrtc.org | 67879bc | 2013-04-09 13:41:51 | [diff] [blame] | 96 | int32_t SendKeyFrame(); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 97 | |
pbos@webrtc.org | 7131030 | 2015-03-18 14:40:03 | [diff] [blame] | 98 | uint32_t LastObservedBitrateBps() const; |
pbos@webrtc.org | 67879bc | 2013-04-09 13:41:51 | [diff] [blame] | 99 | int CodecTargetBitrate(uint32_t* bitrate) const; |
Peter Boström | 2b918e6 | 2015-11-10 15:27:12 | [diff] [blame] | 100 | // Loss protection. Must be called before SetEncoder() to have max packet size |
| 101 | // updated according to protection. |
| 102 | // TODO(pbos): Set protection method on construction or extract vcm_ outside |
| 103 | // this class and set it on construction there. |
| 104 | void SetProtectionMethod(bool nack, bool fec); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 105 | |
mikhal@webrtc.org | 9d6fcb3 | 2013-02-15 23:22:18 | [diff] [blame] | 106 | // Buffering mode. |
| 107 | void SetSenderBufferingMode(int target_delay_ms); |
mikhal@webrtc.org | 0c66de6 | 2013-02-10 18:42:55 | [diff] [blame] | 108 | |
pbos@webrtc.org | 34c89ae | 2015-02-26 13:15:22 | [diff] [blame] | 109 | // Implements VideoEncoderRateObserver. |
| 110 | void OnSetRates(uint32_t bitrate_bps, int framerate) override; |
| 111 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 112 | // Implements VCMPacketizationCallback. |
kjellander@webrtc.org | 860ac53 | 2015-03-04 12:58:35 | [diff] [blame] | 113 | int32_t SendData(uint8_t payload_type, |
| 114 | const EncodedImage& encoded_image, |
| 115 | const RTPFragmentationHeader& fragmentation_header, |
| 116 | const RTPVideoHeader* rtp_video_hdr) override; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 117 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 118 | // Implements VideoSendStatisticsCallback. |
kjellander@webrtc.org | 860ac53 | 2015-03-04 12:58:35 | [diff] [blame] | 119 | int32_t SendStatistics(const uint32_t bit_rate, |
| 120 | const uint32_t frame_rate) override; |
henrike@webrtc.org | 9cd0bbc | 2014-09-11 22:45:54 | [diff] [blame] | 121 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 122 | // Implements RtcpIntraFrameObserver. |
kjellander@webrtc.org | 860ac53 | 2015-03-04 12:58:35 | [diff] [blame] | 123 | void OnReceivedIntraFrameRequest(uint32_t ssrc) override; |
| 124 | void OnReceivedSLI(uint32_t ssrc, uint8_t picture_id) override; |
| 125 | void OnReceivedRPSI(uint32_t ssrc, uint64_t picture_id) override; |
| 126 | void OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc) override; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 127 | |
mflodman@webrtc.org | b6d9cfc | 2012-10-25 11:30:29 | [diff] [blame] | 128 | // Sets SSRCs for all streams. |
Peter Boström | 2b918e6 | 2015-11-10 15:27:12 | [diff] [blame] | 129 | void SetSsrcs(const std::vector<uint32_t>& ssrcs); |
mflodman@webrtc.org | b6d9cfc | 2012-10-25 11:30:29 | [diff] [blame] | 130 | |
pbos@webrtc.org | 3f83f9c | 2014-03-13 12:52:27 | [diff] [blame] | 131 | void SetMinTransmitBitrate(int min_transmit_bitrate_kbps); |
| 132 | |
henrik.lundin@webrtc.org | 4590177 | 2013-11-18 12:18:43 | [diff] [blame] | 133 | // Lets the sender suspend video when the rate drops below |
henrik.lundin@webrtc.org | 39079d1 | 2013-10-02 13:34:26 | [diff] [blame] | 134 | // |threshold_bps|, and turns back on when the rate goes back up above |
| 135 | // |threshold_bps| + |window_bps|. |
henrike@webrtc.org | 9cd0bbc | 2014-09-11 22:45:54 | [diff] [blame] | 136 | void SuspendBelowMinBitrate(); |
henrik.lundin@webrtc.org | 39079d1 | 2013-10-02 13:34:26 | [diff] [blame] | 137 | |
sprang@webrtc.org | 2e98d45 | 2013-11-26 11:41:59 | [diff] [blame] | 138 | // New-style callbacks, used by VideoSendStream. |
sprang@webrtc.org | 2e98d45 | 2013-11-26 11:41:59 | [diff] [blame] | 139 | void RegisterPostEncodeImageCallback( |
| 140 | EncodedImageCallback* post_encode_callback); |
pbos@webrtc.org | 63301bd | 2013-10-21 10:34:43 | [diff] [blame] | 141 | |
stefan | 8e71cce | 2015-07-15 11:39:22 | [diff] [blame] | 142 | int GetPaddingNeededBps() const; |
Stefan Holmer | ca55fa1 | 2015-03-26 10:11:06 | [diff] [blame] | 143 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 144 | protected: |
| 145 | // Called by BitrateObserver. |
stefan@webrtc.org | a5f17c8 | 2014-12-08 19:46:23 | [diff] [blame] | 146 | void OnNetworkChanged(uint32_t bitrate_bps, |
| 147 | uint8_t fraction_lost, |
pkasting@chromium.org | a3166c4 | 2015-01-12 21:51:21 | [diff] [blame] | 148 | int64_t round_trip_time_ms); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 149 | |
| 150 | private: |
stefan@webrtc.org | fedbe8b | 2014-07-09 14:46:31 | [diff] [blame] | 151 | bool EncoderPaused() const EXCLUSIVE_LOCKS_REQUIRED(data_cs_); |
sprang@webrtc.org | 9a8c28f | 2014-11-04 16:27:16 | [diff] [blame] | 152 | void TraceFrameDropStart() EXCLUSIVE_LOCKS_REQUIRED(data_cs_); |
| 153 | void TraceFrameDropEnd() EXCLUSIVE_LOCKS_REQUIRED(data_cs_); |
stefan@webrtc.org | dca71b2 | 2013-03-27 16:36:01 | [diff] [blame] | 154 | |
pbos@webrtc.org | 67879bc | 2013-04-09 13:41:51 | [diff] [blame] | 155 | const uint32_t number_of_cores_; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 156 | |
mflodman | 2335164 | 2015-04-14 19:28:08 | [diff] [blame] | 157 | const rtc::scoped_ptr<VideoProcessingModule> vpm_; |
| 158 | const rtc::scoped_ptr<QMVideoSettingsCallback> qm_callback_; |
| 159 | const rtc::scoped_ptr<VideoCodingModule> vcm_; |
Peter Boström | 623f2f0 | 2015-06-04 13:18:17 | [diff] [blame] | 160 | rtc::scoped_refptr<PayloadRouter> send_payload_router_; |
mflodman@webrtc.org | 52be6c2 | 2015-02-25 13:50:10 | [diff] [blame] | 161 | |
mflodman@webrtc.org | 867d288 | 2015-02-10 08:58:16 | [diff] [blame] | 162 | rtc::scoped_ptr<CriticalSectionWrapper> data_cs_; |
| 163 | rtc::scoped_ptr<BitrateObserver> bitrate_observer_; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 164 | |
Peter Boström | 39ac69b | 2015-09-22 14:28:51 | [diff] [blame] | 165 | SendStatisticsProxy* const stats_proxy_; |
| 166 | I420FrameCallback* const pre_encode_callback_; |
Stefan Holmer | ca55fa1 | 2015-03-26 10:11:06 | [diff] [blame] | 167 | PacedSender* const pacer_; |
stefan@webrtc.org | 3ada29b | 2015-03-04 12:24:26 | [diff] [blame] | 168 | BitrateAllocator* const bitrate_allocator_; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 169 | |
Noah Richards | 14cb949 | 2015-04-15 16:14:12 | [diff] [blame] | 170 | // The time we last received an input frame or encoded frame. This is used to |
| 171 | // track when video is stopped long enough that we also want to stop sending |
| 172 | // padding. |
| 173 | int64_t time_of_last_frame_activity_ms_ GUARDED_BY(data_cs_); |
Peter Boström | 2b918e6 | 2015-11-10 15:27:12 | [diff] [blame] | 174 | VideoCodec encoder_config_ GUARDED_BY(data_cs_); |
pbos@webrtc.org | 3f83f9c | 2014-03-13 12:52:27 | [diff] [blame] | 175 | int min_transmit_bitrate_kbps_ GUARDED_BY(data_cs_); |
pbos@webrtc.org | 7131030 | 2015-03-18 14:40:03 | [diff] [blame] | 176 | uint32_t last_observed_bitrate_bps_ GUARDED_BY(data_cs_); |
stefan@webrtc.org | fedbe8b | 2014-07-09 14:46:31 | [diff] [blame] | 177 | int target_delay_ms_ GUARDED_BY(data_cs_); |
| 178 | bool network_is_transmitting_ GUARDED_BY(data_cs_); |
| 179 | bool encoder_paused_ GUARDED_BY(data_cs_); |
| 180 | bool encoder_paused_and_dropped_frame_ GUARDED_BY(data_cs_); |
| 181 | std::map<unsigned int, int64_t> time_last_intra_request_ms_ |
| 182 | GUARDED_BY(data_cs_); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 183 | |
Peter Boström | 39ac69b | 2015-09-22 14:28:51 | [diff] [blame] | 184 | ProcessThread* module_process_thread_; |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 185 | |
stefan@webrtc.org | fedbe8b | 2014-07-09 14:46:31 | [diff] [blame] | 186 | bool has_received_sli_ GUARDED_BY(data_cs_); |
| 187 | uint8_t picture_id_sli_ GUARDED_BY(data_cs_); |
| 188 | bool has_received_rpsi_ GUARDED_BY(data_cs_); |
| 189 | uint64_t picture_id_rpsi_ GUARDED_BY(data_cs_); |
Peter Boström | 96b6c91 | 2015-05-05 13:16:30 | [diff] [blame] | 190 | std::map<uint32_t, int> ssrc_streams_ GUARDED_BY(data_cs_); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 191 | |
stefan@webrtc.org | fedbe8b | 2014-07-09 14:46:31 | [diff] [blame] | 192 | bool video_suspended_ GUARDED_BY(data_cs_); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | } // namespace webrtc |
| 196 | |
| 197 | #endif // WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_ |