Home | History | Annotate | Download | only in midi
      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
      3 // found in the LICENSE file.
      4 
      5 #include "media/midi/midi_manager.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/bind_helpers.h"
      9 #include "base/threading/thread.h"
     10 
     11 namespace media {
     12 
     13 #if !defined(OS_MACOSX)
     14 // TODO(crogers): implement MIDIManager for other platforms.
     15 MIDIManager* MIDIManager::Create() {
     16   return NULL;
     17 }
     18 #endif
     19 
     20 MIDIManager::MIDIManager()
     21     : initialized_(false) {
     22 }
     23 
     24 MIDIManager::~MIDIManager() {}
     25 
     26 bool MIDIManager::StartSession(MIDIManagerClient* client) {
     27   // Lazily initialize the MIDI back-end.
     28   if (!initialized_)
     29     initialized_ = Initialize();
     30 
     31   if (initialized_) {
     32     base::AutoLock auto_lock(clients_lock_);
     33     clients_.insert(client);
     34   }
     35 
     36   return initialized_;
     37 }
     38 
     39 void MIDIManager::EndSession(MIDIManagerClient* client) {
     40   base::AutoLock auto_lock(clients_lock_);
     41   ClientList::iterator i = clients_.find(client);
     42   if (i != clients_.end())
     43     clients_.erase(i);
     44 }
     45 
     46 void MIDIManager::AddInputPort(const MIDIPortInfo& info) {
     47   input_ports_.push_back(info);
     48 }
     49 
     50 void MIDIManager::AddOutputPort(const MIDIPortInfo& info) {
     51   output_ports_.push_back(info);
     52 }
     53 
     54 void MIDIManager::ReceiveMIDIData(
     55     int port_index,
     56     const uint8* data,
     57     size_t length,
     58     double timestamp) {
     59   base::AutoLock auto_lock(clients_lock_);
     60 
     61   for (ClientList::iterator i = clients_.begin(); i != clients_.end(); ++i)
     62     (*i)->ReceiveMIDIData(port_index, data, length, timestamp);
     63 }
     64 
     65 void MIDIManager::DispatchSendMIDIData(MIDIManagerClient* client,
     66                                        int port_index,
     67                                        const uint8* data,
     68                                        size_t length,
     69                                        double timestamp) {
     70   // Lazily create the thread when first needed.
     71   if (!send_thread_) {
     72     send_thread_.reset(new base::Thread("MIDISendThread"));
     73     send_thread_->Start();
     74     send_message_loop_ = send_thread_->message_loop_proxy();
     75   }
     76 
     77   send_message_loop_->PostTask(
     78      FROM_HERE,
     79      base::Bind(&MIDIManager::SendMIDIData, base::Unretained(this),
     80          client, port_index, data, length, timestamp));
     81 }
     82 
     83 }  // namespace media
     84