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.
5 // This class maintains a send transport for audio and video in a Cast
7 // Audio, video frames and RTCP messages are submitted to this object
8 // and then packetized and paced to the underlying UDP socket.
10 // The hierarchy of send transport in a Cast Streaming session:
12 // CastTransportSender RTP RTCP
13 // ------------------------------------------------------------------
14 // TransportEncryptionHandler (A/V)
15 // RtpSender (A/V) Rtcp (A/V)
16 // PacedSender (Shared)
17 // UdpTransport (Shared)
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
24 #ifndef MEDIA_CAST_NET_CAST_TRANSPORT_SENDER_IMPL_H_
25 #define MEDIA_CAST_NET_CAST_TRANSPORT_SENDER_IMPL_H_
29 #include "base/callback.h"
30 #include "base/gtest_prod_util.h"
31 #include "base/memory/ref_counted.h"
32 #include "base/memory/scoped_ptr.h"
33 #include "base/memory/weak_ptr.h"
34 #include "base/time/tick_clock.h"
35 #include "base/time/time.h"
36 #include "media/cast/common/transport_encryption_handler.h"
37 #include "media/cast/logging/logging_defines.h"
38 #include "media/cast/logging/simple_event_subscriber.h"
39 #include "media/cast/net/cast_transport_config.h"
40 #include "media/cast/net/cast_transport_sender.h"
41 #include "media/cast/net/pacing/paced_sender.h"
42 #include "media/cast/net/rtcp/rtcp.h"
43 #include "media/cast/net/rtp/rtp_parser.h"
44 #include "media/cast/net/rtp/rtp_sender.h"
51 class CastTransportSenderImpl
: public CastTransportSender
{
53 // |external_transport| is only used for testing.
54 // |raw_events_callback|: Raw events will be returned on this callback
55 // which will be invoked every |raw_events_callback_interval|.
56 // This can be a null callback, i.e. if user is not interested in raw events.
57 // |raw_events_callback_interval|: This can be |base::TimeDelta()| if
58 // |raw_events_callback| is a null callback.
59 // |options| contains optional settings for the transport, possible
61 // "DSCP" (value ignored) - turns DSCP on
62 // "non_blocking_io" (value ignored) - Windows only.
63 // Turns on non-blocking IO for socket.
64 // "pacer_target_burst_size": int - specifies how many packets to send
66 // "pacer_max_burst_size": int - specifies how many pakcets to send
68 // "send_buffer_min_size": int - specifies the minimum socket send buffer
70 // "disable_wifi_scan" (value ignored) - disable wifi scans while streaming
71 // "media_streaming_mode" (value ignored) - turn media streaming mode on
72 // Note, these options may be ignored on some platforms.
73 // TODO(hubbe): Too many callbacks, replace with an interface.
74 CastTransportSenderImpl(
76 base::TickClock
* clock
,
77 const net::IPEndPoint
& local_end_point
,
78 const net::IPEndPoint
& remote_end_point
,
79 scoped_ptr
<base::DictionaryValue
> options
,
80 const CastTransportStatusCallback
& status_callback
,
81 const BulkRawEventsCallback
& raw_events_callback
,
82 base::TimeDelta raw_events_callback_interval
,
83 const scoped_refptr
<base::SingleThreadTaskRunner
>& transport_task_runner
,
84 const PacketReceiverCallback
& packet_callback
,
85 PacketSender
* external_transport
);
87 ~CastTransportSenderImpl() override
;
89 // CastTransportSender implementation.
90 void InitializeAudio(const CastTransportRtpConfig
& config
,
91 const RtcpCastMessageCallback
& cast_message_cb
,
92 const RtcpRttCallback
& rtt_cb
) override
;
93 void InitializeVideo(const CastTransportRtpConfig
& config
,
94 const RtcpCastMessageCallback
& cast_message_cb
,
95 const RtcpRttCallback
& rtt_cb
) override
;
96 void InsertFrame(uint32 ssrc
, const EncodedFrame
& frame
) override
;
98 void SendSenderReport(uint32 ssrc
,
99 base::TimeTicks current_time
,
100 uint32 current_time_as_rtp_timestamp
) override
;
102 void CancelSendingFrames(uint32 ssrc
,
103 const std::vector
<uint32
>& frame_ids
) override
;
105 void ResendFrameForKickstart(uint32 ssrc
, uint32 frame_id
) override
;
107 PacketReceiverCallback
PacketReceiverForTesting() override
;
109 // CastTransportReceiver implementation.
110 void AddValidSsrc(uint32 ssrc
) override
;
112 void SendRtcpFromRtpReceiver(
115 const RtcpTimeData
& time_data
,
116 const RtcpCastMessage
* cast_message
,
117 base::TimeDelta target_delay
,
118 const ReceiverRtcpEventSubscriber::RtcpEvents
* rtcp_events
,
119 const RtpReceiverStatistics
* rtp_receiver_statistics
) override
;
122 FRIEND_TEST_ALL_PREFIXES(CastTransportSenderImplTest
, NacksCancelRetransmits
);
123 FRIEND_TEST_ALL_PREFIXES(CastTransportSenderImplTest
, CancelRetransmits
);
124 FRIEND_TEST_ALL_PREFIXES(CastTransportSenderImplTest
, Kickstart
);
125 FRIEND_TEST_ALL_PREFIXES(CastTransportSenderImplTest
,
126 DedupRetransmissionWithAudio
);
128 // Resend packets for the stream identified by |ssrc|.
129 // If |cancel_rtx_if_not_in_list| is true then transmission of packets for the
130 // frames but not in the list will be dropped.
131 // See PacedSender::ResendPackets() to see how |dedup_info| works.
132 void ResendPackets(uint32 ssrc
,
133 const MissingFramesAndPacketsMap
& missing_packets
,
134 bool cancel_rtx_if_not_in_list
,
135 const DedupInfo
& dedup_info
);
137 // If |raw_events_callback_| is non-null, calls it with events collected
138 // by |event_subscriber_| since last call.
139 void SendRawEvents();
141 // Called when a packet is received.
142 bool OnReceivedPacket(scoped_ptr
<Packet
> packet
);
144 // Called when a log message is received.
145 void OnReceivedLogMessage(EventMediaType media_type
,
146 const RtcpReceiverLogMessage
& log
);
148 // Called when a RTCP Cast message is received.
149 void OnReceivedCastMessage(uint32 ssrc
,
150 const RtcpCastMessageCallback
& cast_message_cb
,
151 const RtcpCastMessage
& cast_message
);
153 base::TickClock
* clock_
; // Not owned by this class.
154 CastTransportStatusCallback status_callback_
;
155 scoped_refptr
<base::SingleThreadTaskRunner
> transport_task_runner_
;
157 LoggingImpl logging_
;
159 // Interface to a UDP socket.
160 scoped_ptr
<UdpTransport
> transport_
;
162 // Packet sender that performs pacing.
165 // Packetizer for audio and video frames.
166 scoped_ptr
<RtpSender
> audio_sender_
;
167 scoped_ptr
<RtpSender
> video_sender_
;
169 // Maintains RTCP session for audio and video.
170 scoped_ptr
<Rtcp
> audio_rtcp_session_
;
171 scoped_ptr
<Rtcp
> video_rtcp_session_
;
173 // Encrypts data in EncodedFrames before they are sent. Note that it's
174 // important for the encryption to happen here, in code that would execute in
175 // the main browser process, for security reasons. This helps to mitigate
176 // the damage that could be caused by a compromised renderer process.
177 TransportEncryptionHandler audio_encryptor_
;
178 TransportEncryptionHandler video_encryptor_
;
180 // This is non-null iff |raw_events_callback_| is non-null.
181 scoped_ptr
<SimpleEventSubscriber
> event_subscriber_
;
183 BulkRawEventsCallback raw_events_callback_
;
184 base::TimeDelta raw_events_callback_interval_
;
186 // Right after a frame is sent we record the number of bytes sent to the
187 // socket. We record the corresponding bytes sent for the most recent ACKed
189 int64 last_byte_acked_for_audio_
;
191 // Packets that don't match these ssrcs are ignored.
192 std::set
<uint32
> valid_ssrcs_
;
194 // Called with incoming packets. (Unless they match the
195 // channels created by Initialize{Audio,Video}.
196 PacketReceiverCallback packet_callback_
;
198 scoped_ptr
<net::ScopedWifiOptions
> wifi_options_autoreset_
;
200 base::WeakPtrFactory
<CastTransportSenderImpl
> weak_factory_
;
202 DISALLOW_COPY_AND_ASSIGN(CastTransportSenderImpl
);
208 #endif // MEDIA_CAST_NET_CAST_TRANSPORT_SENDER_IMPL_H_