summaryrefslogtreecommitdiffstats
path: root/media/cast/net/rtp/rtp_sender.h
blob: 68cb601626110995a15f64efb95bacfdff9aa9f4 (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
// 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 file contains the interface to the cast RTP sender.

#ifndef MEDIA_CAST_NET_RTP_RTP_SENDER_H_
#define MEDIA_CAST_NET_RTP_RTP_SENDER_H_

#include <map>
#include <set>

#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "media/cast/cast_environment.h"
#include "media/cast/net/cast_transport_defines.h"
#include "media/cast/net/cast_transport_sender.h"
#include "media/cast/net/pacing/paced_sender.h"
#include "media/cast/net/rtp/packet_storage.h"
#include "media/cast/net/rtp/rtp_packetizer.h"

namespace media {
namespace cast {

// This object is only called from the main cast thread.
// This class handles splitting encoded audio and video frames into packets and
// add an RTP header to each packet. The sent packets are stored until they are
// acknowledged by the remote peer or timed out.
class RtpSender {
 public:
  RtpSender(
      const scoped_refptr<base::SingleThreadTaskRunner>& transport_task_runner,
      PacedSender* const transport);

  ~RtpSender();

  // This must be called before sending any frames. Returns false if
  // configuration is invalid.
  bool Initialize(const CastTransportRtpConfig& config);

  void SendFrame(const EncodedFrame& frame);

  void ResendPackets(const MissingFramesAndPacketsMap& missing_packets,
                     bool cancel_rtx_if_not_in_list,
                     const DedupInfo& dedup_info);

  // Returns the total number of bytes sent to the socket when the specified
  // frame was just sent.
  // Returns 0 if the frame cannot be found or the frame was only sent
  // partially.
  int64_t GetLastByteSentForFrame(uint32_t frame_id);

  void CancelSendingFrames(const std::vector<uint32_t>& frame_ids);

  void ResendFrameForKickstart(uint32_t frame_id,
                               base::TimeDelta dedupe_window);

  size_t send_packet_count() const {
    return packetizer_ ? packetizer_->send_packet_count() : 0;
  }
  size_t send_octet_count() const {
    return packetizer_ ? packetizer_->send_octet_count() : 0;
  }
  uint32_t ssrc() const { return config_.ssrc; }

 private:
  void UpdateSequenceNumber(Packet* packet);

  RtpPacketizerConfig config_;
  PacketStorage storage_;
  scoped_ptr<RtpPacketizer> packetizer_;
  PacedSender* const transport_;
  scoped_refptr<base::SingleThreadTaskRunner> transport_task_runner_;

  // NOTE: Weak pointers must be invalidated before all other member variables.
  base::WeakPtrFactory<RtpSender> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(RtpSender);
};

}  // namespace cast
}  // namespace media

#endif  // MEDIA_CAST_NET_RTP_SENDER_RTP_SENDER_H_