1*30877f79SAndroid Build Coastguard Worker# Cronet DataSource module
2*30877f79SAndroid Build Coastguard Worker
3*30877f79SAndroid Build Coastguard WorkerThis module provides an [HttpDataSource][] implementation that uses [Cronet][].
4*30877f79SAndroid Build Coastguard Worker
5*30877f79SAndroid Build Coastguard WorkerCronet is the Chromium network stack made available to Android apps as a
6*30877f79SAndroid Build Coastguard Workerlibrary. It takes advantage of multiple technologies that reduce the latency and
7*30877f79SAndroid Build Coastguard Workerincrease the throughput of the network requests that your app needs to work. It
8*30877f79SAndroid Build Coastguard Workernatively supports the HTTP, HTTP/2, and HTTP/3 over QUIC protocols. Cronet is
9*30877f79SAndroid Build Coastguard Workerused by some of the world's biggest streaming applications, including YouTube,
10*30877f79SAndroid Build Coastguard Workerand is our recommended network stack for most use cases.
11*30877f79SAndroid Build Coastguard Worker
12*30877f79SAndroid Build Coastguard Worker[HttpDataSource]: https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/upstream/HttpDataSource.html
13*30877f79SAndroid Build Coastguard Worker[Cronet]: https://developer.android.com/guide/topics/connectivity/cronet
14*30877f79SAndroid Build Coastguard Worker
15*30877f79SAndroid Build Coastguard Worker## Getting the module
16*30877f79SAndroid Build Coastguard Worker
17*30877f79SAndroid Build Coastguard WorkerThe easiest way to get the module is to add it as a gradle dependency:
18*30877f79SAndroid Build Coastguard Worker
19*30877f79SAndroid Build Coastguard Worker```gradle
20*30877f79SAndroid Build Coastguard Workerimplementation 'com.google.android.exoplayer:extension-cronet:2.X.X'
21*30877f79SAndroid Build Coastguard Worker```
22*30877f79SAndroid Build Coastguard Worker
23*30877f79SAndroid Build Coastguard Workerwhere `2.X.X` is the version, which must match the version of the other media
24*30877f79SAndroid Build Coastguard Workermodules being used.
25*30877f79SAndroid Build Coastguard Worker
26*30877f79SAndroid Build Coastguard WorkerAlternatively, you can clone this GitHub project and depend on the module
27*30877f79SAndroid Build Coastguard Workerlocally. Instructions for doing this can be found in the [top level README][].
28*30877f79SAndroid Build Coastguard Worker
29*30877f79SAndroid Build Coastguard Worker[top level README]: https://github.com/google/ExoPlayer/blob/release-v2/README.md
30*30877f79SAndroid Build Coastguard Worker
31*30877f79SAndroid Build Coastguard Worker## Using the module
32*30877f79SAndroid Build Coastguard Worker
33*30877f79SAndroid Build Coastguard WorkerMedia components request data through `DataSource` instances. These instances
34*30877f79SAndroid Build Coastguard Workerare obtained from instances of `DataSource.Factory`, which are instantiated and
35*30877f79SAndroid Build Coastguard Workerinjected from application code.
36*30877f79SAndroid Build Coastguard Worker
37*30877f79SAndroid Build Coastguard WorkerIf your application only needs to play http(s) content, using the Cronet
38*30877f79SAndroid Build Coastguard Workerextension is as simple as updating `DataSource.Factory` instantiations in your
39*30877f79SAndroid Build Coastguard Workerapplication code to use `CronetDataSource.Factory`. If your application also
40*30877f79SAndroid Build Coastguard Workerneeds to play non-http(s) content such as local files, use:
41*30877f79SAndroid Build Coastguard Worker
42*30877f79SAndroid Build Coastguard Worker```
43*30877f79SAndroid Build Coastguard Workernew DefaultDataSource.Factory(
44*30877f79SAndroid Build Coastguard Worker    ...
45*30877f79SAndroid Build Coastguard Worker    /* baseDataSourceFactory= */ new CronetDataSource.Factory(...) );
46*30877f79SAndroid Build Coastguard Worker```
47*30877f79SAndroid Build Coastguard Worker
48*30877f79SAndroid Build Coastguard Worker## Cronet implementations
49*30877f79SAndroid Build Coastguard Worker
50*30877f79SAndroid Build Coastguard WorkerTo instantiate a `CronetDataSource.Factory` you'll need a `CronetEngine`. A
51*30877f79SAndroid Build Coastguard Worker`CronetEngine` can be obtained from one of a number of Cronet implementations.
52*30877f79SAndroid Build Coastguard WorkerIt's recommended that an application should only have a single `CronetEngine`
53*30877f79SAndroid Build Coastguard Workerinstance.
54*30877f79SAndroid Build Coastguard Worker
55*30877f79SAndroid Build Coastguard Worker### Available implementations
56*30877f79SAndroid Build Coastguard Worker
57*30877f79SAndroid Build Coastguard Worker#### Google Play Services
58*30877f79SAndroid Build Coastguard Worker
59*30877f79SAndroid Build Coastguard WorkerBy default, this module depends on
60*30877f79SAndroid Build Coastguard Worker`com.google.android.gms:play-services-cronet`, which loads an implementation of
61*30877f79SAndroid Build Coastguard WorkerCronet from Google Play Services. When Google Play Services is available, this
62*30877f79SAndroid Build Coastguard Workerapproach is beneficial because:
63*30877f79SAndroid Build Coastguard Worker
64*30877f79SAndroid Build Coastguard Worker* The increase in application size is negligible.
65*30877f79SAndroid Build Coastguard Worker* The implementation is updated automatically by Google Play Services.
66*30877f79SAndroid Build Coastguard Worker
67*30877f79SAndroid Build Coastguard WorkerThe disadvantage of this approach is that the implementation is not usable on
68*30877f79SAndroid Build Coastguard Workerdevices that do not have Google Play Services. Unless your application also
69*30877f79SAndroid Build Coastguard Workerincludes one of the alternative Cronet implementations described below, you will
70*30877f79SAndroid Build Coastguard Workernot be able to instantiate a `CronetEngine` in this case. Your application code
71*30877f79SAndroid Build Coastguard Workershould handle this by falling back to use `DefaultHttpDataSource` instead.
72*30877f79SAndroid Build Coastguard Worker
73*30877f79SAndroid Build Coastguard Worker#### Cronet Embedded
74*30877f79SAndroid Build Coastguard Worker
75*30877f79SAndroid Build Coastguard WorkerCronet Embedded bundles a full Cronet implementation directly into your
76*30877f79SAndroid Build Coastguard Workerapplication. To use it, add an additional dependency on
77*30877f79SAndroid Build Coastguard Worker`org.chromium.net:cronet-embedded`. Cronet Embedded adds approximately 8MB to
78*30877f79SAndroid Build Coastguard Workeryour application, and so we do not recommend it for most use cases. That said,
79*30877f79SAndroid Build Coastguard Workeruse of Cronet Embedded may be appropriate if:
80*30877f79SAndroid Build Coastguard Worker
81*30877f79SAndroid Build Coastguard Worker* A large percentage of your users are in markets where Google Play Services is
82*30877f79SAndroid Build Coastguard Worker  not widely available.
83*30877f79SAndroid Build Coastguard Worker* You want to control the exact version of the Cronet implementation being used.
84*30877f79SAndroid Build Coastguard Worker
85*30877f79SAndroid Build Coastguard Worker#### Cronet Fallback
86*30877f79SAndroid Build Coastguard Worker
87*30877f79SAndroid Build Coastguard WorkerThere's also a fallback implementation of Cronet, which uses Android's default
88*30877f79SAndroid Build Coastguard Workernetwork stack under the hood. It can be used by adding a dependency on
89*30877f79SAndroid Build Coastguard Worker`org.chromium.net:cronet-fallback`. This implementation should *not* be used
90*30877f79SAndroid Build Coastguard Workerwith `CronetDataSource`, since it's more efficient to use
91*30877f79SAndroid Build Coastguard Worker`DefaultHttpDataSource` directly in this case.
92*30877f79SAndroid Build Coastguard Worker
93*30877f79SAndroid Build Coastguard WorkerWhen using Cronet Fallback for other networking in your application, use the
94*30877f79SAndroid Build Coastguard Workermore advanced approach to instantiating a `CronetEngine` described below so that
95*30877f79SAndroid Build Coastguard Workeryou know when your application's `CronetEngine` has been obtained from the
96*30877f79SAndroid Build Coastguard Workerfallback implementation. In this case, avoid `CronetDataSource` and use
97*30877f79SAndroid Build Coastguard Worker`DefaultHttpDataSource` instead.
98*30877f79SAndroid Build Coastguard Worker
99*30877f79SAndroid Build Coastguard Worker### CronetEngine instantiation
100*30877f79SAndroid Build Coastguard Worker
101*30877f79SAndroid Build Coastguard WorkerCronet's [Send a simple request][] page documents the simplest way of building a
102*30877f79SAndroid Build Coastguard Worker`CronetEngine`, which is suitable if your application is only using the
103*30877f79SAndroid Build Coastguard WorkerGoogle Play Services implementation of Cronet.
104*30877f79SAndroid Build Coastguard Worker
105*30877f79SAndroid Build Coastguard WorkerFor cases where your application also includes one of the other Cronet
106*30877f79SAndroid Build Coastguard Workerimplementations, you can use `CronetProvider.getAllProviders` to list the
107*30877f79SAndroid Build Coastguard Workeravailable implementations. Providers can be identified by name:
108*30877f79SAndroid Build Coastguard Worker
109*30877f79SAndroid Build Coastguard Worker* `CronetProviderInstaller.PROVIDER_NAME`: Google Play Services implementation.
110*30877f79SAndroid Build Coastguard Worker* `CronetProvider.PROVIDER_NAME_APP_PACKAGED`: Embedded implementation.
111*30877f79SAndroid Build Coastguard Worker* `CronetProvider.PROVIDER_NAME_FALLBACK`: Fallback implementation.
112*30877f79SAndroid Build Coastguard Worker
113*30877f79SAndroid Build Coastguard WorkerThis makes it possible to iterate through the providers in your own order of
114*30877f79SAndroid Build Coastguard Workerpreference, trying to build a `CronetEngine` from each in turn using
115*30877f79SAndroid Build Coastguard Worker`CronetProvider.createBuilder()` until one has been successfully created. This
116*30877f79SAndroid Build Coastguard Workerapproach also allows you to determine when the `CronetEngine` has been obtained
117*30877f79SAndroid Build Coastguard Workerfrom Cronet Fallback, in which case you can avoid using `CronetDataSource`
118*30877f79SAndroid Build Coastguard Workerwhilst still using Cronet Fallback for other networking performed by your
119*30877f79SAndroid Build Coastguard Workerapplication.
120*30877f79SAndroid Build Coastguard Worker
121*30877f79SAndroid Build Coastguard Worker[Send a simple request]: https://developer.android.com/guide/topics/connectivity/cronet/start
122*30877f79SAndroid Build Coastguard Worker
123*30877f79SAndroid Build Coastguard Worker## Links
124*30877f79SAndroid Build Coastguard Worker
125*30877f79SAndroid Build Coastguard Worker* [Javadoc][]
126*30877f79SAndroid Build Coastguard Worker
127*30877f79SAndroid Build Coastguard Worker[Javadoc]: https://exoplayer.dev/doc/reference/index.html
128