Improve media packets

This commit is contained in:
2025-12-10 16:30:42 +01:00
parent 772f11dace
commit 519ebbaaff
5 changed files with 198 additions and 35 deletions

View File

@@ -14,6 +14,7 @@ public class AssetEvent {
private AssetView payload;
private String assetId;
private Boolean play;
private AssetPatch patch;
public static AssetEvent created(String channel, AssetView asset) {
AssetEvent event = new AssetEvent();
@@ -24,6 +25,15 @@ public class AssetEvent {
return event;
}
public static AssetEvent updated(String channel, AssetPatch patch) {
AssetEvent event = new AssetEvent();
event.type = Type.UPDATED;
event.channel = channel;
event.assetId = patch.id();
event.patch = patch;
return event;
}
public static AssetEvent play(String channel, AssetView asset, boolean play) {
AssetEvent event = new AssetEvent();
event.type = Type.PLAY;
@@ -34,21 +44,12 @@ public class AssetEvent {
return event;
}
public static AssetEvent updated(String channel, AssetView asset) {
AssetEvent event = new AssetEvent();
event.type = Type.UPDATED;
event.channel = channel;
event.payload = asset;
event.assetId = asset.id();
return event;
}
public static AssetEvent visibility(String channel, AssetView asset) {
public static AssetEvent visibility(String channel, AssetPatch patch) {
AssetEvent event = new AssetEvent();
event.type = Type.VISIBILITY;
event.channel = channel;
event.payload = asset;
event.assetId = asset.id();
event.patch = patch;
event.assetId = patch.id();
return event;
}
@@ -79,4 +80,8 @@ public class AssetEvent {
public Boolean getPlay() {
return play;
}
public AssetPatch getPatch() {
return patch;
}
}

View File

@@ -0,0 +1,63 @@
package com.imgfloat.app.model;
/**
* Represents a partial update for an {@link Asset}. Only the fields that changed
* for a given operation are populated to reduce payload sizes sent over WebSocket.
*/
public record AssetPatch(
String id,
Double x,
Double y,
Double width,
Double height,
Double rotation,
Double speed,
Boolean muted,
Integer zIndex,
Boolean hidden,
Boolean audioLoop,
Integer audioDelayMillis,
Double audioSpeed,
Double audioPitch,
Double audioVolume
) {
public static AssetPatch fromTransform(Asset asset) {
return new AssetPatch(
asset.getId(),
asset.getX(),
asset.getY(),
asset.getWidth(),
asset.getHeight(),
asset.getRotation(),
asset.getSpeed(),
asset.isMuted(),
asset.getZIndex(),
null,
asset.isAudioLoop(),
asset.getAudioDelayMillis(),
asset.getAudioSpeed(),
asset.getAudioPitch(),
asset.getAudioVolume()
);
}
public static AssetPatch fromVisibility(Asset asset) {
return new AssetPatch(
asset.getId(),
null,
null,
null,
null,
null,
null,
null,
null,
asset.isHidden(),
null,
null,
null,
null,
null
);
}
}

View File

@@ -2,6 +2,7 @@ package com.imgfloat.app.service;
import com.imgfloat.app.model.Asset;
import com.imgfloat.app.model.AssetEvent;
import com.imgfloat.app.model.AssetPatch;
import com.imgfloat.app.model.Channel;
import com.imgfloat.app.model.AssetView;
import com.imgfloat.app.model.CanvasSettingsRequest;
@@ -191,7 +192,8 @@ public class ChannelDirectoryService {
}
assetRepository.save(asset);
AssetView view = AssetView.from(normalized, asset);
messagingTemplate.convertAndSend(topicFor(broadcaster), AssetEvent.updated(broadcaster, view));
AssetPatch patch = AssetPatch.fromTransform(asset);
messagingTemplate.convertAndSend(topicFor(broadcaster), AssetEvent.updated(broadcaster, patch));
return view;
});
}
@@ -216,7 +218,8 @@ public class ChannelDirectoryService {
asset.setHidden(request.isHidden());
assetRepository.save(asset);
AssetView view = AssetView.from(normalized, asset);
messagingTemplate.convertAndSend(topicFor(broadcaster), AssetEvent.visibility(broadcaster, view));
AssetPatch patch = AssetPatch.fromVisibility(asset);
messagingTemplate.convertAndSend(topicFor(broadcaster), AssetEvent.visibility(broadcaster, patch));
return view;
});
}