Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(220)

Side by Side Diff: media/midi/midi_manager.h

Issue 261263002: Web MIDI: add an unit test to check MidiManager instantiation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: comment update Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | media/midi/midi_manager.cc » ('j') | media/midi/midi_manager_unittest.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MEDIA_MIDI_MIDI_MANAGER_H_ 5 #ifndef MEDIA_MIDI_MIDI_MANAGER_H_
6 #define MEDIA_MIDI_MIDI_MANAGER_H_ 6 #define MEDIA_MIDI_MIDI_MANAGER_H_
7 7
8 #include <map> 8 #include <map>
9 #include <set> 9 #include <set>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
13 #include "base/synchronization/lock.h" 14 #include "base/synchronization/lock.h"
14 #include "base/time/time.h" 15 #include "base/time/time.h"
15 #include "media/base/media_export.h" 16 #include "media/base/media_export.h"
16 #include "media/midi/midi_port_info.h" 17 #include "media/midi/midi_port_info.h"
17 #include "media/midi/midi_result.h" 18 #include "media/midi/midi_result.h"
18 19
20 namespace base {
21 class SingleThreadTaskRunner;
22 } // namespace base
23
19 namespace media { 24 namespace media {
20 25
21 // A MidiManagerClient registers with the MidiManager to receive MIDI data. 26 // A MidiManagerClient registers with the MidiManager to receive MIDI data.
22 // See MidiManager::RequestAccess() and MidiManager::ReleaseAccess() 27 // See MidiManager::RequestAccess() and MidiManager::ReleaseAccess()
23 // for details. 28 // for details.
24 class MEDIA_EXPORT MidiManagerClient { 29 class MEDIA_EXPORT MidiManagerClient {
25 public: 30 public:
26 virtual ~MidiManagerClient() {} 31 virtual ~MidiManagerClient() {}
27 32
28 // CompleteStartSession() is called when platform dependent preparation is 33 // CompleteStartSession() is called when platform dependent preparation is
(...skipping 14 matching lines...) Expand all
43 // AccumulateMidiBytesSent() is called to acknowledge when bytes have 48 // AccumulateMidiBytesSent() is called to acknowledge when bytes have
44 // successfully been sent to the hardware. 49 // successfully been sent to the hardware.
45 // This happens as a result of the client having previously called 50 // This happens as a result of the client having previously called
46 // MidiManager::DispatchSendMidiData(). 51 // MidiManager::DispatchSendMidiData().
47 virtual void AccumulateMidiBytesSent(size_t n) = 0; 52 virtual void AccumulateMidiBytesSent(size_t n) = 0;
48 }; 53 };
49 54
50 // Manages access to all MIDI hardware. 55 // Manages access to all MIDI hardware.
51 class MEDIA_EXPORT MidiManager { 56 class MEDIA_EXPORT MidiManager {
52 public: 57 public:
58 // The constructor and the destructor will be called on the CrBrowserMain
59 // thread.
53 static MidiManager* Create(); 60 static MidiManager* Create();
54 61
55 MidiManager(); 62 MidiManager();
56 virtual ~MidiManager(); 63 virtual ~MidiManager();
57 64
58 // A client calls StartSession() to receive and send MIDI data. 65 // A client calls StartSession() to receive and send MIDI data.
59 // If the session is ready to start, the MIDI system is lazily initialized 66 // If the session is ready to start, the MIDI system is lazily initialized
60 // and the client is registered to receive MIDI data. 67 // and the client is registered to receive MIDI data.
61 // CompleteStartSession() is called with MIDI_OK if the session is started. 68 // CompleteStartSession() is called with MIDI_OK if the session is started.
62 // Otherwise CompleteStartSession() is called with proper MidiResult code. 69 // Otherwise CompleteStartSession() is called with proper MidiResult code.
70 // StartSession() and EndSession() can be called on the Chrome_IOThread.
71 // CompleteStartSession() will be invoked on the same Chrome_IOThread.
63 void StartSession(MidiManagerClient* client, int client_id); 72 void StartSession(MidiManagerClient* client, int client_id);
64 73
65 // A client calls EndSession() to stop receiving MIDI data. 74 // A client calls EndSession() to stop receiving MIDI data.
66 void EndSession(MidiManagerClient* client); 75 void EndSession(MidiManagerClient* client);
67 76
68 // DispatchSendMidiData() is called when MIDI data should be sent to the MIDI 77 // DispatchSendMidiData() is called when MIDI data should be sent to the MIDI
69 // system. 78 // system.
70 // This method is supposed to return immediately and should not block. 79 // This method is supposed to return immediately and should not block.
71 // |port_index| represents the specific output port from output_ports(). 80 // |port_index| represents the specific output port from output_ports().
72 // |data| represents a series of bytes encoding one or more MIDI messages. 81 // |data| represents a series of bytes encoding one or more MIDI messages.
(...skipping 12 matching lines...) Expand all
85 const MidiPortInfoList& input_ports() { return input_ports_; } 94 const MidiPortInfoList& input_ports() { return input_ports_; }
86 95
87 // output_ports() is a list of MIDI ports for sending MIDI data. 96 // output_ports() is a list of MIDI ports for sending MIDI data.
88 // Each individual port in this list can be identified by its 97 // Each individual port in this list can be identified by its
89 // integer index into this list. 98 // integer index into this list.
90 const MidiPortInfoList& output_ports() { return output_ports_; } 99 const MidiPortInfoList& output_ports() { return output_ports_; }
91 100
92 protected: 101 protected:
93 friend class MidiManagerUsb; 102 friend class MidiManagerUsb;
94 103
95 // Initializes the platform dependent MIDI system. It will call 104 // Initializes the platform dependent MIDI system. It is called on
96 // CompleteInitialization() asynchronously when initialization is finished. 105 // Chrome_IOThread, but will invoke CompleteInitialization() asynchronously
106 // on the same thread or another thread when initialization is finished.
97 // |result| of CompleteInitialization() will be MIDI_OK on success. 107 // |result| of CompleteInitialization() will be MIDI_OK on success.
98 // MidiManager has a default implementation that calls 108 // MidiManager has a default implementation that synchronously calls
99 // CompleteInitialization() with MIDI_NOT_SUPPORTED. 109 // CompleteInitialization() with MIDI_NOT_SUPPORTED on the caller thread.
100 virtual void StartInitialization(); 110 virtual void StartInitialization();
yukawa 2014/05/05 04:07:36 It would be nice if the comment here is written pa
Takashi Toyoshima 2014/05/06 01:58:18 Done.
101 111
102 // Called from a platform dependent implementation of StartInitialization(). 112 // Called from a platform dependent implementation of StartInitialization().
103 // It will distribute |result| to MIDIManagerClient objects in 113 // It invokes CompleteInitializationInternal() on the thread that calls
114 // StartSession() and distributes |result| to MIDIManagerClient objects in
104 // |pending_clients_|. 115 // |pending_clients_|.
105 void CompleteInitialization(MidiResult result); 116 void CompleteInitialization(MidiResult result);
106 117
107 void AddInputPort(const MidiPortInfo& info); 118 void AddInputPort(const MidiPortInfo& info);
108 void AddOutputPort(const MidiPortInfo& info); 119 void AddOutputPort(const MidiPortInfo& info);
109 120
110 // Dispatches to all clients. 121 // Dispatches to all clients.
111 // TODO(toyoshim): Fix the mac implementation to use 122 // TODO(toyoshim): Fix the mac implementation to use
112 // |ReceiveMidiData(..., base::TimeTicks)|. 123 // |ReceiveMidiData(..., base::TimeTicks)|.
113 void ReceiveMidiData(uint32 port_index, 124 void ReceiveMidiData(uint32 port_index,
114 const uint8* data, 125 const uint8* data,
115 size_t length, 126 size_t length,
116 double timestamp); 127 double timestamp);
117 128
118 void ReceiveMidiData(uint32 port_index, 129 void ReceiveMidiData(uint32 port_index,
119 const uint8* data, 130 const uint8* data,
120 size_t length, 131 size_t length,
121 base::TimeTicks time) { 132 base::TimeTicks time) {
122 ReceiveMidiData(port_index, data, length, 133 ReceiveMidiData(port_index, data, length,
123 (time - base::TimeTicks()).InSecondsF()); 134 (time - base::TimeTicks()).InSecondsF());
124 } 135 }
125 136
126 bool initialized_; 137 // Following |clients_| and |pending_clients_| are protected only for testing.
127 MidiResult result_; 138 // Do not access to them for other purposes.
yukawa 2014/05/05 04:07:36 Thank you for the comment. Another possible option
Takashi Toyoshima 2014/05/06 01:58:18 Currently, we adopt anonymous namespace for all un
128
129 // Keeps track of all clients who wish to receive MIDI data. 139 // Keeps track of all clients who wish to receive MIDI data.
130 typedef std::set<MidiManagerClient*> ClientList; 140 typedef std::set<MidiManagerClient*> ClientList;
131 ClientList clients_; 141 ClientList clients_;
132 142
133 // Keeps track of all clients who are waiting for CompleteStartSession(). 143 // Keeps track of all clients who are waiting for CompleteStartSession().
134 typedef std::map<int, MidiManagerClient*> PendingClientMap; 144 typedef std::map<int, MidiManagerClient*> PendingClientMap;
135 PendingClientMap pending_clients_; 145 PendingClientMap pending_clients_;
136 146
137 // Protects access to our clients, |clients_| and |pending_clients_|. 147 private:
138 base::Lock clients_lock_; 148 void CompleteInitializationInternal(MidiResult result);
149
150 // Keeps a SingleThreadTaskRunner of the thread that calls StartSession in
151 // order to invoke CompleteStartSession() on the thread.
152 scoped_refptr<base::SingleThreadTaskRunner> session_thread_runner_;
153
154 // Keeps true if platform dependent initialization is already completed.
155 bool initialized_;
156
157 // Keeps the platform dependent initialization result if initialization is
158 // completed. Otherwise keeps MIDI_NOT_SUPPORTED.
159 MidiResult result_;
160
161 // Protects access to |clients_|, |pending_clients_|, |initialized_|, and
162 // |result_|.
Takashi Toyoshima 2014/05/05 00:49:02 Sorry, this comment was incomplete.
163 base::Lock lock_;
139 164
140 MidiPortInfoList input_ports_; 165 MidiPortInfoList input_ports_;
141 MidiPortInfoList output_ports_; 166 MidiPortInfoList output_ports_;
142 167
143 private:
144 DISALLOW_COPY_AND_ASSIGN(MidiManager); 168 DISALLOW_COPY_AND_ASSIGN(MidiManager);
145 }; 169 };
146 170
147 } // namespace media 171 } // namespace media
148 172
149 #endif // MEDIA_MIDI_MIDI_MANAGER_H_ 173 #endif // MEDIA_MIDI_MIDI_MANAGER_H_
OLDNEW
« no previous file with comments | « no previous file | media/midi/midi_manager.cc » ('j') | media/midi/midi_manager_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698