vie_channel.h revision 0fcaf99b716c71e721db25f37f62dabf5dcbe69a
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#include <map>
16#include <vector>
17
18#include "webrtc/base/scoped_ptr.h"
19#include "webrtc/base/scoped_ref_ptr.h"
20#include "webrtc/modules/remote_bitrate_estimator/include/remote_bitrate_estimator.h"
21#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp.h"
22#include "webrtc/modules/rtp_rtcp/include/rtp_rtcp_defines.h"
23#include "webrtc/modules/video_coding/include/video_coding_defines.h"
24#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
25#include "webrtc/system_wrappers/include/tick_util.h"
26#include "webrtc/typedefs.h"
27#include "webrtc/video_engine/vie_receiver.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 I420FrameCallback;
38class IncomingVideoStream;
39class PacedSender;
40class PacketRouter;
41class PayloadRouter;
42class ProcessThread;
43class ReceiveStatisticsProxy;
44class ReportBlockStats;
45class RtcpRttStats;
46class PlatformThread;
47class ViEChannelProtectionCallback;
48class ViERTPObserver;
49class VideoCodingModule;
50class VideoDecoder;
51class VideoRenderCallback;
52class VoEVideoSync;
53
54enum StreamType {
55  kViEStreamTypeNormal = 0,  // Normal media stream
56  kViEStreamTypeRtx = 1      // Retransmission media stream
57};
58
59class ViEChannel : public VCMFrameTypeCallback,
60                   public VCMReceiveCallback,
61                   public VCMReceiveStatisticsCallback,
62                   public VCMDecoderTimingCallback,
63                   public VCMPacketRequestCallback,
64                   public RtpFeedback {
65 public:
66  friend class ChannelStatsObserver;
67  friend class ViEChannelProtectionCallback;
68
69  ViEChannel(uint32_t number_of_cores,
70             Transport* transport,
71             ProcessThread* module_process_thread,
72             RtcpIntraFrameObserver* intra_frame_observer,
73             RtcpBandwidthObserver* bandwidth_observer,
74             TransportFeedbackObserver* transport_feedback_observer,
75             RemoteBitrateEstimator* remote_bitrate_estimator,
76             RtcpRttStats* rtt_stats,
77             PacedSender* paced_sender,
78             PacketRouter* packet_router,
79             size_t max_rtp_streams,
80             bool sender);
81  ~ViEChannel();
82
83  int32_t Init();
84
85  // Sets the encoder to use for the channel. |new_stream| indicates the encoder
86  // type has changed and we should start a new RTP stream.
87  int32_t SetSendCodec(const VideoCodec& video_codec, bool new_stream = true);
88  int32_t SetReceiveCodec(const VideoCodec& video_codec);
89  // Registers an external decoder. |buffered_rendering| means that the decoder
90  // will render frames after decoding according to the render timestamp
91  // provided by the video coding module. |render_delay| indicates the time
92  // needed to decode and render a frame.
93  int32_t RegisterExternalDecoder(const uint8_t pl_type,
94                                  VideoDecoder* decoder,
95                                  bool buffered_rendering,
96                                  int32_t render_delay);
97  int32_t DeRegisterExternalDecoder(const uint8_t pl_type);
98  int32_t ReceiveCodecStatistics(uint32_t* num_key_frames,
99                                 uint32_t* num_delta_frames);
100  uint32_t DiscardedPackets() const;
101
102  // Returns the estimated delay in milliseconds.
103  int ReceiveDelay() const;
104
105  void SetRTCPMode(const RtcpMode rtcp_mode);
106  void SetProtectionMode(bool enable_nack,
107                         bool enable_fec,
108                         int payload_type_red,
109                         int payload_type_fec);
110  bool IsSendingFecEnabled();
111  int SetSenderBufferingMode(int target_delay_ms);
112  int SetSendTimestampOffsetStatus(bool enable, int id);
113  int SetReceiveTimestampOffsetStatus(bool enable, int id);
114  int SetSendAbsoluteSendTimeStatus(bool enable, int id);
115  int SetReceiveAbsoluteSendTimeStatus(bool enable, int id);
116  int SetSendVideoRotationStatus(bool enable, int id);
117  int SetReceiveVideoRotationStatus(bool enable, int id);
118  int SetSendTransportSequenceNumber(bool enable, int id);
119  int SetReceiveTransportSequenceNumber(bool enable, int id);
120  void SetRtcpXrRrtrStatus(bool enable);
121  void EnableTMMBR(bool enable);
122
123  // Sets SSRC for outgoing stream.
124  int32_t SetSSRC(const uint32_t SSRC,
125                  const StreamType usage,
126                  const unsigned char simulcast_idx);
127
128  // Gets SSRC for outgoing stream number |idx|.
129  int32_t GetLocalSSRC(uint8_t idx, unsigned int* ssrc);
130
131  // Gets SSRC for the incoming stream.
132  uint32_t GetRemoteSSRC();
133
134  int SetRtxSendPayloadType(int payload_type, int associated_payload_type);
135  void SetRtxReceivePayloadType(int payload_type, int associated_payload_type);
136  // If set to true, the RTX payload type mapping supplied in
137  // |SetRtxReceivePayloadType| will be used when restoring RTX packets. Without
138  // it, RTX packets will always be restored to the last non-RTX packet payload
139  // type received.
140  void SetUseRtxPayloadMappingOnRestore(bool val);
141
142  void SetRtpStateForSsrc(uint32_t ssrc, const RtpState& rtp_state);
143  RtpState GetRtpStateForSsrc(uint32_t ssrc);
144
145  // Sets the CName for the outgoing stream on the channel.
146  int32_t SetRTCPCName(const char* rtcp_cname);
147
148  // Gets the CName of the incoming stream.
149  int32_t GetRemoteRTCPCName(char rtcp_cname[]);
150
151  // Returns statistics reported by the remote client in an RTCP packet.
152  // TODO(pbos): Remove this along with VideoSendStream::GetRtt().
153  int32_t GetSendRtcpStatistics(uint16_t* fraction_lost,
154                                uint32_t* cumulative_lost,
155                                uint32_t* extended_max,
156                                uint32_t* jitter_samples,
157                                int64_t* rtt_ms);
158
159  // Called on receipt of RTCP report block from remote side.
160  void RegisterSendChannelRtcpStatisticsCallback(
161      RtcpStatisticsCallback* callback);
162
163  // Called on generation of RTCP stats
164  void RegisterReceiveChannelRtcpStatisticsCallback(
165      RtcpStatisticsCallback* callback);
166
167  // Gets send statistics for the rtp and rtx stream.
168  void GetSendStreamDataCounters(StreamDataCounters* rtp_counters,
169                                 StreamDataCounters* rtx_counters) const;
170
171  // Gets received stream data counters.
172  void GetReceiveStreamDataCounters(StreamDataCounters* rtp_counters,
173                                    StreamDataCounters* rtx_counters) const;
174
175  // Called on update of RTP statistics.
176  void RegisterSendChannelRtpStatisticsCallback(
177      StreamDataCountersCallback* callback);
178
179  // Called on update of RTP statistics.
180  void RegisterReceiveChannelRtpStatisticsCallback(
181      StreamDataCountersCallback* callback);
182
183  void GetSendRtcpPacketTypeCounter(
184      RtcpPacketTypeCounter* packet_counter) const;
185
186  void GetReceiveRtcpPacketTypeCounter(
187      RtcpPacketTypeCounter* packet_counter) const;
188
189  void RegisterSendSideDelayObserver(SendSideDelayObserver* observer);
190
191  // Called on any new send bitrate estimate.
192  void RegisterSendBitrateObserver(BitrateStatisticsObserver* observer);
193
194  // Implements RtpFeedback.
195  int32_t OnInitializeDecoder(const int8_t payload_type,
196                              const char payload_name[RTP_PAYLOAD_NAME_SIZE],
197                              const int frequency,
198                              const uint8_t channels,
199                              const uint32_t rate) override;
200  void OnIncomingSSRCChanged(const uint32_t ssrc) override;
201  void OnIncomingCSRCChanged(const uint32_t CSRC, const bool added) override;
202
203  int32_t SetRemoteSSRCType(const StreamType usage, const uint32_t SSRC);
204
205  int32_t StartSend();
206  int32_t StopSend();
207  bool Sending();
208  void StartReceive();
209  void StopReceive();
210
211  int32_t ReceivedRTPPacket(const void* rtp_packet,
212                            const size_t rtp_packet_length,
213                            const PacketTime& packet_time);
214  int32_t ReceivedRTCPPacket(const void* rtcp_packet,
215                             const size_t rtcp_packet_length);
216
217  // Sets the maximum transfer unit size for the network link, i.e. including
218  // IP, UDP and RTP headers.
219  int32_t SetMTU(uint16_t mtu);
220
221  // Gets the modules used by the channel.
222  RtpRtcp* rtp_rtcp();
223  rtc::scoped_refptr<PayloadRouter> send_payload_router();
224  VCMProtectionCallback* vcm_protection_callback();
225
226
227  CallStatsObserver* GetStatsObserver();
228
229  // Implements VCMReceiveCallback.
230  virtual int32_t FrameToRender(VideoFrame& video_frame);  // NOLINT
231
232  // Implements VCMReceiveCallback.
233  virtual int32_t ReceivedDecodedReferenceFrame(
234      const uint64_t picture_id);
235
236  // Implements VCMReceiveCallback.
237  void OnIncomingPayloadType(int payload_type) override;
238
239  // Implements VCMReceiveStatisticsCallback.
240  void OnReceiveRatesUpdated(uint32_t bit_rate, uint32_t frame_rate) override;
241  void OnDiscardedPacketsUpdated(int discarded_packets) override;
242  void OnFrameCountsUpdated(const FrameCounts& frame_counts) override;
243
244  // Implements VCMDecoderTimingCallback.
245  virtual void OnDecoderTiming(int decode_ms,
246                               int max_decode_ms,
247                               int current_delay_ms,
248                               int target_delay_ms,
249                               int jitter_buffer_ms,
250                               int min_playout_delay_ms,
251                               int render_delay_ms);
252
253  // Implements FrameTypeCallback.
254  virtual int32_t RequestKeyFrame();
255
256  // Implements FrameTypeCallback.
257  virtual int32_t SliceLossIndicationRequest(
258      const uint64_t picture_id);
259
260  // Implements VideoPacketRequestCallback.
261  int32_t ResendPackets(const uint16_t* sequence_numbers,
262                        uint16_t length) override;
263
264  int32_t SetVoiceChannel(int32_t ve_channel_id,
265                          VoEVideoSync* ve_sync_interface);
266  int32_t VoiceChannel();
267
268  // New-style callbacks, used by VideoReceiveStream.
269  void RegisterPreRenderCallback(I420FrameCallback* pre_render_callback);
270  void RegisterPreDecodeImageCallback(
271      EncodedImageCallback* pre_decode_callback);
272
273  void RegisterSendFrameCountObserver(FrameCountObserver* observer);
274  void RegisterRtcpPacketTypeCounterObserver(
275      RtcpPacketTypeCounterObserver* observer);
276  void RegisterReceiveStatisticsProxy(
277      ReceiveStatisticsProxy* receive_statistics_proxy);
278  void SetIncomingVideoStream(IncomingVideoStream* incoming_video_stream);
279
280 protected:
281  static bool ChannelDecodeThreadFunction(void* obj);
282  bool ChannelDecodeProcess();
283
284  void OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms);
285
286  int ProtectionRequest(const FecProtectionParams* delta_fec_params,
287                        const FecProtectionParams* key_fec_params,
288                        uint32_t* sent_video_rate_bps,
289                        uint32_t* sent_nack_rate_bps,
290                        uint32_t* sent_fec_rate_bps);
291
292 private:
293  static std::vector<RtpRtcp*> CreateRtpRtcpModules(
294      bool receiver_only,
295      ReceiveStatistics* receive_statistics,
296      Transport* outgoing_transport,
297      RtcpIntraFrameObserver* intra_frame_callback,
298      RtcpBandwidthObserver* bandwidth_callback,
299      TransportFeedbackObserver* transport_feedback_callback,
300      RtcpRttStats* rtt_stats,
301      RtcpPacketTypeCounterObserver* rtcp_packet_type_counter_observer,
302      RemoteBitrateEstimator* remote_bitrate_estimator,
303      RtpPacketSender* paced_sender,
304      TransportSequenceNumberAllocator* transport_sequence_number_allocator,
305      BitrateStatisticsObserver* send_bitrate_observer,
306      FrameCountObserver* send_frame_count_observer,
307      SendSideDelayObserver* send_side_delay_observer,
308      size_t num_modules);
309
310  // Assumed to be protected.
311  void StartDecodeThread();
312  void StopDecodeThread();
313
314  void ProcessNACKRequest(const bool enable);
315  // Compute NACK list parameters for the buffering mode.
316  int GetRequiredNackListSize(int target_delay_ms);
317  void SetRtxSendStatus(bool enable);
318
319  void UpdateHistograms();
320
321  // ViEChannel exposes methods that allow to modify observers and callbacks
322  // to be modified. Such an API-style is cumbersome to implement and maintain
323  // at all the levels when comparing to only setting them at construction. As
324  // so this class instantiates its children with a wrapper that can be modified
325  // at a later time.
326  template <class T>
327  class RegisterableCallback : public T {
328   public:
329    RegisterableCallback()
330        : critsect_(CriticalSectionWrapper::CreateCriticalSection()),
331          callback_(NULL) {}
332
333    void Set(T* callback) {
334      CriticalSectionScoped cs(critsect_.get());
335      callback_ = callback;
336    }
337
338   protected:
339    // Note: this should be implemented with a RW-lock to allow simultaneous
340    // calls into the callback. However that doesn't seem to be needed for the
341    // current type of callbacks covered by this class.
342    rtc::scoped_ptr<CriticalSectionWrapper> critsect_;
343    T* callback_ GUARDED_BY(critsect_);
344
345   private:
346    RTC_DISALLOW_COPY_AND_ASSIGN(RegisterableCallback);
347  };
348
349  class RegisterableBitrateStatisticsObserver:
350    public RegisterableCallback<BitrateStatisticsObserver> {
351    virtual void Notify(const BitrateStatistics& total_stats,
352                        const BitrateStatistics& retransmit_stats,
353                        uint32_t ssrc) {
354      CriticalSectionScoped cs(critsect_.get());
355      if (callback_)
356        callback_->Notify(total_stats, retransmit_stats, ssrc);
357    }
358  } send_bitrate_observer_;
359
360  class RegisterableFrameCountObserver
361      : public RegisterableCallback<FrameCountObserver> {
362   public:
363    virtual void FrameCountUpdated(const FrameCounts& frame_counts,
364                                   uint32_t ssrc) {
365      CriticalSectionScoped cs(critsect_.get());
366      if (callback_)
367        callback_->FrameCountUpdated(frame_counts, ssrc);
368    }
369
370   private:
371  } send_frame_count_observer_;
372
373  class RegisterableSendSideDelayObserver :
374      public RegisterableCallback<SendSideDelayObserver> {
375    void SendSideDelayUpdated(int avg_delay_ms,
376                              int max_delay_ms,
377                              uint32_t ssrc) override {
378      CriticalSectionScoped cs(critsect_.get());
379      if (callback_)
380        callback_->SendSideDelayUpdated(avg_delay_ms, max_delay_ms, ssrc);
381    }
382  } send_side_delay_observer_;
383
384  class RegisterableRtcpPacketTypeCounterObserver
385      : public RegisterableCallback<RtcpPacketTypeCounterObserver> {
386   public:
387    void RtcpPacketTypesCounterUpdated(
388        uint32_t ssrc,
389        const RtcpPacketTypeCounter& packet_counter) override {
390      CriticalSectionScoped cs(critsect_.get());
391      if (callback_)
392        callback_->RtcpPacketTypesCounterUpdated(ssrc, packet_counter);
393      counter_map_[ssrc] = packet_counter;
394    }
395
396    virtual std::map<uint32_t, RtcpPacketTypeCounter> GetPacketTypeCounterMap()
397        const {
398      CriticalSectionScoped cs(critsect_.get());
399      return counter_map_;
400    }
401
402   private:
403    std::map<uint32_t, RtcpPacketTypeCounter> counter_map_
404        GUARDED_BY(critsect_);
405  } rtcp_packet_type_counter_observer_;
406
407  const uint32_t number_of_cores_;
408  const bool sender_;
409
410  ProcessThread* const module_process_thread_;
411
412  // Used for all registered callbacks except rendering.
413  rtc::scoped_ptr<CriticalSectionWrapper> crit_;
414
415  // Owned modules/classes.
416  rtc::scoped_refptr<PayloadRouter> send_payload_router_;
417  rtc::scoped_ptr<ViEChannelProtectionCallback> vcm_protection_callback_;
418
419  VideoCodingModule* const vcm_;
420  ViEReceiver vie_receiver_;
421  ViESyncModule vie_sync_;
422
423  // Helper to report call statistics.
424  rtc::scoped_ptr<ChannelStatsObserver> stats_observer_;
425
426  // Not owned.
427  ReceiveStatisticsProxy* receive_stats_callback_ GUARDED_BY(crit_);
428  FrameCounts receive_frame_counts_ GUARDED_BY(crit_);
429  IncomingVideoStream* incoming_video_stream_ GUARDED_BY(crit_);
430  RtcpIntraFrameObserver* const intra_frame_observer_;
431  RtcpRttStats* const rtt_stats_;
432  PacedSender* const paced_sender_;
433  PacketRouter* const packet_router_;
434
435  const rtc::scoped_ptr<RtcpBandwidthObserver> bandwidth_observer_;
436  TransportFeedbackObserver* const transport_feedback_observer_;
437
438  rtc::scoped_ptr<PlatformThread> decode_thread_;
439
440  int nack_history_size_sender_;
441  int max_nack_reordering_threshold_;
442  I420FrameCallback* pre_render_callback_ GUARDED_BY(crit_);
443
444  const rtc::scoped_ptr<ReportBlockStats> report_block_stats_sender_;
445
446  int64_t time_of_first_rtt_ms_ GUARDED_BY(crit_);
447  int64_t rtt_sum_ms_ GUARDED_BY(crit_);
448  int64_t last_rtt_ms_ GUARDED_BY(crit_);
449  size_t num_rtts_ GUARDED_BY(crit_);
450
451  // RtpRtcp modules, declared last as they use other members on construction.
452  const std::vector<RtpRtcp*> rtp_rtcp_modules_;
453  size_t num_active_rtp_rtcp_modules_ GUARDED_BY(crit_);
454};
455
456}  // namespace webrtc
457
458#endif  // WEBRTC_VIDEO_ENGINE_VIE_CHANNEL_H_
459