| OLD | NEW |
| 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 <errno.h> | 7 #include <errno.h> |
| 8 #include <poll.h> | 8 #include <poll.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 #include <stdlib.h> | 10 #include <stdlib.h> |
| 11 | 11 |
| 12 #include <algorithm> | 12 #include <algorithm> |
| 13 #include <string> | 13 #include <string> |
| 14 #include <utility> | 14 #include <utility> |
| 15 | 15 |
| 16 #include "base/bind.h" | 16 #include "base/bind.h" |
| 17 #include "base/json/json_string_value_serializer.h" | 17 #include "base/json/json_string_value_serializer.h" |
| 18 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/macros.h" | 19 #include "base/macros.h" |
| 20 #include "base/memory/ptr_util.h" | 20 #include "base/memory/ptr_util.h" |
| 21 #include "base/message_loop/message_loop.h" | 21 #include "base/message_loop/message_loop.h" |
| 22 #include "base/posix/eintr_wrapper.h" | 22 #include "base/posix/eintr_wrapper.h" |
| 23 #include "base/posix/safe_strerror.h" | 23 #include "base/posix/safe_strerror.h" |
| 24 #include "base/single_thread_task_runner.h" |
| 24 #include "base/strings/string_number_conversions.h" | 25 #include "base/strings/string_number_conversions.h" |
| 25 #include "base/strings/stringprintf.h" | 26 #include "base/strings/stringprintf.h" |
| 26 #include "base/time/time.h" | 27 #include "base/time/time.h" |
| 27 #include "crypto/sha2.h" | 28 #include "crypto/sha2.h" |
| 28 #include "media/midi/midi_port_info.h" | 29 #include "media/midi/midi_port_info.h" |
| 29 | 30 |
| 30 namespace media { | 31 namespace media { |
| 31 namespace midi { | 32 namespace midi { |
| 32 | 33 |
| 33 namespace { | 34 namespace { |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 EnumerateAlsaPorts(); | 280 EnumerateAlsaPorts(); |
| 280 | 281 |
| 281 // Generate hotplug events for existing udev devices. This must be done | 282 // Generate hotplug events for existing udev devices. This must be done |
| 282 // after udev_monitor_enable_receiving() is called. See the algorithm | 283 // after udev_monitor_enable_receiving() is called. See the algorithm |
| 283 // at http://www.signal11.us/oss/udev/. | 284 // at http://www.signal11.us/oss/udev/. |
| 284 EnumerateUdevCards(); | 285 EnumerateUdevCards(); |
| 285 | 286 |
| 286 // Start processing events. Don't do this before enumeration of both | 287 // Start processing events. Don't do this before enumeration of both |
| 287 // ALSA and udev. | 288 // ALSA and udev. |
| 288 event_thread_.Start(); | 289 event_thread_.Start(); |
| 289 event_thread_.message_loop()->PostTask( | 290 event_thread_.task_runner()->PostTask( |
| 290 FROM_HERE, | 291 FROM_HERE, |
| 291 base::Bind(&MidiManagerAlsa::ScheduleEventLoop, base::Unretained(this))); | 292 base::Bind(&MidiManagerAlsa::ScheduleEventLoop, base::Unretained(this))); |
| 292 send_thread_.Start(); | 293 send_thread_.Start(); |
| 293 | 294 |
| 294 CompleteInitialization(Result::OK); | 295 CompleteInitialization(Result::OK); |
| 295 } | 296 } |
| 296 | 297 |
| 297 void MidiManagerAlsa::Finalize() { | 298 void MidiManagerAlsa::Finalize() { |
| 298 base::AutoLock lock(lazy_init_member_lock_); | 299 base::AutoLock lock(lazy_init_member_lock_); |
| 299 DCHECK(initialization_thread_checker_->CalledOnValidThread()); | 300 DCHECK(initialization_thread_checker_->CalledOnValidThread()); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 329 const std::vector<uint8_t>& data, | 330 const std::vector<uint8_t>& data, |
| 330 double timestamp) { | 331 double timestamp) { |
| 331 base::TimeDelta delay; | 332 base::TimeDelta delay; |
| 332 if (timestamp != 0.0) { | 333 if (timestamp != 0.0) { |
| 333 base::TimeTicks time_to_send = | 334 base::TimeTicks time_to_send = |
| 334 base::TimeTicks() + base::TimeDelta::FromMicroseconds( | 335 base::TimeTicks() + base::TimeDelta::FromMicroseconds( |
| 335 timestamp * base::Time::kMicrosecondsPerSecond); | 336 timestamp * base::Time::kMicrosecondsPerSecond); |
| 336 delay = std::max(time_to_send - base::TimeTicks::Now(), base::TimeDelta()); | 337 delay = std::max(time_to_send - base::TimeTicks::Now(), base::TimeDelta()); |
| 337 } | 338 } |
| 338 | 339 |
| 339 send_thread_.message_loop()->PostDelayedTask( | 340 send_thread_.task_runner()->PostDelayedTask( |
| 340 FROM_HERE, base::Bind(&MidiManagerAlsa::SendMidiData, | 341 FROM_HERE, base::Bind(&MidiManagerAlsa::SendMidiData, |
| 341 base::Unretained(this), port_index, data), | 342 base::Unretained(this), port_index, data), |
| 342 delay); | 343 delay); |
| 343 | 344 |
| 344 // Acknowledge send. | 345 // Acknowledge send. |
| 345 send_thread_.message_loop()->PostTask( | 346 send_thread_.task_runner()->PostTask( |
| 346 FROM_HERE, base::Bind(&MidiManagerAlsa::AccumulateMidiBytesSent, | 347 FROM_HERE, base::Bind(&MidiManagerAlsa::AccumulateMidiBytesSent, |
| 347 base::Unretained(this), client, data.size())); | 348 base::Unretained(this), client, data.size())); |
| 348 } | 349 } |
| 349 | 350 |
| 350 MidiManagerAlsa::MidiPort::Id::Id() = default; | 351 MidiManagerAlsa::MidiPort::Id::Id() = default; |
| 351 | 352 |
| 352 MidiManagerAlsa::MidiPort::Id::Id(const std::string& bus, | 353 MidiManagerAlsa::MidiPort::Id::Id(const std::string& bus, |
| 353 const std::string& vendor_id, | 354 const std::string& vendor_id, |
| 354 const std::string& model_id, | 355 const std::string& model_id, |
| 355 const std::string& usb_interface_num, | 356 const std::string& usb_interface_num, |
| (...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 879 snd_seq_ev_set_subs(&event); | 880 snd_seq_ev_set_subs(&event); |
| 880 snd_seq_ev_set_direct(&event); | 881 snd_seq_ev_set_direct(&event); |
| 881 snd_seq_event_output_direct(out_client_.get(), &event); | 882 snd_seq_event_output_direct(out_client_.get(), &event); |
| 882 } | 883 } |
| 883 } | 884 } |
| 884 } | 885 } |
| 885 snd_midi_event_free(encoder); | 886 snd_midi_event_free(encoder); |
| 886 } | 887 } |
| 887 | 888 |
| 888 void MidiManagerAlsa::ScheduleEventLoop() { | 889 void MidiManagerAlsa::ScheduleEventLoop() { |
| 889 event_thread_.message_loop()->PostTask( | 890 event_thread_.task_runner()->PostTask( |
| 890 FROM_HERE, | 891 FROM_HERE, |
| 891 base::Bind(&MidiManagerAlsa::EventLoop, base::Unretained(this))); | 892 base::Bind(&MidiManagerAlsa::EventLoop, base::Unretained(this))); |
| 892 } | 893 } |
| 893 | 894 |
| 894 void MidiManagerAlsa::EventLoop() { | 895 void MidiManagerAlsa::EventLoop() { |
| 895 bool loop_again = true; | 896 bool loop_again = true; |
| 896 | 897 |
| 897 struct pollfd pfd[2]; | 898 struct pollfd pfd[2]; |
| 898 snd_seq_poll_descriptors(in_client_.get(), &pfd[0], 1, POLLIN); | 899 snd_seq_poll_descriptors(in_client_.get(), &pfd[0], 1, POLLIN); |
| 899 pfd[1].fd = device::udev_monitor_get_fd(udev_monitor_.get()); | 900 pfd[1].fd = device::udev_monitor_get_fd(udev_monitor_.get()); |
| (...skipping 497 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1397 source_map_[AddrToInt(client_id, port_id)] = port_index; | 1398 source_map_[AddrToInt(client_id, port_id)] = port_index; |
| 1398 return true; | 1399 return true; |
| 1399 } | 1400 } |
| 1400 | 1401 |
| 1401 MidiManager* MidiManager::Create() { | 1402 MidiManager* MidiManager::Create() { |
| 1402 return new MidiManagerAlsa(); | 1403 return new MidiManagerAlsa(); |
| 1403 } | 1404 } |
| 1404 | 1405 |
| 1405 } // namespace midi | 1406 } // namespace midi |
| 1406 } // namespace media | 1407 } // namespace media |
| OLD | NEW |