I did more testing with Fire TV Stick 4K 2nd gen connected to Denon AVC-X4800H.
I believe I found the cause, at least with my device combination. On Sleep event Kodi shuts down the audio engine. Then, during processing of Wake-Up event Kodi reinitializes audio engine. However Kodi receives the Wake-Up event before the handshake between Fire TV and AVR is completed. As a result Kodi doesn't see capabilities of AVR. That's probably also the reason why Kodi IEC packer doesn't appear in audio device list.
If audio engine would be initialised not directly on Wake-Up event but a few seconds later, that would fix the issue.
There is a simple way to test this and you don't even need a special build. The test uses Kodi's support of external players. When Kodi launches an external player it shuts down the audio engine and reinitializes it when the external playback is completed. In our test we use this feature to reinitialize audio engine at any time - we want to do that a few seconds after wake-up.
External players are configured in Kodi using a special configuration file, as described in
Kodi WiKi. I've prepared an
example file - save it as
playercorefactory.xml in Kodi
userdata directory and restart Kodi. Now you should have a context menu item "Play using…", where you can select "Fake player". Kodi will show a message (Note: the config file refers to a non-existing app, nothing will be really started). When you click on OK Kodi will reinitialize audio engine. Passthrough should work again after that.
After knowing that we need a delay before initialising audio engine on wake-up I added a delay to the source code, just before
this line.
In my tests I needed a 3 seconds delay if I put the Fire TV into sleep using Gear-button on remote (AVR and TV remained on). However I needed a much longer delay when all devices were switched off using On/Off-button on remote (as I usually do). When all devices are awaken I can navigate through Kodi menus, then, a few seconds later, the screen goes black for a fraction of second. Only after that the audio engine must be initialised. I don't know why my device combination is doing this but I have to accept this. This requires a delay of whole 10 seconds in my case.
Instead of using such long delay in the wake-up routine I tried a different approach. I just completely disabled deactivating/reactivating of audio engine on sleep/wake-up events. To do this I commented out these two lines of code:
one and
two. I did not encounter any issues with this approach. This has completely fixed the issue for me.
I suggest to implement a setting in Kodi to disable audio engine reinitialization after wake-up (this would also disable audio engine shutdown on sleep).
I understand the intention of the code but not all devices in real world behave as expected. If for some reason I need to reinitialize the audio engine I can restart Kodi. That's much better when having to restart Kodi every single time after sleep/wake-up as I needed before the fix.