• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef CONTENT_BROWSER_MEDIA_MIDI_HOST_H_
6 #define CONTENT_BROWSER_MEDIA_MIDI_HOST_H_
7 
8 #include <vector>
9 
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/scoped_vector.h"
14 #include "content/common/content_export.h"
15 #include "content/public/browser/browser_message_filter.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "media/midi/midi_manager.h"
18 
19 namespace media {
20 class MidiManager;
21 class MidiMessageQueue;
22 }
23 
24 namespace content {
25 
26 class CONTENT_EXPORT MidiHost
27     : public BrowserMessageFilter,
28       public media::MidiManagerClient {
29  public:
30   // Called from UI thread from the owner of this object.
31   MidiHost(int renderer_process_id, media::MidiManager* midi_manager);
32 
33   // BrowserMessageFilter implementation.
34   virtual void OnDestruct() const OVERRIDE;
35   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
36 
37   // MidiManagerClient implementation.
38   virtual void CompleteStartSession(int client_id,
39                                     media::MidiResult result) OVERRIDE;
40   virtual void ReceiveMidiData(uint32 port,
41                                const uint8* data,
42                                size_t length,
43                                double timestamp) OVERRIDE;
44   virtual void AccumulateMidiBytesSent(size_t n) OVERRIDE;
45 
46   // Start session to access MIDI hardware.
47   void OnStartSession(int client_id);
48 
49   // Data to be sent to a MIDI output port.
50   void OnSendData(uint32 port,
51                   const std::vector<uint8>& data,
52                   double timestamp);
53 
54  private:
55   FRIEND_TEST_ALL_PREFIXES(MidiHostTest, IsValidWebMIDIData);
56   friend class base::DeleteHelper<MidiHost>;
57   friend class BrowserThread;
58 
59   virtual ~MidiHost();
60 
61   // Returns true if |data| fulfills the requirements of MidiOutput.send API
62   // defined in the WebMIDI spec.
63   // - |data| must be any number of complete MIDI messages (data abbreviation
64   //    called "running status" is disallowed).
65   // - 1-byte MIDI realtime messages can be placed at any position of |data|.
66   static bool IsValidWebMIDIData(const std::vector<uint8>& data);
67 
68   int renderer_process_id_;
69 
70   // Represents if the renderer has a permission to send/receive MIDI SysEX
71   // messages.
72   bool has_sys_ex_permission_;
73 
74   // |midi_manager_| talks to the platform-specific MIDI APIs.
75   // It can be NULL if the platform (or our current implementation)
76   // does not support MIDI.  If not supported then a call to
77   // OnRequestAccess() will always refuse access and a call to
78   // OnSendData() will do nothing.
79   media::MidiManager* const midi_manager_;
80 
81   // Buffers where data sent from each MIDI input port is stored.
82   ScopedVector<media::MidiMessageQueue> received_messages_queues_;
83 
84   // The number of bytes sent to the platform-specific MIDI sending
85   // system, but not yet completed.
86   size_t sent_bytes_in_flight_;
87 
88   // The number of bytes successfully sent since the last time
89   // we've acknowledged back to the renderer.
90   size_t bytes_sent_since_last_acknowledgement_;
91 
92   // Protects access to |sent_bytes_in_flight_|.
93   base::Lock in_flight_lock_;
94 
95   DISALLOW_COPY_AND_ASSIGN(MidiHost);
96 };
97 
98 }  // namespace content
99 
100 #endif  // CONTENT_BROWSER_MEDIA_MIDI_HOST_H_
101