blob: 6c4aaff9d1a3a6150289cfe99512663cff0d79ba [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,
stefan@webrtc.org71f3f682013-01-09 08:35:40 +000073 WebRtc_UWord8 pl_type,
74 bool internal_source);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000075 WebRtc_Word32 DeRegisterExternalEncoder(WebRtc_UWord8 pl_type);
76 WebRtc_Word32 SetEncoder(const VideoCodec& video_codec);
77 WebRtc_Word32 GetEncoder(VideoCodec* video_codec);
78
79 WebRtc_Word32 GetCodecConfigParameters(
80 unsigned char config_parameters[kConfigParameterSize],
81 unsigned char& config_parameters_size);
82
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +000083 PacedSender* GetPacedSender();
84
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000085 // Scale or crop/pad image.
86 WebRtc_Word32 ScaleInputImage(bool enable);
87
88 // RTP settings.
89 RtpRtcp* SendRtpRtcpModule();
90
91 // Implementing ViEFrameCallback.
92 virtual void DeliverFrame(int id,
mikhal@webrtc.org3bbed742012-10-24 18:33:04 +000093 I420VideoFrame* video_frame,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000094 int num_csrcs = 0,
95 const WebRtc_UWord32 CSRC[kRtpCsrcSize] = NULL);
96 virtual void DelayChanged(int id, int frame_delay);
97 virtual int GetPreferedFrameSettings(int* width,
98 int* height,
99 int* frame_rate);
100
101 virtual void ProviderDestroyed(int id) {
102 return;
103 }
104
105 WebRtc_Word32 SendKeyFrame();
106 WebRtc_Word32 SendCodecStatistics(WebRtc_UWord32* num_key_frames,
107 WebRtc_UWord32* num_delta_frames);
108
109 WebRtc_Word32 EstimatedSendBandwidth(
110 WebRtc_UWord32* available_bandwidth) const;
111
112 int CodecTargetBitrate(WebRtc_UWord32* bitrate) const;
113 // Loss protection.
114 WebRtc_Word32 UpdateProtectionMethod();
115
mikhal@webrtc.org0c66de62013-02-10 18:42:55 +0000116 // Streaming mode.
117 void EnableSenderStreamingMode(int target_delay_ms);
118
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000119 // Implements VCMPacketizationCallback.
120 virtual WebRtc_Word32 SendData(
121 FrameType frame_type,
122 WebRtc_UWord8 payload_type,
123 WebRtc_UWord32 time_stamp,
124 int64_t capture_time_ms,
125 const WebRtc_UWord8* payload_data,
126 WebRtc_UWord32 payload_size,
127 const RTPFragmentationHeader& fragmentation_header,
128 const RTPVideoHeader* rtp_video_hdr);
129
130 // Implements VideoProtectionCallback.
131 virtual int ProtectionRequest(
132 const FecProtectionParams* delta_fec_params,
133 const FecProtectionParams* key_fec_params,
134 WebRtc_UWord32* sent_video_rate_bps,
135 WebRtc_UWord32* sent_nack_rate_bps,
136 WebRtc_UWord32* sent_fec_rate_bps);
137
138 // Implements VideoSendStatisticsCallback.
139 virtual WebRtc_Word32 SendStatistics(const WebRtc_UWord32 bit_rate,
140 const WebRtc_UWord32 frame_rate);
141 WebRtc_Word32 RegisterCodecObserver(ViEEncoderObserver* observer);
142
143 // Implements RtcpIntraFrameObserver.
144 virtual void OnReceivedIntraFrameRequest(uint32_t ssrc);
145 virtual void OnReceivedSLI(uint32_t ssrc, uint8_t picture_id);
146 virtual void OnReceivedRPSI(uint32_t ssrc, uint64_t picture_id);
147 virtual void OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc);
148
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000149 // Sets SSRCs for all streams.
150 bool SetSsrcs(const std::list<unsigned int>& ssrcs);
151
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000152 // Effect filter.
153 WebRtc_Word32 RegisterEffectFilter(ViEEffectFilter* effect_filter);
154
155 // Recording.
156 ViEFileRecorder& GetOutgoingFileRecorder();
157
158 // 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.
172 void TimeToSendPacket(uint32_t ssrc, uint16_t sequence_number,
173 int64_t capture_time_ms);
174
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000175 private:
176 WebRtc_Word32 engine_id_;
177 const int channel_id_;
178 const WebRtc_UWord32 number_of_cores_;
179
180 VideoCodingModule& vcm_;
181 VideoProcessingModule& vpm_;
182 scoped_ptr<RtpRtcp> default_rtp_rtcp_;
183 scoped_ptr<CriticalSectionWrapper> callback_cs_;
184 scoped_ptr<CriticalSectionWrapper> data_cs_;
185 scoped_ptr<BitrateObserver> bitrate_observer_;
pwestin@webrtc.org5e87b5f2012-11-13 21:12:39 +0000186 scoped_ptr<PacedSender> paced_sender_;
187 scoped_ptr<ViEPacedSenderCallback> pacing_callback_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000188
189 BitrateController* bitrate_controller_;
190
191 bool paused_;
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000192 std::map<unsigned int, int64_t> time_last_intra_request_ms_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000193 WebRtc_Word32 channels_dropping_delta_frames_;
194 bool drop_next_frame_;
195
196 bool fec_enabled_;
197 bool nack_enabled_;
198
199 ViEEncoderObserver* codec_observer_;
200 ViEEffectFilter* effect_filter_;
201 ProcessThread& module_process_thread_;
202
203 bool has_received_sli_;
204 WebRtc_UWord8 picture_id_sli_;
205 bool has_received_rpsi_;
206 WebRtc_UWord64 picture_id_rpsi_;
mflodman@webrtc.orgb6d9cfc2012-10-25 11:30:29 +0000207 std::map<unsigned int, int> ssrc_streams_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000208
209 ViEFileRecorder file_recorder_;
210
211 // Quality modes callback
212 QMVideoSettingsCallback* qm_callback_;
213};
214
215} // namespace webrtc
216
217#endif // WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_