RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.onemedia.provider; |
| 17 | |
Jeff Brown | ff0215d | 2014-07-14 04:05:08 -0700 | [diff] [blame] | 18 | import android.media.routing.MediaRouteSelector; |
| 19 | import android.media.routing.MediaRouteService; |
| 20 | import android.media.routing.MediaRouter.ConnectionInfo; |
| 21 | import android.media.routing.MediaRouter.ConnectionRequest; |
| 22 | import android.media.routing.MediaRouter.DestinationInfo; |
| 23 | import android.media.routing.MediaRouter.DiscoveryRequest; |
| 24 | import android.media.routing.MediaRouter.RouteInfo; |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 25 | import android.media.session.PlaybackState; |
| 26 | import android.os.Bundle; |
| 27 | import android.os.Handler; |
Jeff Brown | ff0215d | 2014-07-14 04:05:08 -0700 | [diff] [blame] | 28 | import android.os.Process; |
| 29 | import android.support.media.protocols.MediaPlayerProtocol; |
| 30 | import android.support.media.protocols.MediaPlayerProtocol.MediaInfo; |
| 31 | import android.support.media.protocols.MediaPlayerProtocol.MediaStatus; |
RoboErik | c785a78 | 2014-07-14 13:40:43 -0700 | [diff] [blame] | 32 | import android.os.Looper; |
| 33 | import android.os.ResultReceiver; |
| 34 | import android.os.SystemClock; |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 35 | import android.util.Log; |
| 36 | |
| 37 | import com.android.onemedia.playback.LocalRenderer; |
| 38 | import com.android.onemedia.playback.Renderer; |
| 39 | import com.android.onemedia.playback.RequestUtils; |
| 40 | |
| 41 | import java.util.ArrayList; |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Test of MediaRouteProvider. Show a dummy provider with a simple interface for |
| 45 | * playing music. |
| 46 | */ |
Jeff Brown | ff0215d | 2014-07-14 04:05:08 -0700 | [diff] [blame] | 47 | public class OneMediaRouteProvider extends MediaRouteService { |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 48 | private static final String TAG = "OneMRP"; |
| 49 | private static final boolean DEBUG = true; |
| 50 | |
Jeff Brown | ff0215d | 2014-07-14 04:05:08 -0700 | [diff] [blame] | 51 | private static final String TEST_DESTINATION_ID = "testDestination"; |
| 52 | private static final String TEST_ROUTE_ID = "testRoute"; |
| 53 | |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 54 | private Renderer mRenderer; |
| 55 | private RenderListener mRenderListener; |
| 56 | private PlaybackState mPlaybackState; |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 57 | private Handler mHandler; |
| 58 | |
Jeff Brown | ff0215d | 2014-07-14 04:05:08 -0700 | [diff] [blame] | 59 | private OneStub mStub; |
| 60 | |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 61 | @Override |
| 62 | public void onCreate() { |
| 63 | mHandler = new Handler(); |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 64 | mRenderer = new LocalRenderer(this, null); |
| 65 | mRenderListener = new RenderListener(); |
RoboErik | c785a78 | 2014-07-14 13:40:43 -0700 | [diff] [blame] | 66 | PlaybackState.Builder bob = new PlaybackState.Builder(); |
| 67 | bob.setActions(PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_PLAY); |
| 68 | mPlaybackState = bob.build(); |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 69 | |
| 70 | mRenderer.registerListener(mRenderListener); |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
Jeff Brown | ff0215d | 2014-07-14 04:05:08 -0700 | [diff] [blame] | 74 | public ClientSession onCreateClientSession(ClientInfo client) { |
| 75 | if (client.getUid() != Process.myUid()) { |
| 76 | // for testing purposes, only allow connections from this application |
| 77 | // since this provider is not fully featured |
| 78 | return null; |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 79 | } |
Jeff Brown | ff0215d | 2014-07-14 04:05:08 -0700 | [diff] [blame] | 80 | return new OneSession(client); |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Jeff Brown | ff0215d | 2014-07-14 04:05:08 -0700 | [diff] [blame] | 83 | private final class OneSession extends ClientSession { |
| 84 | private final ClientInfo mClient; |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 85 | |
Jeff Brown | ff0215d | 2014-07-14 04:05:08 -0700 | [diff] [blame] | 86 | public OneSession(ClientInfo client) { |
| 87 | mClient = client; |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
Jeff Brown | ff0215d | 2014-07-14 04:05:08 -0700 | [diff] [blame] | 91 | public boolean onStartDiscovery(DiscoveryRequest req, DiscoveryCallback callback) { |
| 92 | for (MediaRouteSelector selector : req.getSelectors()) { |
| 93 | if (isMatch(selector)) { |
| 94 | DestinationInfo destination = new DestinationInfo.Builder( |
| 95 | TEST_DESTINATION_ID, getServiceMetadata(), "OneMedia") |
| 96 | .setDescription("Test route from OneMedia app.") |
| 97 | .build(); |
| 98 | ArrayList<RouteInfo> routes = new ArrayList<RouteInfo>(); |
| 99 | routes.add(new RouteInfo.Builder( |
| 100 | TEST_ROUTE_ID, destination, selector).build()); |
| 101 | callback.onDestinationFound(destination, routes); |
| 102 | return true; |
| 103 | } |
| 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void onStopDiscovery() { |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public boolean onConnect(ConnectionRequest req, ConnectionCallback callback) { |
| 114 | if (req.getRoute().getId().equals(TEST_ROUTE_ID)) { |
| 115 | mStub = new OneStub(); |
| 116 | ConnectionInfo connection = new ConnectionInfo.Builder(req.getRoute()) |
| 117 | .setProtocolStub(MediaPlayerProtocol.class, mStub) |
| 118 | .build(); |
| 119 | callback.onConnected(connection); |
| 120 | return true; |
| 121 | } |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void onDisconnect() { |
| 127 | mStub = null; |
| 128 | } |
| 129 | |
| 130 | private boolean isMatch(MediaRouteSelector selector) { |
| 131 | if (!selector.containsProtocol(MediaPlayerProtocol.class)) { |
| 132 | return false; |
| 133 | } |
| 134 | for (String protocol : selector.getRequiredProtocols()) { |
| 135 | if (!protocol.equals(MediaPlayerProtocol.class.getName())) { |
| 136 | return false; |
| 137 | } |
| 138 | } |
| 139 | return true; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | private final class OneStub extends MediaPlayerProtocol.Stub { |
| 144 | MediaInfo mMediaInfo; |
| 145 | |
| 146 | public OneStub() { |
| 147 | super(mHandler); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void onLoad(MediaInfo mediaInfo, boolean autoplay, long playPosition, |
| 152 | Bundle extras) { |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 153 | if (DEBUG) { |
Jeff Brown | ff0215d | 2014-07-14 04:05:08 -0700 | [diff] [blame] | 154 | Log.d(TAG, "Attempting to play " + mediaInfo.getContentId()); |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 155 | } |
| 156 | // look up the route and send a play command to it |
Jeff Brown | ff0215d | 2014-07-14 04:05:08 -0700 | [diff] [blame] | 157 | mMediaInfo = mediaInfo; |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 158 | Bundle bundle = new Bundle(); |
Jeff Brown | ff0215d | 2014-07-14 04:05:08 -0700 | [diff] [blame] | 159 | bundle.putString(RequestUtils.EXTRA_KEY_SOURCE, mediaInfo.getContentId()); |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 160 | mRenderer.setContent(bundle); |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Override |
Jeff Brown | ff0215d | 2014-07-14 04:05:08 -0700 | [diff] [blame] | 164 | public void onPlay(Bundle extras) { |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 165 | mRenderer.onPlay(); |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
Jeff Brown | ff0215d | 2014-07-14 04:05:08 -0700 | [diff] [blame] | 169 | public void onPause(Bundle extras) { |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 170 | mRenderer.onPause(); |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | private class RenderListener implements Renderer.Listener { |
| 175 | |
| 176 | @Override |
| 177 | public void onError(int type, int extra, Bundle extras, Throwable error) { |
| 178 | Log.d(TAG, "Sending onError with type " + type + " and extra " + extra); |
Jeff Brown | ff0215d | 2014-07-14 04:05:08 -0700 | [diff] [blame] | 179 | sendStatusUpdate(PlaybackState.STATE_ERROR); |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public void onStateChanged(int newState) { |
RoboErik | f137242 | 2014-04-23 14:38:17 -0700 | [diff] [blame] | 184 | long position = -1; |
| 185 | if (mRenderer != null) { |
| 186 | position = mRenderer.getSeekPosition(); |
| 187 | } |
RoboErik | c785a78 | 2014-07-14 13:40:43 -0700 | [diff] [blame] | 188 | int pbState; |
| 189 | float rate = 0; |
| 190 | String errorMsg = null; |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 191 | switch (newState) { |
| 192 | case Renderer.STATE_ENDED: |
| 193 | case Renderer.STATE_STOPPED: |
RoboErik | c785a78 | 2014-07-14 13:40:43 -0700 | [diff] [blame] | 194 | pbState = PlaybackState.STATE_STOPPED; |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 195 | break; |
| 196 | case Renderer.STATE_INIT: |
| 197 | case Renderer.STATE_PREPARING: |
RoboErik | c785a78 | 2014-07-14 13:40:43 -0700 | [diff] [blame] | 198 | pbState = PlaybackState.STATE_BUFFERING; |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 199 | break; |
| 200 | case Renderer.STATE_ERROR: |
RoboErik | c785a78 | 2014-07-14 13:40:43 -0700 | [diff] [blame] | 201 | pbState = PlaybackState.STATE_ERROR; |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 202 | break; |
| 203 | case Renderer.STATE_PAUSED: |
RoboErik | c785a78 | 2014-07-14 13:40:43 -0700 | [diff] [blame] | 204 | pbState = PlaybackState.STATE_PAUSED; |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 205 | break; |
| 206 | case Renderer.STATE_PLAYING: |
RoboErik | c785a78 | 2014-07-14 13:40:43 -0700 | [diff] [blame] | 207 | pbState = PlaybackState.STATE_PLAYING; |
| 208 | rate = 1; |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 209 | break; |
| 210 | default: |
RoboErik | c785a78 | 2014-07-14 13:40:43 -0700 | [diff] [blame] | 211 | pbState = PlaybackState.STATE_ERROR; |
| 212 | errorMsg = "unknown state"; |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 213 | break; |
| 214 | } |
RoboErik | c785a78 | 2014-07-14 13:40:43 -0700 | [diff] [blame] | 215 | PlaybackState.Builder bob = new PlaybackState.Builder(mPlaybackState); |
| 216 | bob.setState(pbState, position, rate, SystemClock.elapsedRealtime()); |
| 217 | bob.setErrorMessage(errorMsg); |
| 218 | mPlaybackState = bob.build(); |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 219 | |
Jeff Brown | ff0215d | 2014-07-14 04:05:08 -0700 | [diff] [blame] | 220 | sendStatusUpdate(mPlaybackState.getState()); |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public void onBufferingUpdate(int percent) { |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void onFocusLost() { |
RoboErik | c785a78 | 2014-07-14 13:40:43 -0700 | [diff] [blame] | 229 | Log.d(TAG, "Focus lost, pausing"); |
| 230 | // Don't update state here, we'll get a separate call to |
| 231 | // onStateChanged when it pauses |
RoboErik | f137242 | 2014-04-23 14:38:17 -0700 | [diff] [blame] | 232 | mRenderer.onPause(); |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public void onNextStarted() { |
| 237 | } |
Jeff Brown | ff0215d | 2014-07-14 04:05:08 -0700 | [diff] [blame] | 238 | |
| 239 | private void sendStatusUpdate(int state) { |
| 240 | if (mStub != null) { |
| 241 | MediaStatus status = new MediaStatus(1, mStub.mMediaInfo); |
| 242 | switch (state) { |
| 243 | case PlaybackState.STATE_BUFFERING: |
| 244 | case PlaybackState.STATE_FAST_FORWARDING: |
| 245 | case PlaybackState.STATE_REWINDING: |
| 246 | case PlaybackState.STATE_SKIPPING_TO_NEXT: |
| 247 | case PlaybackState.STATE_SKIPPING_TO_PREVIOUS: |
| 248 | status.setPlayerState(MediaStatus.PLAYER_STATE_BUFFERING); |
| 249 | break; |
| 250 | case PlaybackState.STATE_CONNECTING: |
| 251 | case PlaybackState.STATE_STOPPED: |
| 252 | status.setPlayerState(MediaStatus.PLAYER_STATE_IDLE); |
| 253 | break; |
| 254 | case PlaybackState.STATE_PAUSED: |
| 255 | status.setPlayerState(MediaStatus.PLAYER_STATE_PAUSED); |
| 256 | break; |
| 257 | case PlaybackState.STATE_PLAYING: |
| 258 | status.setPlayerState(MediaStatus.PLAYER_STATE_PLAYING); |
| 259 | break; |
| 260 | case PlaybackState.STATE_NONE: |
| 261 | case PlaybackState.STATE_ERROR: |
| 262 | default: |
| 263 | status.setPlayerState(MediaStatus.PLAYER_STATE_UNKNOWN); |
| 264 | break; |
| 265 | } |
| 266 | mStub.sendStatusUpdatedEvent(status, null); |
| 267 | } |
| 268 | } |
RoboErik | 07c7077 | 2014-03-20 13:33:52 -0700 | [diff] [blame] | 269 | } |
| 270 | } |