crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 1 | // Copyright (c) 2013 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_MANAGER_MAC_H_ |
| 6 | #define MEDIA_MIDI_MIDI_MANAGER_MAC_H_ |
| 7 | |
| 8 | #include <CoreMIDI/MIDIServices.h> |
avi | 793390d | 2015-12-22 22:22:36 -0800 | [diff] [blame] | 9 | #include <stdint.h> |
toyoshim@chromium.org | ae6ad36 | 2013-08-27 15:30:20 +0000 | [diff] [blame] | 10 | #include <vector> |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 11 | |
toyoshim | c9f5213 | 2014-10-15 05:50:07 -0700 | [diff] [blame] | 12 | #include "base/callback.h" |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
avi | eea95e8 | 2015-12-18 20:27:08 -0800 | [diff] [blame] | 14 | #include "base/macros.h" |
Takashi Toyoshima | 143f0fd | 2017-08-01 16:15:33 +0000 | [diff] [blame] | 15 | #include "base/synchronization/lock.h" |
Takashi Toyoshima | e8240ab | 2018-10-03 09:30:11 +0000 | [diff] [blame] | 16 | #include "base/thread_annotations.h" |
yukawa@chromium.org | db7ad8b | 2013-12-04 15:42:41 +0000 | [diff] [blame] | 17 | #include "base/threading/thread.h" |
brettw | 49ff017 | 2015-05-05 12:43:04 -0700 | [diff] [blame] | 18 | #include "media/midi/midi_export.h" |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 19 | #include "media/midi/midi_manager.h" |
Adithya Srinivasan | 3325273 | 2018-10-17 15:59:40 +0000 | [diff] [blame] | 20 | #include "media/midi/midi_service.mojom.h" |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 21 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 22 | namespace midi { |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 23 | |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 24 | class MidiService; |
| 25 | |
brettw | 49ff017 | 2015-05-05 12:43:04 -0700 | [diff] [blame] | 26 | class MIDI_EXPORT MidiManagerMac final : public MidiManager { |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 27 | public: |
toyoshim | f4d6152 | 2017-02-10 02:03:32 -0800 | [diff] [blame] | 28 | explicit MidiManagerMac(MidiService* service); |
Peter Boström | 5363403 | 2021-09-22 20:24:34 +0000 | [diff] [blame] | 29 | |
| 30 | MidiManagerMac(const MidiManagerMac&) = delete; |
| 31 | MidiManagerMac& operator=(const MidiManagerMac&) = delete; |
| 32 | |
dcheng | 63ccbc3 | 2014-10-21 05:23:27 -0700 | [diff] [blame] | 33 | ~MidiManagerMac() override; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 34 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 35 | // MidiManager implementation. |
dcheng | 63ccbc3 | 2014-10-21 05:23:27 -0700 | [diff] [blame] | 36 | void StartInitialization() override; |
| 37 | void DispatchSendMidiData(MidiManagerClient* client, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 38 | uint32_t port_index, |
| 39 | const std::vector<uint8_t>& data, |
tzik | 925e2c6 | 2018-02-02 07:39:45 +0000 | [diff] [blame] | 40 | base::TimeTicks timestamp) override; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 41 | |
| 42 | private: |
toyoshim | c9f5213 | 2014-10-15 05:50:07 -0700 | [diff] [blame] | 43 | // Initializes CoreMIDI on |client_thread_| asynchronously. Called from |
| 44 | // StartInitialization(). |
| 45 | void InitializeCoreMIDI(); |
| 46 | |
Takashi Toyoshima | 5a6e6a3 | 2018-09-27 11:20:52 +0000 | [diff] [blame] | 47 | // Completes CoreMIDI initialization and asks the thread that ran |
| 48 | // StartInitialization() to call CompleteStartSession() safely. |
| 49 | void CompleteCoreMIDIInitialization(mojom::Result result); |
| 50 | |
toyoshim | 5c6fe4b | 2015-02-18 23:28:09 -0800 | [diff] [blame] | 51 | // CoreMIDI callback for MIDI notification. |
| 52 | // Receives MIDI related event notifications from CoreMIDI. |
| 53 | static void ReceiveMidiNotifyDispatch(const MIDINotification* message, |
| 54 | void* refcon); |
| 55 | void ReceiveMidiNotify(const MIDINotification* message); |
| 56 | |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 57 | // CoreMIDI callback for MIDI data. |
| 58 | // Each callback can contain multiple packets, each of which can contain |
| 59 | // multiple MIDI messages. |
toyoshim | 5c6fe4b | 2015-02-18 23:28:09 -0800 | [diff] [blame] | 60 | static void ReadMidiDispatch(const MIDIPacketList* packet_list, |
| 61 | void* read_proc_refcon, |
| 62 | void* src_conn_refcon); |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 63 | |
toyoshim@chromium.org | c82e66e | 2014-02-04 07:05:47 +0000 | [diff] [blame] | 64 | // An internal callback that runs on MidiSendThread. |
| 65 | void SendMidiData(MidiManagerClient* client, |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 66 | uint32_t port_index, |
| 67 | const std::vector<uint8_t>& data, |
tzik | 925e2c6 | 2018-02-02 07:39:45 +0000 | [diff] [blame] | 68 | base::TimeTicks timestamp); |
yukawa@chromium.org | db7ad8b | 2013-12-04 15:42:41 +0000 | [diff] [blame] | 69 | |
Takashi Toyoshima | e8240ab | 2018-10-03 09:30:11 +0000 | [diff] [blame] | 70 | // CoreMIDI client reference. |
| 71 | MIDIClientRef midi_client_ GUARDED_BY(midi_client_lock_) = 0; |
Takashi Toyoshima | 143f0fd | 2017-08-01 16:15:33 +0000 | [diff] [blame] | 72 | base::Lock midi_client_lock_; |
| 73 | |
| 74 | // Following members can be accessed without any lock on kClientTaskRunner, |
| 75 | // or on I/O thread before calling BindInstance() or after calling |
| 76 | // UnbindInstance(). |
| 77 | |
| 78 | // CoreMIDI other references. |
| 79 | MIDIPortRef midi_input_ = 0; |
| 80 | MIDIPortRef midi_output_ = 0; |
Avi Drissman | 3528fd0 | 2015-12-18 20:11:31 -0500 | [diff] [blame] | 81 | std::vector<uint8_t> midi_buffer_; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 82 | |
Takashi Toyoshima | 143f0fd | 2017-08-01 16:15:33 +0000 | [diff] [blame] | 83 | // Keeps track of all sources. |
| 84 | std::vector<MIDIEndpointRef> sources_; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 85 | |
| 86 | // Keeps track of all destinations. |
Takashi Toyoshima | 143f0fd | 2017-08-01 16:15:33 +0000 | [diff] [blame] | 87 | std::vector<MIDIEndpointRef> destinations_; |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
toyoshim | e147c5e | 2015-05-07 21:58:31 -0700 | [diff] [blame] | 90 | } // namespace midi |
crogers@google.com | 27356e4 | 2013-06-22 04:03:03 +0000 | [diff] [blame] | 91 | |
| 92 | #endif // MEDIA_MIDI_MIDI_MANAGER_MAC_H_ |