Skip to content

Commit

Permalink
Made KMS service alot more stable
Browse files Browse the repository at this point in the history
  • Loading branch information
matanui159 committed Jul 10, 2021
1 parent 7fe53f1 commit 04938df
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 20 deletions.
38 changes: 30 additions & 8 deletions src/command/svkmscmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ static void kmsSignal(int sig) {
signal(sig, SIG_DFL);
}

static int kmsChmod(const char *path) {
#ifdef RS_BUILD_POSIX_IO_FOUND
if (chmod(path, 0777) == -1) {
int ret = AVERROR(errno);
av_log(NULL, AV_LOG_ERROR, "Failed to change permissions: %s\n", av_err2str(ret));
return ret;
}
return 0;

#else
(void)path;
av_log(NULL, AV_LOG_WARNING,
"Failed to change permissions: Posix I/O was not found during compilation\n");
return 0;
#endif
}

static int kmsConnection(RSSocket *sock) {
int ret;
RSServiceDeviceInfo info;
Expand Down Expand Up @@ -72,10 +89,18 @@ static int kmsConnection(RSSocket *sock) {
}

while (running) {
int64_t pts;
if ((ret = rsDeviceNextFrame(&device, frame)) < 0) {
// TODO: properly handle error
pts = ret;
} else {
pts = frame->pts;
}
if ((ret = rsSocketSend(sock, sizeof(int64_t), &pts, 0, NULL)) < 0) {
goto error;
}
if (pts < 0) {
continue;
}

int objects[AV_DRM_MAX_PLANES];
AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor *)frame->data[0];
Expand All @@ -86,9 +111,6 @@ static int kmsConnection(RSSocket *sock) {
(size_t)desc->nb_objects, objects)) < 0) {
goto error;
}
if ((ret = rsSocketSend(sock, sizeof(int64_t), &frame->pts, 0, NULL)) < 0) {
goto error;
}
av_frame_unref(frame);
}

Expand All @@ -109,10 +131,10 @@ int rsKmsService(void) {
if ((ret = rsSocketBind(&sock, RS_SERVICE_DEVICE_PATH)) < 0) {
goto error;
}
if (chmod(RS_SERVICE_DEVICE_PATH, S_IROTH | S_IWOTH) == -1) {
ret = AVERROR(errno);
av_log(NULL, AV_LOG_ERROR, "Failed to change socket permissions: %s\n",
av_err2str(ret));
if (kmsChmod("/tmp/replay-sorcery") < 0) {
goto error;
}
if (kmsChmod(RS_SERVICE_DEVICE_PATH) < 0) {
goto error;
}

Expand Down
12 changes: 9 additions & 3 deletions src/device/svkmsdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,22 @@ static int kmsServiceDeviceNextFrame(RSDevice *device, AVFrame *frame) {
ret = AVERROR(ENOMEM);
goto error;
}
if ((ret = rsSocketReceive(sock, sizeof(int64_t), &frame->pts, 0, NULL)) < 0) {
goto error;
}
if (frame->pts < 0) {
ret = (int)frame->pts;
av_log(NULL, AV_LOG_ERROR, "KMS service failed to get frame: %s\n",
av_err2str(ret));
goto error;
}
if ((ret = rsSocketReceive(sock, sizeof(AVDRMFrameDescriptor), desc, AV_DRM_MAX_PLANES,
objects)) < 0) {
goto error;
}
for (int i = 0; i < desc->nb_objects; ++i) {
desc->objects[i].fd = objects[i];
}
if ((ret = rsSocketReceive(sock, sizeof(int64_t), &frame->pts, 0, NULL)) < 0) {
goto error;
}

frame->hw_frames_ctx = av_buffer_ref(device->hwFrames);
if (frame->hw_frames_ctx == NULL) {
Expand Down
24 changes: 15 additions & 9 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static AVPacket *videoPacket;
static AVFrame *videoFrame;
static RSAudioThread audioThread;
static RSControl controller;
static int silence = 0;
static volatile sig_atomic_t running = 1;

static void mainSignal(int sig) {
Expand All @@ -60,9 +61,15 @@ static int mainCommand(const char *name) {
}
}

static void mainUnsilence(void) {
if (silence) {
rsLogSilence(-1);
silence = 0;
}
}

static int mainStep(void) {
int ret;
int silence = 0;
while ((ret = rsEncoderNextPacket(&videoEncoder, videoPacket)) == AVERROR(EAGAIN)) {
if ((ret = rsDeviceNextFrame(&videoDevice, videoFrame)) < 0) {
av_log(NULL, AV_LOG_WARNING, "Failed to get frame from device: %s\n",
Expand All @@ -71,14 +78,12 @@ static int mainStep(void) {
rsLogSilence(1);
silence = 1;
}
} else {
if (silence) {
rsLogSilence(-1);
silence = 0;
}
if ((ret = rsEncoderSendFrame(&videoEncoder, videoFrame)) < 0) {
return ret;
}
return 0;
}

mainUnsilence();
if ((ret = rsEncoderSendFrame(&videoEncoder, videoFrame)) < 0) {
return ret;
}
}
if ((ret = rsBufferAddPacket(&videoBuffer, videoPacket)) < 0) {
Expand Down Expand Up @@ -224,6 +229,7 @@ int main(int argc, char *argv[]) {

ret = 0;
error:
mainUnsilence();
rsControlDestroy(&controller);
rsAudioThreadDestroy(&audioThread);
av_frame_free(&videoFrame);
Expand Down
1 change: 1 addition & 0 deletions sys/replay-sorcery.service.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[Unit]
Description=ReplaySorcery: an open-source, instant-replay solution for Linux.
After=graphical-session.target replay-sorcery-kms.service

[Service]
Type=simple
Expand Down

0 comments on commit 04938df

Please sign in to comment.