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#ifndef CONTENT_RENDERER_PEPPER_PEPPER_PLATFORM_AUDIO_INPUT_H_
6#define CONTENT_RENDERER_PEPPER_PEPPER_PLATFORM_AUDIO_INPUT_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/compiler_specific.h"
12#include "base/memory/ref_counted.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/memory/weak_ptr.h"
15#include "media/audio/audio_input_ipc.h"
16#include "media/audio/audio_parameters.h"
17
18class GURL;
19
20namespace base {
21class MessageLoopProxy;
22}
23
24namespace media {
25class AudioParameters;
26}
27
28namespace content {
29
30class PepperAudioInputHost;
31class PepperMediaDeviceManager;
32
33// PepperPlatformAudioInput is operated on two threads: the main thread (the
34// thread on which objects are created) and the I/O thread. All public methods,
35// except the destructor, must be called on the main thread. The notifications
36// to the users of this class (i.e. PepperAudioInputHost) are also sent on the
37// main thread. Internally, this class sends audio input IPC messages and
38// receives media::AudioInputIPCDelegate notifications on the I/O thread.
39
40class PepperPlatformAudioInput
41    : public media::AudioInputIPCDelegate,
42      public base::RefCountedThreadSafe<PepperPlatformAudioInput> {
43 public:
44  // Factory function, returns NULL on failure. StreamCreated() will be called
45  // when the stream is created.
46  static PepperPlatformAudioInput* Create(
47      int render_frame_id,
48      const std::string& device_id,
49      const GURL& document_url,
50      int sample_rate,
51      int frames_per_buffer,
52      PepperAudioInputHost* client);
53
54  // Called on main thread.
55  void StartCapture();
56  void StopCapture();
57  // Closes the stream. Make sure to call this before the object is destructed.
58  void ShutDown();
59
60  // media::AudioInputIPCDelegate.
61  virtual void OnStreamCreated(base::SharedMemoryHandle handle,
62                               base::SyncSocket::Handle socket_handle,
63                               int length,
64                               int total_segments) OVERRIDE;
65  virtual void OnVolume(double volume) OVERRIDE;
66  virtual void OnStateChanged(media::AudioInputIPCDelegate::State state)
67      OVERRIDE;
68  virtual void OnIPCClosed() OVERRIDE;
69
70 protected:
71  virtual ~PepperPlatformAudioInput();
72
73 private:
74  friend class base::RefCountedThreadSafe<PepperPlatformAudioInput>;
75
76  PepperPlatformAudioInput();
77
78  bool Initialize(int render_frame_id,
79                  const std::string& device_id,
80                  const GURL& document_url,
81                  int sample_rate,
82                  int frames_per_buffer,
83                  PepperAudioInputHost* client);
84
85  // I/O thread backends to above functions.
86  void InitializeOnIOThread(int session_id);
87  void StartCaptureOnIOThread();
88  void StopCaptureOnIOThread();
89  void ShutDownOnIOThread();
90
91  void OnDeviceOpened(int request_id, bool succeeded, const std::string& label);
92  void CloseDevice();
93  void NotifyStreamCreationFailed();
94
95  // Can return NULL if the RenderFrame referenced by |render_frame_id_| has
96  // gone away.
97  PepperMediaDeviceManager* GetMediaDeviceManager();
98
99  // The client to notify when the stream is created. THIS MUST ONLY BE
100  // ACCESSED ON THE MAIN THREAD.
101  PepperAudioInputHost* client_;
102
103  // Used to send/receive IPC. THIS MUST ONLY BE ACCESSED ON THE
104  // I/O THREAD.
105  scoped_ptr<media::AudioInputIPC> ipc_;
106
107  scoped_refptr<base::MessageLoopProxy> main_message_loop_proxy_;
108  scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
109
110  // The frame containing the Pepper widget.
111  int render_frame_id_;
112
113  // The unique ID to identify the opened device. THIS MUST ONLY BE ACCESSED ON
114  // THE MAIN THREAD.
115  std::string label_;
116
117  // Initialized on the main thread and accessed on the I/O thread afterwards.
118  media::AudioParameters params_;
119
120  // Whether we have tried to create an audio stream. THIS MUST ONLY BE ACCESSED
121  // ON THE I/O THREAD.
122  bool create_stream_sent_;
123
124  // Whether we have a pending request to open a device. We have to make sure
125  // there isn't any pending request before this object goes away.
126  // THIS MUST ONLY BE ACCESSED ON THE MAIN THREAD.
127  bool pending_open_device_;
128  // THIS MUST ONLY BE ACCESSED ON THE MAIN THREAD.
129  int pending_open_device_id_;
130
131  DISALLOW_COPY_AND_ASSIGN(PepperPlatformAudioInput);
132};
133
134}  // namespace content
135
136#endif  // CONTENT_RENDERER_PEPPER_PEPPER_PLATFORM_AUDIO_INPUT_H_
137