Make StreamWindow create the Session

This commit is contained in:
Florian Märkl 2019-08-03 14:01:55 +02:00
parent c34ce875ba
commit 8e15f498c2
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
6 changed files with 71 additions and 42 deletions

View file

@ -43,6 +43,16 @@ class ChiakiException : public std::exception
const char *what() const noexcept override { return msg.toLocal8Bit().constData(); } const char *what() const noexcept override { return msg.toLocal8Bit().constData(); }
}; };
struct StreamSessionConnectInfo
{
QString host;
QString registkey;
QString ostype;
QString auth;
QString morning;
QString did;
};
class StreamSession : public QObject class StreamSession : public QObject
{ {
friend class StreamSessionPrivate; friend class StreamSessionPrivate;
@ -67,7 +77,7 @@ class StreamSession : public QObject
void PushVideoSample(uint8_t *buf, size_t buf_size); void PushVideoSample(uint8_t *buf, size_t buf_size);
public: public:
explicit StreamSession(const QString &host, const QString &registkey, const QString &ostype, const QString &auth, const QString &morning, const QString &did, QObject *parent = nullptr); explicit StreamSession(const StreamSessionConnectInfo &connect_info, QObject *parent = nullptr);
~StreamSession(); ~StreamSession();
void Stop(); void Stop();

View file

@ -20,15 +20,16 @@
#include <QMainWindow> #include <QMainWindow>
#include "streamsession.h"
class QLabel; class QLabel;
class StreamSession;
class StreamWindow: public QMainWindow class StreamWindow: public QMainWindow
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit StreamWindow(StreamSession *session, QWidget *parent = nullptr); explicit StreamWindow(const StreamSessionConnectInfo &connect_info, QWidget *parent = nullptr);
~StreamWindow(); ~StreamWindow();
private: private:

View file

@ -17,7 +17,7 @@
#include <QCommandLineParser> #include <QCommandLineParser>
int RunStream(QApplication &app, const QString &host, const QString &registkey, const QString &ostype, const QString &auth, const QString &morning, const QString &did); int RunStream(QApplication &app, const StreamSessionConnectInfo &connect_info);
int RunMain(QApplication &app); int RunMain(QApplication &app);
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -60,14 +60,16 @@ int main(int argc, char *argv[])
if(args[0] == "stream") if(args[0] == "stream")
{ {
QString registkey = parser.value(regist_key_option); StreamSessionConnectInfo connect_info;
QString ostype = parser.value(ostype_option); connect_info.host = host;
QString auth = parser.value(auth_option); connect_info.registkey = parser.value(regist_key_option);
QString morning = parser.value(morning_option); connect_info.ostype = parser.value(ostype_option);
QString did = parser.value(did_option); connect_info.auth = parser.value(auth_option);
if(registkey.isEmpty() || ostype.isEmpty() || auth.isEmpty() || morning.isEmpty() || did.isEmpty()) connect_info.morning = parser.value(morning_option);
connect_info.did = parser.value(did_option);
if(connect_info.registkey.isEmpty() || connect_info.ostype.isEmpty() || connect_info.auth.isEmpty() || connect_info.morning.isEmpty() || connect_info.did.isEmpty())
parser.showHelp(1); parser.showHelp(1);
return RunStream(app, host, registkey, ostype, auth, morning, did); return RunStream(app, connect_info);
} }
else if(args[0] == "discover") else if(args[0] == "discover")
{ {
@ -88,11 +90,9 @@ int RunMain(QApplication &app)
int RunStream(QApplication &app, const QString &host, const QString &registkey, const QString &ostype, const QString &auth, const QString &morning, const QString &did) int RunStream(QApplication &app, const StreamSessionConnectInfo &connect_info)
{ {
StreamSession stream_session(host, registkey, ostype, auth, morning, did); StreamWindow window(connect_info);
StreamWindow window(&stream_session);
window.resize(640, 360); window.resize(640, 360);
window.show(); window.show();

View file

@ -33,39 +33,39 @@
static void AudioFrameCb(int16_t *buf, size_t samples_count, void *user); static void AudioFrameCb(int16_t *buf, size_t samples_count, void *user);
static void VideoSampleCb(uint8_t *buf, size_t buf_size, void *user); static void VideoSampleCb(uint8_t *buf, size_t buf_size, void *user);
StreamSession::StreamSession(const QString &host, const QString &registkey, const QString &ostype, const QString &auth, const QString &morning, const QString &did, QObject *parent) StreamSession::StreamSession(const StreamSessionConnectInfo &connect_info, QObject *parent)
: QObject(parent) : QObject(parent)
#if CHIAKI_GUI_ENABLE_QT_GAMEPAD #if CHIAKI_GUI_ENABLE_QT_GAMEPAD
,gamepad(nullptr) ,gamepad(nullptr)
#endif #endif
{ {
QByteArray host_str = host.toUtf8(); QByteArray host_str = connect_info.host.toUtf8();
QByteArray registkey_str = registkey.toUtf8(); QByteArray registkey_str = connect_info.registkey.toUtf8();
QByteArray ostype_str = ostype.toUtf8(); QByteArray ostype_str = connect_info.ostype.toUtf8();
ChiakiConnectInfo connect_info; ChiakiConnectInfo chiaki_connect_info;
connect_info.host = host_str.constData(); chiaki_connect_info.host = host_str.constData();
connect_info.regist_key = registkey_str.constData(); chiaki_connect_info.regist_key = registkey_str.constData();
connect_info.ostype = ostype_str.constData(); chiaki_connect_info.ostype = ostype_str.constData();
QByteArray auth_str = auth.toUtf8(); QByteArray auth_str = connect_info.auth.toUtf8();
size_t auth_len = auth_str.length(); size_t auth_len = auth_str.length();
if(auth_len > sizeof(connect_info.auth)) if(auth_len > sizeof(chiaki_connect_info.auth))
auth_len = sizeof(connect_info.auth); auth_len = sizeof(chiaki_connect_info.auth);
memcpy(connect_info.auth, auth_str.constData(), auth_len); memcpy(chiaki_connect_info.auth, auth_str.constData(), auth_len);
if(auth_len < sizeof(connect_info.auth)) if(auth_len < sizeof(chiaki_connect_info.auth))
memset(connect_info.auth + auth_len, 0, sizeof(connect_info.auth) - auth_len); memset(chiaki_connect_info.auth + auth_len, 0, sizeof(chiaki_connect_info.auth) - auth_len);
size_t morning_size = sizeof(connect_info.morning); size_t morning_size = sizeof(chiaki_connect_info.morning);
QByteArray morning_str = morning.toUtf8(); QByteArray morning_str = connect_info.morning.toUtf8();
ChiakiErrorCode err = chiaki_base64_decode(morning_str.constData(), morning_str.length(), connect_info.morning, &morning_size); ChiakiErrorCode err = chiaki_base64_decode(morning_str.constData(), morning_str.length(), chiaki_connect_info.morning, &morning_size);
if(err != CHIAKI_ERR_SUCCESS || morning_size != sizeof(connect_info.morning)) if(err != CHIAKI_ERR_SUCCESS || morning_size != sizeof(chiaki_connect_info.morning))
throw ChiakiException("Morning invalid"); throw ChiakiException("Morning invalid");
size_t did_size = sizeof(connect_info.did); size_t did_size = sizeof(chiaki_connect_info.did);
QByteArray did_str = did.toUtf8(); QByteArray did_str = connect_info.did.toUtf8();
err = chiaki_base64_decode(did_str.constData(), did_str.length(), connect_info.did, &did_size); err = chiaki_base64_decode(did_str.constData(), did_str.length(), chiaki_connect_info.did, &did_size);
if(err != CHIAKI_ERR_SUCCESS || did_size != sizeof(connect_info.did)) if(err != CHIAKI_ERR_SUCCESS || did_size != sizeof(chiaki_connect_info.did))
throw ChiakiException("Did invalid"); throw ChiakiException("Did invalid");
// TODO: move audio init out of here and use values from audio header // TODO: move audio init out of here and use values from audio header
@ -87,9 +87,9 @@ StreamSession::StreamSession(const QString &host, const QString &registkey, cons
memset(&keyboard_state, 0, sizeof(keyboard_state)); memset(&keyboard_state, 0, sizeof(keyboard_state));
err = chiaki_session_init(&session, &connect_info); err = chiaki_session_init(&session, &chiaki_connect_info);
if(err != CHIAKI_ERR_SUCCESS) if(err != CHIAKI_ERR_SUCCESS)
throw ChiakiException("Chiaki Session Init failed"); throw ChiakiException("Chiaki Session Init failed: " + QString::fromLocal8Bit(chiaki_error_string(err)));
chiaki_session_set_audio_frame_cb(&session, AudioFrameCb, this); chiaki_session_set_audio_frame_cb(&session, AudioFrameCb, this);
chiaki_session_set_video_sample_cb(&session, VideoSampleCb, this); chiaki_session_set_video_sample_cb(&session, VideoSampleCb, this);

View file

@ -19,10 +19,10 @@
#include <streamsession.h> #include <streamsession.h>
#include <QLabel> #include <QLabel>
#include <QMessageBox>
StreamWindow::StreamWindow(StreamSession *session, QWidget *parent) StreamWindow::StreamWindow(const StreamSessionConnectInfo &connect_info, QWidget *parent)
: QMainWindow(parent), : QMainWindow(parent)
session(session)
{ {
imageLabel = new QLabel(this); imageLabel = new QLabel(this);
setCentralWidget(imageLabel); setCentralWidget(imageLabel);
@ -31,6 +31,8 @@ StreamWindow::StreamWindow(StreamSession *session, QWidget *parent)
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored); imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
imageLabel->setScaledContents(true); imageLabel->setScaledContents(true);
session = new StreamSession(connect_info, this);
connect(session->GetVideoDecoder(), &VideoDecoder::FramesAvailable, this, &StreamWindow::FramesAvailable); connect(session->GetVideoDecoder(), &VideoDecoder::FramesAvailable, this, &StreamWindow::FramesAvailable);
FramesAvailable(); FramesAvailable();

View file

@ -34,6 +34,22 @@ CHIAKI_EXPORT const char *chiaki_error_string(ChiakiErrorCode code)
return "Network error"; return "Network error";
case CHIAKI_ERR_INVALID_DATA: case CHIAKI_ERR_INVALID_DATA:
return "Invalid data"; return "Invalid data";
case CHIAKI_ERR_BUF_TOO_SMALL:
return "Buffer too small";
case CHIAKI_ERR_MUTEX_LOCKED:
return "Mutex is locked";
case CHIAKI_ERR_CANCELED:
return "Canceled";
case CHIAKI_ERR_TIMEOUT:
return "Timeout";
case CHIAKI_ERR_INVALID_RESPONSE:
return "Invalid Response";
case CHIAKI_ERR_INVALID_MAC:
return "Invalid MAC";
case CHIAKI_ERR_UNINITIALIZED:
return "Uninitialized";
case CHIAKI_ERR_FEC_FAILED:
return "FEC failed";
default: default:
return "Unknown"; return "Unknown";
} }