From f9b944fafe33a721a14a42bd04dccaf90ced8868 Mon Sep 17 00:00:00 2001 From: mfenjiro <mohamed.fenjiro@savoirfairelinux.com> Date: Fri, 8 Feb 2019 11:48:50 -0500 Subject: [PATCH] alsalayer: cleanup and fixed class Change-Id: Ice2d49076b129c59e3cf6e8a6a70890379ebac71 --- src/media/audio/alsa/alsalayer.cpp | 133 +++++++++++++++-------------- src/media/audio/alsa/alsalayer.h | 8 +- 2 files changed, 73 insertions(+), 68 deletions(-) diff --git a/src/media/audio/alsa/alsalayer.cpp b/src/media/audio/alsa/alsalayer.cpp index 2fe3152e28..906fffc686 100644 --- a/src/media/audio/alsa/alsalayer.cpp +++ b/src/media/audio/alsa/alsalayer.cpp @@ -4,7 +4,8 @@ * Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com> * Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com> * Author: Андрей Лухнов <aol.nnov@gmail.com> - * + * Author: Mohamed Fenjiro <mohamed.fenjiro@savoirfairelinux.com> + * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or @@ -40,7 +41,6 @@ class AlsaThread { public: AlsaThread(AlsaLayer *alsa); ~AlsaThread(); - void initAudioLayer(); void start(); bool isRunning() const; @@ -74,70 +74,9 @@ void AlsaThread::start() thread_ = std::thread(&AlsaThread::run, this); } -void AlsaThread::initAudioLayer(void) -{ - std::string pcmp; - std::string pcmr; - std::string pcmc; - - if (alsa_->audioPlugin_ == PCM_DMIX_DSNOOP) { - pcmp = alsa_->buildDeviceTopo(PCM_DMIX, alsa_->indexOut_); - pcmr = alsa_->buildDeviceTopo(PCM_DMIX, alsa_->indexRing_); - pcmc = alsa_->buildDeviceTopo(PCM_DSNOOP, alsa_->indexIn_); - } else { - pcmp = alsa_->buildDeviceTopo(alsa_->audioPlugin_, alsa_->indexOut_); - pcmr = alsa_->buildDeviceTopo(alsa_->audioPlugin_, alsa_->indexRing_); - pcmc = alsa_->buildDeviceTopo(alsa_->audioPlugin_, alsa_->indexIn_); - } - - if (not alsa_->is_capture_open_) { - alsa_->is_capture_open_ = alsa_->openDevice(&alsa_->captureHandle_, pcmc, SND_PCM_STREAM_CAPTURE, alsa_->audioInputFormat_); - - if (not alsa_->is_capture_open_) - emitSignal<DRing::ConfigurationSignal::Error>(ALSA_CAPTURE_DEVICE); - } - - if (not alsa_->is_playback_open_) { - alsa_->is_playback_open_ = alsa_->openDevice(&alsa_->playbackHandle_, pcmp, SND_PCM_STREAM_PLAYBACK, alsa_->audioFormat_); - - if (not alsa_->is_playback_open_) - emitSignal<DRing::ConfigurationSignal::Error>(ALSA_PLAYBACK_DEVICE); - - if (alsa_->getIndexPlayback() != alsa_->getIndexRingtone()) - if (!alsa_->openDevice(&alsa_->ringtoneHandle_, pcmr, SND_PCM_STREAM_PLAYBACK, alsa_->audioFormat_)) - emitSignal<DRing::ConfigurationSignal::Error>(ALSA_PLAYBACK_DEVICE); - } - - alsa_->hardwareFormatAvailable(alsa_->getFormat()); - alsa_->hardwareInputFormatAvailable(alsa_->audioInputFormat_); - - alsa_->prepareCaptureStream(); - alsa_->preparePlaybackStream(); - - alsa_->startCaptureStream(); - alsa_->startPlaybackStream(); - - alsa_->flushMain(); - alsa_->flushUrgent(); -} - -/** - * Reimplementation of run() - */ void AlsaThread::run() { - initAudioLayer(); - { - std::lock_guard<std::mutex> lock(alsa_->mutex_); - alsa_->status_ = AudioLayer::Status::Started; - } - alsa_->startedCv_.notify_all(); - - while (alsa_->status_ == AudioLayer::Status::Started and running_) { - alsa_->playback(); - alsa_->ringtone(); - alsa_->capture(); - } + alsa_->run(); } AlsaLayer::AlsaLayer(const AudioPreference &pref) @@ -172,6 +111,72 @@ AlsaLayer::~AlsaLayer() closePlaybackStream(); } +void AlsaLayer::initAudioLayer() +{ + std::string pcmp; + std::string pcmr; + std::string pcmc; + + if (audioPlugin_ == PCM_DMIX_DSNOOP) { + pcmp = buildDeviceTopo(PCM_DMIX, indexOut_); + pcmr = buildDeviceTopo(PCM_DMIX, indexRing_); + pcmc = buildDeviceTopo(PCM_DSNOOP, indexIn_); + } else { + pcmp = buildDeviceTopo(audioPlugin_, indexOut_); + pcmr = buildDeviceTopo(audioPlugin_, indexRing_); + pcmc = buildDeviceTopo(audioPlugin_, indexIn_); + } + + if (not is_capture_open_) { + is_capture_open_ = openDevice(&captureHandle_, pcmc, SND_PCM_STREAM_CAPTURE, audioInputFormat_); + + if (not is_capture_open_) + emitSignal<DRing::ConfigurationSignal::Error>(ALSA_CAPTURE_DEVICE); + } + + if (not is_playback_open_) { + is_playback_open_ = openDevice(&playbackHandle_, pcmp, SND_PCM_STREAM_PLAYBACK, audioFormat_); + + if (not is_playback_open_) + emitSignal<DRing::ConfigurationSignal::Error>(ALSA_PLAYBACK_DEVICE); + + if (getIndexPlayback() != getIndexRingtone()) + if (!openDevice(&ringtoneHandle_, pcmr, SND_PCM_STREAM_PLAYBACK, audioFormat_)) + emitSignal<DRing::ConfigurationSignal::Error>(ALSA_PLAYBACK_DEVICE); + } + + hardwareFormatAvailable(getFormat()); + hardwareInputFormatAvailable(audioInputFormat_); + + prepareCaptureStream(); + preparePlaybackStream(); + + startCaptureStream(); + startPlaybackStream(); + + flushMain(); + flushUrgent(); +} + +/** + * Reimplementation of run() + */ +void AlsaLayer::run() +{ + initAudioLayer(); + { + std::lock_guard<std::mutex> lock(mutex_); + status_ = AudioLayer::Status::Started; + } + startedCv_.notify_all(); + + while (status_ == AudioLayer::Status::Started and audioThread_ and audioThread_->isRunning()) { + playback(); + ringtone(); + capture(); + } +} + // Retry approach taken from pa_linux_alsa.c, part of PortAudio bool AlsaLayer::openDevice(snd_pcm_t **pcm, const std::string &dev, snd_pcm_stream_t stream, AudioFormat& format) { diff --git a/src/media/audio/alsa/alsalayer.h b/src/media/audio/alsa/alsalayer.h index 902c777b76..aeb95ed504 100644 --- a/src/media/audio/alsa/alsalayer.h +++ b/src/media/audio/alsa/alsalayer.h @@ -142,8 +142,11 @@ class AlsaLayer : public AudioLayer { return indexRing_; } + void run(); + private: - friend class AlsaThread; + + void initAudioLayer(); /** * Returns a map of audio device hardware description and index */ @@ -231,9 +234,6 @@ class AlsaLayer : public AudioLayer { */ std::string audioPlugin_; - /** Vector to manage all soundcard index - description association of the system */ - // std::vector<HwIDPair> IDSoundCards_; - /** Non-interleaved audio buffers */ AudioBuffer playbackBuff_; AudioBuffer captureBuff_; -- GitLab