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: |
25 // |MidiManager| can be explicitly specified in the constructor for testing. | 30 // |MidiManager| can be explicitly specified in the constructor for testing. |
26 explicit MidiService(std::unique_ptr<MidiManager> manager = nullptr); | 31 explicit MidiService(std::unique_ptr<MidiManager> manager = nullptr); |
27 ~MidiService(); | 32 ~MidiService(); |
28 | 33 |
29 // 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 |
30 // the instance will be destructed on the main thread soon. | 35 // the instance will be destructed on the main thread soon. |
31 void Shutdown(); | 36 void Shutdown(); |
32 | 37 |
33 // A client calls StartSession() to receive and send MIDI data. | 38 // A client calls StartSession() to receive and send MIDI data. |
34 void StartSession(MidiManagerClient* client); | 39 void StartSession(MidiManagerClient* client); |
35 | 40 |
36 // A client calls EndSession() to stop receiving MIDI data. | 41 // A client calls EndSession() to stop receiving MIDI data. |
37 void EndSession(MidiManagerClient* client); | 42 void EndSession(MidiManagerClient* client); |
38 | 43 |
39 // A client calls DispatchSendMidiData() to send MIDI data. | 44 // A client calls DispatchSendMidiData() to send MIDI data. |
40 virtual void DispatchSendMidiData(MidiManagerClient* client, | 45 void DispatchSendMidiData(MidiManagerClient* client, |
41 uint32_t port_index, | 46 uint32_t port_index, |
42 const std::vector<uint8_t>& data, | 47 const std::vector<uint8_t>& data, |
43 double timestamp); | 48 double timestamp); |
44 | 49 |
| 50 protected: |
| 51 friend class MidiManagerAlsa; |
| 52 |
| 53 // Returns a SingleThreadTaskRunner reference to serve MidiManager. Each |
| 54 // TaskRunner will be constructed on demand. |
| 55 // MidiManager that supports the dynamic instantiation feature will use this |
| 56 // method to post tasks that should not run on I/O. Since TaskRunners outlive |
| 57 // MidiManager, each task should be bound with WeakPtr<> if the task is not a |
| 58 // static method. TaskRunners will be reused when another MidiManager is |
| 59 // instantiated. |
| 60 static scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner( |
| 61 size_t runner_id); |
| 62 |
| 63 private: |
| 64 scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunnerImpl( |
| 65 size_t runner_id); |
| 66 |
| 67 // Holds MidiManager instance. If the dynamic instantiation feature is |
| 68 // enabled, the MidiManager would be constructed and destructed on the I/O |
| 69 // thread, and all MidiManager methods would be called on the I/O thread. |
45 std::unique_ptr<MidiManager> manager_; | 70 std::unique_ptr<MidiManager> manager_; |
| 71 |
| 72 // A flag to indicate if the dynamic instantiation feature is supported and |
| 73 // actually enabled. |
| 74 bool is_dynamic_instantiation_enabled_; |
| 75 |
| 76 // Counts active clients to manage dynamic MidiManager instantiation. |
| 77 size_t active_clients_; |
| 78 |
| 79 // Protects all members above. |
46 base::Lock lock_; | 80 base::Lock lock_; |
47 | 81 |
| 82 // Threads to host SingleThreadTaskRunners. |
| 83 std::vector<std::unique_ptr<base::Thread>> threads_; |
| 84 |
| 85 // Protects |threads_|. |
| 86 base::Lock threads_lock_; |
| 87 |
48 DISALLOW_COPY_AND_ASSIGN(MidiService); | 88 DISALLOW_COPY_AND_ASSIGN(MidiService); |
49 }; | 89 }; |
50 | 90 |
51 } // namespace midi | 91 } // namespace midi |
52 | 92 |
53 #endif // MEDIA_MIDI_MIDI_SERVICE_H_ | 93 #endif // MEDIA_MIDI_MIDI_SERVICE_H_ |
OLD | NEW |