2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
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.
11 #ifndef WEBRTC_VIDEO_ENGINE_VIE_CHANNEL_H_
12 #define WEBRTC_VIDEO_ENGINE_VIE_CHANNEL_H_
16 #include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
17 #include "webrtc/modules/rtp_rtcp/interface/rtp_rtcp_defines.h"
18 #include "webrtc/modules/video_coding/main/interface/video_coding_defines.h"
19 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
20 #include "webrtc/system_wrappers/interface/scoped_ptr.h"
21 #include "webrtc/system_wrappers/interface/tick_util.h"
22 #include "webrtc/typedefs.h"
23 #include "webrtc/video_engine/include/vie_network.h"
24 #include "webrtc/video_engine/include/vie_rtp_rtcp.h"
25 #include "webrtc/video_engine/vie_defines.h"
26 #include "webrtc/video_engine/vie_frame_provider_base.h"
27 #include "webrtc/video_engine/vie_receiver.h"
28 #include "webrtc/video_engine/vie_sender.h"
29 #include "webrtc/video_engine/vie_sync_module.h"
33 class CallStatsObserver;
34 class ChannelStatsObserver;
36 class CriticalSectionWrapper;
37 class EncodedImageCallback;
38 class I420FrameCallback;
44 class ViEDecoderObserver;
45 class ViEEffectFilter;
46 class ViERTCPObserver;
48 class VideoCodingModule;
50 class VideoRenderCallback;
54 : public VCMFrameTypeCallback,
55 public VCMReceiveCallback,
56 public VCMReceiveStatisticsCallback,
57 public VCMDecoderTimingCallback,
58 public VCMPacketRequestCallback,
61 public ViEFrameProviderBase {
63 friend class ChannelStatsObserver;
65 ViEChannel(int32_t channel_id,
67 uint32_t number_of_cores,
69 ProcessThread& module_process_thread,
70 RtcpIntraFrameObserver* intra_frame_observer,
71 RtcpBandwidthObserver* bandwidth_observer,
72 RemoteBitrateEstimator* remote_bitrate_estimator,
73 RtcpRttStats* rtt_stats,
74 PacedSender* paced_sender,
75 RtpRtcp* default_rtp_rtcp,
81 // Sets the encoder to use for the channel. |new_stream| indicates the encoder
82 // type has changed and we should start a new RTP stream.
83 int32_t SetSendCodec(const VideoCodec& video_codec, bool new_stream = true);
84 int32_t SetReceiveCodec(const VideoCodec& video_codec);
85 int32_t GetReceiveCodec(VideoCodec* video_codec);
86 int32_t RegisterCodecObserver(ViEDecoderObserver* observer);
87 // Registers an external decoder. |buffered_rendering| means that the decoder
88 // will render frames after decoding according to the render timestamp
89 // provided by the video coding module. |render_delay| indicates the time
90 // needed to decode and render a frame.
91 int32_t RegisterExternalDecoder(const uint8_t pl_type,
92 VideoDecoder* decoder,
93 bool buffered_rendering,
94 int32_t render_delay);
95 int32_t DeRegisterExternalDecoder(const uint8_t pl_type);
96 int32_t ReceiveCodecStatistics(uint32_t* num_key_frames,
97 uint32_t* num_delta_frames);
98 uint32_t DiscardedPackets() const;
100 // Returns the estimated delay in milliseconds.
101 int ReceiveDelay() const;
103 // Only affects calls to SetReceiveCodec done after this call.
104 int32_t WaitForKeyFrame(bool wait);
106 // If enabled, a key frame request will be sent as soon as there are lost
107 // packets. If |only_key_frames| are set, requests are only sent for loss in
109 int32_t SetSignalPacketLossStatus(bool enable, bool only_key_frames);
111 int32_t SetRTCPMode(const RTCPMethod rtcp_mode);
112 int32_t GetRTCPMode(RTCPMethod* rtcp_mode);
113 int32_t SetNACKStatus(const bool enable);
114 int32_t SetFECStatus(const bool enable,
115 const unsigned char payload_typeRED,
116 const unsigned char payload_typeFEC);
117 int32_t SetHybridNACKFECStatus(const bool enable,
118 const unsigned char payload_typeRED,
119 const unsigned char payload_typeFEC);
120 int SetSenderBufferingMode(int target_delay_ms);
121 int SetReceiverBufferingMode(int target_delay_ms);
122 int32_t SetKeyFrameRequestMethod(const KeyFrameRequestMethod method);
123 bool EnableRemb(bool enable);
124 int SetSendTimestampOffsetStatus(bool enable, int id);
125 int SetReceiveTimestampOffsetStatus(bool enable, int id);
126 int SetSendAbsoluteSendTimeStatus(bool enable, int id);
127 int SetReceiveAbsoluteSendTimeStatus(bool enable, int id);
128 bool GetReceiveAbsoluteSendTimeStatus() const;
129 void SetRtcpXrRrtrStatus(bool enable);
130 void SetTransmissionSmoothingStatus(bool enable);
131 int32_t EnableTMMBR(const bool enable);
132 int32_t EnableKeyFrameRequestCallback(const bool enable);
134 // Sets SSRC for outgoing stream.
135 int32_t SetSSRC(const uint32_t SSRC,
136 const StreamType usage,
137 const unsigned char simulcast_idx);
139 // Gets SSRC for outgoing stream number |idx|.
140 int32_t GetLocalSSRC(uint8_t idx, unsigned int* ssrc);
142 // Gets SSRC for the incoming stream.
143 int32_t GetRemoteSSRC(uint32_t* ssrc);
145 // Gets the CSRC for the incoming stream.
146 int32_t GetRemoteCSRC(uint32_t CSRCs[kRtpCsrcSize]);
148 int SetRtxSendPayloadType(int payload_type);
149 // Only has an effect once RTX is enabled.
150 void SetPadWithRedundantPayloads(bool enable);
151 void SetRtxReceivePayloadType(int payload_type);
153 // Sets the starting sequence number, must be called before StartSend.
154 int32_t SetStartSequenceNumber(uint16_t sequence_number);
156 void SetRtpStateForSsrc(uint32_t ssrc, const RtpState& rtp_state);
157 RtpState GetRtpStateForSsrc(uint32_t ssrc);
159 // Sets the CName for the outgoing stream on the channel.
160 int32_t SetRTCPCName(const char rtcp_cname[]);
162 // Gets the CName of the incoming stream.
163 int32_t GetRemoteRTCPCName(char rtcp_cname[]);
164 int32_t RegisterRtpObserver(ViERTPObserver* observer);
165 int32_t RegisterRtcpObserver(ViERTCPObserver* observer);
166 int32_t SendApplicationDefinedRTCPPacket(
167 const uint8_t sub_type,
170 uint16_t data_length_in_bytes);
172 // Returns statistics reported by the remote client in an RTCP packet.
173 int32_t GetSendRtcpStatistics(uint16_t* fraction_lost,
174 uint32_t* cumulative_lost,
175 uint32_t* extended_max,
176 uint32_t* jitter_samples,
179 // Called on receipt of RTCP report block from remote side.
180 void RegisterSendChannelRtcpStatisticsCallback(
181 RtcpStatisticsCallback* callback);
183 // Returns our localy created statistics of the received RTP stream.
184 int32_t GetReceivedRtcpStatistics(uint16_t* fraction_lost,
185 uint32_t* cumulative_lost,
186 uint32_t* extended_max,
187 uint32_t* jitter_samples,
190 // Called on generation of RTCP stats
191 void RegisterReceiveChannelRtcpStatisticsCallback(
192 RtcpStatisticsCallback* callback);
194 // Gets sent/received packets statistics.
195 int32_t GetRtpStatistics(uint32_t* bytes_sent,
196 uint32_t* packets_sent,
197 uint32_t* bytes_received,
198 uint32_t* packets_received) const;
200 // Called on update of RTP statistics.
201 void RegisterSendChannelRtpStatisticsCallback(
202 StreamDataCountersCallback* callback);
204 // Called on update of RTP statistics.
205 void RegisterReceiveChannelRtpStatisticsCallback(
206 StreamDataCountersCallback* callback);
208 void GetRtcpPacketTypeCounters(RtcpPacketTypeCounter* packets_sent,
209 RtcpPacketTypeCounter* packets_received) const;
211 void GetBandwidthUsage(uint32_t* total_bitrate_sent,
212 uint32_t* video_bitrate_sent,
213 uint32_t* fec_bitrate_sent,
214 uint32_t* nackBitrateSent) const;
215 // TODO(holmer): Deprecated. We should use the SendSideDelayObserver instead
216 // to avoid deadlocks.
217 bool GetSendSideDelay(int* avg_send_delay, int* max_send_delay) const;
218 void RegisterSendSideDelayObserver(SendSideDelayObserver* observer);
219 void GetReceiveBandwidthEstimatorStats(
220 ReceiveBandwidthEstimatorStats* output) const;
222 // Called on any new send bitrate estimate.
223 void RegisterSendBitrateObserver(BitrateStatisticsObserver* observer);
225 int32_t StartRTPDump(const char file_nameUTF8[1024],
226 RTPDirections direction);
227 int32_t StopRTPDump(RTPDirections direction);
229 // Implements RtcpFeedback.
230 // TODO(pwestin) Depricate this functionality.
231 virtual void OnApplicationDataReceived(const int32_t id,
232 const uint8_t sub_type,
234 const uint16_t length,
235 const uint8_t* data);
236 // Implements RtpFeedback.
237 virtual int32_t OnInitializeDecoder(
239 const int8_t payload_type,
240 const char payload_name[RTP_PAYLOAD_NAME_SIZE],
242 const uint8_t channels,
243 const uint32_t rate);
244 virtual void OnIncomingSSRCChanged(const int32_t id,
245 const uint32_t ssrc);
246 virtual void OnIncomingCSRCChanged(const int32_t id,
249 virtual void ResetStatistics(uint32_t);
251 int32_t SetLocalReceiver(const uint16_t rtp_port,
252 const uint16_t rtcp_port,
253 const char* ip_address);
254 int32_t GetLocalReceiver(uint16_t* rtp_port,
256 char* ip_address) const;
257 int32_t SetSendDestination(const char* ip_address,
258 const uint16_t rtp_port,
259 const uint16_t rtcp_port,
260 const uint16_t source_rtp_port,
261 const uint16_t source_rtcp_port);
262 int32_t GetSendDestination(char* ip_address,
265 uint16_t* source_rtp_port,
266 uint16_t* source_rtcp_port) const;
267 int32_t GetSourceInfo(uint16_t* rtp_port,
270 uint32_t ip_address_length);
272 int32_t SetRemoteSSRCType(const StreamType usage, const uint32_t SSRC);
277 int32_t StartReceive();
278 int32_t StopReceive();
280 int32_t RegisterSendTransport(Transport* transport);
281 int32_t DeregisterSendTransport();
283 // Incoming packet from external transport.
284 int32_t ReceivedRTPPacket(const void* rtp_packet,
285 const int32_t rtp_packet_length,
286 const PacketTime& packet_time);
288 // Incoming packet from external transport.
289 int32_t ReceivedRTCPPacket(const void* rtcp_packet,
290 const int32_t rtcp_packet_length);
292 // Sets the maximum transfer unit size for the network link, i.e. including
293 // IP, UDP and RTP headers.
294 int32_t SetMTU(uint16_t mtu);
296 // Returns maximum allowed payload size, i.e. the maximum allowed size of
297 // encoded data in each packet.
298 uint16_t MaxDataPayloadLength() const;
299 int32_t SetMaxPacketBurstSize(uint16_t max_number_of_packets);
300 int32_t SetPacketBurstSpreadState(bool enable, const uint16_t frame_periodMS);
302 int32_t EnableColorEnhancement(bool enable);
304 // Gets the modules used by the channel.
307 CallStatsObserver* GetStatsObserver();
309 // Implements VCMReceiveCallback.
310 virtual int32_t FrameToRender(I420VideoFrame& video_frame); // NOLINT
312 // Implements VCMReceiveCallback.
313 virtual int32_t ReceivedDecodedReferenceFrame(
314 const uint64_t picture_id);
316 // Implements VCMReceiveCallback.
317 virtual void IncomingCodecChanged(const VideoCodec& codec);
319 // Implements VCMReceiveStatisticsCallback.
320 virtual int32_t OnReceiveStatisticsUpdate(const uint32_t bit_rate,
321 const uint32_t frame_rate);
323 // Implements VCMDecoderTimingCallback.
324 virtual void OnDecoderTiming(int decode_ms,
326 int current_delay_ms,
328 int jitter_buffer_ms,
329 int min_playout_delay_ms,
330 int render_delay_ms);
332 // Implements VideoFrameTypeCallback.
333 virtual int32_t RequestKeyFrame();
335 // Implements VideoFrameTypeCallback.
336 virtual int32_t SliceLossIndicationRequest(
337 const uint64_t picture_id);
339 // Implements VideoPacketRequestCallback.
340 virtual int32_t ResendPackets(const uint16_t* sequence_numbers,
343 int32_t SetVoiceChannel(int32_t ve_channel_id,
344 VoEVideoSync* ve_sync_interface);
345 int32_t VoiceChannel();
347 // Implements ViEFrameProviderBase.
348 virtual int FrameCallbackChanged() {return -1;}
350 int32_t RegisterEffectFilter(ViEEffectFilter* effect_filter);
352 // New-style callbacks, used by VideoReceiveStream.
353 void RegisterPreRenderCallback(I420FrameCallback* pre_render_callback);
354 void RegisterPreDecodeImageCallback(
355 EncodedImageCallback* pre_decode_callback);
357 void RegisterSendFrameCountObserver(FrameCountObserver* observer);
359 void ReceivedBWEPacket(int64_t arrival_time_ms, int payload_size,
360 const RTPHeader& header);
363 static bool ChannelDecodeThreadFunction(void* obj);
364 bool ChannelDecodeProcess();
366 void OnRttUpdate(uint32_t rtt);
369 void ReserveRtpRtcpModules(size_t total_modules)
370 EXCLUSIVE_LOCKS_REQUIRED(rtp_rtcp_cs_);
371 RtpRtcp* GetRtpRtcpModule(size_t simulcast_idx) const
372 EXCLUSIVE_LOCKS_REQUIRED(rtp_rtcp_cs_);
373 RtpRtcp* CreateRtpRtcpModule();
374 // Assumed to be protected.
375 int32_t StartDecodeThread();
376 int32_t StopDecodeThread();
378 int32_t ProcessNACKRequest(const bool enable);
379 int32_t ProcessFECRequest(const bool enable,
380 const unsigned char payload_typeRED,
381 const unsigned char payload_typeFEC);
382 // Compute NACK list parameters for the buffering mode.
383 int GetRequiredNackListSize(int target_delay_ms);
384 void SetRtxSendStatus(bool enable);
386 // ViEChannel exposes methods that allow to modify observers and callbacks
387 // to be modified. Such an API-style is cumbersome to implement and maintain
388 // at all the levels when comparing to only setting them at construction. As
389 // so this class instantiates its children with a wrapper that can be modified
392 class RegisterableCallback : public T {
394 RegisterableCallback()
395 : critsect_(CriticalSectionWrapper::CreateCriticalSection()),
398 void Set(T* callback) {
399 CriticalSectionScoped cs(critsect_.get());
400 callback_ = callback;
404 // Note: this should be implemented with a RW-lock to allow simultaneous
405 // calls into the callback. However that doesn't seem to be needed for the
406 // current type of callbacks covered by this class.
407 scoped_ptr<CriticalSectionWrapper> critsect_;
408 T* callback_ GUARDED_BY(critsect_);
411 DISALLOW_COPY_AND_ASSIGN(RegisterableCallback);
414 class RegisterableBitrateStatisticsObserver:
415 public RegisterableCallback<BitrateStatisticsObserver> {
416 virtual void Notify(const BitrateStatistics& stats, uint32_t ssrc) {
417 CriticalSectionScoped cs(critsect_.get());
419 callback_->Notify(stats, ssrc);
422 send_bitrate_observer_;
424 class RegisterableFrameCountObserver
425 : public RegisterableCallback<FrameCountObserver> {
426 virtual void FrameCountUpdated(FrameType frame_type,
427 uint32_t frame_count,
428 const unsigned int ssrc) {
429 CriticalSectionScoped cs(critsect_.get());
431 callback_->FrameCountUpdated(frame_type, frame_count, ssrc);
433 } send_frame_count_observer_;
435 class RegisterableSendSideDelayObserver :
436 public RegisterableCallback<SendSideDelayObserver> {
437 virtual void SendSideDelayUpdated(int avg_delay_ms,
439 uint32_t ssrc) OVERRIDE {
440 CriticalSectionScoped cs(critsect_.get());
442 callback_->SendSideDelayUpdated(avg_delay_ms, max_delay_ms, ssrc);
444 } send_side_delay_observer_;
448 uint32_t number_of_cores_;
449 uint8_t num_socket_threads_;
451 // Used for all registered callbacks except rendering.
452 scoped_ptr<CriticalSectionWrapper> callback_cs_;
453 scoped_ptr<CriticalSectionWrapper> rtp_rtcp_cs_;
455 RtpRtcp* default_rtp_rtcp_;
457 // Owned modules/classes.
458 scoped_ptr<RtpRtcp> rtp_rtcp_;
459 std::list<RtpRtcp*> simulcast_rtp_rtcp_;
460 std::list<RtpRtcp*> removed_rtp_rtcp_;
461 VideoCodingModule* const vcm_;
462 ViEReceiver vie_receiver_;
463 ViESender vie_sender_;
464 ViESyncModule vie_sync_;
466 // Helper to report call statistics.
467 scoped_ptr<ChannelStatsObserver> stats_observer_;
470 ProcessThread& module_process_thread_;
471 ViEDecoderObserver* codec_observer_;
472 bool do_key_frame_callbackRequest_;
473 ViERTPObserver* rtp_observer_;
474 ViERTCPObserver* rtcp_observer_;
475 RtcpIntraFrameObserver* intra_frame_observer_;
476 RtcpRttStats* rtt_stats_;
477 PacedSender* paced_sender_;
478 bool pad_with_redundant_payloads_;
480 scoped_ptr<RtcpBandwidthObserver> bandwidth_observer_;
481 int send_timestamp_extension_id_;
482 int absolute_send_time_extension_id_;
484 Transport* external_transport_;
487 // Current receive codec used for codec change callback.
488 VideoCodec receive_codec_;
489 bool wait_for_key_frame_;
490 ThreadWrapper* decode_thread_;
492 ViEEffectFilter* effect_filter_;
493 bool color_enhancement_;
495 // User set MTU, -1 if not set.
499 int nack_history_size_sender_;
500 int max_nack_reordering_threshold_;
501 I420FrameCallback* pre_render_callback_;
503 std::map<uint32_t, RTCPReportBlock> prev_report_blocks_;
506 } // namespace webrtc
508 #endif // WEBRTC_VIDEO_ENGINE_VIE_CHANNEL_H_