Home | History | Annotate | Download | only in cast
      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 #ifndef MEDIA_CAST_CAST_DEFINES_H_
      6 #define MEDIA_CAST_CAST_DEFINES_H_
      7 
      8 #include <stdint.h>
      9 
     10 #include <map>
     11 #include <set>
     12 
     13 #include "base/basictypes.h"
     14 #include "base/compiler_specific.h"
     15 #include "base/logging.h"
     16 #include "base/time/time.h"
     17 #include "media/cast/transport/cast_transport_config.h"
     18 
     19 namespace media {
     20 namespace cast {
     21 
     22 const int64 kDontShowTimeoutMs = 33;
     23 const float kDefaultCongestionControlBackOff = 0.875f;
     24 const uint32 kVideoFrequency = 90000;
     25 const uint32 kStartFrameId = UINT32_C(0xffffffff);
     26 
     27 // This is an important system-wide constant.  This limits how much history the
     28 // implementation must retain in order to process the acknowledgements of past
     29 // frames.
     30 const int kMaxUnackedFrames = 60;
     31 
     32 const size_t kMaxIpPacketSize = 1500;
     33 const int kStartRttMs = 20;
     34 const int64 kCastMessageUpdateIntervalMs = 33;
     35 const int64 kNackRepeatIntervalMs = 30;
     36 
     37 enum CastInitializationStatus {
     38   STATUS_AUDIO_UNINITIALIZED,
     39   STATUS_VIDEO_UNINITIALIZED,
     40   STATUS_AUDIO_INITIALIZED,
     41   STATUS_VIDEO_INITIALIZED,
     42   STATUS_INVALID_CAST_ENVIRONMENT,
     43   STATUS_INVALID_CRYPTO_CONFIGURATION,
     44   STATUS_UNSUPPORTED_AUDIO_CODEC,
     45   STATUS_UNSUPPORTED_VIDEO_CODEC,
     46   STATUS_INVALID_AUDIO_CONFIGURATION,
     47   STATUS_INVALID_VIDEO_CONFIGURATION,
     48   STATUS_GPU_ACCELERATION_NOT_SUPPORTED,
     49   STATUS_GPU_ACCELERATION_ERROR,
     50 };
     51 
     52 enum DefaultSettings {
     53   kDefaultAudioEncoderBitrate = 0,  // This means "auto," and may mean VBR.
     54   kDefaultAudioSamplingRate = 48000,
     55   kDefaultMaxQp = 56,
     56   kDefaultMinQp = 4,
     57   kDefaultMaxFrameRate = 30,
     58   kDefaultNumberOfVideoBuffers = 1,
     59   kDefaultRtcpIntervalMs = 500,
     60   kDefaultRtpHistoryMs = 1000,
     61   kDefaultRtpMaxDelayMs = 100,
     62 };
     63 
     64 enum PacketType {
     65   kNewPacket,
     66   kNewPacketCompletingFrame,
     67   kDuplicatePacket,
     68   kTooOldPacket,
     69 };
     70 
     71 // kRtcpCastAllPacketsLost is used in PacketIDSet and
     72 // on the wire to mean that ALL packets for a particular
     73 // frame are lost.
     74 const uint16 kRtcpCastAllPacketsLost = 0xffff;
     75 
     76 // kRtcpCastLastPacket is used in PacketIDSet to ask for
     77 // the last packet of a frame to be retransmitted.
     78 const uint16 kRtcpCastLastPacket = 0xfffe;
     79 
     80 const size_t kMinLengthOfRtcp = 8;
     81 
     82 // Basic RTP header + cast header.
     83 const size_t kMinLengthOfRtp = 12 + 6;
     84 
     85 // Each uint16 represents one packet id within a cast frame.
     86 // Can also contain kRtcpCastAllPacketsLost and kRtcpCastLastPacket.
     87 typedef std::set<uint16> PacketIdSet;
     88 // Each uint8 represents one cast frame.
     89 typedef std::map<uint8, PacketIdSet> MissingFramesAndPacketsMap;
     90 
     91 // TODO(pwestin): Re-factor the functions bellow into a class with static
     92 // methods.
     93 
     94 // January 1970, in NTP seconds.
     95 // Network Time Protocol (NTP), which is in seconds relative to 0h UTC on
     96 // 1 January 1900.
     97 static const int64 kUnixEpochInNtpSeconds = INT64_C(2208988800);
     98 
     99 // Magic fractional unit. Used to convert time (in microseconds) to/from
    100 // fractional NTP seconds.
    101 static const double kMagicFractionalUnit = 4.294967296E3;
    102 
    103 // The maximum number of Cast receiver events to keep in history for the
    104 // purpose of sending the events through RTCP.
    105 // The number chosen should be more than the number of events that can be
    106 // stored in a RTCP packet.
    107 static const size_t kReceiverRtcpEventHistorySize = 512;
    108 
    109 inline bool IsNewerFrameId(uint32 frame_id, uint32 prev_frame_id) {
    110   return (frame_id != prev_frame_id) &&
    111          static_cast<uint32>(frame_id - prev_frame_id) < 0x80000000;
    112 }
    113 
    114 inline bool IsNewerRtpTimestamp(uint32 timestamp, uint32 prev_timestamp) {
    115   return (timestamp != prev_timestamp) &&
    116          static_cast<uint32>(timestamp - prev_timestamp) < 0x80000000;
    117 }
    118 
    119 inline bool IsOlderFrameId(uint32 frame_id, uint32 prev_frame_id) {
    120   return (frame_id == prev_frame_id) || IsNewerFrameId(prev_frame_id, frame_id);
    121 }
    122 
    123 inline bool IsNewerPacketId(uint16 packet_id, uint16 prev_packet_id) {
    124   return (packet_id != prev_packet_id) &&
    125          static_cast<uint16>(packet_id - prev_packet_id) < 0x8000;
    126 }
    127 
    128 inline bool IsNewerSequenceNumber(uint16 sequence_number,
    129                                   uint16 prev_sequence_number) {
    130   // Same function as IsNewerPacketId just different data and name.
    131   return IsNewerPacketId(sequence_number, prev_sequence_number);
    132 }
    133 
    134 // Create a NTP diff from seconds and fractions of seconds; delay_fraction is
    135 // fractions of a second where 0x80000000 is half a second.
    136 inline uint32 ConvertToNtpDiff(uint32 delay_seconds, uint32 delay_fraction) {
    137   return ((delay_seconds & 0x0000FFFF) << 16) +
    138          ((delay_fraction & 0xFFFF0000) >> 16);
    139 }
    140 
    141 inline base::TimeDelta ConvertFromNtpDiff(uint32 ntp_delay) {
    142   uint32 delay_ms = (ntp_delay & 0x0000ffff) * 1000;
    143   delay_ms >>= 16;
    144   delay_ms += ((ntp_delay & 0xffff0000) >> 16) * 1000;
    145   return base::TimeDelta::FromMilliseconds(delay_ms);
    146 }
    147 
    148 inline void ConvertTimeToFractions(int64 ntp_time_us,
    149                                    uint32* seconds,
    150                                    uint32* fractions) {
    151   DCHECK_GE(ntp_time_us, 0) << "Time must NOT be negative";
    152   const int64 seconds_component =
    153       ntp_time_us / base::Time::kMicrosecondsPerSecond;
    154   // NTP time will overflow in the year 2036.  Also, make sure unit tests don't
    155   // regress and use an origin past the year 2036.  If this overflows here, the
    156   // inverse calculation fails to compute the correct TimeTicks value, throwing
    157   // off the entire system.
    158   DCHECK_LT(seconds_component, INT64_C(4263431296))
    159       << "One year left to fix the NTP year 2036 wrap-around issue!";
    160   *seconds = static_cast<uint32>(seconds_component);
    161   *fractions = static_cast<uint32>(
    162       (ntp_time_us % base::Time::kMicrosecondsPerSecond) *
    163           kMagicFractionalUnit);
    164 }
    165 
    166 inline void ConvertTimeTicksToNtp(const base::TimeTicks& time,
    167                                   uint32* ntp_seconds,
    168                                   uint32* ntp_fractions) {
    169   base::TimeDelta elapsed_since_unix_epoch =
    170       time - base::TimeTicks::UnixEpoch();
    171 
    172   int64 ntp_time_us =
    173       elapsed_since_unix_epoch.InMicroseconds() +
    174       (kUnixEpochInNtpSeconds * base::Time::kMicrosecondsPerSecond);
    175 
    176   ConvertTimeToFractions(ntp_time_us, ntp_seconds, ntp_fractions);
    177 }
    178 
    179 inline base::TimeTicks ConvertNtpToTimeTicks(uint32 ntp_seconds,
    180                                              uint32 ntp_fractions) {
    181   int64 ntp_time_us =
    182       static_cast<int64>(ntp_seconds) * base::Time::kMicrosecondsPerSecond +
    183       static_cast<int64>(ntp_fractions) / kMagicFractionalUnit;
    184 
    185   base::TimeDelta elapsed_since_unix_epoch = base::TimeDelta::FromMicroseconds(
    186       ntp_time_us -
    187       (kUnixEpochInNtpSeconds * base::Time::kMicrosecondsPerSecond));
    188   return base::TimeTicks::UnixEpoch() + elapsed_since_unix_epoch;
    189 }
    190 
    191 inline base::TimeDelta RtpDeltaToTimeDelta(int64 rtp_delta, int rtp_timebase) {
    192   DCHECK_GT(rtp_timebase, 0);
    193   return rtp_delta * base::TimeDelta::FromSeconds(1) / rtp_timebase;
    194 }
    195 
    196 inline uint32 GetVideoRtpTimestamp(const base::TimeTicks& time_ticks) {
    197   base::TimeTicks zero_time;
    198   base::TimeDelta recorded_delta = time_ticks - zero_time;
    199   // Timestamp is in 90 KHz for video.
    200   return static_cast<uint32>(recorded_delta.InMilliseconds() * 90);
    201 }
    202 
    203 }  // namespace cast
    204 }  // namespace media
    205 
    206 #endif  // MEDIA_CAST_CAST_DEFINES_H_
    207