mirror of
https://github.com/niellun/FastCarPlay.git
synced 2026-06-07 09:38:25 +02:00
Better decoder, minor refactoring
This commit is contained in:
+10
-20
@@ -51,15 +51,13 @@ Connector::~Connector()
|
||||
}
|
||||
}
|
||||
|
||||
void Connector::start(IProtocol *protocol)
|
||||
void Connector::start()
|
||||
{
|
||||
_protocol = protocol;
|
||||
|
||||
if (_active)
|
||||
stop();
|
||||
|
||||
_active = true;
|
||||
_write_thread = std::thread(&Connector::write_loop, this);
|
||||
_write_thread = std::thread(&Connector::writeLoop, this);
|
||||
}
|
||||
|
||||
void Connector::stop()
|
||||
@@ -166,8 +164,8 @@ bool Connector::nextState(u_int8_t state)
|
||||
|
||||
void Connector::state(u_int8_t state)
|
||||
{
|
||||
if (nextState(state) && _protocol)
|
||||
_protocol->onStatus(state);
|
||||
if (nextState(state))
|
||||
onStatus(state);
|
||||
}
|
||||
|
||||
bool Connector::linkFail(int status, const char *msg)
|
||||
@@ -312,7 +310,7 @@ void Connector::printMessage(uint32_t cmd, uint32_t length, uint8_t *data, bool
|
||||
std::cout << oss.str() << std::endl;
|
||||
}
|
||||
|
||||
void Connector::read_loop()
|
||||
void Connector::readLoop()
|
||||
{
|
||||
std::mutex mtx;
|
||||
std::condition_variable cv;
|
||||
@@ -351,12 +349,6 @@ void Connector::read_loop()
|
||||
libusb_bulk_transfer(_device, _endpoint_in, data, header.length, &transferred, READ_TIMEOUT);
|
||||
}
|
||||
|
||||
if (!_protocol)
|
||||
{
|
||||
free(data);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (header.magic == MAGIC_ENC)
|
||||
{
|
||||
if (!_cipher)
|
||||
@@ -377,11 +369,11 @@ void Connector::read_loop()
|
||||
|
||||
if (padding > 0)
|
||||
std::fill(data + header.length, data + header.length + padding, 0);
|
||||
_protocol->onData(header.type, header.length, data);
|
||||
onData(header.type, header.length, data);
|
||||
}
|
||||
}
|
||||
|
||||
void Connector::write_loop()
|
||||
void Connector::writeLoop()
|
||||
{
|
||||
std::mutex mtx;
|
||||
std::condition_variable cv;
|
||||
@@ -397,9 +389,8 @@ void Connector::write_loop()
|
||||
{
|
||||
std::cout << "[Connection] Device connected" << std::endl;
|
||||
|
||||
_read_thread = std::thread(&Connector::read_loop, this);
|
||||
if (_protocol)
|
||||
_protocol->onDevice(true);
|
||||
_read_thread = std::thread(&Connector::readLoop, this);
|
||||
onDevice(true);
|
||||
|
||||
state(PROTOCOL_STATUS_ONLINE);
|
||||
while (_connected && _active)
|
||||
@@ -413,8 +404,7 @@ void Connector::write_loop()
|
||||
if (_active)
|
||||
{
|
||||
state(PROTOCOL_STATUS_NO_DEVICE);
|
||||
if (_protocol)
|
||||
_protocol->onDevice(false);
|
||||
onDevice(false);
|
||||
}
|
||||
|
||||
if (_read_thread.joinable())
|
||||
|
||||
+13
-11
@@ -8,7 +8,6 @@
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
#include "helper/iprotocol.h"
|
||||
#include "aes_cipher.h"
|
||||
|
||||
#define READ_TIMEOUT 3000
|
||||
@@ -35,24 +34,30 @@ class Connector
|
||||
|
||||
public:
|
||||
Connector(uint16_t videoPadding);
|
||||
~Connector();
|
||||
virtual ~Connector();
|
||||
|
||||
void start(IProtocol *protocol);
|
||||
void start();
|
||||
void stop();
|
||||
|
||||
int send(int cmd, bool encrypt = true, uint8_t *data = nullptr, uint32_t size = 0);
|
||||
void setEncryption(bool enabled);
|
||||
protected:
|
||||
|
||||
AESCipher *Cypher() const { return _cipher; };
|
||||
int send(int cmd, bool encrypt = true, uint8_t *data = nullptr, uint32_t size = 0);
|
||||
virtual void onData(uint32_t cmd, uint32_t length, uint8_t *data) = 0;
|
||||
virtual void onStatus(u_int8_t status) = 0;
|
||||
virtual void onDevice(bool connected) = 0;
|
||||
|
||||
void setEncryption(bool enabled);
|
||||
|
||||
static void printMessage(uint32_t cmd, uint32_t length, uint8_t *data, bool encrypted, bool out);
|
||||
static void printInts(uint8_t *data, uint32_t length, uint16_t max);
|
||||
static void printBytes(uint8_t *data, uint32_t length, uint16_t max);
|
||||
static const char *cmdString(int cmd);
|
||||
|
||||
AESCipher *_cipher = nullptr;
|
||||
|
||||
private:
|
||||
void read_loop();
|
||||
void write_loop();
|
||||
void readLoop();
|
||||
void writeLoop();
|
||||
|
||||
bool connect(uint16_t vendor_id, uint16_t product_id);
|
||||
bool link();
|
||||
@@ -80,9 +85,6 @@ private:
|
||||
std::atomic<bool> _active = false;
|
||||
|
||||
u_int16_t _videoPadding;
|
||||
|
||||
IProtocol *_protocol = nullptr;
|
||||
AESCipher *_cipher = nullptr;
|
||||
};
|
||||
|
||||
#endif /* SRC_CONNECTOR */
|
||||
|
||||
+2
-4
@@ -143,8 +143,8 @@ void Decoder::loop(AVCodecContext *context, AVCodecParserContext *parser, AVPack
|
||||
uint32_t counter = 0;
|
||||
|
||||
// Main decoding loop; runs until global_quit flag is set
|
||||
_data->wait(_active);
|
||||
while (_active)
|
||||
;
|
||||
while (_data->wait(_active))
|
||||
{
|
||||
// Get raw data segment from queue
|
||||
std::unique_ptr<Message> segment = _data->pop();
|
||||
@@ -196,7 +196,5 @@ void Decoder::loop(AVCodecContext *context, AVCodecParserContext *parser, AVPack
|
||||
_vb->commit();
|
||||
}
|
||||
}
|
||||
|
||||
_data->wait(_active);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
#ifndef SRC_HELPER_IPROTOCOL
|
||||
#define SRC_HELPER_IPROTOCOL
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
|
||||
class IProtocol
|
||||
{
|
||||
public:
|
||||
virtual void onData(uint32_t cmd, uint32_t length, uint8_t *data) = 0;
|
||||
virtual void onStatus(u_int8_t status) = 0;
|
||||
virtual void onDevice(bool connected) = 0;
|
||||
};
|
||||
#endif /* SRC_HELPER_IPROTOCOL */
|
||||
+1
-2
@@ -149,9 +149,8 @@ void PcmAudio::runner()
|
||||
SDL_AudioDeviceID device = 0;
|
||||
SDL_AudioSpec spec;
|
||||
|
||||
while (_active)
|
||||
while (_data->wait(_active))
|
||||
{
|
||||
_data->wait(_active, Settings::audioDelay);
|
||||
const Message *segment = _data->peek();
|
||||
if (!segment)
|
||||
continue;
|
||||
|
||||
+20
-21
@@ -7,32 +7,32 @@
|
||||
#include <cctype>
|
||||
|
||||
Protocol::Protocol(uint16_t width, uint16_t height, uint16_t fps, uint16_t padding)
|
||||
: connector(padding),
|
||||
: Connector(padding),
|
||||
videoData(Settings::videoQueue),
|
||||
audioStreamMain(Settings::audioQueue),
|
||||
audioStreamAux(Settings::audioQueue),
|
||||
phoneConnected(false),
|
||||
_width(width),
|
||||
_height(height),
|
||||
_fps(fps)
|
||||
_fps(fps),
|
||||
_phoneConnected(false)
|
||||
|
||||
{
|
||||
}
|
||||
|
||||
Protocol::~Protocol()
|
||||
{
|
||||
stop();
|
||||
}
|
||||
|
||||
void Protocol::start(uint32_t evtStatus, uint32_t evtPhone)
|
||||
{
|
||||
_evtStatusId = evtStatus;
|
||||
_evtPhoneId = evtPhone;
|
||||
connector.start(this);
|
||||
Connector::start();
|
||||
}
|
||||
|
||||
void Protocol::stop()
|
||||
{
|
||||
connector.stop();
|
||||
Connector::stop();
|
||||
}
|
||||
|
||||
void Protocol::sendInit(int width, int height, int fps)
|
||||
@@ -46,7 +46,7 @@ void Protocol::sendInit(int width, int height, int fps)
|
||||
write_uint32_le(&buf[20], 2);
|
||||
write_uint32_le(&buf[24], 2);
|
||||
|
||||
connector.send(1, true, buf, 28);
|
||||
send(CMD_OPEN, true, buf, 28);
|
||||
}
|
||||
|
||||
void Protocol::sendConfig()
|
||||
@@ -118,7 +118,7 @@ void Protocol::sendKey(int key)
|
||||
uint8_t buf[4];
|
||||
write_uint32_le(&buf[0], key);
|
||||
|
||||
connector.send(8, false, buf, 4);
|
||||
send(CMD_CONTROL, false, buf, 4);
|
||||
}
|
||||
|
||||
void Protocol::sendFile(const char *filename, const char *value)
|
||||
@@ -150,7 +150,7 @@ void Protocol::sendClick(float x, float y, bool down)
|
||||
write_uint32_le(buf + 4, int(10000 * x));
|
||||
write_uint32_le(buf + 8, int(10000 * y));
|
||||
write_uint32_le(buf + 12, 0);
|
||||
connector.send(5, false, buf, 16);
|
||||
send(CMD_TOUCH, false, buf, 16);
|
||||
}
|
||||
|
||||
void Protocol::sendMove(float dx, float dy)
|
||||
@@ -160,7 +160,7 @@ void Protocol::sendMove(float dx, float dy)
|
||||
write_uint32_le(buf + 4, int(10000 * dx));
|
||||
write_uint32_le(buf + 8, int(10000 * dy));
|
||||
write_uint32_le(buf + 12, 0);
|
||||
connector.send(5, false, buf, 16);
|
||||
send(CMD_TOUCH, false, buf, 16);
|
||||
}
|
||||
|
||||
void Protocol::sendFile(const char *filename, const uint8_t *data, uint32_t length)
|
||||
@@ -188,33 +188,32 @@ void Protocol::sendFile(const char *filename, const uint8_t *data, uint32_t leng
|
||||
// 4) content bytes
|
||||
std::memcpy(buf, data, length);
|
||||
|
||||
connector.send(CMD_SEND_FILE, true, result.data(), total);
|
||||
send(CMD_SEND_FILE, true, result.data(), total);
|
||||
}
|
||||
|
||||
void Protocol::sendInt(uint32_t cmd, uint32_t value, bool encryption)
|
||||
{
|
||||
uint8_t buf[4];
|
||||
write_uint32_le(buf, value);
|
||||
connector.send(cmd, encryption, buf, 4);
|
||||
send(cmd, encryption, buf, 4);
|
||||
}
|
||||
|
||||
void Protocol::sendString(uint32_t cmd, char *str, bool encryption)
|
||||
{
|
||||
uint32_t total = strlen(str);
|
||||
connector.send(cmd, true, (uint8_t *)str, total);
|
||||
send(cmd, true, (uint8_t *)str, total);
|
||||
}
|
||||
|
||||
void Protocol::sendEncryption()
|
||||
{
|
||||
AESCipher *cypher = connector.Cypher();
|
||||
if (!cypher)
|
||||
if (!_cipher)
|
||||
{
|
||||
std::cout << "[Protocol] Can't enable encryption: cypher is not initalised";
|
||||
return;
|
||||
}
|
||||
uint8_t buf[4];
|
||||
write_uint32_le(buf, cypher->Seed());
|
||||
connector.send(CMD_ENCRYPTION, false, buf, 4);
|
||||
write_uint32_le(buf, _cipher->Seed());
|
||||
send(CMD_ENCRYPTION, false, buf, 4);
|
||||
}
|
||||
|
||||
void Protocol::onStatus(uint8_t status)
|
||||
@@ -237,15 +236,15 @@ void Protocol::onDevice(bool connected)
|
||||
else
|
||||
{
|
||||
onPhone(false);
|
||||
connector.setEncryption(false);
|
||||
setEncryption(false);
|
||||
}
|
||||
}
|
||||
|
||||
void Protocol::onPhone(bool connected)
|
||||
{
|
||||
if (connected == phoneConnected)
|
||||
if (connected == _phoneConnected)
|
||||
return;
|
||||
phoneConnected = connected;
|
||||
_phoneConnected = connected;
|
||||
|
||||
std::cout << (connected ? "[Protocol] Phone connected" : "[Protocol] Phone disconnected") << std::endl;
|
||||
|
||||
@@ -303,7 +302,7 @@ void Protocol::onData(uint32_t cmd, uint32_t length, uint8_t *data)
|
||||
}
|
||||
case CMD_ENCRYPTION:
|
||||
if (length == 0)
|
||||
connector.setEncryption(true);
|
||||
setEncryption(true);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
+5
-9
@@ -3,22 +3,20 @@
|
||||
|
||||
#include "struct/atomic_queue.h"
|
||||
#include "struct/message.h"
|
||||
#include "helper/iprotocol.h"
|
||||
|
||||
#include "settings.h"
|
||||
#include "connector.h"
|
||||
|
||||
class Protocol : public IProtocol
|
||||
class Protocol : private Connector
|
||||
{
|
||||
|
||||
public:
|
||||
Protocol(uint16_t width, uint16_t height, uint16_t fps, uint16_t padding);
|
||||
~Protocol();
|
||||
~Protocol() override;
|
||||
|
||||
Protocol(const Protocol &) = delete;
|
||||
Protocol &operator=(const Protocol &) = delete;
|
||||
|
||||
static const char *cmdString(int cmd);
|
||||
|
||||
void start(uint32_t evtStatus, uint32_t evtPhone);
|
||||
void stop();
|
||||
|
||||
@@ -29,11 +27,9 @@ public:
|
||||
void sendClick(float x, float y, bool down);
|
||||
void sendMove(float dx, float dy);
|
||||
|
||||
Connector connector;
|
||||
AtomicQueue<Message> videoData;
|
||||
AtomicQueue<Message> audioStreamMain;
|
||||
AtomicQueue<Message> audioStreamAux;
|
||||
bool phoneConnected;
|
||||
|
||||
private:
|
||||
void sendInt(uint32_t cmd, uint32_t value, bool encryption = true);
|
||||
@@ -45,14 +41,14 @@ private:
|
||||
void onStatus(uint8_t status) override;
|
||||
void onDevice(bool connected) override;
|
||||
void onData(uint32_t cmd, uint32_t length, uint8_t *data) override;
|
||||
|
||||
void onPhone(bool connected);
|
||||
|
||||
bool jsonFind(const char *json, uint16_t length, const char *key, char *value, uint16_t size) const;
|
||||
static const char *cmdString(int cmd);
|
||||
|
||||
uint16_t _width;
|
||||
uint16_t _height;
|
||||
uint16_t _fps;
|
||||
bool _phoneConnected;
|
||||
|
||||
uint32_t _evtStatusId = (uint32_t)-1;
|
||||
uint32_t _evtPhoneId = (uint32_t)-1;
|
||||
|
||||
@@ -72,10 +72,9 @@ public:
|
||||
{
|
||||
unique_lock<std::mutex> lock(_mtx);
|
||||
|
||||
bool exit = _count > count;
|
||||
_lock.wait(lock, [&]
|
||||
{ return _count > count || !waitFlag; });
|
||||
return !exit;
|
||||
return waitFlag;
|
||||
}
|
||||
|
||||
void clear()
|
||||
|
||||
Reference in New Issue
Block a user