2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "modules/webmidi/MIDIAccess.h"
34 #include "core/dom/Document.h"
35 #include "core/loader/DocumentLoadTiming.h"
36 #include "core/loader/DocumentLoader.h"
37 #include "modules/webmidi/MIDIAccessInitializer.h"
38 #include "modules/webmidi/MIDIConnectionEvent.h"
39 #include "modules/webmidi/MIDIController.h"
40 #include "modules/webmidi/MIDIInput.h"
41 #include "modules/webmidi/MIDIInputMap.h"
42 #include "modules/webmidi/MIDIOutput.h"
43 #include "modules/webmidi/MIDIOutputMap.h"
44 #include "modules/webmidi/MIDIPort.h"
45 #include "platform/AsyncMethodRunner.h"
50 MIDIAccess::MIDIAccess(PassOwnPtr<MIDIAccessor> accessor, bool sysexEnabled, const Vector<MIDIAccessInitializer::PortDescriptor>& ports, ExecutionContext* executionContext)
51 : ActiveDOMObject(executionContext)
52 , m_accessor(accessor)
53 , m_sysexEnabled(sysexEnabled)
55 m_accessor->setClient(this);
56 for (size_t i = 0; i < ports.size(); ++i) {
57 const MIDIAccessInitializer::PortDescriptor& port = ports[i];
58 if (port.type == MIDIPort::MIDIPortTypeInput) {
59 m_inputs.append(MIDIInput::create(this, port.id, port.manufacturer, port.name, port.version, port.isActive));
61 m_outputs.append(MIDIOutput::create(this, m_outputs.size(), port.id, port.manufacturer, port.name, port.version, port.isActive));
66 MIDIAccess::~MIDIAccess()
70 MIDIInputMap* MIDIAccess::inputs() const
72 HeapHashMap<String, Member<MIDIInput> > inputs;
73 size_t inactiveCount = 0;
74 for (size_t i = 0; i < m_inputs.size(); ++i) {
75 MIDIInput* input = m_inputs[i];
76 if (input->isActive())
77 inputs.add(input->id(), input);
81 if ((inputs.size() + inactiveCount) != m_inputs.size()) {
82 // There is id duplication that violates the spec.
85 return new MIDIInputMap(inputs);
88 MIDIOutputMap* MIDIAccess::outputs() const
90 HeapHashMap<String, Member<MIDIOutput> > outputs;
91 size_t inactiveCount = 0;
92 for (size_t i = 0; i < m_outputs.size(); ++i) {
93 MIDIOutput* output = m_outputs[i];
94 if (output->isActive())
95 outputs.add(output->id(), output);
99 if ((outputs.size() + inactiveCount) != m_outputs.size()) {
100 // There is id duplication that violates the spec.
103 return new MIDIOutputMap(outputs);
106 void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, const String& name, const String& version, bool isActive)
108 ASSERT(isMainThread());
109 m_inputs.append(MIDIInput::create(this, id, manufacturer, name, version, isActive));
112 void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version, bool isActive)
114 ASSERT(isMainThread());
115 unsigned portIndex = m_outputs.size();
116 m_outputs.append(MIDIOutput::create(this, portIndex, id, manufacturer, name, version, isActive));
119 void MIDIAccess::didSetInputPortState(unsigned portIndex, bool isActive)
121 ASSERT(isMainThread());
122 if (portIndex < m_inputs.size())
123 m_inputs[portIndex]->setActiveState(isActive);
126 void MIDIAccess::didSetOutputPortState(unsigned portIndex, bool isActive)
128 ASSERT(isMainThread());
129 if (portIndex < m_outputs.size())
130 m_outputs[portIndex]->setActiveState(isActive);
133 void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStamp)
135 ASSERT(isMainThread());
136 if (portIndex >= m_inputs.size())
139 // Convert from time in seconds which is based on the time coordinate system of monotonicallyIncreasingTime()
140 // into time in milliseconds (a DOMHighResTimeStamp) according to the same time coordinate system as performance.now().
141 // This is how timestamps are defined in the Web MIDI spec.
142 Document* document = toDocument(executionContext());
145 double timeStampInMilliseconds = 1000 * document->loader()->timing()->monotonicTimeToZeroBasedDocumentTime(timeStamp);
147 m_inputs[portIndex]->didReceiveMIDIData(portIndex, data, length, timeStampInMilliseconds);
150 void MIDIAccess::sendMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStampInMilliseconds)
152 if (!data || !length || portIndex >= m_outputs.size())
154 // Convert from a time in milliseconds (a DOMHighResTimeStamp) according to the same time coordinate system as performance.now()
155 // into a time in seconds which is based on the time coordinate system of monotonicallyIncreasingTime().
158 if (!timeStampInMilliseconds) {
159 // We treat a value of 0 (which is the default value) as special, meaning "now".
160 // We need to translate it exactly to 0 seconds.
163 Document* document = toDocument(executionContext());
165 double documentStartTime = document->loader()->timing()->referenceMonotonicTime();
166 timeStamp = documentStartTime + 0.001 * timeStampInMilliseconds;
169 m_accessor->sendMIDIData(portIndex, data, length, timeStamp);
172 void MIDIAccess::stop()
177 void MIDIAccess::trace(Visitor* visitor)
179 visitor->trace(m_inputs);
180 visitor->trace(m_outputs);
181 EventTargetWithInlineData::trace(visitor);