Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add separate network class to fetch videoInfo #62

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions app/src/main/java/com/tpstream/app/PlayerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class PlayerActivity : AppCompatActivity() {
private lateinit var accessToken :String
private lateinit var videoId :String
private lateinit var orgCode :String
private var product :String = "testpress"
private var parameters : TpInitParams? = null

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -40,6 +41,7 @@ class PlayerActivity : AppCompatActivity() {
fun play(){
if (parameters == null){
parameters = TpInitParams.Builder()
.setProduct(product)
.setVideoId(videoId)
.setAccessToken(accessToken)
.setOrgCode(orgCode)
Expand All @@ -64,9 +66,10 @@ class PlayerActivity : AppCompatActivity() {
orgCode = "demoveranda"
}
"Clear" -> {
accessToken = "70f61402-3724-4ed8-99de-5473b2310efe"
videoId = "qJQlWGLJvNv"
orgCode = "demoveranda"
accessToken = "4b11bf9e-d6b7-4b1f-80b8-19d92b26e966"
videoId = "73633fa3-61c6-443c-b625-ac4e85b28cfc"
orgCode = "edee9b"
product = "tpstreams"
}
null ->{}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ internal class CustomHttpDrmMediaCallback(context: Context, private val tpInitPa
private val httpMediaDrmCallback = HttpMediaDrmCallback("", VideoDownloadManager(context).getHttpDataSourceFactory())

private fun fetchDRMLicenseURL(): String {
val url = "/api/v2.5/drm_license/${tpInitParams.videoId}/?access_token=${tpInitParams.accessToken}"
val url = "https://${tpInitParams.orgCode}.testpress.in/api/v2.5/drm_license/${tpInitParams.videoId}/?access_token=${tpInitParams.accessToken}"
val body: RequestBody = ("{\"download\":true}").toRequestBody("application/json".toMediaTypeOrNull())
return try {
val result = Network<DRMLicenseURL>(tpInitParams.orgCode).post(url, body)
val result = Network<DRMLicenseURL>().post(url, body)
result?.licenseUrl ?: ""
} catch (exception:TPException){
SentryLogger.logAPIException(exception,tpInitParams)
Expand Down
9 changes: 4 additions & 5 deletions player/src/main/java/com/tpstream/player/Network.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@ import okhttp3.*
import java.io.IOException
import java.net.URL

internal class Network<T : Any>(val klass: Class<T>, val subdomain: String) {
class Network<T : Any>(val klass: Class<T>) {
companion object {
inline operator fun <reified T : Any>invoke(subdomain: String) = Network(T::class.java, subdomain)
inline operator fun <reified T : Any>invoke() = Network(T::class.java)
}

private val BASE_URL = "https://${subdomain}.testpress.in"
private var client: OkHttpClient = OkHttpClient();
private val gson = Gson()

fun get(url: String, callback: TPResponse<T>? = null): T? {
val request = Request.Builder().url(URL("$BASE_URL$url")).build()
val request = Request.Builder().url(URL(url)).build()
return makeRequest(callback, request)
}

fun post(url: String, body: RequestBody, callback: TPResponse<T>? = null): T? {
val request = Request.Builder().url(URL("$BASE_URL$url")).post(body).build()
val request = Request.Builder().url(URL(url)).post(body).build()
return makeRequest(callback, request)
}

Expand Down
20 changes: 18 additions & 2 deletions player/src/main/java/com/tpstream/player/TpInitParams.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ data class TpInitParams (
var signature: String? = null,
var techOverride: Array<String>? = null,
var isDownloadEnabled: Boolean = false,
var startAt: Long = 0L
var startAt: Long = 0L,
internal var product: String? = null, // The product should be Testpress or TpStreams
internal var isTPStreams: Boolean = false
): Parcelable {

class Builder {
private val testpress = "testpress"
private var tpstreams = "tpstreams"
private var autoPlay: Boolean? = null
private var bufferingGoalMs: Int? = null
private var forceHighestSupportedBitrate: Boolean? = null
Expand All @@ -38,6 +42,8 @@ data class TpInitParams (
private var techOverride: Array<String>? = null
private var isDownloadEnabled: Boolean = false
private var startAt: Long = 0L
private var product: String = testpress // The product default value as testpress
private var isTPStreams: Boolean = false

fun setAutoPlay(autoPlay: Boolean) = apply { this.autoPlay = autoPlay }
fun startAt(timeInSeconds: Long) = apply { this.startAt = timeInSeconds }
Expand All @@ -53,11 +59,15 @@ data class TpInitParams (
fun setSignature(signature: String) = apply { this.signature = signature }
fun setTechOverride(techOverride: Array<String>) = apply { this.techOverride = techOverride }
fun enableDownloadSupport(isDownloadEnabled: Boolean) = apply { this.isDownloadEnabled = isDownloadEnabled }
fun setProduct(product: String) = apply {
this.product = if (product.lowercase() == tpstreams) tpstreams else testpress
}

fun build(): TpInitParams {
if (orgCode == null) {
throw Exception("orgCode should be provided")
}
validateProduct()

return TpInitParams(
autoPlay,
Expand All @@ -74,9 +84,15 @@ data class TpInitParams (
signature,
techOverride,
isDownloadEnabled,
startAt
startAt,
product,
isTPStreams
)
}

private fun validateProduct(){
isTPStreams = product == tpstreams
}
}

val startPositionInMilliSecs: Long
Expand Down
4 changes: 1 addition & 3 deletions player/src/main/java/com/tpstream/player/TpStreamPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ internal class TpStreamPlayerImpl(val context: Context) : TpStreamPlayer {
parameters: TpInitParams,
onError: (exception: TPException) -> Unit
) {
val url =
"/api/v2.5/video_info/${parameters.videoId}/?access_token=${parameters.accessToken}"
Network<VideoInfo>(parameters.orgCode).get(url, object : Network.TPResponse<VideoInfo> {
VideoInfoFetcher(parameters).fetch(object :VideoInfoFetcher.VideoInfoCallback{
override fun onSuccess(result: VideoInfo) {
videoInfo = result
playVideoInUIThread(result.getPlaybackURL(), parameters.startPositionInMilliSecs)
Expand Down
54 changes: 54 additions & 0 deletions player/src/main/java/com/tpstream/player/VideoInfoFetcher.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.tpstream.player

import android.util.Log
import com.tpstream.player.models.TpStreamVideoInfo
import com.tpstream.player.models.VideoInfo

internal class VideoInfoFetcher(private val params: TpInitParams) {

private var videoInfoCallback: VideoInfoCallback? = null

fun fetch(videoInfoCallback: VideoInfoCallback) {
this.videoInfoCallback = videoInfoCallback
Log.d("TAG", "fetch: ${params.isTPStreams}")
if (params.isTPStreams) {
fetchTpStreamVideoInfo()
} else {
fetchTestPressVideoInfo()
}
}

private fun fetchTestPressVideoInfo() {
val url =
"https://${params.orgCode}.testpress.in/api/v2.5/video_info/${params.videoId}/?access_token=${params.accessToken}"
Network<VideoInfo>().get(url, object : Network.TPResponse<VideoInfo> {
override fun onSuccess(result: VideoInfo) {
videoInfoCallback?.onSuccess(result)
}

override fun onFailure(exception: TPException) {
videoInfoCallback?.onFailure(exception)
}
})
}

private fun fetchTpStreamVideoInfo() {
val url = "https://app.tpstreams.com/api/v1/${params.orgCode}/assets/${params.videoId}/?access_token=${params.accessToken}"
Network<TpStreamVideoInfo>().get(
url,
object : Network.TPResponse<TpStreamVideoInfo> {
override fun onSuccess(result: TpStreamVideoInfo) {
videoInfoCallback?.onSuccess(result.asVideoInfo())
}

override fun onFailure(exception: TPException) {
videoInfoCallback?.onFailure(exception)
}
})
}

interface VideoInfoCallback {
fun onSuccess(result: VideoInfo)
fun onFailure(exception: TPException)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.tpstream.player.models

internal data class TpStreamVideoInfo(
val id: String?,
val title: String?,
val bytes: Long?,
val type: String?,
val video: Video?
) {

data class Video(
val progress: Int?,
val thumbnails: Array<String>?,
val status: String?,
val playback_url: String?,
val dash_url: String?,
val preview_thumbnail_url: String?,
val format: String?,
val resolutions: Array<String>?,
val video_codec: String?,
val audio_codec: String?,
val enable_drm: Boolean?
)

fun asVideoInfo():VideoInfo{
return VideoInfo(
title = this.title,
thumbnail = "",
thumbnailSmall = "",
thumbnailMedium = "",
url = this.video?.playback_url,
dashUrl = if (this.video?.enable_drm == true) this.video.dash_url else null,
hlsUrl = "",
duration = "",
description = "",
transcodingStatus = ""
)
}
}