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 <map>
      9 #include <set>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/compiler_specific.h"
     13 #include "base/logging.h"
     14 #include "base/time/time.h"
     15 
     16 namespace media {
     17 namespace cast {
     18 
     19 const int64 kDontShowTimeoutMs = 33;
     20 const float kDefaultCongestionControlBackOff = 0.875f;
     21 const uint32 kVideoFrequency = 90000;
     22 const int64 kSkippedFramesCheckPeriodkMs = 10000;
     23 const uint32 kStartFrameId = GG_UINT32_C(0xffffffff);
     24 
     25 // Number of skipped frames threshold in fps (as configured) per period above.
     26 const int kSkippedFramesThreshold = 3;
     27 const size_t kIpPacketSize = 1500;
     28 const int kStartRttMs = 20;
     29 const int64 kCastMessageUpdateIntervalMs = 33;
     30 const int64 kNackRepeatIntervalMs = 30;
     31 
     32 enum DefaultSettings {
     33   kDefaultAudioEncoderBitrate = 0,  // This means "auto," and may mean VBR.
     34   kDefaultAudioSamplingRate = 48000,
     35   kDefaultMaxQp = 56,
     36   kDefaultMinQp = 4,
     37   kDefaultMaxFrameRate = 30,
     38   kDefaultNumberOfVideoBuffers = 1,
     39   kDefaultRtcpIntervalMs = 500,
     40   kDefaultRtpHistoryMs = 1000,
     41   kDefaultRtpMaxDelayMs = 100,
     42 };
     43 
     44 const uint16 kRtcpCastAllPacketsLost = 0xffff;
     45 
     46 const size_t kMinLengthOfRtcp = 8;
     47 
     48 // Basic RTP header + cast header.
     49 const size_t kMinLengthOfRtp = 12 + 6;
     50 
     51 const size_t kAesBlockSize = 16;
     52 const size_t kAesKeySize = 16;
     53 
     54 // Each uint16 represents one packet id within a cast frame.
     55 typedef std::set<uint16> PacketIdSet;
     56 // Each uint8 represents one cast frame.
     57 typedef std::map<uint8, PacketIdSet> MissingFramesAndPacketsMap;
     58 
     59 // TODO(pwestin): Re-factor the functions bellow into a class with static
     60 // methods.
     61 
     62 // January 1970, in NTP seconds.
     63 // Network Time Protocol (NTP), which is in seconds relative to 0h UTC on
     64 // 1 January 1900.
     65 static const int64 kUnixEpochInNtpSeconds = GG_INT64_C(2208988800);
     66 
     67 // Magic fractional unit. Used to convert time (in microseconds) to/from
     68 // fractional NTP seconds.
     69 static const double kMagicFractionalUnit = 4.294967296E3;
     70 
     71 inline bool IsNewerFrameId(uint32 frame_id, uint32 prev_frame_id) {
     72   return (frame_id != prev_frame_id) &&
     73       static_cast<uint32>(frame_id - prev_frame_id) < 0x80000000;
     74 }
     75 
     76 inline bool IsNewerRtpTimestamp(uint32 timestamp, uint32 prev_timestamp) {
     77   return (timestamp != prev_timestamp) &&
     78       static_cast<uint32>(timestamp - prev_timestamp) < 0x80000000;
     79 }
     80 
     81 inline bool IsOlderFrameId(uint32 frame_id, uint32 prev_frame_id) {
     82   return (frame_id == prev_frame_id) || IsNewerFrameId(prev_frame_id, frame_id);
     83 }
     84 
     85 inline bool IsNewerPacketId(uint16 packet_id, uint16 prev_packet_id) {
     86   return (packet_id != prev_packet_id) &&
     87       static_cast<uint16>(packet_id - prev_packet_id) < 0x8000;
     88 }
     89 
     90 inline bool IsNewerSequenceNumber(uint16 sequence_number,
     91                                   uint16 prev_sequence_number) {
     92   // Same function as IsNewerPacketId just different data and name.
     93   return IsNewerPacketId(sequence_number, prev_sequence_number);
     94 }
     95 
     96 // Create a NTP diff from seconds and fractions of seconds; delay_fraction is
     97 // fractions of a second where 0x80000000 is half a second.
     98 inline uint32 ConvertToNtpDiff(uint32 delay_seconds, uint32 delay_fraction) {
     99   return ((delay_seconds & 0x0000FFFF) << 16) +
    100          ((delay_fraction & 0xFFFF0000) >> 16);
    101 }
    102 
    103 inline base::TimeDelta ConvertFromNtpDiff(uint32 ntp_delay) {
    104   uint32 delay_ms = (ntp_delay & 0x0000ffff) * 1000;
    105   delay_ms >>= 16;
    106   delay_ms += ((ntp_delay & 0xffff0000) >> 16) * 1000;
    107   return base::TimeDelta::FromMilliseconds(delay_ms);
    108 }
    109 
    110 inline void ConvertTimeToFractions(int64 time_us,
    111                                    uint32* seconds,
    112                                    uint32* fractions) {
    113   DCHECK_GE(time_us, 0) << "Time must NOT be negative";
    114   *seconds = static_cast<uint32>(time_us / base::Time::kMicrosecondsPerSecond);
    115   *fractions = static_cast<uint32>(
    116       (time_us % base::Time::kMicrosecondsPerSecond) * kMagicFractionalUnit);
    117 }
    118 
    119 inline void ConvertTimeTicksToNtp(const base::TimeTicks& time,
    120                                   uint32* ntp_seconds,
    121                                   uint32* ntp_fractions) {
    122   base::TimeDelta elapsed_since_unix_epoch =
    123       time - base::TimeTicks::UnixEpoch();
    124 
    125   int64 ntp_time_us = elapsed_since_unix_epoch.InMicroseconds() +
    126       (kUnixEpochInNtpSeconds * base::Time::kMicrosecondsPerSecond);
    127 
    128   ConvertTimeToFractions(ntp_time_us, ntp_seconds, ntp_fractions);
    129 }
    130 
    131 inline base::TimeTicks ConvertNtpToTimeTicks(uint32 ntp_seconds,
    132                                              uint32 ntp_fractions) {
    133   int64 ntp_time_us = static_cast<int64>(ntp_seconds) *
    134       base::Time::kMicrosecondsPerSecond +
    135           static_cast<int64>(ntp_fractions) / kMagicFractionalUnit;
    136 
    137   base::TimeDelta elapsed_since_unix_epoch =
    138       base::TimeDelta::FromMicroseconds(ntp_time_us -
    139           (kUnixEpochInNtpSeconds * base::Time::kMicrosecondsPerSecond));
    140   return base::TimeTicks::UnixEpoch() + elapsed_since_unix_epoch;
    141 }
    142 
    143 inline std::string GetAesNonce(uint32 frame_id, const std::string& iv_mask) {
    144   std::string aes_nonce(kAesBlockSize, 0);
    145 
    146   // Serializing frame_id in big-endian order (aes_nonce[8] is the most
    147   // significant byte of frame_id).
    148   aes_nonce[11] = frame_id & 0xff;
    149   aes_nonce[10] = (frame_id >> 8) & 0xff;
    150   aes_nonce[9] = (frame_id >> 16) & 0xff;
    151   aes_nonce[8] = (frame_id >> 24) & 0xff;
    152 
    153   for (size_t i = 0; i < kAesBlockSize; ++i) {
    154     aes_nonce[i] ^= iv_mask[i];
    155   }
    156   return aes_nonce;
    157 }
    158 
    159 inline uint32 GetVideoRtpTimestamp(const base::TimeTicks& time_ticks) {
    160   base::TimeTicks zero_time;
    161   base::TimeDelta recorded_delta = time_ticks - zero_time;
    162   // Timestamp is in 90 KHz for video.
    163   return static_cast<uint32>(recorded_delta.InMilliseconds() * 90);
    164 }
    165 
    166 }  // namespace cast
    167 }  // namespace media
    168 
    169 #endif  // MEDIA_CAST_CAST_DEFINES_H_
    170