Update V8 to version 4.7.42.
[chromium-blink-merge.git] / media / midi / usb_midi_input_stream.cc
blob10f71467302a3b937fcb2dcf49ef7fc1134ac2e7
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
3 // found in the LICENSE file.
5 #include "media/midi/usb_midi_input_stream.h"
7 #include <string.h>
9 #include "base/logging.h"
10 #include "media/midi/usb_midi_device.h"
12 namespace media {
13 namespace midi {
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) {}
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;
29 bool UsbMidiInputStream::JackUniqueKey::operator<(
30 const JackUniqueKey& that) const {
31 if (device != that.device)
32 return device < that.device;
33 if (endpoint_number != that.endpoint_number)
34 return endpoint_number < that.endpoint_number;
35 return cable_number < that.cable_number;
38 UsbMidiInputStream::UsbMidiInputStream(Delegate* delegate)
39 : delegate_(delegate) {}
41 UsbMidiInputStream::~UsbMidiInputStream() {}
43 void UsbMidiInputStream::Add(const UsbMidiJack& jack) {
44 JackUniqueKey key(jack.device,
45 jack.endpoint_number(),
46 jack.cable_number);
48 jacks_.push_back(jack);
49 DCHECK(jack_dictionary_.end() == jack_dictionary_.find(key));
50 jack_dictionary_.insert(std::make_pair(key, jack_dictionary_.size()));
53 void UsbMidiInputStream::OnReceivedData(UsbMidiDevice* device,
54 int endpoint_number,
55 const uint8* data,
56 size_t size,
57 base::TimeTicks time) {
58 DCHECK_EQ(0u, size % kPacketSize);
59 size_t current = 0;
60 while (current + kPacketSize <= size) {
61 ProcessOnePacket(device, endpoint_number, &data[current], time);
62 current += kPacketSize;
66 void UsbMidiInputStream::ProcessOnePacket(UsbMidiDevice* device,
67 int endpoint_number,
68 const uint8* packet,
69 base::TimeTicks time) {
70 // The first 4 bytes of the packet is accessible here.
71 uint8 code_index = packet[0] & 0x0f;
72 uint8 cable_number = packet[0] >> 4;
73 const size_t packet_size_table[16] = {
74 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1,
76 size_t packet_size = packet_size_table[code_index];
77 if (packet_size == 0) {
78 // These CINs are reserved. Ignore them.
79 DVLOG(1) << "code index number (" << code_index << ") arrives "
80 << "but it is reserved.";
81 return;
83 std::map<JackUniqueKey, size_t>::const_iterator it =
84 jack_dictionary_.find(JackUniqueKey(device,
85 endpoint_number,
86 cable_number));
87 if (it != jack_dictionary_.end())
88 delegate_->OnReceivedData(it->second, &packet[1], packet_size, time);
91 } // namespace midi
92 } // namespace media