media3/libraries/datasource_okhttp
yschimke 80928e730c Workaround for OkHttp Interrupt issues.
Relates to https://github.com/square/okhttp/issues/3146. This was from https://github.com/androidx/media/pull/71.

There is a draft PR https://github.com/square/okhttp/pull/7185/files which documents OkHttp's ideal handling of cancellation including interrupts.

But a few key points

1) This is a target state, and OkHttp does not currently handle interrupts correctly.  In the past this has been identified, and the advice is to avoid interrupts on Http threads, see discussion on https://github.com/square/okhttp/issues/1902. Also an attempt at a fix here https://github.com/square/okhttp/pull/7023 which wasn't in a form to land.

2) Even with this fixed, it is likely to never be optimal, because of OkHttp sharing a socket connection for multiple inflight requests.

From https://github.com/square/okhttp/pull/7185

```
Thread.interrupt() is Clumsy
----------------------------

`Thread.interrupt()` is Java's built-in mechanism to cancel an in-flight `Thread`, regardless of
what work it's currently performing.

We recommend against using `Thread.interrupt()` with OkHttp because it may disrupt shared resources
including HTTP/2 connections and cache files. In particular, calling `Thread.interrupt()` may cause
unrelated threads' call to fail with an `IOException`.
```

This PR leaves the Loader/DataSource thread parked on a countdown latch, while this may seem wasteful and an additional context switch. However in practice the response isn't returned until the Http2Connection and Http2Reader have a response from the server and these means effectively parking in a `wait()` statement here 9e039e9412/okhttp/src/jvmMain/kotlin/okhttp3/internal/http2/Http2Stream.kt (L140)

PiperOrigin-RevId: 446652468
2022-05-09 10:59:41 +01:00
..
2021-10-27 09:12:46 +01:00
2021-10-27 09:12:46 +01:00
2021-10-27 09:12:46 +01:00

OkHttp DataSource module

This module provides an HttpDataSource implementation that uses Square's OkHttp.

OkHttp is a modern network stack that's widely used by many popular Android applications. It supports the HTTP and HTTP/2 protocols.

License note

Please note that whilst the code in this repository is licensed under Apache 2.0, using this extension requires depending on OkHttp, which is licensed separately.

Getting the module

The easiest way to get the module is to add it as a gradle dependency:

implementation 'androidx.media3:media3-datasource-okhttp: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.

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 OkHttp extension is as simple as updating any DataSource.Factory instantiations in your application code to use OkHttpDataSource.Factory. If your application also needs to play non-http(s) content such as local files, use:

new DefaultDataSourceFactory(
    ...
    /* baseDataSourceFactory= */ new OkHttpDataSource.Factory(...));