OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "webkit/media/audio_decoder.h" | 5 #include "webkit/media/audio_decoder.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
(...skipping 22 matching lines...) Expand all Loading... | |
33 // Uses the FFmpeg library for audio file reading. | 33 // Uses the FFmpeg library for audio file reading. |
34 InMemoryUrlProtocol url_protocol(reinterpret_cast<const uint8*>(data), | 34 InMemoryUrlProtocol url_protocol(reinterpret_cast<const uint8*>(data), |
35 data_size, false); | 35 data_size, false); |
36 AudioFileReader reader(&url_protocol); | 36 AudioFileReader reader(&url_protocol); |
37 | 37 |
38 if (!reader.Open()) | 38 if (!reader.Open()) |
39 return false; | 39 return false; |
40 | 40 |
41 size_t number_of_channels = reader.channels(); | 41 size_t number_of_channels = reader.channels(); |
42 double file_sample_rate = reader.sample_rate(); | 42 double file_sample_rate = reader.sample_rate(); |
43 double duration = reader.duration().InSecondsF(); | |
44 size_t number_of_frames = static_cast<size_t>(reader.number_of_frames()); | 43 size_t number_of_frames = static_cast<size_t>(reader.number_of_frames()); |
DaleCurtis
2012/10/12 22:42:02
Unnecessary static_cast.
Chris Rogers
2012/10/12 23:22:29
Done.
DaleCurtis
2012/10/13 01:10:35
Doesn't look changed in the latest patch set?
| |
45 | 44 |
46 // Apply sanity checks to make sure crazy values aren't coming out of | 45 // Apply sanity checks to make sure crazy values aren't coming out of |
47 // FFmpeg. | 46 // FFmpeg. |
48 if (!number_of_channels || | 47 if (!number_of_channels || |
49 number_of_channels > static_cast<size_t>(media::limits::kMaxChannels) || | 48 number_of_channels > static_cast<size_t>(media::limits::kMaxChannels) || |
50 file_sample_rate < media::limits::kMinSampleRate || | 49 file_sample_rate < media::limits::kMinSampleRate || |
51 file_sample_rate > media::limits::kMaxSampleRate) | 50 file_sample_rate > media::limits::kMaxSampleRate) |
52 return false; | 51 return false; |
53 | 52 |
54 DVLOG(1) << "Decoding file data -" | |
55 << " data: " << data | |
56 << " data size: " << data_size | |
57 << " duration: " << duration | |
58 << " number of frames: " << number_of_frames | |
59 << " sample rate: " << file_sample_rate | |
60 << " number of channels: " << number_of_channels; | |
61 | |
62 // Allocate and configure the output audio channel data. | 53 // Allocate and configure the output audio channel data. |
63 destination_bus->initialize(number_of_channels, | 54 destination_bus->initialize(number_of_channels, |
64 number_of_frames, | 55 number_of_frames, |
65 file_sample_rate); | 56 file_sample_rate); |
66 | 57 |
67 // Wrap the channel pointers which will receive the decoded PCM audio. | 58 // Wrap the channel pointers which will receive the decoded PCM audio. |
68 vector<float*> audio_data; | 59 vector<float*> audio_data; |
69 audio_data.reserve(number_of_channels); | 60 audio_data.reserve(number_of_channels); |
70 for (size_t i = 0; i < number_of_channels; ++i) { | 61 for (size_t i = 0; i < number_of_channels; ++i) { |
71 audio_data.push_back(destination_bus->channelData(i)); | 62 audio_data.push_back(destination_bus->channelData(i)); |
72 } | 63 } |
73 | 64 |
74 scoped_ptr<AudioBus> audio_bus = AudioBus::WrapVector( | 65 scoped_ptr<AudioBus> audio_bus = AudioBus::WrapVector( |
75 number_of_frames, audio_data); | 66 number_of_frames, audio_data); |
76 | 67 |
77 // Decode the audio file data. | 68 // Decode the audio file data. |
78 return reader.Read(audio_bus.get()); | 69 size_t actual_frames = static_cast<size_t>(reader.Read(audio_bus.get())); |
DaleCurtis
2012/10/12 22:42:02
This only works for down sizing and not up sizing
Chris Rogers
2012/10/12 23:22:29
Added TODO that this case is still not handled. T
| |
70 | |
71 // Adjust WebKit's bus to account for the actual file length | |
72 // and valid data read. | |
73 if (actual_frames != number_of_frames) { | |
74 DCHECK_LE(actual_frames, number_of_frames); | |
DaleCurtis
2012/10/12 22:42:02
2 space indent.
Chris Rogers
2012/10/12 23:22:29
Done.
| |
75 destination_bus->resize(actual_frames); | |
76 } | |
77 | |
78 double duration = actual_frames / file_sample_rate; | |
79 | |
80 DVLOG(1) << "Decoded file data -" | |
81 << " data: " << data | |
82 << " data size: " << data_size | |
83 << " duration: " << duration | |
84 << " number of frames: " << actual_frames | |
85 << " sample rate: " << file_sample_rate | |
86 << " number of channels: " << number_of_channels; | |
87 | |
88 return actual_frames > 0; | |
79 } | 89 } |
80 | 90 |
81 } // namespace webkit_media | 91 } // namespace webkit_media |
OLD | NEW |