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 "media/audio/audio_parameters.h" | 5 #include "media/audio/audio_parameters.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "media/base/limits.h" | 8 #include "media/base/limits.h" |
9 | 9 |
10 namespace media { | 10 namespace media { |
11 | 11 |
12 AudioParameters::AudioParameters() | 12 AudioParameters::AudioParameters() |
13 : format_(AUDIO_PCM_LINEAR), | 13 : format_(AUDIO_PCM_LINEAR), |
14 channel_layout_(CHANNEL_LAYOUT_NONE), | 14 channel_layout_(CHANNEL_LAYOUT_NONE), |
15 sample_rate_(0), | 15 sample_rate_(0), |
16 bits_per_sample_(0), | 16 bits_per_sample_(0), |
17 frames_per_buffer_(0), | 17 frames_per_buffer_(0), |
18 channels_(0), | 18 channels_(0), |
19 input_channels_(0), | |
20 effects_(NO_EFFECTS) { | 19 effects_(NO_EFFECTS) { |
21 } | 20 } |
22 | 21 |
23 AudioParameters::AudioParameters(Format format, ChannelLayout channel_layout, | 22 AudioParameters::AudioParameters(Format format, ChannelLayout channel_layout, |
24 int sample_rate, int bits_per_sample, | 23 int sample_rate, int bits_per_sample, |
25 int frames_per_buffer) | 24 int frames_per_buffer) |
26 : format_(format), | 25 : format_(format), |
27 channel_layout_(channel_layout), | 26 channel_layout_(channel_layout), |
28 sample_rate_(sample_rate), | 27 sample_rate_(sample_rate), |
29 bits_per_sample_(bits_per_sample), | 28 bits_per_sample_(bits_per_sample), |
30 frames_per_buffer_(frames_per_buffer), | 29 frames_per_buffer_(frames_per_buffer), |
31 channels_(ChannelLayoutToChannelCount(channel_layout)), | 30 channels_(ChannelLayoutToChannelCount(channel_layout)), |
32 input_channels_(0), | |
33 effects_(NO_EFFECTS) { | 31 effects_(NO_EFFECTS) { |
34 } | 32 } |
35 | 33 |
36 AudioParameters::AudioParameters(Format format, ChannelLayout channel_layout, | 34 AudioParameters::AudioParameters(Format format, ChannelLayout channel_layout, |
37 int input_channels, | |
38 int sample_rate, int bits_per_sample, | 35 int sample_rate, int bits_per_sample, |
39 int frames_per_buffer, int effects) | 36 int frames_per_buffer, int effects) |
40 : format_(format), | 37 : format_(format), |
41 channel_layout_(channel_layout), | 38 channel_layout_(channel_layout), |
42 sample_rate_(sample_rate), | 39 sample_rate_(sample_rate), |
43 bits_per_sample_(bits_per_sample), | 40 bits_per_sample_(bits_per_sample), |
44 frames_per_buffer_(frames_per_buffer), | 41 frames_per_buffer_(frames_per_buffer), |
45 channels_(ChannelLayoutToChannelCount(channel_layout)), | 42 channels_(ChannelLayoutToChannelCount(channel_layout)), |
46 input_channels_(input_channels), | |
47 effects_(effects) { | 43 effects_(effects) { |
48 } | 44 } |
49 | 45 |
50 AudioParameters::AudioParameters(Format format, ChannelLayout channel_layout, | 46 AudioParameters::AudioParameters(Format format, ChannelLayout channel_layout, |
51 int channels, int input_channels, | 47 int channels, int sample_rate, |
52 int sample_rate, int bits_per_sample, | 48 int bits_per_sample, int frames_per_buffer, |
53 int frames_per_buffer, int effects) | 49 int effects) |
54 : format_(format), | 50 : format_(format), |
55 channel_layout_(channel_layout), | 51 channel_layout_(channel_layout), |
56 sample_rate_(sample_rate), | 52 sample_rate_(sample_rate), |
57 bits_per_sample_(bits_per_sample), | 53 bits_per_sample_(bits_per_sample), |
58 frames_per_buffer_(frames_per_buffer), | 54 frames_per_buffer_(frames_per_buffer), |
59 channels_(channels), | 55 channels_(channels), |
60 input_channels_(input_channels), | |
61 effects_(effects) { | 56 effects_(effects) { |
62 if (channel_layout != CHANNEL_LAYOUT_DISCRETE) | 57 if (channel_layout != CHANNEL_LAYOUT_DISCRETE) |
63 DCHECK_EQ(channels, ChannelLayoutToChannelCount(channel_layout)); | 58 DCHECK_EQ(channels, ChannelLayoutToChannelCount(channel_layout)); |
64 } | 59 } |
65 | 60 |
66 void AudioParameters::Reset(Format format, ChannelLayout channel_layout, | 61 void AudioParameters::Reset(Format format, ChannelLayout channel_layout, |
67 int channels, int input_channels, | 62 int channels, int sample_rate, |
68 int sample_rate, int bits_per_sample, | 63 int bits_per_sample, int frames_per_buffer) { |
69 int frames_per_buffer) { | |
70 if (channel_layout != CHANNEL_LAYOUT_DISCRETE) | 64 if (channel_layout != CHANNEL_LAYOUT_DISCRETE) |
71 DCHECK_EQ(channels, ChannelLayoutToChannelCount(channel_layout)); | 65 DCHECK_EQ(channels, ChannelLayoutToChannelCount(channel_layout)); |
72 | 66 |
73 format_ = format; | 67 format_ = format; |
74 channel_layout_ = channel_layout; | 68 channel_layout_ = channel_layout; |
75 channels_ = channels; | 69 channels_ = channels; |
76 input_channels_ = input_channels; | |
77 sample_rate_ = sample_rate; | 70 sample_rate_ = sample_rate; |
78 bits_per_sample_ = bits_per_sample; | 71 bits_per_sample_ = bits_per_sample; |
79 frames_per_buffer_ = frames_per_buffer; | 72 frames_per_buffer_ = frames_per_buffer; |
80 } | 73 } |
81 | 74 |
82 bool AudioParameters::IsValid() const { | 75 bool AudioParameters::IsValid() const { |
83 return (format_ >= AUDIO_PCM_LINEAR) && | 76 return (format_ >= AUDIO_PCM_LINEAR) && |
84 (format_ < AUDIO_LAST_FORMAT) && | 77 (format_ < AUDIO_LAST_FORMAT) && |
85 (channels_ > 0) && | 78 (channels_ > 0) && |
86 (channels_ <= media::limits::kMaxChannels) && | 79 (channels_ <= media::limits::kMaxChannels) && |
87 (channel_layout_ > CHANNEL_LAYOUT_UNSUPPORTED) && | 80 (channel_layout_ > CHANNEL_LAYOUT_UNSUPPORTED) && |
88 (channel_layout_ <= CHANNEL_LAYOUT_MAX) && | 81 (channel_layout_ <= CHANNEL_LAYOUT_MAX) && |
89 (input_channels_ >= 0) && | |
90 (input_channels_ <= media::limits::kMaxChannels) && | |
91 (sample_rate_ >= media::limits::kMinSampleRate) && | 82 (sample_rate_ >= media::limits::kMinSampleRate) && |
92 (sample_rate_ <= media::limits::kMaxSampleRate) && | 83 (sample_rate_ <= media::limits::kMaxSampleRate) && |
93 (bits_per_sample_ > 0) && | 84 (bits_per_sample_ > 0) && |
94 (bits_per_sample_ <= media::limits::kMaxBitsPerSample) && | 85 (bits_per_sample_ <= media::limits::kMaxBitsPerSample) && |
95 (frames_per_buffer_ > 0) && | 86 (frames_per_buffer_ > 0) && |
96 (frames_per_buffer_ <= media::limits::kMaxSamplesPerPacket); | 87 (frames_per_buffer_ <= media::limits::kMaxSamplesPerPacket); |
97 } | 88 } |
98 | 89 |
99 int AudioParameters::GetBytesPerBuffer() const { | 90 int AudioParameters::GetBytesPerBuffer() const { |
100 return frames_per_buffer_ * GetBytesPerFrame(); | 91 return frames_per_buffer_ * GetBytesPerFrame(); |
101 } | 92 } |
102 | 93 |
103 int AudioParameters::GetBytesPerSecond() const { | 94 int AudioParameters::GetBytesPerSecond() const { |
104 return sample_rate_ * GetBytesPerFrame(); | 95 return sample_rate_ * GetBytesPerFrame(); |
105 } | 96 } |
106 | 97 |
107 int AudioParameters::GetBytesPerFrame() const { | 98 int AudioParameters::GetBytesPerFrame() const { |
108 return channels_ * bits_per_sample_ / 8; | 99 return channels_ * bits_per_sample_ / 8; |
109 } | 100 } |
110 | 101 |
111 base::TimeDelta AudioParameters::GetBufferDuration() const { | 102 base::TimeDelta AudioParameters::GetBufferDuration() const { |
112 return base::TimeDelta::FromMicroseconds( | 103 return base::TimeDelta::FromMicroseconds( |
113 frames_per_buffer_ * base::Time::kMicrosecondsPerSecond / | 104 frames_per_buffer_ * base::Time::kMicrosecondsPerSecond / |
114 static_cast<float>(sample_rate_)); | 105 static_cast<float>(sample_rate_)); |
115 } | 106 } |
116 | 107 |
117 } // namespace media | 108 } // namespace media |
OLD | NEW |