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_OUTPUT_IMPL_H_
6#define CONTENT_RENDERER_PEPPER_PEPPER_PLATFORM_AUDIO_OUTPUT_IMPL_H_
7
8#include "base/basictypes.h"
9#include "base/memory/ref_counted.h"
10#include "base/memory/scoped_ptr.h"
11#include "media/audio/audio_output_ipc.h"
12
13namespace media {
14class AudioParameters;
15}
16
17namespace base {
18class MessageLoopProxy;
19}
20
21namespace content {
22class AudioHelper;
23
24class PepperPlatformAudioOutput
25    : public media::AudioOutputIPCDelegate,
26      public base::RefCountedThreadSafe<PepperPlatformAudioOutput> {
27 public:
28  // Factory function, returns NULL on failure. StreamCreated() will be called
29  // when the stream is created.
30  static PepperPlatformAudioOutput* Create(int sample_rate,
31                                           int frames_per_buffer,
32                                           int source_render_view_id,
33                                           int source_render_frame_id,
34                                           AudioHelper* client);
35
36  // The following three methods are all called on main thread.
37
38  // Starts the playback. Returns false on error or if called before the
39  // stream is created or after the stream is closed.
40  bool StartPlayback();
41
42  // Stops the playback. Returns false on error or if called before the stream
43  // is created or after the stream is closed.
44  bool StopPlayback();
45
46  // Closes the stream. Make sure to call this before the object is
47  // destructed.
48  void ShutDown();
49
50  // media::AudioOutputIPCDelegate implementation.
51  virtual void OnStateChanged(media::AudioOutputIPCDelegate::State state)
52      OVERRIDE;
53  virtual void OnStreamCreated(base::SharedMemoryHandle handle,
54                               base::SyncSocket::Handle socket_handle,
55                               int length) OVERRIDE;
56  virtual void OnIPCClosed() OVERRIDE;
57
58 protected:
59  virtual ~PepperPlatformAudioOutput();
60
61 private:
62  friend class base::RefCountedThreadSafe<PepperPlatformAudioOutput>;
63
64  PepperPlatformAudioOutput();
65
66  bool Initialize(int sample_rate,
67                  int frames_per_buffer,
68                  int source_render_view_id,
69                  int source_render_frame_id,
70                  AudioHelper* client);
71
72  // I/O thread backends to above functions.
73  void InitializeOnIOThread(const media::AudioParameters& params);
74  void StartPlaybackOnIOThread();
75  void StopPlaybackOnIOThread();
76  void ShutDownOnIOThread();
77
78  // The client to notify when the stream is created. THIS MUST ONLY BE
79  // ACCESSED ON THE MAIN THREAD.
80  AudioHelper* client_;
81
82  // Used to send/receive IPC. THIS MUST ONLY BE ACCESSED ON THE
83  // I/O thread except to send messages and get the message loop.
84  scoped_ptr<media::AudioOutputIPC> ipc_;
85
86  scoped_refptr<base::MessageLoopProxy> main_message_loop_proxy_;
87  scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
88
89  DISALLOW_COPY_AND_ASSIGN(PepperPlatformAudioOutput);
90};
91
92}  // namespace content
93
94#endif  // CONTENT_RENDERER_PEPPER_PEPPER_PLATFORM_AUDIO_OUTPUT_IMPL_H_
95