Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(103)

Side by Side Diff: media/midi/usb_midi_input_stream.cc

Issue 107513012: [WebMIDI] Introduce UsbMidi{Input, Output}Stream. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@usb-midi-parser
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 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
7 #include <string.h>
8 #include <vector>
9
10 #include "base/logging.h"
11 #include "media/midi/usb_midi_device.h"
12 #include "media/midi/usb_midi_jack.h"
13
14 namespace media {
15
16 UsbMidiInputStream::JackUniqueKey::JackUniqueKey(UsbMidiDevice* device,
17 int endpoint_number,
18 int cable_number)
19 : device(device),
20 endpoint_number(endpoint_number),
21 cable_number(cable_number) {}
22
23 bool UsbMidiInputStream::JackUniqueKey::operator==(
24 const JackUniqueKey& that) const {
25 return device == that.device &&
26 endpoint_number == that.endpoint_number &&
27 cable_number == that.cable_number;
28 }
29
30 size_t UsbMidiInputStream::JackHash::operator()
31 (const JackUniqueKey& key) const {
32 // Multiply by some primes and add them each other to mix hash values.
33 return std::hash<void*>()(key.device) * 17 +
Takashi Toyoshima 2014/01/15 12:08:40 Please fix compile errors on using std::hash.
yhirano 2014/01/15 13:25:42 Done.
34 std::hash<int>()(key.endpoint_number) * 19 +
35 std::hash<int>()(key.cable_number) * 31;
36 }
37
38 UsbMidiInputStream::UsbMidiInputStream(const std::vector<UsbMidiJack>& jacks,
39 Delegate* delegate)
40 : jacks_(jacks), delegate_(delegate) {
41 for (size_t i = 0; i < jacks.size(); ++i) {
42 jack_dictionary_.insert(
43 std::make_pair(JackUniqueKey(jacks[i].device,
44 jacks[i].endpoint_number(),
45 jacks[i].cable_number),
46 i));
47 }
48 }
49
50 UsbMidiInputStream::~UsbMidiInputStream() {}
51
52 void UsbMidiInputStream::OnReceivedData(UsbMidiDevice* device,
53 int endpoint_number,
54 const uint8* data,
55 size_t size,
56 double timestamp) {
57 DCHECK_EQ(0u, size % kPacketSize);
58 size_t current = 0;
59 while (current + kPacketSize <= size) {
60 ProcessOnePacket(device, endpoint_number, &data[current], timestamp);
61 current += kPacketSize;
62 }
63 }
64
65 void UsbMidiInputStream::ProcessOnePacket(UsbMidiDevice* device,
66 int endpoint_number,
67 const uint8* packet,
68 double timestamp) {
69 // The first 4 bytes of the packet is accessible here.
70 uint8 code_index = packet[0] & 0x0f;
71 uint8 cable_number = packet[0] >> 4;
72 const size_t packet_size_table[16] = {
73 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1,
74 };
75 size_t packet_size = packet_size_table[code_index];
76 if (packet_size == 0) {
77 // These CINs are reserved. Ignore them.
Takashi Toyoshima 2014/01/15 12:08:40 DVLOG(1) << something_informative ?
yhirano 2014/01/15 13:25:42 Done.
78 return;
79 }
80 base::hash_map<JackUniqueKey, size_t, JackHash>::const_iterator it =
81 jack_dictionary_.find(JackUniqueKey(device,
82 endpoint_number,
83 cable_number));
84 if (it != jack_dictionary_.end())
85 delegate_->OnReceivedData(it->second, &packet[1], packet_size, timestamp);
86 }
87
88 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698