Skip to content

Commit 3294e59

Browse files
authored
Merge pull request #3 from Novage/refactor/naming
Refactor: callback names in P2PMediaLoader class
2 parents 40e3618 + 6eb8039 commit 3294e59

File tree

6 files changed

+29
-25
lines changed

6 files changed

+29
-25
lines changed

app/src/main/java/com/novage/demo/viewmodel/ExoPlayerViewModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class ExoPlayerViewModel(application: Application) : AndroidViewModel(applicatio
3737

3838
fun setupP2PML() {
3939
p2pml = P2PMediaLoader(
40-
readyCallback = { initializePlayback() },
41-
onReadyErrorCallback = { onReadyError(it) },
40+
onP2PReadyCallback = { initializePlayback() },
41+
onP2PReadyErrorCallback = { onReadyError(it) },
4242
coreConfigJson = "{\"swarmId\":\"TEST_KOTLIN\"}",
4343
serverPort = 8081,
4444
)

p2pml/src/main/java/com/novage/p2pml/P2PMediaLoader.kt

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import androidx.media3.exoplayer.ExoPlayer
66
import com.novage.p2pml.Constants.CORE_FILE_URL
77
import com.novage.p2pml.Constants.CUSTOM_FILE_URL
88
import com.novage.p2pml.Constants.QueryParams.MANIFEST
9-
import com.novage.p2pml.interop.OnErrorCallback
10-
import com.novage.p2pml.interop.P2PReadyCallback
9+
import com.novage.p2pml.interop.OnP2PReadyCallback
10+
import com.novage.p2pml.interop.OnP2PReadyErrorCallback
1111
import com.novage.p2pml.parser.HlsManifestParser
1212
import com.novage.p2pml.providers.ExoPlayerPlaybackProvider
1313
import com.novage.p2pml.providers.ExternalPlaybackProvider
@@ -26,8 +26,8 @@ import kotlinx.coroutines.runBlocking
2626
/**
2727
* `P2PMediaLoader` facilitates peer-to-peer media streaming within an Android application.
2828
*
29-
* @param readyCallback Callback invoked when the P2P engine is ready for use
30-
* @param onReadyErrorCallback Callback invoked when an error occurs
29+
* @param onP2PReadyCallback Callback invoked when the P2P engine is ready for use
30+
* @param onP2PReadyErrorCallback Callback invoked when an error occurs
3131
* @param coreConfigJson Sets core P2P configurations. See [P2PML Core Config](https://novage.github.io/p2p-media-loader/docs/v2.1.0/types/p2p_media_loader_core.CoreConfig.html)
3232
* JSON string with core configurations. Default: empty string (uses default config)
3333
*
@@ -40,22 +40,22 @@ import kotlinx.coroutines.runBlocking
4040
*/
4141
@UnstableApi
4242
class P2PMediaLoader(
43-
private val readyCallback: P2PReadyCallback,
44-
private val onReadyErrorCallback: OnErrorCallback,
43+
private val onP2PReadyCallback: OnP2PReadyCallback,
44+
private val onP2PReadyErrorCallback: OnP2PReadyErrorCallback,
4545
private val coreConfigJson: String = "",
4646
private val serverPort: Int = Constants.DEFAULT_SERVER_PORT,
4747
private val customJavaScriptInterfaces: List<Pair<String, Any>> = emptyList(),
4848
private val customEngineImplementationPath: String? = null,
4949
) {
5050
// Second constructor for Java compatibility
5151
constructor(
52-
readyCallback: P2PReadyCallback,
53-
onReadyErrorCallback: OnErrorCallback,
52+
onP2PReadyCallback: OnP2PReadyCallback,
53+
onP2PReadyErrorCallback: OnP2PReadyErrorCallback,
5454
serverPort: Int,
5555
coreConfigJson: String,
5656
) : this(
57-
readyCallback,
58-
onReadyErrorCallback,
57+
onP2PReadyCallback,
58+
onP2PReadyErrorCallback,
5959
coreConfigJson,
6060
serverPort,
6161
emptyList(),
@@ -139,6 +139,7 @@ class P2PMediaLoader(
139139
engineStateManager,
140140
customEngineImplementationPath,
141141
onServerStarted = { onServerStarted() },
142+
onServerError = { onP2PReadyErrorCallback.onError(it) },
142143
onManifestChanged = { onManifestChanged() },
143144
).apply { start(serverPort) }
144145
}
@@ -220,9 +221,9 @@ class P2PMediaLoader(
220221
webViewManager!!.initCoreEngine(coreConfigJson)
221222

222223
try {
223-
readyCallback.onReady()
224+
onP2PReadyCallback.onReady()
224225
} catch (e: Exception) {
225-
onReadyErrorCallback.onError(e.message ?: "Unknown error")
226+
onP2PReadyErrorCallback.onError(e.message ?: "Unknown error")
226227
}
227228
}
228229
}
@@ -235,6 +236,10 @@ class P2PMediaLoader(
235236
Utils.getUrl(serverPort, CORE_FILE_URL)
236237
}
237238

238-
webViewManager!!.loadWebView(urlPath)
239+
try {
240+
webViewManager!!.loadWebView(urlPath)
241+
} catch (e: Exception) {
242+
onP2PReadyErrorCallback.onError(e.message ?: "Unknown error")
243+
}
239244
}
240245
}

p2pml/src/main/java/com/novage/p2pml/interop/P2PReadyCallback.java renamed to p2pml/src/main/java/com/novage/p2pml/interop/OnP2PReadyCallback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.novage.p2pml.interop;
22

3-
public interface P2PReadyCallback {
3+
public interface OnP2PReadyCallback {
44
void onReady();
55
}

p2pml/src/main/java/com/novage/p2pml/interop/OnErrorCallback.java renamed to p2pml/src/main/java/com/novage/p2pml/interop/OnP2PReadyErrorCallback.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.novage.p2pml.interop;
22

3-
public interface OnErrorCallback {
3+
public interface OnP2PReadyErrorCallback {
44
void onError(String error);
55
}

p2pml/src/main/java/com/novage/p2pml/server/SegmentHandler.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ internal class SegmentHandler(
3939
val decodedSegmentUrl = Utils.decodeBase64Url(segmentUrlParam)
4040
val byteRange = call.request.headers[HttpHeaders.Range]
4141

42-
Log.d(
43-
"SegmentHandler",
44-
"Received segment request for $decodedSegmentUrl",
45-
)
46-
4742
try {
4843
val isCurrentSegment = parser.isCurrentSegment(decodedSegmentUrl)
4944
if (p2pEngineStateManager.isEngineDisabled() || !isCurrentSegment) {
@@ -58,7 +53,7 @@ internal class SegmentHandler(
5853

5954
respondSegment(call, segmentBytes, byteRange != null)
6055
} catch (e: SegmentAbortedException) {
61-
Log.e("SegmentHandler", "Segment request aborted: ${e.message}")
56+
Log.w("SegmentHandler", "Segment request aborted: ${e.message}")
6257
call.respondText(
6358
"Segment aborted",
6459
status = HttpStatusCode.RequestTimeout,

p2pml/src/main/java/com/novage/p2pml/server/ServerModule.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ internal class ServerModule(
2323
private val p2pEngineStateManager: P2PStateManager,
2424
private val customEngineImplementationPath: String? = null,
2525
private val onServerStarted: () -> Unit,
26+
private val onServerError: (String) -> Unit,
2627
private val onManifestChanged: suspend () -> Unit,
2728
) {
2829
private var httpClient: OkHttpClient? = null
@@ -31,12 +32,15 @@ internal class ServerModule(
3132
fun start(port: Int = 8080) {
3233
if (server != null) return
3334

34-
server =
35-
embeddedServer(CIO, port) {
35+
try {
36+
server = embeddedServer(CIO, port) {
3637
configureCORS(this)
3738
configureRouting(this)
3839
subscribeToServerStarted(this)
3940
}.start(wait = false)
41+
} catch (e: Exception) {
42+
onServerError(e.message ?: "Failed to start server on port $port")
43+
}
4044
}
4145

4246
private fun stopServer() {

0 commit comments

Comments
 (0)