Tiny Core Linux

General TC => Programming & Scripting - Unofficial => Topic started by: hiro on December 22, 2010, 07:35:58 AM

Title: [Tutorial] tinycore OSS audio streaming
Post by: hiro on December 22, 2010, 07:35:58 AM
I've had my music server running on TC for quite some time now with mpd+OSS.
Next I wanted to listen to some of George Carlin's rantings. But since opera or even flash is too much for my tiny server I'm running all these web applications on my main laptop, which has no sound.
So I use ossrecord, netcat and ossplay to stream all the profanities over my wireless network.

Here's the server's part, it shall ouput your audio to it's soundcard:
Code: [Select]
#!/bin/sh
# listen on port 1234 with netcat, pipe what it gets to ossplay's stdin.
nc -l -p 1234| ossplay -

On your client, the sound source, you have to make OSS create a loop device.
In /usr/lib/oss/conf/osscore.conf set this option:
Code: [Select]
vmix_loopdevs=1Then, in order to hear that newest craze of flash adverts at 100db (or whatever you want to listen to) run this, also on the client:
Code: [Select]
#!/bin/sh
sudo soundoff
sudo soundon
ossrecord -s48000 -b16 -c2 -l -d /dev/oss/oss_hdaudio0/loop0 - | nc 192.168.178.23 1234
/dev/oss/oss_hdaudio0/loop0 is your loop device. For some reason binding to /dev/dsp only allows you to record from line/mic-in. So you have to use the loop device, which gets the full signal directly from your mixer (vmix). The samplerate of my mixer is set to 48kHz, 16bits, and of course 2 channels.
Sometimes restarting OSS is necessary for the loop plugin to work, so I include this in the script.

For this to work on tc you only need OSS.tcz. Here's the whole chain in two lines:
Code: [Select]
annoying flash banner -> OSS (vmix0.pcm -> vmix0-out -> loop0) -> ossrecord -> nc ip port->
soundcard <- OSS(vmix0-out <- vmix0.pcm) <- ossplay <- nc -l port <-
Title: Re: tinycore OSS audio streaming
Post by: tinypoodle on December 23, 2010, 01:20:07 PM
Any chance there would be a way to achieve the same or at least similar with alsa?
It's exactly what I need and want since long, but alas, OSS doesn't like all soundcards which alsa does...   :-\
Title: Re: tinycore OSS audio streaming
Post by: gerald_clark on December 23, 2010, 03:09:20 PM
Have you tried using alsa-oss to provide OSS emulation under alsa?
Title: Re: tinycore OSS audio streaming
Post by: hiro on December 23, 2010, 03:48:48 PM
I abandoned alsa because I couldn't find any documentation about stuff like that. Although I have to admit that I also had to poke around quite a bit to find out about needing to restart OSS.

I've heard that that there is a named pipe output in alsa. I never managed to set it up, but if you do you can cat|nc from that.
Title: Re: tinycore OSS audio streaming
Post by: tinypoodle on December 30, 2010, 01:27:59 PM
First on your client, the sound source, you have to make OSS create a loop device.
In /usr/lib/oss/conf/osscore.conf set this option:
Code: [Select]
vmix_loopdevs=2

The more I try to understand what you did, the more confused I get...
"the sound source" would be the server, rather than the client, no?

And could you please clarify on which box you run the ossplay resp. ossrecord command, the box streamed from versus the box streamed to.

To me the way I understand it, it sounds somehow the wrong way round, but then I can't really understand what role the loop device plays.
Title: Re: tinycore OSS audio streaming
Post by: hiro on December 30, 2010, 01:55:26 PM
No. The loop device has nothing to do with the streaming/networking. I've reordered my instructions to make this clear.

Audio sink and TCP/IP Server: nc -l 1234 |ossplay -
Audio source and TCP/IP Client: ossrecord|nc server

Sounds great here. Don't hesitate to ask questions if you don't understand me. Nothing here has anything to do with pulseaudio or black magic. It's all just pipes and sockets and perhaps a bit of greek mother tongue.
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: tinypoodle on December 30, 2010, 02:19:33 PM
I read the edited first post again (and again and again), and I simply don't get
1. which command is used on which box
2. what exactly the role and function of the loop device is

Do I get it right or not that ossplay is used on the box to be streamed from, while ossrecord is used on the box to be streamed to and sound to be heard on?
If so, then that would somehow go against my theoretical logics; but what matters is what practically has proven to work...
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: hiro on December 30, 2010, 02:36:28 PM
Do I get it right or not that ossplay is used on the box to be streamed from, while ossrecord is used on the box to be streamed to and sound to be heard on?

Nope, it's the other way round. Did I oversee anything?
Perhaps I shouldn't be allowed to use EDIT. :-\
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: tinypoodle on December 30, 2010, 03:32:39 PM
Hmm, I had basically understood so even before your EDIT, I don't think that's at the core of my confusion, but obviously there is some miscommunication somewhere.

I'll try to describe how I understand whatever I do (correctly or not) and hope you could then confirm or correct me:
You refer to the server part which is followed by a command involving aplay.
Later you refer to the client, followed by a command involving arecord.

In my interpretation the server is the box where the sound originates from, e.g. a box where flashplayer would play in a browser well, but this box itself producing crappy or no sound.

The client would be the box where the sound output is desired to be redirected to, e.g. a box of generally lower specs but with a good soundcard and good speakers, producing desirable sound output.

Now in my personal simple linguistics and logics, lacking the full technical knowledge, I would imagine that the sound has to be recorded (e.g. ossrecord) on the box where e.g. the flashplayer is running, and to be played (e.g. ossplay) on the box where output of sound is desired. But the way I read all the instructions, it appears to me to be the other way round.

At least after your last editing I think to understand a tad more about the purpose of a loop device.

Sorry if I am persisting, but I actually had researched since long but casually about achieving this goal before you first posted about it, but then your post somehow inspired me to pursue this purpose with more intensity.
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: hiro on December 30, 2010, 03:57:57 PM
No, the server is what gets started when I run nc -l port.
You should read nc's manpage and perhaps http://en.wikipedia.org/wiki/Internet_socket#Socket_states_and_the_client-server_model

That's why I also brought in the source and sink model, so that you can see what is happening logically.

Your feeling was right. "Name ist Schall und Rauch." --Goethe
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: hiro on December 30, 2010, 04:04:02 PM
Sorry if I am persisting, but I actaually had researched since long but casually about achieving this goal before you first posted about it, but then your post somehow inspired me to pursue this purpose with more intensity.

I hope more people would ask such questions instead of creating monstrous bloat like pulseaudio.
So everyone feel free to ask...
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: tinypoodle on December 30, 2010, 05:46:08 PM
Thanks a lot for the wikipedia pointer, I think now to understand what your terminology is based on.

Remembering, first time I had read the manual of netcat several years ago, when some geek residing at the other end of the globe on IRC convinced me to participate in an experiment, netcat'ing a video file for me to watch in vlc.
Ever since, netcat had some touch of magical mystery to me, and I have occasionally read the manual again and googled for articles about.
Since you opened this thread, of course I have read the manual several times again.
My confusion resulted from the server/client terminology, which I had not taken notice before in relation to netcat.
What I would understand would be the "listen" and "connect" modes.
What I had understood that netcat in listen mode could amongst other obtain a stream.
Hearing about server/client, in my simple logics I associated "client" with the one "obtaining", e.g. running nc in listen mode.
Lacking of having achieved the very purpose discussed in this thread, I have used edna to at least being able to listen to mp3's on the box which has excellent sound but no persistent storage.
In that context, edna - running on the box where the mp3 files are stored - is definitely called a server and therefore I had the perception that the box where the sound originates from would be the server...

Semantics...  a bit of greek mothertongue and some Goethe quotes at times seem more human readable than technological terminology...

"Mit dem Wissen wächst der Zweifel" --Goethe
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: hiro on December 30, 2010, 06:36:59 PM
They should be teaching UNIX pipes and sockets in school. The concept is so easy, the possibilities nearly endless.

Often there are servers, which connect to other servers, which connect to file servers running on virtual servers, which then run on windows servers. Don't let it carry you away;)

edna's idea is also spot-on, and I think some day I might also try to do something like that (but without python). If only I had more time for actually building stuff...
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: hiro on December 30, 2010, 06:47:05 PM
What I had understood that netcat in listen mode could amongst other obtain a stream.
This is mixing up the terminology. A stream obviously has a direction. A socket (i.e. nc) not neccessarily.
Better just forget the word "stream" for the moment. It doesn't describe well what's going on technically.
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: tinypoodle on December 30, 2010, 08:12:18 PM
I might as well be one of your first customers, as I rarely would use python for any other purpose...

I was freely referring to the manual you had adviced me to read (and what I had absorbed and though t to understand about it)...
Quote
the listen mode can be used to obtain some kind of stream of data from a remote site
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: curaga on December 31, 2010, 02:02:18 AM
This is definitely a very nice hack. My first thought would have been to use NAS.
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: hiro on December 31, 2010, 04:17:23 AM
I was freely referring to the manual you had adviced me to read (and what I had absorbed and though t to understand about it)...
Quote
the listen mode can be used to obtain some kind of stream of data from a remote site
Sorry, I haven't read that man page before. ;)

Here are some relevant parts of the old UNIX man pages for it's tcp library. And I guess there are billions of books about this subject...
Code: [Select]
          tcp_connect(fd, lport, faddr, fport)
          tcp_listen(fd, lport, faddr, fport)

          TCP is one of the many protocols which are layered upon IP
          (internet protocol).  It provides full duplex byte stream
          connections between end points called sockets.  The address
          of a socket is composed of the internet address of its host
          and the port number to which the socket is bound.

          Tcp_connect binds socket fd to port lport and attempts to
          set up a connection to the socket bound to port fport on
          host faddr. Tcp_connect returns 0 if the connection is
          established, -1 otherwise.  Communications proceeds by per-
          forming read(2) and write(2) on fd.

          Tcp_listen binds socket fd to port lport and configures the
          socket to listen for connection requests to that port.  If
          faddr and fport are non-zero, only connections coming from
          sockets on machine faddr and bound to port fport are lis-
          tened for.  Tcp_listen returns 0 on success, -1 otherwise.
          Select(2) can be used with a listening socket to provide
          asynchronous polling of connection requests.
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: hiro on December 31, 2010, 04:30:48 AM
This is definitely a very nice hack. My first thought would have been to use NAS.

Quote from: http://radscan.com/nas/nas-README.txt
A X11 build environment (make, xmkmf, imake, X11/ headers, etc) is
required to compile NAS.
Quote from: http://radscan.com/nas/nas-README.txt
Just Say No!
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: curaga on December 31, 2010, 04:36:24 AM
There's a nas.tcz, no compiling required ;)
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: hiro on December 31, 2010, 05:07:24 AM
It depends on the X print library.
Come on, I don't even have a printer :P
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: tinypoodle on December 31, 2010, 08:26:29 PM
Here are some relevant parts of the old UNIX man pages for it's tcp library.

Any particular reason you use tcp for this specific task (as opposed to udp)?
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: tinypoodle on December 31, 2010, 08:35:37 PM
Heh, I was thinking exact same when I first ran into that dep.
On a closer look it is one lib of 20KB ;)
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: curaga on January 01, 2011, 01:18:06 AM
It's unnecessary as well :P

It's a part of the default link line of the Imakefiles, "just in case" the app also uses that. Using -Wl,-as-needed in ldflags usually gets rid of those, or they could also be removed from the generated Makefile. (they, because there are other unused libs linked in too).
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: hiro on January 01, 2011, 03:22:40 AM
Any particular reason you use tcp for this specific task (as opposed to udp)?
None at all. I leave this as an exercise for the reader :)
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: tinypoodle on January 20, 2011, 06:11:00 AM
Wondering if anyone has attempted to take this a step further, i.e. controlling player, volume and netcat of the target box from the source box - all I could think of would be running a ssh server on the target box and client on source box to achieve this.
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: hiro on January 20, 2011, 12:19:11 PM
You mean to have something even more lightweight than mpd?
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: tinypoodle on January 20, 2011, 11:35:51 PM
No idea about mpd.
What I mean is particularly to control alsamixer/ossmix and to reinitiate the netcat pipe - which would die if it had been cancelled on source machine side - on the target machine.
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: hiro on January 21, 2011, 06:37:29 AM
Yeah, well, you can either run the listening process on the target in a loop, like that: "while true; do soundserver.sh; done"

Or, if you have ssh with key certification enabled (copy the source machine's public key to the .ssh/authorized_keys file on the target.), include something like this in the source machine's script: "ssh other_command.sh".
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: tinypoodle on January 21, 2011, 09:36:04 AM
Not using ssh with key certification enabled, just dropbear sshd and dropbear client.
Title: Re: [Tutorial] tinycore OSS audio streaming
Post by: hiro on January 21, 2011, 11:11:42 AM
dropbear knows how to read the authorized_keys file, just cp your public key in there and you can automate ssh commands without typing your password