Skip to content

Commit fcf280b

Browse files
committed
Use content-length header
1 parent ffd9507 commit fcf280b

1 file changed

Lines changed: 24 additions & 22 deletions

File tree

agama/android/src/main/java/com/theoplayeragama/ReactTHEOplayerAgamaModule.kt

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ class ReactTHEOplayerAgamaModule(private val reactContext: ReactApplicationConte
4343
view?.player?.let { player ->
4444
interceptor = object : AgamaInterceptor() {
4545
override suspend fun onResponse(response: InterceptableHTTPResponse) {
46-
if (response.request.type == RequestType.SEGMENT) {
47-
onSegmentResponse(response)
48-
} else if (response.request.type == RequestType.MANIFEST) {
49-
onManifestResponse(response)
46+
when (response.request.type) {
47+
RequestType.SEGMENT -> onSegmentResponse(response)
48+
RequestType.MANIFEST -> onManifestResponse(response)
49+
else -> { /*ignore*/
50+
}
5051
}
5152
}
5253
}.also {
@@ -85,25 +86,26 @@ class ReactTHEOplayerAgamaModule(private val reactContext: ReactApplicationConte
8586
}
8687

8788
fun onSegmentResponse(response: InterceptableHTTPResponse) {
88-
response.onBody { body ->
89-
val mediaType = mediaTypeToString(response.request.mediaType)
90-
val contentLength = body.size.toDouble()
91-
if (debug) {
92-
Log.d(
93-
TAG, "onSegmentResponse - " +
94-
"url(${response.url}) " +
95-
"mediaType($mediaType) " +
96-
"status(${response.status}) " +
97-
"bytes(${contentLength.toLong()}) "
98-
)
99-
}
100-
sendEvent(reactContext, EVENT_SEGMENT_RESPONSE, Arguments.createMap().apply {
101-
putInt(PROP_STATUS, response.status)
102-
putString(PROP_MEDIA_TYPE, mediaType)
103-
putDouble(PROP_TOTAL_BYTES_LOADED, contentLength)
104-
})
105-
body
89+
val mediaType = mediaTypeToString(response.request.mediaType)
90+
val contentLength = response.headers.entries.firstOrNull {
91+
it.key.equals("Content-Length", ignoreCase = true)
92+
}?.value?.toDoubleOrNull() ?: 0.0
93+
94+
if (debug) {
95+
Log.d(
96+
TAG, "onSegmentResponse - " +
97+
"url(${response.url}) " +
98+
"mediaType($mediaType) " +
99+
"status(${response.status}) " +
100+
"bytes(${contentLength.toLong()}) "
101+
)
106102
}
103+
sendEvent(reactContext, EVENT_SEGMENT_RESPONSE, Arguments.createMap().apply {
104+
putInt(PROP_STATUS, response.status)
105+
putString(PROP_MEDIA_TYPE, mediaType)
106+
putDouble(PROP_TOTAL_BYTES_LOADED, contentLength)
107+
})
108+
107109
}
108110

109111
private fun mediaTypeToString(mediaType: RequestMediaType): String {

0 commit comments

Comments
 (0)