CypherpunkArmory / UserLAnd

Main UserLAnd Repository
Other
3.55k stars 392 forks source link

Could I play sound files with mplayer or mpv? #135

Closed bokuno closed 6 years ago

bokuno commented 6 years ago

In Termux, it is able to play sound files but in Userland I found it was not. My phone is non-rooted. I think it should be able as termux does, could anyone tell me how to play sound files in userland?

AdamMatthewLTM commented 6 years ago

@bokuno ,

Thank you for your post and my apologies for the delay. Could you please repaste this issue regarding sound using our template so I can better assist you? Different test cases may require a certain environment to replicate so we need all the info we can get. Thank you so much, -Adam

Template: Describe the bug

A clear and concise description of what the bug is.

To Reproduce

Steps to reproduce the behavior:

Go to '...' Click on '....' Scroll down to '....' See error Expected behavior

A clear and concise description of what you expected to happen.

Screenshots

If applicable, add screenshots to help explain your problem.

Device Information

Device: [e.g. Pixel XL 2] Android Version: [e.g. Oreo, 8.0] UserLAnd Version [e.g. 0.1.2] Additional context

Add any other context about the problem here.

bokuno commented 6 years ago

@AdamMatthewLTM Thanks for your reply. Here I paste what is needed.

** Describe the bug

mplayer could not play sound files.

** To Reproduce

Steps to reproduce the behavior:

I expect it to play the sound file `music.mp3'.

** Screenshots See the output below.

** Device Information

Device: [Oneplus 3T] Android Version: [Oreo, 8.0] UserLAnd Version: [0.2.3] Userland OS: Ubuntu Device rooted: No

** Add any other context about the problem here. This is the output what `mplayer music.mp3' gave.

MPlayer 1.3.0 (Debian), built with gcc-7 (C) 2000-2016 MPlayer Team do_connect: could not connect to socket connect: No such file or directory Failed to open LIRC support. You will not be able to use your remote control. Playing music.mp3. libavformat version 57.83.100 (external) Audio only file format detected. Clip info: Title: Artist: Album: Year: Comment:
Genre: Other Load subtitles in ./ Opening audio decoder: [mpg123] MPEG 1.0/2.0/2.5 layers I, II, III AUDIO: 44100 Hz, 2 ch, s16le, 192.0 kbit/13.61% (ratio: 24000->176400) Selected audio codec: [mpg123] afm: mpg123 (MPEG 1.0/2.0/2.5 layers I, II, III) AO: [pulse] Init failed: Connection refused Failed to initialize audio driver 'pulse' [AO_ALSA] alsa-lib: confmisc.c:767:(parse_card) cannot find card '0' [AO_ALSA] alsa-lib: conf.c:4528:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory [AO_ALSA] alsa-lib: confmisc.c:392:(snd_func_concat) error evaluating strings [AO_ALSA] alsa-lib: conf.c:4528:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory [AO_ALSA] alsa-lib: confmisc.c:1246:(snd_func_refer) error evaluating name [AO_ALSA] alsa-lib: conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory [AO_ALSA] alsa-lib: conf.c:5007:(snd_config_expand) Evaluate error: No such file or directory [AO_ALSA] alsa-lib: pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM default [AO_ALSA] Playback open error: No such file or directory Failed to initialize audio driver 'alsa' [AO SDL] Samplerate: 44100Hz Channels: Stereo Format s16le [AO SDL] using aalib audio driver. [AO SDL] Unable to open audio: No available audio device Failed to initialize audio driver 'sdl:aalib' Could not open/initialize audio device -> no sound. Audio: no sound Video: no video

Exiting... (End of file)

lapsio commented 6 years ago

yes you can. You can't use bare ALSA though as raw soundcard access requires root. You need to install pulseaudio and set it up to stream music over localhost. Then you need to download pulseaudio client for android like PulseDroid or SimpleProtocolPlayer from Play Store.

http://kaytat.com/?page_id=301 https://play.google.com/store/apps/details?id=com.kaytat.simpleprotocolplayer&hl=en

If you want to get this working asap here are my pulseaudio config files:

default.pa

#!/usr/bin/pulseaudio -nF
#
# This file is part of PulseAudio.
#
# PulseAudio is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PulseAudio is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.

# This startup script is used only if PulseAudio is started per-user
# (i.e. not in system mode)

.fail

### Automatically restore the volume of streams and devices
load-module module-device-restore
load-module module-stream-restore
load-module module-card-restore

### Automatically augment property information from .desktop files
### stored in /usr/share/application
load-module module-augment-properties

### Should be after module-*-restore but before module-*-detect
load-module module-switch-on-port-available

### Load audio drivers statically
### (it's probably better to not load these drivers manually, but instead
### use module-udev-detect -- see below -- for doing this automatically)
#load-module module-alsa-sink
#load-module module-alsa-source device=hw:1,0
#load-module module-oss device="/dev/dsp" sink_name=output source_name=input
#load-module module-oss-mmap device="/dev/dsp" sink_name=output source_name=input
#load-module module-null-sink
#load-module module-pipe-sink

### Automatically load driver modules depending on the hardware available
#.ifexists module-udev-detect.so
#load-module module-udev-detect
#.else
### Use the static hardware detection module (for systems that lack udev support)
#load-module module-detect
#.endif

### Automatically connect sink and source if JACK server is present
.ifexists module-jackdbus-detect.so
.nofail
load-module module-jackdbus-detect channels=2
.fail
.endif

### Automatically load driver modules for Bluetooth hardware
.ifexists module-bluetooth-policy.so
load-module module-bluetooth-policy
.endif

.ifexists module-bluetooth-discover.so
load-module module-bluetooth-discover
.endif

### Load several protocols
.ifexists module-esound-protocol-unix.so
load-module module-esound-protocol-unix
.endif
load-module module-native-protocol-unix

## Network access (may be configured with paprefs, so leave this commented
### here if you plan to use paprefs)
#load-module module-esound-protocol-tcp
load-module module-native-protocol-tcp
#load-module module-zeroconf-publish

### Load the RTP receiver module (also configured via paprefs, see above)
#load-module module-rtp-recv

### Load the RTP sender module (also configured via paprefs, see above)
#load-module module-null-sink sink_name=rtp format=s16be channels=2 rate=44100 sink_properties="device.description='RTP Multicast Sink'"
#load-module module-rtp-send source=rtp.monitor

### Load additional modules from GConf settings. This can be configured with the paprefs tool.
### Please keep in mind that the modules configured by paprefs might conflict with manually
### loaded modules.
.ifexists module-gconf.so
.nofail
load-module module-gconf
.fail
.endif

### Automatically restore the default sink/source when changed by the user
### during runtime
### NOTE: This should be loaded as early as possible so that subsequent modules
### that look up the default sink/source get the right value
load-module module-default-device-restore

### Automatically move streams to the default sink if the sink they are
### connected to dies, similar for sources
load-module module-rescue-streams

### Make sure we always have a sink around, even if it is a null sink.
load-module module-always-sink

### Honour intended role device property
load-module module-intended-roles

### Automatically suspend sinks/sources that become idle for too long
#load-module module-suspend-on-idle

### If autoexit on idle is enabled we want to make sure we only quit
### when no local session needs us anymore.
#.ifexists module-console-kit.so
#load-module module-console-kit
#.endif
.ifexists module-systemd-login.so
load-module module-systemd-login
.endif

### Enable positioned event sounds
load-module module-position-event-sounds

### Cork music/video streams when a phone stream is active
load-module module-role-cork

### Modules to allow autoloading of filters (such as echo cancellation)
### on demand. module-filter-heuristics tries to determine what filters
### make sense, and module-filter-apply does the heavy-lifting of
### loading modules and rerouting streams.
load-module module-filter-heuristics
load-module module-filter-apply

### Make some devices default
#set-default-sink output
#set-default-source input

And my .bashrc autorun script which adds monitor device (sorry, I'm using fish shell, deal with it):

autostart.sh

#!/usr/bin/fish

screen -wipe > /dev/null
if screen -list | grep pulse > /dev/null ; echo pulse already running ; 
else ; screen -dmS pulse bash -c 'while true ; do\
       dbus-launch pulseaudio -v --log-level=4 &\
       P=$!
       sleep 6;\
       export PULSE_SERVER=tcp:127.0.0.1:4713;\
       pactl load-module module-simple-protocol-tcp rate=48000 format=s16le channels=2 source=auto_null.monitor record=true port=8000 listen=127.0.0.1;\
       wait $P; done' ; end

Also, however it's not aboslutely necessary I strongly recommend to add following line to:

daemon.conf

exit-idle-time = -1

otherwise pulseaudio will terminate after any longer idle and will require restart of server. In practice it means that for example cmus will play every second track and every second track it will Fail to open audio device

Before you start any app that uses audio, you also must specify network pulseaudio server:

export PULSE_SERVER=tcp:127.0.0.1:4713
mplayer ./mysong.mp3

(you need to export this variable per shell session or add it to bashrc) Now you can just enter valid host:port in SimpleProtocolPlayer (127.0.0.1:8000 in this case) and click play button. It should work. All players that support pulseaudio should work with it. That includes mplayer, vlc, cmus and many others.

Have fun. If you're masochist, you can try to play movies using mplayer -vo caca -quiet. Works quite fine.

lapsio commented 6 years ago

To UserLAnd team I can recommend adding addon pulseaudio config files like for example OpenSUSE with its /etc/asound.pulse file which can be simply renamed to /etc/asound.conf in order to enable audio forward to pulse. We could have /etc/pulse/daemon.remote for example with some short note in comment on top.

bokuno commented 6 years ago

@lapsio Thank you for your reply. With your "default.pa" and the android app "Simple Protocol Player" , now I can play music files in UserLand. But some "audio frame skip(?)" sometimes occured. And "Simple Protocol Player" seems to drain some battery. I'll try to use with it in emacs+emms.

lapsio commented 6 years ago

@bokuno try to increase buffer size in simpleprotocolplayer. I use 200ms. More drastic approach - lower samplerate, it's believed to help.

AdamMatthewLTM commented 6 years ago

@lapsio ,

Thanks a lot for finding this workaround, we appreciate it. We'll definitely be working on the sound issues at one point, and I'll have them take the pulseaudio stuff into consideration.

@bokuno , please let us know if increasing the buffer size or lowering the sample rate has an effect on battery drain if you can.

Thanks guys for being on top of this

lapsio commented 6 years ago

@AdamMatthewLTM If you're interested in making PA easier to use with Android I'd recommend you either reaching out creator of XServer XSDL app or taking look at its code. It has embedded pulseaudio server so that playing audio with it is MUCH simpler - just single export PULSE_SERVER command. I think pulseaudio doesn't even need to be installed in target system. However it's not viable solution to many cases because XSDL stops playback when moved to background. So basically it's usable only if you use actual X or don't mind having screen to display XSDL all the time you want to listen to music

Easiest solution would be probably to ask XSDL creator to add option to keep audio playing when app is in background.

Better would be either to make standalone PA app as "addon" app for UserLAnd or to build it into UserLAnd.

Afaik all pulseaudio apps other than XSDL use just streams (so that PA server is handled by chroot/proot environment, not app itself)

I don't know about bokuno's case but I also had issue with audio stutter, increasing buffer mitigated issue.

CPU usage caused by this method is considerable. Please note that it's not just streaming audio to remote PA server (like with XSDL solution). It's in fact software emulation of playback (with dummy PA soundcard device) and software recording pcm output that is forwarded to it, then encapsulating in some protocol and finally sending. With audio resampling and mixing in the process. So in fact you somewhat play audio twice and record it at the same time and perform significant amount of audio processing on local PA side, as well as reverse transformations (eg. resampling) on Simple Protocol Player side. Everything is doubled. Battery drain sounds natural in such case

lapsio commented 6 years ago

I'd like to note that at some point this method stopped working. I'm not sure whether it's due to UserLAnd update or Android update but nevertheless it now crashes with

Assertion 'fstat(fd, &st) == 0' failed at pulsecore/socket-util.c:76, function pa_socket_peer_to_strong(). Aborting.

I didn't update system since last time it was working so It's external issue. After updating UserLAnd root packages problem still persists.

JanuszChmiel commented 6 years ago

Dear specialists, I AM aware, that it is vberycomplex process to enable userland support sound. Because also Windows subsystem for Linux do not support sound by default. There is also possibility try to provide modified Pulseaudio configuration files by default. It is caused by The fact, that Android uses Alsa server and special Android media module. The question is, if it is really possible to support sound from Userland at all. Since when sound will be enabled by default from console from shell mode, it will be The good start. Ideal situation would be, if you would enable user to download The tested app from Play Store which can play sound from PUlseaudio server. Like connect bot program can be simply installed from yours app. I have found out, that you are using qemu in user mode to execute ARM instructions. For now, there are two main issues related to Userland. System D-bus sockeds which can not be opened, and sound support.

JanuszChmiel commented 6 years ago

Can somebody of us confirm, that this method really do not work? Because I would like to setup userland environment by using this procedure. Yes, core developers of Userland do their best to improve whole project by working on The most complex programmers tasks of this projects, on Proot and other C++ code. And when I have read The most available detailed PROOT_LOG level, I feel deepr and deeper appreciation to their developments work. Many Hexadecimal numberss, many various Kernel service calls and procedures. Really this project is not fun for little child, it is very complex system programmers work andI Am very prout, that I can atleast test it and help a little, while testing, but I know, that my help is very very little.

JanuszChmiel commented 6 years ago

Dear Lapsio, Please, where to put autostart.sh In which folder when I run Debian in userland? So system will automatically load it? Is it necessary to place sudo service dbus start As a first line of autostart.sh so Pulseaudio will work?

JanuszChmiel commented 6 years ago

The most interesting fact about Pulseaudio support is information, if sound latency will be shorter, if we will use Pulsedroid or Simple protocol player. Very interesting is to compare those players with Xserver XSDL. If it will make some differences.

Valenoern commented 5 years ago

For anyone trying to get these scripts to work: they still do. Save autostart.sh anywhere with any name (install fish and screen first to use it exactly as it is) and call it in your .profile

# start of .profile file:
# bashrc, including ~/bin, etc

# replace with whatever filename you saved "autostart.sh"
~/bin/pulseaudio.sh

export PULSE_SERVER=tcp:127.0.0.1:4713

(I think) this should give you a pulseaudio when you open your vnc session, but if not, just go manually run autostart.sh again.

As long as you can see moving output bars in pavucontrol (if not maybe try restarting your linux session and/or rerunning autostart.sh), the pulse server is working and you can then stop/start simple protocol player (It might tell you it's "unable to stream" if there's no audio actually playing, in which case start or restart your audio application(s) and try again.)

I got this to work on:

JanuszChmiel commented 5 years ago

I have done my best to solve this issue. I have pointed Pulseaudio users and developers on this Github thread so they could help us. According to The crash message, problem must have something to do with sockets mechanism. Sockets are perfectly implemented to userland, because many apps based on The sockets communication work like a charm. But may be, that some specific socket transfer is not compatible with Proot and Pulseaudio new version, newer than The version, which have been available on 2.8.2018 assume it. Those deep problems are really for very experienced C programmers. The main problem is, that I AM afraid, that official Pulseaudio developers will have rather very low motivation to help. They are developing so complex sound server free of charge and they will very probably have always prefer to make modification of this sound server only for standard IBM/PC machines with PCI and with Udev support. I also understand, that Userland developers can not make their own Pulseaudio repository for all main streamed Linux distros. So for now, we have two possibility. 1. Use Xserver XSDL and its build in Pulseaudio. It support espeak "some string" without problems, no speech interruption. If users would use Emacspeak and if user will setup Espeak as speech engine, there will also not be crashes while using Xserver XSDL build in Pulseaudio sound server. The only problem exist when using Speech-dispatcher sound server. Speech is very interrupted. And sure. Nobody will solve this, because official Linux environment will be always supported. Making complex condition blocksinside C source codes which will determine from which environment this speech server run is rather redundant work. The next possibility is to have device with more than 2 GB Ram. Run Termux with Pulseaudio support and sending sound to this server with no need to even start Pulseaudio client from Userland based distribution. The question is. For how long will it be possible. Termux developers can modify Pulseaudio source code so it will start work. Xserver XSDL is being actively developed but making so complex project by a limited amount of developer is really not fancy task. So newest Android devices do not work with official Google Play release. But we have some possibility.

JanuszChmiel commented 5 years ago

I Am editing my bad statement. The next possibility is to have device with more than 2 GB Ram. Run Termux with Pulseaudio support and sending sound to this server with no need to even start Pulseaudio client from Userland based distribution. The question is. For how long will it be possible. Termux developers can modify Pulseaudio source code so it will stop work.

corbinlc commented 5 years ago

We are not in an unfriendly competition with the termux team. We share code at times (PRoot mainly). I don't think they would change their code just to break us. The right answer is we should probably incorporate their work at some point.

JanuszChmiel commented 5 years ago

Thank you very much for this very positive statement. I Am very curious. What did you had to to to incorporate so reliable shared memory support? Really it work. I have tested it for Debian Buster. Did you had to incorporate some .so library? Or somebody have recoded Proot to support that?

JanuszChmiel commented 5 years ago

default.txt I have found out, that TCP protocol and sockets are working like a charm when I have finaly applied advices from MR Lapsio successfully. I had to really install sox to prevent Pulseaudio C language crash at startup. So The problem is, that for some reasons, PUlsedroid and Simple protocol player do not play. Network communication exist. I hae read many messages. Mplayer virtually play .mp3 file, but really virtually. No crashes, but no sound when Simple protocol player or Pulsedroid listen for TCP packets. Very interesting. So those tools are only toyes or it would had to setup something else on The Debian. I Am sad, that script from Lapsio, autostart.sh contain screen word and newest Fish shell refuse to work with it. I have also nstalled fish shell. But I had The nearest possible situation. So may be, that my router internal firewall block some data? I could even try to copy all configuration files from Pulseaudio for Termux package and put those files to Debian system on The /etc/pulse. But I Am afraid, that it will also not play on Pulse droid or in Simple protocol player. I Am running Android 6.0. Simple protocol player uses standard Android API, audiotrack to play streamed content. When I press play button by using two of those players, Pulseaudio display info, that connection have been accepted. I have also added I Am sending you my dedefault.pa. It seems to Me, that Pulseaudio player for Android can not correctly decode audio stram.

Root-voidX commented 4 years ago

@lapsio

Following your method, but when i use pactl, it says connection refused? Pulseaudio starts a dummy but wont connect?