blob: 44859b1cd32e168a35eb221876dedd4f2588501a [file] [log] [blame]
andrew@webrtc.orgb015cbe2012-10-22 18:19:231/*
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.orgb6d9cfc2012-10-25 11:30:2914#include <map>
mflodman@webrtc.orgc51222c2015-02-06 13:10:1915#include <vector>
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:2916
mflodman@webrtc.org867d2882015-02-10 08:58:1617#include "webrtc/base/scoped_ptr.h"
Peter Boström623f2f02015-06-04 13:18:1718#include "webrtc/base/scoped_ref_ptr.h"
pbos@webrtc.orgd54aa962014-09-24 06:05:0019#include "webrtc/base/thread_annotations.h"
Henrik Kjellanderfe7633e2015-11-18 21:00:2120#include "webrtc/call/bitrate_allocator.h"
pbos@webrtc.org281cff82013-05-17 13:44:4821#include "webrtc/common_types.h"
Peter Boström623f2f02015-06-04 13:18:1722#include "webrtc/frame_callback.h"
Henrik Kjellander36a14b52015-11-04 07:31:5223#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Henrik Kjellanderfe7633e2015-11-18 21:00:2124#include "webrtc/modules/video_coding/include/video_coding_defines.h"
pbos@webrtc.org281cff82013-05-17 13:44:4825#include "webrtc/modules/video_processing/main/interface/video_processing.h"
pbos@webrtc.org281cff82013-05-17 13:44:4826#include "webrtc/typedefs.h"
Peter Boströmd5d98ba2015-06-26 04:58:1627#include "webrtc/video/video_capture_input.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:2328
29namespace webrtc {
30
mflodman1b78cc32015-11-13 05:02:4231class BitrateAllocator;
32class BitrateObserver;
andresp@webrtc.orgac6d9192013-05-13 10:50:5033class Config;
mflodman@webrtc.orgbf76ae22013-07-23 11:35:0034class CriticalSectionWrapper;
sprang@webrtc.org2e98d452013-11-26 11:41:5935class EncodedImageCallback;
pwestin@webrtc.org5e87b5f2012-11-13 21:12:3936class PacedSender;
mflodman@webrtc.orgc51222c2015-02-06 13:10:1937class PayloadRouter;
andrew@webrtc.orgb015cbe2012-10-22 18:19:2338class ProcessThread;
39class QMVideoSettingsCallback;
pbos@webrtc.orgee9497c2014-12-01 15:23:2140class SendStatisticsProxy;
andrew@webrtc.orgb015cbe2012-10-22 18:19:2341class ViEBitrateObserver;
42class ViEEffectFilter;
mflodman@webrtc.orgbf76ae22013-07-23 11:35:0043class VideoCodingModule;
andrew@webrtc.orgb015cbe2012-10-22 18:19:2344
Peter Boströmd5d98ba2015-06-26 04:58:1645class ViEEncoder : public RtcpIntraFrameObserver,
46 public VideoEncoderRateObserver,
47 public VCMPacketizationCallback,
48 public VCMSendStatisticsCallback,
49 public VideoCaptureCallback {
andrew@webrtc.orgb015cbe2012-10-22 18:19:2350 public:
51 friend class ViEBitrateObserver;
52
mflodmanfd3f2cd2015-10-19 15:12:1253 ViEEncoder(uint32_t number_of_cores,
Peter Boström39ac69b2015-09-22 14:28:5154 ProcessThread* module_process_thread,
55 SendStatisticsProxy* stats_proxy,
56 I420FrameCallback* pre_encode_callback,
Stefan Holmerca55fa12015-03-26 10:11:0657 PacedSender* pacer,
Peter Boströmeff34fc2015-09-15 13:08:0358 BitrateAllocator* bitrate_allocator);
andrew@webrtc.orgb015cbe2012-10-22 18:19:2359 ~ViEEncoder();
60
61 bool Init();
62
mflodman@webrtc.orgaca8c5e2015-02-17 10:15:0663 // 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.org52be6c22015-02-25 13:50:1067 void StartThreadsAndSetSharedMembers(
Peter Boström623f2f02015-06-04 13:18:1768 rtc::scoped_refptr<PayloadRouter> send_payload_router,
mflodman@webrtc.org52be6c22015-02-25 13:50:1069 VCMProtectionCallback* vcm_protection_callback);
mflodman@webrtc.orgaca8c5e2015-02-17 10:15:0670
71 // This function must be called before the corresponding ViEChannel is
72 // deleted.
mflodman@webrtc.org52be6c22015-02-25 13:50:1073 void StopThreadsAndRemoveSharedMembers();
mflodman@webrtc.orgc51222c2015-02-06 13:10:1974
stefan@webrtc.orgdca71b22013-03-27 16:36:0175 void SetNetworkTransmissionState(bool is_transmitting);
76
andrew@webrtc.orgb015cbe2012-10-22 18:19:2377 // 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.orgb015cbe2012-10-22 18:19:2384 // Codec settings.
pbos@webrtc.org67879bc2013-04-09 13:41:5185 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.orgb015cbe2012-10-22 18:19:2392
Peter Boströmd5d98ba2015-06-26 04:58:1693 // Implementing VideoCaptureCallback.
Miguel Casas-Sanchez3ca60f32015-05-30 00:21:4094 void DeliverFrame(VideoFrame video_frame) override;
andrew@webrtc.orgb015cbe2012-10-22 18:19:2395
pbos@webrtc.org67879bc2013-04-09 13:41:5196 int32_t SendKeyFrame();
andrew@webrtc.orgb015cbe2012-10-22 18:19:2397
pbos@webrtc.org71310302015-03-18 14:40:0398 uint32_t LastObservedBitrateBps() const;
pbos@webrtc.org67879bc2013-04-09 13:41:5199 int CodecTargetBitrate(uint32_t* bitrate) const;
Peter Boström2b918e62015-11-10 15:27:12100 // 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.orgb015cbe2012-10-22 18:19:23105
mikhal@webrtc.org9d6fcb32013-02-15 23:22:18106 // Buffering mode.
107 void SetSenderBufferingMode(int target_delay_ms);
mikhal@webrtc.org0c66de62013-02-10 18:42:55108
pbos@webrtc.org34c89ae2015-02-26 13:15:22109 // Implements VideoEncoderRateObserver.
110 void OnSetRates(uint32_t bitrate_bps, int framerate) override;
111
andrew@webrtc.orgb015cbe2012-10-22 18:19:23112 // Implements VCMPacketizationCallback.
kjellander@webrtc.org860ac532015-03-04 12:58:35113 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.orgb015cbe2012-10-22 18:19:23117
andrew@webrtc.orgb015cbe2012-10-22 18:19:23118 // Implements VideoSendStatisticsCallback.
kjellander@webrtc.org860ac532015-03-04 12:58:35119 int32_t SendStatistics(const uint32_t bit_rate,
120 const uint32_t frame_rate) override;
henrike@webrtc.org9cd0bbc2014-09-11 22:45:54121
andrew@webrtc.orgb015cbe2012-10-22 18:19:23122 // Implements RtcpIntraFrameObserver.
kjellander@webrtc.org860ac532015-03-04 12:58:35123 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.orgb015cbe2012-10-22 18:19:23127
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29128 // Sets SSRCs for all streams.
Peter Boström2b918e62015-11-10 15:27:12129 void SetSsrcs(const std::vector<uint32_t>& ssrcs);
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29130
pbos@webrtc.org3f83f9c2014-03-13 12:52:27131 void SetMinTransmitBitrate(int min_transmit_bitrate_kbps);
132
henrik.lundin@webrtc.org45901772013-11-18 12:18:43133 // Lets the sender suspend video when the rate drops below
henrik.lundin@webrtc.org39079d12013-10-02 13:34:26134 // |threshold_bps|, and turns back on when the rate goes back up above
135 // |threshold_bps| + |window_bps|.
henrike@webrtc.org9cd0bbc2014-09-11 22:45:54136 void SuspendBelowMinBitrate();
henrik.lundin@webrtc.org39079d12013-10-02 13:34:26137
sprang@webrtc.org2e98d452013-11-26 11:41:59138 // New-style callbacks, used by VideoSendStream.
sprang@webrtc.org2e98d452013-11-26 11:41:59139 void RegisterPostEncodeImageCallback(
140 EncodedImageCallback* post_encode_callback);
pbos@webrtc.org63301bd2013-10-21 10:34:43141
stefan8e71cce2015-07-15 11:39:22142 int GetPaddingNeededBps() const;
Stefan Holmerca55fa12015-03-26 10:11:06143
andrew@webrtc.orgb015cbe2012-10-22 18:19:23144 protected:
145 // Called by BitrateObserver.
stefan@webrtc.orga5f17c82014-12-08 19:46:23146 void OnNetworkChanged(uint32_t bitrate_bps,
147 uint8_t fraction_lost,
pkasting@chromium.orga3166c42015-01-12 21:51:21148 int64_t round_trip_time_ms);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23149
150 private:
stefan@webrtc.orgfedbe8b2014-07-09 14:46:31151 bool EncoderPaused() const EXCLUSIVE_LOCKS_REQUIRED(data_cs_);
sprang@webrtc.org9a8c28f2014-11-04 16:27:16152 void TraceFrameDropStart() EXCLUSIVE_LOCKS_REQUIRED(data_cs_);
153 void TraceFrameDropEnd() EXCLUSIVE_LOCKS_REQUIRED(data_cs_);
stefan@webrtc.orgdca71b22013-03-27 16:36:01154
pbos@webrtc.org67879bc2013-04-09 13:41:51155 const uint32_t number_of_cores_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23156
mflodman23351642015-04-14 19:28:08157 const rtc::scoped_ptr<VideoProcessingModule> vpm_;
158 const rtc::scoped_ptr<QMVideoSettingsCallback> qm_callback_;
159 const rtc::scoped_ptr<VideoCodingModule> vcm_;
Peter Boström623f2f02015-06-04 13:18:17160 rtc::scoped_refptr<PayloadRouter> send_payload_router_;
mflodman@webrtc.org52be6c22015-02-25 13:50:10161
mflodman@webrtc.org867d2882015-02-10 08:58:16162 rtc::scoped_ptr<CriticalSectionWrapper> data_cs_;
163 rtc::scoped_ptr<BitrateObserver> bitrate_observer_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23164
Peter Boström39ac69b2015-09-22 14:28:51165 SendStatisticsProxy* const stats_proxy_;
166 I420FrameCallback* const pre_encode_callback_;
Stefan Holmerca55fa12015-03-26 10:11:06167 PacedSender* const pacer_;
stefan@webrtc.org3ada29b2015-03-04 12:24:26168 BitrateAllocator* const bitrate_allocator_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23169
Noah Richards14cb9492015-04-15 16:14:12170 // 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öm2b918e62015-11-10 15:27:12174 VideoCodec encoder_config_ GUARDED_BY(data_cs_);
pbos@webrtc.org3f83f9c2014-03-13 12:52:27175 int min_transmit_bitrate_kbps_ GUARDED_BY(data_cs_);
pbos@webrtc.org71310302015-03-18 14:40:03176 uint32_t last_observed_bitrate_bps_ GUARDED_BY(data_cs_);
stefan@webrtc.orgfedbe8b2014-07-09 14:46:31177 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.orgb015cbe2012-10-22 18:19:23183
Peter Boström39ac69b2015-09-22 14:28:51184 ProcessThread* module_process_thread_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23185
stefan@webrtc.orgfedbe8b2014-07-09 14:46:31186 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öm96b6c912015-05-05 13:16:30190 std::map<uint32_t, int> ssrc_streams_ GUARDED_BY(data_cs_);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23191
stefan@webrtc.orgfedbe8b2014-07-09 14:46:31192 bool video_suspended_ GUARDED_BY(data_cs_);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23193};
194
195} // namespace webrtc
196
197#endif // WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_