Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(197)

Unified Diff: media/base/audio_buffer.cc

Issue 261533002: Remove AudioBuffer::set_duration(), instead base on frames. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix divide by zero case. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « media/base/audio_buffer.h ('k') | media/base/audio_buffer_converter.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: media/base/audio_buffer.cc
diff --git a/media/base/audio_buffer.cc b/media/base/audio_buffer.cc
index 4b972b90a1abda47c363b0cd46c7c8362ca60ee2..08bb1e32b8ea5a95827b8726d8ffe5781b470b96 100644
--- a/media/base/audio_buffer.cc
+++ b/media/base/audio_buffer.cc
@@ -11,6 +11,12 @@
namespace media {
+static base::TimeDelta CalculateDuration(int frames, double sample_rate) {
+ DCHECK_GT(sample_rate, 0);
+ return base::TimeDelta::FromMicroseconds(
+ frames * base::Time::kMicrosecondsPerSecond / sample_rate);
+}
+
AudioBuffer::AudioBuffer(SampleFormat sample_format,
ChannelLayout channel_layout,
int channel_count,
@@ -18,8 +24,7 @@ AudioBuffer::AudioBuffer(SampleFormat sample_format,
int frame_count,
bool create_buffer,
const uint8* const* data,
- const base::TimeDelta timestamp,
- const base::TimeDelta duration)
+ const base::TimeDelta timestamp)
: sample_format_(sample_format),
channel_layout_(channel_layout),
channel_count_(channel_count),
@@ -28,7 +33,9 @@ AudioBuffer::AudioBuffer(SampleFormat sample_format,
trim_start_(0),
end_of_stream_(!create_buffer && data == NULL && frame_count == 0),
timestamp_(timestamp),
- duration_(duration) {
+ duration_(end_of_stream_
+ ? base::TimeDelta()
+ : CalculateDuration(adjusted_frame_count_, sample_rate_)) {
CHECK_GE(channel_count_, 0);
CHECK_LE(channel_count_, limits::kMaxChannels);
CHECK_GE(frame_count, 0);
@@ -91,8 +98,7 @@ scoped_refptr<AudioBuffer> AudioBuffer::CopyFrom(
int sample_rate,
int frame_count,
const uint8* const* data,
- const base::TimeDelta timestamp,
- const base::TimeDelta duration) {
+ const base::TimeDelta timestamp) {
// If you hit this CHECK you likely have a bug in a demuxer. Go fix it.
CHECK_GT(frame_count, 0); // Otherwise looks like an EOF buffer.
CHECK(data[0]);
@@ -103,8 +109,7 @@ scoped_refptr<AudioBuffer> AudioBuffer::CopyFrom(
frame_count,
true,
data,
- timestamp,
- duration));
+ timestamp));
}
// static
@@ -122,7 +127,6 @@ scoped_refptr<AudioBuffer> AudioBuffer::CreateBuffer(
frame_count,
true,
NULL,
- kNoTimestamp(),
kNoTimestamp()));
}
@@ -132,8 +136,7 @@ scoped_refptr<AudioBuffer> AudioBuffer::CreateEmptyBuffer(
int channel_count,
int sample_rate,
int frame_count,
- const base::TimeDelta timestamp,
- const base::TimeDelta duration) {
+ const base::TimeDelta timestamp) {
CHECK_GT(frame_count, 0); // Otherwise looks like an EOF buffer.
// Since data == NULL, format doesn't matter.
return make_scoped_refptr(new AudioBuffer(kSampleFormatF32,
@@ -143,8 +146,7 @@ scoped_refptr<AudioBuffer> AudioBuffer::CreateEmptyBuffer(
frame_count,
false,
NULL,
- timestamp,
- duration));
+ timestamp));
}
// static
@@ -156,7 +158,6 @@ scoped_refptr<AudioBuffer> AudioBuffer::CreateEOSBuffer() {
0,
false,
NULL,
- kNoTimestamp(),
kNoTimestamp()));
}
@@ -246,33 +247,23 @@ void AudioBuffer::TrimStart(int frames_to_trim) {
CHECK_GE(frames_to_trim, 0);
CHECK_LE(frames_to_trim, adjusted_frame_count_);
- // Adjust timestamp_ and duration_ to reflect the smaller number of frames.
- double offset = static_cast<double>(duration_.InMicroseconds()) *
- frames_to_trim / adjusted_frame_count_;
- base::TimeDelta offset_as_time =
- base::TimeDelta::FromMicroseconds(static_cast<int64>(offset));
- timestamp_ += offset_as_time;
- duration_ -= offset_as_time;
-
- // Finally adjust the number of frames in this buffer and where the start
- // really is.
+ // Adjust the number of frames in this buffer and where the start really is.
adjusted_frame_count_ -= frames_to_trim;
trim_start_ += frames_to_trim;
+
+ // Adjust timestamp_ and duration_ to reflect the smaller number of frames.
+ const base::TimeDelta old_duration = duration_;
+ duration_ = CalculateDuration(adjusted_frame_count_, sample_rate_);
+ timestamp_ += old_duration - duration_;
}
void AudioBuffer::TrimEnd(int frames_to_trim) {
CHECK_GE(frames_to_trim, 0);
CHECK_LE(frames_to_trim, adjusted_frame_count_);
- // Adjust duration_ only to reflect the smaller number of frames.
- double offset = static_cast<double>(duration_.InMicroseconds()) *
- frames_to_trim / adjusted_frame_count_;
- base::TimeDelta offset_as_time =
- base::TimeDelta::FromMicroseconds(static_cast<int64>(offset));
- duration_ -= offset_as_time;
-
- // Finally adjust the number of frames in this buffer.
+ // Adjust the number of frames and duration for this buffer.
adjusted_frame_count_ -= frames_to_trim;
+ duration_ = CalculateDuration(adjusted_frame_count_, sample_rate_);
}
} // namespace media
« no previous file with comments | « media/base/audio_buffer.h ('k') | media/base/audio_buffer_converter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698