• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 // This class maintains a send transport for audio and video in a Cast
6 // Streaming session.
7 // Audio, video frames and RTCP messages are submitted to this object
8 // and then packetized and paced to the underlying UDP socket.
9 //
10 // The hierarchy of send transport in a Cast Streaming session:
11 //
12 // CastTransportSender              RTP                      RTCP
13 // ------------------------------------------------------------------
14 //                      TransportEncryptionHandler (A/V)
15 //                      RtpSender (A/V)                   Rtcp (A/V)
16 //                                      PacedSender (Shared)
17 //                                      UdpTransport (Shared)
18 //
19 // There are objects of TransportEncryptionHandler, RtpSender and Rtcp
20 // for each audio and video stream.
21 // PacedSender and UdpTransport are shared between all RTP and RTCP
22 // streams.
23 
24 #ifndef MEDIA_CAST_NET_CAST_TRANSPORT_IMPL_H_
25 #define MEDIA_CAST_NET_CAST_TRANSPORT_IMPL_H_
26 
27 #include "base/callback.h"
28 #include "base/gtest_prod_util.h"
29 #include "base/memory/ref_counted.h"
30 #include "base/memory/scoped_ptr.h"
31 #include "base/memory/weak_ptr.h"
32 #include "base/time/tick_clock.h"
33 #include "base/time/time.h"
34 #include "base/timer/timer.h"
35 #include "media/cast/common/transport_encryption_handler.h"
36 #include "media/cast/logging/logging_defines.h"
37 #include "media/cast/logging/simple_event_subscriber.h"
38 #include "media/cast/net/cast_transport_config.h"
39 #include "media/cast/net/cast_transport_sender.h"
40 #include "media/cast/net/pacing/paced_sender.h"
41 #include "media/cast/net/rtcp/rtcp.h"
42 #include "media/cast/net/rtp/rtp_sender.h"
43 
44 namespace media {
45 namespace cast {
46 
47 class UdpTransport;
48 
49 class CastTransportSenderImpl : public CastTransportSender {
50  public:
51   // |external_transport| is only used for testing.
52   // |raw_events_callback|: Raw events will be returned on this callback
53   // which will be invoked every |raw_events_callback_interval|.
54   // This can be a null callback, i.e. if user is not interested in raw events.
55   // |raw_events_callback_interval|: This can be |base::TimeDelta()| if
56   // |raw_events_callback| is a null callback.
57   // |options| contains optional settings for the transport, possible
58   // keys are:
59   //   "DSCP" (value ignored) - turns DSCP on
60   //   "pacer_target_burst_size": int - specifies how many packets to send
61   //                                    per 10 ms ideally.
62   //   "pacer_max_burst_size": int - specifies how many pakcets to send
63   //                                 per 10 ms, max
64   //   "disable_wifi_scan" (value ignored) - disable wifi scans while streaming
65   //   "media_streaming_mode" (value ignored) - turn media streaming mode on
66   // Note, these options may be ignored on some platforms.
67   CastTransportSenderImpl(
68       net::NetLog* net_log,
69       base::TickClock* clock,
70       const net::IPEndPoint& remote_end_point,
71       scoped_ptr<base::DictionaryValue> options,
72       const CastTransportStatusCallback& status_callback,
73       const BulkRawEventsCallback& raw_events_callback,
74       base::TimeDelta raw_events_callback_interval,
75       const scoped_refptr<base::SingleThreadTaskRunner>& transport_task_runner,
76       PacketSender* external_transport);
77 
78   virtual ~CastTransportSenderImpl();
79 
80   virtual void InitializeAudio(const CastTransportRtpConfig& config,
81                                const RtcpCastMessageCallback& cast_message_cb,
82                                const RtcpRttCallback& rtt_cb) OVERRIDE;
83   virtual void InitializeVideo(const CastTransportRtpConfig& config,
84                                const RtcpCastMessageCallback& cast_message_cb,
85                                const RtcpRttCallback& rtt_cb) OVERRIDE;
86   virtual void InsertFrame(uint32 ssrc, const EncodedFrame& frame) OVERRIDE;
87 
88   virtual void SendSenderReport(
89       uint32 ssrc,
90       base::TimeTicks current_time,
91       uint32 current_time_as_rtp_timestamp) OVERRIDE;
92 
93   virtual void CancelSendingFrames(
94       uint32 ssrc,
95       const std::vector<uint32>& frame_ids) OVERRIDE;
96 
97   virtual void ResendFrameForKickstart(uint32 ssrc, uint32 frame_id) OVERRIDE;
98 
99   virtual PacketReceiverCallback PacketReceiverForTesting() OVERRIDE;
100 
101  private:
102   FRIEND_TEST_ALL_PREFIXES(CastTransportSenderImplTest, NacksCancelRetransmits);
103   FRIEND_TEST_ALL_PREFIXES(CastTransportSenderImplTest, CancelRetransmits);
104   FRIEND_TEST_ALL_PREFIXES(CastTransportSenderImplTest, Kickstart);
105   FRIEND_TEST_ALL_PREFIXES(CastTransportSenderImplTest,
106                            DedupRetransmissionWithAudio);
107 
108   // Resend packets for the stream identified by |ssrc|.
109   // If |cancel_rtx_if_not_in_list| is true then transmission of packets for the
110   // frames but not in the list will be dropped.
111   // See PacedSender::ResendPackets() to see how |dedup_info| works.
112   void ResendPackets(uint32 ssrc,
113                      const MissingFramesAndPacketsMap& missing_packets,
114                      bool cancel_rtx_if_not_in_list,
115                      const DedupInfo& dedup_info);
116 
117   // If |raw_events_callback_| is non-null, calls it with events collected
118   // by |event_subscriber_| since last call.
119   void SendRawEvents();
120 
121   // Called when a packet is received.
122   void OnReceivedPacket(scoped_ptr<Packet> packet);
123 
124   // Called when a log message is received.
125   void OnReceivedLogMessage(EventMediaType media_type,
126                             const RtcpReceiverLogMessage& log);
127 
128   // Called when a RTCP Cast message is received.
129   void OnReceivedCastMessage(uint32 ssrc,
130                              const RtcpCastMessageCallback& cast_message_cb,
131                              const RtcpCastMessage& cast_message);
132 
133   base::TickClock* clock_;  // Not owned by this class.
134   CastTransportStatusCallback status_callback_;
135   scoped_refptr<base::SingleThreadTaskRunner> transport_task_runner_;
136 
137   LoggingImpl logging_;
138 
139   // Interface to a UDP socket.
140   scoped_ptr<UdpTransport> transport_;
141 
142   // Packet sender that performs pacing.
143   PacedSender pacer_;
144 
145   // Packetizer for audio and video frames.
146   scoped_ptr<RtpSender> audio_sender_;
147   scoped_ptr<RtpSender> video_sender_;
148 
149   // Maintains RTCP session for audio and video.
150   scoped_ptr<Rtcp> audio_rtcp_session_;
151   scoped_ptr<Rtcp> video_rtcp_session_;
152 
153   // Encrypts data in EncodedFrames before they are sent.  Note that it's
154   // important for the encryption to happen here, in code that would execute in
155   // the main browser process, for security reasons.  This helps to mitigate
156   // the damage that could be caused by a compromised renderer process.
157   TransportEncryptionHandler audio_encryptor_;
158   TransportEncryptionHandler video_encryptor_;
159 
160   // This is non-null iff |raw_events_callback_| is non-null.
161   scoped_ptr<SimpleEventSubscriber> event_subscriber_;
162 
163   BulkRawEventsCallback raw_events_callback_;
164   base::TimeDelta raw_events_callback_interval_;
165 
166   // Right after a frame is sent we record the number of bytes sent to the
167   // socket. We record the corresponding bytes sent for the most recent ACKed
168   // audio packet.
169   int64 last_byte_acked_for_audio_;
170 
171   scoped_ptr<net::ScopedWifiOptions> wifi_options_autoreset_;
172 
173   base::WeakPtrFactory<CastTransportSenderImpl> weak_factory_;
174 
175   DISALLOW_COPY_AND_ASSIGN(CastTransportSenderImpl);
176 };
177 
178 }  // namespace cast
179 }  // namespace media
180 
181 #endif  // MEDIA_CAST_NET_CAST_TRANSPORT_IMPL_H_
182