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