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