blob: 2284cf21f73866dfc65a7cd67a3a01df7f9e60e0 [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"
24#include "webrtc/video_engine/vie_defines.h"
pbos@webrtc.org281cff82013-05-17 13:44:48 +000025#include "webrtc/video_engine/vie_frame_provider_base.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000026
27namespace webrtc {
28
andresp@webrtc.orgac6d9192013-05-13 10:50:50 +000029class Config;
mflodman@webrtc.orgbf76ae22013-07-23 11:35:00 +000030class 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;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000035class ViEBitrateObserver;
36class ViEEffectFilter;
37class ViEEncoderObserver;
mflodman@webrtc.orgbf76ae22013-07-23 11:35:00 +000038class VideoCodingModule;
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,
andresp@webrtc.orgac6d9192013-05-13 10:50:50 +000054 const Config& config,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000055 ProcessThread& module_process_thread,
56 BitrateController* bitrate_controller);
57 ~ViEEncoder();
58
59 bool Init();
60
stefan@webrtc.orgdca71b22013-03-27 16:36:01 +000061 void SetNetworkTransmissionState(bool is_transmitting);
62
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000063 // Returns the id of the owning channel.
64 int Owner() const;
65
66 // Drops incoming packets before they get to the encoder.
67 void Pause();
68 void Restart();
69
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000070 int32_t DropDeltaAfterKey(bool enable);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000071
72 // Codec settings.
pbos@webrtc.org67879bc2013-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);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000081
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000082 int32_t GetCodecConfigParameters(
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000083 unsigned char config_parameters[kConfigParameterSize],
84 unsigned char& config_parameters_size);
85
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +000086 PacedSender* GetPacedSender();
87
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000088 // Scale or crop/pad image.
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000089 int32_t ScaleInputImage(bool enable);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000090
91 // RTP settings.
92 RtpRtcp* SendRtpRtcpModule();
93
94 // Implementing ViEFrameCallback.
95 virtual void DeliverFrame(int id,
mikhal@webrtc.org3bbed742012-10-24 18:33:04 +000096 I420VideoFrame* video_frame,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000097 int num_csrcs = 0,
pbos@webrtc.org67879bc2013-04-09 13:41:51 +000098 const uint32_t CSRC[kRtpCsrcSize] = NULL);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000099 virtual void DelayChanged(int id, int frame_delay);
100 virtual int GetPreferedFrameSettings(int* width,
101 int* height,
102 int* frame_rate);
103
104 virtual void ProviderDestroyed(int id) {
105 return;
106 }
107
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000108 int32_t SendKeyFrame();
109 int32_t SendCodecStatistics(uint32_t* num_key_frames,
110 uint32_t* num_delta_frames);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000111
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000112 int32_t EstimatedSendBandwidth(
113 uint32_t* available_bandwidth) const;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000114
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000115 int CodecTargetBitrate(uint32_t* bitrate) const;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000116 // Loss protection.
tnakamura@webrtc.org0ba496b2013-07-16 19:25:04 +0000117 int32_t UpdateProtectionMethod();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000118
mikhal@webrtc.org9d6fcb32013-02-15 23:22:18 +0000119 // Buffering mode.
120 void SetSenderBufferingMode(int target_delay_ms);
mikhal@webrtc.org0c66de62013-02-10 18:42:55 +0000121
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000122 // Implements VCMPacketizationCallback.
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000123 virtual int32_t SendData(
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000124 FrameType frame_type,
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000125 uint8_t payload_type,
126 uint32_t time_stamp,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000127 int64_t capture_time_ms,
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000128 const uint8_t* payload_data,
129 uint32_t payload_size,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000130 const RTPFragmentationHeader& fragmentation_header,
131 const RTPVideoHeader* rtp_video_hdr);
132
133 // Implements VideoProtectionCallback.
134 virtual int ProtectionRequest(
135 const FecProtectionParams* delta_fec_params,
136 const FecProtectionParams* key_fec_params,
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000137 uint32_t* sent_video_rate_bps,
138 uint32_t* sent_nack_rate_bps,
139 uint32_t* sent_fec_rate_bps);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000140
141 // Implements VideoSendStatisticsCallback.
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000142 virtual int32_t SendStatistics(const uint32_t bit_rate,
143 const uint32_t frame_rate);
144 int32_t RegisterCodecObserver(ViEEncoderObserver* observer);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000145
146 // Implements RtcpIntraFrameObserver.
147 virtual void OnReceivedIntraFrameRequest(uint32_t ssrc);
148 virtual void OnReceivedSLI(uint32_t ssrc, uint8_t picture_id);
149 virtual void OnReceivedRPSI(uint32_t ssrc, uint64_t picture_id);
150 virtual void OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc);
151
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000152 // Sets SSRCs for all streams.
153 bool SetSsrcs(const std::list<unsigned int>& ssrcs);
154
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000155 // Effect filter.
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000156 int32_t RegisterEffectFilter(ViEEffectFilter* effect_filter);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000157
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000158 // Enables recording of debugging information.
159 virtual int StartDebugRecording(const char* fileNameUTF8);
160
161 // Disables recording of debugging information.
162 virtual int StopDebugRecording();
163
mflodman@webrtc.orgff9854b2012-10-25 13:06:22 +0000164 int channel_id() const { return channel_id_; }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000165 protected:
166 // Called by BitrateObserver.
167 void OnNetworkChanged(const uint32_t bitrate_bps,
168 const uint8_t fraction_lost,
169 const uint32_t round_trip_time_ms);
170
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +0000171 // Called by PacedSender.
hclam@chromium.org0f6f7cb2013-06-20 20:18:31 +0000172 bool TimeToSendPacket(uint32_t ssrc, uint16_t sequence_number,
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +0000173 int64_t capture_time_ms);
stefan@webrtc.org69f76052013-06-17 12:53:37 +0000174 int TimeToSendPadding(int bytes);
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +0000175
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000176 private:
stefan@webrtc.orgdca71b22013-03-27 16:36:01 +0000177 bool EncoderPaused() const;
178
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000179 int32_t engine_id_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000180 const int channel_id_;
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000181 const uint32_t number_of_cores_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000182
183 VideoCodingModule& vcm_;
184 VideoProcessingModule& vpm_;
185 scoped_ptr<RtpRtcp> default_rtp_rtcp_;
186 scoped_ptr<CriticalSectionWrapper> callback_cs_;
187 scoped_ptr<CriticalSectionWrapper> data_cs_;
188 scoped_ptr<BitrateObserver> bitrate_observer_;
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +0000189 scoped_ptr<PacedSender> paced_sender_;
190 scoped_ptr<ViEPacedSenderCallback> pacing_callback_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000191
192 BitrateController* bitrate_controller_;
193
stefan@webrtc.org69f76052013-06-17 12:53:37 +0000194 bool send_padding_;
stefan@webrtc.orgdca71b22013-03-27 16:36:01 +0000195 int target_delay_ms_;
196 bool network_is_transmitting_;
197 bool encoder_paused_;
pwestin@webrtc.org36bdba42013-05-02 19:02:17 +0000198 bool encoder_paused_and_dropped_frame_;
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000199 std::map<unsigned int, int64_t> time_last_intra_request_ms_;
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000200 int32_t channels_dropping_delta_frames_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000201 bool drop_next_frame_;
202
203 bool fec_enabled_;
204 bool nack_enabled_;
205
206 ViEEncoderObserver* codec_observer_;
207 ViEEffectFilter* effect_filter_;
208 ProcessThread& module_process_thread_;
209
210 bool has_received_sli_;
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000211 uint8_t picture_id_sli_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000212 bool has_received_rpsi_;
pbos@webrtc.org67879bc2013-04-09 13:41:51 +0000213 uint64_t picture_id_rpsi_;
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000214 std::map<unsigned int, int> ssrc_streams_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000215
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000216 // Quality modes callback
217 QMVideoSettingsCallback* qm_callback_;
218};
219
220} // namespace webrtc
221
222#endif // WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_