Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(402)

Side by Side Diff: media/midi/midi_manager.cc

Issue 662853003: Manage MIDI related objects and sessions' lifecycles correctly (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: unittest Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/midi/midi_manager.h" 5 #include "media/midi/midi_manager.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/debug/trace_event.h" 8 #include "base/debug/trace_event.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/message_loop/message_loop_proxy.h" 10 #include "base/message_loop/message_loop_proxy.h"
11 11
12 namespace media { 12 namespace media {
13 13
14 MidiManager::MidiManager() 14 MidiManager::MidiManager()
15 : initialized_(false), 15 : initialized_(false),
16 result_(MIDI_NOT_SUPPORTED) { 16 result_(MIDI_NOT_SUPPORTED) {
17 } 17 }
18 18
19 MidiManager::~MidiManager() { 19 MidiManager::~MidiManager() {
20 } 20 }
21 21
22 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \ 22 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \
23 !defined(OS_ANDROID) && !defined(OS_CHROMEOS) 23 !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
24 MidiManager* MidiManager::Create() { 24 MidiManager* MidiManager::Create() {
25 return new MidiManager; 25 return new MidiManager;
26 } 26 }
27 #endif 27 #endif
28 28
29 void MidiManager::StartSession(MidiManagerClient* client, int client_id) { 29 void MidiManager::StartSession(MidiManagerClient* client) {
30 bool session_is_ready; 30 bool session_is_ready;
31 bool session_needs_initialization = false; 31 bool session_needs_initialization = false;
32 bool too_many_pending_clients_exist = false; 32 bool too_many_pending_clients_exist = false;
33 33
34 { 34 {
35 base::AutoLock auto_lock(lock_); 35 base::AutoLock auto_lock(lock_);
36 session_is_ready = initialized_; 36 session_is_ready = initialized_;
yhirano 2014/10/20 12:15:15 DCHECK(clients_.find(client) == clients_.end());
Takashi Toyoshima 2014/10/20 17:14:44 Nice. I prefer NOTREACHED() and return.
37 if (!session_is_ready) { 37 if (!session_is_ready) {
38 // Do not accept a new request if the pending client list contains too 38 // Do not accept a new request if the pending client list contains too
39 // many clients. 39 // many clients.
40 too_many_pending_clients_exist = 40 too_many_pending_clients_exist =
41 pending_clients_.size() >= kMaxPendingClientCount; 41 pending_clients_.size() >= kMaxPendingClientCount;
42 42
43 if (!too_many_pending_clients_exist) { 43 if (!too_many_pending_clients_exist) {
44 // Call StartInitialization() only for the first request. 44 // Call StartInitialization() only for the first request.
45 session_needs_initialization = pending_clients_.empty(); 45 session_needs_initialization = pending_clients_.empty();
46 pending_clients_.insert(std::make_pair(client, client_id)); 46 pending_clients_.insert(client);
47 } 47 }
48 } 48 }
49 } 49 }
50 50
51 // Lazily initialize the MIDI back-end. 51 // Lazily initialize the MIDI back-end.
52 if (!session_is_ready) { 52 if (!session_is_ready) {
53 if (session_needs_initialization) { 53 if (session_needs_initialization) {
54 TRACE_EVENT0("midi", "MidiManager::StartInitialization"); 54 TRACE_EVENT0("midi", "MidiManager::StartInitialization");
55 session_thread_runner_ = 55 session_thread_runner_ =
56 base::MessageLoop::current()->message_loop_proxy(); 56 base::MessageLoop::current()->message_loop_proxy();
57 StartInitialization(); 57 StartInitialization();
58 } 58 }
59 if (too_many_pending_clients_exist) { 59 if (too_many_pending_clients_exist) {
60 // Return an error immediately if there are too many requests. 60 // Return an error immediately if there are too many requests.
61 client->CompleteStartSession(client_id, MIDI_INITIALIZATION_ERROR); 61 client->CompleteStartSession(MIDI_INITIALIZATION_ERROR);
62 return; 62 return;
63 } 63 }
64 // CompleteInitialization() will be called asynchronously when platform 64 // CompleteInitialization() will be called asynchronously when platform
65 // dependent initialization is finished. 65 // dependent initialization is finished.
66 return; 66 return;
67 } 67 }
68 68
69 // Platform dependent initialization was already finished for previously 69 // Platform dependent initialization was already finished for previously
70 // initialized clients. 70 // initialized clients.
71 MidiResult result; 71 MidiResult result;
72 { 72 {
73 base::AutoLock auto_lock(lock_); 73 base::AutoLock auto_lock(lock_);
74 if (result_ == MIDI_OK) 74 if (result_ == MIDI_OK)
75 clients_.insert(client); 75 clients_.insert(client);
76 result = result_; 76 result = result_;
77 } 77 }
78 client->CompleteStartSession(client_id, result); 78 client->CompleteStartSession(result);
79 } 79 }
80 80
81 void MidiManager::EndSession(MidiManagerClient* client) { 81 void MidiManager::EndSession(MidiManagerClient* client) {
82 base::AutoLock auto_lock(lock_); 82 base::AutoLock auto_lock(lock_);
83 clients_.erase(client); 83 clients_.erase(client);
84 pending_clients_.erase(client); 84 pending_clients_.erase(client);
85 } 85 }
86 86
87 void MidiManager::DispatchSendMidiData(MidiManagerClient* client, 87 void MidiManager::DispatchSendMidiData(MidiManagerClient* client,
88 uint32 port_index, 88 uint32 port_index,
(...skipping 25 matching lines...) Expand all
114 output_ports_.push_back(info); 114 output_ports_.push_back(info);
115 } 115 }
116 116
117 void MidiManager::ReceiveMidiData( 117 void MidiManager::ReceiveMidiData(
118 uint32 port_index, 118 uint32 port_index,
119 const uint8* data, 119 const uint8* data,
120 size_t length, 120 size_t length,
121 double timestamp) { 121 double timestamp) {
122 base::AutoLock auto_lock(lock_); 122 base::AutoLock auto_lock(lock_);
123 123
124 for (ClientList::iterator i = clients_.begin(); i != clients_.end(); ++i) 124 for (ClientSet::iterator it = clients_.begin(); it != clients_.end(); ++it)
yhirano 2014/10/20 12:15:15 You can use the range-based for.
Takashi Toyoshima 2014/10/20 17:14:44 Done.
125 (*i)->ReceiveMidiData(port_index, data, length, timestamp); 125 (*it)->ReceiveMidiData(port_index, data, length, timestamp);
126 } 126 }
127 127
128 void MidiManager::CompleteInitializationInternal(MidiResult result) { 128 void MidiManager::CompleteInitializationInternal(MidiResult result) {
129 TRACE_EVENT0("midi", "MidiManager::CompleteInitialization"); 129 TRACE_EVENT0("midi", "MidiManager::CompleteInitialization");
130 130
131 base::AutoLock auto_lock(lock_); 131 base::AutoLock auto_lock(lock_);
132 DCHECK(clients_.empty()); 132 DCHECK(clients_.empty());
133 DCHECK(!initialized_); 133 DCHECK(!initialized_);
134 initialized_ = true; 134 initialized_ = true;
135 result_ = result; 135 result_ = result;
136 136
137 for (PendingClientMap::iterator it = pending_clients_.begin(); 137 for (ClientSet::iterator it = pending_clients_.begin();
yhirano 2014/10/20 12:15:15 ditto
Takashi Toyoshima 2014/10/20 17:14:44 Done.
138 it != pending_clients_.end(); 138 it != pending_clients_.end();
139 ++it) { 139 ++it) {
140 if (result_ == MIDI_OK) 140 if (result_ == MIDI_OK)
141 clients_.insert(it->first); 141 clients_.insert(*it);
142 it->first->CompleteStartSession(it->second, result_); 142 (*it)->CompleteStartSession(result_);
143 } 143 }
144 pending_clients_.clear(); 144 pending_clients_.clear();
145 } 145 }
146 146
147 } // namespace media 147 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698