summaryrefslogtreecommitdiffstats
path: root/media/midi/usb_midi_input_stream.cc
blob: 2eaa6e36b478e9142fdaf6ba9b8a00d74b14725c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2014 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 "base/logging.h"
#include "media/midi/usb_midi_device.h"

namespace media {
namespace midi {

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;
}

bool UsbMidiInputStream::JackUniqueKey::operator<(
    const JackUniqueKey& that) const {
  if (device != that.device)
    return device < that.device;
  if (endpoint_number != that.endpoint_number)
    return endpoint_number < that.endpoint_number;
  return cable_number < that.cable_number;
}

UsbMidiInputStream::UsbMidiInputStream(Delegate* delegate)
    : delegate_(delegate) {}

UsbMidiInputStream::~UsbMidiInputStream() {}

void UsbMidiInputStream::Add(const UsbMidiJack& jack) {
  JackUniqueKey key(jack.device,
                    jack.endpoint_number(),
                    jack.cable_number);

  jacks_.push_back(jack);
  DCHECK(jack_dictionary_.end() == jack_dictionary_.find(key));
  jack_dictionary_.insert(std::make_pair(key, jack_dictionary_.size()));
}

void UsbMidiInputStream::OnReceivedData(UsbMidiDevice* device,
                                        int endpoint_number,
                                        const uint8_t* data,
                                        size_t size,
                                        base::TimeTicks time) {
  DCHECK_EQ(0u, size % kPacketSize);
  size_t current = 0;
  while (current + kPacketSize <= size) {
    ProcessOnePacket(device, endpoint_number, &data[current], time);
    current += kPacketSize;
  }
}

void UsbMidiInputStream::ProcessOnePacket(UsbMidiDevice* device,
                                          int endpoint_number,
                                          const uint8_t* packet,
                                          base::TimeTicks time) {
  // The first 4 bytes of the packet is accessible here.
  uint8_t code_index = packet[0] & 0x0f;
  uint8_t 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[code_index];
  if (packet_size == 0) {
    // These CINs are reserved. Ignore them.
    DVLOG(1) << "code index number (" << code_index << ") arrives "
             << "but it is reserved.";
    return;
  }
  std::map<JackUniqueKey, size_t>::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, time);
}

}  // namespace midi
}  // namespace media