midi_manager_win.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2017 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_MIDI_MANAGER_WIN_H_
  5. #define MEDIA_MIDI_MIDI_MANAGER_WIN_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/callback_forward.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/system/system_monitor.h"
  11. #include "media/midi/midi_export.h"
  12. #include "media/midi/midi_manager.h"
  13. namespace base {
  14. class SingleThreadTaskRunner;
  15. class TimeDelta;
  16. } // namespace base
  17. namespace midi {
  18. // New backend for legacy Windows that support dynamic instantiation.
  19. class MidiManagerWin final
  20. : public MidiManager,
  21. public base::SystemMonitor::DevicesChangedObserver {
  22. public:
  23. class PortManager;
  24. MIDI_EXPORT static void OverflowInstanceIdForTesting();
  25. explicit MidiManagerWin(MidiService* service);
  26. MidiManagerWin(const MidiManagerWin&) = delete;
  27. MidiManagerWin& operator=(const MidiManagerWin&) = delete;
  28. ~MidiManagerWin() override;
  29. // Returns PortManager that implements interfaces to help implementation.
  30. // This hides Windows specific structures, i.e. HMIDIIN in the header.
  31. PortManager* port_manager() { return port_manager_.get(); }
  32. // MidiManager overrides:
  33. void StartInitialization() override;
  34. void DispatchSendMidiData(MidiManagerClient* client,
  35. uint32_t port_index,
  36. const std::vector<uint8_t>& data,
  37. base::TimeTicks timestamp) override;
  38. // base::SystemMonitor::DevicesChangedObserver overrides:
  39. void OnDevicesChanged(base::SystemMonitor::DeviceType device_type) override;
  40. private:
  41. class InPort;
  42. class OutPort;
  43. // Handles MIDI inport event posted from a thread system provides.
  44. void ReceiveMidiData(uint32_t index,
  45. const std::vector<uint8_t>& data,
  46. base::TimeTicks time);
  47. // Posts a task to TaskRunner, and ensures that the instance keeps alive while
  48. // the task is running.
  49. void PostTask(base::OnceClosure);
  50. void PostDelayedTask(base::OnceClosure, base::TimeDelta delay);
  51. // Posts a reply task to the I/O thread that hosts MidiManager instance, runs
  52. // it safely, and ensures that the instance keeps alive while the task is
  53. // running.
  54. void PostReplyTask(base::OnceClosure);
  55. // Initializes instance asynchronously on TaskRunner.
  56. void InitializeOnTaskRunner();
  57. // Updates device lists on TaskRunner.
  58. // Returns true if device lists were changed.
  59. void UpdateDeviceListOnTaskRunner();
  60. // Reflect active port list to a device list.
  61. template <typename T>
  62. void ReflectActiveDeviceList(MidiManagerWin* manager,
  63. std::vector<T>* known_ports,
  64. std::vector<T>* active_ports);
  65. // Sends MIDI data on TaskRunner.
  66. void SendOnTaskRunner(MidiManagerClient* client,
  67. uint32_t port_index,
  68. const std::vector<uint8_t>& data);
  69. // Holds an unique instance ID.
  70. const int64_t instance_id_;
  71. // Keeps a TaskRunner for the I/O thread.
  72. scoped_refptr<base::SingleThreadTaskRunner> thread_runner_;
  73. // Manages platform dependent implementation for port managegent. Should be
  74. // accessed with the task lock.
  75. std::unique_ptr<PortManager> port_manager_;
  76. };
  77. } // namespace midi
  78. #endif // MEDIA_MIDI_MIDI_MANAGER_WIN_H_