usb_midi_device.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.
  4. #ifndef MEDIA_MIDI_USB_MIDI_DEVICE_H_
  5. #define MEDIA_MIDI_USB_MIDI_DEVICE_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <vector>
  10. #include "base/callback.h"
  11. #include "base/time/time.h"
  12. #include "media/midi/usb_midi_export.h"
  13. namespace midi {
  14. class UsbMidiDevice;
  15. // Delegate class for UsbMidiDevice.
  16. // Each method is called when an corresponding event arrives at the device.
  17. class USB_MIDI_EXPORT UsbMidiDeviceDelegate {
  18. public:
  19. virtual ~UsbMidiDeviceDelegate() {}
  20. // Called when USB-MIDI data arrives at |device|.
  21. virtual void ReceiveUsbMidiData(UsbMidiDevice* device,
  22. int endpoint_number,
  23. const uint8_t* data,
  24. size_t size,
  25. base::TimeTicks time) = 0;
  26. // Called when a USB-MIDI device is attached.
  27. virtual void OnDeviceAttached(std::unique_ptr<UsbMidiDevice> device) = 0;
  28. // Called when a USB-MIDI device is detached.
  29. virtual void OnDeviceDetached(size_t index) = 0;
  30. };
  31. // UsbMidiDevice represents a USB-MIDI device.
  32. // This is an interface class and each platform-dependent implementation class
  33. // will be a derived class.
  34. class USB_MIDI_EXPORT UsbMidiDevice {
  35. public:
  36. typedef std::vector<std::unique_ptr<UsbMidiDevice>> Devices;
  37. // Factory class for USB-MIDI devices.
  38. // Each concrete implementation will find and create devices
  39. // in platform-dependent way.
  40. class Factory {
  41. public:
  42. typedef base::OnceCallback<void(bool result, Devices* devices)> Callback;
  43. virtual ~Factory() {}
  44. // Enumerates devices.
  45. // Devices that have no USB-MIDI interfaces can be omitted.
  46. // When the operation succeeds, |callback| will be called with |true| and
  47. // devices.
  48. // Otherwise |callback| will be called with |false| and empty devices.
  49. // When this factory is destroyed during the operation, the operation
  50. // will be canceled silently (i.e. |callback| will not be called).
  51. // This function can be called at most once per instance.
  52. virtual void EnumerateDevices(UsbMidiDeviceDelegate* delegate,
  53. Callback callback) = 0;
  54. };
  55. virtual ~UsbMidiDevice() {}
  56. // Returns the descriptors of this device.
  57. virtual std::vector<uint8_t> GetDescriptors() = 0;
  58. // Return the name of the manufacturer.
  59. virtual std::string GetManufacturer() = 0;
  60. // Retur the name of the device.
  61. virtual std::string GetProductName() = 0;
  62. // Return the device version.
  63. virtual std::string GetDeviceVersion() = 0;
  64. // Sends |data| to the given USB endpoint of this device.
  65. virtual void Send(int endpoint_number, const std::vector<uint8_t>& data) = 0;
  66. };
  67. } // namespace midi
  68. #endif // MEDIA_MIDI_USB_MIDI_DEVICE_H_