audio_device_thread.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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             base::SharedMemoryHandle memory,
41             int memory_length,
42             int total_segments);
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 samples_per_ms_;
61    const int bytes_per_ms_;
62
63    base::SharedMemory shared_memory_;
64    const int memory_length_;
65    const int total_segments_;
66    int segment_length_;
67
68   private:
69    DISALLOW_COPY_AND_ASSIGN(Callback);
70  };
71
72  AudioDeviceThread();
73  ~AudioDeviceThread();
74
75  // Starts the audio thread. The thread must not already be running.
76  void Start(AudioDeviceThread::Callback* callback,
77             base::SyncSocket::Handle socket,
78             const char* thread_name);
79
80  // This tells the audio thread to stop and clean up the data.
81  // The method can stop the thread synchronously or asynchronously.
82  // In the latter case, the thread will still be running after Stop()
83  // returns, but the callback pointer is cleared so no further callbacks will
84  // be made (IOW after Stop() returns, it is safe to delete the callback).
85  // The |loop_for_join| parameter is required for asynchronous operation
86  // in order to join the worker thread and close the thread handle later via a
87  // posted task.
88  // If set to NULL, function will wait for the thread to exit before returning.
89  void Stop(MessageLoop* loop_for_join);
90
91  // Returns true if the thread is stopped or stopping.
92  bool IsStopped();
93
94 private:
95  // Our own private SimpleThread override.  We implement this in a
96  // private class so that we get the following benefits:
97  // 1) AudioDeviceThread doesn't expose SimpleThread methods.
98  //    I.e. the caller can't call Start()/Stop() - which would be bad.
99  // 2) We override ThreadMain to add additional on-thread initialization
100  //    while still synchronized with SimpleThread::Start() to provide
101  //    reliable initialization.
102  class Thread;
103
104  base::Lock thread_lock_;
105  scoped_refptr<AudioDeviceThread::Thread> thread_;
106
107  DISALLOW_COPY_AND_ASSIGN(AudioDeviceThread);
108};
109
110}  // namespace media.
111
112#endif  // MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_
113