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));
61 m_outputs.append(MIDIOutput::create(this, m_outputs.size(), port.id, port.manufacturer, port.name, port.version));
66 MIDIAccess::~MIDIAccess()
70 MIDIInputMap* MIDIAccess::inputs() const
72 HeapHashMap<String, Member<MIDIInput> > inputs;
73 for (size_t i = 0; i < m_inputs.size(); ++i) {
74 MIDIInput* input = m_inputs[i];
75 inputs.add(input->id(), input);
77 if (inputs.size() != m_inputs.size()) {
78 // There is id duplication that violates the spec.
81 return new MIDIInputMap(inputs);
84 MIDIOutputMap* MIDIAccess::outputs() const
86 HeapHashMap<String, Member<MIDIOutput> > outputs;
87 for (size_t i = 0; i < m_outputs.size(); ++i) {
88 MIDIOutput* output = m_outputs[i];
89 outputs.add(output->id(), output);
91 if (outputs.size() != m_outputs.size()) {
92 // There is id duplication that violates the spec.
95 return new MIDIOutputMap(outputs);
98 void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, const String& name, const String& version)
100 ASSERT(isMainThread());
101 m_inputs.append(MIDIInput::create(this, id, manufacturer, name, version));
104 void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version)
106 ASSERT(isMainThread());
107 unsigned portIndex = m_outputs.size();
108 m_outputs.append(MIDIOutput::create(this, portIndex, id, manufacturer, name, version));
111 void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStamp)
113 ASSERT(isMainThread());
114 if (portIndex >= m_inputs.size())
117 // Convert from time in seconds which is based on the time coordinate system of monotonicallyIncreasingTime()
118 // into time in milliseconds (a DOMHighResTimeStamp) according to the same time coordinate system as performance.now().
119 // This is how timestamps are defined in the Web MIDI spec.
120 Document* document = toDocument(executionContext());
123 double timeStampInMilliseconds = 1000 * document->loader()->timing()->monotonicTimeToZeroBasedDocumentTime(timeStamp);
125 m_inputs[portIndex]->didReceiveMIDIData(portIndex, data, length, timeStampInMilliseconds);
128 void MIDIAccess::sendMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStampInMilliseconds)
130 if (!data || !length || portIndex >= m_outputs.size())
132 // Convert from a time in milliseconds (a DOMHighResTimeStamp) according to the same time coordinate system as performance.now()
133 // into a time in seconds which is based on the time coordinate system of monotonicallyIncreasingTime().
136 if (!timeStampInMilliseconds) {
137 // We treat a value of 0 (which is the default value) as special, meaning "now".
138 // We need to translate it exactly to 0 seconds.
141 Document* document = toDocument(executionContext());
143 double documentStartTime = document->loader()->timing()->referenceMonotonicTime();
144 timeStamp = documentStartTime + 0.001 * timeStampInMilliseconds;
147 m_accessor->sendMIDIData(portIndex, data, length, timeStamp);
150 void MIDIAccess::stop()
155 void MIDIAccess::trace(Visitor* visitor)
157 visitor->trace(m_inputs);
158 visitor->trace(m_outputs);
159 EventTargetWithInlineData::trace(visitor);