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

Side by Side Diff: components/audio_modem/audio_player_impl.cc

Issue 2131993002: Delete the audio modem and copresence private APIs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@eol
Patch Set: Sync again Created 4 years, 5 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
OLDNEW
(Empty)
1 // Copyright 2015 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 "components/audio_modem/audio_player_impl.h"
6
7 #include <algorithm>
8 #include <string>
9
10 #include "base/bind.h"
11 #include "base/location.h"
12 #include "base/logging.h"
13 #include "components/audio_modem/public/audio_modem_types.h"
14 #include "media/audio/audio_manager.h"
15 #include "media/base/audio_bus.h"
16 #include "media/base/audio_parameters.h"
17
18 namespace {
19
20 const int kDefaultFrameCount = 1024;
21 const double kOutputVolumePercent = 1.0f;
22
23 } // namespace
24
25 namespace audio_modem {
26
27 // Public methods.
28
29 AudioPlayerImpl::AudioPlayerImpl()
30 : is_playing_(false), stream_(nullptr), frame_index_(0) {
31 }
32
33 AudioPlayerImpl::~AudioPlayerImpl() {
34 }
35
36 void AudioPlayerImpl::Initialize() {
37 media::AudioManager::Get()->GetTaskRunner()->PostTask(
38 FROM_HERE,
39 base::Bind(&AudioPlayerImpl::InitializeOnAudioThread,
40 base::Unretained(this)));
41 }
42
43 void AudioPlayerImpl::Play(
44 const scoped_refptr<media::AudioBusRefCounted>& samples) {
45 media::AudioManager::Get()->GetTaskRunner()->PostTask(
46 FROM_HERE,
47 base::Bind(&AudioPlayerImpl::PlayOnAudioThread,
48 base::Unretained(this),
49 samples));
50 }
51
52 void AudioPlayerImpl::Stop() {
53 media::AudioManager::Get()->GetTaskRunner()->PostTask(
54 FROM_HERE,
55 base::Bind(&AudioPlayerImpl::StopOnAudioThread, base::Unretained(this)));
56 }
57
58 void AudioPlayerImpl::Finalize() {
59 media::AudioManager::Get()->GetTaskRunner()->PostTask(
60 FROM_HERE,
61 base::Bind(&AudioPlayerImpl::FinalizeOnAudioThread,
62 base::Unretained(this)));
63 }
64
65 // Private methods.
66
67 void AudioPlayerImpl::InitializeOnAudioThread() {
68 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
69 stream_ = output_stream_for_testing_
70 ? output_stream_for_testing_.get()
71 : media::AudioManager::Get()->MakeAudioOutputStreamProxy(
72 media::AudioParameters(
73 media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
74 media::CHANNEL_LAYOUT_MONO,
75 kDefaultSampleRate,
76 kDefaultBitsPerSample,
77 kDefaultFrameCount),
78 std::string());
79
80 if (!stream_ || !stream_->Open()) {
81 LOG(ERROR) << "Failed to open an output stream.";
82 if (stream_) {
83 stream_->Close();
84 stream_ = nullptr;
85 }
86 return;
87 }
88 stream_->SetVolume(kOutputVolumePercent);
89 }
90
91 void AudioPlayerImpl::PlayOnAudioThread(
92 const scoped_refptr<media::AudioBusRefCounted>& samples) {
93 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
94 if (!stream_ || is_playing_)
95 return;
96
97 {
98 base::AutoLock al(state_lock_);
99 samples_ = samples;
100 frame_index_ = 0;
101 }
102
103 VLOG(3) << "Starting playback.";
104 is_playing_ = true;
105 stream_->Start(this);
106 }
107
108 void AudioPlayerImpl::StopOnAudioThread() {
109 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
110 if (!stream_ || !is_playing_)
111 return;
112
113 VLOG(3) << "Stopping playback.";
114 stream_->Stop();
115 is_playing_ = false;
116 }
117
118 void AudioPlayerImpl::StopAndCloseOnAudioThread() {
119 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
120 if (!stream_)
121 return;
122
123 StopOnAudioThread();
124 stream_->Close();
125 stream_ = nullptr;
126 }
127
128 void AudioPlayerImpl::FinalizeOnAudioThread() {
129 DCHECK(media::AudioManager::Get()->GetTaskRunner()->BelongsToCurrentThread());
130 StopAndCloseOnAudioThread();
131 delete this;
132 }
133
134 int AudioPlayerImpl::OnMoreData(media::AudioBus* dest,
135 uint32_t /* total_bytes_delay */,
136 uint32_t /* frames_skipped */) {
137 base::AutoLock al(state_lock_);
138 // Continuously play our samples till explicitly told to stop.
139 const int leftover_frames = samples_->frames() - frame_index_;
140 const int frames_to_copy = std::min(dest->frames(), leftover_frames);
141
142 samples_->CopyPartialFramesTo(frame_index_, frames_to_copy, 0, dest);
143 frame_index_ += frames_to_copy;
144
145 // If we didn't fill the destination audio bus, wrap around and fill the rest.
146 if (leftover_frames <= dest->frames()) {
147 samples_->CopyPartialFramesTo(
148 0, dest->frames() - frames_to_copy, frames_to_copy, dest);
149 frame_index_ = dest->frames() - frames_to_copy;
150 }
151
152 return dest->frames();
153 }
154
155 void AudioPlayerImpl::OnError(media::AudioOutputStream* /* stream */) {
156 LOG(ERROR) << "Error during system sound reproduction.";
157 media::AudioManager::Get()->GetTaskRunner()->PostTask(
158 FROM_HERE,
159 base::Bind(&AudioPlayerImpl::StopAndCloseOnAudioThread,
160 base::Unretained(this)));
161 }
162
163 } // namespace audio_modem
OLDNEW
« no previous file with comments | « components/audio_modem/audio_player_impl.h ('k') | components/audio_modem/audio_player_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698