Ardour  9.0-pre0-582-g084a23a80d
coreaudio_backend.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015-2018 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2016-2018 Paul Davis <paul@linuxaudiosystems.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef __libbackend_coreaudio_backend_h__
21 #define __libbackend_coreaudio_backend_h__
22 
23 #include <cstdint>
24 #include <map>
25 #include <memory>
26 #include <set>
27 #include <string>
28 #include <vector>
29 
30 #include <pthread.h>
31 
32 #include "pbd/natsort.h"
33 #include "ardour/audio_backend.h"
36 #include "ardour/types.h"
37 
38 #include "coreaudio_pcmio.h"
39 #include "coremidi_io.h"
40 
41 #define MaxCoreMidiEventSize 256 // matches CoreMidi's MIDIPacket (https://developer.apple.com/documentation/coremidi/midipacket)
42 
43 namespace ARDOUR {
44 
45 class CoreAudioBackend;
46 
48  public:
49  CoreMidiEvent (const pframes_t timestamp, const uint8_t* data, size_t size);
50  CoreMidiEvent (const CoreMidiEvent& other);
51  size_t size () const { return _size; };
52  pframes_t timestamp () const { return _timestamp; };
53  const uint8_t* data () const { return _data; };
54  private:
55  size_t _size;
58 };
59 
60 typedef std::vector<CoreMidiEvent> CoreMidiBuffer;
61 
62 class CoreAudioPort : public BackendPort {
63  public:
64  CoreAudioPort (CoreAudioBackend &b, const std::string&, PortFlags);
66 
67  DataType type () const { return DataType::AUDIO; };
68 
69  Sample* buffer () { return _buffer; }
70  const Sample* const_buffer () const { return _buffer; }
71  void* get_buffer (pframes_t nframes);
72 
73  private:
74  Sample _buffer[8192];
75 }; // class CoreAudioPort
76 
77 class CoreMidiPort : public BackendPort {
78  public:
79  CoreMidiPort (CoreAudioBackend &b, const std::string&, PortFlags);
81 
82  DataType type () const { return DataType::MIDI; };
83 
84  void* get_buffer (pframes_t nframes);
85  const CoreMidiBuffer * const_buffer () const { return & _buffer[_bufperiod]; }
86 
87  void next_period() { if (_n_periods > 1) { get_buffer(0); _bufperiod = (_bufperiod + 1) % _n_periods; } }
88  void set_n_periods(int n) { if (n > 0 && n < 3) { _n_periods = n; } }
89 
90  void parse_events (const uint64_t time, const uint8_t *data, const size_t size);
91  void clear_events ();
92  void reset_parser ();
93 
94  private:
98 
99  int queue_event (void* port_buffer, pframes_t timestamp, const uint8_t* buffer, size_t size);
100  bool process_byte (const uint64_t, const uint8_t);
101 
102  void record_byte(uint8_t byte) {
103  if (_total_bytes < sizeof(_parser_buffer)) {
105  } else {
107  }
108  ++_total_bytes;
109  }
110 
111  void prepare_byte_event(const uint64_t time, const uint8_t byte) {
112  _parser_buffer[0] = byte;
113  _event.prepare(time, 1);
114  }
115 
116  bool prepare_buffered_event(const uint64_t time) {
117  const bool result = _unbuffered_bytes == 0;
118  if (result) {
119  _event.prepare(time, _total_bytes);
120  }
121  _total_bytes = 0;
122  _unbuffered_bytes = 0;
123  if (_status_byte >= 0xf0) {
124  _expected_bytes = 0;
125  _status_byte = 0;
126  }
127  return result;
128  }
129 
130  struct ParserEvent {
131  uint64_t _time;
132  size_t _size;
133  bool _pending;
134  ParserEvent (const uint64_t time, const size_t size)
135  : _time(time)
136  , _size(size)
137  , _pending(false) {}
138 
139  void prepare(const uint64_t time, const size_t size) {
140  _time = time;
141  _size = size;
142  _pending = true;
143  }
145 
148  size_t _total_bytes;
150  uint8_t _status_byte;
151  uint8_t _parser_buffer[1024];
152 
153 }; // class CoreMidiPort
154 
156  public:
159 
160  /* AUDIOBACKEND API */
161 
162  std::string name () const;
163  bool is_realtime () const;
164 
165  bool use_separate_input_and_output_devices () const { return true; }
166  std::vector<DeviceStatus> enumerate_devices () const;
167  std::vector<DeviceStatus> enumerate_input_devices () const;
168  std::vector<DeviceStatus> enumerate_output_devices () const;
169 
170  std::vector<float> available_sample_rates (const std::string& device) const;
171  std::vector<float> available_sample_rates2 (const std::string&, const std::string&) const;
172  std::vector<uint32_t> available_buffer_sizes (const std::string& device) const;
173  std::vector<uint32_t> available_buffer_sizes2 (const std::string&, const std::string&) const;
174 
177  bool can_measure_systemic_latency () const { return true; }
178 
179  int set_device_name (const std::string&);
180  int set_input_device_name (const std::string&);
181  int set_output_device_name (const std::string&);
182  int set_sample_rate (float);
183  int set_buffer_size (uint32_t);
184  int set_interleaved (bool yn);
187  int set_systemic_midi_input_latency (std::string const, uint32_t) { return 0; }
188  int set_systemic_midi_output_latency (std::string const, uint32_t) { return 0; }
189 
190  int reset_device () { return 0; };
191 
192  /* Retrieving parameters */
193  std::string device_name () const;
194  std::string input_device_name () const;
195  std::string output_device_name () const;
196  float sample_rate () const;
197  uint32_t buffer_size () const;
198  bool interleaved () const;
199  uint32_t systemic_input_latency () const;
200  uint32_t systemic_output_latency () const;
201  uint32_t systemic_midi_input_latency (std::string const) const { return 0; }
202  uint32_t systemic_midi_output_latency (std::string const) const { return 0; }
203 
204  bool can_set_systemic_midi_latencies () const { return false; /* XXX */}
205 
206  /* External control app */
207  std::string control_app_name () const { return std::string ("Apple"); }
209 
210  /* MIDI */
211  std::vector<std::string> enumerate_midi_options () const;
212  int set_midi_option (const std::string&);
213  std::string midi_option () const;
214 
215  std::vector<DeviceStatus> enumerate_midi_devices () const {
216  return std::vector<AudioBackend::DeviceStatus> ();
217  }
218  int set_midi_device_enabled (std::string const, bool) {
219  return true;
220  }
221  bool midi_device_enabled (std::string const) const {
222  return false;
223  }
224 
225  // really private, but needing static access:
226  int process_callback(uint32_t, uint64_t);
233 
234  protected:
235  /* State Control */
236  int _start (bool for_latency_measurement);
237  public:
238  int stop ();
239  int freewheel (bool);
240  float dsp_load () const;
242 
243  /* Process time */
247 
248  int create_process_thread (std::function<void()> func);
251  uint32_t process_thread_count ();
252 
254 
255  /* PORTENGINE API */
256 
257  void* private_handle () const;
258  const std::string& my_name () const;
259 
260  /* PortEngine API - forwarded to PortEngineSharedImpl */
261 
263  void get_physical_outputs (DataType type, std::vector<std::string>& results) { PortEngineSharedImpl::get_physical_outputs (type, results); }
264  void get_physical_inputs (DataType type, std::vector<std::string>& results) { PortEngineSharedImpl::get_physical_inputs (type, results); }
267  uint32_t port_name_size () const { return PortEngineSharedImpl::port_name_size(); }
272  int get_port_property (PortEngine::PortHandle ph, const std::string& key, std::string& value, std::string& type) const { return PortEngineSharedImpl::get_port_property (ph, key, value, type); }
273  int set_port_property (PortEngine::PortHandle ph, const std::string& key, const std::string& value, const std::string& type) { return PortEngineSharedImpl::set_port_property (ph, key, value, type); }
274  int get_ports (const std::string& port_name_pattern, DataType type, PortFlags flags, std::vector<std::string>& results) const { return PortEngineSharedImpl::get_ports (port_name_pattern, type, flags, results); }
276  PortEngine::PortPtr register_port (const std::string& shortname, ARDOUR::DataType type, ARDOUR::PortFlags flags) { return PortEngineSharedImpl::register_port (shortname, type, flags); }
278  int connect (const std::string& src, const std::string& dst) { return PortEngineSharedImpl::connect (src, dst); }
279  int disconnect (const std::string& src, const std::string& dst) { return PortEngineSharedImpl::disconnect (src, dst); }
280  int connect (PortEngine::PortHandle ph, const std::string& other) { return PortEngineSharedImpl::connect (ph, other); }
281  int disconnect (PortEngine::PortHandle ph, const std::string& other) { return PortEngineSharedImpl::disconnect (ph, other); }
283  bool connected (PortEngine::PortHandle ph, bool process_callback_safe) { return PortEngineSharedImpl::connected (ph, process_callback_safe); }
284  bool connected_to (PortEngine::PortHandle ph, const std::string& other, bool process_callback_safe) { return PortEngineSharedImpl::connected_to (ph, other, process_callback_safe); }
285  bool physically_connected (PortEngine::PortHandle ph, bool process_callback_safe) { return PortEngineSharedImpl::physically_connected (ph, process_callback_safe); }
286  int get_connections (PortEngine::PortHandle ph, std::vector<std::string>& results, bool process_callback_safe) { return PortEngineSharedImpl::get_connections (ph, results, process_callback_safe); }
287 
288  /* MIDI */
289  int midi_event_get (pframes_t& timestamp, size_t& size, uint8_t const** buf, void* port_buffer, uint32_t event_index);
290  int midi_event_put (void* port_buffer, pframes_t timestamp, const uint8_t* buffer, size_t size) {
291  return _midi_event_put (port_buffer, timestamp, buffer, size);
292  }
293 
294  uint32_t get_midi_event_count (void* port_buffer);
295  void midi_clear (void* port_buffer);
296 
297  /* Monitoring */
298 
299  bool can_monitor_input () const;
303 
304  /* Latency management */
305 
306  void set_latency_range (PortHandle, bool for_playback, LatencyRange);
308 
309  /* Getting access to the data buffer for a port */
310 
312 
314  void pre_process ();
316 
317  static int _midi_event_put (void* port_buffer, pframes_t timestamp, const uint8_t* buffer, size_t size);
318 
319  private:
320  std::string _instance_name;
323 
324  bool _run; /* keep going or stop, ardour thread */
325  bool _active_ca; /* is running, process thread */
326  bool _active_fw; /* is running, process thread */
327  bool _preinit;
333 
335 
336  pthread_mutex_t _process_callback_mutex;
337 
338  pthread_mutex_t _freewheel_mutex;
339  pthread_cond_t _freewheel_signal;
340 
341  static std::vector<std::string> _midi_options;
342  static std::vector<AudioBackend::DeviceStatus> _input_audio_device_status;
343  static std::vector<AudioBackend::DeviceStatus> _output_audio_device_status;
344  static std::vector<AudioBackend::DeviceStatus> _duplex_audio_device_status;
345  static std::vector<AudioBackend::DeviceStatus> _midi_device_status;
346 
347  mutable std::string _input_audio_device;
348  mutable std::string _output_audio_device;
349  std::string _midi_driver_option;
350 
351  /* audio settings */
352  float _samplerate;
354  static size_t _max_buffer_size;
355 
358 
361 
362  /* coreaudio specific */
364  uint32_t name_to_id(std::string, DeviceFilter filter = All) const;
365 
367 
368  /* processing */
369  float _dsp_load;
372 
373  pthread_t _main_thread;
375 
376  /* process threads */
377  static void* coreaudio_process_thread (void *);
378  std::vector<pthread_t> _threads;
379 
380  struct ThreadData {
382  std::function<void ()> f;
383  size_t stacksize;
384  double period_ns;
385 
386 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 110000
387  bool _joined_workgroup;
388  os_workgroup_t _workgroup;
389  os_workgroup_join_token_s _join_token;
390 #endif
391 
392  ThreadData (CoreAudioBackend* e, std::function<void ()> fp, size_t stacksz, double period)
393  : engine (e) , f (fp) , stacksize (stacksz), period_ns (period) {}
394  };
395 
396  /* port engine */
398 
399  BackendPortPtr find_port_in (std::vector<BackendPortPtr> const & plist, const std::string& port_name) const {
400  for (std::vector<BackendPortPtr>::const_iterator it = plist.begin (); it != plist.end (); ++it) {
401  if ((*it)->name () == port_name) {
402  return *it;
403  }
404  }
405  return BackendPortPtr();
406  }
407 
409 
411 
412 }; // class CoreAudioBackend
413 
414 } // namespace
415 
416 #endif /* __libbackend_coreaudio_backend_h__ */
AudioBackendInfo & info() const
BackendPortPtr find_port_in(std::vector< BackendPortPtr > const &plist, const std::string &port_name) const
int set_midi_device_enabled(std::string const, bool)
bool can_change_buffer_size_when_running() const
int set_buffer_size(uint32_t)
DataType port_data_type(PortEngine::PortHandle ph) const
int create_process_thread(std::function< void()> func)
int connect(PortEngine::PortHandle ph, const std::string &other)
PortEngine::PortPtr register_port(const std::string &shortname, ARDOUR::DataType type, ARDOUR::PortFlags flags)
bool port_is_physical(PortEngine::PortHandle ph) const
std::vector< pthread_t > _threads
void get_physical_outputs(DataType type, std::vector< std::string > &results)
int set_output_device_name(const std::string &)
int process_callback(uint32_t, uint64_t)
bool use_separate_input_and_output_devices() const
int disconnect(PortEngine::PortHandle ph, const std::string &other)
std::string control_app_name() const
samplepos_t sample_time()
ChanCount n_physical_inputs() const
int ensure_input_monitoring(PortHandle, bool)
static int _midi_event_put(void *port_buffer, pframes_t timestamp, const uint8_t *buffer, size_t size)
BackendPort * port_factory(std::string const &name, ARDOUR::DataType type, ARDOUR::PortFlags)
ARDOUR::DSPLoadCalculator _dsp_load_calc
std::vector< DeviceStatus > enumerate_devices() const
int connect(const std::string &src, const std::string &dst)
int set_port_name(PortEngine::PortHandle ph, const std::string &name)
uint32_t systemic_output_latency() const
std::string output_device_name() const
int set_midi_option(const std::string &)
std::vector< DeviceStatus > enumerate_input_devices() const
void midi_clear(void *port_buffer)
bool monitoring_input(PortHandle)
int midi_event_get(pframes_t &timestamp, size_t &size, uint8_t const **buf, void *port_buffer, uint32_t event_index)
int set_device_name(const std::string &)
uint32_t systemic_midi_input_latency(std::string const) const
bool physically_connected(PortEngine::PortHandle ph, bool process_callback_safe)
static void * coreaudio_process_thread(void *)
std::string input_device_name() const
void unregister_port(PortHandle ph)
std::string midi_option() const
int midi_event_put(void *port_buffer, pframes_t timestamp, const uint8_t *buffer, size_t size)
int set_systemic_output_latency(uint32_t)
bool connected(PortEngine::PortHandle ph, bool process_callback_safe)
void set_latency_range(PortHandle, bool for_playback, LatencyRange)
size_t raw_buffer_size(DataType t)
int set_port_property(PortEngine::PortHandle ph, const std::string &key, const std::string &value, const std::string &type)
std::vector< float > available_sample_rates2(const std::string &, const std::string &) const
std::string get_port_name(PortEngine::PortHandle ph) const
int get_ports(const std::string &port_name_pattern, DataType type, PortFlags flags, std::vector< std::string > &results) const
int set_systemic_midi_input_latency(std::string const, uint32_t)
static std::vector< std::string > _midi_options
uint32_t get_midi_event_count(void *port_buffer)
pframes_t samples_since_cycle_start()
int request_input_monitoring(PortHandle, bool)
pthread_cond_t _freewheel_signal
pthread_mutex_t _process_callback_mutex
samplepos_t sample_time_at_cycle_start()
void get_physical_inputs(DataType type, std::vector< std::string > &results)
uint32_t process_thread_count()
void * get_buffer(PortHandle, pframes_t)
bool can_monitor_input() const
std::vector< float > available_sample_rates(const std::string &device) const
int disconnect(const std::string &src, const std::string &dst)
uint32_t systemic_input_latency() const
std::vector< DeviceStatus > enumerate_midi_devices() const
std::vector< std::string > enumerate_midi_options() const
pthread_mutex_t _freewheel_mutex
PortFlags get_port_flags(PortEngine::PortHandle ph) const
bool midi_device_enabled(std::string const) const
float sample_rate() const
int _start(bool for_latency_measurement)
int set_input_device_name(const std::string &)
int get_connections(PortEngine::PortHandle ph, std::vector< std::string > &results, bool process_callback_safe)
bool can_measure_systemic_latency() const
bool connected_to(PortEngine::PortHandle ph, const std::string &other, bool process_callback_safe)
static std::vector< AudioBackend::DeviceStatus > _input_audio_device_status
const std::string & my_name() const
bool can_change_sample_rate_when_running() const
int get_port_property(PortEngine::PortHandle ph, const std::string &key, std::string &value, std::string &type) const
uint32_t port_name_size() const
static std::vector< AudioBackend::DeviceStatus > _midi_device_status
uint32_t systemic_midi_output_latency(std::string const) const
uint32_t buffer_size() const
static std::vector< AudioBackend::DeviceStatus > _output_audio_device_status
int set_interleaved(bool yn)
int set_systemic_midi_output_latency(std::string const, uint32_t)
std::string device_name() const
PortEngine::PortPtr get_port_by_name(std::string const &name) const
std::string name() const
void * private_handle() const
bool can_set_systemic_midi_latencies() const
int set_systemic_input_latency(uint32_t)
ChanCount n_physical_outputs() const
int disconnect_all(PortEngine::PortHandle ph)
std::vector< uint32_t > available_buffer_sizes2(const std::string &, const std::string &) const
static std::vector< AudioBackend::DeviceStatus > _duplex_audio_device_status
CoreAudioBackend(AudioEngine &e, AudioBackendInfo &info)
LatencyRange get_latency_range(PortHandle, bool for_playback)
std::vector< uint32_t > available_buffer_sizes(const std::string &device) const
std::vector< DeviceStatus > enumerate_output_devices() const
uint32_t name_to_id(std::string, DeviceFilter filter=All) const
CoreAudioPort(CoreAudioBackend &b, const std::string &, PortFlags)
DataType type() const
const Sample * const_buffer() const
void * get_buffer(pframes_t nframes)
CoreMidiEvent(const pframes_t timestamp, const uint8_t *data, size_t size)
CoreMidiEvent(const CoreMidiEvent &other)
const uint8_t * data() const
pframes_t timestamp() const
void record_byte(uint8_t byte)
CoreMidiBuffer _buffer[2]
void parse_events(const uint64_t time, const uint8_t *data, const size_t size)
bool process_byte(const uint64_t, const uint8_t)
CoreMidiPort(CoreAudioBackend &b, const std::string &, PortFlags)
DataType type() const
uint8_t _parser_buffer[1024]
const CoreMidiBuffer * const_buffer() const
void prepare_byte_event(const uint64_t time, const uint8_t byte)
struct ARDOUR::CoreMidiPort::ParserEvent _event
void * get_buffer(pframes_t nframes)
int queue_event(void *port_buffer, pframes_t timestamp, const uint8_t *buffer, size_t size)
bool prepare_buffered_event(const uint64_t time)
int set_port_name(PortEngine::PortHandle, const std::string &)
uint32_t port_name_size() const
virtual void unregister_port(PortEngine::PortHandle)
bool physically_connected(PortEngine::PortHandle, bool process_callback_safe)
int disconnect(const std::string &src, const std::string &dst)
PortEngine::PortPtr get_port_by_name(const std::string &) const
int get_ports(const std::string &port_name_pattern, DataType type, PortFlags flags, std::vector< std::string > &) const
int connect(const std::string &src, const std::string &dst)
ChanCount n_physical_outputs() const
PortFlags get_port_flags(PortEngine::PortHandle) const
bool connected_to(PortEngine::PortHandle, const std::string &, bool process_callback_safe)
bool connected(PortEngine::PortHandle, bool process_callback_safe)
int get_connections(PortEngine::PortHandle, std::vector< std::string > &, bool process_callback_safe)
std::string get_port_name(PortEngine::PortHandle) const
ChanCount n_physical_inputs() const
void get_physical_outputs(DataType type, std::vector< std::string > &)
void get_physical_inputs(DataType type, std::vector< std::string > &)
DataType port_data_type(PortEngine::PortHandle) const
int disconnect_all(PortEngine::PortHandle)
bool port_is_physical(PortEngine::PortHandle) const
int get_port_property(PortEngine::PortHandle, const std::string &key, std::string &value, std::string &type) const
int set_port_property(PortEngine::PortHandle, const std::string &key, const std::string &value, const std::string &type)
PortEngine::PortPtr register_port(const std::string &shortname, ARDOUR::DataType, ARDOUR::PortFlags)
PortPtr const & PortHandle
Definition: port_engine.h:113
std::shared_ptr< ProtoPort > PortPtr
Definition: port_engine.h:107
#define MaxCoreMidiEventSize
uint32_t pframes_t
std::shared_ptr< BackendPort > BackendPortPtr
std::vector< CoreMidiEvent > CoreMidiBuffer
Temporal::samplepos_t samplepos_t
ThreadData(CoreAudioBackend *e, std::function< void()> fp, size_t stacksz, double period)
ParserEvent(const uint64_t time, const size_t size)
void prepare(const uint64_t time, const size_t size)