1*30877f79SAndroid Build Coastguard Worker---
2*30877f79SAndroid Build Coastguard Workertitle: Network stacks
3*30877f79SAndroid Build Coastguard Worker---
4*30877f79SAndroid Build Coastguard Worker
5*30877f79SAndroid Build Coastguard WorkerExoPlayer is commonly used for streaming media over the internet. It supports
6*30877f79SAndroid Build Coastguard Workermultiple network stacks for making its underlying network requests. Your choice
7*30877f79SAndroid Build Coastguard Workerof network stack can have a significant impact on streaming performance.
8*30877f79SAndroid Build Coastguard Worker
9*30877f79SAndroid Build Coastguard WorkerThis page outlines how to configure ExoPlayer to use your network stack of
10*30877f79SAndroid Build Coastguard Workerchoice, lists the available options, and provides some guidance on how to choose
11*30877f79SAndroid Build Coastguard Workera network stack for your application.
12*30877f79SAndroid Build Coastguard Worker
13*30877f79SAndroid Build Coastguard Worker## Configuring ExoPlayer to use a specific network stack ##
14*30877f79SAndroid Build Coastguard Worker
15*30877f79SAndroid Build Coastguard WorkerExoPlayer loads data through `DataSource` components, which it obtains from
16*30877f79SAndroid Build Coastguard Worker`DataSource.Factory` instances that are injected from application code.
17*30877f79SAndroid Build Coastguard Worker
18*30877f79SAndroid Build Coastguard WorkerIf your application only needs to play http(s) content, selecting a network
19*30877f79SAndroid Build Coastguard Workerstack is as simple as updating any `DataSource.Factory` instances that your
20*30877f79SAndroid Build Coastguard Workerapplication injects to be instances of the `HttpDataSource.Factory`
21*30877f79SAndroid Build Coastguard Workerthat corresponds to the network stack you wish to use. If your application also
22*30877f79SAndroid Build Coastguard Workerneeds to play non-http(s) content such as local files, use
23*30877f79SAndroid Build Coastguard Worker
24*30877f79SAndroid Build Coastguard Worker~~~
25*30877f79SAndroid Build Coastguard Workernew DefaultDataSourceFactory(
26*30877f79SAndroid Build Coastguard Worker    ...
27*30877f79SAndroid Build Coastguard Worker    /* baseDataSourceFactory= */ new PreferredHttpDataSource.Factory(...));
28*30877f79SAndroid Build Coastguard Worker~~~
29*30877f79SAndroid Build Coastguard Worker{: .language-java}
30*30877f79SAndroid Build Coastguard Worker
31*30877f79SAndroid Build Coastguard Workerwhere `PreferredHttpDataSource.Factory` is the factory corresponding to your
32*30877f79SAndroid Build Coastguard Workerpreferred network stack. The `DefaultDataSourceFactory` layer adds in support
33*30877f79SAndroid Build Coastguard Workerfor non-http(s) sources such as local files.
34*30877f79SAndroid Build Coastguard Worker
35*30877f79SAndroid Build Coastguard WorkerThe example below shows how to build an `ExoPlayer` that will use the Cronet
36*30877f79SAndroid Build Coastguard Workernetwork stack and also support playback of non-http(s) content.
37*30877f79SAndroid Build Coastguard Worker
38*30877f79SAndroid Build Coastguard Worker~~~
39*30877f79SAndroid Build Coastguard Worker// Given a CronetEngine and Executor, build a CronetDataSource.Factory.
40*30877f79SAndroid Build Coastguard WorkerCronetDataSource.Factory cronetDataSourceFactory =
41*30877f79SAndroid Build Coastguard Worker    new CronetDataSource.Factory(cronetEngine, executor);
42*30877f79SAndroid Build Coastguard Worker
43*30877f79SAndroid Build Coastguard Worker// Wrap the CronetDataSource.Factory in a DefaultDataSource.Factory, which adds
44*30877f79SAndroid Build Coastguard Worker// in support for requesting data from other sources (e.g., files, resources,
45*30877f79SAndroid Build Coastguard Worker// etc).
46*30877f79SAndroid Build Coastguard WorkerDefaultDataSource.Factory dataSourceFactory =
47*30877f79SAndroid Build Coastguard Worker    new DefaultDataSource.Factory(
48*30877f79SAndroid Build Coastguard Worker        context,
49*30877f79SAndroid Build Coastguard Worker        /* baseDataSourceFactory= */ cronetDataSourceFactory);
50*30877f79SAndroid Build Coastguard Worker
51*30877f79SAndroid Build Coastguard Worker// Inject the DefaultDataSourceFactory when creating the player.
52*30877f79SAndroid Build Coastguard WorkerExoPlayer player =
53*30877f79SAndroid Build Coastguard Worker    new ExoPlayer.Builder(context)
54*30877f79SAndroid Build Coastguard Worker        .setMediaSourceFactory(new DefaultMediaSourceFactory(dataSourceFactory))
55*30877f79SAndroid Build Coastguard Worker        .build();
56*30877f79SAndroid Build Coastguard Worker~~~
57*30877f79SAndroid Build Coastguard Worker{: .language-java}
58*30877f79SAndroid Build Coastguard Worker
59*30877f79SAndroid Build Coastguard Worker## Supported network stacks ##
60*30877f79SAndroid Build Coastguard Worker
61*30877f79SAndroid Build Coastguard WorkerExoPlayer provides direct support for Cronet, OkHttp and Android's built-in
62*30877f79SAndroid Build Coastguard Workernetwork stack. It can also be extended to support any other network stack that
63*30877f79SAndroid Build Coastguard Workerworks on Android.
64*30877f79SAndroid Build Coastguard Worker
65*30877f79SAndroid Build Coastguard Worker### Cronet ###
66*30877f79SAndroid Build Coastguard Worker
67*30877f79SAndroid Build Coastguard Worker[Cronet](https://developer.android.com/guide/topics/connectivity/cronet) is the
68*30877f79SAndroid Build Coastguard WorkerChromium network stack made available to Android apps as a library. It takes
69*30877f79SAndroid Build Coastguard Workeradvantage of multiple technologies that reduce the latency and increase the
70*30877f79SAndroid Build Coastguard Workerthroughput of the network requests that your app needs to work, including those
71*30877f79SAndroid Build Coastguard Workermade by ExoPlayer. It natively supports the HTTP, HTTP/2, and HTTP/3 over QUIC
72*30877f79SAndroid Build Coastguard Workerprotocols. Cronet is used by some of the world's biggest streaming applications,
73*30877f79SAndroid Build Coastguard Workerincluding YouTube.
74*30877f79SAndroid Build Coastguard Worker
75*30877f79SAndroid Build Coastguard WorkerExoPlayer supports Cronet via its
76*30877f79SAndroid Build Coastguard Worker[Cronet extension](https://github.com/google/ExoPlayer/tree/dev-v2/extensions/cronet).
77*30877f79SAndroid Build Coastguard WorkerPlease see the extension's `README.md` for detailed instructions on how to use
78*30877f79SAndroid Build Coastguard Workerit. Note that the Cronet extension is able to use three underlying Cronet
79*30877f79SAndroid Build Coastguard Workerimplementations:
80*30877f79SAndroid Build Coastguard Worker
81*30877f79SAndroid Build Coastguard Worker1. **Google Play Services:** We recommend using this implementation in most
82*30877f79SAndroid Build Coastguard Worker  cases, and falling back to Android's built-in network stack
83*30877f79SAndroid Build Coastguard Worker  (i.e., `DefaultHttpDataSource`) if Google Play Services is not available.
84*30877f79SAndroid Build Coastguard Worker1. **Cronet Embedded:** May be a good choice if a large percentage of your users
85*30877f79SAndroid Build Coastguard Worker  are in markets where Google Play Services is not widely available, or if you
86*30877f79SAndroid Build Coastguard Worker  want to control the exact version of the Cronet implementation being used. The
87*30877f79SAndroid Build Coastguard Worker  major disadvantage of Cronet Embedded is that it adds approximately 8MB to
88*30877f79SAndroid Build Coastguard Worker  your application.
89*30877f79SAndroid Build Coastguard Worker1. **Cronet Fallback:** The fallback implementation of Cronet implements
90*30877f79SAndroid Build Coastguard Worker  Cronet's API as a wrapper around Android's built-in network stack. It should
91*30877f79SAndroid Build Coastguard Worker  not be used with ExoPlayer, since using Android's built-in network stack
92*30877f79SAndroid Build Coastguard Worker  directly (i.e., by using `DefaultHttpDataSource`) is more efficient.
93*30877f79SAndroid Build Coastguard Worker
94*30877f79SAndroid Build Coastguard Worker### OkHttp ###
95*30877f79SAndroid Build Coastguard Worker
96*30877f79SAndroid Build Coastguard Worker[OkHttp](https://square.github.io/okhttp/) is another modern network stack that
97*30877f79SAndroid Build Coastguard Workeris widely used by many popular Android applications. It supports HTTP and
98*30877f79SAndroid Build Coastguard WorkerHTTP/2, but does not yet support HTTP/3 over QUIC.
99*30877f79SAndroid Build Coastguard Worker
100*30877f79SAndroid Build Coastguard WorkerExoPlayer supports OkHttp via its
101*30877f79SAndroid Build Coastguard Worker[OkHttp extension](https://github.com/google/ExoPlayer/tree/dev-v2/extensions/okhttp).
102*30877f79SAndroid Build Coastguard WorkerPlease see the extension's `README.md` for detailed instructions on how to use
103*30877f79SAndroid Build Coastguard Workerit. When using the OkHttp extension, the network stack is embedded within the
104*30877f79SAndroid Build Coastguard Workerapplication. This is similar to Cronet Embedded, however OkHttp is significantly
105*30877f79SAndroid Build Coastguard Workersmaller, adding under 1MB to your application.
106*30877f79SAndroid Build Coastguard Worker
107*30877f79SAndroid Build Coastguard Worker### Android's built-in network stack ###
108*30877f79SAndroid Build Coastguard Worker
109*30877f79SAndroid Build Coastguard WorkerExoPlayer supports use of Android's built-in network stack with
110*30877f79SAndroid Build Coastguard Worker`DefaultHttpDataSource` and `DefaultHttpDataSource.Factory`, which are part of
111*30877f79SAndroid Build Coastguard Workerthe core ExoPlayer library.
112*30877f79SAndroid Build Coastguard Worker
113*30877f79SAndroid Build Coastguard WorkerThe exact network stack implementation depends on the software running on the
114*30877f79SAndroid Build Coastguard Workerunderlying device. On most devices (as of 2021) only HTTP is supported (i.e.,
115*30877f79SAndroid Build Coastguard WorkerHTTP/2 and HTTP/3 over QUIC are not supported).
116*30877f79SAndroid Build Coastguard Worker
117*30877f79SAndroid Build Coastguard Worker### Other network stacks ###
118*30877f79SAndroid Build Coastguard Worker
119*30877f79SAndroid Build Coastguard WorkerIt's possible for applications to integrate other network stacks with ExoPlayer.
120*30877f79SAndroid Build Coastguard WorkerTo do this, implement an `HttpDataSource` that wraps the network stack,
121*30877f79SAndroid Build Coastguard Workertogether with a corresponding `HttpDataSource.Factory`. ExoPlayer's Cronet and
122*30877f79SAndroid Build Coastguard WorkerOkHttp extensions are good examples of how to do this.
123*30877f79SAndroid Build Coastguard Worker
124*30877f79SAndroid Build Coastguard WorkerWhen integrating with a pure Java network stack, it's a good idea to implement a
125*30877f79SAndroid Build Coastguard Worker`DataSourceContractTest` to check that your `HttpDataSource` implementation
126*30877f79SAndroid Build Coastguard Workerbehaves correctly. `OkHttpDataSourceContractTest` in the OkHttp extension is a
127*30877f79SAndroid Build Coastguard Workergood example of how to do this.
128*30877f79SAndroid Build Coastguard Worker
129*30877f79SAndroid Build Coastguard Worker## Choosing a network stack ##
130*30877f79SAndroid Build Coastguard Worker
131*30877f79SAndroid Build Coastguard WorkerThe table below outlines the pros and cons of the network stacks supported by
132*30877f79SAndroid Build Coastguard WorkerExoPlayer.
133*30877f79SAndroid Build Coastguard Worker
134*30877f79SAndroid Build Coastguard Worker| Network stack | Protocols | APK size impact | Notes |
135*30877f79SAndroid Build Coastguard Worker|:---|:--:|:--:|:---|
136*30877f79SAndroid Build Coastguard Worker| Cronet (Google Play Services) | HTTP<br>HTTP/2<br>HTTP/3&nbsp;over&nbsp;QUIC | Small<br>(<100KB) | Requires Google Play Services. Cronet version updated automatically |
137*30877f79SAndroid Build Coastguard Worker| Cronet (Embedded) | HTTP<br>HTTP/2<br>HTTP/3&nbsp;over&nbsp;QUIC | Large<br>(~8MB) | Cronet version controlled by app developer |
138*30877f79SAndroid Build Coastguard Worker| Cronet (Fallback) | HTTP<br>(varies&nbsp;by&nbsp;device) | Small<br>(<100KB) | Not recommended for ExoPlayer |
139*30877f79SAndroid Build Coastguard Worker| OkHttp | HTTP<br>HTTP/2 | Small<br>(<1MB) | Requires Kotlin runtime |
140*30877f79SAndroid Build Coastguard Worker| Built-in network stack | HTTP<br>(varies&nbsp;by&nbsp;device) | None | Implementation varies by device |
141*30877f79SAndroid Build Coastguard Worker
142*30877f79SAndroid Build Coastguard WorkerThe HTTP/2 and HTTP/3 over QUIC protocols can significantly improve media
143*30877f79SAndroid Build Coastguard Workerstreaming performance. In particular when streaming adaptive media distributed
144*30877f79SAndroid Build Coastguard Workervia a content distribution network (CDN), there are cases for which use of these
145*30877f79SAndroid Build Coastguard Workerprotocols can allow CDNs to operate much more efficiently. For this reason,
146*30877f79SAndroid Build Coastguard WorkerCronet's support for both HTTP/2 and HTTP/3 over QUIC (and OkHttp's support for
147*30877f79SAndroid Build Coastguard WorkerHTTP/2), is a major benefit compared to using Android's built-in network stack,
148*30877f79SAndroid Build Coastguard Workerprovided the servers on which the content is hosted also support these
149*30877f79SAndroid Build Coastguard Workerprotocols.
150*30877f79SAndroid Build Coastguard Worker
151*30877f79SAndroid Build Coastguard WorkerWhen considering media streaming in isolation, we recommend use of Cronet
152*30877f79SAndroid Build Coastguard Workerprovided by Google Play Services, falling back to `DefaultHttpDataSource` if
153*30877f79SAndroid Build Coastguard WorkerGoogle Play Services is unavailable. This recommendation strikes a good balance
154*30877f79SAndroid Build Coastguard Workerbetween enabling use of HTTP/2 and HTTP/3 over QUIC on most devices, and
155*30877f79SAndroid Build Coastguard Workeravoiding a significant increase in APK size. There are exceptions to this
156*30877f79SAndroid Build Coastguard Workerrecommendation. For cases where Google Play Services is likely to be unavailable
157*30877f79SAndroid Build Coastguard Workeron a significant fraction of devices that will be running your application,
158*30877f79SAndroid Build Coastguard Workerusing Cronet Embedded or OkHttp may be more appropriate. Use of the built-in
159*30877f79SAndroid Build Coastguard Workernetwork stack may be acceptable if APK size is a critical concern, or if media
160*30877f79SAndroid Build Coastguard Workerstreaming is only a minor part of your application's functionality.
161*30877f79SAndroid Build Coastguard Worker
162*30877f79SAndroid Build Coastguard WorkerBeyond just media, it's normally a good idea to choose a single network stack
163*30877f79SAndroid Build Coastguard Workerfor all of the networking performed by your application. This allows resources
164*30877f79SAndroid Build Coastguard Worker(e.g., sockets) to be efficiently pooled and shared between ExoPlayer and other
165*30877f79SAndroid Build Coastguard Workerapplication components.
166*30877f79SAndroid Build Coastguard Worker
167*30877f79SAndroid Build Coastguard WorkerTo assist with resource sharing, it's recommended to use a single `CronetEngine`
168*30877f79SAndroid Build Coastguard Workeror `OkHttpClient` instance throughout your application, when using Cronet or
169*30877f79SAndroid Build Coastguard WorkerOkHttp respectively.
170*30877f79SAndroid Build Coastguard Worker{:.info}
171*30877f79SAndroid Build Coastguard Worker
172*30877f79SAndroid Build Coastguard WorkerSince your application will most likely need to perform networking not related
173*30877f79SAndroid Build Coastguard Workerto media playback, your choice of network stack should ultimately factor in our
174*30877f79SAndroid Build Coastguard Workerrecommendations above for media streaming in isolation, the requirements of any
175*30877f79SAndroid Build Coastguard Workerother components that perform networking, and their relative importance to your
176*30877f79SAndroid Build Coastguard Workerapplication.
177