The document discusses various approaches for playing videos on Android, including built-in players like VideoView and MediaPlayer, and third party players like ExoPlayer. It also covers streaming video to different devices like Android TV and Chromecast using technologies like the Leanback library, Google Cast SDK, and implementing custom receiver applications. Specific code examples and considerations for developing video applications on Android TV and with Chromecast are provided.
Mobile Application Development-Components and Layouts
Video Streaming: from the native Android player to uncoventional devices
1. Video Streaming
from the native Android player
to uncoventional devices
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
2. Open Reply & Android Lab
• Reply is today a leading IT Services Company
• Operates in Italy, Germany, UK, Benelux, USA and Brasil.
• Open Reply is the company of Reply Group focused on open source software,
multichannel web solutions and mobile applications.
• Based in Rome, Open Reply’s Android Lab is a young team of over 20 engineers 100%
focused on Android development.
• We are specialised in broadcasting, banking and Android OS Customisation.
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
3. Presentation Millestones
• Playing a content on your handheld device.
• Entertaiment tailored for the user with
• Easiest way to enjoy online video and music on user TV on
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
4. Android multimedia framework
• Android support for playing several media types from media file stored inside the
application (raw resources, standalone files) or for OTT streaming.
• Documentation of the media framework is enough just for simple test case.
• Underneath MediaPlayer Framework documentation is kind of nightmare (lot of
events and error are not documented at all).
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
5. Supported type
HttpLiveStreaming Protocol 3 is supported by Android 4.0. Best implementation is in
Kitkat.
Google seems putting all his efforts to support new media type ( SmoothStreaming,
Dynamic adaptive streaming)
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
6. Playing a video
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
VideoView
Three different approaches
Mediaplayer
&
SurfaceView
ExoPlayer
7. MediaPlayer code example
android.widget.VideoView
//1. Find the view from the layout
VideoView myVideoView = (VideoView)findViewById(R.id.myvideoview);
//2. Setup video url
myVideoView.setVideoURI(Uri.parse(SrcPath));
//3. Setup Video controller
myVideoView.setMediaController(new MediaController(this));
//4. Start playing
myVideoView.requestFocus();
myVideoView.start();
Programmer doesn’t directly
handle the MediaPlayer
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
8. MediaPlayer code example
android.media.Mediaplayer
//0. Get SurfaceView and its holder
mPreview = (SurfaceView)findViewById(R.id.surfaceView);
holder = mPreview.getHolder();
//1. Create MediaPlayer object:
mp = new MediaPlayer();
//2. Add SurfaceHolder callback - Aware when SurfaceView is created
holder.addCallback(new SurfaceHolder.Callback(){ ....
@Override
public void surfaceCreated(SurfaceHolder holder) {
android.view.SurfaceView
//3. Attach the surface to the player
mp.setDisplay(holder);
try {
mp.setDataSource(filepath);
//4. Prepare the Mediaplayer in sync or async mode ( prepareAsync() )
mp.prepare();
} catch (Exception e) {// Catch the exception}
//5. Start the player
mp.start();
}...
});
&
Application creates SurfaceView and MediaPlayer.
More control on MediaPlayer state
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
9. MediaPlayer code example
// 1. Instantiate the player.
player = ExoPlayer.Factory.newInstance(RENDERER_COUNT);
// 2. Construct renderers.
ExoPlayer
MediaCodecVideoTrackRenderer videoRenderer = …
MediaCodecAudioTrackRenderer audioRenderer = ...
// 3. Inject the renderers through prepare.
player.prepare(videoRenderer, audioRenderer);
// 4. Pass the surface to the video renderer.
player.sendMessage(videoRenderer, MediaCodecVideoTrackRenderer.MSG_SET_SURFACE, surface);
// 5. Start playback.
player.setPlayWhenReady(true);
...
player.release(); // Release when everything is done!
Pre-built player that can be extend.
Implement features not currently
supported in the normal mediaplayer.
Droidcon London – 2014 – Matteo Bonifazi e Alessandro Martellucci
10. What kind of Mediaplayer exist?
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
• Built-in players
ü AwesomePlayer (default player selected)
ü NuPlayer (Apple HLS)
• Extra player factories can be registered
• DIY mediaplayer
ü Demuxing: android.media.mediaExtractor
ü Decoding: android.media.MediaCodec
ü Video rendering: android.media.MediaCodec
ü Audio rendering: android.media.AudioTrack
ü Implement the interface frameworks/av/include/media/MediaPlayerInterface.h
11. Android TV
Entertainment tailored for you
Source:h)p://www.televisedrevolu4on.com/
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
12. Nexus Player
Source:h)p://www.televisedrevolu4on.com/
Asus device – 235 g
1.8GHz Quad Core, Intel® Atom™
Imagination PowerVR Series 6
Graphics 2D/3D Engine
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
1GB RAM
8GB storage
13. Android TV app
features
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
• Android
TV
devices
have
Android
Lollipop
5.0
on
board.
• Android
TV
has
inside
the
same
Android
mul4media
framework
of
normal
devices.
• Android
TV
app
can
be
built
on
API
17
towards.
• Based
on
Leanback
Android
Support
library
14. 10 feet experience
Smartphone 5”
320 dp
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
TV Full HD 30”
320 dpi
10 feet
16 inches
15. Manifest features
Support landscape
Portrait activity are forbidden
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
16. Manifest features
No touch screen
<uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
17. Manifest features
Limit sensor
<uses-feature android:name="android.hardware.sensor.accelerometer”
android:required="false" />
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
18. Keep calm and lean back!!
Provides built-in tailored for 10 feet experience
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
20. Leanback support library
Model View Presenter
Model
Presenter
View
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
21. Leanback UI component
BroswerFragment
allows the developer “to create a primary layout for browsing categories and rows of
media items [with a minimum of code]”
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
22. Leanback UI component
DetailFragment
Display information about the content that the user has selected
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
23. Chromecast
a cast-ready device for multi-screen experience
source: www.google.it
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
25. Chromecast
components
• Google Cast technology
• Multi-Screen experiece
• Google Cast SDK
• Sender Application
• Android app
• iOS app
• Chrome app
• Receiver Application
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
• Default Media Receiver
• Styled Media Receiver
• Custom Media Receiver
26. Android Client Application
library dependencies
• Minimum SDK version supported by Google Cast is 9 (Gingerbread)
• MediaRouter API of android-support-v7
• Google Play Services
• AppCompat API of android-support-v7
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
27. Android Client Application
typical sender application flow
• Sender app starts MediaRouter device discovery: MediaRouter.addCallback
• MediaRouter informs sender app of the route the user selected: MediaRouter.Callback.onRouteSelected
• Sender app retrieves CastDevice instance: CastDevice.getFromBundle
• Sender app creates and uses GoogleApiClient: GoogleApiClient.Builder
source: developers.google.com
• Sender app launches the receiver app: Cast.CastApi.launchApplication
• Sender app creates a communication channel: Cast.CastApi.setMessageReceivedCallbacks
• Sender sends a message to the receiver over the communication channel: Cast.CastApi.sendMessage
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
28. Cast-Ready Device Discovery
capabilities
Remote
Playback
Live
Audio
Live
Video
MediaRouteSelector.Builder mediaRouteSelectorBuilder = new MediaRouteSelector.Builder();
mediaRouteSelectorBuilder.addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK);
mediaRouteSelectorBuilder.addControlCategory(MediaControlIntent.CATEGORY_LIVE_AUDIO);
mediaRouteSelectorBuilder.addControlCategory(MediaControlIntent.CATEGORY_LIVE_VIDEO);
MediaRouterSelector mediaRouterSelector = mediaRouterSelectorBuilder.build();
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
29. Media Cast Button
easy approach for discovering
source: developers.google.com source: developers.google.com
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem mediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item);
MediaRouteActionProvider mediaRouteActionProvider = (MediaRouteActionProvider)
MenuItemCompat.getActionProvider(mediaRouteMenuItem);
mediaRouteActionProvider.setRouteSelector(mMediaRouteSelector); …. }
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
30. Receiver Application
what a mistery?
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
What is?
HTML5 and Javascript application
What does it do?
Display the media content on TV
Message handling
Which type?
Default
Media
Receiver
Styled
Media
Receiver
Custom
Media
Receiver
31. Default Media Receiver
simplest
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
• Off-the-shelf
• No UI customization
• No registration
Source: developers.google.com
32. Styled Media Receiver
simple and customizable
• Similar to Default Media Player
• CSS UI customization
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
• Registration
Source: developers.google.com
33. Custom Media Receiver (1/3)
whatever you want
• Fully Web Applicaiton
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
• Debug(able) at 9222
• Registration
Source: developers.google.com
35. Custom Media Receiver (3/3)
advanced features
• Video Codification/Decodification
• H.264 High Profile Level 4.1, 4.2 and 5
• VP8
• Adaptive Bitrate Streaming
• HTTP Live Streaming (HLS)
• Dynamic Adaptive Streaming over HTTP (MPEG-DASH)
• Smooth Streaming
• Digital Rights Management
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
• Play Ready DRM
• Widevine DRM
• Media Player Library
36. Channel, Namespace and Protocol (1/2)
communication
• Protocol: a set of well-known messages
• Namespace: a labeled protocol
• Channel: the communication layer
class CustomChannel implements Cast.MessageReceivedCallback {
public String getNamespace() { return “urn:x-cast:com.channel.custom”; }
@Override
public void onMessageReceiver(CastDevice castDevice, String namespace, String message) { … }
}
…
Cast.CastApi.setMessageReceivedCallbacks(mApiClient, mCustomChannel.getNamespace(),
mCustomChannel);
…
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
37. Channel, Namespace and Protocol (2/2)
communication
• Media Namespace: urn:x-cast:com.google.media.cast
• RemoteMediaPlayer
• MediaManager
RECEIVER MEDIA CHANNEL
window.mediaManager = new cast.receiver.MediaManager(window.mediaElement);
…
CLIENT MEDIA CHANNEL
Cast.CastApi.setMessageReceivedCallbacks(mApiClient,
mRemoteMediaPlayer.getNamespace(), mCustomChannel);
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
…
38. Google Api Client and Media Route Provider
manufacturing
Source: developers.google.com
CastDevice selectedCastDevice = CastDevice.getFromBundle(selectedRouteInfo.getExtras);
Cast.CastOptions.Builder apiOptionsBuilder = new Cast.CastOptions.Builder(selectedCastDevice, …);
googleApiClient = new GoogleApiClient.Builder().addApi(Cast.API, apiOptionsBuilder.build()).build();
googleApiClient.connect();
Cast.CastApi.launchApplication(googleApiClient, applicationId, launchOptions);
Cast.CastApi.joinApplication(googleApiClient);
Cast.CastApi.stopApplication(googleApiClient);
Cast.CastApi.leaveApplication(googleApiClient);
Droidcon London – 2014 – Matteo Bonifazi & Alessandro Martellucci
android:screenOrientation can’t be portrait, reversePortrait, sensorPortrait, userPortrait or reverseLandscape.
No touchescreen– TV control comes with DPAD. TouchScreen must not to be required since Google Play won’t filter.
Limit Sensor– Make optional every uses features which are for not crucial operation.
Before starting to develop the Android client application is important to have in mind which are the basic requirements.
First of all there are few library dependencies and constraints you’ll need to care about.
Number one: the minimum SDK version supported by Goole Cast technology is the 9 that is Gingerbread
Number two: the Goole Cast client APIs are packaged into the android.support.v7.media package of Android Support Library revision 19 o later
Number three: you’ll need to setup the Google Play Services library revision 4.2 or later
Number four: you must import the AppCompat which provides basic UI elements like the MediaButton, useful for establish a connection to the Chromecast device
Now let me show you what is the typical sender application flow because it’s also important to understand which are the steps the client runs in order to start and control a receiver application. During the development phase you’ll have to interact with several Java objects.
So first of all:
the sender application uses the MediaRouter object to discover all the cast-device available inside the local WiFi network.
through its callback, the MediaRouter informs the sender application which of the available.
From the RouteInfo object the sender application extracts the instance of the CastDevice object: the most important object inside the development lifecycle. This is the object which represents the hardware specification of the cast-ready device
Once the sender application knows the cast device the user selected it makes use of Google Play Services APIs in order to launch the receiver application and to establish a communication channel where several messages are exhanged
Only at the end of these steps you’ll be able to play any kind of content using the RemoteMediaPlayer object
Now I would like to give you more details about the discovery phase. It’s important because, inside the same local WiFi network, many cast-ready devices could be connected, everyone with different capabilities. So the sender app must make a choice in order to select the capailities it needs for.
The Google Cast SDK provides three main categories that the Chromecast is able to manage:
REMOTE PLAYBACK
LIVE AUDIO
LIVE VIDEO
REMOTE PLAYBACK
category idendifies all the routes which present a remote media by playing content from URI. These routes take the responsability for fetching and rendering content on their own. The application doesn’t display any content on the screen of the device.
Different are the cases of the LIVE AUDIO and LIVE VIDEO where the routes support live audio and live video streaming respectively from device to the destination. Theses routes are the easiest to use because the application renders the content locally on the screen of the device and the system streams it automatically to the destination. A destination that could be a local display or wireless display with support for mirroring or Android presentation.
Further, in this slide you can see how it’s easy to set the requested capabilities for the Android client application, using the MediaRouteSelector object.
On the other side we have the receiver application. What a mistery? What a mistery just because I’ve experienced a lot of questions and curiosity from many collegues of mine. They couldn’t imagine what the receiver application is and how it works. I realized it was often confusing and frustrating for Android developers to find out that the receiver application is just an HTML5 and Javascript web application.
Exactly. Inside the Chromecast device what’s running is a custom version of Chrome web browser. This decision is the reason why the Chromecast became a successful product. Having a widespread and consolidated technology like Chrome web browser playing the role of the media player allowed the many developers to solve most of the known problem about video streaming and to mantain client and server technology respectively decoupled.
So what does it do? First of all it displays the media content on the TV and further it provides an handler for the management of the messages exchanged between the client application and the recevier device.
As we’ll see later there is a strong presence of messages flow starting from the client towards the receiver and vice versa.
If you’ll start develpoing a Chromecast application you would choose one of the three available media receiver: a default media receiver, a styled media receiver and finally a custom media receiver. The Chromecast Receiver API provides different solutions based on what are your needs.
And now i’m showing you them.
The default media receiver is the simplest and easy to use. It’s a prebuilt reciever application, hosted into the Google Cloud Factory, and it’s designed for the streaming of audio and video contents.
It doesn’t provide any UI customization as Styled and Custom one permits and further it doesn’t require any kind of registration as Styled and Custom needs to.
The Styled Media Receiver, instead, represents the right choice if you need a ready-made application receiver for media playing with the possibility to customise some little UI elements.
By publishing a simple CSS file, with specific CSS class names, it’s possible change the logo image, the progress bar logo, the title style and so no.
Differenty from the previous Media Receiver, the default one, the styled media receiver requires the user to register the receiver application by simply fullfil a web form.
This registration calls for a name and a URL referring to host where the CSS file is remoted.