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( |
| 15 AudioParameters params) { |
15 if (!has_created_fake_stream_) | 16 if (!has_created_fake_stream_) |
16 base::AtExitManager::RegisterCallback(&DestroyLastFakeStream, NULL); | 17 base::AtExitManager::RegisterCallback(&DestroyLastFakeStream, NULL); |
17 has_created_fake_stream_ = true; | 18 has_created_fake_stream_ = true; |
18 | 19 |
19 FakeAudioOutputStream* new_stream = new FakeAudioOutputStream(); | 20 FakeAudioOutputStream* new_stream = new FakeAudioOutputStream(params); |
20 | 21 |
21 if (last_fake_stream_) { | 22 if (last_fake_stream_) { |
22 DCHECK(last_fake_stream_->closed_); | 23 DCHECK(last_fake_stream_->closed_); |
23 delete last_fake_stream_; | 24 delete last_fake_stream_; |
24 } | 25 } |
25 last_fake_stream_ = new_stream; | 26 last_fake_stream_ = new_stream; |
26 | 27 |
27 return new_stream; | 28 return new_stream; |
28 } | 29 } |
29 | 30 |
30 // static | 31 // static |
31 FakeAudioOutputStream* FakeAudioOutputStream::GetLastFakeStream() { | 32 FakeAudioOutputStream* FakeAudioOutputStream::GetLastFakeStream() { |
32 return last_fake_stream_; | 33 return last_fake_stream_; |
33 } | 34 } |
34 | 35 |
35 bool FakeAudioOutputStream::Open(uint32 packet_size) { | 36 bool FakeAudioOutputStream::Open() { |
36 if (packet_size < sizeof(int16)) | 37 if (packet_size_ < sizeof(int16)) |
37 return false; | 38 return false; |
38 packet_size_ = packet_size; | |
39 buffer_.reset(new uint8[packet_size_]); | 39 buffer_.reset(new uint8[packet_size_]); |
40 return true; | 40 return true; |
41 } | 41 } |
42 | 42 |
43 void FakeAudioOutputStream::Start(AudioSourceCallback* callback) { | 43 void FakeAudioOutputStream::Start(AudioSourceCallback* callback) { |
44 callback_ = callback; | 44 callback_ = callback; |
45 memset(buffer_.get(), 0, packet_size_); | 45 memset(buffer_.get(), 0, packet_size_); |
46 callback_->OnMoreData(this, buffer_.get(), packet_size_, | 46 callback_->OnMoreData(this, buffer_.get(), packet_size_, |
47 AudioBuffersState(0, 0)); | 47 AudioBuffersState(0, 0)); |
48 } | 48 } |
49 | 49 |
50 void FakeAudioOutputStream::Stop() { | 50 void FakeAudioOutputStream::Stop() { |
| 51 callback_ = NULL; |
51 } | 52 } |
52 | 53 |
53 void FakeAudioOutputStream::SetVolume(double volume) { | 54 void FakeAudioOutputStream::SetVolume(double volume) { |
54 volume_ = volume; | 55 volume_ = volume; |
55 } | 56 } |
56 | 57 |
57 void FakeAudioOutputStream::GetVolume(double* volume) { | 58 void FakeAudioOutputStream::GetVolume(double* volume) { |
58 *volume = volume_; | 59 *volume = volume_; |
59 } | 60 } |
60 | 61 |
61 void FakeAudioOutputStream::Close() { | 62 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; | 63 closed_ = true; |
69 } | 64 } |
70 | 65 |
71 FakeAudioOutputStream::FakeAudioOutputStream() | 66 FakeAudioOutputStream::FakeAudioOutputStream(AudioParameters params) |
72 : volume_(0), | 67 : volume_(0), |
73 callback_(NULL), | 68 callback_(NULL), |
74 packet_size_(0), | 69 packet_size_(params.GetPacketSize()), |
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 |