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.
4 
5 // AudioOutputDispatcherImpl is an implementation of AudioOutputDispatcher.
6 //
7 // To avoid opening and closing audio devices more frequently than necessary,
8 // each dispatcher has a pool of inactive physical streams. A stream is closed
9 // only if it hasn't been used for a certain period of time (specified via the
10 // constructor).
11 //
12 
13 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_
14 #define MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_
15 
16 #include <stddef.h>
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 #include "base/containers/flat_map.h"
23 #include "base/macros.h"
24 #include "base/timer/timer.h"
25 #include "media/audio/audio_io.h"
26 #include "media/audio/audio_manager.h"
27 #include "media/audio/audio_output_dispatcher.h"
28 #include "media/base/audio_parameters.h"
29 
30 namespace media {
31 class AudioLog;
32 
33 class MEDIA_EXPORT AudioOutputDispatcherImpl
34     : public AudioOutputDispatcher,
35       public AudioManager::AudioDeviceListener {
36  public:
37   // |close_delay| specifies delay after the stream is idle until the audio
38   // device is closed.
39   AudioOutputDispatcherImpl(AudioManager* audio_manager,
40                             const AudioParameters& params,
41                             const std::string& output_device_id,
42                             base::TimeDelta close_delay);
43   ~AudioOutputDispatcherImpl() override;
44 
45   // AudioOutputDispatcher implementation.
46   AudioOutputProxy* CreateStreamProxy() override;
47   bool OpenStream() override;
48   bool StartStream(AudioOutputStream::AudioSourceCallback* callback,
49                    AudioOutputProxy* stream_proxy) override;
50   void StopStream(AudioOutputProxy* stream_proxy) override;
51   void StreamVolumeSet(AudioOutputProxy* stream_proxy, double volume) override;
52   void CloseStream(AudioOutputProxy* stream_proxy) override;
53   void FlushStream(AudioOutputProxy* stream_proxy) override;
54 
55   // AudioDeviceListener implementation.
56   void OnDeviceChange() override;
57 
58   // Returns true if there are any open AudioOutputProxy objects.
59   bool HasOutputProxies() const;
60 
61   // Closes all |idle_streams_|.
62   void CloseAllIdleStreams();
63 
64  private:
65   // Creates a new physical output stream, opens it and pushes to
66   // |idle_streams_|.  Returns false if the stream couldn't be created or
67   // opened.
68   bool CreateAndOpenStream();
69 
70   // Similar to CloseAllIdleStreams(), but keeps |keep_alive| streams alive.
71   void CloseIdleStreams(size_t keep_alive);
72 
73   void StopPhysicalStream(AudioOutputStream* stream);
74 
75   // Output parameters.
76   const AudioParameters params_;
77 
78   // Output device id.
79   const std::string device_id_;
80 
81   size_t idle_proxies_;
82   std::vector<AudioOutputStream*> idle_streams_;
83 
84   // When streams are stopped they're added to |idle_streams_|, if no stream is
85   // reused before |close_delay_| elapses |close_timer_| will run
86   // CloseIdleStreams().
87   base::DelayTimer close_timer_;
88 
89   typedef base::flat_map<AudioOutputProxy*, AudioOutputStream*> AudioStreamMap;
90   AudioStreamMap proxy_to_physical_map_;
91 
92   using AudioLogMap =
93       base::flat_map<AudioOutputStream*, std::unique_ptr<media::AudioLog>>;
94   AudioLogMap audio_logs_;
95   int audio_stream_id_;
96 
97   base::WeakPtrFactory<AudioOutputDispatcherImpl> weak_factory_{this};
98   DISALLOW_COPY_AND_ASSIGN(AudioOutputDispatcherImpl);
99 };
100 
101 }  // namespace media
102 
103 #endif  // MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_
104