15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// This is the main interface for the cast transport sender.  It accepts encoded
646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// frames (both audio and video), encrypts their encoded data, packetizes them
746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)// and feeds them into a transport (e.g., UDP).
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Construction of the Cast Sender and the Cast Transport Sender should be done
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// in the following order:
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 1. Create CastTransportSender.
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 2. Create CastSender (accepts CastTransportSender as an input).
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// 3. Call CastTransportSender::SetPacketReceiver to ensure that the packets
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//    received by the CastTransportSender will be sent to the CastSender.
15010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)// Steps 3 can be done interchangeably.
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Destruction: The CastTransportSender is assumed to be valid as long as the
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// CastSender is alive. Therefore the CastSender should be destructed before the
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// CastTransportSender.
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// This also works when the CastSender acts as a receiver for the RTCP packets
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// due to the weak pointers in the ReceivedPacket method in cast_sender_impl.cc.
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef MEDIA_CAST_TRANSPORT_CAST_TRANSPORT_SENDER_H_
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define MEDIA_CAST_TRANSPORT_CAST_TRANSPORT_SENDER_H_
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/basictypes.h"
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/callback.h"
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/single_thread_task_runner.h"
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/threading/non_thread_safe.h"
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/time/tick_clock.h"
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "media/cast/logging/logging_defines.h"
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "media/cast/transport/cast_transport_config.h"
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "media/cast/transport/cast_transport_defines.h"
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace net {
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class NetLog;
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  // namespace net
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace media {
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace cast {
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace transport {
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Following the initialization of either audio or video an initialization
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// status will be sent via this callback.
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)typedef base::Callback<void(CastTransportStatus status)>
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    CastTransportStatusCallback;
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)typedef base::Callback<void(const std::vector<PacketEvent>&)>
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    BulkRawEventsCallback;
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// The application should only trigger this class from the transport thread.
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class CastTransportSender : public base::NonThreadSafe {
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static scoped_ptr<CastTransportSender> Create(
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      net::NetLog* net_log,
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      base::TickClock* clock,
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const net::IPEndPoint& remote_end_point,
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const CastTransportStatusCallback& status_callback,
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const BulkRawEventsCallback& raw_events_callback,
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::TimeDelta raw_events_callback_interval,
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const scoped_refptr<base::SingleThreadTaskRunner>& transport_task_runner);
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~CastTransportSender() {}
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Audio/Video initialization.
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Encoded frames cannot be transmitted until the relevant initialize method
67010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // is called. Usually called by CastSender.
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void InitializeAudio(const CastTransportAudioConfig& config) = 0;
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void InitializeVideo(const CastTransportVideoConfig& config) = 0;
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Sets the Cast packet receiver. Should be called after creation on the
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Cast sender. Packets won't be received until this function is called.
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void SetPacketReceiver(
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const PacketReceiverCallback& packet_receiver) = 0;
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The following two functions handle the encoded media frames (audio and
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // video) to be processed.
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Frames will be encrypted, packetized and transmitted to the network.
80cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void InsertCodedAudioFrame(const EncodedFrame& audio_frame) = 0;
81cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void InsertCodedVideoFrame(const EncodedFrame& video_frame) = 0;
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Builds an RTCP packet and sends it to the network.
84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // |ntp_seconds|, |ntp_fraction| and |rtp_timestamp| are used in the
85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // RTCP Sender Report.
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void SendRtcpFromRtpSender(uint32 packet_type_flags,
87cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                     uint32 ntp_seconds,
88cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                     uint32 ntp_fraction,
89cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                     uint32 rtp_timestamp,
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                     const RtcpDlrrReportBlock& dlrr,
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                     uint32 sending_ssrc,
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                     const std::string& c_name) = 0;
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Retransmission request.
95f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // |missing_packets| includes the list of frames and packets in each
96f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // frame to be re-transmitted.
97f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // If |cancel_rtx_if_not_in_list| is used as an optimization to cancel
98f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // pending re-transmission requests of packets not listed in
996d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // |missing_packets|. If the requested packet(s) were sent recently
1006d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // (how long is specified by |dedupe_window|) then this re-transmit
1016d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // will be ignored.
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void ResendPackets(
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      bool is_audio,
104f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      const MissingFramesAndPacketsMap& missing_packets,
1056d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      bool cancel_rtx_if_not_in_list,
1066d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      base::TimeDelta dedupe_window) = 0;
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace transport
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace cast
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace media
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // MEDIA_CAST_TRANSPORT_CAST_TRANSPORT_SENDER_H_
114