blob: f679062cf41066761655d77b13b6eb7c3b9c5e12 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
stefan@webrtc.org07b45a52012-02-02 08:37:48 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
mflodman@webrtc.orge06ca3c2012-06-29 13:20:14 +000011#ifndef WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_
12#define WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +000014#include <list>
15#include <map>
16
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000017#include "webrtc/common_types.h"
18#include "webrtc/modules/bitrate_controller/include/bitrate_controller.h"
19#include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
20#include "webrtc/modules/video_coding/main/interface/video_coding_defines.h"
21#include "webrtc/modules/video_processing/main/interface/video_processing.h"
22#include "webrtc/system_wrappers/interface/scoped_ptr.h"
23#include "webrtc/typedefs.h"
24#include "webrtc/video_engine/vie_defines.h"
pbos@webrtc.orgf5d4cb12013-05-17 13:44:48 +000025#include "webrtc/video_engine/vie_frame_provider_base.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000026
27namespace webrtc {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000028
andresp@webrtc.org7707d062013-05-13 10:50:50 +000029class Config;
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000030class CriticalSectionWrapper;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000031class PacedSender;
niklase@google.com470e71d2011-07-07 08:21:25 +000032class ProcessThread;
stefan@webrtc.org439be292012-02-16 14:45:37 +000033class QMVideoSettingsCallback;
niklase@google.com470e71d2011-07-07 08:21:25 +000034class RtpRtcp;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000035class ViEBitrateObserver;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000036class ViEEffectFilter;
niklase@google.com470e71d2011-07-07 08:21:25 +000037class ViEEncoderObserver;
mflodman@webrtc.org6879c8a2013-07-23 11:35:00 +000038class VideoCodingModule;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000039class ViEPacedSenderCallback;
niklase@google.com470e71d2011-07-07 08:21:25 +000040
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000041class ViEEncoder
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000042 : public RtcpIntraFrameObserver,
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000043 public VCMPacketizationCallback,
44 public VCMProtectionCallback,
45 public VCMSendStatisticsCallback,
46 public ViEFrameCallback {
47 public:
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000048 friend class ViEBitrateObserver;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000049 friend class ViEPacedSenderCallback;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000050
pbos@webrtc.orgb238d122013-04-09 13:41:51 +000051 ViEEncoder(int32_t engine_id,
52 int32_t channel_id,
53 uint32_t number_of_cores,
andresp@webrtc.org7707d062013-05-13 10:50:50 +000054 const Config& config,
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000055 ProcessThread& module_process_thread,
56 BitrateController* bitrate_controller);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000057 ~ViEEncoder();
niklase@google.com470e71d2011-07-07 08:21:25 +000058
wu@webrtc.org5d8c1022012-04-10 16:54:05 +000059 bool Init();
60
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +000061 void SetNetworkTransmissionState(bool is_transmitting);
62
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +000063 // Returns the id of the owning channel.
64 int Owner() const;
65
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000066 // Drops incoming packets before they get to the encoder.
67 void Pause();
68 void Restart();
niklase@google.com470e71d2011-07-07 08:21:25 +000069
pbos@webrtc.orgb238d122013-04-09 13:41:51 +000070 int32_t DropDeltaAfterKey(bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000071
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000072 // Codec settings.
pbos@webrtc.orgb238d122013-04-09 13:41:51 +000073 uint8_t NumberOfCodecs();
74 int32_t GetCodec(uint8_t list_index, VideoCodec* video_codec);
75 int32_t RegisterExternalEncoder(VideoEncoder* encoder,
76 uint8_t pl_type,
77 bool internal_source);
78 int32_t DeRegisterExternalEncoder(uint8_t pl_type);
79 int32_t SetEncoder(const VideoCodec& video_codec);
80 int32_t GetEncoder(VideoCodec* video_codec);
niklase@google.com470e71d2011-07-07 08:21:25 +000081
pbos@webrtc.orgb238d122013-04-09 13:41:51 +000082 int32_t GetCodecConfigParameters(
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000083 unsigned char config_parameters[kConfigParameterSize],
84 unsigned char& config_parameters_size);
niklase@google.com470e71d2011-07-07 08:21:25 +000085
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +000086 PacedSender* GetPacedSender();
87
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000088 // Scale or crop/pad image.
pbos@webrtc.orgb238d122013-04-09 13:41:51 +000089 int32_t ScaleInputImage(bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000090
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000091 // RTP settings.
92 RtpRtcp* SendRtpRtcpModule();
niklase@google.com470e71d2011-07-07 08:21:25 +000093
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000094 // Implementing ViEFrameCallback.
95 virtual void DeliverFrame(int id,
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +000096 I420VideoFrame* video_frame,
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000097 int num_csrcs = 0,
pbos@webrtc.orgb238d122013-04-09 13:41:51 +000098 const uint32_t CSRC[kRtpCsrcSize] = NULL);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000099 virtual void DelayChanged(int id, int frame_delay);
mflodman@webrtc.org8baed512012-06-21 12:11:50 +0000100 virtual int GetPreferedFrameSettings(int* width,
101 int* height,
102 int* frame_rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000103
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000104 virtual void ProviderDestroyed(int id) {
105 return;
106 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000107
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000108 int32_t SendKeyFrame();
109 int32_t SendCodecStatistics(uint32_t* num_key_frames,
110 uint32_t* num_delta_frames);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000111
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000112 int32_t EstimatedSendBandwidth(
113 uint32_t* available_bandwidth) const;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000114
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000115 int CodecTargetBitrate(uint32_t* bitrate) const;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000116 // Loss protection.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000117 int32_t UpdateProtectionMethod(bool enable_nack);
118 bool nack_enabled() const { return nack_enabled_; }
marpan@google.com80c5d7a2011-07-15 21:32:40 +0000119
mikhal@webrtc.orgef9f76a2013-02-15 23:22:18 +0000120 // Buffering mode.
121 void SetSenderBufferingMode(int target_delay_ms);
mikhal@webrtc.org3d305c62013-02-10 18:42:55 +0000122
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000123 // Implements VCMPacketizationCallback.
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000124 virtual int32_t SendData(
andrew@webrtc.orgcdfa63f2012-08-10 22:56:17 +0000125 FrameType frame_type,
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000126 uint8_t payload_type,
127 uint32_t time_stamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000128 int64_t capture_time_ms,
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000129 const uint8_t* payload_data,
130 uint32_t payload_size,
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000131 const RTPFragmentationHeader& fragmentation_header,
132 const RTPVideoHeader* rtp_video_hdr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000133
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000134 // Implements VideoProtectionCallback.
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000135 virtual int ProtectionRequest(
136 const FecProtectionParams* delta_fec_params,
137 const FecProtectionParams* key_fec_params,
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000138 uint32_t* sent_video_rate_bps,
139 uint32_t* sent_nack_rate_bps,
140 uint32_t* sent_fec_rate_bps);
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000141
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000142 // Implements VideoSendStatisticsCallback.
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000143 virtual int32_t SendStatistics(const uint32_t bit_rate,
144 const uint32_t frame_rate);
145 int32_t RegisterCodecObserver(ViEEncoderObserver* observer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000146
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000147 // Implements RtcpIntraFrameObserver.
andrew@webrtc.org96636862012-09-20 23:33:17 +0000148 virtual void OnReceivedIntraFrameRequest(uint32_t ssrc);
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000149 virtual void OnReceivedSLI(uint32_t ssrc, uint8_t picture_id);
150 virtual void OnReceivedRPSI(uint32_t ssrc, uint64_t picture_id);
151 virtual void OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000152
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000153 // Sets SSRCs for all streams.
154 bool SetSsrcs(const std::list<unsigned int>& ssrcs);
155
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000156 // Effect filter.
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000157 int32_t RegisterEffectFilter(ViEEffectFilter* effect_filter);
niklase@google.com470e71d2011-07-07 08:21:25 +0000158
mikhal@webrtc.orge41bbdf2012-08-28 16:15:16 +0000159 // Enables recording of debugging information.
160 virtual int StartDebugRecording(const char* fileNameUTF8);
161
162 // Disables recording of debugging information.
163 virtual int StopDebugRecording();
164
henrik.lundin@webrtc.org7ea4f242013-10-02 13:34:26 +0000165 // Enables AutoMuter to turn off video when the rate drops below
166 // |threshold_bps|, and turns back on when the rate goes back up above
167 // |threshold_bps| + |window_bps|.
168 virtual void EnableAutoMuting(int threshold_bps, int window_bps);
169
mflodman@webrtc.org8dde1972012-10-25 13:06:22 +0000170 int channel_id() const { return channel_id_; }
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000171 protected:
172 // Called by BitrateObserver.
173 void OnNetworkChanged(const uint32_t bitrate_bps,
174 const uint8_t fraction_lost,
175 const uint32_t round_trip_time_ms);
176
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000177 // Called by PacedSender.
hclam@chromium.org2e402ce2013-06-20 20:18:31 +0000178 bool TimeToSendPacket(uint32_t ssrc, uint16_t sequence_number,
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000179 int64_t capture_time_ms);
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000180 int TimeToSendPadding(int bytes);
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000181
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000182 private:
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000183 bool EncoderPaused() const;
184
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000185 int32_t engine_id_;
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000186 const int channel_id_;
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000187 const uint32_t number_of_cores_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000188
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000189 VideoCodingModule& vcm_;
190 VideoProcessingModule& vpm_;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000191 scoped_ptr<RtpRtcp> default_rtp_rtcp_;
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000192 scoped_ptr<CriticalSectionWrapper> callback_cs_;
193 scoped_ptr<CriticalSectionWrapper> data_cs_;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000194 scoped_ptr<BitrateObserver> bitrate_observer_;
pwestin@webrtc.org571a1c02012-11-13 21:12:39 +0000195 scoped_ptr<PacedSender> paced_sender_;
196 scoped_ptr<ViEPacedSenderCallback> pacing_callback_;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000197
198 BitrateController* bitrate_controller_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
stefan@webrtc.org3e005052013-10-18 15:05:29 +0000200 int64_t time_of_last_incoming_frame_ms_;
stefan@webrtc.org508a84b2013-06-17 12:53:37 +0000201 bool send_padding_;
stefan@webrtc.orgbfacda62013-03-27 16:36:01 +0000202 int target_delay_ms_;
203 bool network_is_transmitting_;
204 bool encoder_paused_;
pwestin@webrtc.org52b4e882013-05-02 19:02:17 +0000205 bool encoder_paused_and_dropped_frame_;
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000206 std::map<unsigned int, int64_t> time_last_intra_request_ms_;
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000207 int32_t channels_dropping_delta_frames_;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000208 bool drop_next_frame_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000209
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000210 bool fec_enabled_;
211 bool nack_enabled_;
212
213 ViEEncoderObserver* codec_observer_;
214 ViEEffectFilter* effect_filter_;
215 ProcessThread& module_process_thread_;
216
217 bool has_received_sli_;
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000218 uint8_t picture_id_sli_;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000219 bool has_received_rpsi_;
pbos@webrtc.orgb238d122013-04-09 13:41:51 +0000220 uint64_t picture_id_rpsi_;
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000221 std::map<unsigned int, int> ssrc_streams_;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000222
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000223 // Quality modes callback
stefan@webrtc.org439be292012-02-16 14:45:37 +0000224 QMVideoSettingsCallback* qm_callback_;
henrik.lundin@webrtc.org7ea4f242013-10-02 13:34:26 +0000225 bool video_auto_muted_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000226};
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000227
228} // namespace webrtc
229
mflodman@webrtc.orge06ca3c2012-06-29 13:20:14 +0000230#endif // WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_