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