This file is indexed.

/usr/bin/remuco-mplayer is in remuco-mplayer 0.9.6-2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/python

# =============================================================================
#
#    Remuco - A remote control system for media players.
#    Copyright (C) 2006-2010 by the Remuco team, see AUTHORS.
#
#    This file is part of Remuco.
#
#    Remuco is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    Remuco 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 General Public License
#    along with Remuco.  If not, see <http://www.gnu.org/licenses/>.
#
# =============================================================================

# Some remote control related documentation for MPlayer is available at
# http://www.mplayerhq.hu/DOCS/tech/slave.txt
# To parse the status line, read
# http://www.mplayerhq.hu/DOCS/HTML/en/faq.html#id2734829

"""MPlayer adapter for Remuco, implemented as an executable script."""

import os
import os.path
import gobject
import re
import sys
import subprocess

import remuco
from remuco import log
from remuco.defs import *

FA_APPEND = remuco.ItemAction("Append")
FA_PLAY = remuco.ItemAction("Play")

FILE_ACTIONS = (FA_APPEND, FA_PLAY)

MODE_INDEPENDENT = 1 << 0
MODE_MASTER = 1 << 1

class MplayerAdapter(remuco.PlayerAdapter):

    def __init__(self, filelist=None):

        remuco.PlayerAdapter.__init__(self, "MPlayer",
                                      progress_known=True,
                                      file_actions=FILE_ACTIONS,
                                      mime_types=remuco.MIMETYPES_AV)

        self.__mode = MODE_INDEPENDENT

        if filelist is not None:

            self.filelist = filelist
            self.__mode = MODE_MASTER

        # TODO put this somewhere else
        if self.__mode is MODE_INDEPENDENT:
            self.cmdfifo = os.path.join(self.config.cache, "mplayer.cmdfifo")
            self.cmdfifo = self.config.getx("cmdfifo", self.cmdfifo)

            self.statusfifo = os.path.join(self.config.cache, "mplayer.statusfifo")
            self.statusfifo = self.config.getx("statusfifo", self.statusfifo)

        # file descriptors
        [self.fd_cmd_stream_in, self.fd_cmd_stream_out,
            self.fd_status_stream_in, self.fd_status_stream_out] = [None]*4

        # gobject's watches:
        self.watch_id = None # read from mplayer using statusfifo
        self.watch_death = None # wait for mplayer exit (MODE_MASTER)

        # compile regexes to be used when parsing the status string
        # in __inputevent()
        # TODO use a generic self.current_regex pointing to either one
        # of video_regex or audio_regex and update it whenever the current
        # file in the playlist changes.
        # NOTE there is a space char after every information
        # NOTE audio_regex contains a "pretty position" which looks like:
        #   ([[HH:]MM:]SS.d)
        #   i wonder how far does it go...
        self.video_regex = re.compile(
            r"""^A:\s*(?P<rawaudiopos>\d+\.\d)\s # audio position (seconds)
            V:\s*(?P<rawvideopos>\d+\.\d)\s # video position (seconds)
            A-V:\s*(?P<delay>-?\d+\.\d+)\s # video position (seconds)
            (?P<videoregex>) # identify this regex uniquely
            """, re.VERBOSE)
        self.audio_regex = re.compile(
            r"""^A:\s*(?P<rawaudiopos>\d+\.\d)\s # raw position (seconds)
            \((?P<praudiopos>(\d\d:)*\d\d\.\d)\)\s # pretty position
            of (?P<rawtotal>\d+\.\d)\s # total length in seconds
            \((?P<prtotal>(\d\d:)*\d\d\.\d)\)\s # pretty total
            (?P<audioregex>) # identifier
            """, re.VERBOSE)
        self.info_regex = re.compile(
            r"""^ANS_(?P<infovar>[A-Za-z_]+)=  # information name
            (?P<infovalue>('.*'|\d+\.\d+)) # if quoted, str. else, number
            (?P<inforegex>) # identifier
            """, re.VERBOSE)
        self.pause_regex = re.compile(r" *===+ +\w+ +===+")

        self.__set_default_info()

    def start(self):

        remuco.PlayerAdapter.start(self)

        self.__setup_streams()

        self.watch_id = gobject.io_add_watch(self.fd_status_stream_in,
            gobject.IO_IN | gobject.IO_ERR | gobject.IO_HUP,
            self.__inputevent)

        if self.__mode is MODE_MASTER:

            args = ['mplayer', '-slave', '-input', 'file=/dev/null']
            args += self.filelist

            self.__mplayer_pid = subprocess.Popen(args, bufsize=1,
                stdin=self.fd_cmd_stream_in, stdout=self.fd_status_stream_out).pid

            def cb(pid, condition, user_data=None): #local callback for watch_death
                self.manager.stop()

            self.watch_death = gobject.child_watch_add(self.__mplayer_pid, cb)

        log.debug("starting mplayer adapter")

    def stop(self):

        # FIXME probable bug: call manager.stop from here,
        # and the manager will call stop_pa again
        remuco.PlayerAdapter.stop(self)

        if self.fd_cmd_stream_in is not None:
            os.closerange(self.fd_cmd_stream_in, self.fd_status_stream_out)

        [self.fd_cmd_stream_in, self.fd_cmd_stream_out,
            self.fd_status_stream_in, self.fd_status_stream_out] = [None]*4

        if self.watch_id is not None:
            gobject.source_remove(self.watch_id)
            self.watch_id = None

        if self.watch_death is not None:
            gobject.source_remove(self.watch_death)
            self.watch_death = None

        log.debug("stopping mplayer adapter")


    def poll(self):

        # fetch important information from player
        # NOTE while this fixes most of our problems, it will always
        # clutter mplayer's output
        if self.__playback == PLAYBACK_PLAY:
            self.__command('get_time_length\n')
            self.__command('get_file_name\n')
            self.__command('get_property volume\n')

    # =========================================================================
    # control interface
    # =========================================================================

    def ctrl_toggle_playing(self):
        self.__command('pause\n')

    def ctrl_toggle_fullscreen(self):
        self.__command('vo_fullscreen\n')

    def ctrl_next(self):
        self.__command('pt_step 1\n')
        gobject.idle_add(self.poll)

    def ctrl_previous(self):
        self.__command('pt_step -1\n')
        gobject.idle_add(self.poll)

    def ctrl_volume(self, direction):
        self.__command('volume %d %d\n' % (direction, direction==0))
        gobject.idle_add(self.poll)

    def ctrl_seek(self, direction):
        self.__command('seek %s0\n' % direction)

    def ctrl_navigate(self, action):
        button = ['up', 'down', 'left', 'right', 'select', 'prev', 'menu']

        if action >= 0 and action < button.__len__():
            self.__command('dvdnav %s\n' % button[action])

    # =========================================================================
    # request interface
    # =========================================================================

    #def request_playlist(self, client):

    #    self.reply_playlist_request(client, ["1", "2"],
    #            ["Joe - Joe's Song", "Sue - Sue's Song"])

    # ...

    # =========================================================================
    # actions interface
    # =========================================================================

    def action_files(self, action_id, files, uris):

        if action_id == FA_APPEND.id:
            for file in files:
                self.__command('loadfile "%s" 1' % file)
        elif action_id == FA_PLAY.id:
            self.__command('loadfile "%s"' % files[0])
        else:
            log.error("** BUG ** unexpected action ID")

    # =========================================================================
    # internal methods
    # =========================================================================

    def __command(self, cmd):
        """Write a command to the MPlayer FIFO and handle errors."""

        try:
            os.write(self.fd_cmd_stream_out, cmd)
        except OSError, e:
            log.warning("FIFO to MPlayer broken (%s)", e)
            self.manager.stop()

    def __read_stream(self, fd):
        """Read from a file descriptor and handle errors."""

        try:
            str = os.read(fd, 200) #magic buflength number
            return str
        except OSError, e:
            log.warning("FIFO from MPlayer broken (%s)", e)
            self.manager.stop()

    def __inputevent(self, fd, condition):
        """Parse incoming line from the player"""


        if condition == gobject.IO_IN:
            # read from fd (fd is self.fd_status_stream_in)
            status_str = self.__read_stream(fd)

            if self.pause_regex.search(status_str):
                self.__playback = PLAYBACK_PAUSE
                self.update_playback(PLAYBACK_PAUSE)
                return True

            matchobj = (self.video_regex.match(status_str) or
                        self.audio_regex.match(status_str) or
                        self.info_regex.match(status_str))

            if matchobj is not None:
                self.__playback = PLAYBACK_PLAY
                self.update_playback(self.__playback)

                current_progress = self.__progress
                current_length = self.info[INFO_LENGTH]

                identifier = matchobj.lastgroup
                if identifier == 'videoregex': # we have a video regex
                    current_progress = float(matchobj.group('rawvideopos'))

                elif identifier == 'inforegex': # info regex
                    self.__set_info(matchobj.group('infovar'),
                        matchobj.group('infovalue'))
                    current_length = self.info[INFO_LENGTH] #avoid a race condition

                elif identifier == 'audioregex': # audio regex
                    current_progress = float(matchobj.group('rawaudiopos'))
                    current_length = float(matchobj.group('rawtotal'))

                self.update_progress(current_progress, current_length)
                self.__progress = current_progress
                self.info[INFO_LENGTH] = current_length

            return True

        else:
            if condition == gobject.IO_HUP: # mplayer has probably quit
                log.info("statusfifo hangup. is mplayer still running?")
            else:
                log.error("statusfifo read error")

            self.stop()

            gobject.idle_add(self.start)
            return False

    def __set_info(self, name, value):
        """ Set global information of current item

        @param name:
            The name of the variable in mplayer. (str)
        @param value:
            The value mplayer assigns. (str)

        @see self.__inputevent() and self.info_regex for more information

        """
        if name == 'LENGTH':
            self.info[INFO_LENGTH] = float(value)
        elif name == 'FILENAME':
            self.info[INFO_TITLE] = value.strip("'")
            self.update_item(self.info[INFO_TITLE], self.info, None)
        elif name == 'volume': # lower case because we use the get_property command
            self.update_volume(float(value))

    def __set_default_info(self):
        """ Set default playback information """

        self.__progress = 0
        self.__playback = PLAYBACK_STOP
        self.info = { INFO_LENGTH: 100, INFO_TITLE: '' }

    def __setup_streams(self):
        """ Setup information streams.
            This depends on the mode we are running:
                MODE_INDEPENDENT - uses FIFOs to issue commands and fetch status
                information
                MODE_MASTER - uses a newly created pipe to connect to mplayer
                directly
        """


        if self.__mode is MODE_INDEPENDENT:
            if not(os.path.exists(self.cmdfifo)):
                os.mkfifo(self.cmdfifo)

            if not(os.path.exists(self.statusfifo)):
                os.mkfifo(self.statusfifo)
            else:
                log.warning('statusfifo file already exists. this is not a ' +
                'problem, but you might end up with a big file if ' +
                self.statusfifo + ' is not a FIFO.')

            log.debug("opening cmdfifo and waiting for mplayer to read it")
            self.fd_cmd_stream_out = os.open(self.cmdfifo, os.O_WRONLY)
            self.fd_cmd_stream_in = self.fd_cmd_stream_out

            log.debug("opening statusfifo")
            self.fd_status_stream_in = os.open(self.statusfifo, os.O_RDONLY | os.O_NONBLOCK)
            self.fd_status_stream_out = self.fd_status_stream_in

        else: # MODE_MASTER
            log.debug("creating pipes to communicate with mplayer")
            self.fd_cmd_stream_in, self.fd_cmd_stream_out  = os.pipe()
            self.fd_status_stream_in, self.fd_status_stream_out  = os.pipe()



# =============================================================================
# main
# =============================================================================

if __name__ == '__main__':

    filepaths = None

    # TODO use 'optparser' module instead
    if sys.argv.__len__() > 1:
        filepaths = []

        # TODO parse arguments looking for mplayer options

        #parse arguments looking for video files
        for arg in sys.argv[1:]:
            if os.path.isfile(arg): filepaths.append(arg)

    pa = MplayerAdapter(filepaths) # create the player adapter
    mg = remuco.Manager(pa) # pass it to a manager

    mg.run() # run the manager (blocks until interrupt signal)