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 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_ | 5 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_ |
6 #define MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_ | 6 #define MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "base/timer/timer.h" |
13 #include "media/audio/audio_io.h" | 14 #include "media/audio/audio_io.h" |
14 #include "media/audio/audio_manager.h" | 15 #include "media/audio/audio_manager.h" |
15 #include "media/audio/audio_output_dispatcher.h" | 16 #include "media/audio/audio_output_dispatcher_impl.h" |
16 #include "media/audio/audio_parameters.h" | 17 #include "media/audio/audio_parameters.h" |
17 | 18 |
18 namespace media { | 19 namespace media { |
19 | 20 |
20 class OnMoreDataConverter; | 21 class OnMoreDataConverter; |
21 | 22 |
22 // AudioOutputResampler is a browser-side resampling and buffering solution | 23 // AudioOutputResampler is a browser-side resampling and buffering solution |
23 // which ensures audio data is always output at given parameters. See the | 24 // which ensures audio data is always output at given parameters. See the |
24 // AudioConverter class for details on the conversion process. | 25 // AudioConverter class for details on the conversion process. |
25 // | 26 // |
26 // AOR works by intercepting the AudioSourceCallback provided to StartStream() | 27 // AOR works by intercepting the AudioSourceCallback provided to StartStream() |
27 // and redirecting it through an AudioConverter instance. |total_bytes_delay| | 28 // and redirecting it through an AudioConverter instance. |total_bytes_delay| |
28 // is adjusted for buffer delay caused by the conversion process. | 29 // is adjusted for buffer delay caused by the conversion process. |
29 // | 30 // |
30 // AOR will automatically fall back from AUDIO_PCM_LOW_LATENCY to | 31 // AOR will automatically fall back from AUDIO_PCM_LOW_LATENCY to |
31 // AUDIO_PCM_LINEAR if the output device fails to open at the requested output | 32 // AUDIO_PCM_LINEAR if the output device fails to open at the requested output |
32 // parameters. | 33 // parameters. If opening still fails, it will fallback to AUDIO_FAKE. |
33 // | |
34 // TODO(dalecurtis): Ideally the low latency path will be as reliable as the | |
35 // high latency path once we have channel mixing and support querying for the | |
36 // hardware's configured bit depth. Monitor the UMA stats for fallback and | |
37 // remove fallback support once it's stable. http://crbug.com/148418 | |
38 class MEDIA_EXPORT AudioOutputResampler : public AudioOutputDispatcher { | 34 class MEDIA_EXPORT AudioOutputResampler : public AudioOutputDispatcher { |
39 public: | 35 public: |
40 AudioOutputResampler(AudioManager* audio_manager, | 36 AudioOutputResampler(AudioManager* audio_manager, |
41 const AudioParameters& input_params, | 37 const AudioParameters& input_params, |
42 const AudioParameters& output_params, | 38 const AudioParameters& output_params, |
43 const std::string& output_device_id, | 39 const std::string& output_device_id, |
44 const base::TimeDelta& close_delay); | 40 const base::TimeDelta& close_delay); |
45 | 41 |
46 // AudioOutputDispatcher interface. | 42 // AudioOutputDispatcher interface. |
47 bool OpenStream() override; | 43 bool OpenStream() override; |
48 bool StartStream(AudioOutputStream::AudioSourceCallback* callback, | 44 bool StartStream(AudioOutputStream::AudioSourceCallback* callback, |
49 AudioOutputProxy* stream_proxy) override; | 45 AudioOutputProxy* stream_proxy) override; |
50 void StopStream(AudioOutputProxy* stream_proxy) override; | 46 void StopStream(AudioOutputProxy* stream_proxy) override; |
51 void StreamVolumeSet(AudioOutputProxy* stream_proxy, double volume) override; | 47 void StreamVolumeSet(AudioOutputProxy* stream_proxy, double volume) override; |
52 void CloseStream(AudioOutputProxy* stream_proxy) override; | 48 void CloseStream(AudioOutputProxy* stream_proxy) override; |
53 void Shutdown() override; | 49 void Shutdown() override; |
54 | 50 |
55 private: | 51 private: |
56 friend class base::RefCountedThreadSafe<AudioOutputResampler>; | 52 friend class base::RefCountedThreadSafe<AudioOutputResampler>; |
57 ~AudioOutputResampler() override; | 53 ~AudioOutputResampler() override; |
58 | 54 |
59 // Converts low latency based output parameters into high latency | 55 // Converts low latency based output parameters into high latency |
60 // appropriate output parameters in error situations. | 56 // appropriate output parameters in error situations. |
61 void SetupFallbackParams(); | 57 void SetupFallbackParams(); |
62 | 58 |
63 // Used to initialize and reinitialize |dispatcher_|. | 59 // Used to reinitialize |dispatcher_|. |
| 60 void Reinitialize(); |
| 61 |
| 62 // Used to initialize |dispatcher_|. |
64 void Initialize(); | 63 void Initialize(); |
65 | 64 |
66 // Dispatcher to proxy all AudioOutputDispatcher calls too. | 65 // Dispatcher to proxy all AudioOutputDispatcher calls too. |
67 scoped_refptr<AudioOutputDispatcher> dispatcher_; | 66 scoped_refptr<AudioOutputDispatcherImpl> dispatcher_; |
68 | 67 |
69 // Map of outstanding OnMoreDataConverter objects. A new object is created | 68 // Map of outstanding OnMoreDataConverter objects. A new object is created |
70 // on every StartStream() call and destroyed on CloseStream(). | 69 // on every StartStream() call and destroyed on CloseStream(). |
71 typedef std::map<AudioOutputProxy*, OnMoreDataConverter*> CallbackMap; | 70 typedef std::map<AudioOutputProxy*, OnMoreDataConverter*> CallbackMap; |
72 CallbackMap callbacks_; | 71 CallbackMap callbacks_; |
73 | 72 |
74 // Used by AudioOutputDispatcherImpl; kept so we can reinitialize on the fly. | 73 // Used by AudioOutputDispatcherImpl; kept so we can reinitialize on the fly. |
75 base::TimeDelta close_delay_; | 74 base::TimeDelta close_delay_; |
76 | 75 |
77 // AudioParameters used to setup the output stream. | 76 // AudioParameters used to setup the output stream; changed upon fallback. |
78 AudioParameters output_params_; | 77 AudioParameters output_params_; |
79 | 78 |
| 79 // The original AudioParameters we were constructed with. |
| 80 const AudioParameters original_output_params_; |
| 81 |
80 // Whether any streams have been opened through |dispatcher_|, if so we can't | 82 // Whether any streams have been opened through |dispatcher_|, if so we can't |
81 // fallback on future OpenStream() failures. | 83 // fallback on future OpenStream() failures. |
82 bool streams_opened_; | 84 bool streams_opened_; |
83 | 85 |
| 86 // The reinitialization timer provides a way to recover from temporary failure |
| 87 // states by clearing the dispatcher if all proxies have been closed and none |
| 88 // have been created within |close_delay_|. Without this, audio may be lost |
| 89 // to a fake stream indefinitely for transient errors. |
| 90 base::Timer reinitialize_timer_; |
| 91 |
84 DISALLOW_COPY_AND_ASSIGN(AudioOutputResampler); | 92 DISALLOW_COPY_AND_ASSIGN(AudioOutputResampler); |
85 }; | 93 }; |
86 | 94 |
87 } // namespace media | 95 } // namespace media |
88 | 96 |
89 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_ | 97 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_ |
OLD | NEW |