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// AudioInputDeviceManager manages the audio input devices. In particular it
6// communicates with MediaStreamManager and AudioInputRendererHost on the
7// browser IO thread, handles queries like
8// enumerate/open/close/GetOpenedDeviceInfoById from MediaStreamManager and
9// GetOpenedDeviceInfoById from AudioInputRendererHost.
10// The work for enumerate/open/close is handled asynchronously on Media Stream
11// device thread, while GetOpenedDeviceInfoById is synchronous on the IO thread.
12
13#ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_DEVICE_MANAGER_H_
14#define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_DEVICE_MANAGER_H_
15
16#include <map>
17
18#include "base/basictypes.h"
19#include "base/memory/ref_counted.h"
20#include "base/threading/thread.h"
21#include "content/browser/renderer_host/media/media_stream_provider.h"
22#include "content/common/content_export.h"
23#include "content/common/media/media_stream_options.h"
24#include "content/public/common/media_stream_request.h"
25
26namespace media {
27class AudioManager;
28}
29
30namespace content {
31
32class CONTENT_EXPORT AudioInputDeviceManager : public MediaStreamProvider {
33 public:
34  // Calling Start() with this kFakeOpenSessionId will open the default device,
35  // even though Open() has not been called. This is used to be able to use the
36  // AudioInputDeviceManager before MediaStream is implemented.
37  // TODO(xians): Remove it when the webrtc unittest does not need it any more.
38  static const int kFakeOpenSessionId;
39
40  explicit AudioInputDeviceManager(media::AudioManager* audio_manager);
41
42  // Gets the opened device info by |session_id|. Returns NULL if the device
43  // is not opened, otherwise the opened device. Called on IO thread.
44  const StreamDeviceInfo* GetOpenedDeviceInfoById(int session_id);
45
46  // MediaStreamProvider implementation, called on IO thread.
47  virtual void Register(MediaStreamProviderListener* listener,
48                        base::MessageLoopProxy* device_thread_loop) OVERRIDE;
49  virtual void Unregister() OVERRIDE;
50  virtual void EnumerateDevices(MediaStreamType stream_type) OVERRIDE;
51  virtual int Open(const StreamDeviceInfo& device) OVERRIDE;
52  virtual void Close(int session_id) OVERRIDE;
53
54  void UseFakeDevice();
55  bool ShouldUseFakeDevice() const;
56
57 private:
58  typedef std::vector<StreamDeviceInfo> StreamDeviceList;
59  virtual ~AudioInputDeviceManager();
60
61  // Enumerates audio input devices on media stream device thread.
62  void EnumerateOnDeviceThread(MediaStreamType stream_type);
63  // Opens the device on media stream device thread.
64  void OpenOnDeviceThread(int session_id, const StreamDeviceInfo& info);
65
66  // Callback used by EnumerateOnDeviceThread(), called with a list of
67  // enumerated devices on IO thread.
68  void DevicesEnumeratedOnIOThread(MediaStreamType stream_type,
69                                   scoped_ptr<StreamDeviceInfoArray> devices);
70  // Callback used by OpenOnDeviceThread(), called with the session_id
71  // referencing the opened device on IO thread.
72  void OpenedOnIOThread(int session_id, const StreamDeviceInfo& info);
73  // Callback used by CloseOnDeviceThread(), called with the session_id
74  // referencing the closed device on IO thread.
75  void ClosedOnIOThread(MediaStreamType type, int session_id);
76
77  // Verifies that the calling thread is media stream device thread.
78  bool IsOnDeviceThread() const;
79
80  // Helper to return iterator to the device referenced by |session_id|. If no
81  // device is found, it will return devices_.end().
82  StreamDeviceList::iterator GetDevice(int session_id);
83
84  // Only accessed on Browser::IO thread.
85  MediaStreamProviderListener* listener_;
86  int next_capture_session_id_;
87  bool use_fake_device_;
88  StreamDeviceList devices_;
89
90  media::AudioManager* const audio_manager_;  // Weak.
91
92  // The message loop of media stream device thread that this object runs on.
93  scoped_refptr<base::MessageLoopProxy> device_loop_;
94
95  DISALLOW_COPY_AND_ASSIGN(AudioInputDeviceManager);
96};
97
98}  // namespace content
99
100#endif  // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_DEVICE_MANAGER_H_
101