OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.media.router.cast; | 5 package org.chromium.chrome.browser.media.router.cast; |
6 | 6 |
7 import android.content.Context; | |
8 import android.os.Handler; | 7 import android.os.Handler; |
9 import android.support.v7.media.MediaRouteSelector; | 8 import android.support.v7.media.MediaRouteSelector; |
10 import android.support.v7.media.MediaRouter; | 9 import android.support.v7.media.MediaRouter; |
11 import android.support.v7.media.MediaRouter.RouteInfo; | 10 import android.support.v7.media.MediaRouter.RouteInfo; |
12 | 11 |
13 import org.json.JSONException; | 12 import org.json.JSONException; |
14 import org.json.JSONObject; | 13 import org.json.JSONObject; |
15 | 14 |
16 import org.chromium.base.Log; | 15 import org.chromium.base.Log; |
17 import org.chromium.base.VisibleForTesting; | 16 import org.chromium.base.VisibleForTesting; |
(...skipping 15 matching lines...) Expand all Loading... |
33 /** | 32 /** |
34 * A {@link MediaRouteProvider} implementation for Cast devices and applications
. | 33 * A {@link MediaRouteProvider} implementation for Cast devices and applications
. |
35 */ | 34 */ |
36 public class CastMediaRouteProvider implements MediaRouteProvider, DiscoveryDele
gate { | 35 public class CastMediaRouteProvider implements MediaRouteProvider, DiscoveryDele
gate { |
37 | 36 |
38 private static final String TAG = "MediaRouter"; | 37 private static final String TAG = "MediaRouter"; |
39 | 38 |
40 private static final String AUTO_JOIN_PRESENTATION_ID = "auto-join"; | 39 private static final String AUTO_JOIN_PRESENTATION_ID = "auto-join"; |
41 private static final String PRESENTATION_ID_SESSION_ID_PREFIX = "cast-sessio
n_"; | 40 private static final String PRESENTATION_ID_SESSION_ID_PREFIX = "cast-sessio
n_"; |
42 | 41 |
43 private final Context mApplicationContext; | |
44 private final MediaRouter mAndroidMediaRouter; | 42 private final MediaRouter mAndroidMediaRouter; |
45 private final MediaRouteManager mManager; | 43 private final MediaRouteManager mManager; |
46 private final CastMessageHandler mMessageHandler; | 44 private final CastMessageHandler mMessageHandler; |
47 private final Map<String, DiscoveryCallback> mDiscoveryCallbacks = | 45 private final Map<String, DiscoveryCallback> mDiscoveryCallbacks = |
48 new HashMap<String, DiscoveryCallback>(); | 46 new HashMap<String, DiscoveryCallback>(); |
49 private final Map<String, MediaRoute> mRoutes = new HashMap<String, MediaRou
te>(); | 47 private final Map<String, MediaRoute> mRoutes = new HashMap<String, MediaRou
te>(); |
50 private ClientRecord mLastRemovedRouteRecord; | 48 private ClientRecord mLastRemovedRouteRecord; |
51 private final Map<String, ClientRecord> mClientRecords = new HashMap<String,
ClientRecord>(); | 49 private final Map<String, ClientRecord> mClientRecords = new HashMap<String,
ClientRecord>(); |
52 | 50 |
53 // There can be only one Cast session at the same time on Android. | 51 // There can be only one Cast session at the same time on Android. |
54 private CastSession mSession; | 52 private CastSession mSession; |
55 private CreateRouteRequest mPendingCreateRouteRequest; | 53 private CreateRouteRequest mPendingCreateRouteRequest; |
56 private Handler mHandler = new Handler(); | 54 private Handler mHandler = new Handler(); |
57 | 55 |
58 /** | 56 /** |
59 * Builder class for {@link CastMediaRouteProvider}. | 57 * Builder class for {@link CastMediaRouteProvider}. |
60 */ | 58 */ |
61 public static class Builder implements MediaRouteProvider.Builder { | 59 public static class Builder implements MediaRouteProvider.Builder { |
62 @Override | 60 @Override |
63 public MediaRouteProvider create(Context applicationContext, MediaRouteM
anager manager) { | 61 public MediaRouteProvider create(MediaRouteManager manager) { |
64 return CastMediaRouteProvider.create(applicationContext, manager); | 62 return CastMediaRouteProvider.create(manager); |
65 } | 63 } |
66 } | 64 } |
67 | 65 |
68 private static class OnSinksReceivedRunnable implements Runnable { | 66 private static class OnSinksReceivedRunnable implements Runnable { |
69 | 67 |
70 private final WeakReference<MediaRouteManager> mRouteManager; | 68 private final WeakReference<MediaRouteManager> mRouteManager; |
71 private final MediaRouteProvider mRouteProvider; | 69 private final MediaRouteProvider mRouteProvider; |
72 private final String mSourceId; | 70 private final String mSourceId; |
73 private final List<MediaSink> mSinks; | 71 private final List<MediaSink> mSinks; |
74 | 72 |
75 OnSinksReceivedRunnable(MediaRouteManager manager, MediaRouteProvider ro
uteProvider, | 73 OnSinksReceivedRunnable(MediaRouteManager manager, MediaRouteProvider ro
uteProvider, |
76 String sourceId, List<MediaSink> sinks) { | 74 String sourceId, List<MediaSink> sinks) { |
77 mRouteManager = new WeakReference<MediaRouteManager>(manager); | 75 mRouteManager = new WeakReference<MediaRouteManager>(manager); |
78 mRouteProvider = routeProvider; | 76 mRouteProvider = routeProvider; |
79 mSourceId = sourceId; | 77 mSourceId = sourceId; |
80 mSinks = sinks; | 78 mSinks = sinks; |
81 } | 79 } |
82 | 80 |
83 @Override | 81 @Override |
84 public void run() { | 82 public void run() { |
85 MediaRouteManager manager = mRouteManager.get(); | 83 MediaRouteManager manager = mRouteManager.get(); |
86 if (manager != null) manager.onSinksReceived(mSourceId, mRouteProvid
er, mSinks); | 84 if (manager != null) manager.onSinksReceived(mSourceId, mRouteProvid
er, mSinks); |
87 } | 85 } |
88 } | 86 } |
89 | 87 |
90 /** | 88 /** |
91 * @param applicationContext The application context to use for this route p
rovider. | |
92 * @return Initialized {@link CastMediaRouteProvider} object or null if it's
not supported. | 89 * @return Initialized {@link CastMediaRouteProvider} object or null if it's
not supported. |
93 */ | 90 */ |
94 @Nullable | 91 @Nullable |
95 public static CastMediaRouteProvider create( | 92 public static CastMediaRouteProvider create(MediaRouteManager manager) { |
96 Context applicationContext, MediaRouteManager manager) { | 93 MediaRouter androidMediaRouter = ChromeMediaRouter.getAndroidMediaRouter
(); |
97 assert applicationContext != null; | |
98 MediaRouter androidMediaRouter = | |
99 ChromeMediaRouter.getAndroidMediaRouter(applicationContext); | |
100 if (androidMediaRouter == null) return null; | 94 if (androidMediaRouter == null) return null; |
101 | 95 |
102 return new CastMediaRouteProvider(applicationContext, androidMediaRouter
, manager); | 96 return new CastMediaRouteProvider(androidMediaRouter, manager); |
103 } | 97 } |
104 | 98 |
105 public void onLaunchError() { | 99 public void onLaunchError() { |
106 for (String routeId : mRoutes.keySet()) { | 100 for (String routeId : mRoutes.keySet()) { |
107 mManager.onRouteClosedWithError(routeId, "Launch error"); | 101 mManager.onRouteClosedWithError(routeId, "Launch error"); |
108 } | 102 } |
109 mRoutes.clear(); | 103 mRoutes.clear(); |
110 mClientRecords.clear(); | 104 mClientRecords.clear(); |
111 } | 105 } |
112 | 106 |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 sink.getId(), source.getUrn(), request.getPresentationId()); | 276 sink.getId(), source.getUrn(), request.getPresentationId()); |
283 addRoute(route, request.getOrigin(), request.getTabId()); | 277 addRoute(route, request.getOrigin(), request.getTabId()); |
284 mManager.onRouteCreated(route.id, route.sinkId, request.getNativeRequest
Id(), this, true); | 278 mManager.onRouteCreated(route.id, route.sinkId, request.getNativeRequest
Id(), this, true); |
285 | 279 |
286 if (source.getClientId() != null) { | 280 if (source.getClientId() != null) { |
287 ClientRecord clientRecord = mClientRecords.get(source.getClientId())
; | 281 ClientRecord clientRecord = mClientRecords.get(source.getClientId())
; |
288 if (clientRecord != null) { | 282 if (clientRecord != null) { |
289 sendReceiverAction(clientRecord.routeId, sink, source.getClientI
d(), "cast"); | 283 sendReceiverAction(clientRecord.routeId, sink, source.getClientI
d(), "cast"); |
290 } | 284 } |
291 } | 285 } |
292 request.start(mApplicationContext); | 286 request.start(); |
293 } | 287 } |
294 | 288 |
295 @Override | 289 @Override |
296 public void joinRoute(String sourceId, String presentationId, String origin,
int tabId, | 290 public void joinRoute(String sourceId, String presentationId, String origin,
int tabId, |
297 int nativeRequestId) { | 291 int nativeRequestId) { |
298 MediaSource source = MediaSource.from(sourceId); | 292 MediaSource source = MediaSource.from(sourceId); |
299 if (source == null || source.getClientId() == null) { | 293 if (source == null || source.getClientId() == null) { |
300 mManager.onRouteRequestError("Unsupported presentation URL", nativeR
equestId); | 294 mManager.onRouteRequestError("Unsupported presentation URL", nativeR
equestId); |
301 return; | 295 return; |
302 } | 296 } |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 JSONObject jsonMessage = new JSONObject(); | 440 JSONObject jsonMessage = new JSONObject(); |
447 jsonMessage.put("type", type); | 441 jsonMessage.put("type", type); |
448 jsonMessage.put("sequenceNumber", sequenceNumber); | 442 jsonMessage.put("sequenceNumber", sequenceNumber); |
449 jsonMessage.put("timeoutMillis", 0); | 443 jsonMessage.put("timeoutMillis", 0); |
450 jsonMessage.put("clientId", clientId); | 444 jsonMessage.put("clientId", clientId); |
451 jsonMessage.put("message", message); | 445 jsonMessage.put("message", message); |
452 return jsonMessage.toString(); | 446 return jsonMessage.toString(); |
453 } | 447 } |
454 | 448 |
455 @VisibleForTesting | 449 @VisibleForTesting |
456 static CastMediaRouteProvider createCastMediaRouteProviderForTest( | 450 CastMediaRouteProvider(MediaRouter androidMediaRouter, MediaRouteManager man
ager) { |
457 Context applicationContext, MediaRouter androidMediaRouter, MediaRou
teManager manager) { | |
458 return new CastMediaRouteProvider(applicationContext, androidMediaRouter
, manager); | |
459 } | |
460 | |
461 @VisibleForTesting | |
462 CastMediaRouteProvider( | |
463 Context applicationContext, MediaRouter androidMediaRouter, MediaRou
teManager manager) { | |
464 mApplicationContext = applicationContext; | |
465 mAndroidMediaRouter = androidMediaRouter; | 451 mAndroidMediaRouter = androidMediaRouter; |
466 mManager = manager; | 452 mManager = manager; |
467 mMessageHandler = new CastMessageHandler(this); | 453 mMessageHandler = new CastMessageHandler(this); |
468 } | 454 } |
469 | 455 |
470 @Nullable | 456 @Nullable |
471 private boolean canAutoJoin(MediaSource source, String origin, int tabId) { | 457 private boolean canAutoJoin(MediaSource source, String origin, int tabId) { |
472 if (source.getAutoJoinPolicy().equals(MediaSource.AUTOJOIN_PAGE_SCOPED))
return false; | 458 if (source.getAutoJoinPolicy().equals(MediaSource.AUTOJOIN_PAGE_SCOPED))
return false; |
473 | 459 |
474 MediaSource currentSource = MediaSource.from(mSession.getSourceId()); | 460 MediaSource currentSource = MediaSource.from(mSession.getSourceId()); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
565 } | 551 } |
566 } | 552 } |
567 | 553 |
568 private void removeClient(@Nullable ClientRecord client) { | 554 private void removeClient(@Nullable ClientRecord client) { |
569 if (client == null) return; | 555 if (client == null) return; |
570 | 556 |
571 mLastRemovedRouteRecord = client; | 557 mLastRemovedRouteRecord = client; |
572 mClientRecords.remove(client.clientId); | 558 mClientRecords.remove(client.clientId); |
573 } | 559 } |
574 } | 560 } |
OLD | NEW |