Home | History | Annotate | Download | only in net
      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_NET_CAST_NET_DEFINES_H_
      6 #define MEDIA_CAST_NET_CAST_NET_DEFINES_H_
      7 
      8 #include "base/basictypes.h"
      9 
     10 namespace media {
     11 namespace cast {
     12 
     13 class FrameIdWrapHelper {
     14  public:
     15   FrameIdWrapHelper()
     16       : first_(true),
     17         frame_id_wrap_count_(0),
     18         range_(kLowRange) {}
     19 
     20   uint32 MapTo32bitsFrameId(const uint8 over_the_wire_frame_id) {
     21     if (first_) {
     22       first_ = false;
     23       if (over_the_wire_frame_id == 0xff) {
     24         // Special case for startup.
     25         return kStartFrameId;
     26       }
     27     }
     28 
     29     uint32 wrap_count = frame_id_wrap_count_;
     30     switch (range_) {
     31       case kLowRange:
     32         if (over_the_wire_frame_id > kLowRangeThreshold &&
     33             over_the_wire_frame_id < kHighRangeThreshold) {
     34           range_ = kMiddleRange;
     35         }
     36         if (over_the_wire_frame_id > kHighRangeThreshold) {
     37           // Wrap count was incremented in High->Low transition, but this frame
     38           // is 'old', actually from before the wrap count got incremented.
     39           --wrap_count;
     40         }
     41         break;
     42       case kMiddleRange:
     43         if (over_the_wire_frame_id > kHighRangeThreshold) {
     44           range_ = kHighRange;
     45         }
     46         break;
     47       case kHighRange:
     48         if (over_the_wire_frame_id < kLowRangeThreshold) {
     49           // Wrap-around detected.
     50           range_ = kLowRange;
     51           ++frame_id_wrap_count_;
     52           // Frame triggering wrap-around so wrap count should be incremented as
     53           // as well to match |frame_id_wrap_count_|.
     54           ++wrap_count;
     55         }
     56         break;
     57     }
     58     return (wrap_count << 8) + over_the_wire_frame_id;
     59   }
     60 
     61  private:
     62   enum Range {
     63     kLowRange,
     64     kMiddleRange,
     65     kHighRange,
     66   };
     67 
     68   static const uint8 kLowRangeThreshold = 0x0f;
     69   static const uint8 kHighRangeThreshold = 0xf0;
     70   static const uint32 kStartFrameId = GG_UINT32_C(0xffffffff);
     71 
     72   bool first_;
     73   uint32 frame_id_wrap_count_;
     74   Range range_;
     75 };
     76 
     77 
     78 }  // namespace cast
     79 }  // namespace media
     80 
     81 #endif  // MEDIA_CAST_NET_CAST_NET_DEFINES_H_
     82