summaryrefslogtreecommitdiff
path: root/chromium/media/midi/midi_manager.cc
blob: b3262e4a034516ab8af46629bc3cfffa5b5d5ea7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/midi/midi_manager.h"

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/message_loop/message_loop.h"
#include "base/threading/thread.h"

namespace media {

#if !defined(OS_MACOSX)
// TODO(crogers): implement MIDIManager for other platforms.
MIDIManager* MIDIManager::Create() {
  return NULL;
}
#endif

MIDIManager::MIDIManager()
    : initialized_(false) {
}

MIDIManager::~MIDIManager() {}

bool MIDIManager::StartSession(MIDIManagerClient* client) {
  // Lazily initialize the MIDI back-end.
  if (!initialized_)
    initialized_ = Initialize();

  if (initialized_) {
    base::AutoLock auto_lock(clients_lock_);
    clients_.insert(client);
  }

  return initialized_;
}

void MIDIManager::EndSession(MIDIManagerClient* client) {
  base::AutoLock auto_lock(clients_lock_);
  ClientList::iterator i = clients_.find(client);
  if (i != clients_.end())
    clients_.erase(i);
}

void MIDIManager::AddInputPort(const MIDIPortInfo& info) {
  input_ports_.push_back(info);
}

void MIDIManager::AddOutputPort(const MIDIPortInfo& info) {
  output_ports_.push_back(info);
}

void MIDIManager::ReceiveMIDIData(
    uint32 port_index,
    const uint8* data,
    size_t length,
    double timestamp) {
  base::AutoLock auto_lock(clients_lock_);

  for (ClientList::iterator i = clients_.begin(); i != clients_.end(); ++i)
    (*i)->ReceiveMIDIData(port_index, data, length, timestamp);
}

bool MIDIManager::CurrentlyOnMIDISendThread() {
  return send_thread_->message_loop() == base::MessageLoop::current();
}

void MIDIManager::DispatchSendMIDIData(MIDIManagerClient* client,
                                       uint32 port_index,
                                       const std::vector<uint8>& data,
                                       double timestamp) {
  // Lazily create the thread when first needed.
  if (!send_thread_) {
    send_thread_.reset(new base::Thread("MIDISendThread"));
    send_thread_->Start();
    send_message_loop_ = send_thread_->message_loop_proxy();
  }

  send_message_loop_->PostTask(
     FROM_HERE,
     base::Bind(&MIDIManager::SendMIDIData, base::Unretained(this),
         client, port_index, data, timestamp));
}

}  // namespace media