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"
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
{
30 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;
37 // Adds |frame_count| to the frame counter.
38 // Note: SetBaseTimestamp() must be called with a value other than
39 // kNoTimestamp() before this method can be called.
40 void AddFrames(int frame_count
);
42 // Get the current timestamp. This value is computed from the base_timestamp()
43 // and the number of sample frames that have been added so far.
44 base::TimeDelta
GetTimestamp() const;
46 // Gets the duration if |frame_count| frames were added to the current
47 // timestamp reported by GetTimestamp(). This method ensures that
48 // (GetTimestamp() + GetFrameDuration(n)) will equal the timestamp that
49 // GetTimestamp() will return if AddFrames(n) is called.
50 base::TimeDelta
GetFrameDuration(int frame_count
) const;
52 // Returns the number of frames needed to reach the target timestamp.
53 // Note: |target| must be >= |base_timestamp_|.
54 int64
GetFramesToTarget(base::TimeDelta target
) const;
57 base::TimeDelta
ComputeTimestamp(int64 frame_count
) const;
59 double microseconds_per_frame_
;
61 base::TimeDelta base_timestamp_
;
63 // Number of frames accumulated by AddFrames() calls.
66 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioTimestampHelper
);