Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(519)

Side by Side Diff: content/renderer/media/audio_decoder.cc

Issue 2655783004: Decode entire in-memory file for WebAudio (Closed)
Patch Set: Remove old implmenetation and update tests Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | media/filters/audio_file_reader.h » ('j') | media/filters/audio_file_reader.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <iomanip>
DaleCurtis 2017/02/09 18:47:36 ?
Raymond Toy 2017/02/09 18:56:12 So I can std::fixed and std::setprecision so more
DaleCurtis 2017/02/09 22:42:20 Instead how about using TimeDelta::FromSecondsD().
Raymond Toy 2017/02/09 23:09:27 That produces the result in microseconds, which ma
9 #include <vector> 10 #include <vector>
10 11
11 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
12 #include "base/time/time.h" 13 #include "base/time/time.h"
13 #include "media/base/audio_bus.h" 14 #include "media/base/audio_bus.h"
14 #include "media/base/limits.h" 15 #include "media/base/limits.h"
15 #include "media/filters/audio_file_reader.h" 16 #include "media/filters/audio_file_reader.h"
16 #include "media/filters/in_memory_url_protocol.h" 17 #include "media/filters/in_memory_url_protocol.h"
17 #include "third_party/WebKit/public/platform/WebAudioBus.h" 18 #include "third_party/WebKit/public/platform/WebAudioBus.h"
18 19
19 using media::AudioBus; 20 using media::AudioBus;
20 using media::AudioFileReader; 21 using media::AudioFileReader;
21 using media::InMemoryUrlProtocol; 22 using media::InMemoryUrlProtocol;
22 using std::vector; 23 using std::vector;
23 using blink::WebAudioBus; 24 using blink::WebAudioBus;
24 25
25 namespace content { 26 namespace content {
26 27
27 // Decode in-memory audio file data. 28 // Decode in-memory audio file data.
28 bool DecodeAudioFileData( 29 bool DecodeAudioFileData(
29 blink::WebAudioBus* destination_bus, 30 blink::WebAudioBus* destination_bus,
30 const char* data, size_t data_size) { 31 const char* data, size_t data_size) {
31 DCHECK(destination_bus); 32 DCHECK(destination_bus);
32 if (!destination_bus) 33 if (!destination_bus)
33 return false; 34 return false;
34 35
35 #if !defined(MEDIA_DISABLE_FFMPEG) 36 #if defined(MEDIA_DISABLE_FFMPEG)
37 return false
38 #else
36 // Uses the FFmpeg library for audio file reading. 39 // Uses the FFmpeg library for audio file reading.
37 InMemoryUrlProtocol url_protocol(reinterpret_cast<const uint8_t*>(data), 40 InMemoryUrlProtocol url_protocol(reinterpret_cast<const uint8_t*>(data),
38 data_size, false); 41 data_size, false);
39 AudioFileReader reader(&url_protocol); 42 AudioFileReader reader(&url_protocol);
40 43
41 if (!reader.Open()) 44 if (!reader.Open())
42 return false; 45 return false;
43 46
44 size_t number_of_channels = reader.channels(); 47 size_t number_of_channels = reader.channels();
45 double file_sample_rate = reader.sample_rate(); 48 double file_sample_rate = reader.sample_rate();
46 size_t number_of_frames = static_cast<size_t>(reader.GetNumberOfFrames());
47 49
48 // Apply sanity checks to make sure crazy values aren't coming out of 50 // Apply sanity checks to make sure crazy values aren't coming out of
49 // FFmpeg. 51 // FFmpeg.
50 if (!number_of_channels || 52 if (!number_of_channels ||
51 number_of_channels > static_cast<size_t>(media::limits::kMaxChannels) || 53 number_of_channels > static_cast<size_t>(media::limits::kMaxChannels) ||
52 file_sample_rate < media::limits::kMinSampleRate || 54 file_sample_rate < media::limits::kMinSampleRate ||
53 file_sample_rate > media::limits::kMaxSampleRate) 55 file_sample_rate > media::limits::kMaxSampleRate)
54 return false; 56 return false;
55 57
56 // Allocate and configure the output audio channel data. 58 std::vector<std::unique_ptr<AudioBus>> decodedAudioPackets;
DaleCurtis 2017/02/09 18:47:36 decoded_audio_packets.
Raymond Toy 2017/02/09 21:45:06 Done.
57 destination_bus->initialize(number_of_channels, 59 int number_of_frames = reader.Read(decodedAudioPackets);
DaleCurtis 2017/02/09 18:47:36 Should be passing a pointer in?
Raymond Toy 2017/02/09 21:45:06 Done.
58 number_of_frames, 60
61 if (number_of_frames <= 0)
62 return false;
63
64 // Allocate and configure the output audio channel data and then
65 // copy the decoded data to the destination.
66 destination_bus->initialize(number_of_channels, number_of_frames,
59 file_sample_rate); 67 file_sample_rate);
60 68
61 // Wrap the channel pointers which will receive the decoded PCM audio. 69 int dest_frame_offset = 0;
62 vector<float*> audio_data; 70 for (auto&& packet : decodedAudioPackets) {
DaleCurtis 2017/02/09 18:47:36 && is not allowed per style guide.
Raymond Toy 2017/02/09 21:45:06 Done.
63 audio_data.reserve(number_of_channels); 71 int packet_length = packet->frames();
64 for (size_t i = 0; i < number_of_channels; ++i) { 72 for (size_t ch = 0; ch < number_of_channels; ++ch) {
DaleCurtis 2017/02/09 18:47:36 Just use src->CopyPartialFramesTo(...)
Raymond Toy 2017/02/09 21:45:06 Unfortunately, destination_bus is not an AudioBus.
65 audio_data.push_back(destination_bus->channelData(i)); 73 float* dst = destination_bus->channelData(ch);
74 float* src = packet->channel(ch);
75 DCHECK_LE(dest_frame_offset + packet_length, number_of_frames);
76 memcpy(dst + dest_frame_offset, src, packet_length * sizeof(*dst));
77 }
78 dest_frame_offset += packet_length;
66 } 79 }
67 80
68 std::unique_ptr<AudioBus> audio_bus = 81 DVLOG(1) << "Decoded file data (unknown duration)-"
69 AudioBus::WrapVector(number_of_frames, audio_data); 82 << " data: " << data << " data size: " << data_size
70 83 << " decoded duration: " << std::fixed << std::setprecision(6)
71 // Decode the audio file data. 84 << number_of_frames / file_sample_rate
72 // TODO(crogers): If our estimate was low, then we still may fail to read 85 << " estimated duration (if available): "
73 // all of the data from the file. 86 << (reader.HasKnownDuration() ? reader.GetNumberOfFrames() : 0) /
74 size_t actual_frames = reader.Read(audio_bus.get()); 87 file_sample_rate
75 88 << " number of frames: " << number_of_frames
76 // Adjust WebKit's bus to account for the actual file length
77 // and valid data read.
78 if (actual_frames != number_of_frames) {
79 DCHECK_LE(actual_frames, number_of_frames);
80 destination_bus->resizeSmaller(actual_frames);
81 }
82
83 double duration = actual_frames / file_sample_rate;
84
85 DVLOG(1) << "Decoded file data -"
86 << " data: " << data
87 << " data size: " << data_size
88 << " duration: " << duration
89 << " number of frames: " << actual_frames
90 << " sample rate: " << file_sample_rate 89 << " sample rate: " << file_sample_rate
91 << " number of channels: " << number_of_channels; 90 << " number of channels: " << number_of_channels;
92 91
93 return actual_frames > 0; 92 return number_of_frames > 0;
94 #else 93 #endif // defined(MEDIA_DISABLE_FFMPEG)
95 return false;
96 #endif // !defined(MEDIA_DISABLE_FFMPEG)
97 } 94 }
98 95
99 } // namespace content 96 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | media/filters/audio_file_reader.h » ('j') | media/filters/audio_file_reader.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698