Skip to content

Commit

Permalink
refactor(player): rename videoId to mediaId
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibaultBee committed Mar 19, 2024
1 parent bfcbf47 commit 567b883
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class MainActivity : AppCompatActivity() {
private val sharedPref: SharedPreferences by lazy {
PreferenceManager.getDefaultSharedPreferences(this)
}
private val videoId: String
get() = sharedPref.getString(getString(R.string.video_id_key), null)
?: throw IllegalArgumentException("Video ID is not set")
private val mediaId: String
get() = sharedPref.getString(getString(R.string.media_id_key), null)
?: throw IllegalArgumentException("MediaID is not set")
private val videoType: VideoType
get() {
val videoType =
Expand Down Expand Up @@ -190,7 +190,7 @@ class MainActivity : AppCompatActivity() {

private fun loadVideo() {
try {
playerController.videoOptions = VideoOptions(videoId, videoType, privateVideoToken)
playerController.videoOptions = VideoOptions(mediaId, videoType, privateVideoToken)
} catch (e: Exception) {
Log.e(TAG, "Error while loading video", e)
displayMessage("Error while loading video: ${e.message}")
Expand Down
6 changes: 3 additions & 3 deletions examples/view/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<string name="settings">Settings</string>

<!--Settings-->
<string name="default_video_id">vi77Dgk0F8eLwaFOtC5870yn</string>
<string name="video_id_key">video_id_key</string>
<string name="video_id">VideoId</string>
<string name="default_media_id">vi77Dgk0F8eLwaFOtC5870yn</string>
<string name="media_id_key">media_id_key</string>
<string name="media_id">MediaId</string>
<string name="video_type_key">video_type_key</string>
<string name="video_type_vod">VOD</string>
<string name="video_type_live">Live</string>
Expand Down
6 changes: 3 additions & 3 deletions examples/view/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
xmlns:app="http://schemas.android.com/apk/res-auto">

<EditTextPreference
app:defaultValue="@string/default_video_id"
app:key="@string/video_id_key"
app:title="@string/video_id"
app:defaultValue="@string/default_media_id"
app:key="@string/media_id_key"
app:title="@string/media_id"
app:useSimpleSummaryProvider="true" />

<SwitchPreference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ApiVideoExoPlayerMediaFactory(
.build()
return MediaItem.Builder()
.setUri(request.uri)
.setMediaId(videoOptions.videoId)
.setMediaId(videoOptions.mediaId)
.setTag(videoOptions)
.setMediaMetadata(mediaMetadata)
.setMimeType(
Expand Down
32 changes: 16 additions & 16 deletions player/src/main/java/video/api/player/models/VideoOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ import java.net.URL
/**
* Description of the video to play.
*
* @constructor Creates a [VideoOptions] from a [videoId]. an explicit [VideoType] and the private [token].
* @param videoId the video ID of the video to play
* @constructor Creates a [VideoOptions] from a [mediaId]. an explicit [VideoType] and the private [token].
* @param mediaId the video ID of the video or the live stream ID
* @param videoType the [VideoType] of the video to play. Only [VideoType.VOD] is supported.
* @param token the private video token (only needed for private video, set to null otherwise)
*/
data class VideoOptions(
val videoId: String,
val mediaId: String,
val videoType: VideoType,
val token: String? = null
) {
private val baseUrl = "${videoType.baseUrl}/$videoId"
private val baseUrl = "${videoType.baseUrl}/$mediaId"
private val vodUrl = baseUrl + (token?.let { "/token/$it" } ?: "")
private val liveUrl = videoType.baseUrl + (token?.let { "/private/$it" } ?: "") + "/$videoId"
private val liveUrl = videoType.baseUrl + (token?.let { "/private/$it" } ?: "") + "/$mediaId"

/**
* Creates a [VideoOptions] from a [videoId] and the private [token].
* Creates a [VideoOptions] from a [mediaId] and the private [token].
* The [VideoType] is inferred from the video ID.
*
* @param videoId the video ID of the video to play
* @param mediaId the video ID of the video or the live stream ID
* @param token the private video token (only needed for private video, set to null otherwise)
*/
constructor(videoId: String, token: String?) : this(
videoId,
inferVideoType(videoId),
constructor(mediaId: String, token: String?) : this(
mediaId,
inferVideoType(mediaId),
token
)

Expand Down Expand Up @@ -81,19 +81,19 @@ data class VideoOptions(
) = url.parseAsVideoOptions()

/**
* Infers the [VideoType] from the videoId.
* Infers the [VideoType] from the mediaId.
*
* @param videoId the video ID
* @param mediaId the video ID or the live stream ID
* @return the [VideoType]
*/
private fun inferVideoType(videoId: String): VideoType {
return if (videoId.startsWith("vi")) {
private fun inferVideoType(mediaId: String): VideoType {
return if (mediaId.startsWith("vi")) {
VideoType.VOD
} else if (videoId.startsWith("li")) {
} else if (mediaId.startsWith("li")) {
VideoType.LIVE
} else {
throw IllegalArgumentException(
"Failed to infer the video type from the videoId: $videoId"
"Failed to infer the video type from the mediaId: $mediaId"
)
}
}
Expand Down
6 changes: 3 additions & 3 deletions player/src/main/java/video/api/player/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ object Utils {
try {
matcher.find()
// Group naming is not supported before Android API 26
val videoId = matcher.group(2) ?: throw IOException("Failed to get videoId")
val mediaId = matcher.group(2) ?: throw IOException("Failed to get mediaId")

// For live, we might not have a type for now because there isn't any `/live/` in the URL.
val firstGroup = matcher.group(1)
val videoType = firstGroup?.toVideoType()
?: if (videoId.startsWith("li")) VideoType.LIVE else throw IOException(
?: if (mediaId.startsWith("li")) VideoType.LIVE else throw IOException(
"Failed to get videoType"
)

Expand All @@ -52,7 +52,7 @@ object Utils {
}

return VideoOptions(
videoId,
mediaId,
videoType,
token
)
Expand Down
24 changes: 12 additions & 12 deletions player/src/test/java/video/api/player/models/VideoOptionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class VideoOptionsTest {
fun `test parse embed vod url`() {
val videoOptions =
VideoOptions.fromUrl("https://embed.api.video/vod/vi5oNqxkifcXkT4auGNsvgZB")
assertEquals(videoOptions.videoId, "vi5oNqxkifcXkT4auGNsvgZB")
assertEquals(videoOptions.mediaId, "vi5oNqxkifcXkT4auGNsvgZB")
assertEquals(videoOptions.videoType, VideoType.VOD)
assertNull(videoOptions.token)
}
Expand All @@ -81,7 +81,7 @@ class VideoOptionsTest {
fun `test parse vod url`() {
val videoOptions =
VideoOptions.fromUrl("https://vod.api.video/vod/vi5oNqxkifcXkT4auGNsvgZB/hls/manifest.m3u8")
assertEquals(videoOptions.videoId, "vi5oNqxkifcXkT4auGNsvgZB")
assertEquals(videoOptions.mediaId, "vi5oNqxkifcXkT4auGNsvgZB")
assertEquals(videoOptions.videoType, VideoType.VOD)
assertNull(videoOptions.token)
}
Expand All @@ -90,7 +90,7 @@ class VideoOptionsTest {
fun `test parse private vod url`() {
val videoOptions =
VideoOptions.fromUrl("https://vod.api.video/vod/vi5oNqxkifcXkT4auGNsvgZB/token/PRIVATE_TOKEN/hls/manifest.m3u8")
assertEquals(videoOptions.videoId, "vi5oNqxkifcXkT4auGNsvgZB")
assertEquals(videoOptions.mediaId, "vi5oNqxkifcXkT4auGNsvgZB")
assertEquals(videoOptions.videoType, VideoType.VOD)
assertEquals(videoOptions.token, "PRIVATE_TOKEN")
}
Expand All @@ -99,7 +99,7 @@ class VideoOptionsTest {
fun `test parse MP4 vod url`() {
val videoOptions =
VideoOptions.fromUrl("https://vod.api.video/vod/vi5oNqxkifcXkT4auGNsvgZB/mp4/source.mp4")
assertEquals(videoOptions.videoId, "vi5oNqxkifcXkT4auGNsvgZB")
assertEquals(videoOptions.mediaId, "vi5oNqxkifcXkT4auGNsvgZB")
assertEquals(videoOptions.videoType, VideoType.VOD)
assertNull(videoOptions.token)
}
Expand All @@ -108,7 +108,7 @@ class VideoOptionsTest {
fun `test parse private MP4 vod url`() {
val videoOptions =
VideoOptions.fromUrl("https://vod.api.video/vod/vi5oNqxkifcXkT4auGNsvgZB/token/PRIVATE_TOKEN/mp4/source.mp4")
assertEquals(videoOptions.videoId, "vi5oNqxkifcXkT4auGNsvgZB")
assertEquals(videoOptions.mediaId, "vi5oNqxkifcXkT4auGNsvgZB")
assertEquals(videoOptions.videoType, VideoType.VOD)
assertEquals(videoOptions.token, "PRIVATE_TOKEN")
}
Expand All @@ -117,7 +117,7 @@ class VideoOptionsTest {
fun `test parse embed live url`() {
val videoOptions =
VideoOptions.fromUrl("https://embed.api.video/live/li77ACbZjzEJgmr8d0tm4xFt")
assertEquals(videoOptions.videoId, "li77ACbZjzEJgmr8d0tm4xFt")
assertEquals(videoOptions.mediaId, "li77ACbZjzEJgmr8d0tm4xFt")
assertEquals(videoOptions.videoType, VideoType.LIVE)
assertNull(videoOptions.token)
}
Expand All @@ -126,7 +126,7 @@ class VideoOptionsTest {
fun `test parse live url`() {
val videoOptions =
VideoOptions.fromUrl("https://live.api.video/li77ACbZjzEJgmr8d0tm4xFt.m3u8")
assertEquals(videoOptions.videoId, "li77ACbZjzEJgmr8d0tm4xFt")
assertEquals(videoOptions.mediaId, "li77ACbZjzEJgmr8d0tm4xFt")
assertEquals(videoOptions.videoType, VideoType.LIVE)
assertNull(videoOptions.token)
}
Expand All @@ -135,7 +135,7 @@ class VideoOptionsTest {
fun `test parse private live url`() {
val videoOptions =
VideoOptions.fromUrl("https://live.api.video/private/PRIVATE_TOKEN/li77ACbZjzEJgmr8d0tm4xFt.m3u8")
assertEquals(videoOptions.videoId, "li77ACbZjzEJgmr8d0tm4xFt")
assertEquals(videoOptions.mediaId, "li77ACbZjzEJgmr8d0tm4xFt")
assertEquals(videoOptions.videoType, VideoType.LIVE)
assertEquals(videoOptions.token, "PRIVATE_TOKEN")
}
Expand All @@ -146,7 +146,7 @@ class VideoOptionsTest {
VideoOptions.fromUrl(
"https://mycustom.vod.domain/vod/vi5oNqxkifcXkT4auGNsvgZB/hls/manifest.m3u8"
)
assertEquals(videoOptions.videoId, "vi5oNqxkifcXkT4auGNsvgZB")
assertEquals(videoOptions.mediaId, "vi5oNqxkifcXkT4auGNsvgZB")
assertEquals(videoOptions.videoType, VideoType.VOD)
assertNull(videoOptions.token)
}
Expand All @@ -157,7 +157,7 @@ class VideoOptionsTest {
VideoOptions.fromUrl(
"https://mycustom.vod.domain/vod/vi5oNqxkifcXkT4auGNsvgZB/token/PRIVATE_TOKEN/hls/manifest.m3u8"
)
assertEquals(videoOptions.videoId, "vi5oNqxkifcXkT4auGNsvgZB")
assertEquals(videoOptions.mediaId, "vi5oNqxkifcXkT4auGNsvgZB")
assertEquals(videoOptions.videoType, VideoType.VOD)
assertEquals(videoOptions.token, "PRIVATE_TOKEN")
}
Expand All @@ -167,7 +167,7 @@ class VideoOptionsTest {
val videoOptions = VideoOptions.fromUrl(
"https://mycustom.live.domain/li77ACbZjzEJgmr8d0tm4xFt.m3u8"
)
assertEquals(videoOptions.videoId, "li77ACbZjzEJgmr8d0tm4xFt")
assertEquals(videoOptions.mediaId, "li77ACbZjzEJgmr8d0tm4xFt")
assertEquals(videoOptions.videoType, VideoType.LIVE)
assertNull(videoOptions.token)
}
Expand All @@ -178,7 +178,7 @@ class VideoOptionsTest {
VideoOptions.fromUrl(
"https://mycustom.live.domain/private/PRIVATE_TOKEN/li77ACbZjzEJgmr8d0tm4xFt.m3u8"
)
assertEquals(videoOptions.videoId, "li77ACbZjzEJgmr8d0tm4xFt")
assertEquals(videoOptions.mediaId, "li77ACbZjzEJgmr8d0tm4xFt")
assertEquals(videoOptions.videoType, VideoType.LIVE)
assertEquals(videoOptions.token, "PRIVATE_TOKEN")
}
Expand Down

0 comments on commit 567b883

Please sign in to comment.