OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_MIDI_MIDI_SERVICE_H_ | 5 #ifndef MEDIA_MIDI_MIDI_SERVICE_H_ |
6 #define MEDIA_MIDI_MIDI_SERVICE_H_ | 6 #define MEDIA_MIDI_MIDI_SERVICE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
17 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
18 #include "media/midi/midi_export.h" | 18 #include "media/midi/midi_export.h" |
19 #include "media/midi/midi_manager.h" | 19 #include "media/midi/midi_manager.h" |
20 | 20 |
21 namespace midi { | 21 namespace midi { |
22 | 22 |
23 class MidiManagerAlsa; | |
24 | |
25 // Manages MidiManager backends. This class expects to be constructed and | 23 // Manages MidiManager backends. This class expects to be constructed and |
26 // destructed on the browser main thread, but methods can be called on both | 24 // destructed on the browser main thread, but methods can be called on both |
27 // the main thread and the I/O thread. | 25 // the main thread and the I/O thread. |
28 class MIDI_EXPORT MidiService final { | 26 class MIDI_EXPORT MidiService final { |
29 public: | 27 public: |
30 // Use the first constructor for production code. | 28 // Use the first constructor for production code. |
31 MidiService(); | 29 MidiService(); |
32 // |MidiManager| can be explicitly specified in the constructor for testing. | 30 // |MidiManager| can be explicitly specified in the constructor for testing. |
33 explicit MidiService(std::unique_ptr<MidiManager> manager); | 31 explicit MidiService(std::unique_ptr<MidiManager> manager); |
34 ~MidiService(); | 32 ~MidiService(); |
35 | 33 |
36 // Called on the browser main thread to notify the I/O thread will stop and | 34 // Called on the browser main thread to notify the I/O thread will stop and |
37 // the instance will be destructed on the main thread soon. | 35 // the instance will be destructed on the main thread soon. |
38 void Shutdown(); | 36 void Shutdown(); |
39 | 37 |
40 // A client calls StartSession() to receive and send MIDI data. | 38 // A client calls StartSession() to receive and send MIDI data. |
41 void StartSession(MidiManagerClient* client); | 39 void StartSession(MidiManagerClient* client); |
42 | 40 |
43 // A client calls EndSession() to stop receiving MIDI data. | 41 // A client calls EndSession() to stop receiving MIDI data. |
44 void EndSession(MidiManagerClient* client); | 42 void EndSession(MidiManagerClient* client); |
45 | 43 |
46 // A client calls DispatchSendMidiData() to send MIDI data. | 44 // A client calls DispatchSendMidiData() to send MIDI data. |
47 void DispatchSendMidiData(MidiManagerClient* client, | 45 void DispatchSendMidiData(MidiManagerClient* client, |
48 uint32_t port_index, | 46 uint32_t port_index, |
49 const std::vector<uint8_t>& data, | 47 const std::vector<uint8_t>& data, |
50 double timestamp); | 48 double timestamp); |
51 | 49 |
52 private: | |
53 friend class MidiManagerAlsa; | |
54 | |
55 // Returns a SingleThreadTaskRunner reference to serve MidiManager. Each | 50 // Returns a SingleThreadTaskRunner reference to serve MidiManager. Each |
56 // TaskRunner will be constructed on demand. | 51 // TaskRunner will be constructed on demand. |
57 // MidiManager that supports the dynamic instantiation feature will use this | 52 // MidiManager that supports the dynamic instantiation feature will use this |
58 // method to post tasks that should not run on I/O. Since TaskRunners outlive | 53 // method to post tasks that should not run on I/O. Since TaskRunners outlive |
59 // MidiManager, each task should ensure that MidiManager that posted the task | 54 // MidiManager, each task should ensure that MidiManager that posted the task |
60 // is still alive while accessing |this|. TaskRunners will be reused when | 55 // is still alive while accessing |this|. TaskRunners will be reused when |
61 // another MidiManager is instantiated. | 56 // another MidiManager is instantiated. |
62 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(size_t runner_id); | 57 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(size_t runner_id); |
63 | 58 |
| 59 private: |
64 // Holds MidiManager instance. If the dynamic instantiation feature is | 60 // Holds MidiManager instance. If the dynamic instantiation feature is |
65 // enabled, the MidiManager would be constructed and destructed on the I/O | 61 // enabled, the MidiManager would be constructed and destructed on the I/O |
66 // thread, and all MidiManager methods would be called on the I/O thread. | 62 // thread, and all MidiManager methods would be called on the I/O thread. |
67 std::unique_ptr<MidiManager> manager_; | 63 std::unique_ptr<MidiManager> manager_; |
68 | 64 |
69 // A flag to indicate if the dynamic instantiation feature is supported and | 65 // A flag to indicate if the dynamic instantiation feature is supported and |
70 // actually enabled. | 66 // actually enabled. |
71 const bool is_dynamic_instantiation_enabled_; | 67 const bool is_dynamic_instantiation_enabled_; |
72 | 68 |
73 // Counts active clients to manage dynamic MidiManager instantiation. | 69 // Counts active clients to manage dynamic MidiManager instantiation. |
74 size_t active_clients_; | 70 size_t active_clients_; |
75 | 71 |
76 // Protects all members above. | 72 // Protects all members above. |
77 base::Lock lock_; | 73 base::Lock lock_; |
78 | 74 |
79 // Threads to host SingleThreadTaskRunners. | 75 // Threads to host SingleThreadTaskRunners. |
80 std::vector<std::unique_ptr<base::Thread>> threads_; | 76 std::vector<std::unique_ptr<base::Thread>> threads_; |
81 | 77 |
82 // Protects |threads_|. | 78 // Protects |threads_|. |
83 base::Lock threads_lock_; | 79 base::Lock threads_lock_; |
84 | 80 |
85 DISALLOW_COPY_AND_ASSIGN(MidiService); | 81 DISALLOW_COPY_AND_ASSIGN(MidiService); |
86 }; | 82 }; |
87 | 83 |
88 } // namespace midi | 84 } // namespace midi |
89 | 85 |
90 #endif // MEDIA_MIDI_MIDI_SERVICE_H_ | 86 #endif // MEDIA_MIDI_MIDI_SERVICE_H_ |
OLD | NEW |