forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Web MIDI: minimum support for Linux and Chrome OS (reland)
This change enables Web MIDI API on Linux and Chrome OS. Only Sending functionality works with this first experimental change. BUG=303601 TBR=scherkus@chromium.org NOTRY=true Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=241524 Review URL: https://codereview.chromium.org/111443005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@242871 0039d316-1c4b-4281-b951-d872f2087c98
- Loading branch information
toyoshim@chromium.org
committed
Jan 3, 2014
1 parent
342ed00
commit 28e56e9
Showing
5 changed files
with
213 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
// Copyright 2014 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_linux.h" | ||
|
||
#include <alsa/asoundlib.h> | ||
|
||
#include "base/bind.h" | ||
#include "base/debug/trace_event.h" | ||
#include "base/logging.h" | ||
#include "base/memory/ref_counted.h" | ||
#include "base/message_loop/message_loop.h" | ||
#include "base/strings/stringprintf.h" | ||
#include "base/threading/thread.h" | ||
#include "media/midi/midi_port_info.h" | ||
|
||
namespace media { | ||
namespace { | ||
const char kUnknown[] = "[unknown]"; | ||
} // namespace | ||
|
||
class MIDIManagerLinux::MIDIDeviceInfo | ||
: public base::RefCounted<MIDIDeviceInfo> { | ||
public: | ||
MIDIDeviceInfo(MIDIManagerLinux* manager, | ||
const std::string& card, | ||
const snd_rawmidi_info_t* midi, | ||
int device) { | ||
opened_ = !snd_rawmidi_open(&midi_in_, &midi_out_, card.c_str(), 0); | ||
if (!opened_) | ||
return; | ||
|
||
const std::string name = snd_rawmidi_info_get_name(midi); | ||
const std::string id = base::StringPrintf("%s:%d", card.c_str(), device); | ||
port_info_ = MIDIPortInfo(id, kUnknown, name, kUnknown); | ||
} | ||
|
||
void Send(MIDIManagerClient* client, const std::vector<uint8>& data) { | ||
ssize_t result = snd_rawmidi_write( | ||
midi_out_, reinterpret_cast<const void*>(&data[0]), data.size()); | ||
if (static_cast<size_t>(result) != data.size()) { | ||
// TODO(toyoshim): Disconnect and reopen the device. | ||
LOG(ERROR) << "snd_rawmidi_write fails: " << strerror(-result); | ||
} | ||
base::MessageLoop::current()->PostTask( | ||
FROM_HERE, | ||
base::Bind(&MIDIManagerClient::AccumulateMIDIBytesSent, | ||
base::Unretained(client), data.size())); | ||
} | ||
|
||
const MIDIPortInfo& GetMIDIPortInfo() const { return port_info_; } | ||
bool IsOpened() const { return opened_; } | ||
|
||
private: | ||
friend class base::RefCounted<MIDIDeviceInfo>; | ||
virtual ~MIDIDeviceInfo() { | ||
if (opened_) { | ||
snd_rawmidi_close(midi_in_); | ||
snd_rawmidi_close(midi_out_); | ||
} | ||
} | ||
|
||
bool opened_; | ||
MIDIPortInfo port_info_; | ||
snd_rawmidi_t* midi_in_; | ||
snd_rawmidi_t* midi_out_; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(MIDIDeviceInfo); | ||
}; | ||
|
||
MIDIManagerLinux::MIDIManagerLinux() | ||
: send_thread_("MIDISendThread") { | ||
} | ||
|
||
bool MIDIManagerLinux::Initialize() { | ||
// TODO(toyoshim): Make Initialize() asynchronous. | ||
TRACE_EVENT0("midi", "MIDIManagerMac::Initialize"); | ||
|
||
// Enumerate only hardware MIDI devices because software MIDIs running in | ||
// the browser process is not secure. | ||
snd_ctl_card_info_t* card; | ||
snd_rawmidi_info_t* midi_out; | ||
snd_rawmidi_info_t* midi_in; | ||
snd_ctl_card_info_alloca(&card); | ||
snd_rawmidi_info_alloca(&midi_out); | ||
snd_rawmidi_info_alloca(&midi_in); | ||
for (int index = -1; !snd_card_next(&index) && index >= 0; ) { | ||
const std::string id = base::StringPrintf("hw:CARD=%i", index); | ||
snd_ctl_t* handle; | ||
int err = snd_ctl_open(&handle, id.c_str(), 0); | ||
if (err != 0) { | ||
DLOG(ERROR) << "snd_ctl_open fails: " << snd_strerror(err); | ||
continue; | ||
} | ||
err = snd_ctl_card_info(handle, card); | ||
if (err != 0) { | ||
DLOG(ERROR) << "snd_ctl_card_info fails: " << snd_strerror(err); | ||
snd_ctl_close(handle); | ||
continue; | ||
} | ||
for (int device = -1; | ||
!snd_ctl_rawmidi_next_device(handle, &device) && device >= 0; ) { | ||
bool output; | ||
bool input; | ||
snd_rawmidi_info_set_device(midi_out, device); | ||
snd_rawmidi_info_set_subdevice(midi_out, 0); | ||
snd_rawmidi_info_set_stream(midi_out, SND_RAWMIDI_STREAM_OUTPUT); | ||
output = snd_ctl_rawmidi_info(handle, midi_out) == 0; | ||
snd_rawmidi_info_set_device(midi_in, device); | ||
snd_rawmidi_info_set_subdevice(midi_in, 0); | ||
snd_rawmidi_info_set_stream(midi_in, SND_RAWMIDI_STREAM_INPUT); | ||
input = snd_ctl_rawmidi_info(handle, midi_in) == 0; | ||
if (!output && !input) | ||
continue; | ||
scoped_refptr<MIDIDeviceInfo> port = | ||
new MIDIDeviceInfo(this, id, output ? midi_out : midi_in, device); | ||
if (!port->IsOpened()) { | ||
DLOG(ERROR) << "MIDIDeviceInfo open fails"; | ||
continue; | ||
} | ||
if (input) { | ||
in_devices_.push_back(port); | ||
AddInputPort(port->GetMIDIPortInfo()); | ||
} | ||
if (output) { | ||
out_devices_.push_back(port); | ||
AddOutputPort(port->GetMIDIPortInfo()); | ||
} | ||
} | ||
snd_ctl_close(handle); | ||
} | ||
return true; | ||
} | ||
|
||
MIDIManagerLinux::~MIDIManagerLinux() { | ||
send_thread_.Stop(); | ||
} | ||
|
||
void MIDIManagerLinux::DispatchSendMIDIData(MIDIManagerClient* client, | ||
uint32 port_index, | ||
const std::vector<uint8>& data, | ||
double timestamp) { | ||
if (out_devices_.size() <= port_index) | ||
return; | ||
|
||
base::TimeDelta delay; | ||
if (timestamp != 0.0) { | ||
base::TimeTicks time_to_send = | ||
base::TimeTicks() + base::TimeDelta::FromMicroseconds( | ||
timestamp * base::Time::kMicrosecondsPerSecond); | ||
delay = std::max(time_to_send - base::TimeTicks::Now(), base::TimeDelta()); | ||
} | ||
|
||
if (!send_thread_.IsRunning()) | ||
send_thread_.Start(); | ||
|
||
scoped_refptr<MIDIDeviceInfo> device = out_devices_[port_index]; | ||
send_thread_.message_loop()->PostDelayedTask( | ||
FROM_HERE, | ||
base::Bind(&MIDIDeviceInfo::Send, device, client, data), | ||
delay); | ||
} | ||
|
||
MIDIManager* MIDIManager::Create() { | ||
return new MIDIManagerLinux(); | ||
} | ||
|
||
} // namespace media |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2014 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. | ||
|
||
#ifndef MEDIA_MIDI_MIDI_MANAGER_LINUX_H_ | ||
#define MEDIA_MIDI_MIDI_MANAGER_LINUX_H_ | ||
|
||
#include <vector> | ||
|
||
#include "base/basictypes.h" | ||
#include "base/memory/scoped_ptr.h" | ||
#include "base/threading/thread.h" | ||
#include "media/midi/midi_manager.h" | ||
|
||
namespace media { | ||
|
||
class MIDIManagerLinux : public MIDIManager { | ||
public: | ||
MIDIManagerLinux(); | ||
virtual ~MIDIManagerLinux(); | ||
|
||
// MIDIManager implementation. | ||
virtual bool Initialize() OVERRIDE; | ||
virtual void DispatchSendMIDIData(MIDIManagerClient* client, | ||
uint32 port_index, | ||
const std::vector<uint8>& data, | ||
double timestamp) OVERRIDE; | ||
|
||
private: | ||
class MIDIDeviceInfo; | ||
std::vector<scoped_refptr<MIDIDeviceInfo> > in_devices_; | ||
std::vector<scoped_refptr<MIDIDeviceInfo> > out_devices_; | ||
base::Thread send_thread_; | ||
DISALLOW_COPY_AND_ASSIGN(MIDIManagerLinux); | ||
}; | ||
|
||
} // namespace media | ||
|
||
#endif // MEDIA_MIDI_MIDI_MANAGER_LINUX_H_ |