Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/renderer/media/audio_decoder.h" | 5 #include "content/renderer/media/audio_decoder.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 namespace content { | 25 namespace content { |
| 26 | 26 |
| 27 // Decode in-memory audio file data. | 27 // Decode in-memory audio file data. |
| 28 bool DecodeAudioFileData( | 28 bool DecodeAudioFileData( |
| 29 blink::WebAudioBus* destination_bus, | 29 blink::WebAudioBus* destination_bus, |
| 30 const char* data, size_t data_size) { | 30 const char* data, size_t data_size) { |
| 31 DCHECK(destination_bus); | 31 DCHECK(destination_bus); |
| 32 if (!destination_bus) | 32 if (!destination_bus) |
| 33 return false; | 33 return false; |
| 34 | 34 |
| 35 #if !defined(MEDIA_DISABLE_FFMPEG) | 35 #if defined(MEDIA_DISABLE_FFMPEG) |
| 36 return false | |
| 37 #else | |
| 36 // Uses the FFmpeg library for audio file reading. | 38 // Uses the FFmpeg library for audio file reading. |
| 37 InMemoryUrlProtocol url_protocol(reinterpret_cast<const uint8_t*>(data), | 39 InMemoryUrlProtocol url_protocol(reinterpret_cast<const uint8_t*>(data), |
| 38 data_size, false); | 40 data_size, false); |
| 39 AudioFileReader reader(&url_protocol); | 41 AudioFileReader reader(&url_protocol); |
| 40 | 42 |
| 41 if (!reader.Open()) | 43 if (!reader.Open()) |
| 42 return false; | 44 return false; |
| 43 | 45 |
| 44 size_t number_of_channels = reader.channels(); | 46 size_t number_of_channels = reader.channels(); |
| 45 double file_sample_rate = reader.sample_rate(); | 47 double file_sample_rate = reader.sample_rate(); |
| 46 size_t number_of_frames = static_cast<size_t>(reader.GetNumberOfFrames()); | |
| 47 | 48 |
| 48 // Apply sanity checks to make sure crazy values aren't coming out of | 49 // Apply sanity checks to make sure crazy values aren't coming out of |
| 49 // FFmpeg. | 50 // FFmpeg. |
| 50 if (!number_of_channels || | 51 if (!number_of_channels || |
| 51 number_of_channels > static_cast<size_t>(media::limits::kMaxChannels) || | 52 number_of_channels > static_cast<size_t>(media::limits::kMaxChannels) || |
| 52 file_sample_rate < media::limits::kMinSampleRate || | 53 file_sample_rate < media::limits::kMinSampleRate || |
| 53 file_sample_rate > media::limits::kMaxSampleRate) | 54 file_sample_rate > media::limits::kMaxSampleRate) |
| 54 return false; | 55 return false; |
| 55 | 56 |
| 57 if (!reader.HasKnownDuration()) { | |
| 58 // We don't have an estimated duration so read the entire stream. | |
| 59 std::unique_ptr<AudioBus> decodedAudio = reader.StreamingRead(); | |
| 60 | |
| 61 // Allocate and configure the output audio channel data. | |
|
DaleCurtis
2017/01/25 22:42:25
This adds an extra copy, how about returning the s
| |
| 62 size_t number_of_frames = decodedAudio->frames(); | |
| 63 destination_bus->initialize(number_of_channels, number_of_frames, | |
| 64 file_sample_rate); | |
| 65 | |
| 66 for (size_t ch = 0; ch < number_of_channels; ++ch) { | |
| 67 DCHECK(destination_bus->channelData(ch)); | |
| 68 memcpy(destination_bus->channelData(ch), decodedAudio->channel(ch), | |
| 69 number_of_frames * sizeof(float)); | |
| 70 } | |
| 71 DVLOG(1) << "Decoded file data (unknown duration)-" | |
| 72 << " data: " << data << " data size: " << data_size | |
| 73 << " decoded duration: " << number_of_frames / file_sample_rate | |
| 74 << " number of frames: " << number_of_frames | |
| 75 << " sample rate: " << file_sample_rate | |
| 76 << " number of channels: " << number_of_channels; | |
| 77 return true; | |
| 78 } | |
| 79 | |
| 80 // An estimated duration is available so we can use that to allocate | |
| 81 // memory for the decoded data. | |
| 82 size_t number_of_frames = static_cast<size_t>(reader.GetNumberOfFrames()); | |
| 83 | |
| 56 // Allocate and configure the output audio channel data. | 84 // Allocate and configure the output audio channel data. |
| 57 destination_bus->initialize(number_of_channels, | 85 destination_bus->initialize(number_of_channels, |
| 58 number_of_frames, | 86 number_of_frames, |
| 59 file_sample_rate); | 87 file_sample_rate); |
| 60 | 88 |
| 61 // Wrap the channel pointers which will receive the decoded PCM audio. | 89 // Wrap the channel pointers which will receive the decoded PCM audio. |
| 62 vector<float*> audio_data; | 90 vector<float*> audio_data; |
| 63 audio_data.reserve(number_of_channels); | 91 audio_data.reserve(number_of_channels); |
| 64 for (size_t i = 0; i < number_of_channels; ++i) { | 92 for (size_t i = 0; i < number_of_channels; ++i) { |
| 65 audio_data.push_back(destination_bus->channelData(i)); | 93 audio_data.push_back(destination_bus->channelData(i)); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 84 | 112 |
| 85 DVLOG(1) << "Decoded file data -" | 113 DVLOG(1) << "Decoded file data -" |
| 86 << " data: " << data | 114 << " data: " << data |
| 87 << " data size: " << data_size | 115 << " data size: " << data_size |
| 88 << " duration: " << duration | 116 << " duration: " << duration |
| 89 << " number of frames: " << actual_frames | 117 << " number of frames: " << actual_frames |
| 90 << " sample rate: " << file_sample_rate | 118 << " sample rate: " << file_sample_rate |
| 91 << " number of channels: " << number_of_channels; | 119 << " number of channels: " << number_of_channels; |
| 92 | 120 |
| 93 return actual_frames > 0; | 121 return actual_frames > 0; |
| 94 #else | 122 #endif // defined(MEDIA_DISABLE_FFMPEG) |
| 95 return false; | |
| 96 #endif // !defined(MEDIA_DISABLE_FFMPEG) | |
| 97 } | 123 } |
| 98 | 124 |
| 99 } // namespace content | 125 } // namespace content |
| OLD | NEW |