c096d6f77ce4c9ef1ec5193ec590cb1016175f6b
[platform/framework/web/crosswalk.git] / src / media / cast / transport / cast_transport_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_TRANSPORT_CAST_TRANSPORT_DEFINES_H_
6 #define MEDIA_CAST_TRANSPORT_CAST_TRANSPORT_DEFINES_H_
7
8 #include <list>
9 #include <map>
10 #include <set>
11
12 #include "base/basictypes.h"
13 #include "base/time/time.h"
14
15 namespace media {
16 namespace cast {
17 namespace transport {
18
19 // TODO(mikhal): Implement and add more types.
20 enum CastTransportStatus {
21   TRANSPORT_UNINITIALIZED = 0,
22   TRANSPORT_INITIALIZED,
23   TRANSPORT_INVALID_CRYPTO_CONFIG,
24   TRANSPORT_SOCKET_ERROR
25 };
26
27 const size_t kMaxIpPacketSize = 1500;
28 // Each uint16 represents one packet id within a cast frame.
29 typedef std::set<uint16> PacketIdSet;
30 // Each uint8 represents one cast frame.
31 typedef std::map<uint8, PacketIdSet> MissingFramesAndPacketsMap;
32
33 // Rtcp defines.
34
35 enum RtcpPacketTypes {
36   kPacketTypeLow = 194,  // SMPTE time-code mapping.
37   kPacketTypeInterArrivalJitterReport = 195,
38   kPacketTypeSenderReport = 200,
39   kPacketTypeReceiverReport = 201,
40   kPacketTypeSdes = 202,
41   kPacketTypeBye = 203,
42   kPacketTypeApplicationDefined = 204,
43   kPacketTypeGenericRtpFeedback = 205,
44   kPacketTypePayloadSpecific = 206,
45   kPacketTypeXr = 207,
46   kPacketTypeHigh = 210,  // Port Mapping.
47 };
48
49 // Each uint16 represents one packet id within a cast frame.
50 typedef std::set<uint16> PacketIdSet;
51 // Each uint8 represents one cast frame.
52 typedef std::map<uint8, PacketIdSet> MissingFramesAndPacketsMap;
53
54
55 class FrameIdWrapHelper {
56  public:
57   FrameIdWrapHelper()
58       : first_(true),
59         frame_id_wrap_count_(0),
60         range_(kLowRange) {}
61
62   uint32 MapTo32bitsFrameId(const uint8 over_the_wire_frame_id) {
63     if (first_) {
64       first_ = false;
65       if (over_the_wire_frame_id == 0xff) {
66         // Special case for startup.
67         return kStartFrameId;
68       }
69     }
70
71     uint32 wrap_count = frame_id_wrap_count_;
72     switch (range_) {
73       case kLowRange:
74         if (over_the_wire_frame_id > kLowRangeThreshold &&
75             over_the_wire_frame_id < kHighRangeThreshold) {
76           range_ = kMiddleRange;
77         }
78         if (over_the_wire_frame_id > kHighRangeThreshold) {
79           // Wrap count was incremented in High->Low transition, but this frame
80           // is 'old', actually from before the wrap count got incremented.
81           --wrap_count;
82         }
83         break;
84       case kMiddleRange:
85         if (over_the_wire_frame_id > kHighRangeThreshold) {
86           range_ = kHighRange;
87         }
88         break;
89       case kHighRange:
90         if (over_the_wire_frame_id < kLowRangeThreshold) {
91           // Wrap-around detected.
92           range_ = kLowRange;
93           ++frame_id_wrap_count_;
94           // Frame triggering wrap-around so wrap count should be incremented as
95           // as well to match |frame_id_wrap_count_|.
96           ++wrap_count;
97         }
98         break;
99     }
100     return (wrap_count << 8) + over_the_wire_frame_id;
101   }
102
103  private:
104   enum Range {
105     kLowRange,
106     kMiddleRange,
107     kHighRange,
108   };
109
110   static const uint8 kLowRangeThreshold = 0x0f;
111   static const uint8 kHighRangeThreshold = 0xf0;
112   static const uint32 kStartFrameId = GG_UINT32_C(0xffffffff);
113
114   bool first_;
115   uint32 frame_id_wrap_count_;
116   Range range_;
117 };
118
119 inline uint32 GetVideoRtpTimestamp(const base::TimeTicks& time_ticks) {
120   base::TimeTicks zero_time;
121   base::TimeDelta recorded_delta = time_ticks - zero_time;
122   // Timestamp is in 90 KHz for video.
123   return static_cast<uint32>(recorded_delta.InMilliseconds() * 90);
124 }
125
126 }  // namespace transport
127 }  // namespace cast
128 }  // namespace media
129
130 #endif  // MEDIA_CAST_TRANSPORT_CAST_TRANSPORT_DEFINES_H_