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