summaryrefslogtreecommitdiffstats
path: root/media/cast/net/cast_transport_sender.h
blob: 3272f741de4a691ee6562d962dcc638008f91ec3 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// 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.

// This is the main interface for the cast transport sender.  It accepts encoded
// frames (both audio and video), encrypts their encoded data, packetizes them
// and feeds them into a transport (e.g., UDP).

// Construction of the Cast Sender and the Cast Transport Sender should be done
// in the following order:
// 1. Create CastTransportSender.
// 2. Create CastSender (accepts CastTransportSender as an input).

// Destruction: The CastTransportSender is assumed to be valid as long as the
// CastSender is alive. Therefore the CastSender should be destructed before the
// CastTransportSender.

#ifndef MEDIA_CAST_NET_CAST_TRANSPORT_SENDER_H_
#define MEDIA_CAST_NET_CAST_TRANSPORT_SENDER_H_

#include <stdint.h>

#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/non_thread_safe.h"
#include "base/time/tick_clock.h"
#include "base/values.h"
#include "media/cast/logging/logging_defines.h"
#include "media/cast/net/cast_transport_config.h"
#include "media/cast/net/cast_transport_defines.h"
#include "media/cast/net/rtcp/receiver_rtcp_event_subscriber.h"
#include "media/cast/net/rtcp/rtcp_defines.h"
#include "net/base/ip_endpoint.h"

namespace base {
class DictionaryValue;
}  // namespace base

namespace net {
class NetLog;
}  // namespace net

namespace media {
namespace cast {

struct RtpReceiverStatistics;
struct RtcpTimeData;

// Following the initialization of either audio or video an initialization
// status will be sent via this callback.
typedef base::Callback<void(CastTransportStatus status)>
    CastTransportStatusCallback;

typedef base::Callback<void(scoped_ptr<std::vector<FrameEvent>>,
                            scoped_ptr<std::vector<PacketEvent>>)>
    BulkRawEventsCallback;

// TODO(xjz): Rename CastTransportSender as it also deals with receiving
// packets. http://crbug.com/589157.
// The application should only trigger this class from the transport thread.
class CastTransportSender : public base::NonThreadSafe {
 public:
  // Interface used for receiving status updates, raw events, and RTP packets
  // from CastTransportSender.
  class Client {
   public:
    virtual ~Client(){};

    // Audio and Video transport status change is reported on this callback.
    virtual void OnStatusChanged(CastTransportStatus status) = 0;

    // Raw events will be invoked on this callback periodically, according to
    // the configured logging flush interval passed to
    // CastTransportSender::Create().
    virtual void OnLoggingEventsReceived(
        scoped_ptr<std::vector<FrameEvent>> frame_events,
        scoped_ptr<std::vector<PacketEvent>> packet_events) = 0;

    // Called to pass RTP packets to the Client.
    virtual void ProcessRtpPacket(scoped_ptr<Packet> packet) = 0;
  };

  static scoped_ptr<CastTransportSender> Create(
      base::TickClock* clock,  // Owned by the caller.
      base::TimeDelta logging_flush_interval,
      scoped_ptr<Client> client,
      scoped_ptr<PacketSender> transport,
      const scoped_refptr<base::SingleThreadTaskRunner>& transport_task_runner);

  virtual ~CastTransportSender() {}

  // Audio/Video initialization.
  // Encoded frames cannot be transmitted until the relevant initialize method
  // is called.
  virtual void InitializeAudio(const CastTransportRtpConfig& config,
                               const RtcpCastMessageCallback& cast_message_cb,
                               const RtcpRttCallback& rtt_cb) = 0;
  virtual void InitializeVideo(const CastTransportRtpConfig& config,
                               const RtcpCastMessageCallback& cast_message_cb,
                               const RtcpRttCallback& rtt_cb) = 0;

  // Encrypt, packetize and transmit |frame|. |ssrc| must refer to a
  // a channel already established with InitializeAudio / InitializeVideo.
  virtual void InsertFrame(uint32_t ssrc, const EncodedFrame& frame) = 0;

  // Sends a RTCP sender report to the receiver.
  // |ssrc| is the SSRC for this report.
  // |current_time| is the current time reported by a tick clock.
  // |current_time_as_rtp_timestamp| is the corresponding RTP timestamp.
  virtual void SendSenderReport(uint32_t ssrc,
                                base::TimeTicks current_time,
                                RtpTimeTicks current_time_as_rtp_timestamp) = 0;

  // Cancels sending packets for the frames in the set.
  // |ssrc| is the SSRC for the stream.
  // |frame_ids| contains the IDs of the frames that will be cancelled.
  virtual void CancelSendingFrames(uint32_t ssrc,
                                   const std::vector<uint32_t>& frame_ids) = 0;

  // Resends a frame or part of a frame to kickstart. This is used when the
  // stream appears to be stalled.
  virtual void ResendFrameForKickstart(uint32_t ssrc, uint32_t frame_id) = 0;

  // Returns a callback for receiving packets for testing purposes.
  virtual PacketReceiverCallback PacketReceiverForTesting();

  // The following functions are needed for receving.

  // Add a valid SSRC. This is used to verify that incoming packets
  // come from the right sender. Without valid SSRCs, the return address cannot
  // be automatically established.
  virtual void AddValidSsrc(uint32_t ssrc) = 0;

  // Send an RTCP message from receiver to sender.
  virtual void SendRtcpFromRtpReceiver(
      uint32_t ssrc,
      uint32_t sender_ssrc,
      const RtcpTimeData& time_data,
      const RtcpCastMessage* cast_message,
      base::TimeDelta target_delay,
      const ReceiverRtcpEventSubscriber::RtcpEvents* rtcp_events,
      const RtpReceiverStatistics* rtp_receiver_statistics) = 0;

  // Set options for the PacedSender and Wifi.
  virtual void SetOptions(const base::DictionaryValue& options) = 0;
};

}  // namespace cast
}  // namespace media

#endif  // MEDIA_CAST_NET_CAST_TRANSPORT_SENDER_H_