DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
audio_timestamp_helper.h
1 // Copyright (c) 2012 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_AUDIO_TIMESTAMP_HELPER_H_
6 #define MEDIA_BASE_AUDIO_TIMESTAMP_HELPER_H_
7 
8 #include <stdint.h>
9 
10 #include "packager/base/macros.h"
11 
12 namespace edash_packager {
13 namespace media {
14 
15 // Generates timestamps for a sequence of audio sample frames. This class should
16 // be used any place timestamps need to be calculated for a sequence of audio
17 // samples. It helps avoid timestamps inaccuracies caused by rounding/truncation
18 // in repeated sample count to timestamp conversions.
19 //
20 // The class is constructed with samples_per_second information so that it can
21 // convert audio sample frame counts into timestamps. After the object is
22 // constructed, SetBaseTimestamp() must be called to specify the starting
23 // timestamp of the audio sequence. As audio samples are received, their frame
24 // counts are added using AddFrames(). These frame counts are accumulated by
25 // this class so GetTimestamp() can be used to determine the timestamp for the
26 // samples that have been added. GetDuration() calculates the proper duration
27 // values for samples added to the current timestamp. GetFramesToTarget()
28 // determines the number of frames that need to be added/removed from the
29 // accumulated frames to reach a target timestamp.
31  public:
32  explicit AudioTimestampHelper(uint32_t timescale,
33  uint32_t samples_per_second);
34 
35  // Sets the base timestamp to |base_timestamp| and the sets count to 0.
36  void SetBaseTimestamp(int64_t base_timestamp);
37 
38  int64_t base_timestamp() const;
39  int64_t frame_count() const { return frame_count_; }
40 
41  // Adds |frame_count| to the frame counter.
42  // Note: SetBaseTimestamp() must be called with a value other than
43  // kNoTimestamp() before this method can be called.
44  void AddFrames(int64_t frame_count);
45 
46  // Get the current timestamp. This value is computed from the base_timestamp()
47  // and the number of sample frames that have been added so far.
48  int64_t GetTimestamp() const;
49 
50  // Gets the duration if |frame_count| frames were added to the current
51  // timestamp reported by GetTimestamp(). This method ensures that
52  // (GetTimestamp() + GetFrameDuration(n)) will equal the timestamp that
53  // GetTimestamp() will return if AddFrames(n) is called.
54  int64_t GetFrameDuration(int64_t frame_count) const;
55 
56  // Returns the number of frames needed to reach the target timestamp.
57  // Note: |target| must be >= |base_timestamp_|.
58  int64_t GetFramesToTarget(int64_t target) const;
59 
60  private:
61  int64_t ComputeTimestamp(int64_t frame_count) const;
62 
63  double ticks_per_frame_;
64 
65  int64_t base_timestamp_;
66 
67  // Number of frames accumulated by AddFrames() calls.
68  int64_t frame_count_;
69 
70  DISALLOW_IMPLICIT_CONSTRUCTORS(AudioTimestampHelper);
71 };
72 
73 } // namespace media
74 } // namespace edash_packager
75 
76 #endif