summaryrefslogtreecommitdiffstats
path: root/media/midi/midi_manager_usb.h
blob: a08b9860992fb76f83081f7559af3fbd47aaf9a9 (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
// 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.

#ifndef MEDIA_MIDI_MIDI_MANAGER_USB_H_
#define MEDIA_MIDI_MIDI_MANAGER_USB_H_

#include <utility>
#include <vector>

#include "base/basictypes.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/containers/hash_tables.h"
#include "base/memory/scoped_ptr.h"
#include "media/base/media_export.h"
#include "media/midi/midi_manager.h"
#include "media/midi/usb_midi_device.h"
#include "media/midi/usb_midi_input_stream.h"
#include "media/midi/usb_midi_jack.h"
#include "media/midi/usb_midi_output_stream.h"

namespace media {

// MidiManager for USB-MIDI.
class MEDIA_EXPORT MidiManagerUsb : public MidiManager,
                                    public UsbMidiDeviceDelegate,
                                    public UsbMidiInputStream::Delegate {
 public:
  explicit MidiManagerUsb(scoped_ptr<UsbMidiDevice::Factory> device_factory);
  virtual ~MidiManagerUsb();

  // MidiManager implementation.
  virtual bool Initialize() OVERRIDE;
  virtual void DispatchSendMidiData(MidiManagerClient* client,
                                    uint32 port_index,
                                    const std::vector<uint8>& data,
                                    double timestamp) OVERRIDE;

  // UsbMidiDeviceDelegate implementation.
  virtual void ReceiveUsbMidiData(UsbMidiDevice* device,
                                  int endpoint_number,
                                  const uint8* data,
                                  size_t size,
                                  double timestamp) OVERRIDE;

  // UsbMidiInputStream::Delegate implementation.
  virtual void OnReceivedData(size_t jack_index,
                              const uint8* data,
                              size_t size,
                              double timestamp) OVERRIDE;

  const ScopedVector<UsbMidiOutputStream>& output_streams() const {
    return output_streams_;
  }
  const UsbMidiInputStream* input_stream() const { return input_stream_.get(); }

  // Initializes this object.
  // When the initialization finishes, |callback| will be called with the
  // result.
  // When this factory is destroyed during the operation, the operation
  // will be canceled silently (i.e. |callback| will not be called).
  void Initialize(base::Callback<void(bool result)> callback);

 private:
  void OnEnumerateDevicesDone(bool result, UsbMidiDevice::Devices* devices);

  scoped_ptr<UsbMidiDevice::Factory> device_factory_;
  ScopedVector<UsbMidiDevice> devices_;
  ScopedVector<UsbMidiOutputStream> output_streams_;
  scoped_ptr<UsbMidiInputStream> input_stream_;

  base::Callback<void(bool result)> initialize_callback_;

  // A map from <endpoint_number, cable_number> to the index of input jacks.
  base::hash_map<std::pair<int, int>, size_t> input_jack_dictionary_;

  DISALLOW_COPY_AND_ASSIGN(MidiManagerUsb);
};

}  // namespace media

#endif  // MEDIA_MIDI_MIDI_MANAGER_USB_H_