cast_transport_sender_impl.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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/memory/ref_counted.h"
29#include "base/memory/scoped_ptr.h"
30#include "base/memory/weak_ptr.h"
31#include "base/time/tick_clock.h"
32#include "base/time/time.h"
33#include "base/timer/timer.h"
34#include "media/cast/common/transport_encryption_handler.h"
35#include "media/cast/logging/logging_defines.h"
36#include "media/cast/logging/simple_event_subscriber.h"
37#include "media/cast/net/cast_transport_config.h"
38#include "media/cast/net/cast_transport_sender.h"
39#include "media/cast/net/pacing/paced_sender.h"
40#include "media/cast/net/rtcp/rtcp.h"
41#include "media/cast/net/rtp/rtp_sender.h"
42
43namespace media {
44namespace cast {
45
46class UdpTransport;
47
48class CastTransportSenderImpl : public CastTransportSender {
49 public:
50  // |external_transport| is only used for testing.
51  // |raw_events_callback|: Raw events will be returned on this callback
52  // which will be invoked every |raw_events_callback_interval|.
53  // This can be a null callback, i.e. if user is not interested in raw events.
54  // |raw_events_callback_interval|: This can be |base::TimeDelta()| if
55  // |raw_events_callback| is a null callback.
56  CastTransportSenderImpl(
57      net::NetLog* net_log,
58      base::TickClock* clock,
59      const net::IPEndPoint& remote_end_point,
60      const CastTransportStatusCallback& status_callback,
61      const BulkRawEventsCallback& raw_events_callback,
62      base::TimeDelta raw_events_callback_interval,
63      const scoped_refptr<base::SingleThreadTaskRunner>& transport_task_runner,
64      PacketSender* external_transport);
65
66  virtual ~CastTransportSenderImpl();
67
68  virtual void InitializeAudio(const CastTransportRtpConfig& config,
69                               const RtcpCastMessageCallback& cast_message_cb,
70                               const RtcpRttCallback& rtt_cb) OVERRIDE;
71  virtual void InitializeVideo(const CastTransportRtpConfig& config,
72                               const RtcpCastMessageCallback& cast_message_cb,
73                               const RtcpRttCallback& rtt_cb) OVERRIDE;
74  virtual void InsertCodedAudioFrame(const EncodedFrame& audio_frame) OVERRIDE;
75  virtual void InsertCodedVideoFrame(const EncodedFrame& video_frame) OVERRIDE;
76
77  virtual void SendSenderReport(
78      uint32 ssrc,
79      base::TimeTicks current_time,
80      uint32 current_time_as_rtp_timestamp) OVERRIDE;
81
82  virtual void ResendPackets(bool is_audio,
83                             const MissingFramesAndPacketsMap& missing_packets,
84                             bool cancel_rtx_if_not_in_list,
85                             base::TimeDelta dedupe_window)
86      OVERRIDE;
87
88  virtual PacketReceiverCallback PacketReceiverForTesting() OVERRIDE;
89
90 private:
91  // If |raw_events_callback_| is non-null, calls it with events collected
92  // by |event_subscriber_| since last call.
93  void SendRawEvents();
94
95  // Called when a packet is received.
96  void OnReceivedPacket(scoped_ptr<Packet> packet);
97
98  // Called when a log message is received.
99  void OnReceivedLogMessage(EventMediaType media_type,
100                            const RtcpReceiverLogMessage& log);
101
102  base::TickClock* clock_;  // Not owned by this class.
103  CastTransportStatusCallback status_callback_;
104  scoped_refptr<base::SingleThreadTaskRunner> transport_task_runner_;
105
106  LoggingImpl logging_;
107
108  // Interface to a UDP socket.
109  scoped_ptr<UdpTransport> transport_;
110
111  // Packet sender that performs pacing.
112  PacedSender pacer_;
113
114  // Packetizer for audio and video frames.
115  scoped_ptr<RtpSender> audio_sender_;
116  scoped_ptr<RtpSender> video_sender_;
117
118  // Maintains RTCP session for audio and video.
119  scoped_ptr<Rtcp> audio_rtcp_session_;
120  scoped_ptr<Rtcp> video_rtcp_session_;
121
122  // Encrypts data in EncodedFrames before they are sent.  Note that it's
123  // important for the encryption to happen here, in code that would execute in
124  // the main browser process, for security reasons.  This helps to mitigate
125  // the damage that could be caused by a compromised renderer process.
126  TransportEncryptionHandler audio_encryptor_;
127  TransportEncryptionHandler video_encryptor_;
128
129  // This is non-null iff |raw_events_callback_| is non-null.
130  scoped_ptr<SimpleEventSubscriber> event_subscriber_;
131
132  BulkRawEventsCallback raw_events_callback_;
133  base::TimeDelta raw_events_callback_interval_;
134
135  base::WeakPtrFactory<CastTransportSenderImpl> weak_factory_;
136
137  DISALLOW_COPY_AND_ASSIGN(CastTransportSenderImpl);
138};
139
140}  // namespace cast
141}  // namespace media
142
143#endif  // MEDIA_CAST_NET_CAST_TRANSPORT_IMPL_H_
144