Skip to content

Commit

Permalink
The virtual camera is working.
Browse files Browse the repository at this point in the history
  • Loading branch information
hipersayanX committed Feb 19, 2025
1 parent 9bc8988 commit 942faff
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 44 deletions.
26 changes: 20 additions & 6 deletions Service/src/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ AkVCam::ServicePrivate::ServicePrivate()

this->m_messageServer.setPort(Preferences::servicePort());

this->m_messageServer.subscribe(AKVCAM_SERVICE_MSG_CLIENTS , BIND(ServicePrivate::clients) );
this->m_messageServer.subscribe(AKVCAM_SERVICE_MSG_BROADCAST , BIND(ServicePrivate::broadcast) );
this->m_messageServer.subscribe(AKVCAM_SERVICE_MSG_LISTEN , BIND(ServicePrivate::listen) );
this->m_messageServer.subscribe(AKVCAM_SERVICE_MSG_CLIENTS , BIND(ServicePrivate::clients) );
this->m_messageServer.subscribe(AKVCAM_SERVICE_MSG_BROADCAST, BIND(ServicePrivate::broadcast));
this->m_messageServer.subscribe(AKVCAM_SERVICE_MSG_LISTEN , BIND(ServicePrivate::listen) );

this->m_messageServer.connectConnectionClosed(this, &ServicePrivate::removeClientById);
}
Expand Down Expand Up @@ -213,26 +213,40 @@ bool AkVCam::ServicePrivate::broadcast(uint64_t clientId,
AkLogFunction();
MsgBroadcast msgBroadcast(inMessage);
MsgStatus status(-1, inMessage.queryId());

this->m_peerMutex.lock();

if (this->m_broadcasts.count(msgBroadcast.device()) < 1)
bool isBroadcasting = this->m_broadcasts.count(msgBroadcast.device()) < 1;
AkLogDebug() << "Device" << msgBroadcast.device() << "is broadcasting?:" << (isBroadcasting? "YES": "NO") << std::endl;

if (isBroadcasting) {
AkLogDebug() << "Adding device slot:" << std::endl;
AkLogDebug() << " Device ID:" << msgBroadcast.device() << std::endl;
AkLogDebug() << " Client ID:" << clientId << std::endl;
AkLogDebug() << " Client PID:" << msgBroadcast.pid() << std::endl;

this->m_broadcasts[msgBroadcast.device()] =
{{clientId, msgBroadcast.pid()}, {}, {}};
}

AkLogDebug() << "Get slot" << std::endl;
auto &slot = this->m_broadcasts[msgBroadcast.device()];

if (slot.broadcaster.pid == 0)
if (slot.broadcaster.pid == 0) {
AkLogDebug() << "Set client as broadcaster" << std::endl;
slot.broadcaster = {clientId, msgBroadcast.pid()};
}

if (slot.broadcaster.pid == msgBroadcast.pid()
&& slot.broadcaster.clientId == clientId) {
AkLogDebug() << "Save frame" << std::endl;
slot.frame = msgBroadcast.frame();
status = MsgStatus(0, inMessage.queryId());
this->m_frameAvailable.notify_all();
}

this->m_peerMutex.unlock();

AkLogDebug() << "Sending the response" << std::endl;
outMessage = status.toMessage();

return status.status() == 0;
Expand Down
22 changes: 9 additions & 13 deletions VCamUtils/src/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,8 @@ AkVCam::MsgClients::MsgClients(const Message &message):
totalSize += sizeof(this->d->m_clientType);

size_t clientsSize = 0;
totalSize += sizeof(size_t);
memcpy(&clientsSize, message.data().data() + totalSize, sizeof(size_t) * clientsSize);
totalSize += sizeof(sizeof(uint64_t) * clientsSize);
totalSize += sizeof(size_t) + sizeof(sizeof(uint64_t) * clientsSize);
}

if (message.id() != AKVCAM_SERVICE_MSG_CLIENTS
Expand Down Expand Up @@ -461,18 +460,16 @@ AkVCam::MsgFrameReady::MsgFrameReady(const Message &message):

{
size_t deviceSize = 0;
totalSize += sizeof(size_t);
memcpy(&deviceSize, message.data().data() + totalSize, sizeof(size_t));
totalSize += deviceSize;
totalSize += sizeof(size_t) + deviceSize;

totalSize += sizeof(FourCC);
totalSize += sizeof(int);
totalSize += sizeof(int);

size_t dataSize = 0;
totalSize += sizeof(size_t);
memcpy(&dataSize, message.data().data() + totalSize, sizeof(size_t));
totalSize += dataSize;
totalSize += sizeof(size_t) + dataSize;

totalSize += sizeof(this->d->m_isActive);
}
Expand Down Expand Up @@ -687,19 +684,17 @@ AkVCam::MsgBroadcast::MsgBroadcast(const Message &message):

{
size_t deviceSize = 0;
totalSize += sizeof(size_t);
memcpy(&deviceSize, message.data().data() + totalSize, sizeof(size_t));
totalSize += deviceSize;
totalSize += sizeof(size_t) + deviceSize;

totalSize += sizeof(this->d->m_pid);
totalSize += sizeof(FourCC);
totalSize += sizeof(int);
totalSize += sizeof(int);

size_t dataSize = 0;
totalSize += sizeof(size_t);
memcpy(&dataSize, message.data().data() + totalSize, sizeof(size_t));
totalSize += dataSize;
totalSize += sizeof(size_t) + dataSize;
}

if (message.id() != AKVCAM_SERVICE_MSG_BROADCAST
Expand Down Expand Up @@ -738,7 +733,9 @@ AkVCam::MsgBroadcast::MsgBroadcast(const Message &message):

if (dataSize > 0) {
this->d->m_frame = {VideoFormat(fourcc, width, height)};
memcpy(this->d->m_frame.data().data(), message.data().data() + offset, dataSize);
memcpy(this->d->m_frame.data().data(),
message.data().data() + offset,
std::min(dataSize, this->d->m_frame.data().size()));
}
}

Expand Down Expand Up @@ -886,9 +883,8 @@ AkVCam::MsgListen::MsgListen(const Message &message):

{
size_t deviceSize = 0;
totalSize += sizeof(size_t);
memcpy(&deviceSize, message.data().data() + totalSize, sizeof(size_t));
totalSize += deviceSize;
totalSize += sizeof(size_t) + deviceSize;

totalSize += sizeof(this->d->m_pid);
}
Expand Down
10 changes: 5 additions & 5 deletions VCamUtils/src/servicemsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
#define SERVICEMSG_H

// Clients listing
#define AKVCAM_SERVICE_MSG_CLIENTS 0x001
#define AKVCAM_SERVICE_MSG_CLIENTS 0x001

// Response messages
#define AKVCAM_SERVICE_MSG_STATUS 0x101
#define AKVCAM_SERVICE_MSG_FRAME_READY 0x102
#define AKVCAM_SERVICE_MSG_STATUS 0x101
#define AKVCAM_SERVICE_MSG_FRAME_READY 0x102

// Broadcasting messages
#define AKVCAM_SERVICE_MSG_BROADCAST 0x201
#define AKVCAM_SERVICE_MSG_LISTEN 0x202
#define AKVCAM_SERVICE_MSG_BROADCAST 0x201
#define AKVCAM_SERVICE_MSG_LISTEN 0x202

#endif // SERVICEMSG_H
12 changes: 6 additions & 6 deletions VCamUtils/src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,12 @@ std::string AkVCam::stringFromMessageId(uint32_t messageId)
uint32_t id;
const char *str;
} vcamUtilsClsidToString [] = {
{AKVCAM_SERVICE_MSG_CLIENTS , "CLIENTS" },
{AKVCAM_SERVICE_MSG_STATUS , "STATUS" },
{AKVCAM_SERVICE_MSG_FRAME_READY , "FRAME" },
{AKVCAM_SERVICE_MSG_BROADCAST , "BROADCAST" },
{AKVCAM_SERVICE_MSG_LISTEN , "LISTEN" },
{0 , nullptr },
{AKVCAM_SERVICE_MSG_CLIENTS , "CLIENTS" },
{AKVCAM_SERVICE_MSG_STATUS , "STATUS" },
{AKVCAM_SERVICE_MSG_FRAME_READY, "FRAME" },
{AKVCAM_SERVICE_MSG_BROADCAST , "BROADCAST"},
{AKVCAM_SERVICE_MSG_LISTEN , "LISTEN" },
{0 , nullptr },
};

for (auto msg = vcamUtilsClsidToString; msg->id; ++msg)
Expand Down
16 changes: 9 additions & 7 deletions cmio/VCamIPC/src/ipcbridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "VCamUtils/src/message.h"
#include "VCamUtils/src/messageclient.h"
#include "VCamUtils/src/servicemsg.h"
#include "VCamUtils/src/timer.h"
#include "VCamUtils/src/utils.h"
#include "VCamUtils/src/videoformat.h"
#include "VCamUtils/src/videoframe.h"
Expand Down Expand Up @@ -398,15 +399,15 @@ bool AkVCam::IpcBridge::deviceStart(StreamType type,

if (type == StreamType_Input) {
slot.messageFuture =
this->d->m_messageClient.send([this, &deviceId] (Message &message) -> bool {
return this->d->frameRequired(deviceId, message);
});
this->d->m_messageClient.send(MsgListen(deviceId, currentPid()).toMessage(),
std::bind(&IpcBridgePrivate::frameReady,
this->d,
std::placeholders::_1));
} else {
slot.messageFuture =
this->d->m_messageClient.send(MsgListen(deviceId, currentPid()).toMessage(),
std::bind(&IpcBridgePrivate::frameReady,
this->d,
std::placeholders::_1));
this->d->m_messageClient.send([this, deviceId] (Message &message) -> bool {
return this->d->frameRequired(deviceId, message);
});
}

this->d->m_broadcastsMutex.unlock();
Expand Down Expand Up @@ -560,6 +561,7 @@ AkVCam::IpcBridgePrivate::IpcBridgePrivate(IpcBridge *self):

AkVCam::IpcBridgePrivate::~IpcBridgePrivate()
{
AkLogFunction();
}

bool AkVCam::IpcBridgePrivate::launchService()
Expand Down
15 changes: 8 additions & 7 deletions dshow/VCamIPC/src/ipcbridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ namespace AkVCam
bool frameReady(const Message &message);
static void checkStatus(void *userData);

// Utility methods
bool isRoot() const;
int exec(const std::vector<std::string> &parameters,
const std::string &directory={},
Expand Down Expand Up @@ -447,15 +448,15 @@ bool AkVCam::IpcBridge::deviceStart(StreamType type,

if (type == StreamType_Input) {
slot.messageFuture =
this->d->m_messageClient.send([this, deviceId] (Message &message) -> bool {
return this->d->frameRequired(deviceId, message);
});
this->d->m_messageClient.send(MsgListen(deviceId, currentPid()).toMessage(),
std::bind(&IpcBridgePrivate::frameReady,
this->d,
std::placeholders::_1));
} else {
slot.messageFuture =
this->d->m_messageClient.send(MsgListen(deviceId, currentPid()).toMessage(),
std::bind(&IpcBridgePrivate::frameReady,
this->d,
std::placeholders::_1));
this->d->m_messageClient.send([this, deviceId] (Message &message) -> bool {
return this->d->frameRequired(deviceId, message);
});
}

this->d->m_broadcastsMutex.unlock();
Expand Down

0 comments on commit 942faff

Please sign in to comment.