Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Unix Domain Socket

92 views
Skip to first unread message

Adam Jensen

unread,
Jul 27, 2018, 6:21:29 PM7/27/18
to
Hi,

I would like to control the mpv media player through its JSON IPC
interface [1] from a Tcl script.

[1]: <https://github.com/mpv-player/mpv/blob/master/DOCS/man/ipc.rst>

Does Tcl have convenient access to Unix Domain Sockets (AF_UNIX sockets)?

If not, could something like `socat` be finagled into service within a Tcl
script in such a way to enable interactive communication with mpv?

Any hints, pointers, examples, etc. are all very much appreciated!

Brad Lanam

unread,
Jul 27, 2018, 9:22:12 PM7/27/18
to
I wrote a tclmpv interface (using the library, not JSON).

https://wiki.tcl-lang.org/55174

I do not use the MPV interface in my production code as it
does not act in a consistent manner for MP3 and FLAC files.

For VLC:

https://wiki.tcl-lang.org/48382

Adam Jensen

unread,
Jul 27, 2018, 10:22:04 PM7/27/18
to
Hi, Brad. I found that page a few days ago. It's intriguing and I might
come back to it at some point but there isn't sufficient information
there to evaluate the interface and I don't want to go spelunking through
the mines of Mordor to figure out what it can do, how to compile it, and
how to make use of it.

Thanks for your effort but without set up instructions and a few typical
use-case examples its not very accessible. Does the Tcl wiki make it
difficult for people to provide and maintain this type of information?

Brad Lanam

unread,
Jul 27, 2018, 10:38:26 PM7/27/18
to
No, it's not difficult to update the wiki to include the information.
It's more a matter of my time, and how much interest other people have in
the particular page. Since I have put the MPV interface on hold until
their software is more mature, I don't have a lot of interest in that
particular wiki page.

I can supply you with the tclmpv.so shared library.

As for use:
load [pwd]/tclmpv.so
tclmpv init
tclmpv media myfile.mp3
while { [tclmpv state] != "stopped" } {
set ::x 0
after 1000 set ::x 1
vwait ::x
}
tclmpv close

The complete list of sub-commands that I implemented is:

audiodevlist
audiodevset
close
duration
gettime
init
haveaudiodevlist
isplay
media
pause
play
rate
seek
state
stop
version

Adam Jensen

unread,
Jul 28, 2018, 12:59:16 AM7/28/18
to
For posterity.

Using: <https://rg3.github.io/youtube-dl/>
youtube-dl -f 140 "https://youtu.be/WM-yJgTTntI" -o test.m4a

Get "tclmpv.c" from <https://wiki.tcl-lang.org/55174>

On Ubuntu 18.04, I had to change line 11
From: #include <tcl.h>
To: #include <tcl/tcl.h>

Compile:

gcc -c -fpic tclmpv.c -ltclstub -lmpv
gcc -shared -o tclmpv.so tclmpv.o -ltclstub -lmpv

In tclsh:

load ./tclmpv.so
tclmpv init
tclmpv media test.m4a
while { [tclmpv state] != "stopped" } {
set ::x 0
after 1000
set ::x 1
vwait ::x
}
tclmpv close

This plays the audio file. This setup will also play a video file in a
rudimentary way. It needs work but it might be a good place to start.

Thanks again, Brad!

Brad Lanam

unread,
Jul 28, 2018, 1:09:46 AM7/28/18
to
On Friday, July 27, 2018 at 9:59:16 PM UTC-7, Adam Jensen wrote:
> This plays the audio file. This setup will also play a video file in a
> rudimentary way. It needs work but it might be a good place to start.
>
> Thanks again, Brad!

Right, I only needed audio, so there are no video controls in there.
No playlist support either.

Brad Lanam

unread,
Jul 28, 2018, 9:52:38 AM7/28/18
to
And no, I don't use that vwait loop in my program.

# wait for it to start playing
# this loop assumes no error, it should also check
# for error, stopped, possibly idle states also
while { [tclmpv state] ne "playing" } {
set ::x 0
after 100 [list set ::x 1]
vwait ::x
}
set duration [tclmpv duration]
return $duration

# and now the main program can re-enter the event loop
# and check every now and then...

set timepos [tclmpv gettime]
if { $timepos >= $duration } {
# start the next song, whatever
}

tclmpv close
exit

Christian Gollwitzer

unread,
Jul 29, 2018, 2:11:10 PM7/29/18
to
Am 28.07.2018 um 06:59 schrieb Adam Jensen:

> Get "tclmpv.c" from <https://wiki.tcl-lang.org/55174>
>
> On Ubuntu 18.04, I had to change line 11
> From: #include <tcl.h>
> To: #include <tcl/tcl.h>
>
> Compile:
>
> gcc -c -fpic tclmpv.c -ltclstub -lmpv

I think this line is missing a "-DUSE_TCL_STUBS". It works by chance if
you leave it out (on Linux only), and you will end up with undefined
symbols in the .so, but it will not work as intended for stubs. i.e. you
can load the resulting .so into a different version of Tcl only if
compile with -DUSE_TCL_STUBS and link with -ltclstub. Oh, and for the
compile line, you can drop the -l libraries, they have no effect. OTOH
you could add -I/usr/include/tcl (or similar) so you don't need to patch
the #include line.

> gcc -shared -o tclmpv.so tclmpv.o -ltclstub -lmpv

Christian

Adam Jensen

unread,
Jul 29, 2018, 3:44:15 PM7/29/18
to
Nice. Thanks!

Does the compile command need "-DUSE_TCL_STUBS" even though the source
file specifies "#define USE_TCL_STUBS"? Using the original source from
<https://wiki.tcl-lang.org/55174> on Ubuntu 18.04 gives this result:

$ gcc -c -fpic -DUSE_TCL_STUBS -I/usr/include/tcl tclmpv.c

tclmpv.c:2:0: warning: "USE_TCL_STUBS" redefined
#define USE_TCL_STUBS

<command-line>:0:0: note: this is the location of the previous definition

$ gcc -shared -o tclmpv.so tclmpv.o -ltclstub -lmpv

After this, "load ./tclmpv.so" from within tclsh works as expected.

To be verbose, the alternative is to use the original source and then:

$ gcc -c -fpic -I/usr/include/tcl tclmpv.c
$ gcc -shared -o tclmpv.so tclmpv.o -ltclstub -lmpv
$ tclsh

load ./tclmpv.so
tclmpv init
tclmpv media test.m4a
after 15000 tclmpv close
after 16000 exit

Which also works except without the compiler warning about redefining
"USE_TCL_STUBS".

So for a multi-platform extension, what is the best practice? On my
Ubuntu machine, either the source can specify "#define USE_TCL_STUBS" or
the compile command can specify "-DUSE_TCL_STUBS" and the build will be
smooth, or USE_TCL_STUBS can be specified in both the source file and the
compile command and that will result in a warning but still produces a
seemingly functioning ".so" extension.

Christian Gollwitzer

unread,
Jul 30, 2018, 3:35:34 AM7/30/18
to
Am 29.07.18 um 21:44 schrieb Adam Jensen:
> On Sun, 29 Jul 2018 20:11:06 +0200, Christian Gollwitzer wrote:
>
>> Am 28.07.2018 um 06:59 schrieb Adam Jensen:
>>
>>> Get "tclmpv.c" from <https://wiki.tcl-lang.org/55174>
>>>
>>> On Ubuntu 18.04, I had to change line 11 From: #include <tcl.h>
>>> To: #include <tcl/tcl.h>
>>>
>>> Compile:
>>>
>>> gcc -c -fpic tclmpv.c -ltclstub -lmpv
>>
>> I think this line is missing a "-DUSE_TCL_STUBS". It works by chance if
>> you leave it out (on Linux only), and you will end up with undefined
>> symbols in the .so, but it will not work as intended for stubs. i.e. you
>> can load the resulting .so into a different version of Tcl only if
>> compile with -DUSE_TCL_STUBS and link with -ltclstub. Oh, and for the
>> compile line, you can drop the -l libraries, they have no effect. OTOH
>> you could add -I/usr/include/tcl (or similar) so you don't need to patch
>> the #include line.
>>
>>> gcc -shared -o tclmpv.so tclmpv.o -ltclstub -lmpv
>>
>> Christian
>
> Nice. Thanks!
>
> Does the compile command need "-DUSE_TCL_STUBS" even though the source
> file specifies "#define USE_TCL_STUBS"?

Ah, sorry - I haven't checked this. The -D switch at the command line
and the #define are doing the same thing. For some (mostly historical)
reasons, it is usually not #defined in the sources, but here you are
right. The warning rightly tells you that the stubs are already enabled.

> So for a multi-platform extension, what is the best practice? On my
> Ubuntu machine, either the source can specify "#define USE_TCL_STUBS" or
> the compile command can specify "-DUSE_TCL_STUBS" and the build will be
> smooth, or USE_TCL_STUBS can be specified in both the source file and the
> compile command and that will result in a warning but still produces a
> seemingly functioning ".so" extension.

The only real problem comes up when you don't define "USE_TCL_STUBS" and
link to libtcl8.6.so (which makes the extension loadable only into the
exact same version of Tcl), or worse, link to libtclstub8.6.a (which
creates an invalid file).

For multiplatform, the "correct" way is to package it up with tclconfig,
then you get an autoconf package which detects Tcl for you, lets you do
./configure && make install etc. Look here if you want to do this:

https://wiki.tcl-lang.org/5464

Since this is mostly mechanical work and Brad said that the extension
was not finished, he was probably just too lazy to do this.

Christian

Brad Lanam

unread,
Jul 30, 2018, 8:26:32 AM7/30/18
to
On Monday, July 30, 2018 at 12:35:34 AM UTC-7, Christian Gollwitzer wrote:
> The only real problem comes up when you don't define "USE_TCL_STUBS" and
> link to libtcl8.6.so (which makes the extension loadable only into the
> exact same version of Tcl), or worse, link to libtclstub8.6.a (which
> creates an invalid file).

All of my .so/.dylib/.dll are linked to libtclstub, and they all work.
What makes them invalid?

> For multiplatform, the "correct" way is to package it up with tclconfig,
> then you get an autoconf package which detects Tcl for you, lets you do
> ./configure && make install etc. Look here if you want to do this:
>
> https://wiki.tcl-lang.org/5464
>
> Since this is mostly mechanical work and Brad said that the extension
> was not finished, he was probably just too lazy to do this.

Yes, lazy. I also do not like autoconf.
I just have a makefile.

Brad Lanam

unread,
Jul 30, 2018, 8:32:17 AM7/30/18
to
On Monday, July 30, 2018 at 5:26:32 AM UTC-7, Brad Lanam wrote:
> On Monday, July 30, 2018 at 12:35:34 AM UTC-7, Christian Gollwitzer wrote:
> > The only real problem comes up when you don't define "USE_TCL_STUBS" and
> > link to libtcl8.6.so (which makes the extension loadable only into the
> > exact same version of Tcl), or worse, link to libtclstub8.6.a (which
> > creates an invalid file).
>
> All of my .so/.dylib/.dll are linked to libtclstub, and they all work.
> What makes them invalid?

Oh, never mind. You meant don't define and then link to libtclstub.
I misread that.

> > Since this is mostly mechanical work and Brad said that the extension
> > was not finished, he was probably just too lazy to do this.
>
> Yes, very lazy. I also do not like autoconf.
> I just have a makefile.

And it is generally finished. Works fine with .mp3 files. But libmpv does
not act the same way with .flac files, so that project is on the back
burner.

Adam Jensen

unread,
Jul 30, 2018, 1:48:17 PM7/30/18
to
On Mon, 30 Jul 2018 05:32:15 -0700, Brad Lanam wrote:

> And it is generally finished. Works fine with .mp3 files. But libmpv
> does not act the same way with .flac files, so that project is on the
> back burner.

Hi, Brad. This might be a goofy question but I see that the "tclmpv.c"
source file has a copyright notice:

/*
* Copyright 2018 Brad Lanam Walnut Creek CA US
*/

But there is no mention of a license. Is this Public Domain software?

Brad Lanam

unread,
Jul 30, 2018, 3:00:16 PM7/30/18
to
Oh, I usually use the zlib/png license.
Or the MIT license is fine.
Pick one.
0 new messages