Home | History | Annotate | Download | only in video_sender
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef MEDIA_CAST_VIDEO_SENDER_VIDEO_SENDER_H_
      6 #define MEDIA_CAST_VIDEO_SENDER_VIDEO_SENDER_H_
      7 
      8 #include "base/callback.h"
      9 #include "base/memory/ref_counted.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/memory/weak_ptr.h"
     12 #include "base/threading/non_thread_safe.h"
     13 #include "base/time/tick_clock.h"
     14 #include "base/time/time.h"
     15 #include "media/cast/cast_config.h"
     16 #include "media/cast/cast_environment.h"
     17 #include "media/cast/congestion_control/congestion_control.h"
     18 #include "media/cast/net/rtp_sender/rtp_sender.h"
     19 #include "media/cast/rtcp/rtcp.h"
     20 
     21 namespace crypto {
     22   class Encryptor;
     23 }
     24 
     25 namespace media {
     26 class VideoFrame;
     27 }
     28 
     29 namespace media {
     30 namespace cast {
     31 
     32 class VideoEncoder;
     33 class LocalRtcpVideoSenderFeedback;
     34 class LocalRtpVideoSenderStatistics;
     35 class LocalVideoEncoderCallback;
     36 class PacedPacketSender;
     37 
     38 // Not thread safe. Only called from the main cast thread.
     39 // This class owns all objects related to sending video, objects that create RTP
     40 // packets, congestion control, video encoder, parsing and sending of
     41 // RTCP packets.
     42 // Additionally it posts a bunch of delayed tasks to the main thread for various
     43 // timeouts.
     44 class VideoSender : public base::NonThreadSafe,
     45                     public base::SupportsWeakPtr<VideoSender> {
     46  public:
     47   VideoSender(scoped_refptr<CastEnvironment> cast_environment,
     48               const VideoSenderConfig& video_config,
     49               VideoEncoderController* const video_encoder_controller,
     50               PacedPacketSender* const paced_packet_sender);
     51 
     52   virtual ~VideoSender();
     53 
     54   // The video_frame must be valid until the closure callback is called.
     55   // The closure callback is called from the video encoder thread as soon as
     56   // the encoder is done with the frame; it does not mean that the encoded frame
     57   // has been sent out.
     58   void InsertRawVideoFrame(
     59       const scoped_refptr<media::VideoFrame>& video_frame,
     60       const base::TimeTicks& capture_time);
     61 
     62   // The video_frame must be valid until the closure callback is called.
     63   // The closure callback is called from the main thread as soon as
     64   // the cast sender is done with the frame; it does not mean that the encoded
     65   // frame has been sent out.
     66   void InsertCodedVideoFrame(const EncodedVideoFrame* video_frame,
     67                              const base::TimeTicks& capture_time,
     68                              const base::Closure callback);
     69 
     70   // Only called from the main cast thread.
     71   void IncomingRtcpPacket(const uint8* packet, size_t length,
     72                           const base::Closure callback);
     73 
     74  protected:
     75   // Protected for testability.
     76   void OnReceivedCastFeedback(const RtcpCastMessage& cast_feedback);
     77 
     78  private:
     79   friend class LocalRtcpVideoSenderFeedback;
     80 
     81   // Schedule when we should send the next RTPC report,
     82   // via a PostDelayedTask to the main cast thread.
     83   void ScheduleNextRtcpReport();
     84   void SendRtcpReport();
     85 
     86   // Schedule when we should check that we have received an acknowledgment, or a
     87   // loss report from our remote peer. If we have not heard back from our remote
     88   // peer we speculatively resend our oldest unacknowledged frame (the whole
     89   // frame). Note for this to happen we need to lose all pending packets (in
     90   // normal operation 3 full frames), hence this is the last resort to prevent
     91   // us getting stuck after a long outage.
     92   void ScheduleNextResendCheck();
     93   void ResendCheck();
     94 
     95   // Monitor how many frames that are silently dropped by the video sender
     96   // per time unit.
     97   void ScheduleNextSkippedFramesCheck();
     98   void SkippedFramesCheck();
     99 
    100   void SendEncodedVideoFrame(const EncodedVideoFrame* video_frame,
    101                              const base::TimeTicks& capture_time);
    102   void ResendFrame(uint32 resend_frame_id);
    103   void ReceivedAck(uint32 acked_frame_id);
    104   void UpdateFramesInFlight();
    105 
    106   void SendEncodedVideoFrameMainThread(
    107       scoped_ptr<EncodedVideoFrame> video_frame,
    108       const base::TimeTicks& capture_time);
    109 
    110   void InitializeTimers();
    111 
    112   // Caller must allocate the destination |encrypted_video_frame| the data
    113   // member will be resized to hold the encrypted size.
    114   bool EncryptVideoFrame(const EncodedVideoFrame& encoded_frame,
    115                          EncodedVideoFrame* encrypted_video_frame);
    116 
    117   const base::TimeDelta rtp_max_delay_;
    118   const int max_frame_rate_;
    119 
    120   scoped_refptr<CastEnvironment> cast_environment_;
    121   scoped_ptr<LocalRtcpVideoSenderFeedback> rtcp_feedback_;
    122   scoped_ptr<LocalRtpVideoSenderStatistics> rtp_video_sender_statistics_;
    123   scoped_ptr<VideoEncoder> video_encoder_;
    124   scoped_ptr<Rtcp> rtcp_;
    125   scoped_ptr<RtpSender> rtp_sender_;
    126   VideoEncoderController* video_encoder_controller_;
    127   uint8 max_unacked_frames_;
    128   scoped_ptr<crypto::Encryptor> encryptor_;
    129   std::string iv_mask_;
    130   int last_acked_frame_id_;
    131   int last_sent_frame_id_;
    132   int duplicate_ack_;
    133   base::TimeTicks last_send_time_;
    134   base::TimeTicks last_checked_skip_count_time_;
    135   int last_skip_count_;
    136   CongestionControl congestion_control_;
    137 
    138   bool initialized_;
    139   base::WeakPtrFactory<VideoSender> weak_factory_;
    140 
    141   DISALLOW_COPY_AND_ASSIGN(VideoSender);
    142 };
    143 
    144 }  // namespace cast
    145 }  // namespace media
    146 
    147 #endif  // MEDIA_CAST_VIDEO_SENDER_VIDEO_SENDER_H_
    148 
    149