15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 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)
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#ifndef MEDIA_CAST_NET_CAST_TRANSPORT_CONFIG_H_
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#define MEDIA_CAST_NET_CAST_TRANSPORT_CONFIG_H_
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <string>
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <vector>
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/basictypes.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/callback.h"
130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "base/memory/linked_ptr.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/memory/ref_counted.h"
15cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/stl_util.h"
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "media/cast/net/cast_transport_defines.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace media {
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace cast {
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdochenum Codec {
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  CODEC_UNKNOWN,
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  CODEC_AUDIO_OPUS,
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  CODEC_AUDIO_PCM16,
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  CODEC_VIDEO_FAKE,
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  CODEC_VIDEO_VP8,
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  CODEC_VIDEO_H264,
28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  CODEC_LAST = CODEC_VIDEO_H264
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
31010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)struct CastTransportRtpConfig {
32010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  CastTransportRtpConfig();
33010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  ~CastTransportRtpConfig();
34010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Identifier refering to this sender.
36116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  uint32 ssrc;
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Identifier for incoming RTCP traffic.
395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  uint32 feedback_ssrc;
405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // RTP payload type enum: Specifies the type/encoding of frame data.
42116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  int rtp_payload_type;
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // The AES crypto key and initialization vector.  Each of these strings
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // contains the data in binary form, of size kAesKeySize.  If they are empty
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // strings, crypto is not being used.
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::string aes_key;
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::string aes_iv_mask;
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// A combination of metadata and data for one encoded frame.  This can contain
52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// audio data or video data or other.
53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)struct EncodedFrame {
54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  enum Dependency {
55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // "null" value, used to indicate whether |dependency| has been set.
56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    UNKNOWN_DEPENDENCY,
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // Not decodable without the reference frame indicated by
59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // |referenced_frame_id|.
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    DEPENDENT,
61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // Independently decodable.
63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    INDEPENDENT,
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // Independently decodable, and no future frames will depend on any frames
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // before this one.
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    KEY,
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    DEPENDENCY_LAST = KEY
70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  };
71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  EncodedFrame();
73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ~EncodedFrame();
74cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Convenience accessors to data as an array of uint8 elements.
76cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  const uint8* bytes() const {
77cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return reinterpret_cast<uint8*>(string_as_array(
78cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        const_cast<std::string*>(&data)));
79cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
80cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  uint8* mutable_bytes() {
81cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return reinterpret_cast<uint8*>(string_as_array(&data));
82cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
83cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
8446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Copies all data members except |data| to |dest|.
8546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Does not modify |dest->data|.
8646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void CopyMetadataTo(EncodedFrame* dest) const;
8746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
88cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // This frame's dependency relationship with respect to other frames.
89cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  Dependency dependency;
90cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
91cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // The label associated with this frame.  Implies an ordering relative to
92cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // other frames in the same stream.
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  uint32 frame_id;
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
95cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // The label associated with the frame upon which this frame depends.  If
96cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // this frame does not require any other frame in order to become decodable
97cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // (e.g., key frames), |referenced_frame_id| must equal |frame_id|.
98cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  uint32 referenced_frame_id;
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
100cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // The stream timestamp, on the timeline of the signal data.  For example, RTP
101cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // timestamps for audio are usually defined as the total number of audio
102cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // samples encoded in all prior frames.  A playback system uses this value to
103cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // detect gaps in the stream, and otherwise stretch the signal to match
104cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // playout targets.
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  uint32 rtp_timestamp;
106cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
107cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // The common reference clock timestamp for this frame.  This value originates
108cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // from a sender and is used to provide lip synchronization between streams in
109cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // a receiver.  Thus, in the sender context, this is set to the time at which
110cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // the frame was captured/recorded.  In the receiver context, this is set to
111cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // the target playout time.  Over a sequence of frames, this time value is
112cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // expected to drift with respect to the elapsed time implied by the RTP
113cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // timestamps; and it may not necessarily increment with precise regularity.
114cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::TimeTicks reference_time;
115cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
11603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // Playout delay for this and all future frames. Used by the Adaptive
11703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // Playout delay extension. Zero means no change.
11803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  uint16 new_playout_delay_ms;
11903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
120cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // The encoded signal data.
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string data;
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)typedef std::vector<uint8> Packet;
1250529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochtypedef scoped_refptr<base::RefCountedData<Packet> > PacketRef;
1260529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochtypedef std::vector<PacketRef> PacketList;
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)typedef base::Callback<void(scoped_ptr<Packet> packet)> PacketReceiverCallback;
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class PacketSender {
1315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
1320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Send a packet to the network. Returns false if the network is blocked
1330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // and we should wait for |cb| to be called. It is not allowed to called
1340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // SendPacket again until |cb| has been called. Any other errors that
1350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // occur will be reported through side channels, in such cases, this function
1360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // will return true indicating that the channel is not blocked.
1370529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  virtual bool SendPacket(PacketRef packet, const base::Closure& cb) = 0;
13803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
13903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // Returns the number of bytes ever sent.
14003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  virtual int64 GetBytesSent() = 0;
14103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~PacketSender() {}
1435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
1445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)struct RtcpSenderInfo {
1465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RtcpSenderInfo();
1475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ~RtcpSenderInfo();
1485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // First three members are used for lipsync.
1495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // First two members are used for rtt.
1505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  uint32 ntp_seconds;
1515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  uint32 ntp_fraction;
1525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  uint32 rtp_timestamp;
1535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  uint32 send_packet_count;
1545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  size_t send_octet_count;
1555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)struct RtcpReportBlock {
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RtcpReportBlock();
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ~RtcpReportBlock();
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  uint32 remote_ssrc;  // SSRC of sender of this report.
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  uint32 media_ssrc;   // SSRC of the RTP packet sender.
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  uint8 fraction_lost;
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  uint32 cumulative_lost;  // 24 bits valid.
1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  uint32 extended_high_sequence_number;
1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  uint32 jitter;
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  uint32 last_sr;
1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  uint32 delay_since_last_sr;
1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)struct RtcpDlrrReportBlock {
1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RtcpDlrrReportBlock();
1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ~RtcpDlrrReportBlock();
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  uint32 last_rr;
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  uint32 delay_since_last_rr;
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline bool operator==(RtcpSenderInfo lhs, RtcpSenderInfo rhs) {
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return lhs.ntp_seconds == rhs.ntp_seconds &&
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)         lhs.ntp_fraction == rhs.ntp_fraction &&
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)         lhs.rtp_timestamp == rhs.rtp_timestamp &&
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)         lhs.send_packet_count == rhs.send_packet_count &&
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)         lhs.send_octet_count == rhs.send_octet_count;
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace cast
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace media
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
188116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif  // MEDIA_CAST_NET_CAST_TRANSPORT_CONFIG_H_
189