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

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, 12 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 (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
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 return std::hash<void*>()(key.device) * 17 +
33 std::hash<int>()(key.endpoint_number) * 19 +
34 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.
35 }
36
37 UsbMidiInputStream::UsbMidiInputStream(const std::vector<UsbMidiJack>& jacks,
38 Delegate* delegate)
39 : jacks_(jacks), delegate_(delegate), pending_size_(0) {
40 for (size_t i = 0; i < jacks.size(); ++i) {
41 jack_dictionary_.insert(
42 std::make_pair(JackUniqueKey(jacks[i].device,
43 jacks[i].GetEndpointNumber(),
44 jacks[i].cable_number),
45 i));
46 }
47 }
48
49 UsbMidiInputStream::~UsbMidiInputStream() {}
50
51 void UsbMidiInputStream::OnReceivedData(UsbMidiDevice* device,
52 int endpoint_number,
53 const uint8* data,
54 size_t size,
55 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.
56 size_t current = 0;
57 if (pending_size_ > 0) {
58 if (pending_size_ + size < kPacketSize) {
59 for (size_t i = 0; i < size; ++i)
60 pending_data_[i + pending_size_] = data[i];
61 pending_size_ += size;
62 return;
63 }
64 for (current = 0; current < kPacketSize - pending_size_; ++current)
65 pending_data_[current + pending_size_] = data[current];
66 ProcessOnePacket(device, endpoint_number, pending_data_, timestamp);
67 pending_size_ = 0;
68 }
69 while (current + kPacketSize <= size) {
70 ProcessOnePacket(device, endpoint_number, &data[current], timestamp);
71 current += kPacketSize;
72 }
73 pending_size_ = size - current;
74 DCHECK_LT(pending_size_, arraysize(pending_data_));
75 memcpy(pending_data_, &data[current], pending_size_);
76 }
77
78 void UsbMidiInputStream::ProcessOnePacket(UsbMidiDevice* device,
79 int endpoint_number,
80 const uint8* packet,
81 double timestamp) {
82 // 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
83 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.
84 uint8 cable_number = packet[0] >> 4;
85 const size_t packet_size_table[16] = {
86 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1,
87 };
88 size_t packet_size = packet_size_table[cin];
89 if (packet_size == 0) {
90 // These CINs are reserved. Ignore them.
91 return;
92 }
93 base::hash_map<JackUniqueKey, size_t, JackHash>::const_iterator it =
94 jack_dictionary_.find(JackUniqueKey(device,
95 endpoint_number,
96 cable_number));
97 if (it != jack_dictionary_.end())
98 delegate_->OnReceivedData(it->second, &packet[1], packet_size, timestamp);
99 }
100
101 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698