toyoshim | 14a3234 | 2016-12-14 22:18:29 -0800 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef MEDIA_MIDI_MIDI_SERVICE_H_ |
| 6 | #define MEDIA_MIDI_MIDI_SERVICE_H_ |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | |
| 10 | #include <memory> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "base/macros.h" |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
toyoshim | 14a3234 | 2016-12-14 22:18:29 -0800 | [diff] [blame] | 15 | #include "base/synchronization/lock.h" |
Patrick Monette | 54c53f4 | 2021-10-08 20:27:23 +0000 | [diff] [blame^] | 16 | #include "base/task/single_thread_task_runner_forward.h" |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 17 | #include "base/threading/thread.h" |
Takashi Toyoshima | afb27d5 | 2017-09-13 11:50:41 +0000 | [diff] [blame] | 18 | #include "base/time/time.h" |
toyoshim | 14a3234 | 2016-12-14 22:18:29 -0800 | [diff] [blame] | 19 | #include "media/midi/midi_export.h" |
| 20 | #include "media/midi/midi_manager.h" |
| 21 | |
| 22 | namespace midi { |
| 23 | |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 24 | class TaskService; |
| 25 | |
toyoshim | 14a3234 | 2016-12-14 22:18:29 -0800 | [diff] [blame] | 26 | // Manages MidiManager backends. This class expects to be constructed and |
| 27 | // destructed on the browser main thread, but methods can be called on both |
| 28 | // the main thread and the I/O thread. |
| 29 | class MIDI_EXPORT MidiService final { |
| 30 | public: |
Takashi Toyoshima | a88997d | 2017-09-07 08:30:18 +0000 | [diff] [blame] | 31 | class MIDI_EXPORT ManagerFactory { |
| 32 | public: |
| 33 | ManagerFactory() = default; |
Peter Boström | 5363403 | 2021-09-22 20:24:34 +0000 | [diff] [blame] | 34 | |
| 35 | ManagerFactory(const ManagerFactory&) = delete; |
| 36 | ManagerFactory& operator=(const ManagerFactory&) = delete; |
| 37 | |
Takashi Toyoshima | a88997d | 2017-09-07 08:30:18 +0000 | [diff] [blame] | 38 | virtual ~ManagerFactory() = default; |
| 39 | virtual std::unique_ptr<MidiManager> Create(MidiService* service); |
Takashi Toyoshima | a88997d | 2017-09-07 08:30:18 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
tzik | 925e2c6 | 2018-02-02 07:39:45 +0000 | [diff] [blame] | 42 | // Converts Web MIDI timestamp to base::TimeDelta delay for PostDelayedTask. |
| 43 | static base::TimeDelta TimestampToTimeDeltaDelay(base::TimeTicks timestamp); |
Takashi Toyoshima | afb27d5 | 2017-09-13 11:50:41 +0000 | [diff] [blame] | 44 | |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 45 | MidiService(); |
Takashi Toyoshima | bc959ee | 2018-01-09 16:04:04 +0900 | [diff] [blame] | 46 | // Customized ManagerFactory can be specified in the constructor for testing. |
Takashi Toyoshima | a88997d | 2017-09-07 08:30:18 +0000 | [diff] [blame] | 47 | explicit MidiService(std::unique_ptr<ManagerFactory> factory); |
Peter Boström | 5363403 | 2021-09-22 20:24:34 +0000 | [diff] [blame] | 48 | |
| 49 | MidiService(const MidiService&) = delete; |
| 50 | MidiService& operator=(const MidiService&) = delete; |
| 51 | |
toyoshim | 14a3234 | 2016-12-14 22:18:29 -0800 | [diff] [blame] | 52 | ~MidiService(); |
| 53 | |
| 54 | // Called on the browser main thread to notify the I/O thread will stop and |
| 55 | // the instance will be destructed on the main thread soon. |
| 56 | void Shutdown(); |
| 57 | |
| 58 | // A client calls StartSession() to receive and send MIDI data. |
| 59 | void StartSession(MidiManagerClient* client); |
| 60 | |
| 61 | // A client calls EndSession() to stop receiving MIDI data. |
Takashi Toyoshima | bc959ee | 2018-01-09 16:04:04 +0900 | [diff] [blame] | 62 | // Returns false if |client| did not start a session. |
| 63 | bool EndSession(MidiManagerClient* client); |
toyoshim | 14a3234 | 2016-12-14 22:18:29 -0800 | [diff] [blame] | 64 | |
| 65 | // A client calls DispatchSendMidiData() to send MIDI data. |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 66 | void DispatchSendMidiData(MidiManagerClient* client, |
| 67 | uint32_t port_index, |
| 68 | const std::vector<uint8_t>& data, |
tzik | 925e2c6 | 2018-02-02 07:39:45 +0000 | [diff] [blame] | 69 | base::TimeTicks timestamp); |
toyoshim | 14a3234 | 2016-12-14 22:18:29 -0800 | [diff] [blame] | 70 | |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 71 | // Returns a SingleThreadTaskRunner reference to serve MidiManager. Each |
| 72 | // TaskRunner will be constructed on demand. |
| 73 | // MidiManager that supports the dynamic instantiation feature will use this |
| 74 | // method to post tasks that should not run on I/O. Since TaskRunners outlive |
| 75 | // MidiManager, each task should ensure that MidiManager that posted the task |
| 76 | // is still alive while accessing |this|. TaskRunners will be reused when |
| 77 | // another MidiManager is instantiated. |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 78 | // TODO(toyoshim): Remove this interface. |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 79 | scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(size_t runner_id); |
| 80 | |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 81 | // Obtains a TaskService that lives with MidiService. |
| 82 | TaskService* task_service() { return task_service_.get(); } |
| 83 | |
toyoshim | d28b59c | 2017-02-20 11:07:37 -0800 | [diff] [blame] | 84 | private: |
Takashi Toyoshima | bc959ee | 2018-01-09 16:04:04 +0900 | [diff] [blame] | 85 | // ManagerFactory passed in the constructor. |
Takashi Toyoshima | a88997d | 2017-09-07 08:30:18 +0000 | [diff] [blame] | 86 | std::unique_ptr<ManagerFactory> manager_factory_; |
| 87 | |
Takashi Toyoshima | bc959ee | 2018-01-09 16:04:04 +0900 | [diff] [blame] | 88 | // Holds MidiManager instance. The MidiManager is constructed and destructed |
| 89 | // on the I/O thread, and all MidiManager methods should be called on the I/O |
| 90 | // thread. |
toyoshim | 14a3234 | 2016-12-14 22:18:29 -0800 | [diff] [blame] | 91 | std::unique_ptr<MidiManager> manager_; |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 92 | |
toyoshim | ccb8ff9 | 2017-06-13 05:31:46 -0700 | [diff] [blame] | 93 | // Holds TaskService instance. |
| 94 | std::unique_ptr<TaskService> task_service_; |
| 95 | |
Takashi Toyoshima | f563b61 | 2017-05-26 20:19:00 +0900 | [diff] [blame] | 96 | // TaskRunner to destruct |manager_| on the right thread. |
| 97 | scoped_refptr<base::SingleThreadTaskRunner> manager_destructor_runner_; |
| 98 | |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 99 | // Protects all members above. |
toyoshim | 14a3234 | 2016-12-14 22:18:29 -0800 | [diff] [blame] | 100 | base::Lock lock_; |
| 101 | |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 102 | // Threads to host SingleThreadTaskRunners. |
| 103 | std::vector<std::unique_ptr<base::Thread>> threads_; |
| 104 | |
| 105 | // Protects |threads_|. |
| 106 | base::Lock threads_lock_; |
toyoshim | 14a3234 | 2016-12-14 22:18:29 -0800 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | } // namespace midi |
| 110 | |
| 111 | #endif // MEDIA_MIDI_MIDI_SERVICE_H_ |