vie_channel.h revision 4070935f4fb5b9fb2df246d7073fe0ba7e350791
1/*
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_CHANNEL_H_
12#define WEBRTC_VIDEO_ENGINE_VIE_CHANNEL_H_
13
14#include <list>
15
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/scoped_ptr.h"
20#include "webrtc/system_wrappers/interface/tick_util.h"
21#include "webrtc/typedefs.h"
22#include "webrtc/video_engine/include/vie_network.h"
23#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
24#include "webrtc/video_engine/vie_defines.h"
25#include "webrtc/video_engine/vie_frame_provider_base.h"
26#include "webrtc/video_engine/vie_receiver.h"
27#include "webrtc/video_engine/vie_sender.h"
28#include "webrtc/video_engine/vie_sync_module.h"
29
30namespace webrtc {
31
32class CallStatsObserver;
33class ChannelStatsObserver;
34class Config;
35class CriticalSectionWrapper;
36class EncodedImageCallback;
37class Encryption;
38class I420FrameCallback;
39class PacedSender;
40class ProcessThread;
41class RtcpRttStats;
42class RtpRtcp;
43class ThreadWrapper;
44class ViEDecoderObserver;
45class ViEEffectFilter;
46class ViERTCPObserver;
47class ViERTPObserver;
48class VideoCodingModule;
49class VideoDecoder;
50class VideoRenderCallback;
51class VoEVideoSync;
52
53class ViEChannel
54    : public VCMFrameTypeCallback,
55      public VCMReceiveCallback,
56      public VCMReceiveStatisticsCallback,
57      public VCMDecoderTimingCallback,
58      public VCMPacketRequestCallback,
59      public RtcpFeedback,
60      public RtpFeedback,
61      public ViEFrameProviderBase {
62 public:
63  friend class ChannelStatsObserver;
64
65  ViEChannel(int32_t channel_id,
66             int32_t engine_id,
67             uint32_t number_of_cores,
68             const Config& config,
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,
76             bool sender);
77  ~ViEChannel();
78
79  int32_t Init();
80
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;
99
100  // Returns the estimated delay in milliseconds.
101  int ReceiveDelay() const;
102
103  // Only affects calls to SetReceiveCodec done after this call.
104  int32_t WaitForKeyFrame(bool wait);
105
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
108  // key frames.
109  int32_t SetSignalPacketLossStatus(bool enable, bool only_key_frames);
110
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);
133
134  // Sets SSRC for outgoing stream.
135  int32_t SetSSRC(const uint32_t SSRC,
136                  const StreamType usage,
137                  const unsigned char simulcast_idx);
138
139  // Gets SSRC for outgoing stream number |idx|.
140  int32_t GetLocalSSRC(uint8_t idx, unsigned int* ssrc);
141
142  // Gets SSRC for the incoming stream.
143  int32_t GetRemoteSSRC(uint32_t* ssrc);
144
145  // Gets the CSRC for the incoming stream.
146  int32_t GetRemoteCSRC(uint32_t CSRCs[kRtpCsrcSize]);
147
148  int SetRtxSendPayloadType(int payload_type);
149  void SetRtxReceivePayloadType(int payload_type);
150
151  // Sets the starting sequence number, must be called before StartSend.
152  int32_t SetStartSequenceNumber(uint16_t sequence_number);
153
154  // Sets the CName for the outgoing stream on the channel.
155  int32_t SetRTCPCName(const char rtcp_cname[]);
156
157  // Gets the CName for the outgoing stream on the channel.
158  int32_t GetRTCPCName(char rtcp_cname[]);
159
160  // Gets the CName of the incoming stream.
161  int32_t GetRemoteRTCPCName(char rtcp_cname[]);
162  int32_t RegisterRtpObserver(ViERTPObserver* observer);
163  int32_t RegisterRtcpObserver(ViERTCPObserver* observer);
164  int32_t SendApplicationDefinedRTCPPacket(
165      const uint8_t sub_type,
166      uint32_t name,
167      const uint8_t* data,
168      uint16_t data_length_in_bytes);
169
170  // Returns statistics reported by the remote client in an RTCP packet.
171  int32_t GetSendRtcpStatistics(uint16_t* fraction_lost,
172                                uint32_t* cumulative_lost,
173                                uint32_t* extended_max,
174                                uint32_t* jitter_samples,
175                                int32_t* rtt_ms);
176
177  // Returns our localy created statistics of the received RTP stream.
178  int32_t GetReceivedRtcpStatistics(uint16_t* fraction_lost,
179                                    uint32_t* cumulative_lost,
180                                    uint32_t* extended_max,
181                                    uint32_t* jitter_samples,
182                                    int32_t* rtt_ms);
183
184  // Gets sent/received packets statistics.
185  int32_t GetRtpStatistics(uint32_t* bytes_sent,
186                           uint32_t* packets_sent,
187                           uint32_t* bytes_received,
188                           uint32_t* packets_received) const;
189  void GetBandwidthUsage(uint32_t* total_bitrate_sent,
190                         uint32_t* video_bitrate_sent,
191                         uint32_t* fec_bitrate_sent,
192                         uint32_t* nackBitrateSent) const;
193  void GetEstimatedReceiveBandwidth(uint32_t* estimated_bandwidth) const;
194
195  int32_t StartRTPDump(const char file_nameUTF8[1024],
196                       RTPDirections direction);
197  int32_t StopRTPDump(RTPDirections direction);
198
199  // Implements RtcpFeedback.
200  // TODO(pwestin) Depricate this functionality.
201  virtual void OnApplicationDataReceived(const int32_t id,
202                                         const uint8_t sub_type,
203                                         const uint32_t name,
204                                         const uint16_t length,
205                                         const uint8_t* data);
206  // Implements RtpFeedback.
207  virtual int32_t OnInitializeDecoder(
208      const int32_t id,
209      const int8_t payload_type,
210      const char payload_name[RTP_PAYLOAD_NAME_SIZE],
211      const int frequency,
212      const uint8_t channels,
213      const uint32_t rate);
214  virtual void OnIncomingSSRCChanged(const int32_t id,
215                                     const uint32_t ssrc);
216  virtual void OnIncomingCSRCChanged(const int32_t id,
217                                     const uint32_t CSRC,
218                                     const bool added);
219  virtual void ResetStatistics(uint32_t);
220
221  int32_t SetLocalReceiver(const uint16_t rtp_port,
222                           const uint16_t rtcp_port,
223                           const char* ip_address);
224  int32_t GetLocalReceiver(uint16_t* rtp_port,
225                           uint16_t* rtcp_port,
226                           char* ip_address) const;
227  int32_t SetSendDestination(const char* ip_address,
228                             const uint16_t rtp_port,
229                             const uint16_t rtcp_port,
230                             const uint16_t source_rtp_port,
231                             const uint16_t source_rtcp_port);
232  int32_t GetSendDestination(char* ip_address,
233                             uint16_t* rtp_port,
234                             uint16_t* rtcp_port,
235                             uint16_t* source_rtp_port,
236                             uint16_t* source_rtcp_port) const;
237  int32_t GetSourceInfo(uint16_t* rtp_port,
238                        uint16_t* rtcp_port,
239                        char* ip_address,
240                        uint32_t ip_address_length);
241
242  int32_t SetRemoteSSRCType(const StreamType usage, const uint32_t SSRC);
243
244  int32_t StartSend();
245  int32_t StopSend();
246  bool Sending();
247  int32_t StartReceive();
248  int32_t StopReceive();
249
250  int32_t RegisterSendTransport(Transport* transport);
251  int32_t DeregisterSendTransport();
252
253  // Incoming packet from external transport.
254  int32_t ReceivedRTPPacket(const void* rtp_packet,
255                            const int32_t rtp_packet_length);
256
257  // Incoming packet from external transport.
258  int32_t ReceivedRTCPPacket(const void* rtcp_packet,
259                             const int32_t rtcp_packet_length);
260
261  // Sets the maximum transfer unit size for the network link, i.e. including
262  // IP, UDP and RTP headers.
263  int32_t SetMTU(uint16_t mtu);
264
265  // Returns maximum allowed payload size, i.e. the maximum allowed size of
266  // encoded data in each packet.
267  uint16_t MaxDataPayloadLength() const;
268  int32_t SetMaxPacketBurstSize(uint16_t max_number_of_packets);
269  int32_t SetPacketBurstSpreadState(bool enable, const uint16_t frame_periodMS);
270
271  int32_t EnableColorEnhancement(bool enable);
272
273  // Gets the modules used by the channel.
274  RtpRtcp* rtp_rtcp();
275
276  CallStatsObserver* GetStatsObserver();
277
278  // Implements VCMReceiveCallback.
279  virtual int32_t FrameToRender(I420VideoFrame& video_frame);  // NOLINT
280
281  // Implements VCMReceiveCallback.
282  virtual int32_t ReceivedDecodedReferenceFrame(
283      const uint64_t picture_id);
284
285  // Implements VCMReceiveCallback.
286  virtual void IncomingCodecChanged(const VideoCodec& codec);
287
288  // Implements VCMReceiveStatisticsCallback.
289  virtual int32_t OnReceiveStatisticsUpdate(const uint32_t bit_rate,
290                                    const uint32_t frame_rate);
291
292  // Implements VCMDecoderTimingCallback.
293  virtual void OnDecoderTiming(int decode_ms,
294                               int max_decode_ms,
295                               int current_delay_ms,
296                               int target_delay_ms,
297                               int jitter_buffer_ms,
298                               int min_playout_delay_ms,
299                               int render_delay_ms);
300
301  // Implements VideoFrameTypeCallback.
302  virtual int32_t RequestKeyFrame();
303
304  // Implements VideoFrameTypeCallback.
305  virtual int32_t SliceLossIndicationRequest(
306      const uint64_t picture_id);
307
308  // Implements VideoPacketRequestCallback.
309  virtual int32_t ResendPackets(const uint16_t* sequence_numbers,
310                                uint16_t length);
311
312  int32_t RegisterExternalEncryption(Encryption* encryption);
313  int32_t DeRegisterExternalEncryption();
314
315  int32_t SetVoiceChannel(int32_t ve_channel_id,
316                          VoEVideoSync* ve_sync_interface);
317  int32_t VoiceChannel();
318
319  // Implements ViEFrameProviderBase.
320  virtual int FrameCallbackChanged() {return -1;}
321
322  int32_t RegisterEffectFilter(ViEEffectFilter* effect_filter);
323
324  // New-style callbacks, used by VideoReceiveStream.
325  void RegisterPreRenderCallback(I420FrameCallback* pre_render_callback);
326  void RegisterPreDecodeImageCallback(
327      EncodedImageCallback* pre_decode_callback);
328
329 protected:
330  static bool ChannelDecodeThreadFunction(void* obj);
331  bool ChannelDecodeProcess();
332
333  void OnRttUpdate(uint32_t rtt);
334
335 private:
336  // Assumed to be protected.
337  int32_t StartDecodeThread();
338  int32_t StopDecodeThread();
339
340  int32_t ProcessNACKRequest(const bool enable);
341  int32_t ProcessFECRequest(const bool enable,
342                            const unsigned char payload_typeRED,
343                            const unsigned char payload_typeFEC);
344  // Compute NACK list parameters for the buffering mode.
345  int GetRequiredNackListSize(int target_delay_ms);
346
347  int32_t channel_id_;
348  int32_t engine_id_;
349  uint32_t number_of_cores_;
350  uint8_t num_socket_threads_;
351
352  // Used for all registered callbacks except rendering.
353  scoped_ptr<CriticalSectionWrapper> callback_cs_;
354  scoped_ptr<CriticalSectionWrapper> rtp_rtcp_cs_;
355
356  RtpRtcp* default_rtp_rtcp_;
357
358  // Owned modules/classes.
359  scoped_ptr<RtpRtcp> rtp_rtcp_;
360  std::list<RtpRtcp*> simulcast_rtp_rtcp_;
361  std::list<RtpRtcp*> removed_rtp_rtcp_;
362  VideoCodingModule& vcm_;
363  ViEReceiver vie_receiver_;
364  ViESender vie_sender_;
365  ViESyncModule vie_sync_;
366
367  // Helper to report call statistics.
368  scoped_ptr<ChannelStatsObserver> stats_observer_;
369
370  // Not owned.
371  ProcessThread& module_process_thread_;
372  ViEDecoderObserver* codec_observer_;
373  bool do_key_frame_callbackRequest_;
374  ViERTPObserver* rtp_observer_;
375  ViERTCPObserver* rtcp_observer_;
376  RtcpIntraFrameObserver* intra_frame_observer_;
377  RtcpRttStats* rtt_stats_;
378  PacedSender* paced_sender_;
379
380  scoped_ptr<RtcpBandwidthObserver> bandwidth_observer_;
381  int send_timestamp_extension_id_;
382  int absolute_send_time_extension_id_;
383  bool receive_absolute_send_time_enabled_;
384  bool using_packet_spread_;
385
386  Transport* external_transport_;
387
388  bool decoder_reset_;
389  // Current receive codec used for codec change callback.
390  VideoCodec receive_codec_;
391  bool wait_for_key_frame_;
392  ThreadWrapper* decode_thread_;
393
394  Encryption* external_encryption_;
395
396  ViEEffectFilter* effect_filter_;
397  bool color_enhancement_;
398
399  // User set MTU, -1 if not set.
400  uint16_t mtu_;
401  const bool sender_;
402
403  int nack_history_size_sender_;
404  int max_nack_reordering_threshold_;
405  I420FrameCallback* pre_render_callback_;
406};
407
408}  // namespace webrtc
409
410#endif  // WEBRTC_VIDEO_ENGINE_VIE_CHANNEL_H_
411