Home | History | Annotate | Download | only in base
      1 // Copyright 2014 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_BASE_TEXT_RANGES_H_
      6 #define MEDIA_BASE_TEXT_RANGES_H_
      7 
      8 #include <map>
      9 
     10 #include "base/macros.h"
     11 #include "base/time/time.h"
     12 #include "media/base/media_export.h"
     13 
     14 namespace media {
     15 
     16 // Helper class used by the TextRenderer to filter out text cues that
     17 // have already been passed downstream.
     18 class MEDIA_EXPORT TextRanges {
     19  public:
     20   TextRanges();
     21   ~TextRanges();
     22 
     23   // Reset the current range pointer, such that we bind to a new range
     24   // (either one that exists already, or one that is freshly-created)
     25   // during the next AddCue().
     26   void Reset();
     27 
     28   // Given a cue with starting timestamp |start_time|, add its start
     29   // time to the time ranges. (Note that following a Reset(), cue
     30   // times are assumed to be monotonically increasing.) If this time
     31   // has already been added to the time ranges, then AddCue() returns
     32   // false and clients should not push the cue downstream. Otherwise,
     33   // the time is added to the time ranges and AddCue() returns true,
     34   // meaning that the cue should be pushed downstream.
     35   bool AddCue(base::TimeDelta start_time);
     36 
     37   // Returns a count of the number of time ranges, intended for use by
     38   // the unit test module to vet proper time range merge behavior.
     39   size_t RangeCountForTesting() const;
     40 
     41  private:
     42   // Describes a range of times for cues that have already been
     43   // pushed downstream.
     44   class Range {
     45    public:
     46     // Initialize last_time count.
     47     void ResetCount(base::TimeDelta start_time);
     48 
     49     // Set last_time and associated counts.
     50     void SetLastTime(base::TimeDelta last_time);
     51 
     52     // Adjust time range state to mark the cue as having been seen,
     53     // returning true if we have not seen |start_time| already and
     54     // false otherwise.
     55     bool AddCue(base::TimeDelta start_time);
     56 
     57     // Returns the value of the last time in the range.
     58     base::TimeDelta last_time() const;
     59 
     60    private:
     61     // The last timestamp of this range.
     62     base::TimeDelta last_time_;
     63 
     64     // The number of cues we have detected so far, for this range,
     65     // whose timestamp matches last_time.
     66     int max_count_;
     67 
     68     // The number of cues we have seen since the most recent Reset(),
     69     // whose timestamp matches last_time.
     70     int count_;
     71   };
     72 
     73   typedef std::map<base::TimeDelta, Range> RangeMap;
     74 
     75   // NewRange() is used to create a new time range when AddCue() is
     76   // called immediately following a Reset(), and no existing time
     77   // range contains the indicated |start_time| of the cue.
     78   void NewRange(base::TimeDelta start_time);
     79 
     80   // Coalesce curr_range with the range that immediately follows.
     81   void Merge(Range& curr_range, const RangeMap::iterator& next_range_itr);
     82 
     83   // The collection of time ranges, each of which is bounded
     84   // (inclusive) by the key and Range::last_time.
     85   RangeMap range_map_;
     86 
     87   // The time range to which we bind following a Reset().
     88   RangeMap::iterator curr_range_itr_;
     89 
     90   DISALLOW_COPY_AND_ASSIGN(TextRanges);
     91 };
     92 
     93 }  // namespace media
     94 
     95 #endif  // MEDIA_BASE_TEXT_RANGES_H_
     96