blob: b17bafc6296d127de970b9c28f2114f3723707d3 [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
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000017#include "common_types.h" // NOLINT
18#include "typedefs.h" //NOLINT
19#include "modules/bitrate_controller/include/bitrate_controller.h"
20#include "modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
21#include "modules/video_coding/main/interface/video_coding_defines.h"
22#include "modules/video_processing/main/interface/video_processing.h"
23#include "system_wrappers/interface/scoped_ptr.h"
24#include "video_engine/vie_defines.h"
25#include "video_engine/vie_file_recorder.h"
26#include "video_engine/vie_frame_provider_base.h"
27
28namespace webrtc {
29
30class CriticalSectionWrapper;
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +000031class PacedSender;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000032class ProcessThread;
33class QMVideoSettingsCallback;
34class RtpRtcp;
35class VideoCodingModule;
36class ViEBitrateObserver;
37class ViEEffectFilter;
38class ViEEncoderObserver;
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +000039class ViEPacedSenderCallback;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000040
41class ViEEncoder
42 : public RtcpIntraFrameObserver,
43 public VCMPacketizationCallback,
44 public VCMProtectionCallback,
45 public VCMSendStatisticsCallback,
46 public ViEFrameCallback {
47 public:
48 friend class ViEBitrateObserver;
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +000049 friend class ViEPacedSenderCallback;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000050
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000051 ViEEncoder(int32_t engine_id,
52 int32_t channel_id,
53 uint32_t number_of_cores,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000054 ProcessThread& module_process_thread,
55 BitrateController* bitrate_controller);
56 ~ViEEncoder();
57
58 bool Init();
59
stefan@webrtc.orgdca71b22013-03-27 16:36:01 +000060 void SetNetworkTransmissionState(bool is_transmitting);
61
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000062 // Returns the id of the owning channel.
63 int Owner() const;
64
65 // Drops incoming packets before they get to the encoder.
66 void Pause();
67 void Restart();
68
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000069 int32_t DropDeltaAfterKey(bool enable);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000070
71 // Codec settings.
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000072 uint8_t NumberOfCodecs();
73 int32_t GetCodec(uint8_t list_index, VideoCodec* video_codec);
74 int32_t RegisterExternalEncoder(VideoEncoder* encoder,
75 uint8_t pl_type,
76 bool internal_source);
77 int32_t DeRegisterExternalEncoder(uint8_t pl_type);
78 int32_t SetEncoder(const VideoCodec& video_codec);
79 int32_t GetEncoder(VideoCodec* video_codec);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000080
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000081 int32_t GetCodecConfigParameters(
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000082 unsigned char config_parameters[kConfigParameterSize],
83 unsigned char& config_parameters_size);
84
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +000085 PacedSender* GetPacedSender();
86
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000087 // Scale or crop/pad image.
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000088 int32_t ScaleInputImage(bool enable);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000089
90 // RTP settings.
91 RtpRtcp* SendRtpRtcpModule();
92
93 // Implementing ViEFrameCallback.
94 virtual void DeliverFrame(int id,
mikhal@webrtc.org3bbed742012-10-24 18:33:04 +000095 I420VideoFrame* video_frame,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000096 int num_csrcs = 0,
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000097 const uint32_t CSRC[kRtpCsrcSize] = NULL);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000098 virtual void DelayChanged(int id, int frame_delay);
99 virtual int GetPreferedFrameSettings(int* width,
100 int* height,
101 int* frame_rate);
102
103 virtual void ProviderDestroyed(int id) {
104 return;
105 }
106
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000107 int32_t SendKeyFrame();
108 int32_t SendCodecStatistics(uint32_t* num_key_frames,
109 uint32_t* num_delta_frames);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000110
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000111 int32_t EstimatedSendBandwidth(
112 uint32_t* available_bandwidth) const;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000113
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000114 int CodecTargetBitrate(uint32_t* bitrate) const;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000115 // Loss protection.
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000116 int32_t UpdateProtectionMethod();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000117
mikhal@webrtc.org9d6fcb32013-02-15 23:22:18 +0000118 // Buffering mode.
119 void SetSenderBufferingMode(int target_delay_ms);
mikhal@webrtc.org0c66de62013-02-10 18:42:55 +0000120
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000121 // Implements VCMPacketizationCallback.
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000122 virtual int32_t SendData(
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000123 FrameType frame_type,
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000124 uint8_t payload_type,
125 uint32_t time_stamp,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000126 int64_t capture_time_ms,
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000127 const uint8_t* payload_data,
128 uint32_t payload_size,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000129 const RTPFragmentationHeader& fragmentation_header,
130 const RTPVideoHeader* rtp_video_hdr);
131
132 // Implements VideoProtectionCallback.
133 virtual int ProtectionRequest(
134 const FecProtectionParams* delta_fec_params,
135 const FecProtectionParams* key_fec_params,
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000136 uint32_t* sent_video_rate_bps,
137 uint32_t* sent_nack_rate_bps,
138 uint32_t* sent_fec_rate_bps);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000139
140 // Implements VideoSendStatisticsCallback.
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000141 virtual int32_t SendStatistics(const uint32_t bit_rate,
142 const uint32_t frame_rate);
143 int32_t RegisterCodecObserver(ViEEncoderObserver* observer);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000144
145 // Implements RtcpIntraFrameObserver.
146 virtual void OnReceivedIntraFrameRequest(uint32_t ssrc);
147 virtual void OnReceivedSLI(uint32_t ssrc, uint8_t picture_id);
148 virtual void OnReceivedRPSI(uint32_t ssrc, uint64_t picture_id);
149 virtual void OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc);
150
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000151 // Sets SSRCs for all streams.
152 bool SetSsrcs(const std::list<unsigned int>& ssrcs);
153
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000154 // Effect filter.
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000155 int32_t RegisterEffectFilter(ViEEffectFilter* effect_filter);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000156
157 // Recording.
158 ViEFileRecorder& GetOutgoingFileRecorder();
159
160 // Enables recording of debugging information.
161 virtual int StartDebugRecording(const char* fileNameUTF8);
162
163 // Disables recording of debugging information.
164 virtual int StopDebugRecording();
165
mflodman@webrtc.orgff9854b2012-10-25 13:06:22 +0000166 int channel_id() const { return channel_id_; }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000167 protected:
168 // Called by BitrateObserver.
169 void OnNetworkChanged(const uint32_t bitrate_bps,
170 const uint8_t fraction_lost,
171 const uint32_t round_trip_time_ms);
172
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +0000173 // Called by PacedSender.
174 void TimeToSendPacket(uint32_t ssrc, uint16_t sequence_number,
175 int64_t capture_time_ms);
176
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000177 private:
stefan@webrtc.orgdca71b22013-03-27 16:36:01 +0000178 bool EncoderPaused() const;
179
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000180 int32_t engine_id_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000181 const int channel_id_;
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000182 const uint32_t number_of_cores_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000183
184 VideoCodingModule& vcm_;
185 VideoProcessingModule& vpm_;
186 scoped_ptr<RtpRtcp> default_rtp_rtcp_;
187 scoped_ptr<CriticalSectionWrapper> callback_cs_;
188 scoped_ptr<CriticalSectionWrapper> data_cs_;
189 scoped_ptr<BitrateObserver> bitrate_observer_;
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +0000190 scoped_ptr<PacedSender> paced_sender_;
191 scoped_ptr<ViEPacedSenderCallback> pacing_callback_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000192
193 BitrateController* bitrate_controller_;
194
stefan@webrtc.orgdca71b22013-03-27 16:36:01 +0000195 int target_delay_ms_;
196 bool network_is_transmitting_;
197 bool encoder_paused_;
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000198 std::map<unsigned int, int64_t> time_last_intra_request_ms_;
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000199 int32_t channels_dropping_delta_frames_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000200 bool drop_next_frame_;
201
202 bool fec_enabled_;
203 bool nack_enabled_;
204
205 ViEEncoderObserver* codec_observer_;
206 ViEEffectFilter* effect_filter_;
207 ProcessThread& module_process_thread_;
208
209 bool has_received_sli_;
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000210 uint8_t picture_id_sli_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000211 bool has_received_rpsi_;
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000212 uint64_t picture_id_rpsi_;
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000213 std::map<unsigned int, int> ssrc_streams_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000214
215 ViEFileRecorder file_recorder_;
216
217 // Quality modes callback
218 QMVideoSettingsCallback* qm_callback_;
219};
220
221} // namespace webrtc
222
223#endif // WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_