summaryrefslogtreecommitdiff
path: root/chromium/media/midi/midi_manager_winrt.h
blob: a6ac993cbf06644036c88a679c42dadf6837c149 (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
// Copyright 2016 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_WINRT_H_
#define MEDIA_MIDI_MIDI_MANAGER_WINRT_H_

#include <memory>

#include "base/strings/string16.h"
#include "base/threading/thread.h"
#include "media/midi/midi_manager.h"

namespace base {
class ThreadChecker;
}

namespace midi {

class MidiScheduler;
class MidiService;

class MIDI_EXPORT MidiManagerWinrt final : public MidiManager {
 public:
  explicit MidiManagerWinrt(MidiService* service);
  ~MidiManagerWinrt() override;

  // MidiManager overrides:
  void StartInitialization() final;
  void Finalize() final;
  void DispatchSendMidiData(MidiManagerClient* client,
                            uint32_t port_index,
                            const std::vector<uint8_t>& data,
                            double timestamp) final;

 private:
  // Callbacks on the COM thread.
  void InitializeOnComThread();
  void FinalizeOnComThread();
  void SendOnComThread(uint32_t port_index, const std::vector<uint8_t>& data);

  // Callback from MidiPortManager::OnEnumerationComplete on the COM thread.
  // Calls CompleteInitialization() when both MidiPortManagers are ready.
  void OnPortManagerReady();

  // Subclasses that access private/protected members of MidiManager.
  template <typename InterfaceType,
            typename RuntimeType,
            typename StaticsInterfaceType,
            base::char16 const* runtime_class_id>
  class MidiPortManager;
  class MidiInPortManager;
  class MidiOutPortManager;

  // COM-initialized thread for calling WinRT methods.
  base::Thread com_thread_;

  // Lock to ensure all smart pointers initialized in InitializeOnComThread()
  // and destroyed in FinalizeOnComThread() will not be accidentally destructed
  // twice in the destructor.
  base::Lock lazy_init_member_lock_;

  // Should be instantiated on COM thread.
  std::unique_ptr<base::ThreadChecker>
      com_thread_checker_;  // GUARDED_BY(lazy_init_member_lock_)

  // All operations to MidiPortManager should be done on COM thread.
  std::unique_ptr<MidiInPortManager>
      port_manager_in_;  // GUARDED_BY(lazy_init_member_lock_)
  std::unique_ptr<MidiOutPortManager>
      port_manager_out_;  // GUARDED_BY(lazy_init_member_lock_)

  // |scheduler_| is used by DispatchSendMidiData on Chrome_IOThread, should
  // acquire lock before use.
  std::unique_ptr<MidiScheduler>
      scheduler_;  // GUARDED_BY(lazy_init_member_lock_)

  // Incremented when a MidiPortManager is ready.
  uint8_t port_manager_ready_count_ = 0;

  DISALLOW_COPY_AND_ASSIGN(MidiManagerWinrt);
};

}  // namespace midi

#endif  // MEDIA_MIDI_MIDI_MANAGER_WINRT_H_