i8beef / node-red-contrib-castv2

MIT License
22 stars 14 forks source link

An active cast session isn't closed correctly before castv2-command #49

Closed Einstein2150 closed 3 years ago

Einstein2150 commented 3 years ago

When sending a TTS to a cast-device (f.e. in my case a pair of nest mini) you aren't able to cast a Spotify session to the speakers until a) a unspecific time has passed without any playback b) you told google to stop the audio from playing which disconnects the current TTS session

While the cast-device is blocked from the TTS-Session Spotify says always "connecting" bus doesn't connect...

Is there a solution to force a disconnect after a TTS playback?

Einstein2150 commented 3 years ago

Sending return { payload: { app: "DefaultMediaReceiver", type: "CLOSE" } }; before starting the TTS seems to make the reconnect of the Spotify cast better. Maybe the current playing cast which is connected at the time the TTS starts get somehow stuck in a state of confusion. Maybe you should trigger the CLOSE hardcoded before any action is started...

i8beef commented 3 years ago

The Spotify support is a beta, which needs further development and testing. I would expect it's not an issue with the general approach here, but specific to the Spotify implementation.

I don't use Spotify so i can't really test this here. How are you casting a Spotify session after the tts cast? Are you seeing anything in the nodered log or error nodes (check both)?

Einstein2150 commented 3 years ago

Ok. First im not using the spotify function in castv2. I'm simply stream from the macOS desktop application/iOS app with the embedded cast-function in spotify. While playing the debug says:

{"mediaSessionId":7,"playbackRate":1,"playerState":"PLAYING","currentTime":0.775,"supportedMediaCommands":514511,"volume":{"level":1,"muted":false},"activeTrackIds":[],"currentItemId":1,"repeatMode":"REPEAT_OFF"}

TTS is breaking the established (Spotify-)connection really hard and connects itself to the speakers (bling - connecting sound is playing)

First it is switching to IDLE:

{"mediaSessionId":1,"playbackRate":1,"playerState":"IDLE","currentTime":0,"supportedMediaCommands":274447,"volume":{"level":1,"muted":false},"activeTrackIds":[],"media":{"contentId":"https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=de&q=%22Guten%20Morgen!%20Im%20B%C3%BCro%20sind%20es%2021.6%20Grad.%20Mach%20dir%20einen%20sch%C3%B6nen%20Arbeitstag!","contentType":"audio/mp3","streamType":"BUFFERED","metadata":{"metadataType":0,"title":"Media title","images":[{"url":"https://nodered.org/node-red-icon.png"}]},"mediaCategory":"AUDIO"},"currentItemId":1,"extendedStatus":{"playerState":"LOADING","media":{"contentId":"https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=de&q=%22Guten%20Morgen!%20Im%20B%C3%BCro%20sind%20es%2021.6%20Grad.%20Mach%20dir%20einen%20sch%C3%B6nen%20Arbeitstag!","contentType":"audio/mp3","streamType":"BUFFERED","metadata":{"metadataType":0,"title":"Media title","images":[{"url":"https://nodered.org/node-red-icon.png"}]},"mediaCategory":"AUDIO"},"mediaSessionId":1},"repeatMode":"REPEAT_OFF"}

Then it is PLAYING (5 Debug messages like this)

{"mediaSessionId":1,"playbackRate":1,"playerState":"PLAYING","currentTime":0,"supportedMediaCommands":274447,"volume":{"level":1,"muted":false},"activeTrackIds":[],"media":{"contentId":"https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=de&q=%22Guten%20Morgen!%20Im%20B%C3%BCro%20sind%20es%2021.6%20Grad.%20Mach%20dir%20einen%20sch%C3%B6nen%20Arbeitstag!","contentType":"audio/mp3","streamType":"BUFFERED","metadata":{"metadataType":0,"title":"Media title","images":[{"url":"https://nodered.org/node-red-icon.png"}]},"mediaCategory":"AUDIO","duration":7.512,"tracks":[{"trackId":1,"type":"AUDIO"}],"breakClips":[],"breaks":[]},"currentItemId":1,"items":[{"itemId":1,"media":{"contentId":"https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=de&q=%22Guten%20Morgen!%20Im%20B%C3%BCro%20sind%20es%2021.6%20Grad.%20Mach%20dir%20einen%20sch%C3%B6nen%20Arbeitstag!","contentType":"audio/mp3","streamType":"BUFFERED","metadata":{"metadataType":0,"title":"Media title","images":[{"url":"https://nodered.org/node-red-icon.png"}]},"mediaCategory":"AUDIO","duration":7.512},"autoplay":true,"activeTrackIds":[],"orderId":0}],"repeatMode":"REPEAT_OFF"}

TTS is playing - all fine.

Afte an undefined debug message there are another 2 IDLE like this:

{"mediaSessionId":1,"playbackRate":1,"playerState":"IDLE","currentTime":0,"supportedMediaCommands":274447,"volume":{"level":1,"muted":false},"currentItemId":1,"idleReason":"FINISHED"}

After playing the manual (re)connection from the spotify app is not possible and stuck at connecting (bling - connecting sound is playing)

There is another undefined and a null debug message.

{"mediaSessionId":1,"playbackRate":1,"playerState":"IDLE","supportedMediaCommands":514511,"volume":{},"media":{"contentId":"spotify:track:29MYzr6RNOpMY1SijWMGuJ","streamType":"BUFFERED","mediaCategory":"AUDIO","contentType":"application/x-spotify.track","metadata":{"metadataType":3,"title":"Gangster","songName":"Gangster","artist":"Loredana","albumName":"Gangster","images":[{"url":"https://i.scdn.co/image/ab67616d00001e02c838f900118cfaa17a459fd3","height":300,"width":300},{"url":"https://i.scdn.co/image/ab67616d00004851c838f900118cfaa17a459fd3","height":64,"width":64},{"url":"https://i.scdn.co/image/ab67616d0000b273c838f900118cfaa17a459fd3","height":640,"width":640}]},"entity":"spotify:track:29MYzr6RNOpMY1SijWMGuJ","duration":161.582},"currentItemId":1,"extendedStatus":{"playerState":"LOADING","media":{"contentId":"spotify:track:29MYzr6RNOpMY1SijWMGuJ","streamType":"BUFFERED","mediaCategory":"AUDIO","contentType":"application/x-spotify.track","metadata":{"metadataType":3,"title":"Gangster","songName":"Gangster","artist":"Loredana","albumName":"Gangster","images":[{"url":"https://i.scdn.co/image/ab67616d00001e02c838f900118cfaa17a459fd3","height":300,"width":300},{"url":"https://i.scdn.co/image/ab67616d00004851c838f900118cfaa17a459fd3","height":64,"width":64},{"url":"https://i.scdn.co/image/ab67616d0000b273c838f900118cfaa17a459fd3","height":640,"width":640}]},"entity":"spotify:track:29MYzr6RNOpMY1SijWMGuJ","duration":161.582},"mediaSessionId":1},"repeatMode":"REPEAT_OFF"}

After around 10 seconds there is another connection hearable (bling - connecting sound is playing) and it is switching to

{"mediaSessionId":1,"playbackRate":1,"playerState":"PAUSED","currentTime":29.791,"supportedMediaCommands":514511,"volume":{"level":1,"muted":false},"activeTrackIds":[],"media":{"contentId":"spotify:track:29MYzr6RNOpMY1SijWMGuJ","streamType":"BUFFERED","mediaCategory":"AUDIO","contentType":"application/x-spotify.track","metadata":{"metadataType":3,"title":"Gangster","songName":"Gangster","artist":"Loredana","albumName":"Gangster","images":[{"url":"https://i.scdn.co/image/ab67616d00001e02c838f900118cfaa17a459fd3","height":300,"width":300},{"url":"https://i.scdn.co/image/ab67616d00004851c838f900118cfaa17a459fd3","height":64,"width":64},{"url":"https://i.scdn.co/image/ab67616d0000b273c838f900118cfaa17a459fd3","height":640,"width":640}]},"entity":"spotify:track:29MYzr6RNOpMY1SijWMGuJ","duration":161.582,"tracks":[],"breaks":[],"breakClips":[]},"currentItemId":1,"items":[{"itemId":1,"media":{"contentId":"spotify:track:29MYzr6RNOpMY1SijWMGuJ","streamType":"BUFFERED","mediaCategory":"AUDIO","contentType":"application/x-spotify.track","metadata":{"metadataType":3,"title":"Gangster","songName":"Gangster","artist":"Loredana","albumName":"Gangster","images":[{"url":"https://i.scdn.co/image/ab67616d00001e02c838f900118cfaa17a459fd3","height":300,"width":300},{"url":"https://i.scdn.co/image/ab67616d00004851c838f900118cfaa17a459fd3","height":64,"width":64},{"url":"https://i.scdn.co/image/ab67616d0000b273c838f900118cfaa17a459fd3","height":640,"width":640}]},"entity":"spotify:track:29MYzr6RNOpMY1SijWMGuJ","duration":161.582},"autoplay":false,"customData":{"isForcingLoadOnlyToUpdateUI":true},"orderId":0}],"repeatMode":"REPEAT_OFF"}

after a manual click on the play-button its playing

{"mediaSessionId":1,"playbackRate":1,"playerState":"PLAYING","currentTime":30.085,"supportedMediaCommands":514511,"volume":{"level":1,"muted":false},"activeTrackIds":[],"currentItemId":1,"repeatMode":"REPEAT_OFF"}

Second way: The connection seems to be faster when I'm speaking to Google that he should finish the playback right after TTS is ending. A reconnect in the desktop application is now really quick possible.

Third way: The fastest way is described in my second comment. Sending return { payload: { app: "DefaultMediaReceiver", type: "CLOSE" } }; before TTS seems to close the active cast session correctly. A reconnection to the speaker is now possible without any additional work right after the TTS ends...

i8beef commented 3 years ago

There COULD be a bug here as I noticed it falling back to a connected state on any new MEDIA cast when it should stay in "joined", and it really shouldn't be outputting null payloads. That may be an issue I need to look at this weekend, but shouldn't be your issue.

A quick search makes it look like Spotify has had a relatively broken Cast implementation for years... (which is somewhat disappointing because I'm not sure I'm gonna stick with YouTube Music). Apparently this "connecting" issue has been a complaint for a very long time. I am hesitant to change the model of the entire node for one misbehaving application... there are legitimate cases for NOT calling CLOSE first.

However, you can easily simulate this behavior yourself by just triggering an explicit "CLOSE" before starting other casts directly in node-red. It will break the connection though, so you'll want to make sure you only send this when you are starting a new cast (i.e., right before sending MEDIA or TTS commands, but not before other commands like PLAY, PAUSE, etc.).

i8beef commented 3 years ago

Closing as won't fix for now. If you need more guidance reopen.