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 <stdint.h> 9 10 #include "base/macros.h" 11 #include "base/sync_socket.h" 12 #include "base/threading/platform_thread.h" 13 #include "base/threading/thread_checker.h" 14 #include "media/base/audio_parameters.h" 15 #include "media/base/media_export.h" 16 17 namespace media { 18 19 // Data transfer between browser and render process uses a combination 20 // of sync sockets and shared memory. To read from the socket and render 21 // data, we use a worker thread, a.k.a. the AudioDeviceThread, which reads 22 // data from the browser via the socket and fills the shared memory from the 23 // audio thread via the AudioDeviceThread::Callback interface/class. 24 class MEDIA_EXPORT AudioDeviceThread : public base::PlatformThread::Delegate { 25 public: 26 // This is the callback interface/base class that Audio[Output|Input]Device 27 // implements to render input/output data. The callbacks run on the 28 // thread owned by AudioDeviceThread. 29 class Callback { 30 public: 31 Callback(const AudioParameters& audio_parameters, 32 uint32_t segment_length, 33 uint32_t total_segments); 34 35 // One time initialization for the callback object on the audio thread. 36 void InitializeOnAudioThread(); 37 38 // Derived implementations must map shared memory appropriately before 39 // Process can be called. 40 virtual void MapSharedMemory() = 0; 41 42 // Called whenever we receive notifications about pending input data. 43 virtual void Process(uint32_t pending_data) = 0; 44 buffer_duration()45 base::TimeDelta buffer_duration() const { 46 return audio_parameters_.GetBufferDuration(); 47 } 48 49 protected: 50 virtual ~Callback(); 51 52 // Protected so that derived classes can access directly. 53 // The variables are 'const' since values are calculated/set in the 54 // constructor and must never change. 55 const AudioParameters audio_parameters_; 56 57 const uint32_t memory_length_; 58 const uint32_t total_segments_; 59 const uint32_t segment_length_; 60 61 // Detached in constructor and attached in InitializeOnAudioThread() which 62 // is called on the audio device thread. Sub-classes can then use it for 63 // various thread checking purposes. 64 base::ThreadChecker thread_checker_; 65 66 private: 67 DISALLOW_COPY_AND_ASSIGN(Callback); 68 }; 69 70 // Creates and automatically starts the audio thread. 71 AudioDeviceThread(Callback* callback, 72 base::SyncSocket::ScopedHandle socket, 73 const char* thread_name, 74 base::ThreadPriority thread_priority); 75 76 // This tells the audio thread to stop and clean up the data; this is a 77 // synchronous process and the thread will stop before the method returns. 78 // Blocking call, see base/threading/thread_restrictions.h. 79 ~AudioDeviceThread() override; 80 81 private: 82 base::TimeDelta GetRealtimePeriod() final; 83 void ThreadMain() final; 84 85 Callback* const callback_; 86 const char* thread_name_; 87 base::CancelableSyncSocket socket_; 88 base::PlatformThreadHandle thread_handle_; 89 90 DISALLOW_COPY_AND_ASSIGN(AudioDeviceThread); 91 }; 92 93 } // namespace media. 94 95 #endif // MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_ 96