blob: 339390f235d1a6c3b59a9ec4eff880da8a54d9b1 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
stefan@webrtc.org07b45a52012-02-02 08:37:48 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
mflodman@webrtc.orge06ca3c2012-06-29 13:20:14 +000011#ifndef WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_
12#define WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +000014#include <list>
15#include <map>
16
mflodman@webrtc.orgf5e99db2012-06-27 09:49:37 +000017#include "common_types.h" // NOLINT
18#include "typedefs.h" //NOLINT
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000019#include "modules/bitrate_controller/include/bitrate_controller.h"
20#include "modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
mflodman@webrtc.orgf5e99db2012-06-27 09:49:37 +000021#include "modules/video_coding/main/interface/video_coding_defines.h"
22#include "modules/video_processing/main/interface/video_processing.h"
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +000023#include "system_wrappers/interface/scoped_ptr.h"
mflodman@webrtc.orgf5e99db2012-06-27 09:49:37 +000024#include "video_engine/vie_defines.h"
25#include "video_engine/vie_file_recorder.h"
26#include "video_engine/vie_frame_provider_base.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000027
28namespace webrtc {
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000029
niklase@google.com470e71d2011-07-07 08:21:25 +000030class CriticalSectionWrapper;
31class ProcessThread;
stefan@webrtc.org439be292012-02-16 14:45:37 +000032class QMVideoSettingsCallback;
niklase@google.com470e71d2011-07-07 08:21:25 +000033class RtpRtcp;
niklase@google.com470e71d2011-07-07 08:21:25 +000034class VideoCodingModule;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000035class ViEBitrateObserver;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000036class ViEEffectFilter;
niklase@google.com470e71d2011-07-07 08:21:25 +000037class ViEEncoderObserver;
38
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000039class ViEEncoder
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000040 : public RtcpIntraFrameObserver,
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000041 public VCMPacketizationCallback,
42 public VCMProtectionCallback,
43 public VCMSendStatisticsCallback,
44 public ViEFrameCallback {
45 public:
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000046 friend class ViEBitrateObserver;
47
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000048 ViEEncoder(WebRtc_Word32 engine_id,
49 WebRtc_Word32 channel_id,
50 WebRtc_UWord32 number_of_cores,
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +000051 ProcessThread& module_process_thread,
52 BitrateController* bitrate_controller);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000053 ~ViEEncoder();
niklase@google.com470e71d2011-07-07 08:21:25 +000054
wu@webrtc.org5d8c1022012-04-10 16:54:05 +000055 bool Init();
56
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +000057 // Returns the id of the owning channel.
58 int Owner() const;
59
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000060 // Drops incoming packets before they get to the encoder.
61 void Pause();
62 void Restart();
niklase@google.com470e71d2011-07-07 08:21:25 +000063
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000064 WebRtc_Word32 DropDeltaAfterKey(bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000065
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000066 // Codec settings.
67 WebRtc_UWord8 NumberOfCodecs();
mflodman@webrtc.orgf5e99db2012-06-27 09:49:37 +000068 WebRtc_Word32 GetCodec(WebRtc_UWord8 list_index, VideoCodec* video_codec);
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000069 WebRtc_Word32 RegisterExternalEncoder(VideoEncoder* encoder,
70 WebRtc_UWord8 pl_type);
71 WebRtc_Word32 DeRegisterExternalEncoder(WebRtc_UWord8 pl_type);
72 WebRtc_Word32 SetEncoder(const VideoCodec& video_codec);
mflodman@webrtc.orgf5e99db2012-06-27 09:49:37 +000073 WebRtc_Word32 GetEncoder(VideoCodec* video_codec);
niklase@google.com470e71d2011-07-07 08:21:25 +000074
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000075 WebRtc_Word32 GetCodecConfigParameters(
76 unsigned char config_parameters[kConfigParameterSize],
77 unsigned char& config_parameters_size);
niklase@google.com470e71d2011-07-07 08:21:25 +000078
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000079 // Scale or crop/pad image.
80 WebRtc_Word32 ScaleInputImage(bool enable);
niklase@google.com470e71d2011-07-07 08:21:25 +000081
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000082 // RTP settings.
83 RtpRtcp* SendRtpRtcpModule();
niklase@google.com470e71d2011-07-07 08:21:25 +000084
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000085 // Implementing ViEFrameCallback.
86 virtual void DeliverFrame(int id,
mikhal@webrtc.org9fedff72012-10-24 18:33:04 +000087 I420VideoFrame* video_frame,
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000088 int num_csrcs = 0,
89 const WebRtc_UWord32 CSRC[kRtpCsrcSize] = NULL);
90 virtual void DelayChanged(int id, int frame_delay);
mflodman@webrtc.org8baed512012-06-21 12:11:50 +000091 virtual int GetPreferedFrameSettings(int* width,
92 int* height,
93 int* frame_rate);
niklase@google.com470e71d2011-07-07 08:21:25 +000094
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000095 virtual void ProviderDestroyed(int id) {
96 return;
97 }
niklase@google.com470e71d2011-07-07 08:21:25 +000098
mflodman@webrtc.org84d17832011-12-01 17:02:23 +000099 WebRtc_Word32 SendKeyFrame();
mflodman@webrtc.orgf5e99db2012-06-27 09:49:37 +0000100 WebRtc_Word32 SendCodecStatistics(WebRtc_UWord32* num_key_frames,
101 WebRtc_UWord32* num_delta_frames);
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000102
stefan@webrtc.org439be292012-02-16 14:45:37 +0000103 WebRtc_Word32 EstimatedSendBandwidth(
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000104 WebRtc_UWord32* available_bandwidth) const;
105
stefan@webrtc.org439be292012-02-16 14:45:37 +0000106 int CodecTargetBitrate(WebRtc_UWord32* bitrate) const;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000107 // Loss protection.
108 WebRtc_Word32 UpdateProtectionMethod();
marpan@google.com80c5d7a2011-07-15 21:32:40 +0000109
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000110 // Implements VCMPacketizationCallback.
111 virtual WebRtc_Word32 SendData(
andrew@webrtc.orgcdfa63f2012-08-10 22:56:17 +0000112 FrameType frame_type,
113 WebRtc_UWord8 payload_type,
114 WebRtc_UWord32 time_stamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000115 int64_t capture_time_ms,
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000116 const WebRtc_UWord8* payload_data,
andrew@webrtc.orgcdfa63f2012-08-10 22:56:17 +0000117 WebRtc_UWord32 payload_size,
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000118 const RTPFragmentationHeader& fragmentation_header,
119 const RTPVideoHeader* rtp_video_hdr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000120
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000121 // Implements VideoProtectionCallback.
stefan@webrtc.orge0d6fa42012-03-20 22:10:56 +0000122 virtual int ProtectionRequest(
123 const FecProtectionParams* delta_fec_params,
124 const FecProtectionParams* key_fec_params,
stefan@webrtc.orgf4c82862011-12-13 15:38:14 +0000125 WebRtc_UWord32* sent_video_rate_bps,
126 WebRtc_UWord32* sent_nack_rate_bps,
127 WebRtc_UWord32* sent_fec_rate_bps);
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +0000128
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000129 // Implements VideoSendStatisticsCallback.
130 virtual WebRtc_Word32 SendStatistics(const WebRtc_UWord32 bit_rate,
131 const WebRtc_UWord32 frame_rate);
132 WebRtc_Word32 RegisterCodecObserver(ViEEncoderObserver* observer);
niklase@google.com470e71d2011-07-07 08:21:25 +0000133
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000134 // Implements RtcpIntraFrameObserver.
andrew@webrtc.org96636862012-09-20 23:33:17 +0000135 virtual void OnReceivedIntraFrameRequest(uint32_t ssrc);
mflodman@webrtc.orgaca26292012-10-05 16:17:41 +0000136 virtual void OnReceivedSLI(uint32_t ssrc, uint8_t picture_id);
137 virtual void OnReceivedRPSI(uint32_t ssrc, uint64_t picture_id);
138 virtual void OnLocalSsrcChanged(uint32_t old_ssrc, uint32_t new_ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000139
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000140 // Sets SSRCs for all streams.
141 bool SetSsrcs(const std::list<unsigned int>& ssrcs);
142
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000143 // Effect filter.
144 WebRtc_Word32 RegisterEffectFilter(ViEEffectFilter* effect_filter);
niklase@google.com470e71d2011-07-07 08:21:25 +0000145
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000146 // Recording.
147 ViEFileRecorder& GetOutgoingFileRecorder();
niklase@google.com470e71d2011-07-07 08:21:25 +0000148
mikhal@webrtc.orge41bbdf2012-08-28 16:15:16 +0000149 // Enables recording of debugging information.
150 virtual int StartDebugRecording(const char* fileNameUTF8);
151
152 // Disables recording of debugging information.
153 virtual int StopDebugRecording();
154
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000155 protected:
156 // Called by BitrateObserver.
157 void OnNetworkChanged(const uint32_t bitrate_bps,
158 const uint8_t fraction_lost,
159 const uint32_t round_trip_time_ms);
160
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000161 private:
162 WebRtc_Word32 engine_id_;
mflodman@webrtc.org9ec883e2012-03-05 17:12:41 +0000163 const int channel_id_;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000164 const WebRtc_UWord32 number_of_cores_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000165
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000166 VideoCodingModule& vcm_;
167 VideoProcessingModule& vpm_;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000168 scoped_ptr<RtpRtcp> default_rtp_rtcp_;
mflodman@webrtc.orgd32c4472011-12-22 14:17:53 +0000169 scoped_ptr<CriticalSectionWrapper> callback_cs_;
170 scoped_ptr<CriticalSectionWrapper> data_cs_;
pwestin@webrtc.org49888ce2012-04-27 05:25:53 +0000171 scoped_ptr<BitrateObserver> bitrate_observer_;
172
173 BitrateController* bitrate_controller_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000174
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000175 bool paused_;
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000176 std::map<unsigned int, int64_t> time_last_intra_request_ms_;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000177 WebRtc_Word32 channels_dropping_delta_frames_;
178 bool drop_next_frame_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000179
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000180 bool fec_enabled_;
181 bool nack_enabled_;
182
183 ViEEncoderObserver* codec_observer_;
184 ViEEffectFilter* effect_filter_;
185 ProcessThread& module_process_thread_;
186
187 bool has_received_sli_;
188 WebRtc_UWord8 picture_id_sli_;
189 bool has_received_rpsi_;
190 WebRtc_UWord64 picture_id_rpsi_;
mflodman@webrtc.orgd6ec3862012-10-25 11:30:29 +0000191 std::map<unsigned int, int> ssrc_streams_;
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000192
193 ViEFileRecorder file_recorder_;
194
195 // Quality modes callback
stefan@webrtc.org439be292012-02-16 14:45:37 +0000196 QMVideoSettingsCallback* qm_callback_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000197};
mflodman@webrtc.org84d17832011-12-01 17:02:23 +0000198
199} // namespace webrtc
200
mflodman@webrtc.orge06ca3c2012-06-29 13:20:14 +0000201#endif // WEBRTC_VIDEO_ENGINE_VIE_ENCODER_H_