Home | History | Annotate | Download | only in net
      1 // Copyright 2014 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 #ifndef MEDIA_CAST_NET_CAST_TRANSPORT_CONFIG_H_
      6 #define MEDIA_CAST_NET_CAST_TRANSPORT_CONFIG_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/callback.h"
     13 #include "base/memory/linked_ptr.h"
     14 #include "base/memory/ref_counted.h"
     15 #include "base/stl_util.h"
     16 #include "media/cast/net/cast_transport_defines.h"
     17 
     18 namespace media {
     19 namespace cast {
     20 
     21 enum Codec {
     22   CODEC_UNKNOWN,
     23   CODEC_AUDIO_OPUS,
     24   CODEC_AUDIO_PCM16,
     25   CODEC_VIDEO_FAKE,
     26   CODEC_VIDEO_VP8,
     27   CODEC_VIDEO_H264,
     28   CODEC_LAST = CODEC_VIDEO_H264
     29 };
     30 
     31 struct CastTransportRtpConfig {
     32   CastTransportRtpConfig();
     33   ~CastTransportRtpConfig();
     34 
     35   // Identifier refering to this sender.
     36   uint32 ssrc;
     37 
     38   // Identifier for incoming RTCP traffic.
     39   uint32 feedback_ssrc;
     40 
     41   // RTP payload type enum: Specifies the type/encoding of frame data.
     42   int rtp_payload_type;
     43 
     44   // The AES crypto key and initialization vector.  Each of these strings
     45   // contains the data in binary form, of size kAesKeySize.  If they are empty
     46   // strings, crypto is not being used.
     47   std::string aes_key;
     48   std::string aes_iv_mask;
     49 };
     50 
     51 // A combination of metadata and data for one encoded frame.  This can contain
     52 // audio data or video data or other.
     53 struct EncodedFrame {
     54   enum Dependency {
     55     // "null" value, used to indicate whether |dependency| has been set.
     56     UNKNOWN_DEPENDENCY,
     57 
     58     // Not decodable without the reference frame indicated by
     59     // |referenced_frame_id|.
     60     DEPENDENT,
     61 
     62     // Independently decodable.
     63     INDEPENDENT,
     64 
     65     // Independently decodable, and no future frames will depend on any frames
     66     // before this one.
     67     KEY,
     68 
     69     DEPENDENCY_LAST = KEY
     70   };
     71 
     72   EncodedFrame();
     73   ~EncodedFrame();
     74 
     75   // Convenience accessors to data as an array of uint8 elements.
     76   const uint8* bytes() const {
     77     return reinterpret_cast<uint8*>(string_as_array(
     78         const_cast<std::string*>(&data)));
     79   }
     80   uint8* mutable_bytes() {
     81     return reinterpret_cast<uint8*>(string_as_array(&data));
     82   }
     83 
     84   // Copies all data members except |data| to |dest|.
     85   // Does not modify |dest->data|.
     86   void CopyMetadataTo(EncodedFrame* dest) const;
     87 
     88   // This frame's dependency relationship with respect to other frames.
     89   Dependency dependency;
     90 
     91   // The label associated with this frame.  Implies an ordering relative to
     92   // other frames in the same stream.
     93   uint32 frame_id;
     94 
     95   // The label associated with the frame upon which this frame depends.  If
     96   // this frame does not require any other frame in order to become decodable
     97   // (e.g., key frames), |referenced_frame_id| must equal |frame_id|.
     98   uint32 referenced_frame_id;
     99 
    100   // The stream timestamp, on the timeline of the signal data.  For example, RTP
    101   // timestamps for audio are usually defined as the total number of audio
    102   // samples encoded in all prior frames.  A playback system uses this value to
    103   // detect gaps in the stream, and otherwise stretch the signal to match
    104   // playout targets.
    105   uint32 rtp_timestamp;
    106 
    107   // The common reference clock timestamp for this frame.  This value originates
    108   // from a sender and is used to provide lip synchronization between streams in
    109   // a receiver.  Thus, in the sender context, this is set to the time at which
    110   // the frame was captured/recorded.  In the receiver context, this is set to
    111   // the target playout time.  Over a sequence of frames, this time value is
    112   // expected to drift with respect to the elapsed time implied by the RTP
    113   // timestamps; and it may not necessarily increment with precise regularity.
    114   base::TimeTicks reference_time;
    115 
    116   // Playout delay for this and all future frames. Used by the Adaptive
    117   // Playout delay extension. Zero means no change.
    118   uint16 new_playout_delay_ms;
    119 
    120   // The encoded signal data.
    121   std::string data;
    122 };
    123 
    124 typedef std::vector<uint8> Packet;
    125 typedef scoped_refptr<base::RefCountedData<Packet> > PacketRef;
    126 typedef std::vector<PacketRef> PacketList;
    127 
    128 typedef base::Callback<void(scoped_ptr<Packet> packet)> PacketReceiverCallback;
    129 
    130 class PacketSender {
    131  public:
    132   // Send a packet to the network. Returns false if the network is blocked
    133   // and we should wait for |cb| to be called. It is not allowed to called
    134   // SendPacket again until |cb| has been called. Any other errors that
    135   // occur will be reported through side channels, in such cases, this function
    136   // will return true indicating that the channel is not blocked.
    137   virtual bool SendPacket(PacketRef packet, const base::Closure& cb) = 0;
    138 
    139   // Returns the number of bytes ever sent.
    140   virtual int64 GetBytesSent() = 0;
    141 
    142   virtual ~PacketSender() {}
    143 };
    144 
    145 struct RtcpSenderInfo {
    146   RtcpSenderInfo();
    147   ~RtcpSenderInfo();
    148   // First three members are used for lipsync.
    149   // First two members are used for rtt.
    150   uint32 ntp_seconds;
    151   uint32 ntp_fraction;
    152   uint32 rtp_timestamp;
    153   uint32 send_packet_count;
    154   size_t send_octet_count;
    155 };
    156 
    157 struct RtcpReportBlock {
    158   RtcpReportBlock();
    159   ~RtcpReportBlock();
    160   uint32 remote_ssrc;  // SSRC of sender of this report.
    161   uint32 media_ssrc;   // SSRC of the RTP packet sender.
    162   uint8 fraction_lost;
    163   uint32 cumulative_lost;  // 24 bits valid.
    164   uint32 extended_high_sequence_number;
    165   uint32 jitter;
    166   uint32 last_sr;
    167   uint32 delay_since_last_sr;
    168 };
    169 
    170 struct RtcpDlrrReportBlock {
    171   RtcpDlrrReportBlock();
    172   ~RtcpDlrrReportBlock();
    173   uint32 last_rr;
    174   uint32 delay_since_last_rr;
    175 };
    176 
    177 inline bool operator==(RtcpSenderInfo lhs, RtcpSenderInfo rhs) {
    178   return lhs.ntp_seconds == rhs.ntp_seconds &&
    179          lhs.ntp_fraction == rhs.ntp_fraction &&
    180          lhs.rtp_timestamp == rhs.rtp_timestamp &&
    181          lhs.send_packet_count == rhs.send_packet_count &&
    182          lhs.send_octet_count == rhs.send_octet_count;
    183 }
    184 
    185 }  // namespace cast
    186 }  // namespace media
    187 
    188 #endif  // MEDIA_CAST_NET_CAST_TRANSPORT_CONFIG_H_
    189