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_usb.h" | 5 #include "media/midi/midi_manager_usb.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" |
10 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
11 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
12 #include "media/midi/midi_scheduler.h" | 13 #include "media/midi/midi_scheduler.h" |
13 #include "media/midi/usb_midi_descriptor_parser.h" | 14 #include "media/midi/usb_midi_descriptor_parser.h" |
14 | 15 |
15 namespace midi { | 16 namespace midi { |
16 | 17 |
17 using mojom::PortState; | 18 using mojom::PortState; |
18 using mojom::Result; | 19 using mojom::Result; |
19 | 20 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 // |port_index| is provided by a renderer so we can't believe that it is | 61 // |port_index| is provided by a renderer so we can't believe that it is |
61 // in the valid range. | 62 // in the valid range. |
62 return; | 63 return; |
63 } | 64 } |
64 // output_streams_[port_index] is alive unless MidiManagerUsb is deleted. | 65 // output_streams_[port_index] is alive unless MidiManagerUsb is deleted. |
65 // The task posted to the MidiScheduler will be disposed safely on deleting | 66 // The task posted to the MidiScheduler will be disposed safely on deleting |
66 // the scheduler. | 67 // the scheduler. |
67 scheduler_->PostSendDataTask( | 68 scheduler_->PostSendDataTask( |
68 client, data.size(), timestamp, | 69 client, data.size(), timestamp, |
69 base::Bind(&UsbMidiOutputStream::Send, | 70 base::Bind(&UsbMidiOutputStream::Send, |
70 base::Unretained(output_streams_[port_index]), data)); | 71 base::Unretained(output_streams_[port_index].get()), data)); |
71 } | 72 } |
72 | 73 |
73 void MidiManagerUsb::ReceiveUsbMidiData(UsbMidiDevice* device, | 74 void MidiManagerUsb::ReceiveUsbMidiData(UsbMidiDevice* device, |
74 int endpoint_number, | 75 int endpoint_number, |
75 const uint8_t* data, | 76 const uint8_t* data, |
76 size_t size, | 77 size_t size, |
77 base::TimeTicks time) { | 78 base::TimeTicks time) { |
78 if (!input_stream_) | 79 if (!input_stream_) |
79 return; | 80 return; |
80 input_stream_->OnReceivedData(device, | 81 input_stream_->OnReceivedData(device, |
81 endpoint_number, | 82 endpoint_number, |
82 data, | 83 data, |
83 size, | 84 size, |
84 time); | 85 time); |
85 } | 86 } |
86 | 87 |
87 void MidiManagerUsb::OnDeviceAttached(std::unique_ptr<UsbMidiDevice> device) { | 88 void MidiManagerUsb::OnDeviceAttached(std::unique_ptr<UsbMidiDevice> device) { |
88 int device_id = static_cast<int>(devices_.size()); | 89 int device_id = static_cast<int>(devices_.size()); |
89 devices_.push_back(std::move(device)); | 90 devices_.push_back(std::move(device)); |
90 AddPorts(devices_.back(), device_id); | 91 AddPorts(devices_.back().get(), device_id); |
91 } | 92 } |
92 | 93 |
93 void MidiManagerUsb::OnDeviceDetached(size_t index) { | 94 void MidiManagerUsb::OnDeviceDetached(size_t index) { |
94 if (index >= devices_.size()) { | 95 if (index >= devices_.size()) { |
95 return; | 96 return; |
96 } | 97 } |
97 UsbMidiDevice* device = devices_[index]; | 98 UsbMidiDevice* device = devices_[index].get(); |
98 for (size_t i = 0; i < output_streams_.size(); ++i) { | 99 for (size_t i = 0; i < output_streams_.size(); ++i) { |
99 if (output_streams_[i]->jack().device == device) { | 100 if (output_streams_[i]->jack().device == device) { |
100 SetOutputPortState(static_cast<uint32_t>(i), PortState::DISCONNECTED); | 101 SetOutputPortState(static_cast<uint32_t>(i), PortState::DISCONNECTED); |
101 } | 102 } |
102 } | 103 } |
103 const std::vector<UsbMidiJack>& input_jacks = input_stream_->jacks(); | 104 const std::vector<UsbMidiJack>& input_jacks = input_stream_->jacks(); |
104 for (size_t i = 0; i < input_jacks.size(); ++i) { | 105 for (size_t i = 0; i < input_jacks.size(); ++i) { |
105 if (input_jacks[i].device == device) { | 106 if (input_jacks[i].device == device) { |
106 SetInputPortState(static_cast<uint32_t>(i), PortState::DISCONNECTED); | 107 SetInputPortState(static_cast<uint32_t>(i), PortState::DISCONNECTED); |
107 } | 108 } |
(...skipping 10 matching lines...) Expand all Loading... |
118 | 119 |
119 void MidiManagerUsb::OnEnumerateDevicesDone(bool result, | 120 void MidiManagerUsb::OnEnumerateDevicesDone(bool result, |
120 UsbMidiDevice::Devices* devices) { | 121 UsbMidiDevice::Devices* devices) { |
121 if (!result) { | 122 if (!result) { |
122 initialize_callback_.Run(Result::INITIALIZATION_ERROR); | 123 initialize_callback_.Run(Result::INITIALIZATION_ERROR); |
123 return; | 124 return; |
124 } | 125 } |
125 input_stream_.reset(new UsbMidiInputStream(this)); | 126 input_stream_.reset(new UsbMidiInputStream(this)); |
126 devices->swap(devices_); | 127 devices->swap(devices_); |
127 for (size_t i = 0; i < devices_.size(); ++i) { | 128 for (size_t i = 0; i < devices_.size(); ++i) { |
128 if (!AddPorts(devices_[i], static_cast<int>(i))) { | 129 if (!AddPorts(devices_[i].get(), static_cast<int>(i))) { |
129 initialize_callback_.Run(Result::INITIALIZATION_ERROR); | 130 initialize_callback_.Run(Result::INITIALIZATION_ERROR); |
130 return; | 131 return; |
131 } | 132 } |
132 } | 133 } |
133 initialize_callback_.Run(Result::OK); | 134 initialize_callback_.Run(Result::OK); |
134 } | 135 } |
135 | 136 |
136 bool MidiManagerUsb::AddPorts(UsbMidiDevice* device, int device_id) { | 137 bool MidiManagerUsb::AddPorts(UsbMidiDevice* device, int device_id) { |
137 UsbMidiDescriptorParser parser; | 138 UsbMidiDescriptorParser parser; |
138 std::vector<uint8_t> descriptor = device->GetDescriptors(); | 139 std::vector<uint8_t> descriptor = device->GetDescriptors(); |
(...skipping 10 matching lines...) Expand all Loading... |
149 std::string product_name(device->GetProductName()); | 150 std::string product_name(device->GetProductName()); |
150 std::string version(device->GetDeviceVersion()); | 151 std::string version(device->GetDeviceVersion()); |
151 | 152 |
152 for (size_t j = 0; j < jacks.size(); ++j) { | 153 for (size_t j = 0; j < jacks.size(); ++j) { |
153 // Port ID must be unique in a MIDI manager. This ID setting is | 154 // Port ID must be unique in a MIDI manager. This ID setting is |
154 // sufficiently unique although there is no user-friendly meaning. | 155 // sufficiently unique although there is no user-friendly meaning. |
155 // TODO(yhirano): Use a hashed string as ID. | 156 // TODO(yhirano): Use a hashed string as ID. |
156 std::string id( | 157 std::string id( |
157 base::StringPrintf("usb:port-%d-%ld", device_id, static_cast<long>(j))); | 158 base::StringPrintf("usb:port-%d-%ld", device_id, static_cast<long>(j))); |
158 if (jacks[j].direction() == UsbMidiJack::DIRECTION_OUT) { | 159 if (jacks[j].direction() == UsbMidiJack::DIRECTION_OUT) { |
159 output_streams_.push_back(new UsbMidiOutputStream(jacks[j])); | 160 output_streams_.push_back( |
| 161 base::MakeUnique<UsbMidiOutputStream>(jacks[j])); |
160 AddOutputPort(MidiPortInfo(id, manufacturer, product_name, version, | 162 AddOutputPort(MidiPortInfo(id, manufacturer, product_name, version, |
161 PortState::OPENED)); | 163 PortState::OPENED)); |
162 } else { | 164 } else { |
163 DCHECK_EQ(jacks[j].direction(), UsbMidiJack::DIRECTION_IN); | 165 DCHECK_EQ(jacks[j].direction(), UsbMidiJack::DIRECTION_IN); |
164 input_stream_->Add(jacks[j]); | 166 input_stream_->Add(jacks[j]); |
165 AddInputPort(MidiPortInfo(id, manufacturer, product_name, version, | 167 AddInputPort(MidiPortInfo(id, manufacturer, product_name, version, |
166 PortState::OPENED)); | 168 PortState::OPENED)); |
167 } | 169 } |
168 } | 170 } |
169 return true; | 171 return true; |
170 } | 172 } |
171 | 173 |
172 } // namespace midi | 174 } // namespace midi |
OLD | NEW |