OLD | NEW |
---|---|
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 "chromeos/dbus/cras_audio_client.h" | 5 #include "chromeos/dbus/cras_audio_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "dbus/bus.h" | 8 #include "dbus/bus.h" |
9 #include "dbus/message.h" | 9 #include "dbus/message.h" |
10 #include "dbus/object_path.h" | 10 #include "dbus/object_path.h" |
(...skipping 19 matching lines...) Expand all Loading... | |
30 // The CrasAudioClient implementation used in production. | 30 // The CrasAudioClient implementation used in production. |
31 class CrasAudioClientImpl : public CrasAudioClient { | 31 class CrasAudioClientImpl : public CrasAudioClient { |
32 public: | 32 public: |
33 explicit CrasAudioClientImpl(dbus::Bus* bus) | 33 explicit CrasAudioClientImpl(dbus::Bus* bus) |
34 : cras_proxy_(NULL), | 34 : cras_proxy_(NULL), |
35 weak_ptr_factory_(this) { | 35 weak_ptr_factory_(this) { |
36 cras_proxy_ = bus->GetObjectProxy( | 36 cras_proxy_ = bus->GetObjectProxy( |
37 cras::kCrasServiceName, | 37 cras::kCrasServiceName, |
38 dbus::ObjectPath(cras::kCrasServicePath)); | 38 dbus::ObjectPath(cras::kCrasServicePath)); |
39 | 39 |
40 // Monitor NameOwnerChanged signal. | |
41 cras_proxy_->SetNameOwnerChangedCallback( | |
42 base::Bind(&CrasAudioClientImpl::NameOwnerChangedReceived, | |
43 weak_ptr_factory_.GetWeakPtr())); | |
44 | |
40 // Monitor the D-Bus signal for output volume change. | 45 // Monitor the D-Bus signal for output volume change. |
41 cras_proxy_->ConnectToSignal( | 46 cras_proxy_->ConnectToSignal( |
42 cras::kCrasControlInterface, | 47 cras::kCrasControlInterface, |
43 cras::kOutputVolumeChanged, | 48 cras::kOutputVolumeChanged, |
44 base::Bind(&CrasAudioClientImpl::OutputVolumeChangedReceived, | 49 base::Bind(&CrasAudioClientImpl::OutputVolumeChangedReceived, |
45 weak_ptr_factory_.GetWeakPtr()), | 50 weak_ptr_factory_.GetWeakPtr()), |
46 base::Bind(&CrasAudioClientImpl::SignalConnected, | 51 base::Bind(&CrasAudioClientImpl::SignalConnected, |
47 weak_ptr_factory_.GetWeakPtr())); | 52 weak_ptr_factory_.GetWeakPtr())); |
48 | 53 |
49 // Monitor the D-Bus signal for output mute change. | 54 // Monitor the D-Bus signal for output mute change. |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
205 | 210 |
206 private: | 211 private: |
207 // Called when the cras signal is initially connected. | 212 // Called when the cras signal is initially connected. |
208 void SignalConnected(const std::string& interface_name, | 213 void SignalConnected(const std::string& interface_name, |
209 const std::string& signal_name, | 214 const std::string& signal_name, |
210 bool success) { | 215 bool success) { |
211 LOG_IF(ERROR, !success) | 216 LOG_IF(ERROR, !success) |
212 << "Failed to connect to cras signal:" << signal_name; | 217 << "Failed to connect to cras signal:" << signal_name; |
213 } | 218 } |
214 | 219 |
220 void NameOwnerChangedReceived(dbus::Signal* signal) { | |
221 FOR_EACH_OBSERVER(Observer, observers_, AudioClientRestarted()); | |
222 } | |
223 | |
215 // Called when a OutputVolumeChanged signal is received. | 224 // Called when a OutputVolumeChanged signal is received. |
216 void OutputVolumeChangedReceived(dbus::Signal* signal) { | 225 void OutputVolumeChangedReceived(dbus::Signal* signal) { |
217 dbus::MessageReader reader(signal); | 226 dbus::MessageReader reader(signal); |
218 int32 volume; | 227 int32 volume; |
219 if (!reader.PopInt32(&volume)) { | 228 if (!reader.PopInt32(&volume)) { |
220 LOG(ERROR) << "Error reading signal from cras:" | 229 LOG(ERROR) << "Error reading signal from cras:" |
221 << signal->ToString(); | 230 << signal->ToString(); |
222 } | 231 } |
223 FOR_EACH_OBSERVER(Observer, observers_, OutputVolumeChanged(volume)); | 232 FOR_EACH_OBSERVER(Observer, observers_, OutputVolumeChanged(volume)); |
224 } | 233 } |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
302 callback.Run(volume_state, success); | 311 callback.Run(volume_state, success); |
303 } | 312 } |
304 | 313 |
305 void OnGetNodes(const GetNodesCallback& callback, | 314 void OnGetNodes(const GetNodesCallback& callback, |
306 dbus::Response* response) { | 315 dbus::Response* response) { |
307 bool success = true; | 316 bool success = true; |
308 AudioNodeList node_list; | 317 AudioNodeList node_list; |
309 if (response) { | 318 if (response) { |
310 dbus::MessageReader response_reader(response); | 319 dbus::MessageReader response_reader(response); |
311 dbus::MessageReader array_reader(response); | 320 dbus::MessageReader array_reader(response); |
312 while(response_reader.HasMoreData()) { | 321 while (response_reader.HasMoreData()) { |
313 if (!response_reader.PopArray(&array_reader)) { | 322 if (!response_reader.PopArray(&array_reader)) { |
314 success = false; | 323 success = false; |
315 LOG(ERROR) << "Error reading response from cras: " | 324 LOG(ERROR) << "Error reading response from cras: " |
316 << response->ToString(); | 325 << response->ToString(); |
317 break; | 326 break; |
318 } | 327 } |
319 | 328 |
320 AudioNode node; | 329 AudioNode node; |
321 if (!GetAudioNode(response, &array_reader, &node)) { | 330 if (!GetAudioNode(response, &array_reader, &node)) { |
322 success = false; | 331 success = false; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
381 | 390 |
382 DISALLOW_COPY_AND_ASSIGN(CrasAudioClientImpl); | 391 DISALLOW_COPY_AND_ASSIGN(CrasAudioClientImpl); |
383 }; | 392 }; |
384 | 393 |
385 // The CrasAudioClient implementation used on Linux desktop, | 394 // The CrasAudioClient implementation used on Linux desktop, |
386 // which does nothing. | 395 // which does nothing. |
387 class CrasAudioClientStubImpl : public CrasAudioClient { | 396 class CrasAudioClientStubImpl : public CrasAudioClient { |
388 public: | 397 public: |
389 CrasAudioClientStubImpl() { | 398 CrasAudioClientStubImpl() { |
390 VLOG(1) << "CrasAudioClientStubImpl is created"; | 399 VLOG(1) << "CrasAudioClientStubImpl is created"; |
400 | |
401 // Fake audio nodes. | |
402 AudioNode node_1; | |
403 node_1.is_input = false; | |
404 node_1.id = 10001; | |
405 node_1.device_name = "Fake Audio Output"; | |
406 node_1.type = "INTERNAL_SPEAKER"; | |
407 node_1.name = "Internal Speaker"; | |
408 node_1.active = true; | |
409 | |
410 AudioNode node_2; | |
411 node_2.is_input = true; | |
412 node_2.id = 10002; | |
413 node_2.device_name = "Fake Audio Input"; | |
414 node_2.type = "INTERNAL_MIC"; | |
415 node_2.name = "Internal Mic"; | |
416 node_2.active = true; | |
417 | |
418 node_list_.push_back(node_1); | |
419 node_list_.push_back(node_2); | |
391 } | 420 } |
392 virtual ~CrasAudioClientStubImpl() {} | 421 virtual ~CrasAudioClientStubImpl() { |
422 } | |
393 | 423 |
394 // CrasAudioClient overrides: | 424 // CrasAudioClient overrides: |
395 // TODO(jennyz): Implement the observers and callbacks in the stub for UI | 425 // TODO(jennyz): Implement the observers and callbacks in the stub for UI |
396 // testing. | 426 // testing. |
397 virtual void AddObserver(Observer* observer) OVERRIDE {} | 427 virtual void AddObserver(Observer* observer) OVERRIDE { |
398 virtual void RemoveObserver(Observer* observer) OVERRIDE {} | 428 observers_.AddObserver(observer); |
399 virtual bool HasObserver(Observer* observer) OVERRIDE { return false; } | 429 } |
430 | |
431 virtual void RemoveObserver(Observer* observer) OVERRIDE { | |
432 observers_.RemoveObserver(observer); | |
433 } | |
434 | |
435 virtual bool HasObserver(Observer* observer) OVERRIDE { | |
436 return observers_.HasObserver(observer); | |
437 } | |
438 | |
400 virtual void GetVolumeState(const GetVolumeStateCallback& callback) OVERRIDE { | 439 virtual void GetVolumeState(const GetVolumeStateCallback& callback) OVERRIDE { |
440 callback.Run(volume_state_, true); | |
401 } | 441 } |
402 virtual void GetNodes(const GetNodesCallback& callback)OVERRIDE {} | 442 |
403 virtual void SetOutputVolume(int32 volume) OVERRIDE {} | 443 virtual void GetNodes(const GetNodesCallback& callback)OVERRIDE { |
404 virtual void SetOutputMute(bool mute_on) OVERRIDE {} | 444 callback.Run(node_list_, true); |
405 virtual void SetInputGain(int32 input_gain) OVERRIDE {} | 445 } |
406 virtual void SetInputMute(bool mute_on) OVERRIDE {} | 446 |
407 virtual void SetActiveOutputNode(uint64 node_id) OVERRIDE {} | 447 virtual void SetOutputVolume(int32 volume) OVERRIDE { |
408 virtual void SetActiveInputNode(uint64 node_id) OVERRIDE {} | 448 LOG(ERROR) << "CrasAudioClientStubImpl::SetOutputVolume volume=" << volume; |
stevenjb
2013/04/17 01:25:12
->VLOG here and below
jennyz
2013/04/18 01:21:19
Done.
| |
449 volume_state_.output_volume = volume; | |
450 FOR_EACH_OBSERVER(Observer, | |
451 observers_, | |
452 OutputVolumeChanged(volume_state_.output_volume)); | |
453 } | |
454 | |
455 virtual void SetOutputMute(bool mute_on) OVERRIDE { | |
456 LOG(ERROR) << "CrasAudioClientStubImpl::SetOutputMute, mute_on=" << mute_on; | |
457 volume_state_.output_mute = mute_on; | |
458 FOR_EACH_OBSERVER(Observer, | |
459 observers_, | |
460 OutputMuteChanged(volume_state_.output_mute)); | |
461 } | |
462 | |
463 virtual void SetInputGain(int32 input_gain) OVERRIDE { | |
464 volume_state_.input_gain = input_gain; | |
465 FOR_EACH_OBSERVER(Observer, | |
466 observers_, | |
467 InputGainChanged(volume_state_.input_gain)); | |
468 } | |
469 | |
470 virtual void SetInputMute(bool mute_on) OVERRIDE { | |
471 volume_state_.input_mute = mute_on; | |
472 FOR_EACH_OBSERVER(Observer, | |
473 observers_, | |
474 InputMuteChanged(volume_state_.input_mute)); | |
475 } | |
476 | |
477 virtual void SetActiveOutputNode(uint64 node_id) OVERRIDE { | |
478 active_output_node_id_ = node_id; | |
479 FOR_EACH_OBSERVER(Observer, | |
480 observers_, | |
481 ActiveOutputNodeChanged(node_id)); | |
482 } | |
483 | |
484 virtual void SetActiveInputNode(uint64 node_id) OVERRIDE { | |
485 active_input_node_id_ = node_id; | |
486 FOR_EACH_OBSERVER(Observer, | |
487 observers_, | |
488 ActiveInputNodeChanged(node_id)); | |
489 } | |
409 | 490 |
410 private: | 491 private: |
492 VolumeState volume_state_; | |
493 AudioNodeList node_list_; | |
494 uint64 active_input_node_id_; | |
495 uint64 active_output_node_id_; | |
496 ObserverList<Observer> observers_; | |
497 | |
411 DISALLOW_COPY_AND_ASSIGN(CrasAudioClientStubImpl); | 498 DISALLOW_COPY_AND_ASSIGN(CrasAudioClientStubImpl); |
412 }; | 499 }; |
413 | 500 |
414 CrasAudioClient::Observer::~Observer() { | 501 CrasAudioClient::Observer::~Observer() { |
415 } | 502 } |
416 | 503 |
504 void CrasAudioClient::Observer::AudioClientRestarted() { | |
505 } | |
506 | |
417 void CrasAudioClient::Observer::OutputVolumeChanged(int32 volume) { | 507 void CrasAudioClient::Observer::OutputVolumeChanged(int32 volume) { |
418 } | 508 } |
419 | 509 |
420 void CrasAudioClient::Observer::OutputMuteChanged(bool mute_on) { | 510 void CrasAudioClient::Observer::OutputMuteChanged(bool mute_on) { |
421 } | 511 } |
422 | 512 |
423 void CrasAudioClient::Observer::InputGainChanged(int gain) { | 513 void CrasAudioClient::Observer::InputGainChanged(int gain) { |
424 } | 514 } |
425 | 515 |
426 void CrasAudioClient::Observer::InputMuteChanged(bool mute_on) { | 516 void CrasAudioClient::Observer::InputMuteChanged(bool mute_on) { |
(...skipping 19 matching lines...) Expand all Loading... | |
446 DBusClientImplementationType type, | 536 DBusClientImplementationType type, |
447 dbus::Bus* bus) { | 537 dbus::Bus* bus) { |
448 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) { | 538 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) { |
449 return new CrasAudioClientImpl(bus); | 539 return new CrasAudioClientImpl(bus); |
450 } | 540 } |
451 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | 541 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); |
452 return new CrasAudioClientStubImpl(); | 542 return new CrasAudioClientStubImpl(); |
453 } | 543 } |
454 | 544 |
455 } // namespace chromeos | 545 } // namespace chromeos |
OLD | NEW |