-
Notifications
You must be signed in to change notification settings - Fork 785
Add Ktor HTTP data source module #3071
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
Open
k88936
wants to merge
32
commits into
androidx:main
Choose a base branch
from
k88936:feat/ktor-datasource
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
ad60ff4
Add Ktor HTTP data source module
k88936 b4a3e1b
Add timeouts to speed up KtorDataSourceContractTest 'not found' tests
icbaker acfc387
Reformat with ktfmt
icbaker fd7d188
Move tests from Robolectric to instrumentation
icbaker 03a1dad
Add copyright headers
icbaker e8059b2
Remove parts of README that duplicate Ktor docs about customization
icbaker 70a5143
remove ktor specific Proguard rules. they should be left to the user …
k88936 3954a78
Remove KtorDataSource from the stable API for now
icbaker e8efd2a
remove resolve case-insensitive step
k88936 3ab6985
remove Redundant 'requireNotNull' call
k88936 aa0fe71
Revert "remove resolve case-insensitive step"
k88936 7d9cc25
change to use ktor engine -android impl in test.
k88936 3679598
refactor: instead of providing configurable CoroutineScope, simplify …
k88936 269919e
dep: add dependency 'io.ktor:ktor-client-android:' as default choice
k88936 fb797ba
Various small doc, case-sensitibity & nullability fixes
icbaker 10bc0e2
Remove manual setting of zero-length body
icbaker 95c16f6
Remove setCacheControl, add some ignore-return annotations
icbaker 04605fc
Assert result of open in tests to resolve lint error
icbaker b99f502
Increase timeouts to deflake KtorDataSourceContractTest
icbaker 1183c92
test: add unit tests for `KtorDataSource.Factory` methods - `setUserA…
k88936 1ca74f3
fix flaky race condition in KtorDataSource when processing error mess…
k88936 e65d07b
replace kotlinx.io dependency from KtorDataSource
k88936 19e22fe
Add ktor to JARs list
icbaker 6bae1c6
Remove unneeded proguard rules file
icbaker 5bd6ee8
Move nested classes to bottom of outer class
icbaker d76a7d0
Add release note
icbaker deeba22
Resolve lint error by using result of open() in test
icbaker ccbfc6e
Fix some lint warnings
icbaker cc78baa
Resolve some feedback from internal review
icbaker 8c77927
Resolve internal review feedback
icbaker 82472f9
Fix lint error by using result of open() in tests
icbaker f9dd205
Configure timeout to make tests pass with OkHttp internally
icbaker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # Ktor DataSource module | ||
|
|
||
| This module provides an [HttpDataSource] implementation that uses [Ktor]. | ||
|
|
||
| Ktor is a multiplatform HTTP client developed by JetBrains. It supports HTTP/2, | ||
| WebSocket, and Kotlin coroutines for asynchronous operations. | ||
|
|
||
| [HttpDataSource]: ../datasource/src/main/java/androidx/media3/datasource/HttpDataSource.java | ||
| [Ktor]: https://ktor.io/ | ||
|
|
||
| ## Getting the module | ||
|
|
||
| The easiest way to get the module is to add it as a gradle dependency: | ||
|
|
||
| ```groovy | ||
| implementation 'androidx.media3:media3-datasource-ktor:1.X.X' | ||
| ``` | ||
|
|
||
| where `1.X.X` is the version, which must match the version of the other media | ||
| modules being used. | ||
|
|
||
| Alternatively, you can clone this GitHub project and depend on the module | ||
| locally. Instructions for doing this can be found in the [top level README]. | ||
|
|
||
| [top level README]: ../../README.md | ||
|
|
||
| ## Using the module | ||
|
|
||
| Media components request data through `DataSource` instances. These instances | ||
| are obtained from instances of `DataSource.Factory`, which are instantiated and | ||
| injected from application code. | ||
|
|
||
| If your application only needs to play http(s) content, using the Ktor | ||
| extension is as simple as updating any `DataSource.Factory` instantiations in | ||
| your application code to use `KtorDataSource.Factory`. If your application | ||
| also needs to play non-http(s) content such as local files, use: | ||
| ``` | ||
| new DefaultDataSourceFactory( | ||
| ... | ||
| /* baseDataSourceFactory= */ new KtorDataSource.Factory(...)); | ||
| ``` | ||
|
|
||
| ## Links | ||
|
|
||
| * [Javadoc][] | ||
|
|
||
| [Javadoc]: https://developer.android.com/reference/androidx/media3/datasource/ktor/package-summary | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Copyright 2026 The Android Open Source Project | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // https://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| import org.jetbrains.kotlin.gradle.dsl.JvmTarget | ||
|
|
||
| plugins { | ||
| alias(libs.plugins.android.library) | ||
| alias(libs.plugins.kotlin.android) | ||
| } | ||
|
|
||
| apply(from = "${gradle.extra["androidxMediaSettingsDir"]}/common_config.gradle") | ||
|
|
||
| android { | ||
| namespace = "androidx.media3.datasource.ktor" | ||
|
|
||
| publishing { singleVariant("release") { withSourcesJar() } } | ||
| kotlin { compilerOptions { jvmTarget.set(JvmTarget.JVM_1_8) } } | ||
| } | ||
|
|
||
| dependencies { | ||
| api(libs.ktor.client.android) | ||
| api(libs.ktor.client.core) | ||
| api(project(modulePrefix + "lib-common")) | ||
| api(project(modulePrefix + "lib-datasource")) | ||
| implementation(libs.androidx.annotation) | ||
| compileOnly(libs.errorprone.annotations) | ||
| compileOnly(libs.checkerframework.qual) | ||
| compileOnly(libs.kotlin.annotations.jvm) | ||
| implementation(libs.kotlinx.coroutines.core) | ||
| androidTestImplementation(project(modulePrefix + "test-utils")) | ||
| androidTestImplementation(libs.androidx.test.runner) | ||
| androidTestImplementation(libs.dexmaker.mockito) | ||
| androidTestImplementation(libs.ktor.client.okhttp) | ||
| androidTestImplementation(libs.okhttp.mockwebserver) | ||
| androidTestImplementation(libs.test.parameter.injector) | ||
| } | ||
|
|
||
| extra["releaseArtifactId"] = "media3-datasource-ktor" | ||
|
|
||
| extra["releaseName"] = "Media3 Ktor DataSource module" | ||
|
|
||
| apply(from = "../../publish.gradle") |
28 changes: 28 additions & 0 deletions
28
libraries/datasource_ktor/src/androidTest/AndroidManifest.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <!-- Copyright 2026 The Android Open Source Project | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| https://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| --> | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
| package="androidx.media3.datasource.ktor.test"> | ||
|
|
||
| <uses-permission android:name="android.permission.INTERNET"/> | ||
| <uses-sdk/> | ||
|
|
||
| <application | ||
| android:usesCleartextTraffic="true" /> | ||
|
|
||
| <instrumentation | ||
| android:targetPackage="androidx.media3.datasource.ktor.test" | ||
| android:name="androidx.test.runner.AndroidJUnitRunner"/> | ||
| </manifest> |
67 changes: 67 additions & 0 deletions
67
...e_ktor/src/androidTest/java/androidx/media3/datasource/ktor/KtorDataSourceContractTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * Copyright 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package androidx.media3.datasource.ktor | ||
|
|
||
| import androidx.media3.datasource.DataSource | ||
| import androidx.media3.test.utils.DataSourceContractTest | ||
| import androidx.media3.test.utils.HttpDataSourceTestEnv | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import com.google.common.collect.ImmutableList | ||
| import io.ktor.client.HttpClient | ||
| import io.ktor.client.engine.android.Android | ||
| import io.ktor.client.engine.okhttp.OkHttp | ||
| import io.ktor.client.plugins.HttpTimeout | ||
| import org.junit.Rule | ||
| import org.junit.runner.RunWith | ||
|
|
||
| @RunWith(AndroidJUnit4::class) | ||
| class KtorDataSourceContractTest : DataSourceContractTest() { | ||
|
|
||
| @JvmField @Rule var httpDataSourceTestEnv = HttpDataSourceTestEnv() | ||
|
|
||
| override fun createDataSources(): List<DataSource> { | ||
| return listOf( | ||
| KtorDataSource.Factory( | ||
| HttpClient(Android) { | ||
| install(HttpTimeout) { | ||
| requestTimeoutMillis = 800 | ||
| connectTimeoutMillis = 800 | ||
| socketTimeoutMillis = 800 | ||
| } | ||
| } | ||
| ) | ||
| .createDataSource(), | ||
| KtorDataSource.Factory( | ||
| HttpClient(OkHttp) { | ||
| install(HttpTimeout) { | ||
| requestTimeoutMillis = 800 | ||
| connectTimeoutMillis = 800 | ||
| socketTimeoutMillis = 800 | ||
| } | ||
| } | ||
| ) | ||
| .createDataSource(), | ||
| ) | ||
| } | ||
|
|
||
| override fun getTestResources(): ImmutableList<TestResource> { | ||
| return httpDataSourceTestEnv.servedResources | ||
| } | ||
|
|
||
| override fun getNotFoundResources(): MutableList<TestResource> { | ||
| return httpDataSourceTestEnv.notFoundResources | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.