Merge plainbox-provider-checkbox:fix-audio-test-get-source-sink-ids into plainbox-provider-checkbox:master

Proposed by Yao Wei
Status: Needs review
Proposed branch: plainbox-provider-checkbox:fix-audio-test-get-source-sink-ids
Merge into: plainbox-provider-checkbox:master
Diff against target: 103 lines (+30/-34)
1 file modified
bin/audio_test.py (+30/-34)
Reviewer Review Type Date Requested Status
Checkbox Developers Pending
Review via email: mp+431179@code.launchpad.net

Commit message

bin:audio_test.py: use pactl get-default-source and get-default-sink

This is to make getting default source/sink more reliable instead of manually filtering through the list.

To post a comment you must log in.
Revision history for this message
Kai-Chuan Hsieh (kchsieh) wrote :

Hello Yao,

I tested on KRPL-SFF-DVT-C1_202208-30552, it will show
ubuntu@ubuntu:~$ pactl get-default-sink
alsa_output.pci-0000_00_1f.3.analog-stereo
ubuntu@ubuntu:~$ pactl get-default-source
alsa_output.pci-0000_00_1f.3.analog-stereo.monitor

The original logic seems filter out monitor.
Will it impact the result if you just return the output of $ pactl get-default-{sink,source}?

Thanks,

Revision history for this message
Yao Wei (medicalwei) wrote (last edit ):

The command that controls the volume and unmutes

pactl set-source-volume
pactl set-source-mute
pactl set-sink-volume
pactl set-sink-mute

can actually use the identifier string (instead of numeric ID) to change the settings.

However, your concern is correct that it should disregard .monitor from the default source. It's now updated that if default source contains .monitor in the end of the string, it would return None.

Revision history for this message
jeremyszu (os369510) wrote :

@Wei,

This seems good to me, could you please move it to
https://github.com/canonical/checkbox
?

Revision history for this message
Atlas Yu (pseudoc) wrote :

I run the related test job on some platform, and the unmute logic is not working properly, and the get-default-{source,sink} could solve this problem perfectly.

Unmerged commits

47ff52c... by Yao Wei

bin:audio_test.py: use pactl get-default-source and get-default-sink

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/bin/audio_test.py b/bin/audio_test.py
index 4b3f89c..c229a4d 100755
--- a/bin/audio_test.py
+++ b/bin/audio_test.py
@@ -128,10 +128,12 @@ class PAVolumeController(object):
128 return False128 return False
129 if not self.identifier:129 if not self.identifier:
130 return False130 return False
131 command = ['pactl',131 command = [
132 'set-%s-volume' % (self.pa_types[self.type]),132 "pactl",
133 str(self.identifier[0]),133 "set-%s-volume" % (self.pa_types[self.type]),
134 str(int(volume)) + "%"]134 self.identifier,
135 str(int(volume)) + "%",
136 ]
135 self._pactl_output(command)137 self._pactl_output(command)
136 self._volume = volume138 self._volume = volume
137 return True139 return True
@@ -145,10 +147,12 @@ class PAVolumeController(object):
145 mute = str(int(mute))147 mute = str(int(mute))
146 if not self.identifier:148 if not self.identifier:
147 return False149 return False
148 command = ['pactl',150 command = [
149 'set-%s-mute' % (self.pa_types[self.type]),151 "pactl",
150 str(self.identifier[0]),152 "set-%s-mute" % (self.pa_types[self.type]),
151 mute]153 self.identifier,
154 mute,
155 ]
152 self._pactl_output(command)156 self._pactl_output(command)
153 return True157 return True
154158
@@ -156,46 +160,38 @@ class PAVolumeController(object):
156 if self.type:160 if self.type:
157 self.identifier = self._get_identifier_for(self.type)161 self.identifier = self._get_identifier_for(self.type)
158 if self.identifier and self.logger:162 if self.identifier and self.logger:
159 message = "Using PulseAudio identifier %s (%s) for %s" %\163 message = "Using PulseAudio identifier %s for %s" % (
160 (self.identifier + (self.type,))164 self.identifier,
165 self.type,
166 )
161 self.logger.info(message)167 self.logger.info(message)
162 return self.identifier168 return self.identifier
163169
164 def _get_identifier_for(self, type):170 def _get_identifier_for(self, type):
165 """Gets default PulseAudio identifier for given type.171 """Gets default PulseAudio identifier for given type.
166172
167 Arguments:173 Arguments:
168 type: either input or output174 type: either input or output
169175
170 Returns:176 Returns:
171 A tuple: (pa_id, pa_description)177 A string: device_name
172178
173 """179 """
174180
175 if type not in self.pa_types:181 if type not in self.pa_types:
176 return None182 return None
177 command = ['pactl', 'list', self.pa_types[type] + "s", 'short']183
178184 command = ["pactl", "get-default-" + self.pa_types[type]]
179 # Expect lines of this form (field separator is tab):185 name = self._pactl_output(command).strip()
180 # <ID>\t<NAME>\t<MODULE>\t<SAMPLE_SPEC_WITH_SPACES>\t<STATE>186 if name.startswith("auto_null") or name.endswith(".monitor"):
181 # What we need to return is the ID for the first element on this list
182 # that does not contain auto_null or monitor.
183 pa_info = self._pactl_output(command)
184 valid_elements = None
185
186 if pa_info:
187 reject_regex = '.*(monitor|auto_null).*'
188 valid_elements = [element for element in pa_info.splitlines()
189 if not re.match(reject_regex, element)]
190 if not valid_elements:
191 if self.logger:187 if self.logger:
192 self.logger.error("No valid PulseAudio elements"188 self.logger.error(
193 " for %s" % (self.type))189 "Default PulseAudio element for %s is invalid (%s)"
190 % (self.type, name)
191 )
194 return None192 return None
195 # We only need the pulseaudio numeric ID and long name for each element193
196 valid_elements = [(int(e.split()[0]), e.split()[1])194 return name
197 for e in valid_elements]
198 return valid_elements[0]
199195
200 def _pactl_output(self, command):196 def _pactl_output(self, command):
201 # This method mainly calls pactl (hence the name). Since pactl may197 # This method mainly calls pactl (hence the name). Since pactl may

Subscribers

People subscribed via source and target branches

to all changes: