1/* 2 * Copyright (C) 2013 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 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 13 * distribution. 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. 17 * 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. 29 */ 30 31#include "config.h" 32#include "modules/webmidi/MIDIAccess.h" 33 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" 46#include <v8.h> 47 48namespace blink { 49 50MIDIAccess::MIDIAccess(PassOwnPtr<MIDIAccessor> accessor, bool sysexEnabled, const Vector<MIDIAccessInitializer::PortDescriptor>& ports, ExecutionContext* executionContext) 51 : ActiveDOMObject(executionContext) 52 , m_accessor(accessor) 53 , m_sysexEnabled(sysexEnabled) 54{ 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)); 60 } else { 61 m_outputs.append(MIDIOutput::create(this, m_outputs.size(), port.id, port.manufacturer, port.name, port.version)); 62 } 63 } 64} 65 66MIDIAccess::~MIDIAccess() 67{ 68} 69 70MIDIInputMap* MIDIAccess::inputs() const 71{ 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); 76 } 77 if (inputs.size() != m_inputs.size()) { 78 // There is id duplication that violates the spec. 79 inputs.clear(); 80 } 81 return new MIDIInputMap(inputs); 82} 83 84MIDIOutputMap* MIDIAccess::outputs() const 85{ 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); 90 } 91 if (outputs.size() != m_outputs.size()) { 92 // There is id duplication that violates the spec. 93 outputs.clear(); 94 } 95 return new MIDIOutputMap(outputs); 96} 97 98void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, const String& name, const String& version) 99{ 100 ASSERT(isMainThread()); 101 m_inputs.append(MIDIInput::create(this, id, manufacturer, name, version)); 102} 103 104void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version) 105{ 106 ASSERT(isMainThread()); 107 unsigned portIndex = m_outputs.size(); 108 m_outputs.append(MIDIOutput::create(this, portIndex, id, manufacturer, name, version)); 109} 110 111void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStamp) 112{ 113 ASSERT(isMainThread()); 114 if (portIndex >= m_inputs.size()) 115 return; 116 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()); 121 ASSERT(document); 122 123 double timeStampInMilliseconds = 1000 * document->loader()->timing()->monotonicTimeToZeroBasedDocumentTime(timeStamp); 124 125 m_inputs[portIndex]->didReceiveMIDIData(portIndex, data, length, timeStampInMilliseconds); 126} 127 128void MIDIAccess::sendMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStampInMilliseconds) 129{ 130 if (!data || !length || portIndex >= m_outputs.size()) 131 return; 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(). 134 double timeStamp; 135 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. 139 timeStamp = 0; 140 } else { 141 Document* document = toDocument(executionContext()); 142 ASSERT(document); 143 double documentStartTime = document->loader()->timing()->referenceMonotonicTime(); 144 timeStamp = documentStartTime + 0.001 * timeStampInMilliseconds; 145 } 146 147 m_accessor->sendMIDIData(portIndex, data, length, timeStamp); 148} 149 150void MIDIAccess::stop() 151{ 152 m_accessor.clear(); 153} 154 155void MIDIAccess::trace(Visitor* visitor) 156{ 157 visitor->trace(m_inputs); 158 visitor->trace(m_outputs); 159 EventTargetWithInlineData::trace(visitor); 160} 161 162} // namespace blink 163