Пример #1
0
class ConnectedState:
	'''
	State class when connected to Hyperion and grabbing video
	'''

	def __init__(self, settings):
		'''Constructor
			- settings: Settings structure
		'''
		log("Entering connected state")

		self.__settings = settings
		self.__hyperion = None
		self.__capture = None
		self.__captureState = None
		self.__data = None
		self.__useLegacyApi = True

		# try to connect to hyperion
		self.__hyperion = Hyperion(self.__settings.address, self.__settings.port)

		# create the capture object
		self.__capture = xbmc.RenderCapture()
		self.__capture.capture(self.__settings.capture_width, self.__settings.capture_height)

	def __del__(self):
		'''Destructor
		'''
		del self.__hyperion
		del self.__capture
		del self.__captureState
		del self.__data
		del self.__useLegacyApi

	def execute(self):
		'''Execute the state
			- return: The new state to execute
		'''
		# check if we still need to grab
		if not self.__settings.grabbing():
			# return to the disconnected state
			return DisconnectedState(self.__settings)

		# check the xbmc API Version
		try:
			self.__capture.getCaptureState()
		except:
			self.__useLegacyApi = False

		# capture an image
		startReadOut = False
		if self.__useLegacyApi:
			self.__capture.waitForCaptureStateChangeEvent(200)
			self.__captureState = self.__capture.getCaptureState()
			if self.__captureState == xbmc.CAPTURE_STATE_DONE:
				startReadOut = True
		else:
			self.__data = self.__capture.getImage()
			if len(self.__data) > 0:
				startReadOut = True

		if startReadOut:
			if self.__useLegacyApi:
				self.__data = self.__capture.getImage()

			# retrieve image data and reformat into rgb format
			if self.__capture.getImageFormat() == 'ARGB':
				del self.__data[0::4]
			elif self.__capture.getImageFormat() == 'BGRA':
				del self.__data[3::4]
				self.__data[0::3], self.__data[2::3] = self.__data[2::3], self.__data[0::3]

			try:
				#send image to hyperion
				self.__hyperion.sendImage(self.__capture.getWidth(), self.__capture.getHeight(), str(self.__data), self.__settings.priority, 500)
			except Exception, e:
				# unable to send image. notify and go to the error state
				notify(xbmcaddon.Addon().getLocalizedString(32101))
				return ErrorState(self.__settings)

		if self.__useLegacyApi:
			if self.__captureState != xbmc.CAPTURE_STATE_WORKING:
				#the current capture is processed or it has failed, we request a new one
				self.__capture.capture(self.__settings.capture_width, self.__settings.capture_height)

		#limit the maximum number of frames sent to hyperion
		xbmc.sleep( int(1. / self.__settings.framerate * 1000) )

		return self
Пример #2
0
    while not monitor.abortRequested():
        try:
            if enabled and player.isPlayingVideo():
                data = capture.getImage()
                if len(data) == 0:
                    capture.capture(HYPERION_WIDTH, HYPERION_HEIGHT)
                    hyperion.clear(HYPERION_PRIORITY)
                    continue

                if capture.getImageFormat() == 'ARGB':
                    del data[0::4]
                elif capture.getImageFormat() == 'BGRA':
                    del data[3::4]
                    data[0::3], data[2::3] = data[2::3], data[0::3]

                hyperion.sendImage(capture.getWidth(), capture.getHeight(),
                                   bytes(data), HYPERION_PRIORITY, -1)
            else:
                hyperion.clear(HYPERION_PRIORITY)
                monitor.waitForAbort(1.0)
        except Exception as e:
            notify('Something went wrong')
            log(e)
            log("".join(traceback.format_exception(type(e), e,
                                                   e.__traceback__)))
            hyperion.clear(HYPERION_PRIORITY)
            monitor.waitForAbort(30.0)
            if not monitor.abortRequested():
                capture = xbmc.RenderCapture()
                capture.capture(HYPERION_WIDTH, HYPERION_HEIGHT)
                hyperion = Hyperion(HYPERION_IP, HYPERION_PORT)
Пример #3
0
class ConnectedState:
    '''
    State class when connected to Hyperion and grabbing video
    '''
    def __init__(self, settings):
        '''Constructor
          - settings: Settings structure
        '''
        log("Entering connected state")

        self.__settings = settings
        self.__hyperion = None
        self.__capture = None
        self.__captureState = None
        self.__data = None
        self.__counter = 0

        # try to connect to hyperion
        self.__hyperion = Hyperion(self.__settings.address,
                                   self.__settings.port)

        # Force clearing of priority (mainly for Orbs)
        self.clear_priority()

        # create the capture object
        self.__capture = xbmc.RenderCapture()
        self.__capture.capture(self.__settings.capture_width,
                               self.__settings.capture_height)

    def __del__(self):
        '''Destructor
        '''
        del self.__hyperion
        del self.__capture
        del self.__captureState
        del self.__data

    def clear_priority(self):
        # Force clearing of priority (mainly for Orbs)
        xbmc.sleep(1000)
        self.__hyperion.clear(self.__settings.priority)
        xbmc.sleep(1000)
        self.__hyperion.clear(self.__settings.priority)

    def execute(self):
        '''Execute the state
          - return: The new state to execute
        '''
        # check if we still need to grab
        if not self.__settings.grabbing():
            self.clear_priority()

            # return to the disconnected state
            return DisconnectedState(self.__settings)
        # capture an image
        startReadOut = False

        self.__data = self.__capture.getImage()
        if len(self.__data) > 0:
            startReadOut = True

        if startReadOut:
            # retrieve image data and reformat into rgb format
            if self.__capture.getImageFormat() == 'BGRA':
                del self.__data[3::4]
                self.__data[0::3], self.__data[2::3] = self.__data[
                    2::3], self.__data[0::3]
                ba = self.__data
                size = (self.__capture.getWidth(), self.__capture.getHeight())
                img = Image.frombuffer('RGB', size, str(ba), 'raw', 'RGB', 0,
                                       1)
                #img.save("C:/tmp/kodi/image"+str(self.__counter)+".jpg")
                img.save("/storage/screenshots/" + str(self.__counter) +
                         ".jpg")
                log("Saved image " + str(self.__counter) + ".jpg")
                self.__counter += 1

            try:
                # send image to hyperion
                self.__hyperion.sendImage(self.__capture.getWidth(),
                                          self.__capture.getHeight(),
                                          str(self.__data),
                                          self.__settings.priority, -1)
            except Exception, e:
                # unable to send image. notify and go to the error state
                log(e)
                notify(xbmcaddon.Addon().getLocalizedString(32101))
                return ErrorState(self.__settings)
        else:
Пример #4
0
class ConnectedState(BaseState):
    '''
    State class when connected to Hyperion and grabbing video
    '''
    def __init__(self, settings):
        BaseState.__init__(self, settings)

        self._hyperion = None
        self._capture = None
        self._captureState = None
        self._data = None
        self._useLegacyApi = None

        # try to connect to hyperion
        self._hyperion = Hyperion(self._settings.address, self._settings.port)

        # create the capture object
        self._capture = xbmc.RenderCapture()
        self._capture.capture(self._settings.capture_width,
                              self._settings.capture_height)

    def __del__(self):
        '''Destructor'''
        del self._hyperion
        del self._capture
        del self._captureState
        del self._data
        del self._useLegacyApi

    def useLegacyApi(self):
        '''check the xbmc API Version'''
        if self._useLegacyApi is None:
            try:
                self._capture.getCaptureState()
                self._useLegacyApi = True
            except Exception:
                self._useLegacyApi = False

        return self._useLegacyApi

    def getData(self):
        '''capture an image'''
        data = None
        if self.useLegacyApi():
            self._capture.waitForCaptureStateChangeEvent(200)
            self._captureState = self._capture.getCaptureState()
            if self._captureState == xbmc.CAPTURE_STATE_DONE:
                data = self._capture.getImage()
        else:
            data = self._capture.getImage()

        if not data:
            return

        # retrieve image data and reformat into rgb format
        if self._capture.getImageFormat() == 'ARGB':
            del data[0::4]
        elif self._capture.getImageFormat() == 'BGRA':
            del data[3::4]
            data[0::3], data[2::3] = data[2::3], data[0::3]

        return str(data)

    def execute(self):
        '''Execute the state
            - return: The new state to execute
        '''
        # check if we still need to grab
        if not self._settings.grabbing():
            # return to the disconnected state
            return DisconnectedState(self._settings)

        data = self.getData()
        if data:
            try:
                # send image to hyperion
                self._hyperion.sendImage(
                    self._capture.getWidth(),
                    self._capture.getHeight(),
                    data,
                    self._settings.priority,
                    500,
                )
            except Exception as exception:
                raise
                # unable to send image. notify and go to the error state
                # TODO: fix notification error
                # xbmc.notify(xbmcaddon.Addon().getLocalizedString(32101))
                misc.error('%(message)s %(exception)r',
                           message=xbmcaddon.Addon().getLocalizedString(32101),
                           exception=exception)
                return ErrorState(self._settings, exception)

        if self.useLegacyApi():
            if self._captureState != xbmc.CAPTURE_STATE_WORKING:
                # the current capture is processed or it has failed, we request
                # a new one
                self._capture.capture(self._settings.capture_width,
                                      self._settings.capture_height)

        # limit the maximum number of frames sent to hyperion
        xbmc.sleep(int(1. / self._settings.framerate * 1000))

        return self
Пример #5
0
class ConnectedState:
    '''
	State class when connected to Hyperion and grabbing video
	'''
    def __init__(self, settings):
        '''Constructor
			- settings: Settings structure
		'''
        log("Entering connected state")

        self.__settings = settings
        self.__hyperion = None
        self.__capture = None

        # try to connect to hyperion
        self.__hyperion = Hyperion(self.__settings.address,
                                   self.__settings.port)

        # create the capture object
        self.__capture = xbmc.RenderCapture()
        self.__capture.capture(64, 64)

    def __del__(self):
        '''Destructor
		'''
        del self.__hyperion
        del self.__capture

    def execute(self):
        '''Execute the state
			- return: The new state to execute
		'''
        # check if we still need to grab
        if not self.__settings.grabbing():
            # return to the disconnected state
            return DisconnectedState(self.__settings)

        # capture an image
        self.__capture.waitForCaptureStateChangeEvent(200)
        captureState = self.__capture.getCaptureState()
        if captureState == xbmc.CAPTURE_STATE_DONE:
            # retrieve image data and reformat into rgb format
            data = self.__capture.getImage()
            if self.__capture.getImageFormat() == 'ARGB':
                del data[0::4]
            elif self.__capture.getImageFormat() == 'BGRA':
                del data[3::4]
                data[0::3], data[2::3] = data[2::3], data[0::3]

            try:
                #send image to hyperion
                self.__hyperion.sendImage(self.__capture.getWidth(),
                                          self.__capture.getHeight(),
                                          str(data), self.__settings.priority,
                                          500)
            except Exception, e:
                # unable to send image. notify and go to the error state
                notify(xbmcaddon.Addon().getLocalizedString(32101))
                return ErrorState(self.__settings)

        if captureState != xbmc.CAPTURE_STATE_WORKING:
            #the current capture is processed or it has failed, we request a new one
            self.__capture.capture(64, 64)

        #limit the maximum number of frames sent to hyperion
        xbmc.sleep(100)

        return self
Пример #6
0
class ConnectedState:
    '''
    State class when connected to Hyperion and grabbing video
    '''

    def __init__(self, settings):
        '''Constructor
          - settings: Settings structure
        '''
        log("Entering connected state")

        self.__settings = settings
        self.__hyperion = None
        self.__capture = None
        self.__captureState = None
        self.__data = None

        # try to connect to hyperion
        self.__hyperion = Hyperion(self.__settings.address, self.__settings.port)

        # Force clearing of priority (mainly for Orbs)
        self.clear_priority()

        # create the capture object
        self.__capture = xbmc.RenderCapture()
        self.__capture.capture(self.__settings.capture_width, self.__settings.capture_height)

    def __del__(self):
        '''Destructor
        '''
        del self.__hyperion
        del self.__capture
        del self.__captureState
        del self.__data

    def clear_priority(self):
        # Force clearing of priority (mainly for Orbs)
        xbmc.sleep(1000)
        self.__hyperion.clear(self.__settings.priority)
        xbmc.sleep(1000)
        self.__hyperion.clear(self.__settings.priority)

    def execute(self):
        '''Execute the state
          - return: The new state to execute
        '''
        # check if we still need to grab
        if not self.__settings.grabbing():
            self.clear_priority()

            # return to the disconnected state
            return DisconnectedState(self.__settings)
        # capture an image
        startReadOut = False

        self.__data = self.__capture.getImage()
        if len(self.__data) > 0:
            startReadOut = True

        if startReadOut:
            # retrieve image data and reformat into rgb format
            if self.__capture.getImageFormat() == 'BGRA':
                del self.__data[3::4]
                self.__data[0::3], self.__data[2::3] = self.__data[2::3], self.__data[0::3]

            try:
                # send image to hyperion
                self.__hyperion.sendImage(self.__capture.getWidth(), self.__capture.getHeight(), str(self.__data),
                                          self.__settings.priority, -1)
            except Exception, e:
                # unable to send image. notify and go to the error state
                notify(xbmcaddon.Addon().getLocalizedString(32101))
                return ErrorState(self.__settings)
        else: