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_
9 #include "media/base/media_export.h"
13 // Generates timestamps for a sequence of audio sample bytes. 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 bytes per frame and samples_per_second
19 // information so that it can convert audio sample byte counts into timestamps.
20 // After the object is constructed, SetBaseTimestamp() must be called to specify
21 // the starting timestamp of the audio sequence. As audio samples are received,
22 // their byte counts are added to AddBytes(). These byte counts are
23 // accumulated by this class so GetTimestamp() can be used to determine the
24 // timestamp for the samples that have been added. GetDuration() calculates
25 // the proper duration values for samples added to the current timestamp.
26 // GetBytesToTarget() determines the number of bytes that need to be
27 // added/removed from the accumulated bytes to reach a target timestamp.
28 class MEDIA_EXPORT AudioTimestampHelper
{
30 AudioTimestampHelper(int bytes_per_frame
, 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 sample bytes 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 AddBytes(int byte_count
);
43 // Get the current timestamp. This value is computed from the base_timestamp()
44 // and the number of sample bytes that have been added so far.
45 base::TimeDelta
GetTimestamp() const;
47 // Gets the duration if |byte_count| bytes were added to the current
48 // timestamp reported by GetTimestamp(). This method ensures that
49 // (GetTimestamp() + GetDuration(n)) will equal the timestamp that
50 // GetTimestamp() will return if AddBytes(n) is called.
51 base::TimeDelta
GetDuration(int byte_count
) const;
53 // Returns the number of bytes needed to reach the target timestamp.
55 // Note: |target| must be >= |base_timestamp_|.
56 int64
GetBytesToTarget(base::TimeDelta target
) const;
59 base::TimeDelta
ComputeTimestamp(int64 frame_count
) const;
62 double microseconds_per_frame_
;
64 base::TimeDelta base_timestamp_
;
66 // Number of frames accumulated by byte counts passed to AddBytes() calls.
69 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioTimestampHelper
);