blob: e0960338bed78f46f53fd1801a2dd272bb151963 [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
51 ViEEncoder(WebRtc_Word32 engine_id,
52 WebRtc_Word32 channel_id,
53 WebRtc_UWord32 number_of_cores,
54 ProcessThread& module_process_thread,
55 BitrateController* bitrate_controller);
56 ~ViEEncoder();
57
58 bool Init();
59
60 // Returns the id of the owning channel.
61 int Owner() const;
62
63 // Drops incoming packets before they get to the encoder.
64 void Pause();
65 void Restart();
66
67 WebRtc_Word32 DropDeltaAfterKey(bool enable);
68
69 // Codec settings.
70 WebRtc_UWord8 NumberOfCodecs();
71 WebRtc_Word32 GetCodec(WebRtc_UWord8 list_index, VideoCodec* video_codec);
72 WebRtc_Word32 RegisterExternalEncoder(VideoEncoder* encoder,
73 WebRtc_UWord8 pl_type);
74 WebRtc_Word32 DeRegisterExternalEncoder(WebRtc_UWord8 pl_type);
75 WebRtc_Word32 SetEncoder(const VideoCodec& video_codec);
76 WebRtc_Word32 GetEncoder(VideoCodec* video_codec);
77
78 WebRtc_Word32 GetCodecConfigParameters(
79 unsigned char config_parameters[kConfigParameterSize],
80 unsigned char& config_parameters_size);
81
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +000082 PacedSender* GetPacedSender();
83
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000084 // Scale or crop/pad image.
85 WebRtc_Word32 ScaleInputImage(bool enable);
86
87 // RTP settings.
88 RtpRtcp* SendRtpRtcpModule();
89
90 // Implementing ViEFrameCallback.
91 virtual void DeliverFrame(int id,
mikhal@webrtc.org3bbed742012-10-24 18:33:04 +000092 I420VideoFrame* video_frame,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000093 int num_csrcs = 0,
94 const WebRtc_UWord32 CSRC[kRtpCsrcSize] = NULL);
95 virtual void DelayChanged(int id, int frame_delay);
96 virtual int GetPreferedFrameSettings(int* width,
97 int* height,
98 int* frame_rate);
99
100 virtual void ProviderDestroyed(int id) {
101 return;
102 }
103
104 WebRtc_Word32 SendKeyFrame();
105 WebRtc_Word32 SendCodecStatistics(WebRtc_UWord32* num_key_frames,
106 WebRtc_UWord32* num_delta_frames);
107
108 WebRtc_Word32 EstimatedSendBandwidth(
109 WebRtc_UWord32* available_bandwidth) const;
110
111 int CodecTargetBitrate(WebRtc_UWord32* bitrate) const;
112 // Loss protection.
113 WebRtc_Word32 UpdateProtectionMethod();
114
115 // Implements VCMPacketizationCallback.
116 virtual WebRtc_Word32 SendData(
117 FrameType frame_type,
118 WebRtc_UWord8 payload_type,
119 WebRtc_UWord32 time_stamp,
120 int64_t capture_time_ms,
121 const WebRtc_UWord8* payload_data,
122 WebRtc_UWord32 payload_size,
123 const RTPFragmentationHeader& fragmentation_header,
124 const RTPVideoHeader* rtp_video_hdr);
125
126 // Implements VideoProtectionCallback.
127 virtual int ProtectionRequest(
128 const FecProtectionParams* delta_fec_params,
129 const FecProtectionParams* key_fec_params,
130 WebRtc_UWord32* sent_video_rate_bps,
131 WebRtc_UWord32* sent_nack_rate_bps,
132 WebRtc_UWord32* sent_fec_rate_bps);
133
134 // Implements VideoSendStatisticsCallback.
135 virtual WebRtc_Word32 SendStatistics(const WebRtc_UWord32 bit_rate,
136 const WebRtc_UWord32 frame_rate);
137 WebRtc_Word32 RegisterCodecObserver(ViEEncoderObserver* observer);
138
139 // Implements RtcpIntraFrameObserver.
140 virtual void OnReceivedIntraFrameRequest(uint32_t ssrc);
141 virtual void OnReceivedSLI(uint32_t ssrc, uint8_t picture_id);
142 virtual void OnReceivedRPSI(uint32_t ssrc, uint64_t picture_id);
143 virtual void OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc);
144
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000145 // Sets SSRCs for all streams.
146 bool SetSsrcs(const std::list<unsigned int>& ssrcs);
147
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000148 // Effect filter.
149 WebRtc_Word32 RegisterEffectFilter(ViEEffectFilter* effect_filter);
150
151 // Recording.
152 ViEFileRecorder& GetOutgoingFileRecorder();
153
154 // Enables recording of debugging information.
155 virtual int StartDebugRecording(const char* fileNameUTF8);
156
157 // Disables recording of debugging information.
158 virtual int StopDebugRecording();
159
mflodman@webrtc.orgff9854b2012-10-25 13:06:22 +0000160 int channel_id() const { return channel_id_; }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000161 protected:
162 // Called by BitrateObserver.
163 void OnNetworkChanged(const uint32_t bitrate_bps,
164 const uint8_t fraction_lost,
165 const uint32_t round_trip_time_ms);
166
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +0000167 // Called by PacedSender.
168 void TimeToSendPacket(uint32_t ssrc, uint16_t sequence_number,
169 int64_t capture_time_ms);
170
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000171 private:
172 WebRtc_Word32 engine_id_;
173 const int channel_id_;
174 const WebRtc_UWord32 number_of_cores_;
175
176 VideoCodingModule& vcm_;
177 VideoProcessingModule& vpm_;
178 scoped_ptr<RtpRtcp> default_rtp_rtcp_;
179 scoped_ptr<CriticalSectionWrapper> callback_cs_;
180 scoped_ptr<CriticalSectionWrapper> data_cs_;
181 scoped_ptr<BitrateObserver> bitrate_observer_;
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +0000182 scoped_ptr<PacedSender> paced_sender_;
183 scoped_ptr<ViEPacedSenderCallback> pacing_callback_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000184
185 BitrateController* bitrate_controller_;
186
187 bool paused_;
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000188 std::map<unsigned int, int64_t> time_last_intra_request_ms_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000189 WebRtc_Word32 channels_dropping_delta_frames_;
190 bool drop_next_frame_;
191
192 bool fec_enabled_;
193 bool nack_enabled_;
194
195 ViEEncoderObserver* codec_observer_;
196 ViEEffectFilter* effect_filter_;
197 ProcessThread& module_process_thread_;
198
199 bool has_received_sli_;
200 WebRtc_UWord8 picture_id_sli_;
201 bool has_received_rpsi_;
202 WebRtc_UWord64 picture_id_rpsi_;
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000203 std::map<unsigned int, int> ssrc_streams_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000204
205 ViEFileRecorder file_recorder_;
206
207 // Quality modes callback
208 QMVideoSettingsCallback* qm_callback_;
209};
210
211} // namespace webrtc
212
213#endif // WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_