1// Copyright 2013 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 file contains the interface to the cast RTP sender.
6
7#ifndef MEDIA_CAST_TRANSPORT_RTP_SENDER_RTP_SENDER_H_
8#define MEDIA_CAST_TRANSPORT_RTP_SENDER_RTP_SENDER_H_
9
10#include <map>
11#include <set>
12
13#include "base/memory/scoped_ptr.h"
14#include "base/time/tick_clock.h"
15#include "base/time/time.h"
16#include "base/memory/weak_ptr.h"
17#include "media/cast/cast_config.h"
18#include "media/cast/cast_environment.h"
19#include "media/cast/transport/cast_transport_defines.h"
20#include "media/cast/transport/cast_transport_sender.h"
21#include "media/cast/transport/pacing/paced_sender.h"
22#include "media/cast/transport/rtp_sender/packet_storage/packet_storage.h"
23#include "media/cast/transport/rtp_sender/rtp_packetizer/rtp_packetizer.h"
24
25namespace media {
26namespace cast {
27
28namespace transport {
29
30// This object is only called from the main cast thread.
31// This class handles splitting encoded audio and video frames into packets and
32// add an RTP header to each packet. The sent packets are stored until they are
33// acknowledged by the remote peer or timed out.
34class RtpSender {
35 public:
36  RtpSender(
37      base::TickClock* clock,
38      const scoped_refptr<base::SingleThreadTaskRunner>& transport_task_runner,
39      PacedSender* const transport);
40
41  ~RtpSender();
42
43  // Initialize audio stack. Audio must be initialized prior to sending encoded
44  // audio frames. Returns false if configuration is invalid.
45  bool InitializeAudio(const CastTransportAudioConfig& config);
46
47  // Initialize video stack. Video must be initialized prior to sending encoded
48  // video frames. Returns false if configuration is invalid.
49  bool InitializeVideo(const CastTransportVideoConfig& config);
50
51  void SendFrame(const EncodedFrame& frame);
52
53  void ResendPackets(const MissingFramesAndPacketsMap& missing_packets,
54                     bool cancel_rtx_if_not_in_list,
55                     base::TimeDelta dedupe_window);
56
57  size_t send_packet_count() const {
58    return packetizer_ ? packetizer_->send_packet_count() : 0;
59  }
60  size_t send_octet_count() const {
61    return packetizer_ ? packetizer_->send_octet_count() : 0;
62  }
63  uint32 ssrc() const { return config_.ssrc; }
64
65 private:
66  void UpdateSequenceNumber(Packet* packet);
67
68  base::TickClock* clock_;  // Not owned by this class.
69  RtpPacketizerConfig config_;
70  scoped_ptr<RtpPacketizer> packetizer_;
71  scoped_ptr<PacketStorage> storage_;
72  PacedSender* const transport_;
73  scoped_refptr<base::SingleThreadTaskRunner> transport_task_runner_;
74
75  // NOTE: Weak pointers must be invalidated before all other member variables.
76  base::WeakPtrFactory<RtpSender> weak_factory_;
77
78  DISALLOW_COPY_AND_ASSIGN(RtpSender);
79};
80
81}  // namespace transport
82}  // namespace cast
83}  // namespace media
84
85#endif  // MEDIA_CAST_TRANSPORT_RTP_SENDER_RTP_SENDER_H_
86