media icon indicating copy to clipboard operation
media copied to clipboard

Ktor support

Open JJSarrasin opened this issue 1 year ago • 1 comments

The standard for HTTP clients is increasingly shifting towards Ktor. It avoids to manage multiple http source in the same project.

My main concern is about DrmSessionManagerProvider.setDrmHttpDataSourceFactory + MediaSourceFactory.setDataSourceFactory, maybe there are other entry points.

Note that Ktor is using OkHttp (or other engines) under the hood, it's more like a convenient way to use Ktor everywhere from a developer perspective. It won't improve any performance or so.

JJSarrasin avatar Aug 22 '24 14:08 JJSarrasin

My main concern is about DrmSessionManagerProvider.setDrmHttpDataSourceFactory + MediaSourceFactory.setDataSourceFactory, maybe there are other entry points.

Can you please add a bit more concrete info on how Ktor would resolve this? At the moment these are configured with different DataSource.Factory instances, and each default to DefaultHttpDataSource.Factory for handling HTTP URIs.

What are you suggesting would change here?

Note that we probably wouldn't accept a dependency on Ktor from the main exoplayer module, so any integration would need to be in an extension (similar to the datasource-okhttp extension).

icbaker avatar Aug 27 '24 16:08 icbaker

From my understanding, there is a dependency between media3 and okhttp.

Here is the minimal use we do from exo:

val renderersFactory = DefaultRenderersFactory(context)
val dataSourceFactory = OkHttpDataSource.Factory(httpClient)

val drmSessionManagerProvider = DefaultDrmSessionManagerProvider()
drmSessionManagerProvider.setDrmHttpDataSourceFactory(dataSourceFactory)

val mediaSourceFactory = DefaultMediaSourceFactory(context)
    .setLoadErrorHandlingPolicy(DefaultLoadErrorHandlingPolicy(2))
    .setDrmSessionManagerProvider(drmSessionManagerProvider)

mediaSourceFactory.setDataSourceFactory(dataSourceFactory)

val exoPlayer = ExoPlayer.Builder(context, renderersFactory)
    .setMediaSourceFactory(mediaSourceFactory)
    .build()

The passed httpClient is a OkHttpclient variable.

Of course, we won't use media3 on the iOS/web part of our project, so we'll only use the okhttp client implementation of ktor wrapper. We're using Ktor for all the common part of the code and it's kind of the main lib for doing network request in modern KMP projects.

Maybe we should ask Ktor lib to expose a way to get the OkHttpEngine/OkHttpClient instead ?

JJSarrasin avatar Aug 31 '24 08:08 JJSarrasin

Maybe we should ask Ktor lib to expose a way to get the OkHttpEngine/OkHttpClient instead ?

This seems like a a reasonable request to me.


Regarding the suggestion for a KtorDataSource, we're unlikely to work on this soon I'm afraid, but we would accept a high quality PR. Any such PR should include a subclass of DataSourceContractTest (see an example in OkHttpDataSourceContractTest).

icbaker avatar Sep 04 '24 15:09 icbaker

Maybe we should ask Ktor lib to expose a way to get the OkHttpEngine/OkHttpClient instead ?

That would be pointless as ktor is design to be http client agnostic, and is recommending using CIO for modern development.

OkHttp is only used as a convenient fallback when CIO cannot be used, but it cannot be part of the ktor API.

Jean-Daniel avatar Apr 04 '25 15:04 Jean-Daniel

Ktor may use okhttp under the hood, but if your authentication/token refresh is handled in ktor (e.g., shared in KMP), then you're kind of hosed. You then need to have multiple clients, and it's very messy. Givilen the growing popularity of KMP, this should probably get a bump in priority.

mandrachek avatar Sep 17 '25 14:09 mandrachek

The solution is not to expose okhttp, but to write a proper KtorDataSource.Factory.

Jean-Daniel avatar Sep 17 '25 17:09 Jean-Daniel