Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: media/audio/audio_device_thread.h

Issue 2076793002: Cleanup AudioDeviceThread to reduce locking and unused features. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Relocate one-time use variables. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | media/audio/audio_device_thread.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
12 #include "base/memory/shared_memory.h" 11 #include "base/memory/shared_memory.h"
13 #include "base/sync_socket.h" 12 #include "base/sync_socket.h"
14 #include "base/synchronization/lock.h" 13 #include "base/threading/platform_thread.h"
15 #include "base/threading/thread_checker.h" 14 #include "base/threading/thread_checker.h"
16 #include "media/base/audio_parameters.h" 15 #include "media/base/audio_parameters.h"
17 #include "media/base/media_export.h" 16 #include "media/base/media_export.h"
18 17
19 namespace base {
20 class MessageLoop;
21 }
22
23 namespace media { 18 namespace media {
24 class AudioBus;
25 19
26 // Data transfer between browser and render process uses a combination 20 // Data transfer between browser and render process uses a combination
27 // of sync sockets and shared memory. To read from the socket and render 21 // of sync sockets and shared memory. To read from the socket and render
28 // data, we use a worker thread, a.k.a. the AudioDeviceThread, which reads 22 // data, we use a worker thread, a.k.a. the AudioDeviceThread, which reads
29 // data from the browser via the socket and fills the shared memory from the 23 // data from the browser via the socket and fills the shared memory from the
30 // audio thread via the AudioDeviceThread::Callback interface/class. 24 // audio thread via the AudioDeviceThread::Callback interface/class.
31 // For more details see the documentation in audio_device.h. 25 class MEDIA_EXPORT AudioDeviceThread : public base::PlatformThread::Delegate {
32 //
33 // TODO(tommi): Multiple audio input/output device instances should be able to
34 // share the same thread instead of spinning one per instance.
35 class MEDIA_EXPORT AudioDeviceThread {
36 public: 26 public:
37 // This is the callback interface/base class that Audio[Output|Input]Device 27 // This is the callback interface/base class that Audio[Output|Input]Device
38 // implements to render input/output data. The callbacks run on the 28 // implements to render input/output data. The callbacks run on the
39 // thread owned by AudioDeviceThread. 29 // thread owned by AudioDeviceThread.
40 class Callback { 30 class Callback {
41 public: 31 public:
42 Callback(const AudioParameters& audio_parameters, 32 Callback(const AudioParameters& audio_parameters,
43 base::SharedMemoryHandle memory, 33 base::SharedMemoryHandle memory,
44 int memory_length, 34 int memory_length,
45 int total_segments); 35 int total_segments);
46 virtual ~Callback();
47 36
48 // One time initialization for the callback object on the audio thread. 37 // One time initialization for the callback object on the audio thread.
49 void InitializeOnAudioThread(); 38 void InitializeOnAudioThread();
50 39
51 // Derived implementations must call shared_memory_.Map appropriately 40 // Derived implementations must call shared_memory_.Map appropriately
52 // before Process can be called. 41 // before Process can be called.
53 virtual void MapSharedMemory() = 0; 42 virtual void MapSharedMemory() = 0;
54 43
55 // Called whenever we receive notifications about pending input data. 44 // Called whenever we receive notifications about pending input data.
56 virtual void Process(uint32_t pending_data) = 0; 45 virtual void Process(uint32_t pending_data) = 0;
57 46
58 protected: 47 protected:
48 virtual ~Callback();
49
59 // Protected so that derived classes can access directly. 50 // Protected so that derived classes can access directly.
60 // The variables are 'const' since values are calculated/set in the 51 // The variables are 'const' since values are calculated/set in the
61 // constructor and must never change. 52 // constructor and must never change.
62 const AudioParameters audio_parameters_; 53 const AudioParameters audio_parameters_;
63 const double samples_per_ms_;
64 const double bytes_per_ms_;
65 const uint32_t bytes_per_frame_;
66 54
67 base::SharedMemory shared_memory_; 55 base::SharedMemory shared_memory_;
68 const int memory_length_; 56 const int memory_length_;
69 const int total_segments_; 57 const int total_segments_;
70 int segment_length_; 58 const int segment_length_;
71 59
72 // Detached in constructor and attached in InitializeOnAudioThread() which 60 // Detached in constructor and attached in InitializeOnAudioThread() which
73 // is called on the audio device thread. Sub-classes can then use it for 61 // is called on the audio device thread. Sub-classes can then use it for
74 // various thread checking purposes. 62 // various thread checking purposes.
75 base::ThreadChecker thread_checker_; 63 base::ThreadChecker thread_checker_;
76 64
77 private: 65 private:
78 DISALLOW_COPY_AND_ASSIGN(Callback); 66 DISALLOW_COPY_AND_ASSIGN(Callback);
79 }; 67 };
80 68
81 AudioDeviceThread(); 69 // Creates and automatically starts the audio thread.
82 ~AudioDeviceThread(); 70 AudioDeviceThread(Callback* callback,
71 base::SyncSocket::Handle socket,
72 const char* thread_name);
83 73
84 // Starts the audio thread. The thread must not already be running. If 74 // This tells the audio thread to stop and clean up the data; this is a
85 // |sychronized_buffers| is set, the browser expects to be notified via the 75 // synchronous process and the thread will stop before the method returns.
86 // |socket| every time AudioDeviceThread::Process() completes. 76 ~AudioDeviceThread() override;
87 void Start(AudioDeviceThread::Callback* callback,
88 base::SyncSocket::Handle socket,
89 const char* thread_name,
90 bool synchronized_buffers);
91
92 // This tells the audio thread to stop and clean up the data.
93 // The method can stop the thread synchronously or asynchronously.
94 // In the latter case, the thread will still be running after Stop()
95 // returns, but the callback pointer is cleared so no further callbacks will
96 // be made (IOW after Stop() returns, it is safe to delete the callback).
97 // The |loop_for_join| parameter is required for asynchronous operation
98 // in order to join the worker thread and close the thread handle later via a
99 // posted task.
100 // If set to NULL, function will wait for the thread to exit before returning.
101 void Stop(base::MessageLoop* loop_for_join);
102
103 // Returns true if the thread is stopped or stopping.
104 bool IsStopped();
105 77
106 private: 78 private:
107 // Our own private SimpleThread override. We implement this in a 79 void ThreadMain() final;
108 // private class so that we get the following benefits:
109 // 1) AudioDeviceThread doesn't expose SimpleThread methods.
110 // I.e. the caller can't call Start()/Stop() - which would be bad.
111 // 2) We override ThreadMain to add additional on-thread initialization
112 // while still synchronized with SimpleThread::Start() to provide
113 // reliable initialization.
114 class Thread;
115 80
116 base::Lock thread_lock_; 81 Callback* const callback_;
117 scoped_refptr<AudioDeviceThread::Thread> thread_; 82 const char* thread_name_;
83 base::CancelableSyncSocket socket_;
84 base::PlatformThreadHandle thread_handle_;
118 85
119 DISALLOW_COPY_AND_ASSIGN(AudioDeviceThread); 86 DISALLOW_COPY_AND_ASSIGN(AudioDeviceThread);
120 }; 87 };
121 88
122 } // namespace media. 89 } // namespace media.
123 90
124 #endif // MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_ 91 #endif // MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_
OLDNEW
« no previous file with comments | « no previous file | media/audio/audio_device_thread.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698