Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(164)

Side by Side Diff: media/midi/midi_manager_alsa.cc

Issue 264973012: Use seq instead of rawmidi for MidiManagerAlsa (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Increase MIDI send buffer size to 256, to avoid splitting sysex messages too much. Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/midi/midi_manager_alsa.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/midi/midi_manager_alsa.h" 5 #include "media/midi/midi_manager_alsa.h"
6 6
7 #include <alsa/asoundlib.h> 7 #include <alsa/asoundlib.h>
8 #include <stdlib.h> 8 #include <stdlib.h>
9 #include <algorithm> 9 #include <algorithm>
10 #include <string> 10 #include <string>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
16 #include "base/posix/eintr_wrapper.h" 16 #include "base/posix/eintr_wrapper.h"
17 #include "base/strings/stringprintf.h" 17 #include "base/strings/stringprintf.h"
18 #include "base/threading/thread.h" 18 #include "base/threading/thread.h"
19 #include "base/time/time.h" 19 #include "base/time/time.h"
20 #include "media/midi/midi_port_info.h" 20 #include "media/midi/midi_port_info.h"
21 21
22 namespace media { 22 namespace media {
23 23
24 namespace { 24 namespace {
25 25
26 const size_t kReceiveBufferSize = 4096; 26 const size_t kSendBufferSize = 256;
Takashi Toyoshima 2014/05/07 13:06:13 If larger buffer size improves sysex performance,
Adam Goode 2014/05/08 03:00:54 I put a note here. I don't think a large buffer is
27 const unsigned short kPollEventMask = POLLIN | POLLERR | POLLNVAL; 27 const unsigned int kRequiredInputPortCaps =
28 SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ;
29 const unsigned int kRequiredOutputPortCaps =
30 SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE;
31
32 int AddrToInt(const snd_seq_addr_t* addr) {
33 return (addr->client << 8) | addr->port;
34 }
28 35
29 } // namespace 36 } // namespace
30 37
31 class MidiManagerAlsa::MidiDeviceInfo
32 : public base::RefCounted<MidiDeviceInfo> {
33 public:
34 MidiDeviceInfo(MidiManagerAlsa* manager,
35 const std::string& bus_id,
36 snd_ctl_card_info_t* card,
37 const snd_rawmidi_info_t* midi,
38 int device) {
39 opened_ = !snd_rawmidi_open(&midi_in_, &midi_out_, bus_id.c_str(), 0);
40 if (!opened_)
41 return;
42
43 const std::string id = base::StringPrintf("%s:%d", bus_id.c_str(), device);
44 const std::string name = snd_rawmidi_info_get_name(midi);
45 // We assume that card longname is in the format of
46 // "<manufacturer> <name> at <bus>". Otherwise, we give up to detect
47 // a manufacturer name here.
48 std::string manufacturer;
49 const std::string card_name = snd_ctl_card_info_get_longname(card);
50 size_t name_index = card_name.find(name);
51 if (std::string::npos != name_index)
52 manufacturer = card_name.substr(0, name_index - 1);
53 const std::string version =
54 base::StringPrintf("%s / ALSA library version %d.%d.%d",
55 snd_ctl_card_info_get_driver(card),
56 SND_LIB_MAJOR, SND_LIB_MINOR, SND_LIB_SUBMINOR);
57 port_info_ = MidiPortInfo(id, manufacturer, name, version);
58 }
59
60 void Send(MidiManagerClient* client, const std::vector<uint8>& data) {
61 ssize_t result = snd_rawmidi_write(
62 midi_out_, reinterpret_cast<const void*>(&data[0]), data.size());
63 if (static_cast<size_t>(result) != data.size()) {
64 // TODO(toyoshim): Handle device disconnection.
65 VLOG(1) << "snd_rawmidi_write fails: " << strerror(-result);
66 }
67 base::MessageLoop::current()->PostTask(
68 FROM_HERE,
69 base::Bind(&MidiManagerClient::AccumulateMidiBytesSent,
70 base::Unretained(client), data.size()));
71 }
72
73 // Read input data from a MIDI input device which is ready to read through
74 // the ALSA library. Called from EventLoop() and read data will be sent to
75 // blink through MIDIManager base class.
76 size_t Receive(uint8* data, size_t length) {
77 return snd_rawmidi_read(midi_in_, reinterpret_cast<void*>(data), length);
78 }
79
80 const MidiPortInfo& GetMidiPortInfo() const { return port_info_; }
81
82 // Get the number of descriptors which is required to call poll() on the
83 // device. The ALSA library always returns 1 here now, but it may be changed
84 // in the future.
85 int GetPollDescriptorsCount() {
86 return snd_rawmidi_poll_descriptors_count(midi_in_);
87 }
88
89 // Following API initializes pollfds for polling the device, and returns the
90 // number of descriptors they are initialized. It must be the same value with
91 // snd_rawmidi_poll_descriptors_count().
92 int SetupPollDescriptors(struct pollfd* pfds, unsigned int count) {
93 return snd_rawmidi_poll_descriptors(midi_in_, pfds, count);
94 }
95
96 unsigned short GetPollDescriptorsRevents(struct pollfd* pfds) {
97 unsigned short revents;
98 snd_rawmidi_poll_descriptors_revents(midi_in_,
99 pfds,
100 GetPollDescriptorsCount(),
101 &revents);
102 return revents;
103 }
104
105 bool IsOpened() const { return opened_; }
106
107 private:
108 friend class base::RefCounted<MidiDeviceInfo>;
109 virtual ~MidiDeviceInfo() {
110 if (opened_) {
111 snd_rawmidi_close(midi_in_);
112 snd_rawmidi_close(midi_out_);
113 }
114 }
115
116 bool opened_;
117 MidiPortInfo port_info_;
118 snd_rawmidi_t* midi_in_;
119 snd_rawmidi_t* midi_out_;
120
121 DISALLOW_COPY_AND_ASSIGN(MidiDeviceInfo);
122 };
123
124 MidiManagerAlsa::MidiManagerAlsa() 38 MidiManagerAlsa::MidiManagerAlsa()
125 : send_thread_("MidiSendThread"), 39 : in_client_(NULL),
126 event_thread_("MidiEventThread") { 40 out_client_(NULL),
127 for (size_t i = 0; i < arraysize(pipe_fd_); ++i) 41 out_client_id_(-1),
128 pipe_fd_[i] = -1; 42 in_port_(-1),
43 decoder_(NULL),
44 send_thread_("MidiSendThread"),
45 event_thread_("MidiEventThread"),
46 event_thread_shutdown_(false) {
47 // Initialize decoder.
48 snd_midi_event_new(0, &decoder_);
49 snd_midi_event_no_status(decoder_, 1);
129 } 50 }
130 51
131 void MidiManagerAlsa::StartInitialization() { 52 void MidiManagerAlsa::StartInitialization() {
132 // Enumerate only hardware MIDI devices because software MIDIs running in 53 // Create client handles.
133 // the browser process is not secure. 54 int err = snd_seq_open(&in_client_, "hw", SND_SEQ_OPEN_INPUT, 0);
134 snd_ctl_card_info_t* card; 55 if (err != 0) {
135 snd_rawmidi_info_t* midi_out; 56 VLOG(1) << "snd_seq_open fails: " << snd_strerror(err);
136 snd_rawmidi_info_t* midi_in; 57 // Nothing to close, just return failure.
137 snd_ctl_card_info_alloca(&card); 58 return CompleteInitialization(MIDI_INITIALIZATION_ERROR);
138 snd_rawmidi_info_alloca(&midi_out); 59 }
139 snd_rawmidi_info_alloca(&midi_in); 60 int in_client_id = snd_seq_client_id(in_client_);
140 for (int index = -1; !snd_card_next(&index) && index >= 0; ) { 61 err = snd_seq_open(&out_client_, "hw", SND_SEQ_OPEN_OUTPUT, 0);
141 const std::string id = base::StringPrintf("hw:CARD=%i", index); 62 if (err != 0) {
142 snd_ctl_t* handle; 63 VLOG(1) << "snd_seq_open fails: " << snd_strerror(err);
143 int err = snd_ctl_open(&handle, id.c_str(), 0); 64 return CompleteInitialization(MIDI_INITIALIZATION_ERROR);
144 if (err != 0) { 65 }
145 VLOG(1) << "snd_ctl_open fails: " << snd_strerror(err); 66 out_client_id_ = snd_seq_client_id(out_client_);
67
68 // Name the clients.
69 err = snd_seq_set_client_name(in_client_, "Google Chrome (input)");
70 if (err != 0) {
71 VLOG(1) << "snd_seq_set_client_name fails: " << snd_strerror(err);
72 return CompleteInitialization(MIDI_INITIALIZATION_ERROR);
73 }
74 err = snd_seq_set_client_name(out_client_, "Google Chrome (output)");
75 if (err != 0) {
76 VLOG(1) << "snd_seq_set_client_name fails: " << snd_strerror(err);
77 return CompleteInitialization(MIDI_INITIALIZATION_ERROR);
78 }
79
80 // Create input port.
81 in_port_ = snd_seq_create_simple_port(in_client_, NULL,
82 SND_SEQ_PORT_CAP_WRITE |
83 SND_SEQ_PORT_CAP_NO_EXPORT,
84 SND_SEQ_PORT_TYPE_MIDI_GENERIC |
85 SND_SEQ_PORT_TYPE_APPLICATION);
86 if (in_port_ < 0) {
87 VLOG(1) << "snd_seq_create_simple_port fails: " << snd_strerror(in_port_);
88 return CompleteInitialization(MIDI_INITIALIZATION_ERROR);
89 }
90
91 // Subscribe to the announce port.
92 snd_seq_port_subscribe_t* subs;
93 snd_seq_port_subscribe_alloca(&subs);
94 snd_seq_addr_t announce_sender;
95 snd_seq_addr_t announce_dest;
96 announce_sender.client = SND_SEQ_CLIENT_SYSTEM;
97 announce_sender.port = SND_SEQ_PORT_SYSTEM_ANNOUNCE;
98 announce_dest.client = in_client_id;
99 announce_dest.port = in_port_;
100 snd_seq_port_subscribe_set_sender(subs, &announce_sender);
101 snd_seq_port_subscribe_set_dest(subs, &announce_dest);
102 err = snd_seq_subscribe_port(in_client_, subs);
103 if (err != 0) {
104 VLOG(1) << "snd_seq_subscribe_port on the announce port fails: "
105 << snd_strerror(err);
106 return CompleteInitialization(MIDI_INITIALIZATION_ERROR);
107 }
108
109 // Enumerate all ports in all clients.
110 snd_seq_client_info_t* client_info;
111 snd_seq_client_info_alloca(&client_info);
112 snd_seq_port_info_t* port_info;
113 snd_seq_port_info_alloca(&port_info);
114
115 snd_seq_client_info_set_client(client_info, -1);
116 // Enumerate clients.
117 uint32 current_input = 0;
118 while (!snd_seq_query_next_client(in_client_, client_info)) {
119 int client_id = snd_seq_client_info_get_client(client_info);
120 if ((client_id == in_client_id) || (client_id == out_client_id_)) {
121 // Skip our own clients.
146 continue; 122 continue;
147 } 123 }
148 err = snd_ctl_card_info(handle, card); 124 snd_seq_port_info_set_client(port_info, client_id);
149 if (err != 0) { 125 snd_seq_port_info_set_port(port_info, -1);
150 VLOG(1) << "snd_ctl_card_info fails: " << snd_strerror(err); 126 // Enumerate ports.
151 snd_ctl_close(handle); 127 while (!snd_seq_query_next_port(in_client_, port_info)) {
152 continue; 128 unsigned int port_type = snd_seq_port_info_get_type(port_info);
153 } 129 if (port_type & SND_SEQ_PORT_TYPE_MIDI_GENERIC) {
154 for (int device = -1; 130 const snd_seq_addr_t* addr = snd_seq_port_info_get_addr(port_info);
155 !snd_ctl_rawmidi_next_device(handle, &device) && device >= 0; ) { 131 const std::string name = snd_seq_port_info_get_name(port_info);
156 bool output; 132 const std::string id = base::StringPrintf("%d:%d %s",
157 bool input; 133 addr->client,
158 snd_rawmidi_info_set_device(midi_out, device); 134 addr->port,
159 snd_rawmidi_info_set_subdevice(midi_out, 0); 135 name.c_str());
160 snd_rawmidi_info_set_stream(midi_out, SND_RAWMIDI_STREAM_OUTPUT); 136
161 output = snd_ctl_rawmidi_info(handle, midi_out) == 0; 137 unsigned int caps = snd_seq_port_info_get_capability(port_info);
162 snd_rawmidi_info_set_device(midi_in, device); 138 if ((caps & kRequiredInputPortCaps) == kRequiredInputPortCaps) {
163 snd_rawmidi_info_set_subdevice(midi_in, 0); 139 // Subscribe to this port.
164 snd_rawmidi_info_set_stream(midi_in, SND_RAWMIDI_STREAM_INPUT); 140 const snd_seq_addr_t* sender = snd_seq_port_info_get_addr(port_info);
165 input = snd_ctl_rawmidi_info(handle, midi_in) == 0; 141 snd_seq_addr_t dest;
166 if (!output && !input) 142 dest.client = snd_seq_client_id(in_client_);
167 continue; 143 dest.port = in_port_;
168 scoped_refptr<MidiDeviceInfo> port = new MidiDeviceInfo( 144 snd_seq_port_subscribe_set_sender(subs, sender);
169 this, id, card, output ? midi_out : midi_in, device); 145 snd_seq_port_subscribe_set_dest(subs, &dest);
170 if (!port->IsOpened()) { 146 err = snd_seq_subscribe_port(in_client_, subs);
171 VLOG(1) << "MidiDeviceInfo open fails"; 147 if (err != 0) {
172 continue; 148 VLOG(1) << "snd_seq_subscribe_port fails: " << snd_strerror(err);
149 } else {
150 source_map_[AddrToInt(sender)] = current_input++;
151 AddInputPort(MidiPortInfo(id, "", name, ""));
152 }
153 }
154 if ((caps & kRequiredOutputPortCaps) == kRequiredOutputPortCaps) {
155 // Create a port for us to send on.
156 int out_port =
157 snd_seq_create_simple_port(out_client_, NULL,
158 SND_SEQ_PORT_CAP_READ |
159 SND_SEQ_PORT_CAP_NO_EXPORT,
160 SND_SEQ_PORT_TYPE_MIDI_GENERIC |
161 SND_SEQ_PORT_TYPE_APPLICATION);
162 if (out_port < 0) {
163 VLOG(1) << "snd_seq_create_simple_port fails: "
164 << snd_strerror(out_port);
165 // Skip this output port for now.
166 continue;
167 }
168
169 // Activate port subscription.
170 snd_seq_addr_t sender;
171 const snd_seq_addr_t* dest = snd_seq_port_info_get_addr(port_info);
172 sender.client = snd_seq_client_id(out_client_);
173 sender.port = out_port;
174 snd_seq_port_subscribe_set_sender(subs, &sender);
175 snd_seq_port_subscribe_set_dest(subs, dest);
176 err = snd_seq_subscribe_port(out_client_, subs);
177 if (err != 0) {
178 VLOG(1) << "snd_seq_subscribe_port fails: " << snd_strerror(err);
179 snd_seq_delete_simple_port(out_client_, out_port);
180 } else {
181 snd_midi_event_t* encoder;
182 snd_midi_event_new(kSendBufferSize, &encoder);
183 encoders_.push_back(encoder);
184 out_ports_.push_back(out_port);
185 AddOutputPort(MidiPortInfo(id, "", name, ""));
186 }
187 }
173 } 188 }
174 if (input) { 189 }
175 in_devices_.push_back(port); 190 }
176 AddInputPort(port->GetMidiPortInfo()); 191
177 } 192 event_thread_.Start();
178 if (output) { 193 event_thread_.message_loop()->PostTask(
179 out_devices_.push_back(port); 194 FROM_HERE,
180 AddOutputPort(port->GetMidiPortInfo()); 195 base::Bind(&MidiManagerAlsa::EventReset, base::Unretained(this)));
181 } 196
182 } 197 CompleteInitialization(MIDI_OK);
183 snd_ctl_close(handle);
184 }
185
186 if (pipe(pipe_fd_) < 0) {
187 VPLOG(1) << "pipe() failed";
188 CompleteInitialization(MIDI_INITIALIZATION_ERROR);
189 } else {
190 event_thread_.Start();
191 event_thread_.message_loop()->PostTask(
192 FROM_HERE,
193 base::Bind(&MidiManagerAlsa::EventReset, base::Unretained(this)));
194 CompleteInitialization(MIDI_OK);
195 }
196 } 198 }
197 199
198 MidiManagerAlsa::~MidiManagerAlsa() { 200 MidiManagerAlsa::~MidiManagerAlsa() {
199 // Send a shutdown message to awake |event_thread_| from poll(). 201 // Tell the event thread it will soon be time to shut down.
200 if (pipe_fd_[1] >= 0) 202 shutdown_mu_.Acquire();
201 HANDLE_EINTR(write(pipe_fd_[1], "Q", 1)); 203 event_thread_shutdown_ = true;
202 204 shutdown_mu_.Release();
203 // Stop receiving messages. 205
206 // Stop the send thread.
207 send_thread_.Stop();
208
209 // Close the out client. This will trigger the event thread to stop,
210 // because of SND_SEQ_EVENT_CLIENT_EXIT.
211 if (out_client_) {
212 snd_seq_close(out_client_);
213 }
214
215 // Wait for the event thread to stop.
204 event_thread_.Stop(); 216 event_thread_.Stop();
205 217
206 for (int i = 0; i < 2; ++i) { 218 // Close the in client.
207 if (pipe_fd_[i] >= 0) 219 if (in_client_) {
208 close(pipe_fd_[i]); 220 snd_seq_close(in_client_);
209 } 221 }
210 send_thread_.Stop(); 222
223 // Free the decoder.
224 snd_midi_event_free(decoder_);
225
226 // Free the encoders.
227 for (EncoderList::iterator i = encoders_.begin(); i != encoders_.end(); ++i) {
228 snd_midi_event_free(*i);
229 }
230 }
231
232 void MidiManagerAlsa::SendMidiData(uint32 port_index,
233 const std::vector<uint8>& data) {
234 DCHECK(send_thread_.message_loop_proxy()->BelongsToCurrentThread());
235
236 snd_midi_event_t* encoder = encoders_[port_index];
237 for (unsigned int i = 0; i < data.size(); i++) {
238 snd_seq_event_t event;
239 int result = snd_midi_event_encode_byte(encoder, data[i], &event);
240 if (result == 1) {
241 // Full event, send it.
242 snd_seq_ev_set_source(&event, out_ports_[port_index]);
243 snd_seq_ev_set_subs(&event);
244 snd_seq_ev_set_direct(&event);
245 snd_seq_event_output_direct(out_client_, &event);
246 }
247 }
211 } 248 }
212 249
213 void MidiManagerAlsa::DispatchSendMidiData(MidiManagerClient* client, 250 void MidiManagerAlsa::DispatchSendMidiData(MidiManagerClient* client,
214 uint32 port_index, 251 uint32 port_index,
215 const std::vector<uint8>& data, 252 const std::vector<uint8>& data,
216 double timestamp) { 253 double timestamp) {
217 if (out_devices_.size() <= port_index) 254 if (out_ports_.size() <= port_index)
218 return; 255 return;
219 256
257 if (!send_thread_.IsRunning())
258 send_thread_.Start();
259
260 // Compute delay.
220 base::TimeDelta delay; 261 base::TimeDelta delay;
221 if (timestamp != 0.0) { 262 if (timestamp != 0.0) {
222 base::TimeTicks time_to_send = 263 delay =
223 base::TimeTicks() + base::TimeDelta::FromMicroseconds( 264 base::TimeDelta::FromSecondsD(timestamp) -
224 timestamp * base::Time::kMicrosecondsPerSecond); 265 (base::TimeTicks::HighResNow() - base::TimeTicks());
225 delay = std::max(time_to_send - base::TimeTicks::Now(), base::TimeDelta()); 266 }
226 } 267
227 268 // Avoid negative delay. This can happen: because of timer jitter?
228 if (!send_thread_.IsRunning()) 269 if (delay < base::TimeDelta()) {
229 send_thread_.Start(); 270 delay = base::TimeDelta();
230 271 }
Takashi Toyoshima 2014/05/07 13:06:13 Just a question, but why do you want to change thi
Adam Goode 2014/05/08 03:00:54 Thanks for pointing this out. I actually think my
231 scoped_refptr<MidiDeviceInfo> device = out_devices_[port_index]; 272
232 send_thread_.message_loop()->PostDelayedTask( 273 send_thread_.message_loop()->PostDelayedTask(
233 FROM_HERE, 274 FROM_HERE,
234 base::Bind(&MidiDeviceInfo::Send, device, client, data), 275 base::Bind(&MidiManagerAlsa::SendMidiData, base::Unretained(this),
235 delay); 276 port_index, data), delay);
277
278 // Acknowledge send.
279 send_thread_.message_loop()->PostTask(
280 FROM_HERE,
281 base::Bind(&MidiManagerClient::AccumulateMidiBytesSent,
282 base::Unretained(client), data.size()));
236 } 283 }
237 284
238 void MidiManagerAlsa::EventReset() { 285 void MidiManagerAlsa::EventReset() {
239 CHECK_GE(pipe_fd_[0], 0);
240
241 // Sum up descriptors which are needed to poll input devices and a shutdown
242 // message.
243 // Keep the first one descriptor for a shutdown message.
244 size_t poll_fds_size = 1;
245 for (size_t i = 0; i < in_devices_.size(); ++i)
246 poll_fds_size += in_devices_[i]->GetPollDescriptorsCount();
247 poll_fds_.resize(poll_fds_size);
248
249 // Setup struct pollfd to poll input MIDI devices and a shutdown message.
250 // The first pollfd is for a shutdown message.
251 poll_fds_[0].fd = pipe_fd_[0];
252 poll_fds_[0].events = kPollEventMask;
253 int fds_index = 1;
254 for (size_t i = 0; i < in_devices_.size(); ++i) {
255 fds_index += in_devices_[i]->SetupPollDescriptors(
256 &poll_fds_[fds_index], poll_fds_.size() - fds_index);
257 }
258
259 event_thread_.message_loop()->PostTask( 286 event_thread_.message_loop()->PostTask(
260 FROM_HERE, 287 FROM_HERE,
261 base::Bind(&MidiManagerAlsa::EventLoop, base::Unretained(this))); 288 base::Bind(&MidiManagerAlsa::EventLoop, base::Unretained(this)));
262 } 289 }
263 290
264 void MidiManagerAlsa::EventLoop() { 291 void MidiManagerAlsa::EventLoop() {
265 if (HANDLE_EINTR(poll(&poll_fds_[0], poll_fds_.size(), -1)) < 0) {
266 VPLOG(1) << "Couldn't poll(). Stop to poll input MIDI devices.";
267 // TODO(toyoshim): Handle device disconnection, and try to reconnect?
268 return;
269 }
270
271 // Check timestamp as soon as possible because the API requires accurate
272 // timestamp as possible. It will be useful for recording MIDI events.
273 base::TimeTicks now = base::TimeTicks::HighResNow();
274
275 // Is this thread going to be shutdown?
276 if (poll_fds_[0].revents & kPollEventMask)
277 return;
278
279 // Read available incoming MIDI data. 292 // Read available incoming MIDI data.
280 int fds_index = 1; 293 snd_seq_event_t* event;
281 uint8 buffer[kReceiveBufferSize]; 294 int err = snd_seq_event_input(in_client_, &event);
282 295 double timestamp =
283 for (size_t i = 0; i < in_devices_.size(); ++i) { 296 (base::TimeTicks::HighResNow() - base::TimeTicks()).InSecondsF();
284 unsigned short revents = 297 if (err == -ENOSPC) {
285 in_devices_[i]->GetPollDescriptorsRevents(&poll_fds_[fds_index]); 298 VLOG(1) << "snd_seq_event_input detected buffer overrun";
286 if (revents & (POLLERR | POLLNVAL)) { 299
287 // TODO(toyoshim): Handle device disconnection. 300 // We've lost events: check another way to see if we need to shut down.
288 VLOG(1) << "snd_rawmidi_descriptors_revents fails"; 301 base::AutoLock lock(shutdown_mu_);
289 poll_fds_[fds_index].events = 0; 302 if (event_thread_shutdown_) {
290 } 303 return;
291 if (revents & POLLIN) { 304 }
292 size_t read_size = in_devices_[i]->Receive(buffer, kReceiveBufferSize); 305 } else if (err < 0) {
293 ReceiveMidiData(i, buffer, read_size, now); 306 VLOG(1) << "snd_seq_event_input fails: " << snd_strerror(err);
294 } 307 return;
295 fds_index += in_devices_[i]->GetPollDescriptorsCount(); 308 } else {
309 // Check for disconnection of out client. This means "shut down".
310 if (event->source.client == SND_SEQ_CLIENT_SYSTEM &&
311 event->source.port == SND_SEQ_PORT_SYSTEM_ANNOUNCE &&
312 event->type == SND_SEQ_EVENT_CLIENT_EXIT &&
313 event->data.addr.client == out_client_id_) {
314 return;
315 }
316
317 std::map<int, uint32>::iterator source_it =
318 source_map_.find(AddrToInt(&event->source));
319 if (source_it != source_map_.end()) {
320 uint32 source = source_it->second;
321 if (event->type == SND_SEQ_EVENT_SYSEX) {
322 // Special! Variable-length sysex.
323 ReceiveMidiData(source, static_cast<const uint8*>(event->data.ext.ptr),
324 event->data.ext.len,
325 timestamp);
326 } else {
327 // Otherwise, decode this and send that on.
328 unsigned char buf[12];
329 long count = snd_midi_event_decode(decoder_, buf, sizeof(buf), event);
330 if (count <= 0) {
331 if (count != -ENOENT) {
332 // ENOENT means that it's not a MIDI message, which is not an
333 // error, but other negative values are errors for us.
334 VLOG(1) << "snd_midi_event_decoder fails " << snd_strerror(count);
335 }
336 } else {
337 ReceiveMidiData(source, buf, count, timestamp);
338 }
339 }
340 }
296 } 341 }
297 342
298 // Do again. 343 // Do again.
299 event_thread_.message_loop()->PostTask( 344 event_thread_.message_loop()->PostTask(
300 FROM_HERE, 345 FROM_HERE,
301 base::Bind(&MidiManagerAlsa::EventLoop, base::Unretained(this))); 346 base::Bind(&MidiManagerAlsa::EventLoop, base::Unretained(this)));
302 } 347 }
303 348
304 MidiManager* MidiManager::Create() { 349 MidiManager* MidiManager::Create() {
305 return new MidiManagerAlsa(); 350 return new MidiManagerAlsa();
306 } 351 }
307 352
308 } // namespace media 353 } // namespace media
OLDNEW
« no previous file with comments | « media/midi/midi_manager_alsa.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698