Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/midi/usb_midi_input_stream.h" | |
| 6 | |
|
Takashi Toyoshima
2013/12/24 04:19:32
string.h for memcpy ?
yhirano
2013/12/24 05:57:01
Done.
| |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "media/midi/usb_midi_device.h" | |
| 11 #include "media/midi/usb_midi_jack.h" | |
| 12 | |
| 13 namespace media { | |
| 14 | |
| 15 UsbMidiInputStream::JackUniqueKey::JackUniqueKey(UsbMidiDevice* device, | |
| 16 int endpoint_number, | |
| 17 int cable_number) | |
| 18 : device(device), | |
| 19 endpoint_number(endpoint_number), | |
| 20 cable_number(cable_number) {} | |
| 21 | |
| 22 bool UsbMidiInputStream::JackUniqueKey::operator==( | |
| 23 const JackUniqueKey& that) const { | |
| 24 return device == that.device && | |
| 25 endpoint_number == that.endpoint_number && | |
| 26 cable_number == that.cable_number; | |
| 27 } | |
| 28 | |
| 29 size_t UsbMidiInputStream::JackHash::operator() | |
| 30 (const JackUniqueKey& key) const { | |
| 31 return std::hash<UsbMidiDevice*>()(key.device) * 17 + | |
| 32 std::hash<int>()(key.endpoint_number) * 19 + | |
| 33 std::hash<int>()(key.cable_number) * 31; | |
| 34 } | |
| 35 | |
| 36 UsbMidiInputStream::UsbMidiInputStream(const std::vector<UsbMidiJack>& jacks, | |
| 37 Delegate* delegate) | |
| 38 : jacks_(jacks), delegate_(delegate), pending_size_(0) { | |
| 39 for (size_t i = 0; i < jacks.size(); ++i) { | |
| 40 jack_dictionary_.insert( | |
| 41 std::make_pair(JackUniqueKey(jacks[i].device, | |
| 42 jacks[i].GetEndpointNumber(), | |
| 43 jacks[i].cable_number), | |
| 44 i)); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 UsbMidiInputStream::~UsbMidiInputStream() {} | |
| 49 | |
| 50 void UsbMidiInputStream::OnReceivedData(UsbMidiDevice* device, | |
| 51 int endpoint_number, | |
| 52 const uint8* data, | |
| 53 size_t size, | |
| 54 double timestamp) { | |
| 55 size_t current = 0; | |
| 56 if (pending_size_ > 0) { | |
| 57 if (pending_size_ + size < kPacketSize) { | |
| 58 for (size_t i = 0; i < size; ++i) | |
| 59 pending_data_[i + pending_size_] = data[i]; | |
| 60 pending_size_ += size; | |
| 61 return; | |
| 62 } | |
| 63 for (current = 0; current < kPacketSize - pending_size_; ++current) | |
| 64 pending_data_[current + pending_size_] = data[current]; | |
| 65 ProcessOnePacket(device, endpoint_number, pending_data_, timestamp); | |
| 66 pending_size_ = 0; | |
| 67 } | |
| 68 while (current + kPacketSize <= size) { | |
| 69 ProcessOnePacket(device, endpoint_number, &data[current], timestamp); | |
| 70 current += kPacketSize; | |
| 71 } | |
| 72 pending_size_ = size - current; | |
| 73 DCHECK_LT(pending_size_, arraysize(pending_data_)); | |
| 74 memcpy(pending_data_, &data[current], pending_size_); | |
| 75 } | |
| 76 | |
| 77 void UsbMidiInputStream::ProcessOnePacket(UsbMidiDevice* device, | |
| 78 int endpoint_number, | |
| 79 const uint8* packet, | |
| 80 double timestamp) { | |
| 81 // packet[0:4] is accessible here. | |
|
Takashi Toyoshima
2013/12/24 04:19:32
It's nice to have some information about USB MIDI
yhirano
2013/12/24 05:57:01
Added comments in headers.
| |
| 82 uint8 cin = packet[0] & 0x0f; | |
| 83 uint8 cable_number = packet[0] >> 4; | |
| 84 const size_t packet_size_table[16] = { | |
| 85 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1, | |
| 86 }; | |
| 87 size_t packet_size = packet_size_table[cin]; | |
| 88 if (packet_size == 0) { | |
| 89 // The CINs are reserved. Ignore them. | |
| 90 return; | |
| 91 } | |
| 92 base::hash_map<JackUniqueKey, size_t, JackHash>::const_iterator it = | |
| 93 jack_dictionary_.find(JackUniqueKey(device, | |
| 94 endpoint_number, | |
| 95 cable_number)); | |
| 96 if (it != jack_dictionary_.end()) | |
| 97 delegate_->OnReceivedData(it->second, &packet[1], packet_size, timestamp); | |
| 98 } | |
| 99 | |
| 100 } // namespace media | |
| OLD | NEW |