{"action":"created","issue":{"url":"https://api.github.com/repos/termux/termux-packages/issues/821","repository_url":"https://api.github.com/repos/termux/termux-packages","labels_url":"https://api.github.com/repos/termux/termux-packages/issues/821/labels{/name}","comments_url":"https://api.github.com/repos/termux/termux-packages/issues/821/comments","events_url":"https://api.github.com/repos/termux/termux-packages/issues/821/events","html_url":"https://github.com/termux/termux-packages/issues/821","id":211992772,"number":821,"title":"Integrating ALSA and Pulseaudio into Termux","user":{"login":"ackalker","id":429567,"avatar_url":"https://avatars2.githubusercontent.com/u/429567?v=4","gravatar_id":"","url":"https://api.github.com/users/ackalker","html_url":"https://github.com/ackalker","followers_url":"https://api.github.com/users/ackalker/followers","following_url":"https://api.github.com/users/ackalker/following{/other_user}","gists_url":"https://api.github.com/users/ackalker/gists{/gist_id}","starred_url":"https://api.github.com/users/ackalker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ackalker/subscriptions","organizations_url":"https://api.github.com/users/ackalker/orgs","repos_url":"https://api.github.com/users/ackalker/repos","events_url":"https://api.github.com/users/ackalker/events{/privacy}","received_events_url":"https://api.github.com/users/ackalker/received_events","type":"User","site_admin":false},"labels":[{"id":223831284,"url":"https://api.github.com/repos/termux/termux-packages/labels/enhancement","name":"enhancement","color":"84b6eb","default":true}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":155,"created_at":"2017-03-05T23:03:30Z","updated_at":"2018-02-06T00:35:48Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"# Goals #\r\n\r\nThis issue is for tracking progress on integrating ALSA and Pulseaudio into Termux.\r\nOf course, ALSA in Termux will not have direct access to sound hardware on an Android device unless rooted and the vendor uses an ALSA driver in the kernel. The goal is to have a means to communicate with the OpenSL ES subsystem in Android as efficiently as possible.  Currently, this is done by streaming a Pulseaudio sink onto the localhost network. In future, it may be possible to write or adapt a Pulseaudio backend driver or an ALSA plugin to talk directly to OpenSL ES, using the so-called \"fast mixer\", with minimum latency when possible.\r\n\r\n# Overview of tasks and status #\r\n\r\n## Building and using the required packages ##\r\n\r\n- [x] `libpulseaudio` (#825)\r\n   - [x] `pulseaudio`\r\n   - [x] `libsndfile`\r\n   - [ ] `fftw` (useful?)\r\n   - [ ] `libspeex` (useful?)\r\n   - [ ] `libsoxr` (useful?)\r\n   - [ ] `liborc` (useful?)\r\n   - [ ] `libwebrtc-audio-processing` (useful?)\r\n- [ ] `alsa-lib` (#825, disabled: not working yet)\r\n- [ ] `alsa-plugins` (#825, disabled: not working yet)\r\n- [ ] `alsa-utils` (#825, disabled: not working yet)\r\n\r\n## Writing OpenSL ES backend drivers and plugins ##\r\n\r\n- [ ] for ALSA\r\n- [ ] for PulseAudio\r\n\r\n## Enabling audio related packages to make use of ALSA and/or Pulseaudio ##\r\n\r\n- [x] `cmus` (#209)\r\n   - [x] `libmad` (in repository)\r\n   - [x] `libvorbis` (in repository)\r\n   - [x] `libflac` (in repository)\r\n   - [ ] `mp4v2` (needed?)\r\n   - [ ] ...\r\n- [ ] `mpv`\r\n- [ ] `mpd`\r\n- [ ] `espeak`\r\n- [ ] ...\r\n\r\n# Detailed description #\r\n\r\n## Building and using the required packages ##\r\n\r\nAll packages build without problems on my system. They're mostly in a minimal, PoC state, I'm open for discussion on what extra dependencies to include, any tweaks to make.\r\n\r\n`libpulseaudio` is usable with Pulseaudio enabled packages, even without `pulseaudio` installed:\r\n\r\n* Install the [XServer XSDL](https://play.google.com/store/apps/details?id=x.org.server&hl=en) app from Google Play\r\n* Make sure to add the following to `~/.bash_profile`:\r\n```sh\r\nexport PULSE_SERVER=tcp::4712\r\n```\r\n* Open the XServer XSDL app\r\n* In Termux, run your Pulseaudio enabled application as usual\r\n\r\nKnown issue: Sound gets muted when device screen turns off or XServer XSDL is not running in the foreground.\r\n\r\n`pulseaudio` is a really bare-bones, minimal affair, no `speex` (I doubt if it is worth the CPU resources on Android), no ORC (AFAICT it currently only supports an architecture with MMX/SSE instructions anyway). I've also disabled Neon optimization: the build generates assembly which the assembler seems not to be able to handle (it spews errors about unknown registers, `q0` among them). I symlinked the shared libraries into `$TERMUX_PREFIX/lib` to avoid having to write an ugly wrapper. If there are any conflicting filenames, I'd like to hear about them.\r\nTo get sound output from applications running in Termux, you can adapt and run the following script:\r\n\r\n`setup-pulseaudio.sh`:\r\n```sh\r\n#!/data/data/com.termux/files/usr/bin/sh\r\n\r\nTERMUX_PREFIX=~/../usr\r\n\r\n# Do not shutdown server when idle.\r\nsed -i $TERMUX_PREFIX/etc/pulse/daemon.conf \\\r\n    -e '/^; exit-idle-time =/{s/^; //;s/ 20/ -1/}'\r\n\r\n# When no sinks are available, Pulseaudio automatically adds a null sink named 'auto_null'.\r\n\r\n# Stream the 'auto_null' sink over TCP.\r\n# This can be played by Simple Protocol Player app on Google Play.\r\nsed -i $TERMUX_PREFIX/etc/pulse/default.pa \\\r\n    -e '$aload-module module-simple-protocol-tcp source=auto_null.monitor port=12345 record=true'\r\n\r\n# In addition, or alternatively,\r\n# add a null sink named 'rtp' and stream it over RTP.\r\n# This can be played by VLC, etc.\r\n# sed -i $TERMUX_PREFIX/etc/pulse/default.pa \\\r\n#     -e '/^#load-module module-null-sink sink_name=rtp /s/#//' \\\r\n#     -e '/^#load-module module-rtp-send source=rtp.monitor$/s/#//'\r\n```\r\n\r\nTo play sound from a TCP stream:\r\n\r\n* Install [Simple Protocol Player]() from Google Play\r\n* in Termux, run `pulseaudio --daemon`\r\n* Open the Simple Protocol Player app\r\n* Make sure that `IP Address` is set to `127.0.0.1` and `Audio Port` is set to `12345` (or whatever value was set in the `setup-pulseaudio.sh` script\r\n* Tap the Play button\r\n* In Termux, run your Pulseaudio enabled application as usual\r\n\r\nTo play sound from an RTP stream:\r\n* Install [VLC for Android](https://play.google.com/store/apps/details?id=org.videolan.vlc&hl=en) (or a similar app which can play back RTP streams) from Google Play\r\n* In Termux, run `pulseaudio --daemon`\r\n* Open VLC or similar media player app\r\n* Choose to play back an RTP stream (how to do this depends on the media player app) from host `127.0.0.1`, port `???` (TODO: Add simple way to discover RTP port number)\r\n* In Termux, run your Pulseaudio enabled application as usual\r\n\r\n`alsa-lib` is pretty complete, I've worked around a problem where the path `/etc/asound.conf` is hardcoded, which may conflict with system ALSA and isn't useful on non-rooted devices anyway.\r\n\r\n`alsa-utils`: Also pretty complete, not sure if we need all of the utilities. Fun tidbit: `alsamixer` runs really well in Termux terminal emulator, it's even possible to drag with a finger to change the volume, yay!\r\n\r\n`alsa-plugins`: Only built the Pulseaudio plugin. I'm not sure if we need the samplerate plugin because Pulseaudio and OpenSL ES can both resample audio.\r\n\r\n## Writing OpenSL ES backend drivers and plugins ##\r\n\r\n`pulseaudio` is one of the targets for a dedicated OpenSL ES driver, I haven't looked into that yet. There appears to be a [(reportedly buggy) backend](https://github.com/pelya/pulseaudio-android/commit/f5bebe88cb4101b842b44183c7eb46aa9405eb9f) available, maybe we can adapt that.\r\n\r\n`alsa-plugins` is another target for more direct interaction with OpenSL ES, this time as a (pair of?) plugin(s), a virtual PCM and CTL device.\r\n\r\n## Enabling audio related packages to make use of ALSA and/or Pulseaudio ##\r\n\r\n`cmus` runs on Termux. Some of its optional dependencies (support for various file types) still need to be packaged.\r\n\r\n# Resources #\r\n\r\n## ALSA ##\r\n\r\n* [PCM External Plugin SDK](http://www.alsa-project.org/alsa-doc/alsa-lib/pcm_external_plugins.html)\r\n* [ALSA project - the C library reference: External I/O plugin SDK](http://www.alsa-project.org/alsa-doc/alsa-lib/group___p_c_m___i_o_plug.html)\r\n\r\n## Pulseaudio ##\r\n\r\n* [Modules](https://www.freedesktop.org/wiki/Software/PulseAudio/Documentation/User/Modules/)\r\n\r\n## Android ##\r\n\r\n* [High-Performance Audio](https://developer.android.com/ndk/guides/audio/index.html)\r\n* [High-Performance Audio (2017)](https://developer.android.com/ndk/guides/audio/index.html)\r\n* [`gkasten/high-performance-audio`](https://github.com/gkasten/high-performance-audio) (code used in Google IO 2014 talk)\r\n* [Audio Latency](https://developer.android.com/ndk/guides/audio/audio-latency.html)\r\n* [Measuring Audio Latency](https://source.android.com/devices/audio/latency_measure.html)\r\n* [SO answer](http://stackoverflow.com/a/16205370) about low-latency playback on Android (IMPORTANT!)\r\n* [`slesTestFeedback.cpp`](https://android.googlesource.com/platform/frameworks/wilhelm/+/master/tests/examples/slesTestFeedback.cpp) (FIFO implementation in `audio-utils` needs porting, uses system APIs which are not exposed by the NDK).\r\n* [`audio-echo`](https://github.com/googlesamples/android-ndk/tree/master/audio-echo)\r\n* [`native-audio`](https://github.com/googlesamples/android-ndk/tree/master/native-audio)\r\n* [Android audio streaming with OpenSL ES and the NDK](https://audioprograming.wordpress.com/2012/03/03/android-audio-streaming-with-opensl-es-and-the-ndk/)\r\n* [Lock-free audio IO with OpenSL ES on Android](https://audioprograming.wordpress.com/2012/10/29/lock-free-audio-io-with-opensl-es-on-android/)\r\n\r\n## Third-party libraries ##\r\n\r\n* [`cubeb`](https://github.com/kinetiknz/cubeb)  ([`cubeb/cubeb_opensl.c`](https://github.com/kinetiknz/cubeb/blob/master/src/cubeb_opensl.c))\r\n* [`openal-soft`](http://kcat.strangesoft.net/openal.html) ([`Alc/backends/opensl.c`](http://repo.or.cz/openal-soft.git/blob/HEAD:/Alc/backends/opensl.c))\r\n* [`opensl_stream`](https://github.com/nettoyeurny/opensl_stream) ([`opensl_stream/opensl_stream.c`](https://github.com/nettoyeurny/opensl_stream/blob/master/opensl_stream.c))\r\n"},"comment":{"url":"https://api.github.com/repos/termux/termux-packages/issues/comments/363269559","html_url":"https://github.com/termux/termux-packages/issues/821#issuecomment-363269559","issue_url":"https://api.github.com/repos/termux/termux-packages/issues/821","id":363269559,"user":{"login":"its-pointless","id":16748215,"avatar_url":"https://avatars0.githubusercontent.com/u/16748215?v=4","gravatar_id":"","url":"https://api.github.com/users/its-pointless","html_url":"https://github.com/its-pointless","followers_url":"https://api.github.com/users/its-pointless/followers","following_url":"https://api.github.com/users/its-pointless/following{/other_user}","gists_url":"https://api.github.com/users/its-pointless/gists{/gist_id}","starred_url":"https://api.github.com/users/its-pointless/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/its-pointless/subscriptions","organizations_url":"https://api.github.com/users/its-pointless/orgs","repos_url":"https://api.github.com/users/its-pointless/repos","events_url":"https://api.github.com/users/its-pointless/events{/privacy}","received_events_url":"https://api.github.com/users/its-pointless/received_events","type":"User","site_admin":false},"created_at":"2018-02-06T00:35:48Z","updated_at":"2018-02-06T00:35:48Z","author_association":"CONTRIBUTOR","body":"X can work in termux its been done before i even got R gui working. It will be slow though using xsdl or vnc. Im very skeptical of getting opengl X apps working in a reasonable in android since opengl android drivers are very different to ones that work in x. What can be done though is to have opengl draw in an android app. You might be able to get opengl output of mpv on your device using a shared memory and integrating in termux with termux exposing opengl api.\r\nIntegrating that into a software output X doing kludge of some kind might kind of work but it will likely be terrible to actually use.  \r\n \r\nI can't really help since i do not do opengl. I just researched this a few years ago out of curiosity. "}}
{"actor":{"display_login":"its-pointless"}}