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/audio/cras_audio_handler.h" | 5 #include "chromeos/audio/cras_audio_handler.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 499 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 SetupAudioInputState(); | 510 SetupAudioInputState(); |
511 SetActiveInputNode(active_input_node_id_); | 511 SetActiveInputNode(active_input_node_id_); |
512 } else { | 512 } else { |
513 if (!ChangeActiveDevice(device, &active_output_node_id_)) | 513 if (!ChangeActiveDevice(device, &active_output_node_id_)) |
514 return; | 514 return; |
515 SetupAudioOutputState(); | 515 SetupAudioOutputState(); |
516 SetActiveOutputNode(active_output_node_id_); | 516 SetActiveOutputNode(active_output_node_id_); |
517 } | 517 } |
518 } | 518 } |
519 | 519 |
| 520 bool CrasAudioHandler::HasDeviceChange(const AudioNodeList& new_nodes, |
| 521 bool is_input) { |
| 522 size_t num_old_devices = 0; |
| 523 size_t num_new_devices = 0; |
| 524 for (AudioDeviceMap::const_iterator it = audio_devices_.begin(); |
| 525 it != audio_devices_.end(); ++it) { |
| 526 if (is_input == it->second.is_input) |
| 527 ++num_old_devices; |
| 528 } |
| 529 |
| 530 for (AudioNodeList::const_iterator it = new_nodes.begin(); |
| 531 it != new_nodes.end(); ++it) { |
| 532 if (is_input == it->is_input) |
| 533 ++num_new_devices; |
| 534 } |
| 535 return num_old_devices != num_new_devices; |
| 536 } |
| 537 |
520 void CrasAudioHandler::UpdateDevicesAndSwitchActive( | 538 void CrasAudioHandler::UpdateDevicesAndSwitchActive( |
521 const AudioNodeList& nodes) { | 539 const AudioNodeList& nodes) { |
522 size_t old_audio_devices_size = audio_devices_.size(); | 540 size_t old_audio_devices_size = audio_devices_.size(); |
| 541 bool output_devices_changed = HasDeviceChange(nodes, false); |
| 542 bool input_devices_changed = HasDeviceChange(nodes, true); |
523 audio_devices_.clear(); | 543 audio_devices_.clear(); |
524 has_alternative_input_ = false; | 544 has_alternative_input_ = false; |
525 has_alternative_output_ = false; | 545 has_alternative_output_ = false; |
526 | 546 |
527 while (!input_devices_pq_.empty()) | 547 while (!input_devices_pq_.empty()) |
528 input_devices_pq_.pop(); | 548 input_devices_pq_.pop(); |
529 while (!output_devices_pq_.empty()) | 549 while (!output_devices_pq_.empty()) |
530 output_devices_pq_.pop(); | 550 output_devices_pq_.pop(); |
531 | 551 |
532 for (size_t i = 0; i < nodes.size(); ++i) { | 552 for (size_t i = 0; i < nodes.size(); ++i) { |
(...skipping 12 matching lines...) Expand all Loading... |
545 | 565 |
546 if (device.is_input) | 566 if (device.is_input) |
547 input_devices_pq_.push(device); | 567 input_devices_pq_.push(device); |
548 else | 568 else |
549 output_devices_pq_.push(device); | 569 output_devices_pq_.push(device); |
550 } | 570 } |
551 | 571 |
552 // If audio nodes change is caused by unplugging some non-active audio | 572 // If audio nodes change is caused by unplugging some non-active audio |
553 // devices, the previously set active audio device will stay active. | 573 // devices, the previously set active audio device will stay active. |
554 // Otherwise, switch to a new active audio device according to their priority. | 574 // Otherwise, switch to a new active audio device according to their priority. |
555 if (!NonActiveDeviceUnplugged(old_audio_devices_size, | 575 if (input_devices_changed && |
| 576 !NonActiveDeviceUnplugged(old_audio_devices_size, |
556 audio_devices_.size(), | 577 audio_devices_.size(), |
557 active_input_node_id_) && | 578 active_input_node_id_) && |
558 !input_devices_pq_.empty()) | 579 !input_devices_pq_.empty()) |
559 SwitchToDevice(input_devices_pq_.top()); | 580 SwitchToDevice(input_devices_pq_.top()); |
560 if (!NonActiveDeviceUnplugged(old_audio_devices_size, | 581 if (output_devices_changed && |
| 582 !NonActiveDeviceUnplugged(old_audio_devices_size, |
561 audio_devices_.size(), | 583 audio_devices_.size(), |
562 active_output_node_id_) && | 584 active_output_node_id_) && |
563 !output_devices_pq_.empty()) { | 585 !output_devices_pq_.empty()) { |
564 SwitchToDevice(output_devices_pq_.top()); | 586 SwitchToDevice(output_devices_pq_.top()); |
565 } | 587 } |
566 } | 588 } |
567 | 589 |
568 void CrasAudioHandler::HandleGetNodes(const chromeos::AudioNodeList& node_list, | 590 void CrasAudioHandler::HandleGetNodes(const chromeos::AudioNodeList& node_list, |
569 bool success) { | 591 bool success) { |
570 if (!success) { | 592 if (!success) { |
571 LOG(ERROR) << "Failed to retrieve audio nodes data"; | 593 LOG(ERROR) << "Failed to retrieve audio nodes data"; |
572 return; | 594 return; |
573 } | 595 } |
574 | 596 |
575 UpdateDevicesAndSwitchActive(node_list); | 597 UpdateDevicesAndSwitchActive(node_list); |
576 FOR_EACH_OBSERVER(AudioObserver, observers_, OnAudioNodesChanged()); | 598 FOR_EACH_OBSERVER(AudioObserver, observers_, OnAudioNodesChanged()); |
577 } | 599 } |
578 | 600 |
579 } // namespace chromeos | 601 } // namespace chromeos |
OLD | NEW |