Gray Skold | (former Android Video Engineer) ; Lin Wang | Android Efficiency Engineer; Sheng Liu | Android Efficiency Engineer
Pinterest Android App affords a uncommon expertise with a mixture of photographs and movies on a two-column grid. So as to preserve a performant video expertise on Android units, we targeted on:
- Warming up
- Pooling gamers
So as to scale back the startup latency, we set up a video community connection by sending a dummy HTTP HEAD request in the course of the early utility startup time. The identical connection can be utilized to play future movies. That is carried out even earlier than any video urls are returned from our server.
The identical technique additionally applies to UI rendering. We discovered Exoplayer tends to do a lot of work after parsing the data from the video url. It:
Since most of our movies’ side ratios are pre-determined, we are able to forestall the above work by:
The latter prevents the participant from attempting to recalculate the side ratio.
ExoPlayer offers us the setBufferDurationsMs() to customise varied buffering durations used to delay playback till now we have enough information. Since a majority of Pinterest’s media content material is in short-form, we are able to use a lot shorter buffering durations which is able to lead to much less time ready for information to load.
By doing this, we noticed a major discount in video startup latency. Though the rebuffer price will increase a bit, the general video UX remains to be improved.
Utilizing the next two parameters, we might successfully guarantee movies loaded in-feed are restricted by their viewport dimension (i.e. to keep away from loading a big 1080p video in a small 360 pixel viewport).
The Pinterest app performs a number of muted movies within the grid on the identical time. Utilizing the next parameters, we might disable the audio rendering to avoid wasting community bandwidth to obtain the audio and reminiscence consumption to course of them.
Exoplayer offers a cache interface to maintain downloaded media information on disk. Nevertheless, within the instances after we run into deadly errors attributable to backend bugs, the unhealthy contents may also stick within the cache. This will trigger the applying to proceed encountering the identical playback errors although the backend is fastened.
- ERROR_CODE_IO_UNSPECIFIED 2000
- ERROR_CODE_IO_INVALID_HTTP_CONTENT_TYPE 2003
- ERROR_CODE_IO_BAD_HTTP_STATUS 2004
- ERROR_CODE_IO_FILE_NOT_FOUND 2005
- ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE 2008
Lastly, we constructed our personal cache to pool gamers as they’re wanted. Traditionally, we instantiated new participant cases on the fly, which induced important overhead on each the reminiscence and bandwidth. Listed here are some excessive degree learnings:
Separate By Encoding
Participant cases are successfully holding onto a reference to an underlying decoder based mostly on the final rendered media’s encoding sort. It takes a measurable quantity of labor for gamers to change context between totally different decoders. Subsequently we pooled our gamers based mostly on the preliminary decoding format. By making certain the recycled gamers with decoders matching the media’s encoding, we eliminated any latency overhead induced when switching encoding codecs.
The pool dimension itself ran by way of a number of iterations to search out the perfect area wanted to accommodate a number of video performs whereas avoiding OutOfMemory (OOM) and ANR. The next two APIs are utilized to not over-burdening the system:
This was our key callback used to tell us that we must always clear our participant cache pool as we begin to get dangerously near sending OOMs.
Setting this flag will let the Exoplayer to retain a direct reference to the video decoder and maintain it in reminiscence even within the idle state. Nevertheless, it takes a major toll on reminiscence and system stability. We needed to construct logic to make use of this methodology conservatively, based mostly on each the present utility lifecycle and present out there reminiscence on the system.
Bettering the efficiency of video playback is a unending investigation into the inner-workings of the ExoPlayer library in addition to our personal product’s distinct use-cases, however what’s probably the most difficult is constructing an structure that works for the uniquely Pinterest expertise of the house feed. Our long-term objective is to share this work to different builders seeking to construct a seamless video UX with minimal setup required. Within the meantime, we hope everybody builds a extra performant video expertise.