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/simple_sources.h" | 5 #include "media/audio/simple_sources.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <math.h> | 8 #include <math.h> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/ref_counted.h" | |
12 #include "media/audio/audio_output.h" | 13 #include "media/audio/audio_output.h" |
14 #include "media/base/data_buffer.h" | |
13 | 15 |
14 ////////////////////////////////////////////////////////////////////////////// | 16 ////////////////////////////////////////////////////////////////////////////// |
15 // SineWaveAudioSource implementation. | 17 // SineWaveAudioSource implementation. |
16 | 18 |
17 SineWaveAudioSource::SineWaveAudioSource(Format format, int channels, | 19 SineWaveAudioSource::SineWaveAudioSource(Format format, int channels, |
18 double freq, double sample_freq) | 20 double freq, double sample_freq) |
19 : format_(format), | 21 : format_(format), |
20 channels_(channels), | 22 channels_(channels), |
21 freq_(freq), | 23 freq_(freq), |
22 sample_freq_(sample_freq) { | 24 sample_freq_(sample_freq) { |
(...skipping 21 matching lines...) Expand all Loading... | |
44 } | 46 } |
45 | 47 |
46 void SineWaveAudioSource::OnError(AudioOutputStream* stream, int code) { | 48 void SineWaveAudioSource::OnError(AudioOutputStream* stream, int code) { |
47 NOTREACHED(); | 49 NOTREACHED(); |
48 } | 50 } |
49 | 51 |
50 ////////////////////////////////////////////////////////////////////////////// | 52 ////////////////////////////////////////////////////////////////////////////// |
51 // PushSource implementation. | 53 // PushSource implementation. |
52 | 54 |
53 PushSource::PushSource() | 55 PushSource::PushSource() |
54 : buffered_bytes_(0), | 56 : buffer_(0, 0) { |
55 front_buffer_consumed_(0) { | |
56 } | 57 } |
57 | 58 |
58 PushSource::~PushSource() { | 59 PushSource::~PushSource() { } |
59 CleanUp(); | |
60 } | |
61 | 60 |
62 uint32 PushSource::OnMoreData(AudioOutputStream* stream, void* dest, | 61 uint32 PushSource::OnMoreData(AudioOutputStream* stream, void* dest, |
63 uint32 max_size, uint32 pending_bytes) { | 62 uint32 max_size, uint32 pending_bytes) { |
64 uint32 copied = 0; | 63 AutoLock auto_lock(buffer_lock_); |
awong
2010/05/26 20:54:41
PushSource really just becomes a small interface a
scherkus (not reviewing)
2010/05/27 02:36:06
Agreed. SeekableBuffer is now a sort of swiss arm
| |
65 while (copied < max_size) { | 64 return buffer_.Read(static_cast<uint8*>(dest), max_size); |
66 AutoLock auto_lock(lock_); | |
67 | |
68 // Under lock processing in this scope. | |
69 if (!packets_.size()) | |
70 break; | |
71 Packet packet = packets_.front(); | |
72 uint32 size = std::min(max_size - copied, | |
73 packet.size - front_buffer_consumed_); | |
74 memcpy(static_cast<char*>(dest) + copied, | |
75 packet.buffer + front_buffer_consumed_, | |
76 size); | |
77 front_buffer_consumed_ += size; | |
78 buffered_bytes_ -= size; | |
79 copied += size; | |
80 if (front_buffer_consumed_ == packet.size) { | |
81 delete [] packet.buffer; | |
82 packets_.pop_front(); | |
83 front_buffer_consumed_ = 0; | |
84 } | |
85 } | |
86 return copied; | |
87 } | 65 } |
88 | 66 |
89 void PushSource::OnClose(AudioOutputStream* stream) { | 67 void PushSource::OnClose(AudioOutputStream* stream) { |
90 CleanUp(); | 68 CleanUp(); |
91 } | 69 } |
92 | 70 |
93 void PushSource::OnError(AudioOutputStream* stream, int code) { | 71 void PushSource::OnError(AudioOutputStream* stream, int code) { |
94 NOTREACHED(); | 72 NOTREACHED(); |
95 } | 73 } |
96 | 74 |
97 // TODO(cpu): Manage arbitrary large sizes. | 75 // TODO(cpu): Manage arbitrary large sizes. |
98 bool PushSource::Write(const void *data, uint32 len) { | 76 bool PushSource::Write(const void *data, uint32 len) { |
99 if (len == 0) { | 77 if (len == 0) { |
100 NOTREACHED(); | 78 NOTREACHED(); |
101 return false; | 79 return false; |
102 } | 80 } |
103 Packet packet = { new char[len], len }; | 81 AutoLock auto_lock(buffer_lock_); |
104 memcpy(packet.buffer, data, packet.size); | 82 buffer_.Append(static_cast<const uint8*>(data), len); |
105 // Under lock processing here. | |
106 AutoLock auto_lock(lock_); | |
107 packets_.push_back(packet); | |
108 buffered_bytes_ += len; | |
109 return true; | 83 return true; |
110 } | 84 } |
111 | 85 |
112 uint32 PushSource::UnProcessedBytes() { | 86 uint32 PushSource::UnProcessedBytes() { |
113 AutoLock auto_lock(lock_); | 87 AutoLock auto_lock(buffer_lock_); |
114 return buffered_bytes_; | 88 return buffer_.forward_bytes(); |
115 } | 89 } |
116 | 90 |
117 void PushSource::ClearAll() { | 91 void PushSource::ClearAll() { |
118 // Cleanup() will discard all the data. | 92 // Cleanup() will discard all the data. |
119 CleanUp(); | 93 CleanUp(); |
120 } | 94 } |
121 | 95 |
122 void PushSource::CleanUp() { | 96 void PushSource::CleanUp() { |
123 AutoLock auto_lock(lock_); | 97 AutoLock auto_lock(buffer_lock_); |
124 PacketList::const_iterator it; | 98 buffer_.Clear(); |
125 for (it = packets_.begin(); it != packets_.end(); ++it) { | |
126 delete [] it->buffer; | |
127 } | |
128 packets_.clear(); | |
129 buffered_bytes_ = 0; | |
130 front_buffer_consumed_ = 0; | |
131 } | 99 } |
OLD | NEW |