Fix #2478; deref of 0 pointer if jack_port_register fails.
[ardour.git] / libs / midi++2 / jack_midiport.cc
1 /*
2   Copyright (C) 2006 Paul Davis 
3   Written by Dave Robillard
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
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <fcntl.h>
21 #include <cerrno>
22 #include <cassert>
23
24 #include <pbd/error.h>
25
26 #include <midi++/types.h>
27 #include <midi++/jack.h>
28
29 using namespace std;
30 using namespace MIDI;
31 using namespace PBD;
32
33 pthread_t JACK_MidiPort::_process_thread;
34
35 JACK_MidiPort::JACK_MidiPort(const XMLNode& node, jack_client_t* jack_client)
36         : Port(node)
37         , _jack_client(jack_client)
38         , _jack_input_port(NULL)
39         , _jack_output_port(NULL)
40         , _last_read_index(0)
41         , non_process_thread_fifo (512)
42 {
43         int err = create_ports (node);
44
45         if (!err) {
46                 _ok = true;
47         } 
48 }
49
50 JACK_MidiPort::~JACK_MidiPort()
51 {
52         // FIXME: remove port
53 }
54
55 void
56 JACK_MidiPort::cycle_start (nframes_t nframes)
57 {
58         Port::cycle_start(nframes);
59         assert(_nframes_this_cycle == nframes);
60         _last_read_index = 0;
61         _last_write_timestamp = 0;
62
63         if (_jack_output_port != 0) {
64                 // output
65                 void *buffer = jack_port_get_buffer (_jack_output_port, nframes);
66                 jack_midi_clear_buffer (buffer);
67                 flush (buffer); 
68         }
69         
70         if (_jack_input_port != 0) {
71                 // input
72                 void* jack_buffer = jack_port_get_buffer(_jack_input_port, nframes);
73                 const nframes_t event_count = jack_midi_get_event_count(jack_buffer);
74
75                 jack_midi_event_t ev;
76
77                 for (nframes_t i=0; i < event_count; ++i) {
78
79                         jack_midi_event_get (&ev, jack_buffer, i);
80
81                         if (input_parser) {
82                                 for (size_t i = 0; i < ev.size; i++) {
83                                         // the midi events here are used for MIDI clock only
84                                         input_parser->set_midi_clock_timestamp(ev.time + jack_last_frame_time(_jack_client));
85                                         input_parser->scanner (ev.buffer[i]);
86                                 }       
87                         }
88                 }       
89         }
90 }
91
92 int
93 JACK_MidiPort::write(byte * msg, size_t msglen, timestamp_t timestamp)
94 {
95         int ret = 0;
96
97         if (!is_process_thread()) {
98
99                 Glib::Mutex::Lock lm (non_process_thread_fifo_lock);
100                 RingBuffer<Evoral::Event>::rw_vector vec;
101                 
102                 non_process_thread_fifo.get_write_vector (&vec);
103
104                 if (vec.len[0] + vec.len[1] < 1) {
105                         error << "no space in FIFO for non-process thread MIDI write"
106                               << endmsg;
107                         return 0;
108                 }
109
110                 if (vec.len[0]) {
111                         vec.buf[0]->set (msg, msglen, timestamp);
112                 } else {
113                         vec.buf[1]->set (msg, msglen, timestamp);
114                 }
115
116                 non_process_thread_fifo.increment_write_idx (1);
117                 
118                 ret = msglen;
119                                 
120         } else {
121
122                 assert(_jack_output_port);
123                 
124                 // XXX This had to be temporarily commented out to make export work again
125                 if (!(timestamp < _nframes_this_cycle)) {
126                         std::cerr << "assertion timestamp < _nframes_this_cycle failed!" << std::endl;
127                 }
128
129                 if (_currently_in_cycle) {
130                         if (timestamp == 0) {
131                                 timestamp = _last_write_timestamp;
132                         } 
133
134                         if (jack_midi_event_write (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle), 
135                                                 timestamp, msg, msglen) == 0) {
136                                 ret = msglen;
137                                 _last_write_timestamp = timestamp;
138
139                         } else {
140                                 ret = 0;
141                                 cerr << "write of " << msglen << " failed, port holds "
142                                         << jack_midi_get_event_count (jack_port_get_buffer (_jack_output_port, _nframes_this_cycle))
143                                         << endl;
144                         }
145                 } else {
146                         cerr << "write to JACK midi port failed: not currently in a process cycle." << endl;
147                 }
148         }
149
150         if (ret > 0 && output_parser) {
151                 output_parser->raw_preparse (*output_parser, msg, ret);
152                 for (int i = 0; i < ret; i++) {
153                         output_parser->scanner (msg[i]);
154                 }
155                 output_parser->raw_postparse (*output_parser, msg, ret);
156         }       
157
158         return ret;
159 }
160
161 void
162 JACK_MidiPort::flush (void* jack_port_buffer)
163 {
164         RingBuffer<Evoral::Event>::rw_vector vec;
165         size_t written;
166
167         non_process_thread_fifo.get_read_vector (&vec);
168
169         if (vec.len[0] + vec.len[1]) {
170                 cerr << "Flush " << vec.len[0] + vec.len[1] << " events from non-process FIFO\n";
171         }
172
173         if (vec.len[0]) {
174                 Evoral::Event* evp = vec.buf[0];
175                 
176                 for (size_t n = 0; n < vec.len[0]; ++n, ++evp) {
177                         jack_midi_event_write (jack_port_buffer,
178                                                (timestamp_t) evp->time(), evp->buffer(), evp->size());
179                 }
180         }
181         
182         if (vec.len[1]) {
183                 Evoral::Event* evp = vec.buf[1];
184
185                 for (size_t n = 0; n < vec.len[1]; ++n, ++evp) {
186                         jack_midi_event_write (jack_port_buffer,
187                                                (timestamp_t) evp->time(), evp->buffer(), evp->size());
188                 }
189         }
190         
191         if ((written = vec.len[0] + vec.len[1]) != 0) {
192                 non_process_thread_fifo.increment_read_idx (written);
193         }
194 }
195
196 int
197 JACK_MidiPort::read(byte * buf, size_t bufsize)
198 {
199         assert(_currently_in_cycle);
200         assert(_jack_input_port);
201         
202         jack_midi_event_t ev;
203
204         cerr << "JACK_MidiPort::read called" << endl;
205         
206         int err = jack_midi_event_get (&ev,
207                                        jack_port_get_buffer(_jack_input_port, _nframes_this_cycle),
208                                        _last_read_index++);
209         
210         // XXX this doesn't handle ev.size > max
211
212         if (!err) {
213                 size_t limit = min (bufsize, ev.size);
214                 memcpy(buf, ev.buffer, limit);
215
216                 if (input_parser) {
217                         input_parser->raw_preparse (*input_parser, buf, limit);
218                         for (size_t i = 0; i < limit; i++) {
219                                 input_parser->scanner (buf[i]);
220                         }       
221                         input_parser->raw_postparse (*input_parser, buf, limit);
222                 }
223
224                 return limit;
225         } else {
226                 return 0;
227         }
228 }
229
230 int
231 JACK_MidiPort::create_ports(const XMLNode& node)
232 {
233         Descriptor desc (node);
234
235         assert(!_jack_input_port);
236         assert(!_jack_output_port);
237         
238         jack_nframes_t nframes = jack_get_buffer_size(_jack_client);
239
240         bool ret = true;
241
242         if (desc.mode == O_RDWR || desc.mode == O_WRONLY) {
243                 _jack_output_port = jack_port_register(_jack_client,
244                                                        string(desc.tag).append("_out").c_str(),
245                                                        JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
246                 if (_jack_output_port) {
247                         jack_midi_clear_buffer(jack_port_get_buffer(_jack_output_port, nframes));
248                 }
249                 ret = ret && (_jack_output_port != NULL);
250         }
251         
252         if (desc.mode == O_RDWR || desc.mode == O_RDONLY) {
253                 _jack_input_port = jack_port_register(_jack_client,
254                                                       string(desc.tag).append("_in").c_str(),
255                                                       JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
256                 if (_jack_input_port) {
257                         jack_midi_clear_buffer(jack_port_get_buffer(_jack_input_port, nframes));
258                 }
259                 ret = ret && (_jack_input_port != NULL);
260         }
261
262         return ret ? 0 : -1;
263 }
264
265 XMLNode& 
266 JACK_MidiPort::get_state () const
267 {
268         XMLNode& root (Port::get_state ());
269         return root;
270 }
271
272 void
273 JACK_MidiPort::set_state (const XMLNode& node)
274 {
275 }
276
277 void
278 JACK_MidiPort::set_process_thread (pthread_t thr)
279 {
280         _process_thread = thr;
281 }
282
283 bool
284 JACK_MidiPort::is_process_thread()
285 {
286         return (pthread_self() == _process_thread);
287 }