audio_timestamp_helper.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #ifndef MEDIA_BASE_AUDIO_TIMESTAMP_HELPER_H_
  5. #define MEDIA_BASE_AUDIO_TIMESTAMP_HELPER_H_
  6. #include <stdint.h>
  7. #include "base/time/time.h"
  8. #include "media/base/media_export.h"
  9. namespace media {
  10. // Generates timestamps for a sequence of audio sample frames. This class should
  11. // be used any place timestamps need to be calculated for a sequence of audio
  12. // samples. It helps avoid timestamps inaccuracies caused by rounding/truncation
  13. // in repeated sample count to timestamp conversions.
  14. //
  15. // The class is constructed with samples_per_second information so that it can
  16. // convert audio sample frame counts into timestamps. After the object is
  17. // constructed, SetBaseTimestamp() must be called to specify the starting
  18. // timestamp of the audio sequence. As audio samples are received, their frame
  19. // counts are added using AddFrames(). These frame counts are accumulated by
  20. // this class so GetTimestamp() can be used to determine the timestamp for the
  21. // samples that have been added. GetDuration() calculates the proper duration
  22. // values for samples added to the current timestamp. GetFramesToTarget()
  23. // determines the number of frames that need to be added/removed from the
  24. // accumulated frames to reach a target timestamp.
  25. class MEDIA_EXPORT AudioTimestampHelper {
  26. public:
  27. // Returns the time duration of the given number of frames of audio with the
  28. // given sample rate (in samples per second).
  29. static base::TimeDelta FramesToTime(int64_t frames, int samples_per_second);
  30. // Returns the number of frames in the given duration of audio with the given
  31. // sample rate (in samples per second).
  32. static int64_t TimeToFrames(base::TimeDelta time, int samples_per_second);
  33. AudioTimestampHelper() = delete;
  34. explicit AudioTimestampHelper(int samples_per_second);
  35. AudioTimestampHelper(const AudioTimestampHelper&) = delete;
  36. AudioTimestampHelper& operator=(const AudioTimestampHelper&) = delete;
  37. // Sets the base timestamp to |base_timestamp| and the sets count to 0.
  38. void SetBaseTimestamp(base::TimeDelta base_timestamp);
  39. base::TimeDelta base_timestamp() const;
  40. int64_t frame_count() const { return frame_count_; }
  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(int frame_count);
  45. // Get the current timestamp. This value is computed from the base_timestamp()
  46. // and the number of sample frames that have been added so far.
  47. base::TimeDelta GetTimestamp() const;
  48. // Gets the duration of |frame_count| frames by calculating the difference
  49. // between the current timestamp and what the timestamp would be if
  50. // |frame_count| frames were added.
  51. 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_t GetFramesToTarget(base::TimeDelta target) const;
  55. private:
  56. base::TimeDelta ComputeTimestamp(int64_t frame_count) const;
  57. double microseconds_per_frame_;
  58. base::TimeDelta base_timestamp_;
  59. // Number of frames accumulated by AddFrames() calls.
  60. int64_t frame_count_;
  61. };
  62. } // namespace media
  63. #endif // MEDIA_BASE_AUDIO_TIMESTAMP_HELPER_H_