summaryrefslogtreecommitdiff
path: root/chromium/content/browser/renderer_host/media/midi_host.cc
blob: 6ed473afeff10538ef4ee2b8fb7db5ce3b5beccb (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// 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 "content/browser/renderer_host/media/midi_host.h"

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/debug/trace_event.h"
#include "base/process/process.h"
#include "content/browser/browser_main_loop.h"
#include "content/browser/media/media_internals.h"
#include "content/common/media/midi_messages.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/media_observer.h"
#include "media/midi/midi_manager.h"

using media::MIDIManager;
using media::MIDIPortInfoList;

// The total number of bytes which we're allowed to send to the OS
// before knowing that they have been successfully sent.
static const size_t kMaxInFlightBytes = 10 * 1024 * 1024;  // 10 MB.

// We keep track of the number of bytes successfully sent to
// the hardware.  Every once in a while we report back to the renderer
// the number of bytes sent since the last report. This threshold determines
// how many bytes will be sent before reporting back to the renderer.
static const size_t kAcknowledgementThresholdBytes = 1024 * 1024;  // 1 MB.

static const uint8 kSysExMessage = 0xf0;

namespace content {

MIDIHost::MIDIHost(media::MIDIManager* midi_manager)
    : midi_manager_(midi_manager),
      sent_bytes_in_flight_(0),
      bytes_sent_since_last_acknowledgement_(0) {
}

MIDIHost::~MIDIHost() {
  if (midi_manager_)
    midi_manager_->EndSession(this);
}

void MIDIHost::OnChannelClosing() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));

  BrowserMessageFilter::OnChannelClosing();
}

void MIDIHost::OnDestruct() const {
  BrowserThread::DeleteOnIOThread::Destruct(this);
}

///////////////////////////////////////////////////////////////////////////////
// IPC Messages handler
bool MIDIHost::OnMessageReceived(const IPC::Message& message,
                                 bool* message_was_ok) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP_EX(MIDIHost, message, *message_was_ok)
    IPC_MESSAGE_HANDLER(MIDIHostMsg_StartSession, OnStartSession)
    IPC_MESSAGE_HANDLER(MIDIHostMsg_SendData, OnSendData)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP_EX()

  return handled;
}

void MIDIHost::OnStartSession(int client_id) {
  MIDIPortInfoList input_ports;
  MIDIPortInfoList output_ports;

  // Initialize devices and register to receive MIDI data.
  bool success = false;
  if (midi_manager_) {
    success = midi_manager_->StartSession(this);
    if (success) {
      input_ports = midi_manager_->input_ports();
      output_ports = midi_manager_->output_ports();
    }
  }

  Send(new MIDIMsg_SessionStarted(
       client_id,
       success,
       input_ports,
       output_ports));
}

void MIDIHost::OnSendData(int port,
                          const std::vector<uint8>& data,
                          double timestamp) {
  if (!midi_manager_)
    return;

  base::AutoLock auto_lock(in_flight_lock_);

  // Sanity check that we won't send too much.
  if (sent_bytes_in_flight_ > kMaxInFlightBytes ||
      data.size() > kMaxInFlightBytes ||
      data.size() + sent_bytes_in_flight_ > kMaxInFlightBytes)
    return;

  // For now disallow all System Exclusive messages even if we
  // have permission.
  // TODO(toyoshim): allow System Exclusive if browser has granted
  // this client access.  We'll likely need to pass a GURL
  // here to compare against our permissions.
  if (data.size() > 0 && data[0] >= kSysExMessage)
      return;

#if defined(OS_ANDROID)
  // TODO(toyoshim): figure out why data() method does not compile on Android.
  NOTIMPLEMENTED();
#else
  midi_manager_->DispatchSendMIDIData(
      this,
      port,
      data.data(),
      data.size(),
      timestamp);
#endif

  sent_bytes_in_flight_ += data.size();
}

void MIDIHost::ReceiveMIDIData(
    int port_index,
    const uint8* data,
    size_t length,
    double timestamp) {
  TRACE_EVENT0("midi", "MIDIHost::ReceiveMIDIData");

  // For now disallow all System Exclusive messages even if we
  // have permission.
  // TODO(toyoshim): allow System Exclusive if browser has granted
  // this client access.  We'll likely need to pass a GURL
  // here to compare against our permissions.
  if (length > 0 && data[0] >= kSysExMessage)
      return;

  // Send to the renderer.
  std::vector<uint8> v(data, data + length);
  Send(new MIDIMsg_DataReceived(port_index, v, timestamp));
}

void MIDIHost::AccumulateMIDIBytesSent(size_t n) {
  {
    base::AutoLock auto_lock(in_flight_lock_);
    if (n <= sent_bytes_in_flight_)
      sent_bytes_in_flight_ -= n;
  }

  if (bytes_sent_since_last_acknowledgement_ + n >=
      bytes_sent_since_last_acknowledgement_)
    bytes_sent_since_last_acknowledgement_ += n;

  if (bytes_sent_since_last_acknowledgement_ >=
      kAcknowledgementThresholdBytes) {
    Send(new MIDIMsg_AcknowledgeSentData(
        bytes_sent_since_last_acknowledgement_));
    bytes_sent_since_last_acknowledgement_ = 0;
  }
}

}  // namespace content