blob: 43a38f6564015f45a4f9b9041fe1630c74ded004 [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;
andresp@webrtc.orgac6d9192013-05-13 10:50:50 +000031class Config;
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +000032class PacedSender;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000033class ProcessThread;
34class QMVideoSettingsCallback;
35class RtpRtcp;
36class VideoCodingModule;
37class ViEBitrateObserver;
38class ViEEffectFilter;
39class ViEEncoderObserver;
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +000040class ViEPacedSenderCallback;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000041
42class ViEEncoder
43 : public RtcpIntraFrameObserver,
44 public VCMPacketizationCallback,
45 public VCMProtectionCallback,
46 public VCMSendStatisticsCallback,
47 public ViEFrameCallback {
48 public:
49 friend class ViEBitrateObserver;
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +000050 friend class ViEPacedSenderCallback;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000051
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000052 ViEEncoder(int32_t engine_id,
53 int32_t channel_id,
54 uint32_t number_of_cores,
andresp@webrtc.orgac6d9192013-05-13 10:50:50 +000055 const Config& config,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000056 ProcessThread& module_process_thread,
57 BitrateController* bitrate_controller);
58 ~ViEEncoder();
59
60 bool Init();
61
stefan@webrtc.orgdca71b22013-03-27 16:36:01 +000062 void SetNetworkTransmissionState(bool is_transmitting);
63
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000064 // Returns the id of the owning channel.
65 int Owner() const;
66
67 // Drops incoming packets before they get to the encoder.
68 void Pause();
69 void Restart();
70
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000071 int32_t DropDeltaAfterKey(bool enable);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000072
73 // Codec settings.
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000074 uint8_t NumberOfCodecs();
75 int32_t GetCodec(uint8_t list_index, VideoCodec* video_codec);
76 int32_t RegisterExternalEncoder(VideoEncoder* encoder,
77 uint8_t pl_type,
78 bool internal_source);
79 int32_t DeRegisterExternalEncoder(uint8_t pl_type);
80 int32_t SetEncoder(const VideoCodec& video_codec);
81 int32_t GetEncoder(VideoCodec* video_codec);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000082
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000083 int32_t GetCodecConfigParameters(
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000084 unsigned char config_parameters[kConfigParameterSize],
85 unsigned char& config_parameters_size);
86
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +000087 PacedSender* GetPacedSender();
88
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000089 // Scale or crop/pad image.
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000090 int32_t ScaleInputImage(bool enable);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000091
92 // RTP settings.
93 RtpRtcp* SendRtpRtcpModule();
94
95 // Implementing ViEFrameCallback.
96 virtual void DeliverFrame(int id,
mikhal@webrtc.org3bbed742012-10-24 18:33:04 +000097 I420VideoFrame* video_frame,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000098 int num_csrcs = 0,
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000099 const uint32_t CSRC[kRtpCsrcSize] = NULL);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000100 virtual void DelayChanged(int id, int frame_delay);
101 virtual int GetPreferedFrameSettings(int* width,
102 int* height,
103 int* frame_rate);
104
105 virtual void ProviderDestroyed(int id) {
106 return;
107 }
108
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000109 int32_t SendKeyFrame();
110 int32_t SendCodecStatistics(uint32_t* num_key_frames,
111 uint32_t* num_delta_frames);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000112
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000113 int32_t EstimatedSendBandwidth(
114 uint32_t* available_bandwidth) const;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000115
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000116 int CodecTargetBitrate(uint32_t* bitrate) const;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000117 // Loss protection.
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000118 int32_t UpdateProtectionMethod();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000119
mikhal@webrtc.org9d6fcb32013-02-15 23:22:18 +0000120 // Buffering mode.
121 void SetSenderBufferingMode(int target_delay_ms);
mikhal@webrtc.org0c66de62013-02-10 18:42:55 +0000122
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000123 // Implements VCMPacketizationCallback.
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000124 virtual int32_t SendData(
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000125 FrameType frame_type,
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000126 uint8_t payload_type,
127 uint32_t time_stamp,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000128 int64_t capture_time_ms,
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000129 const uint8_t* payload_data,
130 uint32_t payload_size,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000131 const RTPFragmentationHeader& fragmentation_header,
132 const RTPVideoHeader* rtp_video_hdr);
133
134 // Implements VideoProtectionCallback.
135 virtual int ProtectionRequest(
136 const FecProtectionParams* delta_fec_params,
137 const FecProtectionParams* key_fec_params,
pbos@webrtc.org67879bc2013-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);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000141
142 // Implements VideoSendStatisticsCallback.
pbos@webrtc.org67879bc2013-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);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000146
147 // Implements RtcpIntraFrameObserver.
148 virtual void OnReceivedIntraFrameRequest(uint32_t ssrc);
149 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);
152
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000153 // Sets SSRCs for all streams.
154 bool SetSsrcs(const std::list<unsigned int>& ssrcs);
155
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000156 // Effect filter.
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000157 int32_t RegisterEffectFilter(ViEEffectFilter* effect_filter);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000158
159 // Recording.
160 ViEFileRecorder& GetOutgoingFileRecorder();
161
162 // Enables recording of debugging information.
163 virtual int StartDebugRecording(const char* fileNameUTF8);
164
165 // Disables recording of debugging information.
166 virtual int StopDebugRecording();
167
mflodman@webrtc.orgff9854b2012-10-25 13:06:22 +0000168 int channel_id() const { return channel_id_; }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000169 protected:
170 // Called by BitrateObserver.
171 void OnNetworkChanged(const uint32_t bitrate_bps,
172 const uint8_t fraction_lost,
173 const uint32_t round_trip_time_ms);
174
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +0000175 // Called by PacedSender.
176 void TimeToSendPacket(uint32_t ssrc, uint16_t sequence_number,
177 int64_t capture_time_ms);
178
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000179 private:
stefan@webrtc.orgdca71b22013-03-27 16:36:01 +0000180 bool EncoderPaused() const;
181
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000182 int32_t engine_id_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000183 const int channel_id_;
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000184 const uint32_t number_of_cores_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000185
186 VideoCodingModule& vcm_;
187 VideoProcessingModule& vpm_;
188 scoped_ptr<RtpRtcp> default_rtp_rtcp_;
189 scoped_ptr<CriticalSectionWrapper> callback_cs_;
190 scoped_ptr<CriticalSectionWrapper> data_cs_;
191 scoped_ptr<BitrateObserver> bitrate_observer_;
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +0000192 scoped_ptr<PacedSender> paced_sender_;
193 scoped_ptr<ViEPacedSenderCallback> pacing_callback_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000194
195 BitrateController* bitrate_controller_;
196
stefan@webrtc.orgdca71b22013-03-27 16:36:01 +0000197 int target_delay_ms_;
198 bool network_is_transmitting_;
199 bool encoder_paused_;
pwestin@webrtc.org36bdba42013-05-02 19:02:17 +0000200 bool encoder_paused_and_dropped_frame_;
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000201 std::map<unsigned int, int64_t> time_last_intra_request_ms_;
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000202 int32_t channels_dropping_delta_frames_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000203 bool drop_next_frame_;
204
205 bool fec_enabled_;
206 bool nack_enabled_;
207
208 ViEEncoderObserver* codec_observer_;
209 ViEEffectFilter* effect_filter_;
210 ProcessThread& module_process_thread_;
211
212 bool has_received_sli_;
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000213 uint8_t picture_id_sli_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000214 bool has_received_rpsi_;
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000215 uint64_t picture_id_rpsi_;
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000216 std::map<unsigned int, int> ssrc_streams_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000217
218 ViEFileRecorder file_recorder_;
219
220 // Quality modes callback
221 QMVideoSettingsCallback* qm_callback_;
222};
223
224} // namespace webrtc
225
226#endif // WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_