Add some instrumentation for jank in URLRequest::Start.
[chromium-blink-merge.git] / media / audio / audio_output_resampler.h
blob18d4905b801bf84f859aa6e240f4bb589e947cf2
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
3 // found in the LICENSE file.
5 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_
6 #define MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_
8 #include <map>
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/time/time.h"
13 #include "base/timer/timer.h"
14 #include "media/audio/audio_io.h"
15 #include "media/audio/audio_manager.h"
16 #include "media/audio/audio_output_dispatcher_impl.h"
17 #include "media/audio/audio_parameters.h"
19 namespace media {
21 class OnMoreDataConverter;
23 // AudioOutputResampler is a browser-side resampling and buffering solution
24 // which ensures audio data is always output at given parameters. See the
25 // AudioConverter class for details on the conversion process.
27 // AOR works by intercepting the AudioSourceCallback provided to StartStream()
28 // and redirecting it through an AudioConverter instance. |total_bytes_delay|
29 // is adjusted for buffer delay caused by the conversion process.
31 // AOR will automatically fall back from AUDIO_PCM_LOW_LATENCY to
32 // AUDIO_PCM_LINEAR if the output device fails to open at the requested output
33 // parameters. If opening still fails, it will fallback to AUDIO_FAKE.
34 class MEDIA_EXPORT AudioOutputResampler : public AudioOutputDispatcher {
35 public:
36 AudioOutputResampler(AudioManager* audio_manager,
37 const AudioParameters& input_params,
38 const AudioParameters& output_params,
39 const std::string& output_device_id,
40 const base::TimeDelta& close_delay);
42 // AudioOutputDispatcher interface.
43 bool OpenStream() override;
44 bool StartStream(AudioOutputStream::AudioSourceCallback* callback,
45 AudioOutputProxy* stream_proxy) override;
46 void StopStream(AudioOutputProxy* stream_proxy) override;
47 void StreamVolumeSet(AudioOutputProxy* stream_proxy, double volume) override;
48 void CloseStream(AudioOutputProxy* stream_proxy) override;
49 void Shutdown() override;
51 private:
52 friend class base::RefCountedThreadSafe<AudioOutputResampler>;
53 ~AudioOutputResampler() override;
55 // Converts low latency based output parameters into high latency
56 // appropriate output parameters in error situations.
57 void SetupFallbackParams();
59 // Used to reinitialize |dispatcher_|.
60 void Reinitialize();
62 // Used to initialize |dispatcher_|.
63 void Initialize();
65 // Dispatcher to proxy all AudioOutputDispatcher calls too.
66 scoped_refptr<AudioOutputDispatcherImpl> dispatcher_;
68 // Map of outstanding OnMoreDataConverter objects. A new object is created
69 // on every StartStream() call and destroyed on CloseStream().
70 typedef std::map<AudioOutputProxy*, OnMoreDataConverter*> CallbackMap;
71 CallbackMap callbacks_;
73 // Used by AudioOutputDispatcherImpl; kept so we can reinitialize on the fly.
74 base::TimeDelta close_delay_;
76 // AudioParameters used to setup the output stream; changed upon fallback.
77 AudioParameters output_params_;
79 // The original AudioParameters we were constructed with.
80 const AudioParameters original_output_params_;
82 // Whether any streams have been opened through |dispatcher_|, if so we can't
83 // fallback on future OpenStream() failures.
84 bool streams_opened_;
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_;
92 DISALLOW_COPY_AND_ASSIGN(AudioOutputResampler);
95 } // namespace media
97 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_RESAMPLER_H_