lochstar / nativescript-cast

NativeScript Chromecast Plugin.
Apache License 2.0
16 stars 4 forks source link

Add support for custom media receiver + sendMessage #6

Open WeeJeWel opened 5 years ago

WeeJeWel commented 5 years ago

Currently, it seems that only the media namespace is supported.

For my application, I have a custom media receiver with a message bus on a separate namespace (urn:x-cast:foo.bar).

I would like to be able to use sendMessage to send a message to the app running on the chromecast.

jcarolus commented 5 years ago

@WeeJeWel Do you have a public receiver app that can be used for testing?

WeeJeWel commented 5 years ago

I will publish one for you that echoes back what has been sent asap.

Sent from my iPhone

On 26 May 2019, at 17:35, Jeroen Carolus notifications@github.com wrote:

@WeeJeWel Do you have a public receiver app that can be used for testing?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

WeeJeWel commented 5 years ago

I've created a sample application you can test it with. Source: https://github.com/WeeJeWel/chromecast-sandbox

Sender (for use in Chrome): https://weejewel.github.io/chromecast-sandbox/sender.html App ID: 8CBC577D Namespace: urn:x-cast:com.sandbox

If you need anything else, please let me know!

jcarolus commented 5 years ago

Is the receiver app accidentally pointed to the sender as well? It looks as if the Chromecast is also loading the sender.

WeeJeWel commented 5 years ago

I had that as well. A reboot of the Chromecast fixed it. Probably needed to refresh the list of app IDs or something like it.

lochstar commented 5 years ago

I'd like this as well. Should be able to get it added for the next release.

jcarolus commented 5 years ago

Ah yes - if something is published while your Chromecast is still on, funny. My primary focus is on the media controller, @lochstar we could add a folder for the different receiver apps (one for the customData, one for this messaging)?

lochstar commented 5 years ago

I'll create a custom reciever that we can expand to handle our test cases. I don't think we'll need multiple. Leave this with me, I need this feature for one of my projects.

WeeJeWel commented 5 years ago

Hi guys. Anything I can do to help speed up development? I was hoping to get my hands dirty on my project this weekend :)

lochstar commented 5 years ago

Working on it. Are you after iOS or Android support first?

WeeJeWel commented 5 years ago

Eventually both but me personally first iOS.

lochstar commented 5 years ago

You can check out the custom-channel branch for the iOS work-in-progress. It has addChannel, removeChannel, & sendMessage functions added. See https://github.com/lochstar/nativescript-cast/blob/custom-channel/demo/app/main-view-model.ts#L122 for sample usage.

It requires a bit more testing and I'd like to create a custom reciever for the demo app. I'm also considering promisifying the functions.

WeeJeWel commented 5 years ago

Verrrry nice! I got it working in my project.

One odd thing I noticed: the didConnect method is executed immediately. So sending a message using cast.sendMessage(channel, ...) in the didConnect method crashes the app, because the const channel = cast.addChannel hasn't returned yet. Took me a while to figure that out.

Maybe actually wait for the connection, or else maybe put it in a nextTick.

Brilliant work so far!

WeeJeWel commented 5 years ago

@lochstar Can I help to get Android support working as well?

lochstar commented 5 years ago

Yep. It immediately crashes with no error message when I try to create a new com.google.android.gms.cast.Cast.MessageReceivedCallback.

const castSession = this.mSessionManager.getCurrentCastSession();

// Immediately crashes with no error ?
castSession.setMessageReceivedCallbacks(namespace, new com.google.android.gms.cast.Cast.MessageReceivedCallback({
  onMessageReceived(param0: com.google.android.gms.cast.CastDevice, param1: string, param2: string): void {
    console.log('onMessageReceived');
    // didReceiveTextMessage(args);
  }
}));
jcarolus commented 5 years ago

@lochstar @WeeJeWel

Using adb logcat you can see some of the crash dump - containing:

# Fatal error in v8::ToLocalChecked
# Empty MaybeLocal.
libc    : Fatal signal 5 (SIGTRAP), code 1 in tid 12212 (tivescript.cast), pid 12212 (tivescript.cast)
crash_dump64: type=1400 audit(0.0:243): avc: denied { search } for name="com.google.android.gms" dev="dm-2" ino=662350 scontext=u:r:crash_dump:s0:c512,c768 tcontext=u:object_r:app_data_file:s0:c512,c768 tclass=dir permissive=0

This is usually some null error.

Perhaps the instantiation of com.google.android.gms.cast.Cast.MessageReceivedCallback is failing somehow. Was this generated code (param0 ... etc and interface methods) copy pasted from a working Android app (same gms lib verstion etc)?

lochstar commented 5 years ago

No, not copy pasted from another app. I've tried making it a named interface as well and get the same crash. As soon as com.google.android.gms.cast.Cast.MessageReceivedCallback is touched it crashes.

jcarolus commented 5 years ago

I just noticed that https://developers.google.com/cast/docs/migrate_v2/android_sender mentions under Custom Channel Registration that The registration can be done in the SessionManagerListener.onSessionStarted callback method. This could be the reason for the crash? Documentation is lacking on setMessageReceivedCallbacks...

Edit: can you also push your latest changes (there is something missing), so I can debug a bit more.

lochstar commented 5 years ago

I tried registering it in the onSessionStarted callback, no luck. Same crash. I also tried using the deprecated Cast.CastApi to set the method, but same problem.

jcarolus commented 5 years ago

I am not a nativescript expert (yet) :) - but could it be that you need to run tns plugin build again, not sure if that checks somehow the used classes and build them into the nativescript_cast.aar?

jcarolus commented 5 years ago

Checked ^^, but no, still crashes. Another thing that is curious is that it's a static interface - public static interface Cast.MessageReceivedCallback. From Java perspective, implementing this interface is still the same, but I am a bit in the dark here, so anything is suspicious... When I have some more time, I can try to debug some deeper. Let me know if you find anything that could help.

WeeJeWel commented 5 years ago

I've tried to take a look at it but my limited Android skills didn't get me any far either..

lochstar commented 5 years ago

Tried it out with NS6 and it still crashes.. damn.

WeeJeWel commented 5 years ago

Is there anyone working on this issue? :)

lochstar commented 5 years ago

I did a little debugging in the NS Slack. I need to create a ticket for the NS team to look at on the android-runtime repo. I'll post here when I do.

gscottschulz commented 4 years ago

Any progress on this for Android @lochstar ?

lochstar commented 4 years ago

@gscottschulz No, not at all. I did check recently when I updated all the libs but it still crashes on Android and I havent yet escalated it to the NativeScript team.