Index: media/midi/usb_midi_input_stream.cc |
diff --git a/media/midi/usb_midi_input_stream.cc b/media/midi/usb_midi_input_stream.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4d418c492014003a689cf4cc92f813d2f9d7a3c7 |
--- /dev/null |
+++ b/media/midi/usb_midi_input_stream.cc |
@@ -0,0 +1,101 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "media/midi/usb_midi_input_stream.h" |
+ |
+#include <string.h> |
+#include <vector> |
+ |
+#include "base/logging.h" |
+#include "media/midi/usb_midi_device.h" |
+#include "media/midi/usb_midi_jack.h" |
+ |
+namespace media { |
+ |
+UsbMidiInputStream::JackUniqueKey::JackUniqueKey(UsbMidiDevice* device, |
+ int endpoint_number, |
+ int cable_number) |
+ : device(device), |
+ endpoint_number(endpoint_number), |
+ cable_number(cable_number) {} |
+ |
+bool UsbMidiInputStream::JackUniqueKey::operator==( |
+ const JackUniqueKey& that) const { |
+ return device == that.device && |
+ endpoint_number == that.endpoint_number && |
+ cable_number == that.cable_number; |
+} |
+ |
+size_t UsbMidiInputStream::JackHash::operator() |
+ (const JackUniqueKey& key) const { |
+ return std::hash<void*>()(key.device) * 17 + |
+ std::hash<int>()(key.endpoint_number) * 19 + |
+ std::hash<int>()(key.cable_number) * 31; |
Takashi Toyoshima
2013/12/24 07:39:41
Can you add some comments on these magic numbers,
yhirano
2014/01/14 10:52:49
Done.
|
+} |
+ |
+UsbMidiInputStream::UsbMidiInputStream(const std::vector<UsbMidiJack>& jacks, |
+ Delegate* delegate) |
+ : jacks_(jacks), delegate_(delegate), pending_size_(0) { |
+ for (size_t i = 0; i < jacks.size(); ++i) { |
+ jack_dictionary_.insert( |
+ std::make_pair(JackUniqueKey(jacks[i].device, |
+ jacks[i].GetEndpointNumber(), |
+ jacks[i].cable_number), |
+ i)); |
+ } |
+} |
+ |
+UsbMidiInputStream::~UsbMidiInputStream() {} |
+ |
+void UsbMidiInputStream::OnReceivedData(UsbMidiDevice* device, |
+ int endpoint_number, |
+ const uint8* data, |
+ size_t size, |
+ double timestamp) { |
Takashi Toyoshima
2013/12/24 07:39:41
The 32bit packet is defined as a USB transport lay
yhirano
2014/01/14 10:52:49
Done.
|
+ size_t current = 0; |
+ if (pending_size_ > 0) { |
+ if (pending_size_ + size < kPacketSize) { |
+ for (size_t i = 0; i < size; ++i) |
+ pending_data_[i + pending_size_] = data[i]; |
+ pending_size_ += size; |
+ return; |
+ } |
+ for (current = 0; current < kPacketSize - pending_size_; ++current) |
+ pending_data_[current + pending_size_] = data[current]; |
+ ProcessOnePacket(device, endpoint_number, pending_data_, timestamp); |
+ pending_size_ = 0; |
+ } |
+ while (current + kPacketSize <= size) { |
+ ProcessOnePacket(device, endpoint_number, &data[current], timestamp); |
+ current += kPacketSize; |
+ } |
+ pending_size_ = size - current; |
+ DCHECK_LT(pending_size_, arraysize(pending_data_)); |
+ memcpy(pending_data_, &data[current], pending_size_); |
+} |
+ |
+void UsbMidiInputStream::ProcessOnePacket(UsbMidiDevice* device, |
+ int endpoint_number, |
+ const uint8* packet, |
+ double timestamp) { |
+ // packet[0:4] is accessible here. |
Takashi Toyoshima
2013/12/24 07:39:41
[0:3]
yhirano
2014/01/14 10:52:49
I used the python notation, but it seems not clear
|
+ uint8 cin = packet[0] & 0x0f; |
Takashi Toyoshima
2013/12/24 07:39:41
code_index is better here since it is consistent w
yhirano
2014/01/14 10:52:49
Done.
|
+ uint8 cable_number = packet[0] >> 4; |
+ const size_t packet_size_table[16] = { |
+ 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1, |
+ }; |
+ size_t packet_size = packet_size_table[cin]; |
+ if (packet_size == 0) { |
+ // These CINs are reserved. Ignore them. |
+ return; |
+ } |
+ base::hash_map<JackUniqueKey, size_t, JackHash>::const_iterator it = |
+ jack_dictionary_.find(JackUniqueKey(device, |
+ endpoint_number, |
+ cable_number)); |
+ if (it != jack_dictionary_.end()) |
+ delegate_->OnReceivedData(it->second, &packet[1], packet_size, timestamp); |
+} |
+ |
+} // namespace media |