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 "content/renderer/media/renderer_webaudiodevice_impl.h" | 5 #include "content/renderer/media/renderer_webaudiodevice_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 23 matching lines...) Expand all Loading... |
34 AudioDeviceFactory::SourceType GetLatencyHintSourceType( | 34 AudioDeviceFactory::SourceType GetLatencyHintSourceType( |
35 WebAudioLatencyHint::AudioContextLatencyCategory latency_category) { | 35 WebAudioLatencyHint::AudioContextLatencyCategory latency_category) { |
36 switch (latency_category) { | 36 switch (latency_category) { |
37 case WebAudioLatencyHint::kCategoryInteractive: | 37 case WebAudioLatencyHint::kCategoryInteractive: |
38 return AudioDeviceFactory::kSourceWebAudioInteractive; | 38 return AudioDeviceFactory::kSourceWebAudioInteractive; |
39 case WebAudioLatencyHint::kCategoryBalanced: | 39 case WebAudioLatencyHint::kCategoryBalanced: |
40 return AudioDeviceFactory::kSourceWebAudioBalanced; | 40 return AudioDeviceFactory::kSourceWebAudioBalanced; |
41 case WebAudioLatencyHint::kCategoryPlayback: | 41 case WebAudioLatencyHint::kCategoryPlayback: |
42 return AudioDeviceFactory::kSourceWebAudioPlayback; | 42 return AudioDeviceFactory::kSourceWebAudioPlayback; |
43 case WebAudioLatencyHint::kCategoryExact: | 43 case WebAudioLatencyHint::kCategoryExact: |
44 // TODO implement CategoryExact | 44 return AudioDeviceFactory::kSourceWebAudioExact; |
45 return AudioDeviceFactory::kSourceWebAudioInteractive; | |
46 } | 45 } |
47 NOTREACHED(); | 46 NOTREACHED(); |
48 return AudioDeviceFactory::kSourceWebAudioInteractive; | 47 return AudioDeviceFactory::kSourceWebAudioInteractive; |
49 } | 48 } |
50 | 49 |
| 50 int GetOutputBufferSize(const blink::WebAudioLatencyHint& latency_hint, |
| 51 media::AudioLatency::LatencyType latency, |
| 52 const media::AudioParameters& hardware_params) { |
| 53 // Adjust output buffer size according to the latency requirement. |
| 54 switch (latency) { |
| 55 case media::AudioLatency::LATENCY_INTERACTIVE: |
| 56 return media::AudioLatency::GetInteractiveBufferSize( |
| 57 hardware_params.frames_per_buffer()); |
| 58 break; |
| 59 case media::AudioLatency::LATENCY_RTC: |
| 60 return media::AudioLatency::GetRtcBufferSize( |
| 61 hardware_params.sample_rate(), hardware_params.frames_per_buffer()); |
| 62 break; |
| 63 case media::AudioLatency::LATENCY_PLAYBACK: |
| 64 return media::AudioLatency::GetHighLatencyBufferSize( |
| 65 hardware_params.sample_rate(), 0); |
| 66 break; |
| 67 case media::AudioLatency::LATENCY_EXACT_MS: |
| 68 // TODO(andrew.macpherson@soundtrap.com): http://crbug.com/708917 |
| 69 return std::min(4096, |
| 70 media::AudioLatency::GetExactBufferSize( |
| 71 base::TimeDelta::FromSecondsD(latency_hint.Seconds()), |
| 72 hardware_params.sample_rate(), |
| 73 hardware_params.frames_per_buffer())); |
| 74 break; |
| 75 default: |
| 76 NOTREACHED(); |
| 77 } |
| 78 return 0; |
| 79 } |
| 80 |
51 int FrameIdFromCurrentContext() { | 81 int FrameIdFromCurrentContext() { |
52 // Assumption: This method is being invoked within a V8 call stack. CHECKs | 82 // Assumption: This method is being invoked within a V8 call stack. CHECKs |
53 // will fail in the call to frameForCurrentContext() otherwise. | 83 // will fail in the call to frameForCurrentContext() otherwise. |
54 // | 84 // |
55 // Therefore, we can perform look-ups to determine which RenderView is | 85 // Therefore, we can perform look-ups to determine which RenderView is |
56 // starting the audio device. The reason for all this is because the creator | 86 // starting the audio device. The reason for all this is because the creator |
57 // of the WebAudio objects might not be the actual source of the audio (e.g., | 87 // of the WebAudio objects might not be the actual source of the audio (e.g., |
58 // an extension creates a object that is passed and used within a page). | 88 // an extension creates a object that is passed and used within a page). |
59 blink::WebLocalFrame* const web_frame = | 89 blink::WebLocalFrame* const web_frame = |
60 blink::WebLocalFrame::FrameForCurrentContext(); | 90 blink::WebLocalFrame::FrameForCurrentContext(); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 const OutputDeviceParamsCallback& device_params_cb, | 127 const OutputDeviceParamsCallback& device_params_cb, |
98 const RenderFrameIdCallback& render_frame_id_cb) | 128 const RenderFrameIdCallback& render_frame_id_cb) |
99 : latency_hint_(latency_hint), | 129 : latency_hint_(latency_hint), |
100 client_callback_(callback), | 130 client_callback_(callback), |
101 session_id_(session_id), | 131 session_id_(session_id), |
102 security_origin_(security_origin), | 132 security_origin_(security_origin), |
103 frame_id_(render_frame_id_cb.Run()) { | 133 frame_id_(render_frame_id_cb.Run()) { |
104 DCHECK(client_callback_); | 134 DCHECK(client_callback_); |
105 DCHECK_NE(frame_id_, MSG_ROUTING_NONE); | 135 DCHECK_NE(frame_id_, MSG_ROUTING_NONE); |
106 | 136 |
107 media::AudioParameters hardware_params(device_params_cb.Run( | 137 const media::AudioParameters hardware_params(device_params_cb.Run( |
108 frame_id_, session_id_, std::string(), security_origin_)); | 138 frame_id_, session_id_, std::string(), security_origin_)); |
109 | 139 |
110 int output_buffer_size = 0; | 140 const media::AudioLatency::LatencyType latency = |
111 | |
112 media::AudioLatency::LatencyType latency = | |
113 AudioDeviceFactory::GetSourceLatencyType( | 141 AudioDeviceFactory::GetSourceLatencyType( |
114 GetLatencyHintSourceType(latency_hint_.Category())); | 142 GetLatencyHintSourceType(latency_hint_.Category())); |
115 | 143 |
116 // Adjust output buffer size according to the latency requirement. | 144 const int output_buffer_size = |
117 switch (latency) { | 145 GetOutputBufferSize(latency_hint_, latency, hardware_params); |
118 case media::AudioLatency::LATENCY_INTERACTIVE: | 146 DCHECK_NE(0, output_buffer_size); |
119 output_buffer_size = media::AudioLatency::GetInteractiveBufferSize( | |
120 hardware_params.frames_per_buffer()); | |
121 break; | |
122 case media::AudioLatency::LATENCY_RTC: | |
123 output_buffer_size = media::AudioLatency::GetRtcBufferSize( | |
124 hardware_params.sample_rate(), hardware_params.frames_per_buffer()); | |
125 break; | |
126 case media::AudioLatency::LATENCY_PLAYBACK: | |
127 output_buffer_size = media::AudioLatency::GetHighLatencyBufferSize( | |
128 hardware_params.sample_rate(), 0); | |
129 break; | |
130 case media::AudioLatency::LATENCY_EXACT_MS: | |
131 // TODO(olka): add support when WebAudio requires it. | |
132 default: | |
133 NOTREACHED(); | |
134 } | |
135 | |
136 DCHECK_NE(output_buffer_size, 0); | |
137 | 147 |
138 sink_params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, layout, | 148 sink_params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY, layout, |
139 hardware_params.sample_rate(), 16, output_buffer_size); | 149 hardware_params.sample_rate(), 16, output_buffer_size); |
140 // Always set channels, this should be a no-op in all but the discrete case; | 150 // Always set channels, this should be a no-op in all but the discrete case; |
141 // this call will fail if channels doesn't match the layout in other cases. | 151 // this call will fail if channels doesn't match the layout in other cases. |
142 sink_params_.set_channels_for_discrete(channels); | 152 sink_params_.set_channels_for_discrete(channels); |
143 | 153 |
144 // Specify the latency info to be passed to the browser side. | 154 // Specify the latency info to be passed to the browser side. |
145 sink_params_.set_latency_tag(latency); | 155 sink_params_.set_latency_tag(latency); |
146 } | 156 } |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 const scoped_refptr<base::SingleThreadTaskRunner>& | 240 const scoped_refptr<base::SingleThreadTaskRunner>& |
231 RendererWebAudioDeviceImpl::GetMediaTaskRunner() { | 241 RendererWebAudioDeviceImpl::GetMediaTaskRunner() { |
232 if (!media_task_runner_) { | 242 if (!media_task_runner_) { |
233 media_task_runner_ = | 243 media_task_runner_ = |
234 RenderThreadImpl::current()->GetMediaThreadTaskRunner(); | 244 RenderThreadImpl::current()->GetMediaThreadTaskRunner(); |
235 } | 245 } |
236 return media_task_runner_; | 246 return media_task_runner_; |
237 } | 247 } |
238 | 248 |
239 } // namespace content | 249 } // namespace content |
OLD | NEW |