Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / content / browser / renderer_host / media / audio_input_device_manager.h
blob231f30b3a3294b151065470a83d4135754f6347f
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.
13 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_DEVICE_MANAGER_H_
14 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_DEVICE_MANAGER_H_
16 #include <map>
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"
26 namespace media {
27 class AudioManager;
30 namespace content {
32 class 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;
40 explicit AudioInputDeviceManager(media::AudioManager* audio_manager);
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);
46 // MediaStreamProvider implementation, called on IO thread.
47 virtual void Register(MediaStreamProviderListener* listener,
48 const scoped_refptr<base::SingleThreadTaskRunner>&
49 device_task_runner) OVERRIDE;
50 virtual void Unregister() OVERRIDE;
51 virtual void EnumerateDevices(MediaStreamType stream_type) OVERRIDE;
52 virtual int Open(const StreamDeviceInfo& device) OVERRIDE;
53 virtual void Close(int session_id) OVERRIDE;
55 void UseFakeDevice();
56 bool ShouldUseFakeDevice() const;
58 private:
59 typedef std::vector<StreamDeviceInfo> StreamDeviceList;
60 virtual ~AudioInputDeviceManager();
62 // Enumerates audio input devices on media stream device thread.
63 void EnumerateOnDeviceThread(MediaStreamType stream_type);
64 // Opens the device on media stream device thread.
65 void OpenOnDeviceThread(int session_id, const StreamDeviceInfo& info);
67 // Callback used by EnumerateOnDeviceThread(), called with a list of
68 // enumerated devices on IO thread.
69 void DevicesEnumeratedOnIOThread(MediaStreamType stream_type,
70 scoped_ptr<StreamDeviceInfoArray> devices);
71 // Callback used by OpenOnDeviceThread(), called with the session_id
72 // referencing the opened device on IO thread.
73 void OpenedOnIOThread(int session_id, const StreamDeviceInfo& info);
74 // Callback used by CloseOnDeviceThread(), called with the session_id
75 // referencing the closed device on IO thread.
76 void ClosedOnIOThread(MediaStreamType type, int session_id);
78 // Verifies that the calling thread is media stream device thread.
79 bool IsOnDeviceThread() const;
81 // Helper to return iterator to the device referenced by |session_id|. If no
82 // device is found, it will return devices_.end().
83 StreamDeviceList::iterator GetDevice(int session_id);
85 // Only accessed on Browser::IO thread.
86 MediaStreamProviderListener* listener_;
87 int next_capture_session_id_;
88 bool use_fake_device_;
89 StreamDeviceList devices_;
91 media::AudioManager* const audio_manager_; // Weak.
93 // The message loop of media stream device thread that this object runs on.
94 scoped_refptr<base::SingleThreadTaskRunner> device_task_runner_;
96 DISALLOW_COPY_AND_ASSIGN(AudioInputDeviceManager);
99 } // namespace content
101 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_INPUT_DEVICE_MANAGER_H_