OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_ | 5 #ifndef MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_ |
6 #define MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_ | 6 #define MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/shared_memory.h" | 12 #include "base/memory/shared_memory.h" |
13 #include "base/sync_socket.h" | 13 #include "base/sync_socket.h" |
14 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 15 #include "media/audio/audio_io.h" |
15 #include "media/base/audio_parameters.h" | 16 #include "media/base/audio_parameters.h" |
16 #include "media/base/media_export.h" | 17 #include "media/base/media_export.h" |
17 | 18 |
18 namespace base { | 19 namespace base { |
19 class MessageLoop; | 20 class MessageLoop; |
20 } | 21 } |
21 | 22 |
22 namespace media { | 23 namespace media { |
23 class AudioBus; | 24 class AudioBus; |
| 25 struct AudioTimestamp; |
24 | 26 |
25 // Data transfer between browser and render process uses a combination | 27 // Data transfer between browser and render process uses a combination |
26 // of sync sockets and shared memory. To read from the socket and render | 28 // 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 | 29 // 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 | 30 // data from the browser via the socket and fills the shared memory from the |
29 // audio thread via the AudioDeviceThread::Callback interface/class. | 31 // audio thread via the AudioDeviceThread::Callback interface/class. |
30 // For more details see the documentation in audio_device.h. | 32 // For more details see the documentation in audio_device.h. |
31 // | 33 // |
32 // TODO(tommi): Multiple audio input/output device instances should be able to | 34 // TODO(tommi): Multiple audio input/output device instances should be able to |
33 // share the same thread instead of spinning one per instance. | 35 // share the same thread instead of spinning one per instance. |
(...skipping 11 matching lines...) Expand all Loading... |
45 virtual ~Callback(); | 47 virtual ~Callback(); |
46 | 48 |
47 // One time initialization for the callback object on the audio thread. | 49 // One time initialization for the callback object on the audio thread. |
48 void InitializeOnAudioThread(); | 50 void InitializeOnAudioThread(); |
49 | 51 |
50 // Derived implementations must call shared_memory_.Map appropriately | 52 // Derived implementations must call shared_memory_.Map appropriately |
51 // before Process can be called. | 53 // before Process can be called. |
52 virtual void MapSharedMemory() = 0; | 54 virtual void MapSharedMemory() = 0; |
53 | 55 |
54 // Called whenever we receive notifications about pending input data. | 56 // Called whenever we receive notifications about pending input data. |
55 virtual void Process(uint32_t pending_data) = 0; | 57 // It also provides the current audio stream timestamp on the device. |
| 58 virtual void Process(uint32_t pending_data, |
| 59 const AudioTimestamp& timestamp) = 0; |
56 | 60 |
57 protected: | 61 protected: |
58 // Protected so that derived classes can access directly. | 62 // Protected so that derived classes can access directly. |
59 // The variables are 'const' since values are calculated/set in the | 63 // The variables are 'const' since values are calculated/set in the |
60 // constructor and must never change. | 64 // constructor and must never change. |
61 const AudioParameters audio_parameters_; | 65 const AudioParameters audio_parameters_; |
62 const double samples_per_ms_; | 66 const double samples_per_ms_; |
63 const double bytes_per_ms_; | 67 const double bytes_per_ms_; |
64 const uint32_t bytes_per_frame_; | 68 const uint32_t bytes_per_frame_; |
65 | 69 |
66 base::SharedMemory shared_memory_; | 70 base::SharedMemory shared_memory_; |
67 const int memory_length_; | 71 const int memory_length_; |
68 const int total_segments_; | 72 const int total_segments_; |
69 int segment_length_; | 73 int segment_length_; |
70 | 74 |
71 private: | 75 private: |
72 DISALLOW_COPY_AND_ASSIGN(Callback); | 76 DISALLOW_COPY_AND_ASSIGN(Callback); |
73 }; | 77 }; |
74 | 78 |
75 AudioDeviceThread(); | 79 AudioDeviceThread(); |
76 ~AudioDeviceThread(); | 80 ~AudioDeviceThread(); |
77 | 81 |
| 82 // Represents a data packet to be received via the socket. |
| 83 struct Packet { |
| 84 uint32_t pending_data; |
| 85 AudioTimestamp timestamp; |
| 86 }; |
| 87 |
78 // Starts the audio thread. The thread must not already be running. If | 88 // Starts the audio thread. The thread must not already be running. If |
79 // |sychronized_buffers| is set, the browser expects to be notified via the | 89 // |sychronized_buffers| is set, the browser expects to be notified via the |
80 // |socket| every time AudioDeviceThread::Process() completes. | 90 // |socket| every time AudioDeviceThread::Process() completes. |
81 void Start(AudioDeviceThread::Callback* callback, | 91 void Start(AudioDeviceThread::Callback* callback, |
82 base::SyncSocket::Handle socket, | 92 base::SyncSocket::Handle socket, |
83 const char* thread_name, | 93 const char* thread_name, |
84 bool synchronized_buffers); | 94 bool synchronized_buffers); |
85 | 95 |
86 // This tells the audio thread to stop and clean up the data. | 96 // This tells the audio thread to stop and clean up the data. |
87 // The method can stop the thread synchronously or asynchronously. | 97 // The method can stop the thread synchronously or asynchronously. |
(...skipping 21 matching lines...) Expand all Loading... |
109 | 119 |
110 base::Lock thread_lock_; | 120 base::Lock thread_lock_; |
111 scoped_refptr<AudioDeviceThread::Thread> thread_; | 121 scoped_refptr<AudioDeviceThread::Thread> thread_; |
112 | 122 |
113 DISALLOW_COPY_AND_ASSIGN(AudioDeviceThread); | 123 DISALLOW_COPY_AND_ASSIGN(AudioDeviceThread); |
114 }; | 124 }; |
115 | 125 |
116 } // namespace media. | 126 } // namespace media. |
117 | 127 |
118 #endif // MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_ | 128 #endif // MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_ |
OLD | NEW |