Make Stream Connection better stoppable

This commit is contained in:
Florian Märkl 2019-08-03 13:14:34 +02:00
commit 5d9f114e01
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
2 changed files with 20 additions and 16 deletions

View file

@ -142,6 +142,8 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_session_stop(ChiakiSession *session)
chiaki_stop_pipe_stop(&session->stop_pipe);
chiaki_cond_signal(&session->state_cond);
chiaki_stream_connection_stop(&session->stream_connection);
chiaki_mutex_unlock(&session->state_mutex);
return CHIAKI_ERR_SUCCESS;
}
@ -285,19 +287,14 @@ static void *session_thread_func(void *arg)
QUIT(quit_audio_receiver);
}
chiaki_mutex_unlock(&session->state_mutex);
err = chiaki_stream_connection_run(&session->stream_connection);
if(err != CHIAKI_ERR_SUCCESS)
{
chiaki_mutex_lock(&session->state_mutex);
if(err != CHIAKI_ERR_SUCCESS && err != CHIAKI_ERR_CANCELED)
CHIAKI_LOGE(&session->log, "StreamConnection run failed");
QUIT(quit_stream_connection);
}
else
CHIAKI_LOGI(&session->log, "StreamConnection completed successfully");
CHIAKI_LOGI(&session->log, "StreamConnection completed successfully");
quit_stream_connection:
chiaki_stream_connection_fini(&session->stream_connection);
quit_video_receiver:
chiaki_video_receiver_free(session->video_receiver);
session->video_receiver = NULL;
@ -318,6 +315,7 @@ quit:
return NULL;
#undef CHECK_STOP
#undef QUIT
}

View file

@ -142,6 +142,13 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_stream_connection_run(ChiakiStreamConnectio
err = chiaki_mutex_lock(&stream_connection->state_mutex);
assert(err == CHIAKI_ERR_SUCCESS);
#define CHECK_STOP(quit_label) do { \
if(stream_connection->should_stop) \
{ \
session->quit_reason = CHIAKI_QUIT_REASON_STOPPED; \
goto quit_label; \
} } while(0)
stream_connection->state = STATE_TAKION_CONNECT;
stream_connection->state_finished = false;
stream_connection->state_failed = false;
@ -154,10 +161,9 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_stream_connection_run(ChiakiStreamConnectio
return err;
}
// TODO: timeout?
err = chiaki_cond_wait_pred(&stream_connection->state_cond, &stream_connection->state_mutex, state_finished_cond_check, stream_connection);
err = chiaki_cond_timedwait_pred(&stream_connection->state_cond, &stream_connection->state_mutex, EXPECT_TIMEOUT_MS, state_finished_cond_check, stream_connection);
assert(err == CHIAKI_ERR_SUCCESS || err == CHIAKI_ERR_TIMEOUT);
// TODO: check stop
CHECK_STOP(close_takion);
if(err != CHIAKI_ERR_SUCCESS)
{
CHIAKI_LOGE(&session->log, "StreamConnection Takion connect failed");
@ -178,14 +184,13 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_stream_connection_run(ChiakiStreamConnectio
err = chiaki_cond_timedwait_pred(&stream_connection->state_cond, &stream_connection->state_mutex, EXPECT_TIMEOUT_MS, state_finished_cond_check, stream_connection);
assert(err == CHIAKI_ERR_SUCCESS || err == CHIAKI_ERR_TIMEOUT);
CHECK_STOP(disconnect);
if(!stream_connection->state_finished)
{
if(err == CHIAKI_ERR_TIMEOUT)
CHIAKI_LOGE(&session->log, "StreamConnection bang receive timeout");
// TODO: check canceled and don't report error
CHIAKI_LOGE(&session->log, "StreamConnection didn't receive bang or failed to handle it");
err = CHIAKI_ERR_UNKNOWN;
goto disconnect;
@ -198,6 +203,7 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_stream_connection_run(ChiakiStreamConnectio
stream_connection->state_failed = false;
err = chiaki_cond_timedwait_pred(&stream_connection->state_cond, &stream_connection->state_mutex, EXPECT_TIMEOUT_MS, state_finished_cond_check, stream_connection);
assert(err == CHIAKI_ERR_SUCCESS || err == CHIAKI_ERR_TIMEOUT);
CHECK_STOP(disconnect);
if(!stream_connection->state_finished)
{
@ -255,9 +261,9 @@ disconnect:
if(stream_connection->should_stop)
CHIAKI_LOGI(stream_connection->log, "StreamConnection was requested to stop");
close_takion:
chiaki_mutex_unlock(&stream_connection->state_mutex);
close_takion:
chiaki_takion_close(&stream_connection->takion);
CHIAKI_LOGI(&session->log, "StreamConnection closed takion");