Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / media / base / audio_timestamp_helper.h
blob1da8b4a7cda6d3ca796d69ea0288663ad2235fb2
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.
5 #ifndef MEDIA_BASE_AUDIO_TIMESTAMP_HELPER_H_
6 #define MEDIA_BASE_AUDIO_TIMESTAMP_HELPER_H_
8 #include "base/time/time.h"
9 #include "media/base/media_export.h"
11 namespace media {
13 // Generates timestamps for a sequence of audio sample frames. This class should
14 // be used any place timestamps need to be calculated for a sequence of audio
15 // samples. It helps avoid timestamps inaccuracies caused by rounding/truncation
16 // in repeated sample count to timestamp conversions.
18 // The class is constructed with samples_per_second information so that it can
19 // convert audio sample frame counts into timestamps. After the object is
20 // constructed, SetBaseTimestamp() must be called to specify the starting
21 // timestamp of the audio sequence. As audio samples are received, their frame
22 // counts are added using AddFrames(). These frame counts are accumulated by
23 // this class so GetTimestamp() can be used to determine the timestamp for the
24 // samples that have been added. GetDuration() calculates the proper duration
25 // values for samples added to the current timestamp. GetFramesToTarget()
26 // determines the number of frames that need to be added/removed from the
27 // accumulated frames to reach a target timestamp.
28 class MEDIA_EXPORT AudioTimestampHelper {
29 public:
30 explicit AudioTimestampHelper(int samples_per_second);
32 // Sets the base timestamp to |base_timestamp| and the sets count to 0.
33 void SetBaseTimestamp(base::TimeDelta base_timestamp);
35 base::TimeDelta base_timestamp() const;
36 int64 frame_count() const { return frame_count_; }
38 // Adds |frame_count| to the frame counter.
39 // Note: SetBaseTimestamp() must be called with a value other than
40 // kNoTimestamp() before this method can be called.
41 void AddFrames(int frame_count);
43 // Get the current timestamp. This value is computed from the base_timestamp()
44 // and the number of sample frames that have been added so far.
45 base::TimeDelta GetTimestamp() const;
47 // Gets the duration if |frame_count| frames were added to the current
48 // timestamp reported by GetTimestamp(). This method ensures that
49 // (GetTimestamp() + GetFrameDuration(n)) will equal the timestamp that
50 // GetTimestamp() will return if AddFrames(n) is called.
51 base::TimeDelta GetFrameDuration(int frame_count) const;
53 // Returns the number of frames needed to reach the target timestamp.
54 // Note: |target| must be >= |base_timestamp_|.
55 int64 GetFramesToTarget(base::TimeDelta target) const;
57 private:
58 base::TimeDelta ComputeTimestamp(int64 frame_count) const;
60 double microseconds_per_frame_;
62 base::TimeDelta base_timestamp_;
64 // Number of frames accumulated by AddFrames() calls.
65 int64 frame_count_;
67 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioTimestampHelper);
70 } // namespace media
72 #endif