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()); |
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 // TODO(crogers): If our estimate was low, then we still may fail to read |
| 70 // all of the data from the file. |
| 71 size_t actual_frames = reader.Read(audio_bus.get()); |
| 72 |
| 73 // Adjust WebKit's bus to account for the actual file length |
| 74 // and valid data read. |
| 75 if (actual_frames != number_of_frames) { |
| 76 DCHECK_LE(actual_frames, number_of_frames); |
| 77 destination_bus->resizeSmaller(actual_frames); |
| 78 } |
| 79 |
| 80 double duration = actual_frames / file_sample_rate; |
| 81 |
| 82 DVLOG(1) << "Decoded file data -" |
| 83 << " data: " << data |
| 84 << " data size: " << data_size |
| 85 << " duration: " << duration |
| 86 << " number of frames: " << actual_frames |
| 87 << " sample rate: " << file_sample_rate |
| 88 << " number of channels: " << number_of_channels; |
| 89 |
| 90 return actual_frames > 0; |
79 } | 91 } |
80 | 92 |
81 } // namespace webkit_media | 93 } // namespace webkit_media |
OLD | NEW |