blob: cd8328e82e51f83d0db68e63fc2f3df95b823500 [file] [log] [blame]
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001/*
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:29 +000014#include <list>
15#include <map>
16
pbos@webrtc.org281cff82013-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"
pbos@webrtc.org24e20892013-10-28 16:32:01 +000024#include "webrtc/frame_callback.h"
pbos@webrtc.org281cff82013-05-17 13:44:48 +000025#include "webrtc/video_engine/vie_defines.h"
pbos@webrtc.org281cff82013-05-17 13:44:48 +000026#include "webrtc/video_engine/vie_frame_provider_base.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000027
28namespace webrtc {
29
andresp@webrtc.orgac6d9192013-05-13 10:50:50 +000030class Config;
mflodman@webrtc.orgbf76ae22013-07-23 11:35:00 +000031class CriticalSectionWrapper;
sprang@webrtc.org2e98d452013-11-26 11:41:59 +000032class EncodedImageCallback;
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +000033class PacedSender;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000034class ProcessThread;
35class QMVideoSettingsCallback;
36class RtpRtcp;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000037class ViEBitrateObserver;
38class ViEEffectFilter;
39class ViEEncoderObserver;
mflodman@webrtc.orgbf76ae22013-07-23 11:35:00 +000040class VideoCodingModule;
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +000041class ViEPacedSenderCallback;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000042
43class ViEEncoder
44 : public RtcpIntraFrameObserver,
45 public VCMPacketizationCallback,
46 public VCMProtectionCallback,
47 public VCMSendStatisticsCallback,
48 public ViEFrameCallback {
49 public:
50 friend class ViEBitrateObserver;
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +000051 friend class ViEPacedSenderCallback;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000052
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000053 ViEEncoder(int32_t engine_id,
54 int32_t channel_id,
55 uint32_t number_of_cores,
andresp@webrtc.orgac6d9192013-05-13 10:50:50 +000056 const Config& config,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000057 ProcessThread& module_process_thread,
58 BitrateController* bitrate_controller);
59 ~ViEEncoder();
60
61 bool Init();
62
stefan@webrtc.orgdca71b22013-03-27 16:36:01 +000063 void SetNetworkTransmissionState(bool is_transmitting);
64
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000065 // Returns the id of the owning channel.
66 int Owner() const;
67
68 // Drops incoming packets before they get to the encoder.
69 void Pause();
70 void Restart();
71
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000072 int32_t DropDeltaAfterKey(bool enable);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000073
74 // Codec settings.
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000075 uint8_t NumberOfCodecs();
76 int32_t GetCodec(uint8_t list_index, VideoCodec* video_codec);
77 int32_t RegisterExternalEncoder(VideoEncoder* encoder,
78 uint8_t pl_type,
79 bool internal_source);
80 int32_t DeRegisterExternalEncoder(uint8_t pl_type);
81 int32_t SetEncoder(const VideoCodec& video_codec);
82 int32_t GetEncoder(VideoCodec* video_codec);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000083
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000084 int32_t GetCodecConfigParameters(
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000085 unsigned char config_parameters[kConfigParameterSize],
86 unsigned char& config_parameters_size);
87
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +000088 PacedSender* GetPacedSender();
89
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000090 // Scale or crop/pad image.
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000091 int32_t ScaleInputImage(bool enable);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000092
93 // RTP settings.
94 RtpRtcp* SendRtpRtcpModule();
95
96 // Implementing ViEFrameCallback.
97 virtual void DeliverFrame(int id,
mikhal@webrtc.org3bbed742012-10-24 18:33:04 +000098 I420VideoFrame* video_frame,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000099 int num_csrcs = 0,
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000100 const uint32_t CSRC[kRtpCsrcSize] = NULL);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000101 virtual void DelayChanged(int id, int frame_delay);
102 virtual int GetPreferedFrameSettings(int* width,
103 int* height,
104 int* frame_rate);
105
106 virtual void ProviderDestroyed(int id) {
107 return;
108 }
109
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000110 int32_t SendKeyFrame();
111 int32_t SendCodecStatistics(uint32_t* num_key_frames,
112 uint32_t* num_delta_frames);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000113
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000114 int32_t EstimatedSendBandwidth(
115 uint32_t* available_bandwidth) const;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000116
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000117 int CodecTargetBitrate(uint32_t* bitrate) const;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000118 // Loss protection.
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +0000119 int32_t UpdateProtectionMethod(bool enable_nack);
120 bool nack_enabled() const { return nack_enabled_; }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000121
mikhal@webrtc.org9d6fcb32013-02-15 23:22:18 +0000122 // Buffering mode.
123 void SetSenderBufferingMode(int target_delay_ms);
mikhal@webrtc.org0c66de62013-02-10 18:42:55 +0000124
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000125 // Implements VCMPacketizationCallback.
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000126 virtual int32_t SendData(
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000127 FrameType frame_type,
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000128 uint8_t payload_type,
129 uint32_t time_stamp,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000130 int64_t capture_time_ms,
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000131 const uint8_t* payload_data,
132 uint32_t payload_size,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000133 const RTPFragmentationHeader& fragmentation_header,
134 const RTPVideoHeader* rtp_video_hdr);
135
136 // Implements VideoProtectionCallback.
137 virtual int ProtectionRequest(
138 const FecProtectionParams* delta_fec_params,
139 const FecProtectionParams* key_fec_params,
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000140 uint32_t* sent_video_rate_bps,
141 uint32_t* sent_nack_rate_bps,
142 uint32_t* sent_fec_rate_bps);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000143
144 // Implements VideoSendStatisticsCallback.
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000145 virtual int32_t SendStatistics(const uint32_t bit_rate,
146 const uint32_t frame_rate);
147 int32_t RegisterCodecObserver(ViEEncoderObserver* observer);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000148
149 // Implements RtcpIntraFrameObserver.
150 virtual void OnReceivedIntraFrameRequest(uint32_t ssrc);
151 virtual void OnReceivedSLI(uint32_t ssrc, uint8_t picture_id);
152 virtual void OnReceivedRPSI(uint32_t ssrc, uint64_t picture_id);
153 virtual void OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc);
154
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000155 // Sets SSRCs for all streams.
156 bool SetSsrcs(const std::list<unsigned int>& ssrcs);
157
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000158 // Effect filter.
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000159 int32_t RegisterEffectFilter(ViEEffectFilter* effect_filter);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000160
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000161 // Enables recording of debugging information.
162 virtual int StartDebugRecording(const char* fileNameUTF8);
163
164 // Disables recording of debugging information.
165 virtual int StopDebugRecording();
166
henrik.lundin@webrtc.org45901772013-11-18 12:18:43 +0000167 // Lets the sender suspend video when the rate drops below
henrik.lundin@webrtc.org39079d12013-10-02 13:34:26 +0000168 // |threshold_bps|, and turns back on when the rate goes back up above
169 // |threshold_bps| + |window_bps|.
henrik.lundin@webrtc.org45901772013-11-18 12:18:43 +0000170 virtual void SuspendBelowMinBitrate();
henrik.lundin@webrtc.org39079d12013-10-02 13:34:26 +0000171
sprang@webrtc.org2e98d452013-11-26 11:41:59 +0000172 // New-style callbacks, used by VideoSendStream.
pbos@webrtc.org63301bd2013-10-21 10:34:43 +0000173 void RegisterPreEncodeCallback(I420FrameCallback* pre_encode_callback);
174 void DeRegisterPreEncodeCallback();
sprang@webrtc.org2e98d452013-11-26 11:41:59 +0000175 void RegisterPostEncodeImageCallback(
176 EncodedImageCallback* post_encode_callback);
177 void DeRegisterPostEncodeImageCallback();
pbos@webrtc.org63301bd2013-10-21 10:34:43 +0000178
mflodman@webrtc.orgff9854b2012-10-25 13:06:22 +0000179 int channel_id() const { return channel_id_; }
sprang@webrtc.org2e98d452013-11-26 11:41:59 +0000180
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000181 protected:
182 // Called by BitrateObserver.
183 void OnNetworkChanged(const uint32_t bitrate_bps,
184 const uint8_t fraction_lost,
185 const uint32_t round_trip_time_ms);
186
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +0000187 // Called by PacedSender.
hclam@chromium.org0f6f7cb2013-06-20 20:18:31 +0000188 bool TimeToSendPacket(uint32_t ssrc, uint16_t sequence_number,
stefan@webrtc.orgb748c9d2013-11-13 15:29:21 +0000189 int64_t capture_time_ms, bool retransmission);
stefan@webrtc.org69f76052013-06-17 12:53:37 +0000190 int TimeToSendPadding(int bytes);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000191 private:
stefan@webrtc.orgdca71b22013-03-27 16:36:01 +0000192 bool EncoderPaused() const;
193
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000194 int32_t engine_id_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000195 const int channel_id_;
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000196 const uint32_t number_of_cores_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000197
198 VideoCodingModule& vcm_;
199 VideoProcessingModule& vpm_;
200 scoped_ptr<RtpRtcp> default_rtp_rtcp_;
201 scoped_ptr<CriticalSectionWrapper> callback_cs_;
202 scoped_ptr<CriticalSectionWrapper> data_cs_;
203 scoped_ptr<BitrateObserver> bitrate_observer_;
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +0000204 scoped_ptr<PacedSender> paced_sender_;
205 scoped_ptr<ViEPacedSenderCallback> pacing_callback_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000206
207 BitrateController* bitrate_controller_;
208
stefan@webrtc.org5e74d962013-10-18 15:05:29 +0000209 int64_t time_of_last_incoming_frame_ms_;
stefan@webrtc.org69f76052013-06-17 12:53:37 +0000210 bool send_padding_;
stefan@webrtc.orgdca71b22013-03-27 16:36:01 +0000211 int target_delay_ms_;
212 bool network_is_transmitting_;
213 bool encoder_paused_;
pwestin@webrtc.org36bdba42013-05-02 19:02:17 +0000214 bool encoder_paused_and_dropped_frame_;
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000215 std::map<unsigned int, int64_t> time_last_intra_request_ms_;
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000216 int32_t channels_dropping_delta_frames_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000217 bool drop_next_frame_;
218
219 bool fec_enabled_;
220 bool nack_enabled_;
221
222 ViEEncoderObserver* codec_observer_;
223 ViEEffectFilter* effect_filter_;
224 ProcessThread& module_process_thread_;
225
226 bool has_received_sli_;
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000227 uint8_t picture_id_sli_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000228 bool has_received_rpsi_;
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000229 uint64_t picture_id_rpsi_;
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000230 std::map<unsigned int, int> ssrc_streams_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000231
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000232 // Quality modes callback
233 QMVideoSettingsCallback* qm_callback_;
henrik.lundin@webrtc.org45901772013-11-18 12:18:43 +0000234 bool video_suspended_;
pbos@webrtc.org63301bd2013-10-21 10:34:43 +0000235 I420FrameCallback* pre_encode_callback_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000236};
237
238} // namespace webrtc
239
240#endif // WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_