blob: c17d80e1fa70ba05b83f1e0a693e1023382dac5f [file] [log] [blame]
crogers@google.com27356e42013-06-22 04:03:03 +00001// 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>
avi793390d2015-12-22 22:22:36 -08009#include <stdint.h>
toyoshim@chromium.orgae6ad362013-08-27 15:30:20 +000010#include <vector>
crogers@google.com27356e42013-06-22 04:03:03 +000011
toyoshimc9f52132014-10-15 05:50:07 -070012#include "base/callback.h"
crogers@google.com27356e42013-06-22 04:03:03 +000013#include "base/compiler_specific.h"
avieea95e82015-12-18 20:27:08 -080014#include "base/macros.h"
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +000015#include "base/synchronization/lock.h"
Takashi Toyoshimae8240ab2018-10-03 09:30:11 +000016#include "base/thread_annotations.h"
yukawa@chromium.orgdb7ad8b2013-12-04 15:42:41 +000017#include "base/threading/thread.h"
brettw49ff0172015-05-05 12:43:04 -070018#include "media/midi/midi_export.h"
crogers@google.com27356e42013-06-22 04:03:03 +000019#include "media/midi/midi_manager.h"
Adithya Srinivasan33252732018-10-17 15:59:40 +000020#include "media/midi/midi_service.mojom.h"
crogers@google.com27356e42013-06-22 04:03:03 +000021
toyoshime147c5e2015-05-07 21:58:31 -070022namespace midi {
crogers@google.com27356e42013-06-22 04:03:03 +000023
toyoshimf4d61522017-02-10 02:03:32 -080024class MidiService;
25
brettw49ff0172015-05-05 12:43:04 -070026class MIDI_EXPORT MidiManagerMac final : public MidiManager {
crogers@google.com27356e42013-06-22 04:03:03 +000027 public:
toyoshimf4d61522017-02-10 02:03:32 -080028 explicit MidiManagerMac(MidiService* service);
Peter Boström53634032021-09-22 20:24:34 +000029
30 MidiManagerMac(const MidiManagerMac&) = delete;
31 MidiManagerMac& operator=(const MidiManagerMac&) = delete;
32
dcheng63ccbc32014-10-21 05:23:27 -070033 ~MidiManagerMac() override;
crogers@google.com27356e42013-06-22 04:03:03 +000034
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000035 // MidiManager implementation.
dcheng63ccbc32014-10-21 05:23:27 -070036 void StartInitialization() override;
37 void DispatchSendMidiData(MidiManagerClient* client,
Avi Drissman3528fd02015-12-18 20:11:31 -050038 uint32_t port_index,
39 const std::vector<uint8_t>& data,
tzik925e2c62018-02-02 07:39:45 +000040 base::TimeTicks timestamp) override;
crogers@google.com27356e42013-06-22 04:03:03 +000041
42 private:
toyoshimc9f52132014-10-15 05:50:07 -070043 // Initializes CoreMIDI on |client_thread_| asynchronously. Called from
44 // StartInitialization().
45 void InitializeCoreMIDI();
46
Takashi Toyoshima5a6e6a32018-09-27 11:20:52 +000047 // Completes CoreMIDI initialization and asks the thread that ran
48 // StartInitialization() to call CompleteStartSession() safely.
49 void CompleteCoreMIDIInitialization(mojom::Result result);
50
toyoshim5c6fe4b2015-02-18 23:28:09 -080051 // 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.com27356e42013-06-22 04:03:03 +000057 // CoreMIDI callback for MIDI data.
58 // Each callback can contain multiple packets, each of which can contain
59 // multiple MIDI messages.
toyoshim5c6fe4b2015-02-18 23:28:09 -080060 static void ReadMidiDispatch(const MIDIPacketList* packet_list,
61 void* read_proc_refcon,
62 void* src_conn_refcon);
crogers@google.com27356e42013-06-22 04:03:03 +000063
toyoshim@chromium.orgc82e66e2014-02-04 07:05:47 +000064 // An internal callback that runs on MidiSendThread.
65 void SendMidiData(MidiManagerClient* client,
Avi Drissman3528fd02015-12-18 20:11:31 -050066 uint32_t port_index,
67 const std::vector<uint8_t>& data,
tzik925e2c62018-02-02 07:39:45 +000068 base::TimeTicks timestamp);
yukawa@chromium.orgdb7ad8b2013-12-04 15:42:41 +000069
Takashi Toyoshimae8240ab2018-10-03 09:30:11 +000070 // CoreMIDI client reference.
71 MIDIClientRef midi_client_ GUARDED_BY(midi_client_lock_) = 0;
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +000072 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 Drissman3528fd02015-12-18 20:11:31 -050081 std::vector<uint8_t> midi_buffer_;
crogers@google.com27356e42013-06-22 04:03:03 +000082
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +000083 // Keeps track of all sources.
84 std::vector<MIDIEndpointRef> sources_;
crogers@google.com27356e42013-06-22 04:03:03 +000085
86 // Keeps track of all destinations.
Takashi Toyoshima143f0fd2017-08-01 16:15:33 +000087 std::vector<MIDIEndpointRef> destinations_;
crogers@google.com27356e42013-06-22 04:03:03 +000088};
89
toyoshime147c5e2015-05-07 21:58:31 -070090} // namespace midi
crogers@google.com27356e42013-06-22 04:03:03 +000091
92#endif // MEDIA_MIDI_MIDI_MANAGER_MAC_H_