<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Allen Sun on Medium]]></title>
        <description><![CDATA[Stories by Allen Sun on Medium]]></description>
        <link>https://medium.com/@asun_55699?source=rss-2e6de42d5a3d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xXHUnn7VDptJnVdVjhkBSg.jpeg</url>
            <title>Stories by Allen Sun on Medium</title>
            <link>https://medium.com/@asun_55699?source=rss-2e6de42d5a3d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 10:57:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@asun_55699/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How ExoPlayer reads HLS files]]></title>
            <link>https://code.tubitv.com/how-exoplayer-reads-hls-files-fcd82b1c8c6b?source=rss-2e6de42d5a3d------2</link>
            <guid isPermaLink="false">https://medium.com/p/fcd82b1c8c6b</guid>
            <category><![CDATA[exoplayer]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[streaming-video]]></category>
            <dc:creator><![CDATA[Allen Sun]]></dc:creator>
            <pubDate>Wed, 18 Apr 2018 17:54:17 GMT</pubDate>
            <atom:updated>2018-04-24T06:10:50.615Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, I will talk about how ExoPlayer interprets HLS files, because <strong>this is one of the first steps to really understand how ExoPlayer supports HLS adaptive streaming internally. </strong>Afterall, the ExoPlayer library handles adaptive streaming logic, and the first thing it requires is the HLS formatted file to fetch and manage video resources automatically.</p><h4>HLS-adaptive streaming overview</h4><p>HTTP Live Streaming (also known as HLS), is an HTTP-based media streaming protocol established by Apple Inc. HLS is supported natively by operating systems such as iOS and OSX, by browsers such as Safari, Firefox and Microsoft Edge, and by applications such as iTunes, Kodi, VLC and much more. HLS is designed for reliability, and it’s capable of dynamically adapting to network conditions by optimizing playback for a variety of speeds ranging from high speed wired connections to low speed 2G wireless connections.</p><p>In simple terms, imagine there are multiple streams of video tracks with different bitrates and resolutions, arranged in parallel, and they are broken into small chunks of equal durations (usually 5 to 10 seconds). Tracks with higher bitrate and resolution have better image quality but are larger in size, and vice versa. Therefore, this creates an optimized solution for streaming tradeoff between network speed and video quality. When network speed is low, Exoplayer switches to lower quality video chunks for quick buffering, and when network speed is high, it switches to higher quality video chunks. See graph below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ebjJELrNqfOP9sI2." /></figure><h4>HLS files representation in ExoPlayer</h4><p>Since HLS is based on HTTP protocol, everything is being generated as files and hosted on servers waiting to be retrieved. In fact, one of the best advantages of HLS is that it doesn’t require a streaming server, as any HTTP server can host HLS files. Here is an image example of HLS files representation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9Z-gUKY4i88FGgu4Wj8cEA.png" /></figure><ul><li><strong>HlsMasterPlayList</strong> acts as general directory listing all parallel media tracks of different bitrates and resolutions. Consider it to be a general directory of parallel video tracks.</li><li><strong>HlsMediaPlayList </strong>is individual media track’s subdirectory listing all sequential chunks of encoded video stream file. Consider it to be a sub-directory of single video track.</li></ul><h4>HlsPlaylistParser</h4><p>ExoPlayer will need to parse information contained in both types of files in order to stream HLS videos. HLS protocol states that certain tags can only be presented in certain files. As the result, HlsMasterPlaylist and HlsMediaPlaylist represent different sets of information and therefore can be identified by discovering unique tags with regex. ExoPlayer has an internal class called <strong>HlsPlaylistParser.java</strong>, and it uses two-step logic to parse HlsMasterPlayerlist and HlsLMediaPlaylist into POJO. See code below:</p><ol><li>Identify HlsMasterPlaylist | HlsLMediaPlaylist type by reading tags.</li><li>Use separate methods to parse HlsMasterPlaylist | HlsLMediaPlaylist into POJO.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/822756fda312154d68ba6ae650aacb14/href">https://medium.com/media/822756fda312154d68ba6ae650aacb14/href</a></iframe><h4>1. Determine HlsMasterPlaylist | HlsLMediaPlaylist type.</h4><p>HlsPlaylistParser will parse file from inputStream, and will try to determine the file type by using regex to find the tags in the file:</p><ul><li>If there is any line with <strong>#EXT-X-STREAM-INF</strong> in the beginning of the line, then that indicate this is a <strong><em>MasterPlaylist</em></strong>. (line 15–17)</li><li>If there is any line with <strong>#EXT-X-TARGETDURATION </strong>|<strong> #EXT-X-MEDIA-SEQUENCE </strong>|<strong> #EXTINF </strong>|<strong> #EXT-X-KEY </strong>|<strong> #EXT-X-BYTERANGE </strong>|<strong> #EXT-X-DISCONTINUITY </strong>|<strong> #EXT-X-DISCONTINUITY-SEQUENCE </strong>|<strong> #EXT-X-ENDLIST </strong>in the beginning of the line, it indicates that this is a <strong><em>MediaPlayList</em></strong>. (line 19- 27)</li></ul><h4>2. Two methods to parse HlsMasterPlaylist | HlsLMediaPlaylist into POJO.</h4><p>After HlsPlaylistParser determined the type of the file, MasterPlayerlist or MediaPlaylist, it will then use private method<a href="https://gist.github.com/allengotstuff/6aa305962bbaddfc64b41784b7467cc1"> <strong>parseMasterPlaylist(LineIterator iterator, String baseUri)</strong></a> and<a href="https://gist.github.com/allengotstuff/335f6925065153811a2aee080ac460cb"> <strong>parseMediaPlaylist(LineIterator iterator, String baseUri)</strong></a> to map the file into objects. In those two methods, there are more regex to parse tagged information into attributes. I don’t have the time required here to go into the full details, but you are welcome to read the method implementation if you are interested.</p><h3>Summary</h3><p>Without tracking mechanic of MasterPlaylist and MediaPlaylist, Exoplayer can’t achieve HLS adaptive streaming on the client side, and the biggest advantages of HLS is that it doesn’t require streaming server capabilities, and it greatly reduces the development effort and maintenance pressure of servers while still delivering exceptional streaming user experience. By understanding how Exoplayer reads HLS files as the first step, we can have more deeper exploration in ExoPlayer library in the future.</p><p>I hope this article helps shed some light on this subject. We will explore the ExoPlayer in more depth in future posts, so make sure to follow and as always, questions and comments are welcomed.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fcd82b1c8c6b" width="1" height="1" alt=""><hr><p><a href="https://code.tubitv.com/how-exoplayer-reads-hls-files-fcd82b1c8c6b">How ExoPlayer reads HLS files</a> was originally published in <a href="https://code.tubitv.com/">Tubi Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exploring useful programming patterns in Google’s ExoPlayer]]></title>
            <link>https://code.tubitv.com/exploring-useful-programming-patterns-in-googles-exoplayer-e7be8610a4f?source=rss-2e6de42d5a3d------2</link>
            <guid isPermaLink="false">https://medium.com/p/e7be8610a4f</guid>
            <dc:creator><![CDATA[Allen Sun]]></dc:creator>
            <pubDate>Tue, 02 Jan 2018 00:26:59 GMT</pubDate>
            <atom:updated>2018-01-04T18:07:11.262Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zYnXGOggU8hMPzvpyhC-JQ.png" /><figcaption>ExoPlayer makes building high quality media apps on Android easy</figcaption></figure><p>Learning is always exciting and one of the best ways to learn is reading good source code. On top of that, reading source code of libraries that your project heavily depends on comes with the added benefit of gaining a deep understanding of your own code and should be an important routine we as engineers do often.</p><p>In the past few months, I have been working on completely revamping the video playback experience for Tubi’s Android app. Our previous solution, a propitiatory 3rd party player, didn’t allow us to customize and enhance the user experience as much or as fast as we wanted, not to mention we were at the vendor’s mercy for how quick critical bugs could be fixed. We decided to built our new player on top of Google’s ExoPlayer and enhance it with capabilities for seamless transition between movie &amp; ad playback, support for VPAID, make it easier to test and generally provide a higher level abstraction to make development more productive. In the coming months, we will be open sourcing our work with a permissive license and I will be blogging more extensively on the lessons learned while working on the project.</p><h4>What is ExoPlayer and why use it?</h4><p>ExoPlayer is an application level multi-media player built on top of Android’s low level media player APIs, developed and maintained by Google. It provides a lot of flexible and customizable features that developers can leverage to build high quality Android media applications. A best in class media application needs to address a number of concerns, for example:</p><ul><li>Adaptive streaming to handle bandwidth fluctuations typically associated with mobile networks</li><li>Support of different packaging formats</li><li>Different audio tracks, subtitles</li><li>Buffering strategy under different connections and constraints</li><li>Customizing the UX and UI to fit the application</li><li>Transitioning in and out of the playback experience, as well as between content and ads</li></ul><p>Fortunately, ExoPlayer is here to the rescue. The brilliant engineers at Google have built a very modular and easily customizable library. In this post, we will look how ExoPlayer achieves some of that customizability using the Proxy design pattern.</p><h4>ExoPlayer Implementation and the Proxy Design Pattern</h4><blockquote>“In short, a proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes. Use of the proxy can simply be forwarding to the real object, or can provide additional logic” (Wikipedia)</blockquote><p>The proxy pattern is extremely simple and most of the time, we implement it inadvertently because it is so useful. At its core, the pattern enables developers to separate otherwise huge lines of business into different components, so that project’s external facing complexity is reduced to a manageable minimum. Let’s take a look at a trivial example:</p><pre>// A general object representation of a primate. All primates can think</pre><pre>interface Primate {<br>  void think();<br>}</pre><pre>Class Human implements Primate {<br>  void think() {<br>  // Humans have share similar thinking processes<br>  /*...code implementation...*/<br>  }<br>}</pre><pre>Class Peter {<br>  Human mHuman;<br>  <br>  void think() {<br>    mHuman.think();<br>  }<br>}</pre><p>The pattern can be used to simply allow different implementations or the proxy can take on additional responsibilities.</p><p>ExoPlayer library skeleton can be summarized in three important classes that glue together most of the playback components: SimpleExoPlayer, ExoPlayerImpl and ExoPlayerImplInternal. The relationship between those three classes is a great example of the pattern.</p><p>At the very basic level, the SimpleExoPlayer class implements the ExoPlayer interface and instantiates the ExoPlayerImpl class in its constructor. The ExoPlayerImpl class also implements the ExoPlayer interface and in turn instantiates the ExoPlayerImplInternal class, which in essence implements the core playback logic.</p><h4>SimpleExoPlayer</h4><p>The most high level class, implements @ExoPlayer interface. Within its constructor, it has initialized an ExoPlayerImpl instance. Overall, SimpleExoPlayer is a wrapper class that hides the implementation details and exposes a simple API for developers to use directly.</p><h4>ExoPlayerImpl</h4><p>This class has a dual purpose. First it acts as an intermediate wrapper that hides the real playback logic of different ExoPlayer’s components and their interactions. In addition, it behaves like a message receiver, receiving ExoPlayer’s playback state changes and immediately triggering different event listener’s callbacks that developers can choose to react to.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/891c0c72a4c377ce5a71a62e8632f939/href">https://medium.com/media/891c0c72a4c377ce5a71a62e8632f939/href</a></iframe><p>Some key-points that are very important to keep in mind for when we look into the next implementation layer in ExoPlayerImplInternal:</p><ul><li>The eventHandler member variable in the constructor is a handler attached to UIThread</li><li>The listener object keeps track of all registered ExoPlayer Listeners</li><li>The handleEvent() method is only invoked by the eventHandler. This is a fundamental concurrency pattern in Android, the handler binds to a thread, UIThread in this case, so others can later send code blocks to execute on that thread</li></ul><h4>ExoPlayerImplInternal</h4><p>The bulk of the actual logic lives in this class. It acts as a glue to all the different components needed for media playback. All of the code above has so far been executed on MainThread, and it makes senses to do that because user behaviors are mostly triggered on MainThread for reasons that it wants users to know immediately what is happening when ExoPlayer changes states. However, the when dealing with operations across a network boundary such as downloading manifest playlists, video segments files, rendering byte array on screens, etc, we do not want to be blocking the main thread. Instead, blocking or CPU intensive code shoud be executed on a worker thread. To facilitate that, ExoPlayerImplInternal:</p><ol><li>Creates a HandlerThread instance in its constructor. This is the worker thread that all internal playback code logic runs on.</li><li>It implements Handler.Callback, needed to enable code to be executed on the worker thread.</li><li>It receives the previously mentioned eventHandler in its constructor. The eventHandler acts as a communication channel between the UIThread and the worker thread.</li></ol><h4>Summary</h4><p>The ExoPlayer library has separated @ExoPlayer interface implementations into three different layers:</p><p><strong>SimpleExoPlayer</strong>: api level, easy to use getters and setters.</p><p><strong>ExoPlayerImpl</strong>: informational and transitional level, monitor playback state changes</p><p><strong>ExoPlayerImplInternal</strong>: playback level, implementing core playback logic.</p><p>By doing so, a lot of low level implementation details are hidden in lower layers, reducing the overall project complexity.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e7be8610a4f" width="1" height="1" alt=""><hr><p><a href="https://code.tubitv.com/exploring-useful-programming-patterns-in-googles-exoplayer-e7be8610a4f">Exploring useful programming patterns in Google’s ExoPlayer</a> was originally published in <a href="https://code.tubitv.com/">Tubi Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>