Home | History | Annotate | Download | only in transport
      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_TRANSPORT_CAST_TRANSPORT_CONFIG_H_
      6 #define MEDIA_CAST_TRANSPORT_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/transport/cast_transport_defines.h"
     17 #include "net/base/ip_endpoint.h"
     18 
     19 namespace media {
     20 namespace cast {
     21 namespace transport {
     22 
     23 enum RtcpMode {
     24   kRtcpCompound,     // Compound RTCP mode is described by RFC 4585.
     25   kRtcpReducedSize,  // Reduced-size RTCP mode is described by RFC 5506.
     26 };
     27 
     28 enum VideoCodec {
     29   kUnknownVideoCodec,
     30   kFakeSoftwareVideo,
     31   kVp8,
     32   kH264,
     33   kVideoCodecLast = kH264
     34 };
     35 
     36 enum AudioCodec {
     37   kUnknownAudioCodec,
     38   kOpus,
     39   kPcm16,
     40   kAudioCodecLast = kPcm16
     41 };
     42 
     43 struct RtpConfig {
     44   RtpConfig();
     45   ~RtpConfig();
     46   uint32 ssrc;
     47   int max_delay_ms;
     48   int payload_type;
     49   std::string aes_key;      // Binary string of size kAesKeySize.
     50   std::string aes_iv_mask;  // Binary string of size kAesBlockSize.
     51 };
     52 
     53 struct CastTransportRtpConfig {
     54   CastTransportRtpConfig();
     55   ~CastTransportRtpConfig();
     56   RtpConfig config;
     57   int max_outstanding_frames;
     58 };
     59 
     60 struct CastTransportAudioConfig {
     61   CastTransportAudioConfig();
     62   ~CastTransportAudioConfig();
     63 
     64   CastTransportRtpConfig rtp;
     65   AudioCodec codec;
     66   int frequency;
     67   int channels;
     68 };
     69 
     70 struct CastTransportVideoConfig {
     71   CastTransportVideoConfig();
     72   ~CastTransportVideoConfig();
     73 
     74   CastTransportRtpConfig rtp;
     75   VideoCodec codec;
     76 };
     77 
     78 // A combination of metadata and data for one encoded frame.  This can contain
     79 // audio data or video data or other.
     80 struct EncodedFrame {
     81   enum Dependency {
     82     // "null" value, used to indicate whether |dependency| has been set.
     83     UNKNOWN_DEPENDENCY,
     84 
     85     // Not decodable without the reference frame indicated by
     86     // |referenced_frame_id|.
     87     DEPENDENT,
     88 
     89     // Independently decodable.
     90     INDEPENDENT,
     91 
     92     // Independently decodable, and no future frames will depend on any frames
     93     // before this one.
     94     KEY,
     95 
     96     DEPENDENCY_LAST = KEY
     97   };
     98 
     99   EncodedFrame();
    100   ~EncodedFrame();
    101 
    102   // Convenience accessors to data as an array of uint8 elements.
    103   const uint8* bytes() const {
    104     return reinterpret_cast<uint8*>(string_as_array(
    105         const_cast<std::string*>(&data)));
    106   }
    107   uint8* mutable_bytes() {
    108     return reinterpret_cast<uint8*>(string_as_array(&data));
    109   }
    110 
    111   // Copies all data members except |data| to |dest|.
    112   // Does not modify |dest->data|.
    113   void CopyMetadataTo(EncodedFrame* dest) const;
    114 
    115   // This frame's dependency relationship with respect to other frames.
    116   Dependency dependency;
    117 
    118   // The label associated with this frame.  Implies an ordering relative to
    119   // other frames in the same stream.
    120   uint32 frame_id;
    121 
    122   // The label associated with the frame upon which this frame depends.  If
    123   // this frame does not require any other frame in order to become decodable
    124   // (e.g., key frames), |referenced_frame_id| must equal |frame_id|.
    125   uint32 referenced_frame_id;
    126 
    127   // The stream timestamp, on the timeline of the signal data.  For example, RTP
    128   // timestamps for audio are usually defined as the total number of audio
    129   // samples encoded in all prior frames.  A playback system uses this value to
    130   // detect gaps in the stream, and otherwise stretch the signal to match
    131   // playout targets.
    132   uint32 rtp_timestamp;
    133 
    134   // The common reference clock timestamp for this frame.  This value originates
    135   // from a sender and is used to provide lip synchronization between streams in
    136   // a receiver.  Thus, in the sender context, this is set to the time at which
    137   // the frame was captured/recorded.  In the receiver context, this is set to
    138   // the target playout time.  Over a sequence of frames, this time value is
    139   // expected to drift with respect to the elapsed time implied by the RTP
    140   // timestamps; and it may not necessarily increment with precise regularity.
    141   base::TimeTicks reference_time;
    142 
    143   // The encoded signal data.
    144   std::string data;
    145 };
    146 
    147 typedef std::vector<uint8> Packet;
    148 typedef scoped_refptr<base::RefCountedData<Packet> > PacketRef;
    149 typedef std::vector<PacketRef> PacketList;
    150 
    151 typedef base::Callback<void(scoped_ptr<Packet> packet)> PacketReceiverCallback;
    152 
    153 class PacketSender {
    154  public:
    155   // Send a packet to the network. Returns false if the network is blocked
    156   // and we should wait for |cb| to be called. It is not allowed to called
    157   // SendPacket again until |cb| has been called. Any other errors that
    158   // occur will be reported through side channels, in such cases, this function
    159   // will return true indicating that the channel is not blocked.
    160   virtual bool SendPacket(PacketRef packet, const base::Closure& cb) = 0;
    161   virtual ~PacketSender() {}
    162 };
    163 
    164 struct RtcpSenderInfo {
    165   RtcpSenderInfo();
    166   ~RtcpSenderInfo();
    167   // First three members are used for lipsync.
    168   // First two members are used for rtt.
    169   uint32 ntp_seconds;
    170   uint32 ntp_fraction;
    171   uint32 rtp_timestamp;
    172   uint32 send_packet_count;
    173   size_t send_octet_count;
    174 };
    175 
    176 struct RtcpReportBlock {
    177   RtcpReportBlock();
    178   ~RtcpReportBlock();
    179   uint32 remote_ssrc;  // SSRC of sender of this report.
    180   uint32 media_ssrc;   // SSRC of the RTP packet sender.
    181   uint8 fraction_lost;
    182   uint32 cumulative_lost;  // 24 bits valid.
    183   uint32 extended_high_sequence_number;
    184   uint32 jitter;
    185   uint32 last_sr;
    186   uint32 delay_since_last_sr;
    187 };
    188 
    189 struct RtcpDlrrReportBlock {
    190   RtcpDlrrReportBlock();
    191   ~RtcpDlrrReportBlock();
    192   uint32 last_rr;
    193   uint32 delay_since_last_rr;
    194 };
    195 
    196 // This is only needed because IPC messages don't support more than
    197 // 5 arguments.
    198 struct SendRtcpFromRtpSenderData {
    199   SendRtcpFromRtpSenderData();
    200   ~SendRtcpFromRtpSenderData();
    201   uint32 packet_type_flags;
    202   uint32 sending_ssrc;
    203   std::string c_name;
    204   uint32 ntp_seconds;
    205   uint32 ntp_fraction;
    206   uint32 rtp_timestamp;
    207 };
    208 
    209 inline bool operator==(RtcpSenderInfo lhs, RtcpSenderInfo rhs) {
    210   return lhs.ntp_seconds == rhs.ntp_seconds &&
    211          lhs.ntp_fraction == rhs.ntp_fraction &&
    212          lhs.rtp_timestamp == rhs.rtp_timestamp &&
    213          lhs.send_packet_count == rhs.send_packet_count &&
    214          lhs.send_octet_count == rhs.send_octet_count;
    215 }
    216 
    217 }  // namespace transport
    218 }  // namespace cast
    219 }  // namespace media
    220 
    221 #endif  // MEDIA_CAST_TRANSPORT_CAST_TRANSPORT_CONFIG_H_
    222