Home | History | Annotate | Download | only in webmidi
      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/MIDIOptions.h"
     41 #include "modules/webmidi/MIDIPort.h"
     42 #include "platform/AsyncMethodRunner.h"
     43 #include <v8.h>
     44 
     45 namespace WebCore {
     46 
     47 MIDIAccess::MIDIAccess(PassOwnPtr<MIDIAccessor> accessor, bool sysexEnabled, const Vector<MIDIAccessInitializer::PortDescriptor>& ports, ExecutionContext* executionContext)
     48     : ActiveDOMObject(executionContext)
     49     , m_accessor(accessor)
     50     , m_sysexEnabled(sysexEnabled)
     51 {
     52     ScriptWrappable::init(this);
     53     m_accessor->setClient(this);
     54     for (size_t i = 0; i < ports.size(); ++i) {
     55         const MIDIAccessInitializer::PortDescriptor& port = ports[i];
     56         if (port.type == MIDIPort::MIDIPortTypeInput) {
     57             m_inputs.append(MIDIInput::create(this, port.id, port.manufacturer, port.name, port.version));
     58         } else {
     59             m_outputs.append(MIDIOutput::create(this, m_outputs.size(), port.id, port.manufacturer, port.name, port.version));
     60         }
     61     }
     62 }
     63 
     64 MIDIAccess::~MIDIAccess()
     65 {
     66 }
     67 
     68 void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, const String& name, const String& version)
     69 {
     70     ASSERT(isMainThread());
     71     m_inputs.append(MIDIInput::create(this, id, manufacturer, name, version));
     72 }
     73 
     74 void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version)
     75 {
     76     ASSERT(isMainThread());
     77     unsigned portIndex = m_outputs.size();
     78     m_outputs.append(MIDIOutput::create(this, portIndex, id, manufacturer, name, version));
     79 }
     80 
     81 void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStamp)
     82 {
     83     ASSERT(isMainThread());
     84     if (portIndex >= m_inputs.size())
     85         return;
     86 
     87     // Convert from time in seconds which is based on the time coordinate system of monotonicallyIncreasingTime()
     88     // into time in milliseconds (a DOMHighResTimeStamp) according to the same time coordinate system as performance.now().
     89     // This is how timestamps are defined in the Web MIDI spec.
     90     Document* document = toDocument(executionContext());
     91     ASSERT(document);
     92 
     93     double timeStampInMilliseconds = 1000 * document->loader()->timing()->monotonicTimeToZeroBasedDocumentTime(timeStamp);
     94 
     95     m_inputs[portIndex]->didReceiveMIDIData(portIndex, data, length, timeStampInMilliseconds);
     96 }
     97 
     98 void MIDIAccess::sendMIDIData(unsigned portIndex, const unsigned char* data, size_t length, double timeStampInMilliseconds)
     99 {
    100     if (!data || !length || portIndex >= m_outputs.size())
    101         return;
    102     // Convert from a time in milliseconds (a DOMHighResTimeStamp) according to the same time coordinate system as performance.now()
    103     // into a time in seconds which is based on the time coordinate system of monotonicallyIncreasingTime().
    104     double timeStamp;
    105 
    106     if (!timeStampInMilliseconds) {
    107         // We treat a value of 0 (which is the default value) as special, meaning "now".
    108         // We need to translate it exactly to 0 seconds.
    109         timeStamp = 0;
    110     } else {
    111         Document* document = toDocument(executionContext());
    112         ASSERT(document);
    113         double documentStartTime = document->loader()->timing()->referenceMonotonicTime();
    114         timeStamp = documentStartTime + 0.001 * timeStampInMilliseconds;
    115     }
    116 
    117     m_accessor->sendMIDIData(portIndex, data, length, timeStamp);
    118 }
    119 
    120 void MIDIAccess::stop()
    121 {
    122     m_accessor.clear();
    123 }
    124 
    125 void MIDIAccess::trace(Visitor* visitor)
    126 {
    127     visitor->trace(m_inputs);
    128     visitor->trace(m_outputs);
    129     EventTargetWithInlineData::trace(visitor);
    130 }
    131 
    132 } // namespace WebCore
    133