"""Experiment with omxplayer-wrapper."""
import sys
from time import sleep
from omxplayer.player import OMXPlayer

##  List of cameras.
CAMERAS = [
    '10.6.10.161',
    '10.6.10.162'
]

##  Video (upper left corner) in pixels.
POS = (480,0)

##  Video size in pixels.
SIZE = (960,540)

##
#   Arguments for OMXPlayer.
#
#   TCP transport seems to be what works for the ZCam E2 cameras. This should
#   probably be investigated more carefully. UDP would be much more efficient.
#
#   Presumably --live means that it displays each frame as it comes in, however
#   that should be checked.
#
#   "--aidx -1" effectively turns off audio processing.
#
E2_STREAMING_ARGS = [
    '--avdict', 'rts_transport:tcp',
    '--live',
    '--aidx', '-1'
    ]

##
#   A video window.
#
#   This uses OMXPlayer as the display mechanism. OMXPlayer uses the GPU
#   to minimize CPU load. The caller provides the URL for the video stream,
#   e.g. rtsp://...
#
class VideoWindow:
    ##
    #   Constructor.
    #
    #   @param  self        Object pointer.
    #   @param  size        (x,y) Size of window in pixels.
    #   @param  position    (x,y) Screen position of upper left corner in pixels.
    #
    def __init__(self, size, position):
        self._rtsp_url = ''
        self._player = None
        self._ul_x = position[0]
        self._ul_y = position[1]
        self._lr_x = self._ul_x + size[0]
        self._lr_y = self._ul_y + size[1]
        self._streaming_args = []

    ##
    #   Set OMXPlayer arguments.
    #
    #   Set arguments that are appropriate for the camera or the application.
    #
    #   @param  self        Object pointer.
    #   @param  args        (list) Arguments.
    #
    #   @remark Anything that would be separated by a space on the command line
    #           must be a separate list item for "args".
    #
    def set_streaming_args(self, args):
        self._streaming_args = args

    ##
    #   If the video window is active.
    #
    #   @param  self        Object pointer.
    #
    def is_running(self):
        return self._player is not None

    ##
    #   Start the video window or switch to a different camera stream.
    #
    #   @param  self        Object pointer.
    #   @param  url         URL of camera stream to display.
    #
    def go(self, url):
        self._rtsp_url = url
        self.stop()
        self._start()

    ##
    #   Start the video window.
    #   (Private method.)
    #
    #   @param  self        Object pointer.
    #   @throws AssertionException  Video window is already running.
    #
    def _start(self):
        assert(not self.is_running())

        try:
            self._player = OMXPlayer(self._rtsp_url, args=self._streaming_args)
            self._player.set_video_pos(self._ul_x, self._ul_y, self._lr_x, self._lr_y)
            self._player.set_aspect_mode('letterbox')
        except Exception as err:
            self._player = None
            print('exception:', err)

    ##
    #   Stop the video window.
    #
    #   @param  self        Object pointer.
    #
    def stop(self):
        if self.is_running():
            self._player.quit()
            self._player = None


##  Main loop.
def main(video):
    while 1:
        for camera_ip in CAMERAS:
            input('ENTER for next camera, CTRL-D to exit\n')
            print('connecting', camera_ip)
            url = f'rtsp://{camera_ip}/live_stream'
            print(url)
            video.go(url)

        input('ENTER to stop video, CTRL-D to exit\n')
        print('disconnecting')
        video.stop()


if __name__ == '__main__':
    video = VideoWindow(size=SIZE, position=POS)
    video.set_streaming_args(E2_STREAMING_ARGS)
    try:
        main(video)
    except EOFError as err:
        print('exiting')
    finally:
        video.stop()
