Skip to content

Commit

Permalink
Refactor: WebMessageProtocol error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaDemchenko committed Jan 14, 2025
1 parent ebc442b commit 0b4eb6f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 26 deletions.
17 changes: 4 additions & 13 deletions p2pml/src/main/java/com/novage/p2pml/webview/WebMessageProtocol.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ internal class WebMessageProtocol(
private var wasInitialMessageSent = false

init {
setupWebMessageCallback()
initializeWebMessageCallback()
}

@SuppressLint("RequiresFeature")
private fun setupWebMessageCallback() {
private fun initializeWebMessageCallback() {
channels[0].setWebMessageCallback(
object : WebMessagePortCompat.WebMessageCallbackCompat() {
override fun onMessage(
Expand All @@ -61,9 +61,6 @@ internal class WebMessageProtocol(
}

private fun handleSegmentIdBytes(arrayBuffer: ByteArray) {
if (incomingRequestId == null) {
Log.d("WebMessageProtocol", "Error: Empty segment ID")
}
val requestId =
incomingRequestId
?: throw IllegalStateException("Received segment bytes without a segment ID")
Expand All @@ -72,7 +69,7 @@ internal class WebMessageProtocol(
val deferred = getSegmentResponseCallback(requestId)

if (deferred == null) {
Log.d(
Log.e(
"WebMessageProtocol",
"Error: No deferred found for segment ID: $requestId",
)
Expand Down Expand Up @@ -104,7 +101,7 @@ internal class WebMessageProtocol(

val deferredSegmentBytes = getSegmentResponseCallback(requestId)
if (deferredSegmentBytes == null) {
Log.d(
Log.e(
"WebMessageProtocol",
"Error: No deferred found for segment ID: $segmentId",
)
Expand All @@ -125,12 +122,6 @@ internal class WebMessageProtocol(
}

private fun handleSegmentIdMessage(requestId: String) {
if (incomingRequestId != null) {
Log.d(
"WebMessageProtocol",
"Received incorrect request ID: $requestId. Expected: $incomingRequestId",
)
}
val requestIdToInt =
requestId.toIntOrNull() ?: throw IllegalStateException("Invalid request ID")
incomingRequestId = requestIdToInt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ internal class WebViewManager(
while (isActive) {
try {
if (engineStateManager.isEngineDisabled()) {
Log.d(
"WebViewManager",
"P2P Engine disabled, stopping playback info update.",
)
playbackInfoJob?.cancel()
playbackInfoJob = null
break
Expand Down
10 changes: 1 addition & 9 deletions p2pml/src/main/resources/p2pml/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
const streams = JSON.parse(jsonData);

for (const stream of streams) {
console.log(">>> Add stream:", stream);
this.core.addStreamIfNoneExists(stream);
}
}
Expand All @@ -72,11 +71,6 @@
const { streamRuntimeId, addSegments, removeSegmentsIds, isLive } =
playlist;

console.log(">>> Parsed stream:", streamRuntimeId);
console.log("+++ Add segments:", addSegments);
console.log("--- Remove segments:", removeSegmentsIds);
console.log(">>> Is live:", isLive);

this.core.updateStream(
streamRuntimeId,
addSegments,
Expand All @@ -97,7 +91,7 @@
this.port.postMessage(`${requestId}`);
this.port.postMessage(copyData, [copyData]);
} catch (e) {
console.error("Error in onSuccess:", e);
this.port.postMessage(`Error:${requestId}:failed|${segmentUrl}`);
}
};

Expand All @@ -119,13 +113,11 @@
onError,
});
} catch (e) {
console.warn("Error in processSegmentRequest:", e);
this.port.postMessage(`Error:${requestId}:not_found|${segmentUrl}`);
}
}

setManifestUrl(manifestUrl) {
console.log(">>> Set manifest URL:", manifestUrl);
this.core.setManifestResponseUrl(manifestUrl);
}

Expand Down

0 comments on commit 0b4eb6f

Please sign in to comment.