Beispiel #1
0
class PiPresents(object):
    def pipresents_version(self):
        vitems = self.pipresents_issue.split('.')
        if len(vitems) == 2:
            # cope with 2 digit version numbers before 1.3.2
            return 1000 * int(vitems[0]) + 100 * int(vitems[1])
        else:
            return 1000 * int(vitems[0]) + 100 * int(vitems[1]) + int(
                vitems[2])

    def __init__(self):
        # gc.set_debug(gc.DEBUG_UNCOLLECTABLE|gc.DEBUG_INSTANCES|gc.DEBUG_OBJECTS|gc.DEBUG_SAVEALL)
        gc.set_debug(gc.DEBUG_UNCOLLECTABLE | gc.DEBUG_SAVEALL)
        self.pipresents_issue = "1.4.6"
        self.pipresents_minorissue = '1.4.6c'

        StopWatch.global_enable = False

        # set up the handler for SIGTERM
        signal.signal(signal.SIGTERM, self.handle_sigterm)

        # ****************************************
        # Initialisation
        # ***************************************
        # get command line options
        self.options = command_options()
        # print (self.options)

        # get Pi Presents code directory
        pp_dir = sys.path[0]
        self.pp_dir = pp_dir

        if not os.path.exists(pp_dir + "/pipresents.py"):
            if self.options['manager'] is False:
                tkinter.messagebox.showwarning("Pi Presents",
                                               "Bad Application Directory")
            exit(102)

        # Initialise logging and tracing

        # initlize VLCDriver logger
        self.logger = Logger(enabled=True)
        self.logger.init()
        # TURN OFF REST OF LOGGING IN pp_vlcdriver.py

        # Init main monitor
        Monitor.log_path = pp_dir
        self.mon = Monitor()
        # Init in PiPresents only
        self.mon.init()

        # uncomment to enable control of logging from within a class
        # Monitor.enable_in_code = True # enables control of log level in the code for a class  - self.mon.set_log_level()

        # make a shorter list to log/trace only some classes without using enable_in_code.
        Monitor.classes = [
            'PiPresents', 'HyperlinkShow', 'RadioButtonShow', 'ArtLiveShow',
            'ArtMediaShow', 'MediaShow', 'LiveShow', 'MenuShow', 'GapShow',
            'Show', 'ArtShow', 'AudioPlayer', 'BrowserPlayer', 'ImagePlayer',
            'MenuPlayer', 'MessagePlayer', 'VideoPlayer', 'Player',
            'VLCPlayer', 'ChromePlayer', 'MediaList', 'LiveList', 'ShowList',
            'PathManager', 'ControlsManager', 'ShowManager',
            'TrackPluginManager', 'IOPluginManager', 'MplayerDriver',
            'OMXDriver', 'UZBLDriver', 'TimeOfDay', 'ScreenDriver', 'Animate',
            'OSCDriver', 'CounterManager', 'Network', 'Mailer'
        ]

        # Monitor.classes=['PiPresents','MediaShow','GapShow','Show','VideoPlayer','Player','OMXDriver']
        # Monitor.classes=['OSCDriver']

        # get global log level from command line
        Monitor.log_level = int(self.options['debug'])
        Monitor.manager = self.options['manager']
        # print self.options['manager']
        self.mon.newline(3)
        self.mon.sched(
            self, None,
            "Pi Presents is starting, Version:" + self.pipresents_minorissue +
            ' at ' + time.strftime("%Y-%m-%d %H:%M.%S"))
        self.mon.log(
            self,
            "Pi Presents is starting, Version:" + self.pipresents_minorissue +
            ' at ' + time.strftime("%Y-%m-%d %H:%M.%S"))
        # self.mon.log (self," OS and separator:" + os.name +'  ' + os.sep)
        self.mon.log(self, "sys.path[0] -  location of code: " + sys.path[0])

        # log versions of Raspbian and omxplayer, and GPU Memory
        with open("/boot/issue.txt") as ifile:
            self.mon.log(self, '\nRaspbian: ' + ifile.read())

        self.mon.log(
            self,
            '\n' + check_output(["omxplayer", "-v"], universal_newlines=True))
        self.mon.log(
            self,
            '\nGPU Memory: ' + check_output(["vcgencmd", "get_mem", "gpu"],
                                            universal_newlines=True))

        if os.geteuid() == 0:
            print('Do not run Pi Presents with sudo')
            self.mon.log(self, 'Do not run Pi Presents with sudo')
            self.mon.finish()
            sys.exit(102)

        if "DESKTOP_SESSION" not in os.environ:
            print('Pi Presents must be run from the Desktop')
            self.mon.log(self, 'Pi Presents must be run from the Desktop')
            self.mon.finish()
            sys.exit(102)
        else:
            self.mon.log(self, 'Desktop is ' + os.environ['DESKTOP_SESSION'])

        # optional other classes used
        self.root = None
        self.ppio = None
        self.tod = None
        self.dm = None
        self.animate = None
        self.ioplugin_manager = None
        self.oscdriver = None
        self.osc_enabled = False
        self.tod_enabled = False
        self.email_enabled = False

        user = os.getenv('USER')

        if user is None:
            tkinter.messagebox.showwarning(
                "You must be logged in to run Pi Presents")
            exit(102)

        if user != 'pi':
            self.mon.warn(self, "You must be logged as pi to use GPIO")

        self.mon.log(self, 'User is: ' + user)
        # self.mon.log(self,"os.getenv('HOME') -  user home directory (not used): " + os.getenv('HOME')) # does not work
        # self.mon.log(self,"os.path.expanduser('~') -  user home directory: " + os.path.expanduser('~'))   # does not work

        # check network is available
        self.network_connected = False
        self.network_details = False
        self.interface = ''
        self.ip = ''
        self.unit = ''

        # sets self.network_connected and self.network_details
        self.init_network()

        # start the mailer and send email when PP starts
        self.email_enabled = False
        if self.network_connected is True:
            self.init_mailer()
            if self.email_enabled is True and self.mailer.email_at_start is True:
                subject = '[Pi Presents] ' + self.unit + ': PP Started on ' + time.strftime(
                    "%Y-%m-%d %H:%M")
                message = time.strftime(
                    "%Y-%m-%d %H:%M"
                ) + '\nUnit: ' + self.unit + '   Profile: ' + self.options[
                    'profile'] + '\n ' + self.interface + '\n ' + self.ip
                self.send_email('start', subject, message)

        # get profile path from -p option
        if self.options['profile'] != '':
            self.pp_profile_path = "/pp_profiles/" + self.options['profile']
        else:
            self.mon.err(self, "Profile not specified in command ")
            self.end('error',
                     'Profile not specified with the commands -p option')

    # get directory containing pp_home from the command,
        if self.options['home'] == "":
            home = os.sep + 'home' + os.sep + user + os.sep + "pp_home"
        else:
            home = self.options['home'] + os.sep + "pp_home"
        self.mon.log(self, "pp_home directory is: " + home)

        # check if pp_home exists.
        # try for 10 seconds to allow usb stick to automount
        found = False
        for i in range(1, 10):
            self.mon.log(self,
                         "Trying pp_home at: " + home + " (" + str(i) + ')')
            if os.path.exists(home):
                found = True
                self.pp_home = home
                break
            time.sleep(1)
        if found is True:
            self.mon.log(
                self,
                "Found Requested Home Directory, using pp_home at: " + home)
        else:
            self.mon.err(self, "Failed to find pp_home directory at " + home)
            self.end('error', "Failed to find pp_home directory at " + home)

        # check profile exists
        self.pp_profile = self.pp_home + self.pp_profile_path
        if os.path.exists(self.pp_profile):
            self.mon.sched(self, None,
                           "Running profile: " + self.pp_profile_path)
            self.mon.log(
                self, "Found Requested profile - pp_profile directory is: " +
                self.pp_profile)
        else:
            self.mon.err(
                self, "Failed to find requested profile: " + self.pp_profile)
            self.end('error',
                     "Failed to find requested profile: " + self.pp_profile)

        self.mon.start_stats(self.options['profile'])

        # initialise and read the showlist in the profile
        self.showlist = ShowList()
        self.showlist_file = self.pp_profile + "/pp_showlist.json"
        if os.path.exists(self.showlist_file):
            self.showlist.open_json(self.showlist_file)
        else:
            self.mon.err(self, "showlist not found at " + self.showlist_file)
            self.end('error', "showlist not found at " + self.showlist_file)

        # check profile and Pi Presents issues are compatible
        if self.showlist.profile_version() != self.pipresents_version():
            self.mon.err(
                self,
                "Version of showlist " + self.showlist.profile_version_string +
                " is not  same as Pi Presents")
            self.end(
                'error',
                "Version of showlist " + self.showlist.profile_version_string +
                " is not  same as Pi Presents")

        # get the 'start' show from the showlist
        index = self.showlist.index_of_start_show()
        if index >= 0:
            self.showlist.select(index)
            self.starter_show = self.showlist.selected_show()
        else:
            self.mon.err(self, "Show [start] not found in showlist")
            self.end('error', "Show [start] not found in showlist")

# ********************
# SET UP THE GUI
# ********************
# turn off the screenblanking and saver
        if self.options['noblank'] is True:
            call(["xset", "s", "off"])
            call(["xset", "s", "-dpms"])

        # find connected displays and create a canvas for each display
        self.dm = DisplayManager()
        status, message, self.root = self.dm.init(self.options,
                                                  self.handle_user_abort,
                                                  self.pp_dir, False)
        if status != 'normal':
            self.mon.err(self, message)
            self.end('error', message)

        self.mon.log(
            self,
            str(DisplayManager.num_displays) + ' Displays are connected:')

        for display_id in DisplayManager.displays:
            if self.dm.has_canvas(display_id):
                canvas_obj = self.dm.canvas_widget(display_id)
                canvas_obj.config(bg=self.starter_show['background-colour'])
            name = self.dm.name_of_display(display_id)
            width, height = self.dm.real_display_dimensions(display_id)
            x, y = self.dm.real_display_position(display_id)
            matrix, ms = self.dm.touch_matrix_for(display_id)
            rotation = self.dm.real_display_orientation(display_id)
            self.mon.log(
                self, '   - ' + name + ' Id: ' + str(display_id) + ' ' +
                str(x) + '+' + str(y) + '+' + str(width) + '*' + str(height) +
                ' ' + rotation)
            self.mon.log(self, '     ' + ms)

            status, message, driver_name = self.dm.get_driver_name(display_id)
            if status == 'normal':
                self.mon.log(self,
                             name + ':  Touch Driver: ' + driver_name + '\n')
            elif status == 'null':
                self.mon.log(self, name + ':  Touch Driver not Defined\n')
            else:
                self.mon.err(self, message)

# ****************************************
# INITIALISE THE TOUCHSCREEN DRIVER
# ****************************************

# each driver takes a set of inputs, binds them to symboic names
# and sets up a callback which returns the symbolic name when an input event occurs

        self.sr = ScreenDriver()
        # read the screen click area config file
        reason, message = self.sr.read(pp_dir, self.pp_home, self.pp_profile)
        if reason == 'error':
            self.end('error', 'cannot find, or error in screen.cfg')

        # create click areas on the canvases, must be polygon as outline rectangles are not filled as far as find_closest goes
        reason, message = self.sr.make_click_areas(self.handle_input_event)
        if reason == 'error':
            self.mon.err(self, message)
            self.end('error', message)

# ****************************************
# INITIALISE THE APPLICATION AND START
# ****************************************
        self.shutdown_required = False
        self.reboot_required = False
        self.terminate_required = False
        self.exitpipresents_required = False
        self.restartpipresents_required = False

        #initialise the Audio manager
        self.audiomanager = AudioManager()
        status, message = self.audiomanager.init(self.pp_dir)
        if status == 'error':
            self.mon.err(self, message)
            self.end('error', message)

        # initialise the Beeps Player
        self.bp = BeepPlayer()
        self.bp.init(self.pp_home, self.pp_profile)

        # initialise the I/O plugins by importing their drivers
        self.ioplugin_manager = IOPluginManager()
        reason, message = self.ioplugin_manager.init(self.pp_dir,
                                                     self.pp_profile,
                                                     self.root,
                                                     self.handle_input_event,
                                                     self.pp_home)
        if reason == 'error':
            self.mon.err(self, message)
            self.end('error', message)

        # kick off animation sequencer
        self.animate = Animate()
        self.animate.init(pp_dir, self.pp_home, self.pp_profile, self.root,
                          200, self.handle_output_event)
        self.animate.poll()

        #create a showmanager ready for time of day scheduler and osc server
        show_id = -1
        self.show_manager = ShowManager(show_id, self.showlist,
                                        self.starter_show, self.root,
                                        self.pp_dir, self.pp_profile,
                                        self.pp_home)
        # first time through set callback to terminate Pi Presents if all shows have ended.
        self.show_manager.init(self.all_shows_ended_callback,
                               self.handle_command, self.showlist)
        # Register all the shows in the showlist
        reason, message = self.show_manager.register_shows()
        if reason == 'error':
            self.mon.err(self, message)
            self.end('error', message)

        # Init OSCDriver, read config and start OSC server
        self.osc_enabled = False
        if self.network_connected is True:
            if os.path.exists(self.pp_profile + os.sep + 'pp_io_config' +
                              os.sep + 'osc.cfg'):
                self.oscdriver = OSCDriver()
                reason, message = self.oscdriver.init(
                    self.pp_profile, self.unit, self.interface, self.ip,
                    self.handle_command, self.handle_input_event,
                    self.e_osc_handle_animate)
                if reason == 'error':
                    self.mon.err(self, message)
                    self.end('error', message)
                else:
                    self.osc_enabled = True
                    self.root.after(1000, self.oscdriver.start_server())

        # initialise ToD scheduler calculating schedule for today
        self.tod = TimeOfDay()
        reason, message, self.tod_enabled = self.tod.init(
            pp_dir, self.pp_home, self.pp_profile, self.showlist, self.root,
            self.handle_command)
        if reason == 'error':
            self.mon.err(self, message)
            self.end('error', message)

        # warn if the network not available when ToD required
        if self.tod_enabled is True and self.network_connected is False:
            self.mon.warn(
                self,
                'Network not connected  so Time of Day scheduler may be using the internal clock'
            )

        # init the counter manager
        self.counter_manager = CounterManager()
        if self.starter_show['counters-store'] == 'yes':
            store_enable = True
        else:
            store_enable = False
        reason, message = self.counter_manager.init(
            self.pp_profile + '/counters.cfg', store_enable,
            self.options['loadcounters'],
            self.starter_show['counters-initial'])
        if reason == 'error':
            self.mon.err(self, message)
            self.end('error', message)

        # warn about start shows and scheduler

        if self.starter_show['start-show'] == '' and self.tod_enabled is False:
            self.mon.sched(
                self, None,
                "No Start Shows in Start Show and no shows scheduled")
            self.mon.warn(
                self, "No Start Shows in Start Show and no shows scheduled")

        if self.starter_show['start-show'] != '' and self.tod_enabled is True:
            self.mon.sched(
                self, None,
                "Start Shows in Start Show and shows scheduled - conflict?")
            self.mon.warn(
                self,
                "Start Shows in Start Show and shows scheduled - conflict?")

        # run the start shows
        self.run_start_shows()

        # kick off the time of day scheduler which may run additional shows
        if self.tod_enabled is True:
            self.tod.poll()

        # start the I/O plugins input event generation
        self.ioplugin_manager.start()

        # start Tkinters event loop
        self.root.mainloop()

# *********************
#  RUN START SHOWS
# ********************

    def run_start_shows(self):
        self.mon.trace(self, 'run start shows')
        # parse the start shows field and start the initial shows
        show_refs = self.starter_show['start-show'].split()
        for show_ref in show_refs:
            reason, message = self.show_manager.control_a_show(
                show_ref, 'open')
            if reason == 'error':
                self.mon.err(self, message)
                self.terminate()
                #self.end(reason,message)

# *********************
# User inputs
# ********************

    def e_osc_handle_animate(self, line):
        #jump  out of server thread
        self.root.after(1, lambda arg=line: self.osc_handle_animate(arg))

    def osc_handle_animate(self, line):
        self.mon.log(self, "animate command received: " + line)
        #osc sends output events as a string
        reason, message, delay, name, param_type, param_values = self.animate.parse_animate_fields(
            line)
        if reason == 'error':
            self.mon.err(self, message)
            self.end(reason, message)
        self.handle_output_event(name, param_type, param_values, 0)

    def show_control_handle_animate(self, line):
        self.mon.log(self, "animate show control command received: " + line)
        line = '0 ' + line
        reason, message, delay, name, param_type, param_values = self.animate.parse_animate_fields(
            line)
        # print (reason,message,delay,name,param_type,param_values)
        if reason == 'error':
            self.mon.err(self, message)
            self.end(reason, message)
        self.handle_output_event(name, param_type, param_values, 0)

    # output events are animate commands
    def handle_output_event(self, symbol, param_type, param_values, req_time):
        reason, message = self.ioplugin_manager.handle_output_event(
            symbol, param_type, param_values, req_time)
        if reason == 'error':
            self.mon.err(self, message)
            self.end(reason, message)

    # all input events call this callback providing a symbolic name.
    # handle events that affect PP overall, otherwise pass to all active shows
    def handle_input_event(self, symbol, source):
        self.mon.log(self, "event received: " + symbol + ' from ' + source)
        if symbol == 'pp-terminate':
            self.handle_user_abort()

        elif symbol == 'pp-shutdown':
            self.mon.err(
                self,
                'pp-shutdown removed in version 1.3.3a, see Release Notes')
            self.end(
                'error',
                'pp-shutdown removed in version 1.3.3a, see Release Notes')

        elif symbol == 'pp-shutdownnow':
            # need root.after to get out of st thread
            self.root.after(1, self.shutdownnow_pressed)
            return

        elif symbol == 'pp-exitpipresents':
            self.exitpipresents_required = True
            if self.show_manager.all_shows_exited() is True:
                # need root.after to grt out of st thread
                self.root.after(1, self.e_all_shows_ended_callback)
                return
            reason, message = self.show_manager.exit_all_shows()

        elif symbol == 'pp-restartpipresents':
            self.restartpipresents_required = True
            if self.show_manager.all_shows_exited() is True:
                # need root.after to grt out of st thread
                self.root.after(1, self.e_all_shows_ended_callback)
                return
            reason, message = self.show_manager.exit_all_shows()

        else:
            # pass the input event to all registered shows
            for show in self.show_manager.shows:
                show_obj = show[ShowManager.SHOW_OBJ]
                if show_obj is not None:
                    show_obj.handle_input_event(symbol)

    # commands are generated by tracks and shows
    # they can open or close shows, generate input events and do special tasks
    # commands also generate osc outputs to other computers
    # handles one command provided as a line of text

    def handle_command(self, command_text, source='', show=''):
        # print 'PIPRESENTS ',command_text,'\n   Source',source,'from',show
        self.mon.log(self, "command received: " + command_text)
        if command_text.strip() == "":
            return

        fields = command_text.split()

        if fields[0] in ('osc', 'OSC'):
            if self.osc_enabled is True:
                status, message = self.oscdriver.parse_osc_command(fields[1:])
                if status == 'warn':
                    self.mon.warn(self, message)
                if status == 'error':
                    self.mon.err(self, message)
                    self.end('error', message)
                return
            else:
                return

        if fields[0] == 'counter':
            status, message = self.counter_manager.parse_counter_command(
                fields[1:])
            if status == 'error':
                self.mon.err(self, message)
                self.end('error', message)
            return

        if fields[0] == 'beep':
            status, message = self.bp.play_show_beep(command_text)
            if status == 'error':
                self.mon.err(self, message)
                self.end('error', message)
                return
            return

        if fields[0] == 'backlight':
            # on, off, inc val, dec val, set val fade val duration
            status, message = self.dm.do_backlight_command(command_text)
            if status == 'error':
                self.mon.err(self, message)
                self.end('error', message)
                return
            return

        if fields[0] == 'monitor':
            status, message = self.dm.handle_monitor_command(fields[1:])
            if status == 'error':
                self.mon.err(self, message)
                self.end('error', message)
                return
            return

        if fields[0] == 'cec':
            status, message = self.handle_cec_command(fields[1:])
            if status == 'error':
                self.mon.err(self, message)
                self.end('error', message)
                return
            return

        if fields[0] == 'animate':
            self.show_control_handle_animate(' '.join(fields[1:]))
            return

        # show commands
        show_command = fields[0]
        if len(fields) > 1:
            show_ref = fields[1]
        else:
            show_ref = ''

        if show_command in ('open', 'close', 'closeall', 'openexclusive'):
            self.mon.sched(self, TimeOfDay.now,
                           command_text + ' received from show:' + show)
            if self.shutdown_required is False and self.terminate_required is False:
                reason, message = self.show_manager.control_a_show(
                    show_ref, show_command)
            else:
                return

        elif show_command == 'event':
            self.handle_input_event(show_ref, 'Show Control')
            return

        elif show_command == 'exitpipresents':
            self.exitpipresents_required = True
            if self.show_manager.all_shows_exited() is True:
                # need root.after to get out of st thread
                self.root.after(1, self.e_all_shows_ended_callback)
                return
            else:
                reason, message = self.show_manager.exit_all_shows()

        elif show_command == 'shutdownnow':
            # need root.after to get out of st thread
            self.root.after(1, self.shutdownnow_pressed)
            return

        elif show_command == 'reboot':
            # need root.after to get out of st thread
            self.root.after(1, self.reboot_pressed)
            return

        else:
            reason = 'error'
            message = 'command not recognised: ' + show_command

        if reason == 'error':
            self.mon.err(self, message)
            self.end('error', message)
            return
        return

    def handle_cec_command(self, args):
        if len(args) == 0:
            return 'error', 'no arguments for CEC command'

        if len(args) == 1:
            device = '0'
            if args[0] == 'scan':
                com = 'echo scan | cec-client -s -d 1'
                #print (com)
                os.system(com)
                return 'normal', ''

            if args[0] == 'as':
                com = 'echo as | cec-client -s -d 1'
                #print (com)
                os.system(com)
                return 'normal', ''

        if len(args) == 2:
            device = args[1]
            if not device.isdigit():
                return 'error', 'device is not a positive integer'

        if args[0] == 'on':
            com = 'echo "on ' + device + '" | cec-client -s -d 1'
            #print (com)
            os.system(com)
            return 'normal', ''
        elif args[0] == 'standby':
            com = 'echo "standby ' + device + '" | cec-client -s -d 1'
            #print (com)
            os.system(com)
            return 'normal', ''
        else:
            return 'error', 'Unknown CEC command: ' + args[0]

    # deal with differnt commands/input events

    def shutdownnow_pressed(self):
        self.shutdown_required = True
        if self.show_manager.all_shows_exited() is True:
            self.all_shows_ended_callback('normal', 'no shows running')
        else:
            # calls exit method of all shows, results in all_shows_closed_callback
            self.show_manager.exit_all_shows()

    def reboot_pressed(self):
        self.reboot_required = True
        if self.show_manager.all_shows_exited() is True:
            self.all_shows_ended_callback('normal', 'no shows running')
        else:
            # calls exit method of all shows, results in all_shows_closed_callback
            self.show_manager.exit_all_shows()

    def handle_sigterm(self, signum, fframe):
        self.mon.log(self, 'SIGTERM received - ' + str(signum))
        self.terminate()

    def handle_user_abort(self):
        self.mon.log(self, 'User abort received')
        self.terminate()

    def terminate(self):
        self.mon.log(self, "terminate received")
        self.terminate_required = True
        needs_termination = False
        for show in self.show_manager.shows:
            # print  show[ShowManager.SHOW_OBJ], show[ShowManager.SHOW_REF]
            if show[ShowManager.SHOW_OBJ] is not None:
                needs_termination = True
                self.mon.log(
                    self,
                    "Sent terminate to show " + show[ShowManager.SHOW_REF])
                # call shows terminate method
                # eventually the show will exit and after all shows have exited all_shows_callback will be executed.
                show[ShowManager.SHOW_OBJ].terminate()
        if needs_termination is False:
            self.end('killed', 'killed - no termination of shows required')

# ******************************
# Ending Pi Presents after all the showers and players are closed
# **************************

    def e_all_shows_ended_callback(self):
        self.all_shows_ended_callback('normal', 'no shows running')

    # callback from ShowManager when all shows have ended
    def all_shows_ended_callback(self, reason, message):
        for display_name in DisplayManager.display_map:
            status, message, display_id, canvas_obj = self.dm.id_of_canvas(
                display_name)
            if status != 'normal':
                continue
            canvas_obj.config(bg=self.starter_show['background-colour'])
        if reason in (
                'killed', 'error'
        ) or self.shutdown_required is True or self.exitpipresents_required is True or self.reboot_required is True or self.restartpipresents_required is True:
            self.end(reason, message)

    def end(self, reason, message):
        self.mon.log(self, "Pi Presents ending with reason: " + reason)
        if self.root is not None:
            self.root.destroy()
        self.tidy_up()

        if reason == 'killed':
            if self.email_enabled is True and self.mailer.email_on_terminate is True:
                subject = '[Pi Presents] ' + self.unit + ': PP Exited with reason: Terminated'
                message = time.strftime(
                    "%Y-%m-%d %H:%M"
                ) + '\n ' + self.unit + '\n ' + self.interface + '\n ' + self.ip
                self.send_email(reason, subject, message)
            self.mon.sched(self, None, "Pi Presents Terminated, au revoir\n")
            self.mon.log(self, "Pi Presents Terminated, au revoir")

            # close logging files
            self.mon.finish()
            #print('Uncollectable Garbage',gc.collect())
            # objgraph.show_backrefs(objgraph.by_type('Canvas'),filename='backrefs.png')
            sys.exit(101)

        elif reason == 'error':
            if self.email_enabled is True and self.mailer.email_on_error is True:
                subject = '[Pi Presents] ' + self.unit + ': PP Exited with reason: Error'
                message_text = 'Download log for error message\n' + time.strftime(
                    "%Y-%m-%d %H:%M"
                ) + '\n ' + self.unit + '\n ' + self.interface + '\n ' + self.ip
                self.send_email(reason, subject, message_text)
            self.mon.sched(self, None,
                           "Pi Presents closing because of error, sorry\n")
            self.mon.log(self, "Pi Presents closing because of error, sorry")

            # close logging files
            self.mon.finish()
            #print('uncollectable garbage',gc.collect())
            sys.exit(102)

        else:
            self.mon.sched(self, None, "Pi Presents  exiting normally, bye\n")
            self.mon.log(self, "Pi Presents  exiting normally, bye")

            # close logging files
            self.mon.finish()
            if self.restartpipresents_required is True:
                #print ('restart')
                return

            if self.reboot_required is True:
                # print 'REBOOT'
                call(['sudo', 'reboot'])
            if self.shutdown_required is True:
                # print 'SHUTDOWN'
                call(['sudo', 'shutdown', 'now', 'SHUTTING DOWN'])
            #print('uncollectable garbage',gc.collect())
            sys.exit(100)

    # tidy up all the peripheral bits of Pi Presents
    def tidy_up(self):
        self.mon.log(self, "Tidying Up")
        # backlight
        if self.dm != None:
            self.dm.terminate()
        # turn screen blanking back on
        if self.options['noblank'] is True:
            call(["xset", "s", "on"])
            call(["xset", "s", "+dpms"])

        # tidy up animation
        if self.animate is not None:
            self.animate.terminate()

        # tidy up i/o plugins
        if self.ioplugin_manager != None:
            self.ioplugin_manager.terminate()

        if self.osc_enabled is True:
            self.oscdriver.terminate()

        # tidy up time of day scheduler
        if self.tod_enabled is True:
            self.tod.terminate()


# *******************************
# Connecting to network and email
# *******************************

    def init_network(self):

        timeout = int(self.options['nonetwork'])
        if timeout == 0:
            self.network_connected = False
            self.unit = ''
            self.ip = ''
            self.interface = ''
            return

        self.network = Network()
        self.network_connected = False

        # try to connect to network
        self.mon.log(self,
                     'Waiting up to ' + str(timeout) + ' seconds for network')
        success = self.network.wait_for_network(timeout)
        if success is False:
            self.mon.warn(
                self, 'Failed to connect to network after ' + str(timeout) +
                ' seconds')
            # tkMessageBox.showwarning("Pi Presents","Failed to connect to network so using fake-hwclock")
            return

        self.network_connected = True
        self.mon.sched(
            self, None, 'Time after network check is ' +
            time.strftime("%Y-%m-%d %H:%M.%S"))
        self.mon.log(
            self, 'Time after network check is ' +
            time.strftime("%Y-%m-%d %H:%M.%S"))

        # Get web configuration
        self.network_details = False
        network_options_file_path = self.pp_dir + os.sep + 'pp_config' + os.sep + 'pp_web.cfg'
        if not os.path.exists(network_options_file_path):
            self.mon.warn(
                self, "pp_web.cfg not found at " + network_options_file_path)
            return
        self.mon.log(self, 'Found pp_web.cfg in ' + network_options_file_path)

        self.network.read_config(network_options_file_path)
        self.unit = self.network.unit

        # get interface and IP details of preferred interface
        self.interface, self.ip = self.network.get_preferred_ip()
        if self.interface == '':
            self.network_connected = False
            return
        self.network_details = True
        self.mon.log(
            self, 'Network details ' + self.unit + ' ' + self.interface + ' ' +
            self.ip)

    def init_mailer(self):

        self.email_enabled = False
        email_file_path = self.pp_dir + os.sep + 'pp_config' + os.sep + 'pp_email.cfg'
        if not os.path.exists(email_file_path):
            self.mon.log(self, 'pp_email.cfg not found at ' + email_file_path)
            return
        self.mon.log(self, 'Found pp_email.cfg at ' + email_file_path)
        self.mailer = Mailer()
        self.mailer.read_config(email_file_path)
        # all Ok so can enable email if config file allows it.
        if self.mailer.email_allowed is True:
            self.email_enabled = True
            self.mon.log(self, 'Email Enabled')

    def try_connect(self):
        tries = 1
        while True:
            success, error = self.mailer.connect()
            if success is True:
                return True
            else:
                self.mon.log(
                    self, 'Failed to connect to email SMTP server ' +
                    str(tries) + '\n ' + str(error))
                tries += 1
                if tries > 5:
                    self.mon.log(
                        self, 'Failed to connect to email SMTP server after ' +
                        str(tries))
                    return False

    def send_email(self, reason, subject, message):
        if self.try_connect() is False:
            return False
        else:
            success, error = self.mailer.send(subject, message)
            if success is False:
                self.mon.log(self, 'Failed to send email: ' + str(error))
                success, error = self.mailer.disconnect()
                if success is False:
                    self.mon.log(self,
                                 'Failed disconnect after send:' + str(error))
                return False
            else:
                self.mon.log(self, 'Sent email for ' + reason)
                success, error = self.mailer.disconnect()
                if success is False:
                    self.mon.log(
                        self,
                        'Failed disconnect from email server ' + str(error))
                return True
Beispiel #2
0
class VideoPlayer(Player):
    """
    plays a track using omxplayer
    _init_ iniitalises state and checks resources are available.
    use the returned instance reference in all other calls.
    At the end of the path (when closed) do not re-use, make instance= None and start again.
    States - 'initialised' when done successfully
    Initialisation is immediate so just returns with error code.
    """

    debug = False
    debug = True

    def __init__(self, show_id, showlist, root, canvas, show_params,
                 track_params, pp_dir, pp_home, pp_profile, end_callback,
                 command_callback):

        # initialise items common to all players
        Player.__init__(self, show_id, showlist, root, canvas, show_params,
                        track_params, pp_dir, pp_home, pp_profile,
                        end_callback, command_callback)
        # print ' !!!!!!!!!!!videoplayer init'
        self.mon.trace(self, '')
        self.video_start_timestamp = 0
        self.dm = DisplayManager()

        # get player parameters
        if self.track_params['omx-audio'] != "":
            self.omx_audio = self.track_params['omx-audio']
        else:
            self.omx_audio = self.show_params['omx-audio']
        if self.omx_audio != "": self.omx_audio = "-o " + self.omx_audio

        self.omx_max_volume_text = self.track_params['omx-max-volume']
        if self.omx_max_volume_text != "":
            self.omx_max_volume = int(self.omx_max_volume_text)
        else:
            self.omx_max_volume = 0

        if self.track_params['omx-volume'] != "":
            self.omx_volume = self.track_params['omx-volume']
        else:
            self.omx_volume = self.show_params['omx-volume']

        if self.omx_volume != "":
            self.omx_volume = int(self.omx_volume)
        else:
            self.omx_volume = 0

        self.omx_volume = min(self.omx_volume, self.omx_max_volume)

        if self.track_params['omx-window'] != '':
            self.omx_window = self.track_params['omx-window']
        else:
            self.omx_window = self.show_params['omx-window']

        if self.track_params['omx-other-options'] != '':
            self.omx_other_options = self.track_params['omx-other-options']
        else:
            self.omx_other_options = self.show_params['omx-other-options']

        if self.track_params['freeze-at-start'] != '':
            self.freeze_at_start = self.track_params['freeze-at-start']
        else:
            self.freeze_at_start = self.show_params['freeze-at-start']

        if self.track_params['freeze-at-end'] != '':
            freeze_at_end_text = self.track_params['freeze-at-end']
        else:
            freeze_at_end_text = self.show_params['freeze-at-end']

        if freeze_at_end_text == 'yes':
            self.freeze_at_end_required = True
        else:
            self.freeze_at_end_required = False

        if self.track_params['seamless-loop'] == 'yes':
            self.seamless_loop = ' --loop '
        else:
            self.seamless_loop = ''

        if self.track_params['pause-timeout'] != '':
            pause_timeout_text = self.track_params['pause-timeout']
        else:
            pause_timeout_text = self.show_params['pause-timeout']

        if pause_timeout_text.isdigit():
            self.pause_timeout = int(pause_timeout_text)
        else:
            self.pause_timeout = 0

        # initialise video playing state and signals
        self.quit_signal = False
        self.unload_signal = False
        self.play_state = 'initialised'
        self.frozen_at_end = False
        self.pause_timer = None

    # LOAD - creates and omxplayer instance, loads a track and then pause
    def load(self, track, loaded_callback, enable_menu):
        # instantiate arguments
        self.track = track
        self.loaded_callback = loaded_callback  #callback when loaded
        # print '!!!!!!!!!!! videoplayer load',self.track
        self.mon.log(
            self, "Load track received from show Id: " + str(self.show_id) +
            ' ' + self.track)
        self.mon.trace(self, '')

        # do common bits of  load
        Player.pre_load(self)

        # set up video display
        if self.track_params['display-name'] != "":
            video_display_name = self.track_params['display-name']
        else:
            video_display_name = self.show_canvas_display_name
        status, message, self.omx_display_id = self.dm.id_of_display(
            video_display_name)
        if status == 'error':
            self.mon.err(self, message)
            self.play_state = 'load-failed'
            if self.loaded_callback is not None:
                self.loaded_callback(
                    'error', 'Display not connected: ' + video_display_name)
                return

        # set up video window and calc orientation
        status, message, command, has_window, x1, y1, x2, y2 = self.parse_video_window(
            self.omx_window, self.omx_display_id)
        if status == 'error':
            self.mon.err(
                self,
                'omx window error: ' + message + ' in ' + self.omx_window)
            self.play_state = 'load-failed'
            if self.loaded_callback is not None:
                self.loaded_callback(
                    'error',
                    'omx window error: ' + message + ' in ' + self.omx_window)
                return
        else:
            if has_window is True:
                self.omx_window_processed = '--win " ' + str(x1) + ' ' + str(
                    y1) + ' ' + str(x2) + ' ' + str(y2) + ' " '
            else:
                self.omx_window_processed = self.omx_aspect_mode

        # load the plugin, this may modify self.track and enable the plugin drawign to canvas
        if self.track_params['plugin'] != '':
            status, message = self.load_plugin()
            if status == 'error':
                self.mon.err(self, message)
                self.play_state = 'load-failed'
                if self.loaded_callback is not None:
                    self.loaded_callback('error', message)
                    return

        # load the images and text
        status, message = self.load_x_content(enable_menu)
        if status == 'error':
            self.mon.err(self, message)
            self.play_state = 'load-failed'
            if self.loaded_callback is not None:
                self.loaded_callback('error', message)
                return

        if not (track[0:3] in ('udp', 'tcp')
                or track[0:4] in ('rtsp', 'rtmp')):

            if not os.path.exists(track):
                self.mon.err(self, "Track file not found: " + track)
                self.play_state = 'load-failed'
                if self.loaded_callback is not None:
                    self.loaded_callback('error',
                                         'track file not found: ' + track)
                    return

        self.omx = OMXDriver(self.canvas, self.pp_dir)
        self.start_state_machine_load(self.track)

    # SHOW - show a track
    def show(self, ready_callback, finished_callback, closed_callback):
        # print "!!!! videoplayer show"
        # instantiate arguments
        self.ready_callback = ready_callback  # callback when ready to show video
        self.finished_callback = finished_callback  # callback when finished showing
        self.closed_callback = closed_callback

        self.mon.trace(self, '')

        #  do animation at start etc.
        Player.pre_show(self)

        # start show state machine
        self.start_state_machine_show()
        self.video_start_timestamp = self.omx.video_start_timestamp
        global globalVideoTimestampStart
        globalVideoTimestampStart = self.video_start_timestamp
        self.mon.log(
            self, "Timestamp from show starting: " +
            str(self.omx.video_start_timestamp))

    # UNLOAD - abort a load when omplayer is loading or loaded
    def unload(self):
        self.mon.trace(self, '')

        self.mon.log(self,
                     ">unload received from show Id: " + str(self.show_id))
        self.start_state_machine_unload()

    # CLOSE - quits omxplayer from 'pause at end' state
    def close(self, closed_callback):
        self.mon.trace(self, '')
        self.mon.log(self,
                     ">close received from show Id: " + str(self.show_id))
        self.closed_callback = closed_callback
        self.start_state_machine_close()

    def input_pressed(self, symbol):
        if symbol[0:4] == 'omx-':
            if symbol[0:5] in ('omx-+', 'omx--', 'omx-='):
                if symbol[4] in ('+', '='):
                    self.inc_volume()
                else:
                    self.dec_volume()
            else:
                self.control(symbol[4])
        elif symbol == 'inc-volume':
            self.inc_volume()
        elif symbol == 'dec-volume':
            self.dec_volume()
        elif symbol == 'pause':
            self.pause()
        elif symbol == 'go':
            self.go()
        elif symbol == 'unmute':
            self.unmute()
        elif symbol == 'mute':
            self.mute()
        elif symbol == 'pause-on':
            self.pause_on()
        elif symbol == 'pause-off':
            self.pause_off()
        elif symbol == 'stop':
            self.stop()

    # respond to normal stop
    def stop(self):
        self.mon.log(self, ">stop received from show Id: " + str(self.show_id))
        # cancel the pause timer
        if self.pause_timer != None:
            self.canvas.after_cancel(self.pause_timer)
            self.pause_timer = None

        # send signal to freeze the track - causes either pause or quit depends on freeze at end
        if self.freeze_at_end_required is True:
            if self.play_state == 'showing' and self.frozen_at_end is False:
                self.frozen_at_end = True
                # pause the track
                self.omx.pause('freeze at end from user stop')
                self.quit_signal = True
                # and return to show so it can end  the track and the video in track ready callback
##                if self.finished_callback is not None:
##                    # print 'finished from stop'
##                    self.finished_callback('pause_at_end','pause at end')
            else:
                self.mon.log(self, "!<stop rejected")
        else:
            # freeze not required and its showing just stop the video
            if self.play_state == 'showing':
                self.quit_signal = True
            else:
                self.mon.log(self, "!<stop rejected")

    def inc_volume(self):
        self.mon.log(self,
                     ">inc-volume received from show Id: " + str(self.show_id))
        if self.play_state == 'showing' and self.frozen_at_end is False and self.omx.paused_at_start == 'done':
            self.omx.inc_volume()
            return True
        else:
            self.mon.log(self, "!<inc-volume rejected " + self.play_state)
            return False

    def dec_volume(self):
        self.mon.log(self,
                     ">dec-volume received from show Id: " + str(self.show_id))
        if self.play_state == 'showing' and self.frozen_at_end is False and self.omx.paused_at_start == 'done':
            self.omx.dec_volume()
            return True
        else:
            self.mon.log(self, "!<dec-volume rejected " + self.play_state)
            return False

    def mute(self):
        self.mon.log(self, ">mute received from show Id: " + str(self.show_id))
        if self.play_state == 'showing' and self.frozen_at_end is False and self.omx.paused_at_start == 'done':
            self.omx.mute()
            return True
        else:
            self.mon.log(self, "!<mute rejected " + self.play_state)
            return False

    def unmute(self):
        self.mon.log(self,
                     ">unmute received from show Id: " + str(self.show_id))
        if self.play_state == 'showing' and self.frozen_at_end is False and self.omx.paused_at_start == 'done':
            self.omx.unmute()
            return True
        else:
            self.mon.log(self, "!<unmute rejected " + self.play_state)
            return False

    # toggle pause
    def pause(self):
        self.mon.log(
            self, ">toggle pause received from show Id: " + str(self.show_id))
        if self.play_state == 'showing' and self.frozen_at_end is False and self.omx.paused_at_start == 'done':
            self.omx.toggle_pause('user')
            if self.omx.paused is True and self.pause_timeout > 0:
                # kick off the pause teimeout timer
                self.pause_timer = self.canvas.after(
                    self.pause_timeout * 1000, self.pause_timeout_callback)
            else:
                # cancel the pause timer
                if self.pause_timer != None:
                    self.canvas.after_cancel(self.pause_timer)
                    self.pause_timer = None
            return True
        else:
            self.mon.log(self, "!<pause rejected " + self.play_state)
            return False

    def pause_timeout_callback(self):
        self.pause_off()
        self.pause_timer = None

    # pause on
    def pause_on(self):
        self.mon.log(self,
                     ">pause on received from show Id: " + str(self.show_id))
        if self.play_state == 'showing' and self.frozen_at_end is False and self.omx.paused_at_start == 'done':
            self.omx.pause_on()
            if self.omx.paused is True and self.pause_timeout > 0:
                # kick off the pause teimeout timer
                self.pause_timer = self.canvas.after(
                    self.pause_timeout * 1000, self.pause_timeout_callback)
            return True
        else:
            self.mon.log(self, "!<pause on rejected " + self.play_state)
            return False

    # pause off
    def pause_off(self):
        self.mon.log(self,
                     ">pause off received from show Id: " + str(self.show_id))
        if self.play_state == 'showing' and self.frozen_at_end is False and self.omx.paused_at_start == 'done':
            self.omx.pause_off()
            if self.omx.paused is False:
                # cancel the pause timer
                if self.pause_timer != None:
                    self.canvas.after_cancel(self.pause_timer)
                    self.pause_timer = None
            return True
        else:
            self.mon.log(self, "!<pause off rejected " + self.play_state)
            return False

    # go after freeze at start
    def go(self):
        self.mon.log(self, ">go received from show Id: " + str(self.show_id))
        if self.play_state == 'showing' and self.omx.paused_at_start == 'True':
            self.omx.go()
            return True
        else:
            self.mon.log(self, "!<go rejected " + self.play_state)
            return False

    # other control when playing
    def control(self, char):
        if self.play_state == 'showing' and self.frozen_at_end is False and self.omx.paused_at_start == 'done' and char not in (
                'q'):
            self.mon.log(self, "> send control to omx: " + char)
            self.omx.control(char)
            return True
        else:
            self.mon.log(self, "!<control rejected")
            return False

# ***********************
# track showing state machine
# **********************

    """
    STATES OF STATE MACHINE
    Threre are ongoing states and states that are set just before callback

    >init - Create an instance of the class
    <On return - state = initialised   -  - init has been completed, do not generate errors here

    >load
        Fatal errors should be detected in load. If so  loaded_callback is called with 'load-failed'
         Ongoing - state=loading - load called, waiting for load to complete   
    < loaded_callback with status = normal
         state=loaded - load has completed and video paused before first frame      
    <loaded_callback with status=error
        state= load-failed - omxplayer process has been killed because of failure to load   

    On getting the loaded_callback with status=normal the track can be shown using show
    Duration obtained from track should always cause pause_at_end. if not please tell me as the fudge factor may need adjusting.


    >show
        show assumes a track has been loaded and is paused.
       Ongoing - state=showing - video is showing 
    <finished_callback with status = pause_at_end
            state=showing but frozen_at_end is True
    <closed_callback with status= normal
            state = closed - video has ended omxplayer has terminated.


    On getting finished_callback with status=pause_at end a new track can be shown and then use close to quit the video when new track is ready
    On getting closed_callback with status=  nice_day omxplayer closing should not be attempted as it is already closed
    Do not generate user errors in Show. Only generate system erros such as illegal state abd then use end()

    >close
       Ongoing state - closing - omxplayer processes are dying due to quit sent
    <closed_callback with status= normal - omxplayer is dead, can close the track instance.

    >unload
        Ongoing states - start_unload and unloading - omxplayer processes are dying due to quit sent.
        when unloading is complete state=unloaded
        I have not added a callback to unload. its easy to add one if you want.

    closed is needed because wait_for end in pp_show polls for closed and does not use closed_callback
    
    """
    def start_state_machine_load(self, track):
        self.track = track
        # initialise all the state machine variables
        self.loading_count = 0  # initialise loading timeout counter
        self.play_state = 'loading'

        # load the selected track
        # options= '  ' + self.omx_audio+ ' --vol -6000 ' + self.omx_window_processed + ' ' + self.seamless_loop + ' ' + self.omx_other_options +" "

        options = ' --display ' + str(
            self.omx_display_id
        ) + ' --no-osd ' + self.omx_audio + ' --vol -6000 ' + self.omx_window_processed + self.omx_rotate + ' ' + self.seamless_loop + ' ' + self.omx_other_options + " "
        self.omx.load(track, self.freeze_at_start, options,
                      self.mon.pretty_inst(self), self.omx_volume,
                      self.omx_max_volume)
        # self.mon.log (self,'Send load command track '+ self.track + 'with options ' + options + 'from show Id: '+ str(self.show_id))
        # print 'omx.load started ',self.track
        # and start polling for state changes
        self.tick_timer = self.canvas.after(50, self.load_state_machine)

    def start_state_machine_unload(self):
        # print ('videoplayer - starting unload',self.play_state)
        if self.play_state in ('closed', 'initialised', 'unloaded'):
            # omxplayer already closed
            self.play_state = 'unloaded'
            # print ' closed so no need to unload'
        else:
            if self.play_state == 'loaded':
                # load already complete so set unload signal and kick off state machine
                self.play_state = 'start_unload'
                self.unloading_count = 0
                self.unload_signal = True
                self.tick_timer = self.canvas.after(50,
                                                    self.load_state_machine)
            elif self.play_state == 'loading':
                # wait for load to complete before unloading - ???? must do this because does not respond to quit when loading
                # state machine will go to start_unloading state and stop omxplayer
                self.unload_signal = True
            else:
                self.mon.err(
                    self, 'illegal state in unload method: ' + self.play_state)
                self.end('error',
                         'illegal state in unload method: ' + self.play_state)

    def start_state_machine_show(self):
        if self.play_state == 'loaded':
            # print '\nstart show state machine ' + self.play_state
            self.play_state = 'showing'
            self.freeze_signal = False  # signal that user has pressed stop
            self.must_quit_signal = False
            # show the track and content
            self.omx.show(self.freeze_at_end_required)
            self.mon.log(self,
                         '>showing track from show Id: ' + str(self.show_id))

            # and start polling for state changes
            # print 'start show state machine show'
            self.tick_timer = self.canvas.after(0, self.show_state_machine)
        # race condition don't start state machine as unload in progress
        elif self.play_state == 'start_unload':
            pass
        else:
            self.mon.fatal(self,
                           'illegal state in show method ' + self.play_state)
            self.play_state = 'show-failed'
            if self.finished_callback is not None:
                self.finished_callback(
                    'error',
                    'illegal state in show method: ' + self.play_state)

    def start_state_machine_close(self):
        self.quit_signal = True
        # print 'start close state machine close'
        self.tick_timer = self.canvas.after(0, self.show_state_machine)

    def load_state_machine(self):
        # print 'vidoeplayer state is'+self.play_state
        if self.play_state == 'loading':
            # if omxdriver says loaded then can finish normally
            # self.mon.log(self,"      State machine: " + self.play_state)
            if self.omx.end_play_signal is True:
                # got nice day before the first timestamp
                self.mon.warn(self, self.track)
                self.mon.warn(
                    self,
                    "loading  - omxplayer ended before starting track with reason: "
                    + self.omx.end_play_reason + ' at ' +
                    str(self.omx.video_position))
                self.omx.kill()
                self.mon.err(self, 'omxplayer ended before loading track')
                self.play_state = 'load-failed'
                self.mon.log(
                    self, "      Entering state : " + self.play_state +
                    ' from show Id: ' + str(self.show_id))
                if self.loaded_callback is not None:
                    self.loaded_callback(
                        'error', 'omxplayer ended before loading track')
            else:
                # end play signal false  - continue waiting for first timestamp
                self.loading_count += 1
                # video has loaded
                if self.omx.start_play_signal is True:
                    self.mon.log(
                        self, "Loading complete from show Id: " +
                        str(self.show_id) + ' ' + self.track)
                    self.mon.log(
                        self, 'Got video duration from track, frezing at: ' +
                        str(self.omx.duration) + ' microsecs.')

                    if self.unload_signal is True:
                        # print('unload sig=true state= start_unload')
                        # need to unload, kick off state machine in 'start_unload' state
                        self.play_state = 'start_unload'
                        self.unloading_count = 0
                        self.mon.log(
                            self, "      Entering state : " + self.play_state +
                            ' from show Id: ' + str(self.show_id))
                        self.tick_timer = self.canvas.after(
                            200, self.load_state_machine)
                    else:
                        self.play_state = 'loaded'
                        self.mon.log(
                            self, "      Entering state : " + self.play_state +
                            ' from show Id: ' + str(self.show_id))
                        if self.loaded_callback is not None:
                            # print 'callback when loaded'
                            self.loaded_callback('normal', 'video loaded')
                else:
                    # start play signal false - continue to wait
                    if self.loading_count > 400:  #40 seconds
                        # deal with omxplayer crashing while  loading and hence not receive start_play_signal
                        self.mon.warn(self, self.track)
                        self.mon.warn(
                            self, "loading - videoplayer counted out: " +
                            self.omx.end_play_reason + ' at ' +
                            str(self.omx.video_position))
                        self.omx.kill()
                        self.mon.warn(
                            self,
                            'videoplayer counted out when loading track ')
                        self.play_state = 'load-failed'
                        self.mon.log(
                            self, "      Entering state : " + self.play_state +
                            ' from show Id: ' + str(self.show_id))
                        if self.loaded_callback is not None:
                            self.loaded_callback(
                                'error',
                                'omxplayer counted out when loading track')
                    else:
                        self.tick_timer = self.canvas.after(
                            100, self.load_state_machine)  #200

        elif self.play_state == 'start_unload':
            # omxplayer reports it is terminating
            # self.mon.log(self,"      State machine: " + self.play_state)

            # deal with unload signal
            if self.unload_signal is True:
                self.unload_signal = False
                self.omx.stop()

            if self.omx.end_play_signal is True:
                self.omx.end_play_signal = False
                self.mon.log(
                    self,
                    "            <end play signal received with reason: " +
                    self.omx.end_play_reason + ' at: ' +
                    str(self.omx.video_position))

                # omxplayer has been closed
                if self.omx.end_play_reason == 'nice_day':
                    # no problem with omxplayer
                    self.play_state = 'unloading'
                    self.unloading_count = 0
                    self.mon.log(
                        self, "      Entering state : " + self.play_state +
                        ' from show Id: ' + str(self.show_id))
                    self.tick_timer = self.canvas.after(
                        50, self.load_state_machine)
                else:
                    # unexpected reason
                    self.mon.err(
                        self, 'unexpected reason at unload: ' +
                        self.omx.end_play_reason)
                    self.end(
                        'error', 'unexpected reason at unload: ' +
                        self.omx.end_play_reason)
            else:
                # end play signal false
                self.tick_timer = self.canvas.after(50,
                                                    self.load_state_machine)

        elif self.play_state == 'unloading':
            # wait for unloading to complete
            self.mon.log(self, "      State machine: " + self.play_state)

            # if spawned process has closed can change to closed state
            if self.omx.is_running() is False:
                self.mon.log(self, "            <omx process is dead")
                self.play_state = 'unloaded'
                self.mon.log(
                    self, "      Entering state : " + self.play_state +
                    ' from show Id: ' + str(self.show_id))
            else:
                # process still running
                self.unloading_count += 1
                if self.unloading_count > 10:
                    # deal with omxplayer not terminating at the end of a track
                    self.mon.warn(self, self.track)
                    self.mon.warn(
                        self,
                        "            <unloading - omxplayer failed to close at: "
                        + str(self.omx.video_position))
                    self.mon.warn(self, 'omxplayer should now  be killed ')
                    self.omx.kill()
                    self.play_state = 'unloaded'
                    self.mon.log(
                        self, "      Entering state : " + self.play_state +
                        ' from show Id: ' + str(self.show_id))
                else:
                    self.tick_timer = self.canvas.after(
                        200, self.load_state_machine)
        else:
            self.mon.err(
                self,
                'illegal state in load state machine: ' + self.play_state)
            self.end('error',
                     'load state machine in illegal state: ' + self.play_state)

    def show_state_machine(self):
        # print self.play_state
        # if self.play_state != 'showing': print 'show state is '+self.play_state
        if self.play_state == 'showing':
            # service any queued stop signals by sending quit to omxplayer
            # self.mon.log(self,"      State machine: " + self.play_state)
            if self.quit_signal is True:
                self.quit_signal = False
                self.mon.log(self, "      quit video - Send stop to omxdriver")
                self.omx.stop()
                self.tick_timer = self.canvas.after(50,
                                                    self.show_state_machine)

            # omxplayer reports it is terminating
            elif self.omx.end_play_signal is True:
                self.omx.end_play_signal = False
                self.mon.log(
                    self, "end play signal received with reason: " +
                    self.omx.end_play_reason + ' at: ' +
                    str(self.omx.video_position))
                # paused at end of track so return so calling prog can release the pause
                if self.omx.end_play_reason == 'pause_at_end':
                    self.frozen_at_end = True
                    if self.finished_callback is not None:
                        self.finished_callback('pause_at_end', 'pause at end')

                elif self.omx.end_play_reason == 'nice_day':
                    # no problem with omxplayer
                    self.play_state = 'closing'
                    self.closing_count = 0
                    self.mon.log(
                        self, "      Entering state : " + self.play_state +
                        ' from show Id: ' + str(self.show_id))
                    self.tick_timer = self.canvas.after(
                        50, self.show_state_machine)
                else:
                    # unexpected reason
                    self.mon.err(
                        self, 'unexpected reason at end of show ' +
                        self.omx.end_play_reason)
                    self.play_state = 'show-failed'
                    if self.finished_callback is not None:
                        self.finished_callback(
                            'error', 'unexpected reason at end of show: ' +
                            self.omx.end_play_reason)

            else:
                # nothing to do just try again
                # print 'showing - try again'
                self.tick_timer = self.canvas.after(50,
                                                    self.show_state_machine)

        elif self.play_state == 'closing':
            # wait for closing to complete
            self.mon.log(self, "      State machine: " + self.play_state)
            if self.omx.is_running() is False:
                # if spawned process has closed can change to closed state
                self.mon.log(self, "            <omx process is dead")
                self.play_state = 'closed'
                # print 'process dead going to closed'
                self.omx = None
                self.mon.log(
                    self, "      Entering state : " + self.play_state +
                    ' from show Id: ' + str(self.show_id))
                if self.closed_callback is not None:
                    self.closed_callback('normal', 'omxplayer closed')
            else:
                # process still running
                self.closing_count += 1
                # print 'closing - waiting for process to die',self.closing_count
                if self.closing_count > 10:
                    # deal with omxplayer not terminating at the end of a track
                    # self.mon.warn(self,self.track)
                    # self.mon.warn(self,"omxplayer failed to close at: " + str(self.omx.video_position))
                    self.mon.warn(
                        self,
                        'failed to close - omxplayer now being killed with SIGINT'
                    )
                    self.omx.kill()
                    # print 'closing - precess will not die so ita been killed with SIGINT'
                    self.play_state = 'closed'
                    self.omx = None
                    self.mon.log(
                        self, "      Entering state : " + self.play_state +
                        ' from show Id: ' + str(self.show_id))
                    if self.closed_callback is not None:
                        self.closed_callback('normal',
                                             'closed omxplayer after sigint')
                else:
                    self.tick_timer = self.canvas.after(
                        200, self.show_state_machine)

        elif self.play_state == 'closed':
            # needed because wait_for_end polls the state and does not use callback
            self.mon.log(self, "      State machine: " + self.play_state)
            self.tick_timer = self.canvas.after(200, self.show_state_machine)

        else:
            self.mon.err(
                self,
                'unknown state in show/close state machine ' + self.play_state)
            self.play_state = 'show-failed'
            if self.finished_callback is not None:
                self.finished_callback(
                    'error',
                    'show state machine in unknown state: ' + self.play_state)

    def parse_video_window(self, line, display_id):
        # model other than 4 video is rotated by hdmi_display_rotate in config.txt
        if self.dm.model_of_pi() == 4:
            rotation = self.dm.real_display_orientation(self.omx_display_id)
        else:
            rotation = 'normal'

        if rotation == 'normal':
            self.omx_rotate = ''
        elif rotation == 'right':
            self.omx_rotate = ' --orientation 90 '
        elif rotation == 'left':
            self.omx_rotate = ' --orientation 270 '
        else:
            #inverted
            self.omx_rotate = ' --orientation 180 '
        fields = line.split()
        # check there is a command field
        if len(fields) < 1:
            return 'error', 'no type field: ' + line, '', False, 0, 0, 0, 0

        # deal with types which have no paramters
        if fields[0] in ('original', 'letterbox', 'fill', 'default',
                         'stretch'):
            if len(fields) != 1:
                return 'error', 'number of fields for original: ' + line, '', False, 0, 0, 0, 0
            if fields[0] in ('letterbox', 'fill', 'stretch'):
                self.omx_aspect_mode = ' --aspect-mode ' + fields[0]
            else:
                self.omx_aspect_mode = ''
            return 'normal', '', fields[0], False, 0, 0, 0, 0

        # deal with warp which has 0 or 1 or 4 parameters (1 is x+y+w*h)
        # check basic syntax
        if fields[0] != 'warp':
            return 'error', 'not a valid type: ' + fields[
                0], '', False, 0, 0, 0, 0
        if len(fields) not in (1, 2, 5):
            return 'error', 'wrong number of coordinates for warp: ' + line, '', False, 0, 0, 0, 0

        # deal with window coordinates, just warp
        if len(fields) == 1:
            has_window = True
            x1 = self.show_canvas_x1
            y1 = self.show_canvas_y1
            x2 = self.show_canvas_x2
            y2 = self.show_canvas_y2
        else:
            # window is specified warp + dimesions etc.
            status, message, x1, y1, x2, y2 = parse_rectangle(' '.join(
                fields[1:]))
            if status == 'error':
                return 'error', message, '', False, 0, 0, 0, 0
            has_window = True
        x1_res, y1_res, x2_res, y2_res = self.transform_for_rotation(
            display_id, rotation, x1, y1, x2, y2)
        return 'normal', '', fields[
            0], has_window, x1_res, y1_res, x2_res, y2_res

    def transform_for_rotation(self, display_id, rotation, x1, y1, x2, y2):

        # adjust rotation of video for display rotation

        video_width = x2 - x1
        video_height = y2 - y1
        display_width, display_height = self.dm.real_display_dimensions(
            display_id)

        if rotation == 'right':
            x1_res = display_height - video_height - y1
            x2_res = display_height - y1

            y1_res = y1
            y2_res = video_width + y1

        elif rotation == 'normal':
            x1_res = x1
            y1_res = y1
            x2_res = x2
            y2_res = y2

        elif rotation == 'left':
            x1_res = y1
            x2_res = video_height + y1

            y1_res = display_width - video_width - x1
            y2_res = display_width - x1

        else:
            # inverted
            x2_res = display_width - x1
            x1_res = display_width - video_width - x1

            y2_res = display_height - y1
            y1_res = display_height - video_height - y1

        if VideoPlayer.debug:
            print('\nWarp calculation for Display Id', display_id, rotation)
            print('Video Window', x1, y1, x2, y2)
            print('video width/height', video_width, video_height)
            print('display width/height', display_width, display_height)
            print('Translated Window', x1_res, y1_res, x2_res, y2_res)
        return x1_res, y1_res, x2_res, y2_res
Beispiel #3
0
class VLCPlayer(Player):
    """
    plays a track using VLCDriver to access VLC via libvlc
    _init_ iniitalises state and checks resources are available.
    use the returned instance reference in all other calls.
    At the end of the path (when closed) do not re-use, make instance= None and start again.
    States - 'initialised' when done successfully.
    """

    debug = False
    debug = True

    def __init__(self, show_id, showlist, root, canvas, show_params,
                 track_params, pp_dir, pp_home, pp_profile, end_callback,
                 command_callback):

        # initialise items common to all players
        Player.__init__(self, show_id, showlist, root, canvas, show_params,
                        track_params, pp_dir, pp_home, pp_profile,
                        end_callback, command_callback)
        self.mon.trace(self, '')

        self.dm = DisplayManager()
        self.am = AudioManager()

        # get player parameters from show/track
        if self.track_params['vlc-audio'] != "":
            self.vlc_audio = self.track_params['vlc-audio']
        else:
            self.vlc_audio = self.show_params['vlc-audio']

        if self.track_params['vlc-volume'] != "":
            self.vlc_volume_text = self.track_params['vlc-volume']
        else:
            self.vlc_volume_text = self.show_params['vlc-volume']

        if self.track_params['vlc-window'] != '':
            self.vlc_window_text = self.track_params['vlc-window']
        else:
            self.vlc_window_text = self.show_params['vlc-window']

        if self.track_params['vlc-aspect-mode'] != '':
            self.vlc_aspect_mode = self.track_params['vlc-aspect-mode']
        else:
            self.vlc_aspect_mode = self.show_params['vlc-aspect-mode']

        if self.track_params['vlc-image-duration'] != '':
            self.vlc_image_duration_text = self.track_params[
                'vlc-image-duration']
        else:
            self.vlc_image_duration_text = self.show_params[
                'vlc-image-duration']

        if self.track_params['vlc-other-options'] != '':
            self.vlc_other_options = self.track_params['vlc-other-options']
        else:
            self.vlc_other_options = self.show_params['vlc-other-options']

        if self.track_params['vlc-layer'] != '':
            self.vlc_layer_text = self.track_params['vlc-layer']
        else:
            self.vlc_layer_text = self.show_params['vlc-layer']

        if self.track_params['vlc-freeze-at-start'] != '':
            self.freeze_at_start_text = self.track_params[
                'vlc-freeze-at-start']
        else:
            self.freeze_at_start_text = self.show_params['vlc-freeze-at-start']

        if self.track_params['vlc-freeze-at-end'] != '':
            self.freeze_at_end_text = self.track_params['vlc-freeze-at-end']
        else:
            self.freeze_at_end_text = self.show_params['vlc-freeze-at-end']

        if self.track_params['pause-timeout'] != '':
            pause_timeout_text = self.track_params['pause-timeout']
        else:
            pause_timeout_text = self.show_params['pause-timeout']

        if pause_timeout_text.isdigit():
            self.pause_timeout = int(pause_timeout_text)
        else:
            self.pause_timeout = 0

        # track only

        self.vlc_subtitles = self.track_params['vlc-subtitles']

        self.vlc_max_volume_text = self.track_params['vlc-max-volume']

        self.vlc_aspect_ratio = self.track_params['vlc-aspect-ratio']

        self.vlc_crop = self.track_params['vlc-crop']

        # initialise video playing state and signals
        self.quit_signal = False
        self.unload_signal = False
        self.play_state = 'initialised'
        self.frozen_at_end = False
        self.pause_timer = None

    # LOAD - creates a VLC instance, loads a track and then pause
    def load(self, track, loaded_callback, enable_menu):
        # instantiate arguments
        self.track = track
        self.loaded_callback = loaded_callback  #callback when loaded
        self.mon.log(
            self, "Load track received from show Id: " + str(self.show_id) +
            ' ' + self.track)
        self.mon.trace(self, '')

        #process vlc parameters
        status, message = self.process_params()
        if status == 'error':
            self.mon.err(self, message)
            self.play_state = 'load-failed'
            if self.loaded_callback is not None:
                self.loaded_callback('error', message)
                return

        #for pulse get sink name and check device is connected
        self.audio_sys = self.am.get_audio_sys()
        if self.audio_sys == 'pulse':
            status, message, self.vlc_sink = self.am.get_sink(self.vlc_audio)
            if status == 'error':
                self.mon.err(self, message)
                self.play_state = 'load-failed'
                if self.loaded_callback is not None:
                    self.loaded_callback('error', message)
                    return

            if not self.am.sink_connected(self.vlc_sink):
                self.mon.err(
                    self, self.vlc_audio +
                    ' audio device not connected\n\n    sink: ' +
                    self.vlc_sink)
                self.play_state = 'load-failed'
                if self.loaded_callback is not None:
                    self.loaded_callback('error', 'audio device not connected')
                    return
        else:
            self.mon.err(
                self,
                'audio systems other than pulseaudio are not supported\n hint: audio.cfg error'
            )
            self.play_state = 'load-failed'
            if self.loaded_callback is not None:
                self.loaded_callback('error', 'audio device not connected')
                return

        # do common bits of  load
        Player.pre_load(self)

        # load the plugin, this may modify self.track and enable the plugin drawing to canvas
        if self.track_params['plugin'] != '':
            status, message = self.load_plugin()
            if status == 'error':
                self.mon.err(self, message)
                self.play_state = 'load-failed'
                if self.loaded_callback is not None:
                    self.loaded_callback('error', message)
                    return

        # load the images and text
        status, message = self.load_x_content(enable_menu)
        if status == 'error':
            self.mon.err(self, message)
            self.play_state = 'load-failed'
            if self.loaded_callback is not None:
                self.loaded_callback('error', message)
                return

        # check file exists if not a mrl
        if not ':' in track:
            if not os.path.exists(track):
                self.mon.err(self, "Track file not found: " + track)
                self.play_state = 'load-failed'
                if self.loaded_callback is not None:
                    self.loaded_callback('error',
                                         'track file not found: ' + track)
                    return

        cmd = 'DISPLAY= python3 ' + self.pp_dir + '/pp_vlcdriver.py'
        #print (cmd)
        # need bash because of DISPLAY=
        self.vlcdriver = pexpect.spawn('/bin/bash', ['-c', cmd],
                                       encoding='utf-8')

        # get rid of driver start message
        start_message = self.vlcdriver.readline().strip('\r\n')
        if start_message != 'VLCDriver starting':
            self.mon.fatal(
                self, "VLCDriver failed\n Hint: sudo pip3 install python-vlc ")
            self.play_state = 'load-failed'
            if self.loaded_callback is not None:
                self.loaded_callback('error', 'VLCDriver starting')
                return
        self.vlcdriver.setecho(False)

        self.vlcdriver.sendline('iopts' + self.iopts)
        self.vlcdriver.sendline('pauseopts' + self.pauseopts)
        self.vlcdriver.sendline('track ' + track)

        # load the media
        self.vlcdriver.sendline('load')

        #get size of media, do after load and before play. Not used at the moment
        self.media_width, self.media_height = self.get_size()

        # calculate and send crop/aspect ratio and aspect mode. Done here as size might be needed in the future
        status, message = self.aspect_mode()
        if status != 'normal':
            self.mon.err(self, message)
            self.play_state = 'load-failed'
            if self.loaded_callback is not None:
                self.loaded_callback('error', 'track file not found: ' + track)
                return

        # play until pause at start
        self.vlcdriver.sendline('play')
        self.start_state_machine_load()

    # SHOW - show a track
    def show(self, ready_callback, finished_callback, closed_callback):
        self.ready_callback = ready_callback  # callback when paused after load ready to show video
        self.finished_callback = finished_callback  # callback when finished showing
        self.closed_callback = closed_callback

        self.mon.trace(self, '')

        #  do animation at start and ready_callback
        Player.pre_show(self)

        # start show state machine
        self.start_state_machine_show()

    # UNLOAD - abort a load when vlcdriver is loading or loaded
    def unload(self):
        self.mon.trace(self, '')
        self.mon.log(self,
                     ">unload received from show Id: " + str(self.show_id))
        self.start_state_machine_unload()

    # CLOSE - quits vlcdriver from 'pause at end' state
    def close(self, closed_callback):
        self.mon.trace(self, '')
        self.mon.log(self,
                     ">close received from show Id: " + str(self.show_id))
        self.closed_callback = closed_callback
        self.start_state_machine_close()

    def get_size(self):
        self.vlcdriver.sendline('get-size')
        resp = self.vlcdriver.readline().strip('\r\n')
        resp_list = resp.split(' ')
        return resp_list[0], resp_list[1]

    def get_state(self):
        self.vlcdriver.sendline('t')
        resp = self.vlcdriver.readline().strip('\r\n')
        #print (resp)
        return resp

# ***********************
# track showing state machine
# **********************

    """
    STATES OF STATE MACHINE
    Threre are ongoing states and states that are set just before callback

    >init - Create an instance of the class
    <On return - state = initialised   -  - init has been completed, do not generate errors here

    >load
        Fatal errors should be detected in load. If so  loaded_callback is called with 'load-failed'
         Ongoing - state=loading - load called, waiting for load to complete   
    < loaded_callback with status = normal
         state=loaded - load has completed and video paused before or after first frame      
    <loaded_callback with status=error
        state= load-failed -  failure to load   

    On getting the loaded_callback with status=normal the track can be shown using show


    >show
        show assumes a track has been loaded and is paused.
       Ongoing - state=showing - video is showing 
    <finished_callback with status = pause_at_end
            state=showing but frozen_at_end is True
    <closed_callback with status= normal
            state = closed - video has ended vlc has terminated.


    On getting finished_callback with status=pause_at end a new track can be shown and then use close to close the previous video when new track is ready
    On getting closed_callback with status=  nice_day vlcdriver closing should not be attempted as it is already closed
    Do not generate user errors in Show. Only generate system errors such as illegal state and then use end()

    >close
       Ongoing state - closing - vlcdriver processes are dying
    <closed_callback with status= normal - vlcdriver is dead, can close the track instance.

    >unload
        Ongoing states - start_unload and unloading - vlcdriver processes are dying.
        when unloading is complete state=unloaded
        I have not added a callback to unload. its easy to add one if you want.

    closed is needed because wait_for_end in pp_show polls for closed and does not use closed_callback
    
    """
    def start_state_machine_load(self):
        # initialise all the state machine variables
        self.play_state = 'loading'
        #self.set_volume(self.volume)
        self.tick_timer = self.canvas.after(1, self.load_state_machine)  #50

    def load_state_machine(self):
        if self.unload_signal is True:
            self.unload_signal = False
            self.state = 'unloading'
            self.vlcdriver.sendline('unload')
            self.root.after(100, self.load_state_machine)
        else:
            resp = self.get_state()
            # pp_vlcdriver changes state from load-loading when track is frozen at start.
            if resp == 'load-fail':
                self.play_state = 'load-failed'
                self.mon.log(
                    self, "      Entering state : " + self.play_state +
                    ' from show Id: ' + str(self.show_id))
                if self.loaded_callback is not None:
                    self.loaded_callback('error',
                                         'timeout when loading vlc track')
                return
            elif resp == 'load-unloaded':
                self.play_state = 'unloaded'
                self.mon.log(
                    self, "      Entering state : " + self.play_state +
                    ' from show Id: ' + str(self.show_id))
                # PP does not need this callback
                #if self.loaded_callback is not  None:
                #self.loaded_callback('normal','unloaded')
                return
            elif resp in ('load-ok', 'stop-frozen'):
                # stop received while in freeze-at-start - quit showing as soon as it starts
                if resp == 'stop-frozen':
                    self.quit_signal = True
                    self.mon.log(self,
                                 'stop received while in freeze-at-start')
                self.play_state = 'loaded'
                if self.vlc_sink != '':
                    self.set_device(self.vlc_sink)
                else:
                    self.set_device('')
                self.set_volume(self.volume)
                self.mon.log(
                    self, "      Entering state : " + self.play_state +
                    ' from show Id: ' + str(self.show_id))
                if self.loaded_callback is not None:
                    self.loaded_callback('normal', 'loaded')
                return
            else:
                self.root.after(10, self.load_state_machine)  #100

    def start_state_machine_unload(self):
        # print ('videoplayer - starting unload',self.play_state)
        if self.play_state in ('closed', 'initialised', 'unloaded'):
            # vlcdriver already closed
            self.play_state = 'unloaded'
            # print ' closed so no need to unload'
        else:
            if self.play_state == 'loaded':
                # load already complete so set unload signal and kick off load state machine
                self.play_state = 'start_unload'
                self.unload_signal = True
                self.tick_timer = self.canvas.after(50,
                                                    self.load_state_machine)

            elif self.play_state == 'loading':
                # signal load state machine to start_unloading state and stop vlcdriver
                self.unload_signal = True
            else:
                self.mon.err(
                    self, 'illegal state in unload method: ' + self.play_state)
                self.end('error',
                         'illegal state in unload method: ' + self.play_state)

    def start_state_machine_show(self):
        if self.play_state == 'loaded':
            # print '\nstart show state machine ' + self.play_state
            self.play_state = 'showing'
            self.freeze_signal = False  # signal that user has pressed stop
            self.must_quit_signal = False
            # show the track and content
            self.vlcdriver.sendline('show')
            self.mon.log(self,
                         '>showing track from show Id: ' + str(self.show_id))
            self.set_volume(self.volume)
            # and start polling for state changes
            # print 'start show state machine show'
            self.tick_timer = self.canvas.after(0, self.show_state_machine)
            """
            # race condition don't start state machine as unload in progress
            elif self.play_state == 'start_unload':
                pass
            """
        else:
            self.mon.fatal(self,
                           'illegal state in show method ' + self.play_state)
            self.play_state = 'show-failed'
            if self.finished_callback is not None:
                self.finished_callback(
                    'error',
                    'illegal state in show method: ' + self.play_state)

    def show_state_machine(self):
        if self.play_state == 'showing':
            if self.quit_signal is True:
                # service any queued stop signals by sending stop to vlcdriver
                self.quit_signal = False
                self.mon.log(self, "      stop video - Send stop to vlcdriver")
                self.vlcdriver.sendline('stop')
                self.tick_timer = self.canvas.after(10,
                                                    self.show_state_machine)
            else:
                resp = self.get_state()
                #print (resp)
                # driver changes state from show-showing depending on freeze-at-end.
                if resp == 'show-pauseatend':
                    self.mon.log(self, 'vlcdriver says pause_at_end')
                    self.frozen_at_end = True
                    if self.finished_callback is not None:
                        self.finished_callback('pause_at_end', 'pause at end')

                elif resp == 'show-niceday':
                    self.mon.log(self, 'vlcdriver says nice_day')
                    self.play_state = 'closing'
                    self.vlcdriver.sendline('close')
                    # and terminate the vlcdriver process through pexpect
                    self.vlcdriver.terminate()
                    self.tick_timer = self.canvas.after(
                        10, self.show_state_machine)

                elif resp == 'show-fail':
                    self.play_state = 'show-failed'
                    if self.finished_callback is not None:
                        self.finished_callback(
                            'error', 'pp_vlcdriver says show failed: ' +
                            self.play_state)
                else:
                    self.tick_timer = self.canvas.after(
                        30, self.show_state_machine)

        elif self.play_state == 'closing':
            # close the pexpect process
            self.vlcdriver.close()
            self.play_state = 'closed'
            # state change needed for wait for end
            self.mon.log(
                self, "      Entering state : " + self.play_state +
                ' from show Id: ' + str(self.show_id))
            if self.closed_callback is not None:
                self.closed_callback('normal', 'vlcdriver closed')

    # respond to normal stop
    def stop(self):
        self.mon.log(self, ">stop received from show Id: " + str(self.show_id))
        # cancel the pause timer
        if self.pause_timer != None:
            self.canvas.after_cancel(self.pause_timer)
            self.pause_timer = None
        self.vlcdriver.sendline('stop')

    def start_state_machine_close(self):
        # self.mon.log(self,">close received from show Id: "+ str(self.show_id))
        # cancel the pause timer
        if self.pause_timer != None:
            self.canvas.after_cancel(self.pause_timer)
            self.pause_timer = None
        self.vlcdriver.sendline('close')
        self.play_state = 'closing'
        #print ('start close state machine close')
        self.tick_timer = self.canvas.after(0, self.show_state_machine)

# ************************
# COMMANDS
# ************************

    def input_pressed(self, symbol):
        if symbol == 'inc-volume':
            self.inc_volume()
        elif symbol == 'dec-volume':
            self.dec_volume()
        elif symbol == 'pause':
            self.pause()
        elif symbol == 'go':
            self.go()
        elif symbol == 'unmute':
            self.unmute()
        elif symbol == 'mute':
            self.mute()
        elif symbol == 'pause-on':
            self.pause_on()
        elif symbol == 'pause-off':
            self.pause_off()
        elif symbol == 'stop':
            self.stop()

    def inc_volume(self):
        self.mon.log(self,
                     ">inc-volume received from show Id: " + str(self.show_id))
        if self.play_state == 'showing':
            if self.volume < self.max_volume:
                self.volume += 1
            self.set_volume(self.volume)
            return True
        else:
            self.mon.log(self, "!<inc-volume rejected " + self.play_state)
            return False

    def dec_volume(self):
        self.mon.log(self,
                     ">dec-volume received from show Id: " + str(self.show_id))

        if self.play_state == 'showing':
            if self.volume > 0:
                self.volume -= 1
            self.set_volume(self.volume)
            return True
        else:
            self.mon.log(self, "!<dec-volume rejected " + self.play_state)
            return False

    def set_volume(self, vol):
        # print ('SET VOLUME',vol)
        self.vlcdriver.sendline('vol ' + str(vol))

    def set_device(self, device):
        self.vlcdriver.sendline('device ' + device)

    def mute(self):
        self.mon.log(self, ">mute received from show Id: " + str(self.show_id))
        self.vlcdriver.sendline('mute')
        return True

    def unmute(self):
        self.mon.log(self,
                     ">unmute received from show Id: " + str(self.show_id))
        self.vlcdriver.sendline('unmute')

    # toggle pause
    def pause(self):
        self.mon.log(
            self, ">toggle pause received from show Id: " + str(self.show_id))
        self.vlcdriver.sendline('pause')
        reply = self.vlcdriver.readline().strip('\r\n')
        if reply == 'pause-on-ok':
            if self.pause_timeout > 0:
                # kick off the pause timeout timer
                self.pause_timer = self.canvas.after(
                    self.pause_timeout * 1000, self.pause_timeout_callback)
            return True
        elif reply == 'pause-off-ok':
            if self.pause_timer != None:
                # cancel the pause timer
                self.canvas.after_cancel(self.pause_timer)
                self.pause_timer = None
            return True
        else:
            self.mon.log(self, "!<toggle pause rejected " + self.play_state)
            return False

    def pause_timeout_callback(self):
        self.pause_off()
        self.pause_timer = None

    # pause on
    def pause_on(self):
        self.mon.log(self,
                     ">pause on received from show Id: " + str(self.show_id))
        self.vlcdriver.sendline('pause-on')
        reply = self.vlcdriver.readline().strip('\r\n')
        if reply == 'pause-on-ok':
            if self.pause_timeout > 0:
                # kick off the pause timeout timer
                self.pause_timer = self.canvas.after(
                    self.pause_timeout * 1000, self.pause_timeout_callback)
            return True
        else:
            self.mon.log(self, "!<pause on rejected " + self.play_state)
            return False

    # pause off
    def pause_off(self):
        self.mon.log(self,
                     ">pause off received from show Id: " + str(self.show_id))
        self.vlcdriver.sendline('pause-off')
        reply = self.vlcdriver.readline().strip('\r\n')
        if reply == 'pause-off-ok':
            if self.pause_timer != None:
                # cancel the pause timer
                self.canvas.after_cancel(self.pause_timer)
                self.pause_timer = None
            return True
        else:
            self.mon.log(self, "!<pause off rejected " + self.play_state)
            return False

    # go after freeze at start
    def go(self):
        self.vlcdriver.sendline('go')
        reply = self.vlcdriver.readline().strip('\r\n')
        if reply == 'go-ok':
            return True
        else:
            self.mon.log(self, "!<go rejected " + self.play_state)
            return False


# *****************************
# SETUP
# *****************************

    def process_params(self):

        # volume will be set during show by set_volume()
        # --------------------------------------
        if self.vlc_max_volume_text != "":
            if not self.vlc_max_volume_text.isdigit():
                return 'error', 'VLC Max Volume must be a positive integer: ' + self.vlc_max_volume_text
            self.max_volume = int(self.vlc_max_volume_text)
            if self.max_volume > 100:
                return 'error', 'VLC Max Volume must be <= 100: ' + self.vlc_max_volume_text
        else:
            self.max_volume = 100

        if self.vlc_volume_text != "":
            if not self.vlc_volume_text.isdigit():
                return 'error', 'VLC Volume must be a positive integer: ' + self.vlc_volume_text
            self.volume = int(self.vlc_volume_text)
            if self.volume > 100:
                return 'error', 'VLC Volume must be <= 100: ' + self.vlc_max_volume_text
        else:
            self.volume = 100

        self.volume = min(self.volume, self.max_volume)

        # instance options
        # ----------------

        #audio system - pulseaudio
        audio_opt = '--aout=pulse '

        # other options
        if self.vlc_other_options != '':
            other_opts = self.vlc_other_options + ' '
        else:
            other_opts = ''

        # subtitles
        if self.vlc_subtitles != 'yes':
            subtitle_opt = '--no-spu '
        else:
            subtitle_opt = ''

        # display
        # -------
        # start with board name
        if self.track_params['display-name'] != "":
            video_display_name = self.track_params['display-name']
        else:
            video_display_name = self.show_canvas_display_name
        # Is it valid and connected
        status, message, self.display_id = self.dm.id_of_display(
            video_display_name)
        if status == 'error':
            return 'error', message

        #convert to task bar names for vlc
        vlc_display_name = self.dm.vlc_display_name_map[video_display_name]

        display_opt = '--mmal-display=' + vlc_display_name + ' '
        # does it do DSI-1????

        # layer
        # ******
        if self.vlc_layer_text == '':
            layer_opt = ''
        elif self.vlc_layer_text == 'hidden':
            layer_opt = '--mmal-layer=-128 '
        else:
            if not self.vlc_layer_text.isdigit():
                return 'error', 'Display Layer is not a positive number: ' + self.vlc_layer_text
            layer_opt = '--mmal-layer=' + self.vlc_layer_text + ' '

        # image duration
        # **************
        if not self.vlc_image_duration_text.isdigit():
            return 'error', 'Image Duration is not a positive number: ' + self.vlc_image_duration_text
        if int(self.vlc_image_duration_text) == 0:
            image_duration_opt = '--image-duration -1 '
        else:
            image_duration_opt = '--image-duration ' + self.vlc_image_duration_text + ' '

        # video window
        # ************
        # parse video window and mangle wxh+x+y string for aspect_mode
        status, message, window_text = self.parse_vlc_video_window(
            self.vlc_window_text)
        if status == 'error':
            return 'error', message

        if window_text != '':
            window_opt = '--mmal-vout-window ' + window_text + ' '
        else:
            window_opt = ''

        # transformation
        # ***************
        # model other than 4 video is rotated by hdmi_display_rotate in config.txt
        if self.dm.model_of_pi() == 4:
            rotation = self.dm.real_display_orientation(self.display_id)
        else:
            rotation = 'normal'

        if rotation == 'normal':
            transform_opt = ' --mmal-vout-transform=0 '
        elif rotation == 'right':
            transform_opt = ' --mmal-vout-transform=90 '
        elif rotation == 'left':
            transform_opt = ' --mmal-vout-transform=270 '
        else:
            #inverted
            transform_opt = ' --mmal-vout-transform=180 '

        self.iopts = ' ' + window_opt + layer_opt + display_opt + transform_opt + subtitle_opt + audio_opt + image_duration_opt + other_opts

        # pause options
        #---------------

        if self.freeze_at_end_text == 'yes':
            self.freeze_at_end_required = True
        else:
            self.freeze_at_end_required = False

        freeze_start_opt = ' ' + self.freeze_at_start_text
        freeze_end_opt = ' ' + self.freeze_at_end_text

        self.pauseopts = freeze_start_opt + freeze_end_opt

        return 'normal', ''

    def aspect_mode(self):
        """
        In omxplayer Video Window has parameters only for warp, all other modes are centred the full screen

        In vlcplayer there are two fields, VLC Window and Aspect Mode. VLC Window position and sizes the window then
        Aspect Mode is applied to all results of VLC Window :
        
        stretch - transform video to make it fill the window with aspect ratio the same as the window

        fill - crop the video so that it fills the window while retaining the video's aspect ratio

        letterbox - vlc's default mode
                        adjust the video  so that whole video is seen in the window while keeping the video's aspect ratio
                        If the window's aspect ratio does not match that of the video media then the x,y, position will be incorrect,
                        (except on fullscreen). This is because the result is centred in the window.


        vlc - use vlc's aspect ratio and crop in the defined window
            Cannot use both crop and aspect ratio
            window size w*h needs to have the same ratio as the result of crop or aspect-ratio otherwise the displayed position will be incorrect
            values must be integers e.g 5.1:1 does not work 
            crop formats:    <aspect_num>:<aspect_den> e.g.4:3
                            <width>x<height>+<x>+<y>
                            <left>+<top>+<right>+<bottom>
            aspect ratio formats: <aspect_num>:<aspect_den> e.g.4:3
        """
        if self.vlc_aspect_mode == 'stretch':
            window_ratio = self.vlc_window_width / self.vlc_window_height
            self.vlcdriver.sendline('ratio ' + str(self.vlc_window_width) +
                                    ':' + str(self.vlc_window_height))
            return 'normal', ''

        elif self.vlc_aspect_mode == 'fill':
            self.vlcdriver.sendline('crop ' + str(self.vlc_window_width) +
                                    ':' + str(self.vlc_window_height))
            return 'normal', ''

        elif self.vlc_aspect_mode == 'letterbox':
            # default vlc behavior
            return 'normal', ''

        elif self.vlc_aspect_mode == 'vlc':
            if self.vlc_aspect_ratio != '' or self.vlc_crop != '':
                if self.vlc_crop != '':
                    self.vlcdriver.sendline('crop ' + self.vlc_crop)

                if self.vlc_aspect_ratio != '':
                    self.vlcdriver.sendline('ratio ' + self.vlc_aspect_ratio)
                return 'normal', ''
            else:
                return 'error', 'crop or aspect mode not specified for vlc option'
        else:
            return 'error', 'Aspect Mode cannot be blank ' + self.vlc_aspect_mode

    def parse_vlc_video_window(self, line):
        words = line.split()
        if len(words) not in (1, 2):
            return 'error', 'bad vlc video window form ' + line, ''

        if words[0] not in ('display', 'showcanvas'):
            return 'error', 'Bad VLC Window option: ' + line, ''

        if words[0] == 'display':
            x_org = 0
            y_org = 0
            width, height = self.dm.canvas_dimensions(self.display_id)

        if words[0] == 'showcanvas':
            x_org = self.show_canvas_x1
            y_org = self.show_canvas_y1
            width = self.show_canvas_width
            height = self.show_canvas_height

        #replace canvas/display height/width from spec
        x_offset = 0
        y_offset = 0
        if len(words) == 2:
            #pass in canvas/display width/height. Returns window width/height
            status, message, x_offset, y_offset, width, height = self.parse_dimensions(
                words[1], width, height)
            if status == 'error':
                return 'error', message, ''

        x = x_org + x_offset
        y = y_org + y_offset
        self.vlc_window_x = x
        self.vlc_window_y = y
        self.vlc_window_width = width
        self.vlc_window_height = height
        vlc_text = str(width) + 'x' + str(height) + '+' + str(x) + '+' + str(y)
        return 'normal', '', vlc_text

    def parse_dimensions(self, dim_text, show_width, show_height):
        # parse x+y+width*height or width*height
        if '+' in dim_text:
            # x+y+width*height
            fields = dim_text.split('+')
            if len(fields) != 3:
                return 'error', 'bad vlc video window form ' + dim_text, 0, 0, 0, 0

            if not fields[0].isdigit():
                return 'error', 'x value is not a positive decimal in vlc video window ' + dim_text, 0, 0, 0, 0
            else:
                x = int(fields[0])

            if not fields[1].isdigit():
                return 'error', 'y value is not a positive decimal in vlc video window ' + dim_text, 0, 0, 0, 0
            else:
                y = int(fields[1])

            dimensions = fields[2].split('*')
            if len(dimensions) != 2:
                return 'error', 'bad vlc video window dimensions ' + dim_text, 0, 0, 0, 0

            if not dimensions[0].isdigit():
                return 'error', 'width is not a positive decimal in vlc video window ' + dim_text, 0, 0, 0, 0
            else:
                width = int(dimensions[0])

            if not dimensions[1].isdigit():
                return 'error', 'height is not a positive decimal in vlc video window ' + dim_text, 0, 0, 0, 0
            else:
                height = int(dimensions[1])

            return 'normal', '', x, y, width, height
        else:
            dimensions = dim_text.split('*')
            if len(dimensions) != 2:
                return 'error', 'bad vlc video window dimensions ' + dim_text, 0, 0, 0, 0

            if not dimensions[0].isdigit():
                return 'error', 'width is not a positive decimal in vlc video window ' + dim_text, 0, 0, 0, 0
            else:
                window_width = int(dimensions[0])

            if not dimensions[1].isdigit():
                return 'error', 'height is not a positive decimal in vlc video window ' + dim_text, 0, 0, 0, 0
            else:
                window_height = int(dimensions[1])

            x = int((show_width - window_width) / 2)
            y = int((show_height - window_height) / 2)
            return 'normal', '', x, y, window_width, window_height