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 <poll.h> | 7 #include <poll.h> |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <string> | 10 #include <string> |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 size_t i = syspath_str.rfind(kCardSyspath); | 117 size_t i = syspath_str.rfind(kCardSyspath); |
118 if (i == std::string::npos) | 118 if (i == std::string::npos) |
119 return -1; | 119 return -1; |
120 | 120 |
121 int number; | 121 int number; |
122 if (!base::StringToInt(syspath_str.substr(i + strlen(kCardSyspath)), &number)) | 122 if (!base::StringToInt(syspath_str.substr(i + strlen(kCardSyspath)), &number)) |
123 return -1; | 123 return -1; |
124 return number; | 124 return number; |
125 } | 125 } |
126 | 126 |
| 127 std::string GetVendor(udev_device* dev) { |
| 128 // Try to get the vendor string. Sometimes it is encoded. |
| 129 std::string vendor = device::UdevDecodeString( |
| 130 device::UdevDeviceGetPropertyValue(dev, kUdevIdVendorEnc)); |
| 131 // Sometimes it is not encoded. |
| 132 if (vendor.empty()) |
| 133 vendor = |
| 134 UdevDeviceGetPropertyOrSysattr(dev, kUdevIdVendor, kSysattrVendorName); |
| 135 return vendor; |
| 136 } |
| 137 |
127 void SetStringIfNonEmpty(base::DictionaryValue* value, | 138 void SetStringIfNonEmpty(base::DictionaryValue* value, |
128 const std::string& path, | 139 const std::string& path, |
129 const std::string& in_value) { | 140 const std::string& in_value) { |
130 if (!in_value.empty()) | 141 if (!in_value.empty()) |
131 value->SetString(path, in_value); | 142 value->SetString(path, in_value); |
132 } | 143 } |
133 | 144 |
134 } // namespace | 145 } // namespace |
135 | 146 |
136 MidiManagerAlsa::MidiManagerAlsa() | 147 MidiManagerAlsa::MidiManagerAlsa() |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 FROM_HERE, base::Bind(&MidiManagerAlsa::SendMidiData, | 302 FROM_HERE, base::Bind(&MidiManagerAlsa::SendMidiData, |
292 base::Unretained(this), port_index, data), | 303 base::Unretained(this), port_index, data), |
293 delay); | 304 delay); |
294 | 305 |
295 // Acknowledge send. | 306 // Acknowledge send. |
296 send_thread_.message_loop()->PostTask( | 307 send_thread_.message_loop()->PostTask( |
297 FROM_HERE, base::Bind(&MidiManagerClient::AccumulateMidiBytesSent, | 308 FROM_HERE, base::Bind(&MidiManagerClient::AccumulateMidiBytesSent, |
298 base::Unretained(client), data.size())); | 309 base::Unretained(client), data.size())); |
299 } | 310 } |
300 | 311 |
| 312 MidiManagerAlsa::MidiPort::Id::Id() = default; |
| 313 |
| 314 MidiManagerAlsa::MidiPort::Id::Id(const std::string& bus, |
| 315 const std::string& vendor_id, |
| 316 const std::string& model_id, |
| 317 const std::string& usb_interface_num, |
| 318 const std::string& serial) |
| 319 : bus_(bus), |
| 320 vendor_id_(vendor_id), |
| 321 model_id_(model_id), |
| 322 usb_interface_num_(usb_interface_num), |
| 323 serial_(serial) { |
| 324 } |
| 325 |
| 326 MidiManagerAlsa::MidiPort::Id::Id(const Id&) = default; |
| 327 |
| 328 MidiManagerAlsa::MidiPort::Id::~Id() = default; |
| 329 |
| 330 bool MidiManagerAlsa::MidiPort::Id::operator==(const Id& rhs) const { |
| 331 return (bus_ == rhs.bus_) && (vendor_id_ == rhs.vendor_id_) && |
| 332 (model_id_ == rhs.model_id_) && |
| 333 (usb_interface_num_ == rhs.usb_interface_num_) && |
| 334 (serial_ == rhs.serial_); |
| 335 } |
| 336 |
| 337 bool MidiManagerAlsa::MidiPort::Id::empty() const { |
| 338 return bus_.empty() && vendor_id_.empty() && model_id_.empty() && |
| 339 usb_interface_num_.empty() && serial_.empty(); |
| 340 } |
| 341 |
301 MidiManagerAlsa::MidiPort::MidiPort(const std::string& path, | 342 MidiManagerAlsa::MidiPort::MidiPort(const std::string& path, |
302 const std::string& id, | 343 const Id& id, |
303 int client_id, | 344 int client_id, |
304 int port_id, | 345 int port_id, |
305 int midi_device, | 346 int midi_device, |
306 const std::string& client_name, | 347 const std::string& client_name, |
307 const std::string& port_name, | 348 const std::string& port_name, |
308 const std::string& manufacturer, | 349 const std::string& manufacturer, |
309 const std::string& version, | 350 const std::string& version, |
310 Type type) | 351 Type type) |
311 : id_(id), | 352 : id_(id), |
312 midi_device_(midi_device), | 353 midi_device_(midi_device), |
(...skipping 19 matching lines...) Expand all Loading... |
332 switch (type_) { | 373 switch (type_) { |
333 case Type::kInput: | 374 case Type::kInput: |
334 type = "input"; | 375 type = "input"; |
335 break; | 376 break; |
336 case Type::kOutput: | 377 case Type::kOutput: |
337 type = "output"; | 378 type = "output"; |
338 break; | 379 break; |
339 } | 380 } |
340 value->SetString("type", type); | 381 value->SetString("type", type); |
341 SetStringIfNonEmpty(value.get(), "path", path_); | 382 SetStringIfNonEmpty(value.get(), "path", path_); |
342 SetStringIfNonEmpty(value.get(), "id", id_); | |
343 SetStringIfNonEmpty(value.get(), "clientName", client_name_); | 383 SetStringIfNonEmpty(value.get(), "clientName", client_name_); |
344 SetStringIfNonEmpty(value.get(), "portName", port_name_); | 384 SetStringIfNonEmpty(value.get(), "portName", port_name_); |
345 value->SetInteger("clientId", client_id_); | 385 value->SetInteger("clientId", client_id_); |
346 value->SetInteger("portId", port_id_); | 386 value->SetInteger("portId", port_id_); |
347 value->SetInteger("midiDevice", midi_device_); | 387 value->SetInteger("midiDevice", midi_device_); |
348 | 388 |
| 389 // Flatten id fields. |
| 390 SetStringIfNonEmpty(value.get(), "bus", id_.bus()); |
| 391 SetStringIfNonEmpty(value.get(), "vendorId", id_.vendor_id()); |
| 392 SetStringIfNonEmpty(value.get(), "modelId", id_.model_id()); |
| 393 SetStringIfNonEmpty(value.get(), "usbInterfaceNum", id_.usb_interface_num()); |
| 394 SetStringIfNonEmpty(value.get(), "serial", id_.serial()); |
| 395 |
349 return value.Pass(); | 396 return value.Pass(); |
350 } | 397 } |
351 | 398 |
352 std::string MidiManagerAlsa::MidiPort::JSONValue() const { | 399 std::string MidiManagerAlsa::MidiPort::JSONValue() const { |
353 std::string json; | 400 std::string json; |
354 JSONStringValueSerializer serializer(&json); | 401 JSONStringValueSerializer serializer(&json); |
355 serializer.Serialize(*Value().get()); | 402 serializer.Serialize(*Value().get()); |
356 return json; | 403 return json; |
357 } | 404 } |
358 | 405 |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
614 int card_midi_device = -1; | 661 int card_midi_device = -1; |
615 for (const auto& client_pair : clients_) { | 662 for (const auto& client_pair : clients_) { |
616 int client_id = client_pair.first; | 663 int client_id = client_pair.first; |
617 const auto& client = client_pair.second; | 664 const auto& client = client_pair.second; |
618 | 665 |
619 // Get client metadata. | 666 // Get client metadata. |
620 const std::string client_name = client->name(); | 667 const std::string client_name = client->name(); |
621 std::string manufacturer; | 668 std::string manufacturer; |
622 std::string driver; | 669 std::string driver; |
623 std::string path; | 670 std::string path; |
624 std::string id; | 671 MidiPort::Id id; |
625 std::string serial; | |
626 std::string card_name; | 672 std::string card_name; |
627 std::string card_longname; | 673 std::string card_longname; |
628 int midi_device = -1; | 674 int midi_device = -1; |
629 | 675 |
630 if (IsCardClient(client->type(), client_id)) { | 676 if (IsCardClient(client->type(), client_id)) { |
631 auto& card = card_it->second; | 677 auto& card = card_it->second; |
632 if (card_midi_device == -1) | 678 if (card_midi_device == -1) |
633 card_midi_device = 0; | 679 card_midi_device = 0; |
634 | 680 |
635 manufacturer = card->manufacturer(); | 681 manufacturer = card->manufacturer(); |
| 682 path = card->path(); |
| 683 id = MidiPort::Id(card->bus(), card->vendor_id(), card->model_id(), |
| 684 card->usb_interface_num(), card->serial()); |
| 685 card_name = card->name(); |
| 686 card_longname = card->longname(); |
636 midi_device = card_midi_device; | 687 midi_device = card_midi_device; |
637 | 688 |
638 ++card_midi_device; | 689 ++card_midi_device; |
639 if (card_midi_device >= card->midi_device_count()) { | 690 if (card_midi_device >= card->midi_device_count()) { |
640 card_midi_device = -1; | 691 card_midi_device = -1; |
641 ++card_it; | 692 ++card_it; |
642 } | 693 } |
643 } | 694 } |
644 | 695 |
645 for (const auto& port_pair : *client) { | 696 for (const auto& port_pair : *client) { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
708 MidiManagerAlsa::AlsaSeqState::Client::begin() const { | 759 MidiManagerAlsa::AlsaSeqState::Client::begin() const { |
709 return ports_.begin(); | 760 return ports_.begin(); |
710 } | 761 } |
711 | 762 |
712 MidiManagerAlsa::AlsaSeqState::Client::PortMap::const_iterator | 763 MidiManagerAlsa::AlsaSeqState::Client::PortMap::const_iterator |
713 MidiManagerAlsa::AlsaSeqState::Client::end() const { | 764 MidiManagerAlsa::AlsaSeqState::Client::end() const { |
714 return ports_.end(); | 765 return ports_.end(); |
715 } | 766 } |
716 | 767 |
717 MidiManagerAlsa::AlsaCard::AlsaCard(udev_device* dev, | 768 MidiManagerAlsa::AlsaCard::AlsaCard(udev_device* dev, |
718 const std::string& alsa_name, | 769 const std::string& name, |
719 const std::string& alsa_longname, | 770 const std::string& longname, |
720 const std::string& alsa_driver, | 771 const std::string& driver, |
721 int midi_device_count) | 772 int midi_device_count) |
722 : alsa_name_(alsa_name), | 773 : name_(name), |
723 alsa_longname_(alsa_longname), | 774 longname_(longname), |
724 alsa_driver_(alsa_driver), | 775 driver_(driver), |
725 midi_device_count_(midi_device_count) { | 776 path_(device::UdevDeviceGetPropertyValue(dev, kUdevIdPath)), |
726 // Try to get the vendor string. Sometimes it is encoded. | 777 bus_(device::UdevDeviceGetPropertyValue(dev, kUdevIdBus)), |
727 std::string vendor = device::UdevDecodeString( | 778 vendor_id_( |
728 device::UdevDeviceGetPropertyValue(dev, kUdevIdVendorEnc)); | 779 UdevDeviceGetPropertyOrSysattr(dev, kUdevIdVendorId, kSysattrVendor)), |
729 // Sometimes it is not encoded. | 780 model_id_( |
730 if (vendor.empty()) | 781 UdevDeviceGetPropertyOrSysattr(dev, kUdevIdModelId, kSysattrModel)), |
731 vendor = | 782 usb_interface_num_( |
732 UdevDeviceGetPropertyOrSysattr(dev, kUdevIdVendor, kSysattrVendorName); | 783 device::UdevDeviceGetPropertyValue(dev, kUdevIdUsbInterfaceNum)), |
733 // Also get the vendor string from the hardware database. | 784 serial_(UdevDeviceGetPropertyOrSysattr(dev, |
734 std::string vendor_from_database = | 785 kUdevIdSerialShort, |
735 device::UdevDeviceGetPropertyValue(dev, kUdevIdVendorFromDatabase); | 786 kSysattrGuid)), |
736 | 787 midi_device_count_(midi_device_count), |
737 // Get the device path. | 788 manufacturer_(ExtractManufacturerString( |
738 path_ = device::UdevDeviceGetPropertyValue(dev, kUdevIdPath); | 789 GetVendor(dev), |
739 // Get the bus. | 790 vendor_id_, |
740 bus_ = device::UdevDeviceGetPropertyValue(dev, kUdevIdBus); | 791 device::UdevDeviceGetPropertyValue(dev, kUdevIdVendorFromDatabase), |
741 | 792 name, |
742 // Get the "serial" number. (Often untrustable or missing.) | 793 longname)) { |
743 serial_ = | |
744 UdevDeviceGetPropertyOrSysattr(dev, kUdevIdSerialShort, kSysattrGuid); | |
745 | |
746 // Get the vendor id, by either property or sysattr. | |
747 vendor_id_ = | |
748 UdevDeviceGetPropertyOrSysattr(dev, kUdevIdVendorId, kSysattrVendor); | |
749 // Get the model id, by either property or sysattr. | |
750 model_id_ = | |
751 UdevDeviceGetPropertyOrSysattr(dev, kUdevIdModelId, kSysattrModel); | |
752 // Get the usb interface number. | |
753 usb_interface_num_ = | |
754 device::UdevDeviceGetPropertyValue(dev, kUdevIdUsbInterfaceNum); | |
755 manufacturer_ = ExtractManufacturerString( | |
756 vendor, vendor_id_, vendor_from_database, alsa_name, alsa_longname); | |
757 } | 794 } |
758 | 795 |
759 MidiManagerAlsa::AlsaCard::~AlsaCard() = default; | 796 MidiManagerAlsa::AlsaCard::~AlsaCard() = default; |
760 | 797 |
761 // static | 798 // static |
762 std::string MidiManagerAlsa::AlsaCard::ExtractManufacturerString( | 799 std::string MidiManagerAlsa::AlsaCard::ExtractManufacturerString( |
763 const std::string& udev_id_vendor, | 800 const std::string& udev_id_vendor, |
764 const std::string& udev_id_vendor_id, | 801 const std::string& udev_id_vendor_id, |
765 const std::string& udev_id_vendor_from_database, | 802 const std::string& udev_id_vendor_from_database, |
766 const std::string& alsa_name, | 803 const std::string& alsa_name, |
(...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1321 source_map_[AddrToInt(client_id, port_id)] = port_index; | 1358 source_map_[AddrToInt(client_id, port_id)] = port_index; |
1322 return true; | 1359 return true; |
1323 } | 1360 } |
1324 | 1361 |
1325 MidiManager* MidiManager::Create() { | 1362 MidiManager* MidiManager::Create() { |
1326 return new MidiManagerAlsa(); | 1363 return new MidiManagerAlsa(); |
1327 } | 1364 } |
1328 | 1365 |
1329 } // namespace midi | 1366 } // namespace midi |
1330 } // namespace media | 1367 } // namespace media |
OLD | NEW |