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

Side by Side Diff: content/renderer/media/audio_renderer_mixer_manager.h

Issue 2067863003: Mixing audio with different latency requirements (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: mixing inputs of the same latency Created 4 years, 6 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
OLDNEW
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 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_MIXER_MANAGER_H_ 5 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_MIXER_MANAGER_H_
6 #define CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_MIXER_MANAGER_H_ 6 #define CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_MIXER_MANAGER_H_
7 7
8 #include <map> 8 #include <map>
9 #include <memory> 9 #include <memory>
10 #include <string> 10 #include <string>
11 11
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/synchronization/lock.h" 13 #include "base/synchronization/lock.h"
14 #include "content/common/content_export.h" 14 #include "content/common/content_export.h"
15 #include "media/audio/audio_device_description.h" 15 #include "media/audio/audio_device_description.h"
16 #include "media/base/audio_latency.h"
16 #include "media/base/audio_parameters.h" 17 #include "media/base/audio_parameters.h"
17 #include "media/base/audio_renderer_mixer_pool.h" 18 #include "media/base/audio_renderer_mixer_pool.h"
18 #include "media/base/output_device_info.h" 19 #include "media/base/output_device_info.h"
19 #include "url/origin.h" 20 #include "url/origin.h"
20 21
21 namespace media { 22 namespace media {
22 class AudioRendererMixer; 23 class AudioRendererMixer;
23 class AudioRendererMixerInput; 24 class AudioRendererMixerInput;
24 class AudioRendererSink; 25 class AudioRendererSink;
25 } 26 }
(...skipping 28 matching lines...) Expand all
54 // |source_render_frame_id| refers to the RenderFrame containing the entity 55 // |source_render_frame_id| refers to the RenderFrame containing the entity
55 // rendering the audio. Caller must ensure AudioRendererMixerManager outlives 56 // rendering the audio. Caller must ensure AudioRendererMixerManager outlives
56 // the returned input. |device_id|, |session_id| and |security_origin| 57 // the returned input. |device_id|, |session_id| and |security_origin|
57 // identify the output device to use. If |device_id| is empty and |session_id| 58 // identify the output device to use. If |device_id| is empty and |session_id|
58 // is nonzero, output device associated with the opened input device 59 // is nonzero, output device associated with the opened input device
59 // designated by |session_id| is used. Otherwise, |session_id| is ignored. 60 // designated by |session_id| is used. Otherwise, |session_id| is ignored.
60 media::AudioRendererMixerInput* CreateInput( 61 media::AudioRendererMixerInput* CreateInput(
61 int source_render_frame_id, 62 int source_render_frame_id,
62 int session_id, 63 int session_id,
63 const std::string& device_id, 64 const std::string& device_id,
64 const url::Origin& security_origin); 65 const url::Origin& security_origin,
66 media::AudioLatency::LatencyType latency);
65 67
66 // AudioRendererMixerPool implementation. 68 // AudioRendererMixerPool implementation.
67 69
68 media::AudioRendererMixer* GetMixer( 70 media::AudioRendererMixer* GetMixer(
69 int source_render_frame_id, 71 int source_render_frame_id,
70 const media::AudioParameters& params, 72 const media::AudioParameters& input_params,
73 media::AudioLatency::LatencyType latency,
71 const std::string& device_id, 74 const std::string& device_id,
72 const url::Origin& security_origin, 75 const url::Origin& security_origin,
73 media::OutputDeviceStatus* device_status) final; 76 media::OutputDeviceStatus* device_status) final;
74 77
75 void ReturnMixer(int source_render_frame_id, 78 void ReturnMixer(const media::AudioRendererMixer* mixer) final;
76 const media::AudioParameters& params,
77 const std::string& device_id,
78 const url::Origin& security_origin) final;
79 79
80 media::OutputDeviceInfo GetOutputDeviceInfo( 80 media::OutputDeviceInfo GetOutputDeviceInfo(
81 int source_render_frame_id, 81 int source_render_frame_id,
82 int session_id, 82 int session_id,
83 const std::string& device_id, 83 const std::string& device_id,
84 const url::Origin& security_origin) final; 84 const url::Origin& security_origin) final;
85 85
86 protected: 86 protected:
87 explicit AudioRendererMixerManager( 87 explicit AudioRendererMixerManager(
88 std::unique_ptr<AudioRendererSinkCache> sink_cache); 88 std::unique_ptr<AudioRendererSinkCache> sink_cache);
89 89
90 private: 90 private:
91 friend class AudioRendererMixerManagerTest; 91 friend class AudioRendererMixerManagerTest;
92 92
93 // Define a key so that only those AudioRendererMixerInputs from the same 93 // Define a key so that only those AudioRendererMixerInputs from the same
94 // RenderView, AudioParameters and output device can be mixed together. 94 // RenderView, AudioParameters and output device can be mixed together.
95 struct MixerKey { 95 struct MixerKey {
96 MixerKey(int source_render_frame_id, 96 MixerKey(int source_render_frame_id,
97 const media::AudioParameters& params, 97 const media::AudioParameters& params,
98 media::AudioLatency::LatencyType latency,
98 const std::string& device_id, 99 const std::string& device_id,
99 const url::Origin& security_origin); 100 const url::Origin& security_origin);
100 MixerKey(const MixerKey& other); 101 MixerKey(const MixerKey& other);
101 int source_render_frame_id; 102 int source_render_frame_id;
102 media::AudioParameters params; 103 media::AudioParameters params;
104 media::AudioLatency::LatencyType latency;
103 std::string device_id; 105 std::string device_id;
104 url::Origin security_origin; 106 url::Origin security_origin;
105 }; 107 };
106 108
107 // Custom compare operator for the AudioRendererMixerMap. Allows reuse of 109 // Custom compare operator for the AudioRendererMixerMap. Allows reuse of
108 // mixers where only irrelevant keys mismatch; e.g., effects, bits per sample. 110 // mixers where only irrelevant keys mismatch; e.g., effects, bits per sample.
109 struct MixerKeyCompare { 111 struct MixerKeyCompare {
110 bool operator()(const MixerKey& a, const MixerKey& b) const { 112 bool operator()(const MixerKey& a, const MixerKey& b) const {
111 if (a.source_render_frame_id != b.source_render_frame_id) 113 if (a.source_render_frame_id != b.source_render_frame_id)
112 return a.source_render_frame_id < b.source_render_frame_id; 114 return a.source_render_frame_id < b.source_render_frame_id;
113 if (a.params.channels() != b.params.channels()) 115 if (a.params.channels() != b.params.channels())
114 return a.params.channels() < b.params.channels(); 116 return a.params.channels() < b.params.channels();
115 117
116 // Ignore effects(), bits_per_sample(), format(), and frames_per_buffer(), 118 // Ignore effects(), bits_per_sample(), format(), and frames_per_buffer(),
117 // these parameters do not affect mixer reuse. All AudioRendererMixer 119 // these parameters do not affect mixer reuse. All AudioRendererMixer
118 // units disable FIFO, so frames_per_buffer() can be safely ignored. 120 // units disable FIFO, so frames_per_buffer() can be safely ignored.
119 if (a.params.channel_layout() != b.params.channel_layout()) 121 if (a.params.channel_layout() != b.params.channel_layout())
120 return a.params.channel_layout() < b.params.channel_layout(); 122 return a.params.channel_layout() < b.params.channel_layout();
121 123
122 if (media::AudioDeviceDescription::IsDefaultDevice(a.device_id) && 124 if (media::AudioDeviceDescription::IsDefaultDevice(a.device_id) &&
123 media::AudioDeviceDescription::IsDefaultDevice(b.device_id)) { 125 media::AudioDeviceDescription::IsDefaultDevice(b.device_id)) {
124 // Both device IDs represent the same default device => do not compare 126 // Both device IDs represent the same default device => do not compare
125 // them; the default device is always authorized => ignoring security 127 // them; the default device is always authorized => ignoring security
126 // origin. 128 // origin.
127 return false; 129 return CompareLatency(a, b);
chcunningham 2016/06/22 02:13:56 Can you CompareLatency just once above this if and
o1ka 2016/06/23 16:36:15 It's me who's missed something :) Done.
128 } 130 }
129 131
130 if (a.device_id != b.device_id) 132 if (a.device_id != b.device_id)
131 return a.device_id < b.device_id; 133 return a.device_id < b.device_id;
132 134
135 if (a.security_origin == b.security_origin)
136 return CompareLatency(a, b);
137
133 return a.security_origin < b.security_origin; 138 return a.security_origin < b.security_origin;
134 } 139 }
140
141 bool CompareLatency(const MixerKey& a, const MixerKey& b) const {
142 // LATENCY_EXACT_MS can be mixed only with the same latency type and the
143 // same required buffer duration.
144 if ((a.latency == media::AudioLatency::LATENCY_EXACT_MS) &&
145 (b.latency == media::AudioLatency::LATENCY_EXACT_MS))
146 return a.params.GetBufferDuration() < b.params.GetBufferDuration();
147
148 return a.latency < b.latency;
149 }
135 }; 150 };
136 151
137 // Map of MixerKey to <AudioRendererMixer, Count>. Count allows 152 // Map of MixerKey to <AudioRendererMixer, Count>. Count allows
138 // AudioRendererMixerManager to keep track explicitly (v.s. RefCounted which 153 // AudioRendererMixerManager to keep track explicitly (v.s. RefCounted which
139 // is implicit) of the number of outstanding AudioRendererMixers. 154 // is implicit) of the number of outstanding AudioRendererMixers.
140 struct AudioRendererMixerReference { 155 struct AudioRendererMixerReference {
141 media::AudioRendererMixer* mixer; 156 media::AudioRendererMixer* mixer;
142 int ref_count; 157 int ref_count;
143 // Mixer sink pointer, to remove a sink from cache upon mixer destruction. 158 // Mixer sink pointer, to remove a sink from cache upon mixer destruction.
144 const media::AudioRendererSink* sink_ptr; 159 const media::AudioRendererSink* sink_ptr;
145 }; 160 };
146 161
147 using AudioRendererMixerMap = 162 using AudioRendererMixerMap =
148 std::map<MixerKey, AudioRendererMixerReference, MixerKeyCompare>; 163 std::map<MixerKey, AudioRendererMixerReference, MixerKeyCompare>;
149 164
150 // Active mixers. 165 // Active mixers.
151 AudioRendererMixerMap mixers_; 166 AudioRendererMixerMap mixers_;
152 base::Lock mixers_lock_; 167 base::Lock mixers_lock_;
153 168
154 // Mixer sink cache. 169 // Mixer sink cache.
155 const std::unique_ptr<AudioRendererSinkCache> sink_cache_; 170 const std::unique_ptr<AudioRendererSinkCache> sink_cache_;
156 171
157 DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerManager); 172 DISALLOW_COPY_AND_ASSIGN(AudioRendererMixerManager);
158 }; 173 };
159 174
160 } // namespace content 175 } // namespace content
161 176
162 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_MIXER_MANAGER_H_ 177 #endif // CONTENT_RENDERER_MEDIA_AUDIO_RENDERER_MIXER_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698