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

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

Issue 9826023: Merge AudioRendererImpl and AudioRendererBase; add NullAudioSink (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix win build Created 8 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 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
(Empty)
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
5 #include "media/audio/null_audio_sink.h"
6
7 #include "base/bind.h"
8 #include "base/threading/platform_thread.h"
9
10 namespace media {
11
12 NullAudioSink::NullAudioSink()
13 : initialized_(false),
14 playback_rate_(0.0),
15 playing_(false),
16 callback_(NULL),
17 thread_("NullAudioThread") {
18 }
19
20 NullAudioSink::~NullAudioSink() {
21 DCHECK(!thread_.IsRunning());
22 for (size_t i = 0; i < audio_data_.size(); ++i)
23 delete [] audio_data_[i];
24 }
25
26
27 void NullAudioSink::Start() {
28 if (!thread_.Start())
29 return;
30
31 thread_.message_loop()->PostTask(FROM_HERE, base::Bind(
32 &NullAudioSink::FillBufferTask, this));
33 }
34
35 void NullAudioSink::Stop() {
36 SetPlaying(false);
37 thread_.Stop();
38 }
39
40 void NullAudioSink::Play() {
41 SetPlaying(true);
42 }
43
44 void NullAudioSink::Pause(bool /* flush */) {
45 SetPlaying(false);
46 }
47
48 void NullAudioSink::SetPlaybackRate(float rate) {
49 base::AutoLock auto_lock(lock_);
50 playback_rate_ = rate;
51 }
52
53 bool NullAudioSink::SetVolume(double volume) {
54 // Audio is always muted.
55 return volume == 0.0;
56 }
57
58 void NullAudioSink::GetVolume(double* volume) {
59 // Audio is always muted.
60 *volume = 0.0;
61 }
62
63 void NullAudioSink::SetPlaying(bool is_playing) {
64 base::AutoLock auto_lock(lock_);
65 playing_ = is_playing;
66 }
67
68 void NullAudioSink::Initialize(const AudioParameters& params,
69 RenderCallback* callback) {
70 DCHECK(!initialized_);
71 params_ = params;
72
73 audio_data_.reserve(params.channels());
74 for (int i = 0; i < params.channels(); ++i) {
75 float* channel_data = new float[params.frames_per_buffer()];
76 audio_data_.push_back(channel_data);
77 }
78
79 callback_ = callback;
80 initialized_ = true;
81 }
82
83 void NullAudioSink::FillBufferTask() {
84 base::AutoLock auto_lock(lock_);
85
86 base::TimeDelta delay;
87 // Only consume buffers when actually playing.
88 if (playing_) {
89 DCHECK_GT(playback_rate_, 0.0f);
90 int requested_frames = params_.frames_per_buffer();
91 int frames_received = callback_->Render(audio_data_, requested_frames, 0);
92 int frames_per_millisecond =
93 params_.sample_rate() / base::Time::kMillisecondsPerSecond;
94
95 // Calculate our sleep duration, taking playback rate into consideration.
96 delay = base::TimeDelta::FromMilliseconds(
97 frames_received / (frames_per_millisecond * playback_rate_));
98 } else {
99 // If paused, sleep for 10 milliseconds before polling again.
100 delay = base::TimeDelta::FromMilliseconds(10);
101 }
102
103 // Sleep for at least one millisecond so we don't spin the CPU.
104 MessageLoop::current()->PostDelayedTask(
105 FROM_HERE,
106 base::Bind(&NullAudioSink::FillBufferTask, this),
107 std::max(delay, base::TimeDelta::FromMilliseconds(1)));
108 }
109
110 } // 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