11

I'm switching my app away from the CastCompanionLibrary because I need to support more streaming devices and a few of my users are having trouble with Chromecast discovery. I stripped out all of the unnecessary code and made an app just to see what was going on during discovery and sent it to one of those users and the code is never getting any of the callbacks from the media router. At the same time they can run my old app with the CastCompanionLibrary and it will find the Chromecast just fine.

Here is the code I sent the user, it is a self contained class and layout:

    public class MainActivity extends AppCompatActivity {

    private static SimpleDateFormat TIME_ONLY = new SimpleDateFormat("HH:mm:ss.SSS");
    private static Handler handler;
    protected MediaRouter.Callback mMediaRouterCallback;
    boolean isRunning = false;
    private TextView logView;
    private MediaRouter mMediaRouter;
    private MediaRouteSelector mMediaRouteSelector;
    private Timer removeRoutesTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        logView = (TextView) findViewById(R.id.log);
        Button sendLog = (Button)findViewById(R.id.send_log);
        sendLog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent send = new Intent(Intent.ACTION_SENDTO);
                String uriText = "mailto:" + Uri.encode("myemail") +
                        "?subject=" + Uri.encode("Logs") +
                        "&body=" + Uri.encode(logView.getText().toString());
                Uri uri = Uri.parse(uriText);

                send.setData(uri);
                startActivity(Intent.createChooser(send, "Send logs..."));
            }
        });

        mMediaRouter = createMediaRouter(getApplicationContext());
        mMediaRouterCallback = new MediaRouterCallback();
        mMediaRouteSelector = new MediaRouteSelector.Builder()
                .addControlCategory(CastMediaControlIntent.categoryForCast(
                        CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID))
                .build();
        mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
                MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
        log("Discovery callback requested");
    }

    private void printRoutes() {
        List<MediaRouter.RouteInfo> routes = mMediaRouter.getRoutes();
        log("has routes " + routes.size() );
        for(MediaRouter.RouteInfo info : routes){
            log("has routes " + info.getName() +" - " + info.getDescription());
        }
    }

    public static void runOnUI(Runnable runnable) {
        if (handler == null) {
            handler = new Handler(Looper.getMainLooper());
        }

        handler.post(runnable);
    }
    protected MediaRouter createMediaRouter(Context context) {
        return MediaRouter.getInstance(context);
    }



    private void log(String start) {
        Calendar cal = new GregorianCalendar();
        String currentLog = logView.getText().toString();
        logView.setText(currentLog + "\n" + TIME_ONLY.format(cal.getTime()) + " - " + start);

    }








    private class MediaRouterCallback extends MediaRouter.Callback {

        @Override
        public void onRouteAdded(MediaRouter router, MediaRouter.RouteInfo route) {
            log("onRouteAdded - " + route.getName());

            super.onRouteAdded(router, route);


        }

        @Override
        public void onRouteChanged(MediaRouter router, MediaRouter.RouteInfo route) {
            log("onRouteChanged - " + route.getName());

            super.onRouteChanged(router, route);


        }


        @Override
        public void onRoutePresentationDisplayChanged(MediaRouter router,
                                                      MediaRouter.RouteInfo route) {

            log("onRoutePresentationDisplayChanged -  [" + route.getName() + "] ["
                    + route.getDescription() + "]");

            super.onRoutePresentationDisplayChanged(router, route);
        }

        @Override
        public void onRouteRemoved(final MediaRouter router, final MediaRouter.RouteInfo route) {
            log("onRouteRemoved - " + route.getName());

            super.onRouteRemoved(router, route);


        }

        @Override
        public void onRouteVolumeChanged(MediaRouter router, final MediaRouter.RouteInfo route) {

            super.onRouteVolumeChanged(router, route);
        }

        private void removeServices(final MediaRouter.RouteInfo route) {
            log("removeServices - " + route.getName());


        }
    }
}

Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                tools:context=".MainActivity">

    <Button
        android:id="@+id/send_log"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_alignParentTop="true"
        android:text="Send log"/>



    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/send_log">

        <TextView
            android:id="@+id/log"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            />
    </ScrollView>
</RelativeLayout>

Here is the log she sent me:

08:06:52.542 - Discovery callback requested

So basically no calls to the callback methods.

Here is a log from my network where it works just fine (exact same apk):

16:56:06.085 - Discovery callback requested
16:56:07.193 - onRouteAdded - Master bedroom
16:56:07.268 - onRouteAdded - Living room chromecast
16:56:07.274 - onRouteChanged - Master bedroom
16:56:07.332 - onRouteAdded - Gym
16:56:07.337 - onRouteChanged - Living room chromecast
16:56:07.341 - onRouteChanged - Master bedroom
16:56:07.405 - onRouteChanged - Gym
16:56:07.409 - onRouteChanged - Living room chromecast
16:56:07.413 - onRouteAdded - Basement east bedroom
16:56:07.417 - onRouteChanged - Master bedroom
16:56:07.517 - onRouteChanged - Gym
16:56:07.522 - onRouteChanged - Living room chromecast
16:56:07.526 - onRouteChanged - Basement east bedroom
16:56:07.530 - onRouteChanged - Master bedroom

As you can see the code for discovering a Chromecast is ridiculously simple, I don't see where the mistake could be. As far as I know these are all the lines needed:

mMediaRouter = createMediaRouter(getApplicationContext());
            mMediaRouterCallback = new MediaRouterCallback();
            mMediaRouteSelector = new MediaRouteSelector.Builder()
                    .addControlCategory(CastMediaControlIntent.categoryForCast(
                            CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID))
                    .build();
            mMediaRouter.addCallback(mMediaRouteSelector, mMediaRouterCallback,
                    MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);

I should add that for this particular user if I change MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY to MediaRouter.CALLBACK_FLAG_FORCE_DISCOVERY then it works fine.

So what is wrong with my discovery code?

Thanks.

casolorz
  • 8,486
  • 19
  • 93
  • 200
  • If it's a discovery issue only affect some of your users, have you checked that those users set up [chromecast properly](https://support.google.com/chromecast/answer/3249268?hl=en&ref_topic=3297376&vid=1-635779274275327339-3122124891)? Particularly, having their network being chromecast enabled and compatible. – Andy Sep 15 '15 at 15:25
  • Yes I have. Also it works fine if I send them an app using the CastCompanionLibrary. I have read the CastCompanionLibrary discovery code multiple times and I don't see what they are doing different, the code is so simple I just don't know where the mistake is. – casolorz Sep 15 '15 at 18:45
  • Are you using same application id? If not, did you add your new application to the google cast developer console? – Ilya Tretyakov Sep 21 '15 at 09:48
  • I've tried several application ids, I have two chromecast apps, one released over a year and a half ago and the other six months ago, I've tried both those IDs, and on this sample test I sent to the user I tried `CastMediaControlIntent.DEFAULT_MEDIA_RECEIVER_APPLICATION_ID`. – casolorz Sep 21 '15 at 14:13
  • I have a similar issue where the `onRouteAdded` callback is never invoked (though the others are). Not sure if that's normal, however judging from your log it appears that it's not. – aroth May 22 '17 at 07:54

0 Answers0