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" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "chromeos/audio/audio_devices_pref_handler.h" | 13 #include "chromeos/audio/audio_devices_pref_handler.h" |
14 #include "chromeos/audio/audio_devices_pref_handler_stub.h" | 14 #include "chromeos/audio/audio_devices_pref_handler_stub.h" |
15 #include "chromeos/dbus/dbus_thread_manager.h" | 15 #include "chromeos/dbus/dbus_thread_manager.h" |
16 #include "media/audio/audio_manager.h" | |
16 | 17 |
17 using std::max; | 18 using std::max; |
18 using std::min; | 19 using std::min; |
19 | 20 |
20 namespace chromeos { | 21 namespace chromeos { |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 // Default value for unmuting, as a percent in the range [0, 100]. | 25 // Default value for unmuting, as a percent in the range [0, 100]. |
25 // Used when sound is unmuted, but volume was less than kMuteThresholdPercent. | 26 // Used when sound is unmuted, but volume was less than kMuteThresholdPercent. |
26 const int kDefaultUnmuteVolumePercent = 4; | 27 const int kDefaultUnmuteVolumePercent = 4; |
27 | 28 |
28 // Volume value which should be considered as muted in range [0, 100]. | 29 // Volume value which should be considered as muted in range [0, 100]. |
29 const int kMuteThresholdPercent = 1; | 30 const int kMuteThresholdPercent = 1; |
30 | 31 |
31 static CrasAudioHandler* g_cras_audio_handler = NULL; | 32 static CrasAudioHandler* g_cras_audio_handler = NULL; |
32 | 33 |
34 // audio_manager will point to a AudioManagerWrapperImpl object or | |
35 // a mocked AudioManagerWrapper object for testing. | |
36 static CrasAudioHandler::AudioManagerWrapper* audio_manager = NULL; | |
Daniel Erat
2015/06/23 14:15:49
does this really need to be a global, or could it
cychiang
2015/06/24 08:11:29
Done.
Now this manager needs to be passed through
| |
37 | |
33 bool IsSameAudioDevice(const AudioDevice& a, const AudioDevice& b) { | 38 bool IsSameAudioDevice(const AudioDevice& a, const AudioDevice& b) { |
34 return a.id == b.id && a.is_input == b.is_input && a.type == b.type | 39 return a.id == b.id && a.is_input == b.is_input && a.type == b.type |
35 && a.device_name == b.device_name; | 40 && a.device_name == b.device_name; |
36 } | 41 } |
37 | 42 |
38 bool IsInNodeList(uint64_t node_id, | 43 bool IsInNodeList(uint64_t node_id, |
39 const CrasAudioHandler::NodeIdList& id_list) { | 44 const CrasAudioHandler::NodeIdList& id_list) { |
40 return std::find(id_list.begin(), id_list.end(), node_id) != id_list.end(); | 45 return std::find(id_list.begin(), id_list.end(), node_id) != id_list.end(); |
41 } | 46 } |
42 | 47 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
74 | 79 |
75 void CrasAudioHandler::AudioObserver::OnAudioNodesChanged() { | 80 void CrasAudioHandler::AudioObserver::OnAudioNodesChanged() { |
76 } | 81 } |
77 | 82 |
78 void CrasAudioHandler::AudioObserver::OnActiveOutputNodeChanged() { | 83 void CrasAudioHandler::AudioObserver::OnActiveOutputNodeChanged() { |
79 } | 84 } |
80 | 85 |
81 void CrasAudioHandler::AudioObserver::OnActiveInputNodeChanged() { | 86 void CrasAudioHandler::AudioObserver::OnActiveInputNodeChanged() { |
82 } | 87 } |
83 | 88 |
89 CrasAudioHandler::AudioManagerWrapper::AudioManagerWrapper() { | |
90 } | |
91 CrasAudioHandler::AudioManagerWrapper::~AudioManagerWrapper() { | |
92 } | |
93 | |
94 CrasAudioHandler::AudioManagerWrapperImpl::AudioManagerWrapperImpl() { | |
95 } | |
96 CrasAudioHandler::AudioManagerWrapperImpl::~AudioManagerWrapperImpl() { | |
97 } | |
98 | |
99 void CrasAudioHandler::AudioManagerWrapperImpl::SetHasInputDevices( | |
100 bool has_input_devices) { | |
101 media::AudioManager::Get()->SetHasInputDevices(has_input_devices); | |
102 } | |
103 | |
104 // static | |
105 // Set audio manager wrapper for test. Must be called before Initialize | |
Daniel Erat
2015/06/23 14:15:49
delete this comment; it's already present in the h
cychiang
2015/06/24 08:11:29
Done.
| |
106 // for testing. CrasAudioHandler will delete this pointer at Shutdown. | |
107 void CrasAudioHandler::SetUpAudioManagerWrapperForTesting( | |
108 AudioManagerWrapper* test_wrapper) { | |
109 DCHECK(!audio_manager); | |
110 audio_manager = test_wrapper; | |
111 } | |
112 | |
84 // static | 113 // static |
85 void CrasAudioHandler::Initialize( | 114 void CrasAudioHandler::Initialize( |
86 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler) { | 115 scoped_refptr<AudioDevicesPrefHandler> audio_pref_handler) { |
87 CHECK(!g_cras_audio_handler); | 116 CHECK(!g_cras_audio_handler); |
88 g_cras_audio_handler = new CrasAudioHandler(audio_pref_handler); | 117 g_cras_audio_handler = new CrasAudioHandler(audio_pref_handler); |
118 // Creates an AudioManagerWrapperImpl if audio_manager is not set | |
119 // by SetUpAudioManagerWrapperForTesting. | |
120 if (!audio_manager) | |
121 audio_manager = new CrasAudioHandler::AudioManagerWrapperImpl(); | |
89 } | 122 } |
90 | 123 |
91 // static | 124 // static |
92 void CrasAudioHandler::InitializeForTesting() { | 125 void CrasAudioHandler::InitializeForTesting() { |
93 CHECK(!g_cras_audio_handler); | 126 CHECK(!g_cras_audio_handler); |
94 CrasAudioHandler::Initialize(new AudioDevicesPrefHandlerStub()); | 127 CrasAudioHandler::Initialize(new AudioDevicesPrefHandlerStub()); |
95 } | 128 } |
96 | 129 |
97 // static | 130 // static |
98 void CrasAudioHandler::Shutdown() { | 131 void CrasAudioHandler::Shutdown() { |
99 CHECK(g_cras_audio_handler); | 132 CHECK(g_cras_audio_handler); |
100 delete g_cras_audio_handler; | 133 delete g_cras_audio_handler; |
101 g_cras_audio_handler = NULL; | 134 g_cras_audio_handler = NULL; |
135 CHECK(audio_manager); | |
136 delete audio_manager; | |
137 audio_manager = NULL; | |
102 } | 138 } |
103 | 139 |
104 // static | 140 // static |
105 bool CrasAudioHandler::IsInitialized() { | 141 bool CrasAudioHandler::IsInitialized() { |
106 return g_cras_audio_handler != NULL; | 142 return g_cras_audio_handler != NULL; |
107 } | 143 } |
108 | 144 |
109 // static | 145 // static |
110 CrasAudioHandler* CrasAudioHandler::Get() { | 146 CrasAudioHandler* CrasAudioHandler::Get() { |
111 CHECK(g_cras_audio_handler) | 147 CHECK(g_cras_audio_handler) |
(...skipping 784 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
896 } else { | 932 } else { |
897 // ditto input node case. | 933 // ditto input node case. |
898 if (!active_output_node_id_ || hotplug_output_nodes.empty() || | 934 if (!active_output_node_id_ || hotplug_output_nodes.empty() || |
899 IsNodeInTheList(output_devices_pq_.top().id, hotplug_output_nodes)) { | 935 IsNodeInTheList(output_devices_pq_.top().id, hotplug_output_nodes)) { |
900 SwitchToDevice(output_devices_pq_.top(), true); | 936 SwitchToDevice(output_devices_pq_.top(), true); |
901 } | 937 } |
902 } | 938 } |
903 } | 939 } |
904 } | 940 } |
905 | 941 |
942 void CrasAudioHandler::SetAudioManagerHasInputDevices(bool has_input_devices) { | |
Daniel Erat
2015/06/23 14:15:49
this method is just one line; it seems simpler to
cychiang
2015/06/24 08:11:29
Done.
| |
943 audio_manager->SetHasInputDevices(has_input_devices); | |
944 } | |
945 | |
946 void CrasAudioHandler::UpdateAudioManagerHasInputDevices() { | |
947 AudioDeviceList devices; | |
948 GetAudioDevices(&devices); | |
949 for (size_t i = 0; i < devices.size(); ++i) { | |
950 if (devices[i].is_input && devices[i].IsForSimpleUsage()) { | |
951 SetAudioManagerHasInputDevices(true); | |
952 return; | |
953 } | |
954 } | |
955 SetAudioManagerHasInputDevices(false); | |
956 } | |
957 | |
906 void CrasAudioHandler::HandleGetNodes(const chromeos::AudioNodeList& node_list, | 958 void CrasAudioHandler::HandleGetNodes(const chromeos::AudioNodeList& node_list, |
907 bool success) { | 959 bool success) { |
908 if (!success) { | 960 if (!success) { |
909 LOG_IF(ERROR, log_errors_) << "Failed to retrieve audio nodes data"; | 961 LOG_IF(ERROR, log_errors_) << "Failed to retrieve audio nodes data"; |
910 return; | 962 return; |
911 } | 963 } |
912 | 964 |
913 UpdateDevicesAndSwitchActive(node_list); | 965 UpdateDevicesAndSwitchActive(node_list); |
966 UpdateAudioManagerHasInputDevices(); | |
914 FOR_EACH_OBSERVER(AudioObserver, observers_, OnAudioNodesChanged()); | 967 FOR_EACH_OBSERVER(AudioObserver, observers_, OnAudioNodesChanged()); |
915 } | 968 } |
916 | 969 |
917 void CrasAudioHandler::HandleGetNodesError(const std::string& error_name, | 970 void CrasAudioHandler::HandleGetNodesError(const std::string& error_name, |
918 const std::string& error_msg) { | 971 const std::string& error_msg) { |
919 LOG_IF(ERROR, log_errors_) << "Failed to call GetNodes: " | 972 LOG_IF(ERROR, log_errors_) << "Failed to call GetNodes: " |
920 << error_name << ": " << error_msg; | 973 << error_name << ": " << error_msg; |
921 } | 974 } |
922 | 975 |
923 void CrasAudioHandler::AddAdditionalActiveNode(uint64_t node_id, bool notify) { | 976 void CrasAudioHandler::AddAdditionalActiveNode(uint64_t node_id, bool notify) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
970 active_output_node_id_ = 0; | 1023 active_output_node_id_ = 0; |
971 chromeos::DBusThreadManager::Get() | 1024 chromeos::DBusThreadManager::Get() |
972 ->GetCrasAudioClient() | 1025 ->GetCrasAudioClient() |
973 ->RemoveActiveOutputNode(node_id); | 1026 ->RemoveActiveOutputNode(node_id); |
974 if (notify) | 1027 if (notify) |
975 NotifyActiveNodeChanged(false); | 1028 NotifyActiveNodeChanged(false); |
976 } | 1029 } |
977 } | 1030 } |
978 | 1031 |
979 } // namespace chromeos | 1032 } // namespace chromeos |
OLD | NEW |