Skip to content

Commit

Permalink
8321176: [Screencast] make a second attempt on screencast failure
Browse files Browse the repository at this point in the history
Reviewed-by: azvegint, prr
  • Loading branch information
antbob authored and Alexander Zvegintsev committed Dec 11, 2023
1 parent d13302f commit 92fd490
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/java.desktop/unix/native/libawt_xawt/awt/fp_pipewire.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void (*fp_pw_stream_destroy)(struct pw_stream *stream);


void (*fp_pw_init)(int *argc, char **argv[]);

void (*fp_pw_deinit)(void);

struct pw_core *
(*fp_pw_context_connect_fd)(struct pw_context *context,
Expand Down
82 changes: 59 additions & 23 deletions src/java.desktop/unix/native/libawt_xawt/awt/screencast_pipewire.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,17 @@ static gboolean initScreenSpace() {
}

static void doCleanup() {
if (pw.loop) {
DEBUG_SCREENCAST("STOPPING loop\n", NULL);
fp_pw_thread_loop_stop(pw.loop);
}

for (int i = 0; i < screenSpace.screenCount; ++i) {
struct ScreenProps *screenProps = &screenSpace.screens[i];
if (screenProps->data) {
if (screenProps->data->stream) {
fp_pw_stream_disconnect(screenProps->data->stream);
fp_pw_thread_loop_lock(pw.loop);
fp_pw_stream_disconnect(screenProps->data->stream);
fp_pw_stream_destroy(screenProps->data->stream);
fp_pw_thread_loop_unlock(pw.loop);
screenProps->data->stream = NULL;
Expand All @@ -116,10 +121,7 @@ static void doCleanup() {
pw.core = NULL;
}

DEBUG_SCREENCAST("STOPPING loop\n", NULL)

if (pw.loop) {
fp_pw_thread_loop_stop(pw.loop);
fp_pw_thread_loop_destroy(pw.loop);
pw.loop = NULL;
}
Expand All @@ -130,6 +132,10 @@ static void doCleanup() {
screenSpace.screenCount = 0;
}

if (!sessionClosed) {
fp_pw_deinit();
}

gtk->g_string_set_size(activeSessionToken, 0);
sessionClosed = TRUE;
}
Expand Down Expand Up @@ -571,6 +577,7 @@ static const struct pw_core_events coreEvents = {
* @return TRUE on success
*/
static gboolean doLoop(GdkRectangle requestedArea) {
gboolean isLoopLockTaken = FALSE;
if (!pw.loop && !sessionClosed) {
pw.loop = fp_pw_thread_loop_new("AWT Pipewire Thread", NULL);

Expand Down Expand Up @@ -599,6 +606,7 @@ static gboolean doLoop(GdkRectangle requestedArea) {
}

fp_pw_thread_loop_lock(pw.loop);
isLoopLockTaken = TRUE;

pw.core = fp_pw_context_connect_fd(
pw.context,
Expand Down Expand Up @@ -639,12 +647,16 @@ static gboolean doLoop(GdkRectangle requestedArea) {
DEBUG_SCREEN_PREFIX(screen, "@@@ screen processed %i\n", i);
}

fp_pw_thread_loop_unlock(pw.loop);
if (isLoopLockTaken) {
fp_pw_thread_loop_unlock(pw.loop);
}

return TRUE;

fail:
fp_pw_thread_loop_unlock(pw.loop);
if (isLoopLockTaken) {
fp_pw_thread_loop_unlock(pw.loop);
}
doCleanup();
return FALSE;
}
Expand Down Expand Up @@ -700,6 +712,7 @@ static gboolean loadSymbols() {
LOAD_SYMBOL(fp_pw_stream_disconnect, "pw_stream_disconnect");
LOAD_SYMBOL(fp_pw_stream_destroy, "pw_stream_destroy");
LOAD_SYMBOL(fp_pw_init, "pw_init");
LOAD_SYMBOL(fp_pw_deinit, "pw_deinit");
LOAD_SYMBOL(fp_pw_context_connect_fd, "pw_context_connect_fd");
LOAD_SYMBOL(fp_pw_core_disconnect, "pw_core_disconnect");
LOAD_SYMBOL(fp_pw_context_new, "pw_context_new");
Expand Down Expand Up @@ -855,6 +868,33 @@ static void arrayToRectangles(JNIEnv *env,
(*env)->ReleaseIntArrayElements(env, boundsArray, body, 0);
}

static int makeScreencast(
const gchar *token,
GdkRectangle *requestedArea,
GdkRectangle *affectedScreenBounds,
gint affectedBoundsLength
) {
if (!initScreencast(token, affectedScreenBounds, affectedBoundsLength)) {
return pw.pwFd;
}

if (!doLoop(*requestedArea)) {
return RESULT_ERROR;
}

while (!isAllDataReady()) {
fp_pw_thread_loop_lock(pw.loop);
fp_pw_thread_loop_wait(pw.loop);
fp_pw_thread_loop_unlock(pw.loop);
if (hasPipewireFailed) {
doCleanup();
return RESULT_ERROR;
}
}

return RESULT_OK;
}

/*
* Class: sun_awt_screencast_ScreencastHelper
* Method: closeSession
Expand Down Expand Up @@ -911,26 +951,22 @@ JNIEXPORT jint JNICALL Java_sun_awt_screencast_ScreencastHelper_getRGBPixelsImpl
jx, jy, jwidth, jheight, token
);

if (!initScreencast(token, affectedScreenBounds, affectedBoundsLength)) {
releaseToken(env, jtoken, token);
return pw.pwFd;
}

if (!doLoop(requestedArea)) {
releaseToken(env, jtoken, token);
return RESULT_ERROR;
}
int attemptResult = makeScreencast(
token, &requestedArea, affectedScreenBounds, affectedBoundsLength);

while (!isAllDataReady()) {
fp_pw_thread_loop_lock(pw.loop);
fp_pw_thread_loop_wait(pw.loop);
if (hasPipewireFailed) {
fp_pw_thread_loop_unlock(pw.loop);
doCleanup();
if (attemptResult) {
if (attemptResult == RESULT_DENIED) {
releaseToken(env, jtoken, token);
return RESULT_ERROR;
return attemptResult;
}
DEBUG_SCREENCAST("Screencast attempt failed with %i, re-trying...\n",
attemptResult);
attemptResult = makeScreencast(
token, &requestedArea, affectedScreenBounds, affectedBoundsLength);
if (attemptResult) {
releaseToken(env, jtoken, token);
return attemptResult;
}
fp_pw_thread_loop_unlock(pw.loop);
}

DEBUG_SCREENCAST("\nall data ready\n", NULL);
Expand Down

5 comments on commit 92fd490

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheRealMDoerr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk21u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 92fd490 Jun 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TheRealMDoerr the backport was successfully created on the branch backport-TheRealMDoerr-92fd490f-master in my personal fork of openjdk/jdk21u-dev. To create a pull request with this backport targeting openjdk/jdk21u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 92fd490f from the openjdk/jdk repository.

The commit being backported was authored by Anton Bobrov on 11 Dec 2023 and was reviewed by Alexander Zvegintsev and Phil Race.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk21u-dev:

$ git fetch https://github.com/openjdk-bots/jdk21u-dev.git backport-TheRealMDoerr-92fd490f-master:backport-TheRealMDoerr-92fd490f-master
$ git checkout backport-TheRealMDoerr-92fd490f-master
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk21u-dev.git backport-TheRealMDoerr-92fd490f-master

@MBaesken
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 92fd490 Jul 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MBaesken the backport was successfully created on the branch backport-MBaesken-92fd490f-master in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 92fd490f from the openjdk/jdk repository.

The commit being backported was authored by Anton Bobrov on 11 Dec 2023 and was reviewed by Alexander Zvegintsev and Phil Race.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev.git backport-MBaesken-92fd490f-master:backport-MBaesken-92fd490f-master
$ git checkout backport-MBaesken-92fd490f-master
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev.git backport-MBaesken-92fd490f-master

Please sign in to comment.