OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/fake_audio_output_stream.h" | 5 #include "media/audio/fake_audio_output_stream.h" |
6 | 6 |
7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 | 9 |
10 bool FakeAudioOutputStream::has_created_fake_stream_ = false; | 10 bool FakeAudioOutputStream::has_created_fake_stream_ = false; |
11 FakeAudioOutputStream* FakeAudioOutputStream::last_fake_stream_ = NULL; | 11 FakeAudioOutputStream* FakeAudioOutputStream::last_fake_stream_ = NULL; |
12 | 12 |
13 // static | 13 // static |
14 AudioOutputStream* FakeAudioOutputStream::MakeFakeStream() { | 14 AudioOutputStream* FakeAudioOutputStream::MakeFakeStream(AudioParameters params) { |
scherkus (not reviewing)
2010/11/09 02:28:31
>80 chars
Sergey Ulanov
2010/11/09 22:29:58
Done.
| |
15 if (!has_created_fake_stream_) | 15 if (!has_created_fake_stream_) |
16 base::AtExitManager::RegisterCallback(&DestroyLastFakeStream, NULL); | 16 base::AtExitManager::RegisterCallback(&DestroyLastFakeStream, NULL); |
17 has_created_fake_stream_ = true; | 17 has_created_fake_stream_ = true; |
18 | 18 |
19 FakeAudioOutputStream* new_stream = new FakeAudioOutputStream(); | 19 FakeAudioOutputStream* new_stream = new FakeAudioOutputStream(params); |
20 | 20 |
21 if (last_fake_stream_) { | 21 if (last_fake_stream_) { |
22 DCHECK(last_fake_stream_->closed_); | 22 DCHECK(last_fake_stream_->closed_); |
23 delete last_fake_stream_; | 23 delete last_fake_stream_; |
24 } | 24 } |
25 last_fake_stream_ = new_stream; | 25 last_fake_stream_ = new_stream; |
26 | 26 |
27 return new_stream; | 27 return new_stream; |
28 } | 28 } |
29 | 29 |
30 // static | 30 // static |
31 FakeAudioOutputStream* FakeAudioOutputStream::GetLastFakeStream() { | 31 FakeAudioOutputStream* FakeAudioOutputStream::GetLastFakeStream() { |
32 return last_fake_stream_; | 32 return last_fake_stream_; |
33 } | 33 } |
34 | 34 |
35 bool FakeAudioOutputStream::Open(uint32 packet_size) { | 35 bool FakeAudioOutputStream::Open() { |
36 if (packet_size < sizeof(int16)) | 36 if (packet_size_ < sizeof(int16)) |
37 return false; | 37 return false; |
38 packet_size_ = packet_size; | |
39 buffer_.reset(new uint8[packet_size_]); | 38 buffer_.reset(new uint8[packet_size_]); |
40 return true; | 39 return true; |
41 } | 40 } |
42 | 41 |
43 void FakeAudioOutputStream::Start(AudioSourceCallback* callback) { | 42 void FakeAudioOutputStream::Start(AudioSourceCallback* callback) { |
44 callback_ = callback; | 43 callback_ = callback; |
45 memset(buffer_.get(), 0, packet_size_); | 44 memset(buffer_.get(), 0, packet_size_); |
46 callback_->OnMoreData(this, buffer_.get(), packet_size_, | 45 callback_->OnMoreData(this, buffer_.get(), packet_size_, |
47 AudioBuffersState(0, 0)); | 46 AudioBuffersState(0, 0)); |
48 } | 47 } |
49 | 48 |
50 void FakeAudioOutputStream::Stop() { | 49 void FakeAudioOutputStream::Stop() { |
50 callback_ = NULL; | |
51 } | 51 } |
52 | 52 |
53 void FakeAudioOutputStream::SetVolume(double volume) { | 53 void FakeAudioOutputStream::SetVolume(double volume) { |
54 volume_ = volume; | 54 volume_ = volume; |
55 } | 55 } |
56 | 56 |
57 void FakeAudioOutputStream::GetVolume(double* volume) { | 57 void FakeAudioOutputStream::GetVolume(double* volume) { |
58 *volume = volume_; | 58 *volume = volume_; |
59 } | 59 } |
60 | 60 |
61 void FakeAudioOutputStream::Close() { | 61 void FakeAudioOutputStream::Close() { |
62 // Calls |callback_| only if it is valid. We don't have |callback_| if | |
63 // we have not yet started. | |
64 if (callback_) { | |
65 callback_->OnClose(this); | |
66 callback_ = NULL; | |
67 } | |
68 closed_ = true; | 62 closed_ = true; |
69 } | 63 } |
70 | 64 |
71 FakeAudioOutputStream::FakeAudioOutputStream() | 65 FakeAudioOutputStream::FakeAudioOutputStream(AudioParameters params) |
72 : volume_(0), | 66 : volume_(0), |
73 callback_(NULL), | 67 callback_(NULL), |
74 packet_size_(0), | 68 packet_size_(params.samples_per_packet * params.sample_rate * |
69 params.bits_per_sample / 8), | |
75 closed_(false) { | 70 closed_(false) { |
76 } | 71 } |
77 | 72 |
78 FakeAudioOutputStream::~FakeAudioOutputStream() {} | 73 FakeAudioOutputStream::~FakeAudioOutputStream() {} |
79 | 74 |
80 // static | 75 // static |
81 void FakeAudioOutputStream::DestroyLastFakeStream(void* param) { | 76 void FakeAudioOutputStream::DestroyLastFakeStream(void* param) { |
82 if (last_fake_stream_) { | 77 if (last_fake_stream_) { |
83 DCHECK(last_fake_stream_->closed_); | 78 DCHECK(last_fake_stream_->closed_); |
84 delete last_fake_stream_; | 79 delete last_fake_stream_; |
85 } | 80 } |
86 } | 81 } |
OLD | NEW |