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

Side by Side Diff: media/audio/null_audio_sink.cc

Issue 10837032: Fix playback rate w/o audio. Remove unused SetPlaybackRate(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 4 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « media/audio/null_audio_sink.h ('k') | media/base/audio_renderer_sink.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/audio/null_audio_sink.h" 5 #include "media/audio/null_audio_sink.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/stringprintf.h" 8 #include "base/stringprintf.h"
9 #include "base/sys_byteorder.h" 9 #include "base/sys_byteorder.h"
10 #include "base/threading/platform_thread.h" 10 #include "base/threading/platform_thread.h"
11 11
12 namespace media { 12 namespace media {
13 13
14 NullAudioSink::NullAudioSink() 14 NullAudioSink::NullAudioSink()
15 : initialized_(false), 15 : initialized_(false),
16 playback_rate_(0.0),
17 playing_(false), 16 playing_(false),
18 callback_(NULL), 17 callback_(NULL),
19 thread_("NullAudioThread"), 18 thread_("NullAudioThread"),
20 hash_audio_for_testing_(false) { 19 hash_audio_for_testing_(false) {
21 } 20 }
22 21
23 void NullAudioSink::Initialize(const AudioParameters& params, 22 void NullAudioSink::Initialize(const AudioParameters& params,
24 RenderCallback* callback) { 23 RenderCallback* callback) {
25 DCHECK(!initialized_); 24 DCHECK(!initialized_);
26 params_ = params; 25 params_ = params;
(...skipping 28 matching lines...) Expand all
55 } 54 }
56 55
57 void NullAudioSink::Play() { 56 void NullAudioSink::Play() {
58 SetPlaying(true); 57 SetPlaying(true);
59 } 58 }
60 59
61 void NullAudioSink::Pause(bool /* flush */) { 60 void NullAudioSink::Pause(bool /* flush */) {
62 SetPlaying(false); 61 SetPlaying(false);
63 } 62 }
64 63
65 void NullAudioSink::SetPlaybackRate(float rate) {
66 base::AutoLock auto_lock(lock_);
67 playback_rate_ = rate;
68 }
69
70 bool NullAudioSink::SetVolume(double volume) { 64 bool NullAudioSink::SetVolume(double volume) {
71 // Audio is always muted. 65 // Audio is always muted.
72 return volume == 0.0; 66 return volume == 0.0;
73 } 67 }
74 68
75 void NullAudioSink::SetPlaying(bool is_playing) { 69 void NullAudioSink::SetPlaying(bool is_playing) {
76 base::AutoLock auto_lock(lock_); 70 base::AutoLock auto_lock(lock_);
77 playing_ = is_playing; 71 playing_ = is_playing;
78 } 72 }
79 73
80 NullAudioSink::~NullAudioSink() { 74 NullAudioSink::~NullAudioSink() {
81 DCHECK(!thread_.IsRunning()); 75 DCHECK(!thread_.IsRunning());
82 for (size_t i = 0; i < audio_data_.size(); ++i) 76 for (size_t i = 0; i < audio_data_.size(); ++i)
83 delete [] audio_data_[i]; 77 delete [] audio_data_[i];
84 } 78 }
85 79
86 void NullAudioSink::FillBufferTask() { 80 void NullAudioSink::FillBufferTask() {
87 base::AutoLock auto_lock(lock_); 81 base::AutoLock auto_lock(lock_);
88 82
89 base::TimeDelta delay; 83 base::TimeDelta delay;
90 // Only consume buffers when actually playing. 84 // Only consume buffers when actually playing.
91 if (playing_) { 85 if (playing_) {
92 DCHECK_GT(playback_rate_, 0.0f);
93 int requested_frames = params_.frames_per_buffer(); 86 int requested_frames = params_.frames_per_buffer();
94 int frames_received = callback_->Render(audio_data_, requested_frames, 0); 87 int frames_received = callback_->Render(audio_data_, requested_frames, 0);
95 int frames_per_millisecond = 88 int frames_per_millisecond =
96 params_.sample_rate() / base::Time::kMillisecondsPerSecond; 89 params_.sample_rate() / base::Time::kMillisecondsPerSecond;
97 90
98 if (hash_audio_for_testing_ && frames_received > 0) { 91 if (hash_audio_for_testing_ && frames_received > 0) {
99 DCHECK_EQ(sizeof(float), sizeof(uint32)); 92 DCHECK_EQ(sizeof(float), sizeof(uint32));
100 int channels = audio_data_.size(); 93 int channels = audio_data_.size();
101 for (int channel_idx = 0; channel_idx < channels; ++channel_idx) { 94 for (int channel_idx = 0; channel_idx < channels; ++channel_idx) {
102 float* channel = audio_data_[channel_idx]; 95 float* channel = audio_data_[channel_idx];
103 for (int frame_idx = 0; frame_idx < frames_received; frame_idx++) { 96 for (int frame_idx = 0; frame_idx < frames_received; frame_idx++) {
104 // Convert float to uint32 w/o conversion loss. 97 // Convert float to uint32 w/o conversion loss.
105 uint32 frame = base::ByteSwapToLE32( 98 uint32 frame = base::ByteSwapToLE32(
106 bit_cast<uint32>(channel[frame_idx])); 99 bit_cast<uint32>(channel[frame_idx]));
107 base::MD5Update( 100 base::MD5Update(
108 &md5_channel_contexts_[channel_idx], base::StringPiece( 101 &md5_channel_contexts_[channel_idx], base::StringPiece(
109 reinterpret_cast<char*>(&frame), sizeof(frame))); 102 reinterpret_cast<char*>(&frame), sizeof(frame)));
110 } 103 }
111 } 104 }
112 } 105 }
113 106
114 // Calculate our sleep duration, taking playback rate into consideration. 107 // Calculate our sleep duration.
115 delay = base::TimeDelta::FromMilliseconds( 108 delay = base::TimeDelta::FromMilliseconds(
116 frames_received / (frames_per_millisecond * playback_rate_)); 109 frames_received / frames_per_millisecond);
117 } else { 110 } else {
118 // If paused, sleep for 10 milliseconds before polling again. 111 // If paused, sleep for 10 milliseconds before polling again.
119 delay = base::TimeDelta::FromMilliseconds(10); 112 delay = base::TimeDelta::FromMilliseconds(10);
120 } 113 }
121 114
122 // Sleep for at least one millisecond so we don't spin the CPU. 115 // Sleep for at least one millisecond so we don't spin the CPU.
123 MessageLoop::current()->PostDelayedTask( 116 MessageLoop::current()->PostDelayedTask(
124 FROM_HERE, 117 FROM_HERE,
125 base::Bind(&NullAudioSink::FillBufferTask, this), 118 base::Bind(&NullAudioSink::FillBufferTask, this),
126 std::max(delay, base::TimeDelta::FromMilliseconds(1))); 119 std::max(delay, base::TimeDelta::FromMilliseconds(1)));
(...skipping 19 matching lines...) Expand all
146 base::MD5Final(&digest, &md5_channel_contexts_[i]); 139 base::MD5Final(&digest, &md5_channel_contexts_[i]);
147 base::MD5Update(&md5_channel_contexts_[0], base::StringPiece( 140 base::MD5Update(&md5_channel_contexts_[0], base::StringPiece(
148 reinterpret_cast<char*>(&digest), sizeof(base::MD5Digest))); 141 reinterpret_cast<char*>(&digest), sizeof(base::MD5Digest)));
149 } 142 }
150 143
151 base::MD5Final(&digest, &md5_channel_contexts_[0]); 144 base::MD5Final(&digest, &md5_channel_contexts_[0]);
152 return base::MD5DigestToBase16(digest); 145 return base::MD5DigestToBase16(digest);
153 } 146 }
154 147
155 } // namespace media 148 } // namespace media
OLDNEW
« no previous file with comments | « media/audio/null_audio_sink.h ('k') | media/base/audio_renderer_sink.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698