[Unofficial] moonlight-embedded

moonlight-embedded w/ Luna (BETA)

Source code with amlogic decoder changes: https://github.com/TheChoconut/moonlight-embedded
Modified Luna: https://github.com/stickman89/Luna
Moonlight install script: https://gist.githubusercontent.com/TheChoconut/fe550f8c19c11f71a85841f135eddecb/raw/

Setting up:

Download Luna from GitHub releases:

Latest direct download: https://github.com/stickman89/Luna/releases/download/v0.6.6.1-alpha/script.luna-0.6.6.1-alpha.zip

Install Luna in Kodi:
Addons -> Install from ZIP --> script.luna.zip

Select “Setup Moonlight PKG” in the settings window. It will automatically setup moonlight package so you don’t have to worry about anything else.

Set up Luna by entering Host IP in script settings. Click on Pair Host and enter the code on your host machine. Set your wanted settings (resolution, framerate, etc.)
Your game libraries is available in “Games”. Clicking on a game will launch the game with previously selected settings.

Alternative: Manual setup (use this if method above doesn't work)

SSH into your box.
Paste the following line in the console:

Stable version

wget https://gist.githubusercontent.com/TheChoconut/fe550f8c19c11f71a85841f135eddecb/raw/ -qO - | bash
Moonlight-experiment dl helper command
wget https://gist.githubusercontent.com/TheChoconut/6d260b55439f39bf69e7dd030d7190dd/raw/8e40e3e7bafcc5472be55b997aaf4ebb9419a39e/download_moonlight.sh -qO - | bash

This will set up your moonlight binaries in the right spot so it is compatible with Luna script.

ZeroTier notices by @stickman89

Init scripts are not required for ZeroTier.
Just enable ‘ZeroTier Connect’ via Luna. Set the ‘Host IP’ to your hosts ZeroTier assigned address within Luna > Settings and ZeroTier will automatically initiate a connection before game launch, and close the connection on session disconnect.

Thank you @stickman89!

Latest Luna changelog

New loading animation
Cleanup duplicate code
Update the app icon
Monitor app icon for changes and update cache accordingly
Move some configuration files to Settings.xml. (zerotier & lastrun)

Latest moonlight-embedded changelog

Added support for delaying A/V playback.
Tweaked build scripts as some libraries aren’t required anymore.

3 Likes

Gratz for the effort.
And bump for visibility.
Myself can’t help on development (lack of skills),
but a moonlight (and/or steam) client is more than welcome.

I did a build of moonlight a little while ago, didn’t have to use any outside stuff or add anything. I think just one network related package needed to be bumped.

using the osmc stuff can also cause issues with playback. since there are differences between the version of the library used by each project.

Few questions.

What compiler did you use for compiling moonlight?
Where could I locate libopus, libenet on CoreELEC?
What version of libamcodec were you using for compilation?

When I use libamcodec from CoreELEC, moonlight won’t accept libmoonlight-aml.so because /usr/lib/libamcodec.so: undefined symbol: audio_decode_set_pre_mute.
My first assumption would be missing libavutils or libamadec, but the CoreELEC build of libamcodec doesn’t require those. When I use OSMC’s version, it works just fine.

I just created a new moonlight package.

There are packages for both opus and enet in the CoreELEC repo.

You may need to update the enet package however.

diff --git a/packages/addons/addon-depends/enet/package.mk b/packages/addons/addon-depends/
enet/package.mk
index f056918478..b7687bab39 100644
--- a/packages/addons/addon-depends/enet/package.mk
+++ b/packages/addons/addon-depends/enet/package.mk
@@ -2,11 +2,11 @@
 # Copyright (C) 2016-present Team LibreELEC (https://libreelec.tv)
 
 PKG_NAME="enet"
-PKG_VERSION="e33ca1d"
-PKG_SHA256="0ba5547de2c4c7fc79d367179a9bc92a7ac27e9258dd50fb277cd8761afaf9b0"
+PKG_VERSION="eda26a26d913a1e86f44fae725049c97dc1ce5a8"
+PKG_SHA256=""
 PKG_LICENSE=""
-PKG_SITE="https://github.com/cgutman/enet/"
-PKG_URL="https://github.com/cgutman/enet/archive/$PKG_VERSION.tar.gz"
+PKG_SITE="https://github.com/lsalzman/enet/"
+PKG_URL="https://github.com/lsalzman/enet/archive/$PKG_VERSION.tar.gz"
 PKG_DEPENDS_TARGET="toolchain"
 PKG_LONGDESC="A network communication layer on top of UDP (User Datagram Protocol)."
 PKG_TOOLCHAIN="autotools"

For now I consider these two packages unimportant, my main concern is libamcodec as this library is managing video output and how moonlight communicates to aml video drivers. I’m not sure how this one is getting built, I only saw CoreELEC is pulling it from your own sources but it originates from amlogic sites. How exactly are you getting the source code for libamlogic? And does that library work just fine or not? In my case it doesn’t want to load it at all because of missing symbols… Does it work for you?

Sending good news!

I have checked out some issues of moonlight-embedded on github and found the solution for constant codec failures that kept disabling the video output and making the stream stutter. Applied the fixes from sdl decoder to amlogic and now it doesn’t drop any frames!

I have very good connection and I almost cannot experience frame drops. I am still not sure how good my coding skills are since this was my first attempt at doing this. Even though I do know programming, I never did anything close to this so I would need someone to test with wifi connections or something that can drop frames.

I will drop footage on Dropbox and more details on how to grab experimental version tmrw.
1080p60 was working on both H264 and H265 codec.

2 Likes

I have published experimental binaries of moonlight-embedded on Dropbox, it would be nice if you can checkout with poor wifi connection to see how the decoder performs (or doesn’t work at all).

I still wanna know if I could get libopus and libenet from /usr/lib? Where are these located on a CoreELEC installation? Also could you try updating libamcodec to osmc version? Or if not, I’d like to know what’s the build process for the current one. I only saw you are downloading prebuilt sources from your own site.

Thank you for your help!

Seems unfortunate that there are no testers yet. I don’t think its so dangerous to run this.
Anyways, I have now added a script.luna addon to Dropbox, which is modified from the original to work with this experimental build. I did not build libraries so it cannot find hosts on its own, you have to write the host IP address and pair with code. After that it should be working fine.

I have also tried to remove the limitation of modifying LD_LIBRARY_PATH but unfortunately it has to be modified to load the modules because it always looks at /usr/lib before anything else and it always loads libamcodec.so from there instead of using the one I prepared which works fine.
The only solution is to update the libamcodec to a version that will work w/ moonlight or to remove it from /usr/lib or somehow find a linker workaround to not search in /usr/lib.

Best fix would be to implement moonlight back in CoreELEC repo and make it an optional addon which would simplify everything. With moonlight we could also add luna launcher so its easier to load games directly from Kodi. Before that happens, this binary has to be tested on multiple devices.

1 Like

Will try this the next days with a khadas vim and report back.

Would be nice if this becomes a real addon

Installed it via Luna, but unfortunately I get an error if I want to change audio settings and nothing happens when I want to add a host.
Is there a log file anywhere I can give you?

Actually not sure about Luna. Could you try doing it manually, non-luna way?
I am still trying to understand the code structure of Luna.

If you are able to, provide kodi.log with debug logging enabled and click on controller settings, audio settings and add host. With that I can see what does and what doesn’t.

I can confirm… The luna script is the only thing not working. I’m not able to add the host. ‘Pairing failed… Try again’ loop. Using the same ip (known working setup) alongside the manual method works fine. It’d be good to have a working graphical interface though. I’ll see if I can help with that as having to connect via SSH each time is just so cumbersome imho.

Just noticed this in the logs… “Could not load advanced pairing manager. Reason: ImportError(‘No module named M2Crypto’,)”

I’m having a hard time getting m2crypto installed. I’m assuming that’ll solve the pairing screen dialog not showing for luna script’s interface. Even if I pair via SSH first… Luna script won’t make a connection using the pre-authenticated handshake. Any advice?

Tried installing m2crypto via PIP, since Coreelec doesn’t have OPKG installed by default and has purged useful repositories. Anyway I get this:

Defaulting to user installation because normal site-packages is not writeable
Collecting m2crypto
Using cached M2Crypto-0.36.0.tar.gz (1.1 MB)
ERROR: Command errored out with exit status 1:
command: /usr/bin/python -c ‘import sys, setuptools, tokenize; sys.argv[0] = ‘"’"’/storage/.opt/tmp/pip-install-0kZIu4/m2crypto/setup.py’"’"’; file=’"’"’/storage/.opt/tmp/pip-install-0kZIu4/m2crypto/setup.py’"’"’;f=getattr(tokenize, ‘"’"‘open’"’"’, open)(file);code=f.read().replace(’"’"’\r\n’"’"’, ‘"’"’\n’"’"’);f.close();exec(compile(code, file, ‘"’"‘exec’"’"’))’ egg_info --egg-base /storage/.opt/tmp/pip-pip-egg-info-ZXWqNj
cwd: /storage/.opt/tmp/pip-install-0kZIu4/m2crypto/
Complete output (8 lines):
running egg_info
creating /storage/.opt/tmp/pip-pip-egg-info-ZXWqNj/M2Crypto.egg-info
writing requirements to /storage/.opt/tmp/pip-pip-egg-info-ZXWqNj/M2Crypto.egg-info/requires.txt
writing /storage/.opt/tmp/pip-pip-egg-info-ZXWqNj/M2Crypto.egg-info/PKG-INFO
writing top-level names to /storage/.opt/tmp/pip-pip-egg-info-ZXWqNj/M2Crypto.egg-info/top_level.txt
writing dependency_links to /storage/.opt/tmp/pip-pip-egg-info-ZXWqNj/M2Crypto.egg-info/dependency_links.txt
writing manifest file ‘/storage/.opt/tmp/pip-pip-egg-info-ZXWqNj/M2Crypto.egg-info/SOURCES.txt’
error: [Errno 2] No such file or directory
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

Well, after a couple days of work… I managed to fixup an older version of luna script; the ‘Discovery Service’ breaks things on the newer iteration of Luna script - along with many other things. Anyway, I made a few UI inclusions to fit my need. Polished off some stuff, and fixed a few more things. Luna script is so outdated to the point it is broken on coreelec. Like the game image scraper stuff, override resolution settings, dependencies we can’t satisfy because no OPKG repo’s… Broken or not fit for use. So I removed them using alternatives where possible.


Amazing job @stickman89!

I am very sorry for not replying sooner, I am trying to focus on other things and I forgot to check out this forum. On my device I managed to fix Luna scripts but I did some hard-coding so that’s probably why its only working for me.

Please tell me what version did you use for Luna. In my opinion, this is a far more superior version of Luna as you can use whatever viewtype your skin supports. Maybe I could port this to Game section of Luna 18, and we could then ask CoreELEC team to add this as a package! When this happens, we wouldn’t have to use /storage/moonlight hardcoded and make the Luna launch scripts more cross-platform.

Thank you very much!

Hey @smg04

Thanks for the supportive words. I used Luna Script 1.6.3 as the base… Yeah I thought that too. The interface seems (imho) nicer than the newer versions, especially as it does support themes rather well.

Here’s some of the changes I made:

-Added my own solution for host pairing with ui prompts (requires ‘screen’… entware/opkg) - Pairing is accessible from the default location > Settings > Streaming > Pair host… This could be ‘an official package’ show stopper…

-Refined and optimized shell and python scripts to start stream after systemctl stop kodi. Otherwise you lose some input devices and performance is negatively affected.

-Added resume session support with prompts

-Added quit game support; with prompts when attempting to run a different game whilst a stream is still active…

-Added zerotier Switch… Starts zerotier on game launch and prior to stream if enabled (requires ‘zerotier-one’… entware/opkg)

-Disabled/hidden the broken online ‘Scraper’ stuff by default and prevented it from spamming Kodi logs… Geforce experience does that stuff for you anyway and is retrieved upon entering the ‘Games’ area.

-Reworked how the launch scripts obtains user defined app settings; since the untouched original didn’t pass those arguments properly, despite being user configurable.

-Hardcoded “-audio sysdefault” flag at launch scripts to fix no audio on stream.

-Remote Audio / Local Audio and NOPS flags prevent a stream from initiating. Instant close. Not sure what’s causing this. Binary related? Definitely not Luna Script’s fault. Removed for time being.

Probably a few more minor fixups; but I cannot for the life of me remember; wasn’t keeping track. Guilty as charged.

I’m just need (want) to alter a few resources then I’ll share it here. As mentioned above… The few additional software pre-requisites might be a show stopper for the ‘Official’ status? Perhaps we can figure something out.

stickman89

@smg04 - I’ve removed my dependency on ‘screen’ for moonlight in order to keep the core app operational without relying on a 3rd party binary to do so; aside from zerotier, ofcoarse (non-core). I opted to use ‘screen’ at the time as a substitute for openvt (not available to coreelec) for it’s ability to detach and run as a daemon; along with capturing session output at any given time through command. The latter being handy for obtaining moonlight’s generated pairing PIN (as it is displayed). I later discovered why the original pairing library was broken, and have fixed it. Then just rewrote things to get rid of ‘screen’ altogether. I ended up translating 99% of the shell related scripts that existed into python code/calls… Moonlight-heartbeat is the only one that made it.

I do need to report an anomaly relating to moonlight though…

Every now and then moonlight will lock up to black screen early on initiating a connection. Logs are cut short and moonlight hangs in an unresponsive state. Sometimes you can recover with (START + SELECT + LB + RB - if quick enough), and when you cannot, you are forced to reboot via SSH. Have you noticed this? ‘systemctl stop kodi’ doesn’t visibly start Coreelec’s interface back up either, and ‘pkill moonlight’ seems to do nothing to. SSH is still responsive though. Pretty frustrating.

Trying to gracefully handle it but it’s really difficult to catch and recover from in a clean manner.

I kind of get the feeling the problem is related to the audio interface failing to attach properly. Moonlight’s logs sometimes spew ALSA audio related errors over and over, and you can sometimes hear the audio crunching during the beginning of a stream.

Anyway. I’ll keep investigating, pehaps you can to. You might have already seen it. I’ll try and collect some better logs with debugging enabled.

Cheers.

stickman89

I did not notice moonlight failing to connect and not closing, I have noticed a black screen and alsa decoder errors and sound sometimes getting broken. I didn’t check out sound at all and I have no idea why it could fail.

You should always be able to kill moonlight and then restart kodi. This should be working. At worst you may have to set the disable_video flag to 0.

The only moment it really won’t work is if the display crashes and this is the problem echo 0 > /sys/class/video/disable_video.
Sometimes amvideo will just get disabled and this is most of the times shown in the logs. I have no idea when does this disable_video flag get set to 1. I think I noticed it when the decoder crashes. I still am not sure because the decoder API is not really well documented.

I have just updated the moonlight-embedded on GitHub.
I checked out a few codes and did an experiment and found out that when Kodi is stopped, disable_video flag will be set to 1. Because I think that flag is the only reason why video output wouldn’t work, I added a few lines that will set the flag to 0 before the stream is started. After the stream is stopped, the disable_video flag is set back to 1, but I might instead set it to the state it was in before moonlight was started. I also fixed a memory leak with the frame buffer. The memory leak was small but its better to be safe.

I have tested out and played a game on 1080p60 and I didn’t notice any black screens. I did notice short stutters and I am not sure what causes them. Most likely its the decoder that can’t keep up and I am not sure how I can stop that from happening.

On Dropbox you will find the compiled version of this binary that will patch disable_video flag and memory leak. Just extract it to /storage/moonlight and the rest stays the same. Continue runinng moonlight in debug mode. In case you get the codec to reset or black screen, please send logs so we can see if anything can be done about that.

I would really like to see what you did with Luna. This latest version I am running seems to be broken most of the times even though I tried to patch it. For now I keep running it from the console and everything seems to work fine. Recommended way to “pause” and “continue” Kodi while you start a game from Luna is to use: killall -STOP kodi.bin and killall -CONT kodi.bin. These seem to work just fine, but important notice is that Kodi must release the AMLCodec so the playback has to be stopped to get moonlight to work. If Kodi plays anything in the background it is probably utilizing AMLCodec for decoding and then moonlight cannot use it to decode the stream.

1 Like

About | FAQ | Terms of Service | Privacy Policy | Legal Notice