Skip to content

Commit

Permalink
[Cast,Android,Presentation API] Send messages back to the right clien…
Browse files Browse the repository at this point in the history
…ts, listen for messages sooner.

CastRouteController now sends messages to the route id associated with the client id, mMediaRouteId was removed.
Also PresentationDispatcher subscribes to the PresentationConnection messages before passing the new connection to the page.

BUG=569601
TEST=manually run all previously passing Cast e2e tests with the newly passing auto-join test.

Review URL: https://codereview.chromium.org/1522313003

Cr-Commit-Position: refs/heads/master@{#365635}
  • Loading branch information
avayvod authored and Commit bot committed Dec 16, 2015
1 parent 798046b commit 25472ea
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ public interface RouteController {
*/
String getSourceId();

/**
* @return the route id
*/
String getRouteId();

/**
* @return the media sink id for the route.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ public interface RouteDelegate {
void onRouteRequestError(String message, int requestId);

/**
* Called when the route is closed.
* @param routeId the id of the closed route.
* Called when the session is closed.
*/
void onRouteClosed(String routeId);
void onSessionClosed();

/**
* Called when sending a message to the route has finished.
Expand All @@ -38,8 +37,8 @@ public interface RouteDelegate {

/**
* Called when the route receives a message.
* @param routeId The id of the route.
* @param clientId The id of the client receiving the message.
* @param message The message received.
*/
void onMessage(String routeId, String message);
void onMessage(String clientId, String message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,23 @@ public void onRouteRequestError(String message, int requestId) {
}

@Override
public void onRouteClosed(String routeId) {
mLastRemovedRouteRecord = getClientRecordByRouteId(routeId);
mClientRecords.remove(mLastRemovedRouteRecord);
public void onSessionClosed() {
if (mSession == null) return;

mManager.onRouteClosed(routeId);
if (mSession != null) {
for (String sessionRouteId : mSession.routeIds) {
if (sessionRouteId.equals(routeId)) continue;
if (mClientRecords.isEmpty()) {
mRoutes.clear();
mSession.clientIds.clear();
mSession.routeIds.clear();
} else {
mLastRemovedRouteRecord = mClientRecords.iterator().next();
for (ClientRecord client : mClientRecords) {
mManager.onRouteClosed(client.routeId);

mManager.onRouteClosed(routeId);
mRoutes.remove(client.routeId);
mSession.routeIds.remove(client.routeId);
mSession.clientIds.remove(client.clientId);
}
mClientRecords.clear();
}

mSession = null;
Expand All @@ -141,8 +147,11 @@ public void onMessageSentResult(boolean success, int callbackId) {
}

@Override
public void onMessage(String routeId, String message) {
mManager.onMessage(routeId, message);
public void onMessage(String clientId, String message) {
ClientRecord clientRecord = getClientRecordByClientId(clientId);
if (clientRecord == null) return;

mManager.onMessage(clientRecord.routeId, message);
}

/**
Expand Down Expand Up @@ -282,24 +291,17 @@ public void joinRoute(String sourceId, String presentationId, String origin, int
}

MediaRoute route = new MediaRoute(mSession.session.getSinkId(), sourceId, presentationId);
mRoutes.put(route.id, route);

this.onRouteCreated(nativeRequestId, route, mSession.session);
}

@Override
public void closeRoute(String routeId) {
MediaRoute route = mRoutes.get(routeId);

if (route == null) {
onRouteClosed(routeId);
return;
}
if (route == null) return;

if (mSession == null || !mSession.routeIds.contains(routeId)) {
if (mSession == null) {
mRoutes.remove(routeId);

onRouteClosed(routeId);
return;
}

Expand All @@ -314,13 +316,17 @@ public void closeRoute(String routeId) {
@Override
public void detachRoute(String routeId) {
mRoutes.remove(routeId);
if (mSession != null) mSession.routeIds.remove(routeId);
ClientRecord client = getClientRecordByRouteId(routeId);

for (int i = mClientRecords.size() - 1; i >= 0; --i) {
ClientRecord client = mClientRecords.get(i);
if (client.routeId.equals(routeId)) mClientRecords.remove(i);
if (mSession != null) mSession.clientIds.remove(client.clientId);
if (client != null) {
mClientRecords.remove(client);
mLastRemovedRouteRecord = client;
}

if (mSession == null) return;

mSession.routeIds.remove(routeId);
if (client != null) mSession.clientIds.remove(client.clientId);
}

@Override
Expand Down Expand Up @@ -351,6 +357,8 @@ private CastMediaRouteProvider(

@Nullable
private boolean canAutoJoin(MediaSource source, String origin, int tabId) {
if (source.getAutoJoinPolicy().equals(MediaSource.AUTOJOIN_PAGE_SCOPED)) return false;

MediaSource currentSource = MediaSource.from(mSession.session.getSourceId());
if (!currentSource.getApplicationId().equals(source.getApplicationId())) return false;

Expand All @@ -365,9 +373,7 @@ private boolean canAutoJoin(MediaSource source, String origin, int tabId) {

if (client == null) return false;

if (source.getAutoJoinPolicy().equals(MediaSource.AUTOJOIN_PAGE_SCOPED)) {
return false;
} else if (source.getAutoJoinPolicy().equals(MediaSource.AUTOJOIN_ORIGIN_SCOPED)) {
if (source.getAutoJoinPolicy().equals(MediaSource.AUTOJOIN_ORIGIN_SCOPED)) {
return origin.equals(client.origin);
} else if (source.getAutoJoinPolicy().equals(MediaSource.AUTOJOIN_TAB_AND_ORIGIN_SCOPED)) {
return origin.equals(client.origin) && tabId == client.tabId;
Expand All @@ -393,6 +399,11 @@ private boolean canJoinExistingSession(String presentationId, String origin, int
return false;
}

private void onRouteClosed(String routeId) {
mManager.onRouteClosed(routeId);
detachRoute(routeId);
}

@Nullable
private ClientRecord getClientRecordByClientId(String clientId) {
for (ClientRecord record : mClientRecords) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ private static void removeNullFields(Object object) throws JSONException {
}
}

private final String mMediaRouteId;
private final String mOrigin;
private final int mTabId;
private final CastMessagingChannel mMessageChannel;
Expand Down Expand Up @@ -182,7 +181,6 @@ private static void removeNullFields(Object object) throws JSONException {
* Initializes a new {@link CastRouteController} instance.
* @param apiClient The Google Play Services client used to create the session.
* @param sessionId The session identifier to use with the Cast SDK.
* @param mediaRouteId The media route identifier associated with this session.
* @param origin The origin of the frame requesting the route.
* @param tabId the id of the tab containing the frame requesting the route.
* @param source The {@link MediaSource} corresponding to this session.
Expand All @@ -194,14 +192,12 @@ public CastRouteController(
ApplicationMetadata metadata,
String applicationStatus,
CastDevice castDevice,
String mediaRouteId,
String origin,
int tabId,
MediaSource source,
RouteDelegate delegate) {
mApiClient = apiClient;
mSessionId = sessionId;
mMediaRouteId = mediaRouteId;
mOrigin = origin;
mTabId = tabId;
mSource = source;
Expand Down Expand Up @@ -301,11 +297,6 @@ public String getSourceId() {
return mSource.getUrn();
}

@Override
public String getRouteId() {
return mMediaRouteId;
}

@Override
public String getSinkId() {
return mCastDevice.getDeviceId();
Expand Down Expand Up @@ -436,7 +427,7 @@ public void onResult(Status status) {
mSessionId = null;
mApiClient = null;

mRouteDelegate.onRouteClosed(getRouteId());
mRouteDelegate.onSessionClosed();
mStoppingApplication = false;

MediaNotificationManager.hide(
Expand Down Expand Up @@ -511,8 +502,8 @@ private boolean handleClientConnectMessage(JSONObject jsonMessage)

mClients.add(clientId);

mRouteDelegate.onMessage(mMediaRouteId, buildInternalMessage(
"new_session", buildSessionMessage(), clientId, INVALID_SEQUENCE_NUMBER));
sendClientMessageTo(
clientId, "new_session", buildSessionMessage(), INVALID_SEQUENCE_NUMBER);

if (mMediaPlayer != null && !isApiClientInvalid()) mMediaPlayer.requestStatus(mApiClient);

Expand All @@ -539,8 +530,7 @@ private boolean handleLeaveSessionMessage(JSONObject jsonMessage) throws JSONExc

// TODO(avayvod): "leave" the other clients with the matching origin/tab id.
// See https://crbug.com/549957.
mRouteDelegate.onMessage(mMediaRouteId,
buildInternalMessage("leave_session", null, clientId, sequenceNumber));
sendClientMessageTo(clientId, "leave_session", null, sequenceNumber);

mClients.remove(clientId);

Expand Down Expand Up @@ -875,7 +865,7 @@ private void broadcastClientMessage(String type, String message) {

private void sendClientMessageTo(
String clientId, String type, String message, int sequenceNumber) {
mRouteDelegate.onMessage(mMediaRouteId,
mRouteDelegate.onMessage(clientId,
buildInternalMessage(type, message, clientId, sequenceNumber));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ private void reportSuccess(Cast.ApplicationConnectionResult result) {
result.getApplicationMetadata(),
result.getApplicationStatus(),
mSink.getDevice(),
route.id,
mOrigin,
mTabId,
mSource,
Expand Down
8 changes: 4 additions & 4 deletions content/renderer/presentation/presentation_dispatcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,9 @@ void PresentationDispatcher::OnDefaultSessionStarted(
return;

if (!session_info.is_null()) {
presentation_service_->ListenForSessionMessages(session_info.Clone());
controller_->didStartDefaultSession(
new PresentationConnectionClient(session_info.Clone()));
presentation_service_->ListenForSessionMessages(session_info.Pass());
new PresentationConnectionClient(session_info.Pass()));
}
}

Expand All @@ -366,9 +366,9 @@ void PresentationDispatcher::OnSessionCreated(
}

DCHECK(!session_info.is_null());
presentation_service_->ListenForSessionMessages(session_info.Clone());
callback->onSuccess(blink::adoptWebPtr(
new PresentationConnectionClient(session_info.Clone())));
presentation_service_->ListenForSessionMessages(session_info.Pass());
new PresentationConnectionClient(session_info.Pass())));
}

void PresentationDispatcher::OnConnectionStateChanged(
Expand Down

0 comments on commit 25472ea

Please sign in to comment.