Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / media / cast / cast_defines.h
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 int64 kSkippedFramesCheckPeriodkMs = 10000;
26 const uint32 kStartFrameId = UINT32_C(0xffffffff);
27
28 // Number of skipped frames threshold in fps (as configured) per period above.
29 const int kSkippedFramesThreshold = 3;
30 const size_t kMaxIpPacketSize = 1500;
31 const int kStartRttMs = 20;
32 const int64 kCastMessageUpdateIntervalMs = 33;
33 const int64 kNackRepeatIntervalMs = 30;
34
35 enum CastInitializationStatus {
36   STATUS_AUDIO_UNINITIALIZED,
37   STATUS_VIDEO_UNINITIALIZED,
38   STATUS_AUDIO_INITIALIZED,
39   STATUS_VIDEO_INITIALIZED,
40   STATUS_INVALID_CAST_ENVIRONMENT,
41   STATUS_INVALID_CRYPTO_CONFIGURATION,
42   STATUS_UNSUPPORTED_AUDIO_CODEC,
43   STATUS_UNSUPPORTED_VIDEO_CODEC,
44   STATUS_INVALID_AUDIO_CONFIGURATION,
45   STATUS_INVALID_VIDEO_CONFIGURATION,
46   STATUS_GPU_ACCELERATION_NOT_SUPPORTED,
47   STATUS_GPU_ACCELERATION_ERROR,
48 };
49
50 enum DefaultSettings {
51   kDefaultAudioEncoderBitrate = 0,  // This means "auto," and may mean VBR.
52   kDefaultAudioSamplingRate = 48000,
53   kDefaultMaxQp = 56,
54   kDefaultMinQp = 4,
55   kDefaultMaxFrameRate = 30,
56   kDefaultNumberOfVideoBuffers = 1,
57   kDefaultRtcpIntervalMs = 500,
58   kDefaultRtpHistoryMs = 1000,
59   kDefaultRtpMaxDelayMs = 100,
60 };
61
62 enum PacketType {
63   kNewPacket,
64   kNewPacketCompletingFrame,
65   kDuplicatePacket,
66   kTooOldPacket,
67 };
68
69 const uint16 kRtcpCastAllPacketsLost = 0xffff;
70
71 const size_t kMinLengthOfRtcp = 8;
72
73 // Basic RTP header + cast header.
74 const size_t kMinLengthOfRtp = 12 + 6;
75
76 // Each uint16 represents one packet id within a cast frame.
77 typedef std::set<uint16> PacketIdSet;
78 // Each uint8 represents one cast frame.
79 typedef std::map<uint8, PacketIdSet> MissingFramesAndPacketsMap;
80
81 // TODO(pwestin): Re-factor the functions bellow into a class with static
82 // methods.
83
84 // January 1970, in NTP seconds.
85 // Network Time Protocol (NTP), which is in seconds relative to 0h UTC on
86 // 1 January 1900.
87 static const int64 kUnixEpochInNtpSeconds = INT64_C(2208988800);
88
89 // Magic fractional unit. Used to convert time (in microseconds) to/from
90 // fractional NTP seconds.
91 static const double kMagicFractionalUnit = 4.294967296E3;
92
93 // The maximum number of Cast receiver events to keep in history for the
94 // purpose of sending the events through RTCP.
95 // The number chosen should be more than the number of events that can be
96 // stored in a RTCP packet.
97 static const size_t kReceiverRtcpEventHistorySize = 512;
98
99 inline bool IsNewerFrameId(uint32 frame_id, uint32 prev_frame_id) {
100   return (frame_id != prev_frame_id) &&
101          static_cast<uint32>(frame_id - prev_frame_id) < 0x80000000;
102 }
103
104 inline bool IsNewerRtpTimestamp(uint32 timestamp, uint32 prev_timestamp) {
105   return (timestamp != prev_timestamp) &&
106          static_cast<uint32>(timestamp - prev_timestamp) < 0x80000000;
107 }
108
109 inline bool IsOlderFrameId(uint32 frame_id, uint32 prev_frame_id) {
110   return (frame_id == prev_frame_id) || IsNewerFrameId(prev_frame_id, frame_id);
111 }
112
113 inline bool IsNewerPacketId(uint16 packet_id, uint16 prev_packet_id) {
114   return (packet_id != prev_packet_id) &&
115          static_cast<uint16>(packet_id - prev_packet_id) < 0x8000;
116 }
117
118 inline bool IsNewerSequenceNumber(uint16 sequence_number,
119                                   uint16 prev_sequence_number) {
120   // Same function as IsNewerPacketId just different data and name.
121   return IsNewerPacketId(sequence_number, prev_sequence_number);
122 }
123
124 // Create a NTP diff from seconds and fractions of seconds; delay_fraction is
125 // fractions of a second where 0x80000000 is half a second.
126 inline uint32 ConvertToNtpDiff(uint32 delay_seconds, uint32 delay_fraction) {
127   return ((delay_seconds & 0x0000FFFF) << 16) +
128          ((delay_fraction & 0xFFFF0000) >> 16);
129 }
130
131 inline base::TimeDelta ConvertFromNtpDiff(uint32 ntp_delay) {
132   uint32 delay_ms = (ntp_delay & 0x0000ffff) * 1000;
133   delay_ms >>= 16;
134   delay_ms += ((ntp_delay & 0xffff0000) >> 16) * 1000;
135   return base::TimeDelta::FromMilliseconds(delay_ms);
136 }
137
138 inline void ConvertTimeToFractions(int64 time_us,
139                                    uint32* seconds,
140                                    uint32* fractions) {
141   DCHECK_GE(time_us, 0) << "Time must NOT be negative";
142   *seconds = static_cast<uint32>(time_us / base::Time::kMicrosecondsPerSecond);
143   *fractions = static_cast<uint32>(
144       (time_us % base::Time::kMicrosecondsPerSecond) * kMagicFractionalUnit);
145 }
146
147 inline void ConvertTimeTicksToNtp(const base::TimeTicks& time,
148                                   uint32* ntp_seconds,
149                                   uint32* ntp_fractions) {
150   base::TimeDelta elapsed_since_unix_epoch =
151       time - base::TimeTicks::UnixEpoch();
152
153   int64 ntp_time_us =
154       elapsed_since_unix_epoch.InMicroseconds() +
155       (kUnixEpochInNtpSeconds * base::Time::kMicrosecondsPerSecond);
156
157   ConvertTimeToFractions(ntp_time_us, ntp_seconds, ntp_fractions);
158 }
159
160 inline base::TimeTicks ConvertNtpToTimeTicks(uint32 ntp_seconds,
161                                              uint32 ntp_fractions) {
162   int64 ntp_time_us =
163       static_cast<int64>(ntp_seconds) * base::Time::kMicrosecondsPerSecond +
164       static_cast<int64>(ntp_fractions) / kMagicFractionalUnit;
165
166   base::TimeDelta elapsed_since_unix_epoch = base::TimeDelta::FromMicroseconds(
167       ntp_time_us -
168       (kUnixEpochInNtpSeconds * base::Time::kMicrosecondsPerSecond));
169   return base::TimeTicks::UnixEpoch() + elapsed_since_unix_epoch;
170 }
171
172 inline uint32 GetVideoRtpTimestamp(const base::TimeTicks& time_ticks) {
173   base::TimeTicks zero_time;
174   base::TimeDelta recorded_delta = time_ticks - zero_time;
175   // Timestamp is in 90 KHz for video.
176   return static_cast<uint32>(recorded_delta.InMilliseconds() * 90);
177 }
178
179 class RtpSenderStatistics {
180  public:
181   explicit RtpSenderStatistics(int frequency)
182       : frequency_(frequency),
183         rtp_timestamp_(0) {
184     memset(&sender_info_, 0, sizeof(sender_info_));
185   }
186
187   ~RtpSenderStatistics() {}
188
189   void UpdateInfo(const base::TimeTicks& now) {
190     // Update RTP timestamp and return last stored statistics.
191     uint32 ntp_seconds = 0;
192     uint32 ntp_fraction = 0;
193     uint32 rtp_timestamp = 0;
194     if (rtp_timestamp_ > 0) {
195       base::TimeDelta time_since_last_send = now - time_sent_;
196       rtp_timestamp = rtp_timestamp_ + time_since_last_send.InMilliseconds() *
197                                            (frequency_ / 1000);
198       // Update NTP time to current time.
199       ConvertTimeTicksToNtp(now, &ntp_seconds, &ntp_fraction);
200     }
201     // Populate sender info.
202     sender_info_.rtp_timestamp = rtp_timestamp;
203     sender_info_.ntp_seconds = ntp_seconds;
204     sender_info_.ntp_fraction = ntp_fraction;
205   }
206
207   transport::RtcpSenderInfo sender_info() const {
208     return sender_info_;
209   }
210
211   void Store(transport::RtcpSenderInfo sender_info,
212              base::TimeTicks time_sent,
213              uint32 rtp_timestamp) {
214     sender_info_ = sender_info;
215     time_sent_ = time_sent;
216     rtp_timestamp_ = rtp_timestamp;
217 }
218
219  private:
220   int frequency_;
221   transport::RtcpSenderInfo sender_info_;
222   base::TimeTicks time_sent_;
223   uint32 rtp_timestamp_;
224
225   DISALLOW_COPY_AND_ASSIGN(RtpSenderStatistics);
226 };
227
228 }  // namespace cast
229 }  // namespace media
230
231 #endif  // MEDIA_CAST_CAST_DEFINES_H_