kiran-cc-daemon/0002-feature-audio-Try-reconnection-if-failed-to-connect-.patch
tangjie02 17bae3209d fix(audio): Fix the coredump as failed to connect pulaseaudio service.
Signed-off-by: tangjie02 <tangjie02@kylinsec.com.cn>
2022-07-12 09:19:52 +08:00

185 lines
6.9 KiB
Diff
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

From 7a35e9c6b3a8ec4eb45b708b32b175e1f4419983 Mon Sep 17 00:00:00 2001
From: tangjie02 <tangjie02@kylinsec.com.cn>
Date: Mon, 11 Jul 2022 21:00:16 +0800
Subject: [PATCH 2/2] feature(audio): Try reconnection if failed to connect
pulseaudio service firstly.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 如果第一次链接puluseaudio失败也进行重新连接尝试因为pulseaudio可能在kiran-session-daemon之后启动
Signed-off-by: tangjie02 <tangjie02@kylinsec.com.cn>
---
plugins/audio/pulse/pulse-backend.cpp | 50 ++++++++++++++-------------
plugins/audio/pulse/pulse-backend.h | 4 +--
plugins/audio/pulse/pulse-context.cpp | 6 ++--
3 files changed, 32 insertions(+), 28 deletions(-)
diff --git a/plugins/audio/pulse/pulse-backend.cpp b/plugins/audio/pulse/pulse-backend.cpp
index e1844dc..5190eb4 100644
--- a/plugins/audio/pulse/pulse-backend.cpp
+++ b/plugins/audio/pulse/pulse-backend.cpp
@@ -16,8 +16,10 @@
namespace Kiran
{
+#define MAX_RECONNECTION_NUM 50
+
PulseBackend::PulseBackend() : state_(AudioState::AUDIO_STATE_IDLE),
- connected_once_(false),
+ reconnection_count_(0),
reconnection_handle_(0)
{
this->context_ = std::make_shared<PulseContext>();
@@ -99,7 +101,7 @@ bool PulseBackend::init()
this->set_state(AudioState::AUDIO_STATE_CONNECTING);
- if (!this->context_->connect(false))
+ if (!this->context_->connect(true))
{
this->set_state(AudioState::AUDIO_STATE_FAILED);
return false;
@@ -110,7 +112,7 @@ bool PulseBackend::init()
void PulseBackend::set_state(AudioState state)
{
- KLOG_PROFILE("state: %d.", state);
+ KLOG_DEBUG("Audio state: %d.", state);
if (this->state_ != state)
{
@@ -121,7 +123,16 @@ void PulseBackend::set_state(AudioState state)
bool PulseBackend::try_reconnection()
{
- KLOG_PROFILE("");
+ ++this->reconnection_count_;
+
+ KLOG_DEBUG("Try to reconnect pulseaudio service. reconnection count: %d.", this->reconnection_count_);
+
+ if (this->reconnection_count_ > MAX_RECONNECTION_NUM)
+ {
+ KLOG_WARNING("The maximum number of reconnections (%d) has been exceeded. Stop reconnection", MAX_RECONNECTION_NUM);
+ this->reconnection_handle_ = 0;
+ return G_SOURCE_REMOVE;
+ }
if (this->context_->connect(true))
{
@@ -168,38 +179,28 @@ void PulseBackend::reset_data()
void PulseBackend::on_connection_state_changed_cb(PulseConnectionState connection_state)
{
- KLOG_PROFILE("connection state: %d.", connection_state);
+ KLOG_DEBUG("Connection state: %d.", connection_state);
switch (connection_state)
{
case PulseConnectionState::PULSE_CONNECTION_DISCONNECTED:
{
- // 如果之前已经成功连接过一次,此时突然断开了连接,则重新进行连接
// 重新连接之前需要清理掉之前的数据需要测试一下重启pulseaudio服务程序会不会出问题
this->reset_data();
+ this->set_state(AudioState::AUDIO_STATE_CONNECTING);
- if (this->connected_once_)
+ if (this->reconnection_handle_)
{
- this->set_state(AudioState::AUDIO_STATE_CONNECTING);
-
- if (this->reconnection_handle_)
- {
- KLOG_DEBUG("The reconnection handle is already exist. handle: %d.", this->reconnection_handle_);
- break;
- }
-
- if (!this->context_->connect(true))
- {
- auto timeout_source = Glib::TimeoutSource::create(200);
- timeout_source->connect(sigc::mem_fun(this, &PulseBackend::try_reconnection));
- auto glib_context = Glib::wrap(g_main_context_get_thread_default());
- this->reconnection_handle_ = timeout_source->attach(glib_context);
- }
+ KLOG_DEBUG("The reconnection handle is already exist. handle: %d.", this->reconnection_handle_);
}
else
{
- this->set_state(AudioState::AUDIO_STATE_FAILED);
+ auto timeout_source = Glib::TimeoutSource::create(400);
+ timeout_source->connect(sigc::mem_fun(this, &PulseBackend::try_reconnection));
+ auto glib_context = Glib::wrap(g_main_context_get_thread_default());
+ this->reconnection_handle_ = timeout_source->attach(glib_context);
}
+
break;
}
case PulseConnectionState::PULSE_CONNECTION_CONNECTING:
@@ -209,7 +210,8 @@ void PulseBackend::on_connection_state_changed_cb(PulseConnectionState connectio
break;
case PulseConnectionState::PULSE_CONNECTION_CONNECTED:
{
- this->connected_once_ = true;
+ // 如果连接成功重连次数清0
+ this->reconnection_count_ = 0;
this->set_state(AudioState::AUDIO_STATE_READY);
break;
}
diff --git a/plugins/audio/pulse/pulse-backend.h b/plugins/audio/pulse/pulse-backend.h
index 5494c32..1d27bf2 100644
--- a/plugins/audio/pulse/pulse-backend.h
+++ b/plugins/audio/pulse/pulse-backend.h
@@ -171,8 +171,8 @@ private:
// 可用状态
AudioState state_;
- // 是否成功连接过一次
- bool connected_once_;
+ // 重新连接次数
+ int32_t reconnection_count_;
uint32_t reconnection_handle_;
PulseServerInfo server_info_;
diff --git a/plugins/audio/pulse/pulse-context.cpp b/plugins/audio/pulse/pulse-context.cpp
index 699b7c6..eb3e8a5 100644
--- a/plugins/audio/pulse/pulse-context.cpp
+++ b/plugins/audio/pulse/pulse-context.cpp
@@ -56,7 +56,7 @@ PulseContext::~PulseContext()
bool PulseContext::connect(bool wait_for_daemon)
{
- KLOG_PROFILE("wait for deamon: %d.", wait_for_daemon);
+ KLOG_DEBUG("Wait for deamon: %d.", wait_for_daemon);
RETURN_VAL_IF_FALSE(this->main_loop_ != NULL, false);
@@ -75,7 +75,6 @@ bool PulseContext::connect(bool wait_for_daemon)
pa_context_set_state_callback(this->context_, &PulseContext::on_pulse_state_cb, this);
pa_context_flags_t flags = wait_for_daemon ? PA_CONTEXT_NOFAIL : PA_CONTEXT_NOFLAGS;
-
if (pa_context_connect(this->context_, NULL, flags, NULL) == 0)
{
this->set_connection_state(PulseConnectionState::PULSE_CONNECTION_CONNECTING);
@@ -83,6 +82,7 @@ bool PulseContext::connect(bool wait_for_daemon)
}
else
{
+ KLOG_WARNING("Failed to connect pulseaudio service.");
// on_pulse_state_cb回调函数可能已经进行了释放操作所以这里需要进一步判断
if (this->context_)
{
@@ -569,6 +569,8 @@ void PulseContext::on_pulse_state_cb(pa_context *context, void *userdata)
PulseContext *self = (PulseContext *)(userdata);
auto state = pa_context_get_state(self->context_);
+ KLOG_DEBUG("Pulse state change, state: %d.", state);
+
if (state == PA_CONTEXT_READY)
{
if (self->connection_state_ == PULSE_CONNECTION_LOADING || self->connection_state_ == PULSE_CONNECTION_CONNECTED)
--
2.33.0