summaryrefslogtreecommitdiff
path: root/media/base/audio_timestamp_helper.h
diff options
context:
space:
mode:
authorTorne (Richard Coles) <torne@google.com>2013-03-28 15:31:22 +0000
committerTorne (Richard Coles) <torne@google.com>2013-03-28 15:31:22 +0000
commit2a99a7e74a7f215066514fe81d2bfa6639d9eddd (patch)
tree7c2d04841fcd599fd83b0f0bb1100e1c89a35bae /media/base/audio_timestamp_helper.h
parent61c449bbbb53310a8c041d8cefdd6b01a126cc7e (diff)
downloadchromium_org-2a99a7e74a7f215066514fe81d2bfa6639d9eddd.tar.gz
Merge from Chromium at DEPS revision r190564
This commit was generated by merge_to_master.py. Change-Id: Icadecbce29854b8fa25fd335b2c1949b5ca5d170
Diffstat (limited to 'media/base/audio_timestamp_helper.h')
-rw-r--r--media/base/audio_timestamp_helper.h74
1 files changed, 74 insertions, 0 deletions
diff --git a/media/base/audio_timestamp_helper.h b/media/base/audio_timestamp_helper.h
new file mode 100644
index 0000000000..4b38be7b96
--- /dev/null
+++ b/media/base/audio_timestamp_helper.h
@@ -0,0 +1,74 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MEDIA_BASE_AUDIO_TIMESTAMP_HELPER_H_
+#define MEDIA_BASE_AUDIO_TIMESTAMP_HELPER_H_
+
+#include "base/time.h"
+#include "media/base/media_export.h"
+
+namespace media {
+
+// Generates timestamps for a sequence of audio sample bytes. This class should
+// be used any place timestamps need to be calculated for a sequence of audio
+// samples. It helps avoid timestamps inaccuracies caused by rounding/truncation
+// in repeated sample count to timestamp conversions.
+//
+// The class is constructed with bytes per frame and samples_per_second
+// information so that it can convert audio sample byte counts into timestamps.
+// After the object is constructed, SetBaseTimestamp() must be called to specify
+// the starting timestamp of the audio sequence. As audio samples are received,
+// their byte counts are added to AddBytes(). These byte counts are
+// accumulated by this class so GetTimestamp() can be used to determine the
+// timestamp for the samples that have been added. GetDuration() calculates
+// the proper duration values for samples added to the current timestamp.
+// GetBytesToTarget() determines the number of bytes that need to be
+// added/removed from the accumulated bytes to reach a target timestamp.
+class MEDIA_EXPORT AudioTimestampHelper {
+ public:
+ AudioTimestampHelper(int bytes_per_frame, int samples_per_second);
+
+ // Sets the base timestamp to |base_timestamp| and the sets count to 0.
+ void SetBaseTimestamp(base::TimeDelta base_timestamp);
+
+ base::TimeDelta base_timestamp() const;
+
+ // Adds sample bytes to the frame counter.
+ //
+ // Note: SetBaseTimestamp() must be called with a value other than
+ // kNoTimestamp() before this method can be called.
+ void AddBytes(int byte_count);
+
+ // Get the current timestamp. This value is computed from the base_timestamp()
+ // and the number of sample bytes that have been added so far.
+ base::TimeDelta GetTimestamp() const;
+
+ // Gets the duration if |byte_count| bytes were added to the current
+ // timestamp reported by GetTimestamp(). This method ensures that
+ // (GetTimestamp() + GetDuration(n)) will equal the timestamp that
+ // GetTimestamp() will return if AddBytes(n) is called.
+ base::TimeDelta GetDuration(int byte_count) const;
+
+ // Returns the number of bytes needed to reach the target timestamp.
+ //
+ // Note: |target| must be >= |base_timestamp_|.
+ int64 GetBytesToTarget(base::TimeDelta target) const;
+
+ private:
+ base::TimeDelta ComputeTimestamp(int64 frame_count) const;
+
+ int bytes_per_frame_;
+ double microseconds_per_frame_;
+
+ base::TimeDelta base_timestamp_;
+
+ // Number of frames accumulated by byte counts passed to AddBytes() calls.
+ int64 frame_count_;
+
+ DISALLOW_IMPLICIT_CONSTRUCTORS(AudioTimestampHelper);
+};
+
+} // namespace media
+
+#endif