summaryrefslogtreecommitdiffstats
path: root/media/cast/net/cast_transport_sender_impl.h
blob: 5d34fdeb0172a5e88b9636fe0a2d19f38ee12408 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_CAST_NET_CAST_TRANSPORT_IMPL_H_
#define MEDIA_CAST_NET_CAST_TRANSPORT_IMPL_H_

#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/time/tick_clock.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "media/cast/common/transport_encryption_handler.h"
#include "media/cast/logging/logging_defines.h"
#include "media/cast/logging/simple_event_subscriber.h"
#include "media/cast/net/cast_transport_config.h"
#include "media/cast/net/cast_transport_sender.h"
#include "media/cast/net/pacing/paced_sender.h"
#include "media/cast/net/rtcp/rtcp_builder.h"
#include "media/cast/net/rtp/rtp_sender.h"

namespace media {
namespace cast {

class UdpTransport;

class CastTransportSenderImpl : public CastTransportSender {
 public:
  // external_transport is only used for testing.
  // Note that SetPacketReceiver does not work if an external
  // transport is provided.
  // |raw_events_callback|: Raw events will be returned on this callback
  // which will be invoked every |raw_events_callback_interval|.
  // This can be a null callback, i.e. if user is not interested in raw events.
  // |raw_events_callback_interval|: This can be |base::TimeDelta()| if
  // |raw_events_callback| is a null callback.
  CastTransportSenderImpl(
      net::NetLog* net_log,
      base::TickClock* clock,
      const net::IPEndPoint& remote_end_point,
      const CastTransportStatusCallback& status_callback,
      const BulkRawEventsCallback& raw_events_callback,
      base::TimeDelta raw_events_callback_interval,
      const scoped_refptr<base::SingleThreadTaskRunner>& transport_task_runner,
      PacketSender* external_transport);

  virtual ~CastTransportSenderImpl();

  virtual void InitializeAudio(const CastTransportRtpConfig& config) OVERRIDE;
  virtual void InitializeVideo(const CastTransportRtpConfig& config) OVERRIDE;

  // CastTransportSender implementation.
  virtual void SetPacketReceiver(const PacketReceiverCallback& packet_receiver)
      OVERRIDE;

  virtual void InsertCodedAudioFrame(const EncodedFrame& audio_frame) OVERRIDE;
  virtual void InsertCodedVideoFrame(const EncodedFrame& video_frame) OVERRIDE;

  virtual void SendRtcpFromRtpSender(uint32 packet_type_flags,
                                     uint32 ntp_seconds,
                                     uint32 ntp_fraction,
                                     uint32 rtp_timestamp,
                                     const RtcpDlrrReportBlock& dlrr,
                                     uint32 sending_ssrc,
                                     const std::string& c_name) OVERRIDE;

  virtual void ResendPackets(bool is_audio,
                             const MissingFramesAndPacketsMap& missing_packets,
                             bool cancel_rtx_if_not_in_list,
                             base::TimeDelta dedupe_window)
      OVERRIDE;

 private:
  // If |raw_events_callback_| is non-null, calls it with events collected
  // by |event_subscriber_| since last call.
  void SendRawEvents();

  base::TickClock* clock_;  // Not owned by this class.
  CastTransportStatusCallback status_callback_;
  scoped_refptr<base::SingleThreadTaskRunner> transport_task_runner_;

  scoped_ptr<UdpTransport> transport_;
  LoggingImpl logging_;
  PacedSender pacer_;
  RtcpBuilder rtcp_builder_;
  scoped_ptr<RtpSender> audio_sender_;
  scoped_ptr<RtpSender> video_sender_;

  // Encrypts data in EncodedFrames before they are sent.  Note that it's
  // important for the encryption to happen here, in code that would execute in
  // the main browser process, for security reasons.  This helps to mitigate
  // the damage that could be caused by a compromised renderer process.
  TransportEncryptionHandler audio_encryptor_;
  TransportEncryptionHandler video_encryptor_;

  // This is non-null iff |raw_events_callback_| is non-null.
  scoped_ptr<SimpleEventSubscriber> event_subscriber_;
  base::RepeatingTimer<CastTransportSenderImpl> raw_events_timer_;

  BulkRawEventsCallback raw_events_callback_;

  DISALLOW_COPY_AND_ASSIGN(CastTransportSenderImpl);
};

}  // namespace cast
}  // namespace media

#endif  // MEDIA_CAST_NET_CAST_TRANSPORT_IMPL_H_