Skip to content

Commit

Permalink
add a audio card resource
Browse files Browse the repository at this point in the history
  • Loading branch information
diohe0311 committed Jul 17, 2023
1 parent 0552b38 commit ba58ab8
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 4 deletions.
70 changes: 70 additions & 0 deletions providers/base/bin/audiocard_resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env python3
#
# This file is part of Checkbox.
#
# Copyright 2023 Canonical Ltd.
# Authors: Dio He <dio.he@canonical.com>
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox. If not, see <http://www.gnu.org/licenses/>.

import os


def get_audio_cards():
"""Retrieve audio card information."""
audio_cards = []
PCM_FILE = '/proc/asound/pcm'
if os.path.exists(PCM_FILE):
with open(PCM_FILE, 'r') as f:
for line in f:
t = [device_line.strip() for device_line in line.split(':')]
ids = t[0].split('-')
card_id = ids[0]
device_id = ids[1]
device_name = t[1]
capabilities = t[3:]
audio_cards.append({
'Card': card_id,
'Device': device_id,
'Name': device_name,
'Playback': 1 if any(cap.startswith('playback') for cap in capabilities) else 0,
'Capture': 1 if any(cap.startswith('capture') for cap in capabilities) else 0
})
return audio_cards


def print_audio_cards(cards):
"""Print audio card information."""
print("Audio Cards:")
for card in cards:
print(f"Card: {card['Card']}")
print(f"Device: {card['Device']}")
print(f"Name: {card['Name']}")
if card['Playback']:
print("Playback: 1")
if card['Capture']:
print("Capture: 1")
print()


def main():
cards = get_audio_cards()

if cards:
print_audio_cards(cards)
else:
print("No audio cards found.")


if __name__ == "__main__":
main()
6 changes: 3 additions & 3 deletions providers/base/units/audio/jobs.pxu
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ category_id: com.canonical.plainbox::audio
flags: also-after-suspend
imports: from com.canonical.plainbox import manifest
requires:
manifest.has_audio_playback == 'True'
audio_card.Playback == '1'
command:
COUNT=$(alsa_pcm_info.py | grep -c Playback)
echo "Count: $COUNT"
Expand All @@ -713,7 +713,7 @@ category_id: com.canonical.plainbox::audio
flags: also-after-suspend
imports: from com.canonical.plainbox import manifest
requires:
manifest.has_audio_capture == 'True'
audio_card.Capture == '1'
command:
COUNT=$(alsa_pcm_info.py | grep -c Capture)
echo "Count: $COUNT"
Expand Down Expand Up @@ -920,4 +920,4 @@ plugin: manual
flags: also-after-suspend
requires: snap.name == 'pulseaudio'
category_id: com.canonical.plainbox::audio
estimated_duration: 1m
estimated_duration: 1m
8 changes: 8 additions & 0 deletions providers/base/units/audio/resource.pxu
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ _description: Gather device info about alsa sound devices
command: alsa_pcm_info.py
estimated_duration: 1s
flags: preserve-locale

id: audio_card
category_id: com.canonical.plainbox::audio
plugin: resource
_summary: Collect information about the audio card
_description: Gets audio resource info from /proc/asound/pcm
command: audiocard_resource.py
estimated_duration: 1s
6 changes: 5 additions & 1 deletion providers/base/units/audio/test-plan.pxu
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ include:
audio/detect-playback-devices
audio/detect-capture-devices
audio/alsa-loopback-automated
bootstrap_include:
audio_card

id: after-suspend-audio-full
unit: test plan
Expand Down Expand Up @@ -164,4 +166,6 @@ _description: Automated audio tests for Snappy Ubuntu Core devices
include:
after-suspend-audio/detect-playback-devices
after-suspend-audio/detect-capture-devices
after-suspend-audio/alsa-loopback-automated
after-suspend-audio/alsa-loopback-automated
bootstrap_include:
audio_card

0 comments on commit ba58ab8

Please sign in to comment.