audio_device_thread.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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 MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_
6#define MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_
7
8#include "base/basictypes.h"
9#include "base/memory/ref_counted.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/shared_memory.h"
12#include "base/sync_socket.h"
13#include "base/synchronization/lock.h"
14#include "media/base/media_export.h"
15#include "media/audio/audio_parameters.h"
16#include "media/audio/shared_memory_util.h"
17
18class MessageLoop;
19
20namespace media {
21class AudioBus;
22
23// Data transfer between browser and render process uses a combination
24// of sync sockets and shared memory. To read from the socket and render
25// data, we use a worker thread, a.k.a. the AudioDeviceThread, which reads
26// data from the browser via the socket and fills the shared memory from the
27// audio thread via the AudioDeviceThread::Callback interface/class.
28// For more details see the documentation in audio_device.h.
29//
30// TODO(tommi): Multiple audio input/output device instances should be able to
31// share the same thread instead of spinning one per instance.
32class MEDIA_EXPORT AudioDeviceThread {
33 public:
34  // This is the callback interface/base class that Audio[Output|Input]Device
35  // implements to render input/output data. The callbacks run on the
36  // thread owned by AudioDeviceThread.
37  class Callback {
38   public:
39    Callback(const AudioParameters& audio_parameters,
40             int input_channels,
41             base::SharedMemoryHandle memory,
42             int memory_length);
43    virtual ~Callback();
44
45    // One time initialization for the callback object on the audio thread.
46    void InitializeOnAudioThread();
47
48    // Derived implementations must call shared_memory_.Map appropriately
49    // before Process can be called.
50    virtual void MapSharedMemory() = 0;
51
52    // Called whenever we receive notifications about pending data.
53    virtual void Process(int pending_data) = 0;
54
55   protected:
56    // Protected so that derived classes can access directly.
57    // The variables are 'const' since values are calculated/set in the
58    // constructor and must never change.
59    const AudioParameters audio_parameters_;
60    const int input_channels_;
61    const int samples_per_ms_;
62    const int bytes_per_ms_;
63
64    base::SharedMemory shared_memory_;
65    const int memory_length_;
66
67   private:
68    DISALLOW_COPY_AND_ASSIGN(Callback);
69  };
70
71  AudioDeviceThread();
72  ~AudioDeviceThread();
73
74  // Starts the audio thread. The thread must not already be running.
75  void Start(AudioDeviceThread::Callback* callback,
76             base::SyncSocket::Handle socket,
77             const char* thread_name);
78
79  // This tells the audio thread to stop and clean up the data.
80  // The method can stop the thread synchronously or asynchronously.
81  // In the latter case, the thread will still be running after Stop()
82  // returns, but the callback pointer is cleared so no further callbacks will
83  // be made (IOW after Stop() returns, it is safe to delete the callback).
84  // The |loop_for_join| parameter is required for asynchronous operation
85  // in order to join the worker thread and close the thread handle later via a
86  // posted task.
87  // If set to NULL, function will wait for the thread to exit before returning.
88  void Stop(MessageLoop* loop_for_join);
89
90  // Returns true if the thread is stopped or stopping.
91  bool IsStopped();
92
93 private:
94  // Our own private SimpleThread override.  We implement this in a
95  // private class so that we get the following benefits:
96  // 1) AudioDeviceThread doesn't expose SimpleThread methods.
97  //    I.e. the caller can't call Start()/Stop() - which would be bad.
98  // 2) We override ThreadMain to add additional on-thread initialization
99  //    while still synchronized with SimpleThread::Start() to provide
100  //    reliable initialization.
101  class Thread;
102
103  base::Lock thread_lock_;
104  scoped_refptr<AudioDeviceThread::Thread> thread_;
105
106  DISALLOW_COPY_AND_ASSIGN(AudioDeviceThread);
107};
108
109}  // namespace media.
110
111#endif  // MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_
112