#!/usr/bin/env python3
"""
ingest.py: source client for Voctomix.

   Copyright: 2015-2020 Carl F. Karsten <carl@nextdayvideo.com>,
                        Ryan Verner <ryan@nextdayvideo.com.au>

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to
   deal in the Software without restriction, including without limitation the
   rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
   sell copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.


Features:
    Retrieves audio and video-caps config from core.
      and client config
    Uses core's clock.
    Mix and match audio and video sources muxed into one stream.
    Can display video locally, including frame count and fps.
    Defaults to test audio and video sent to local core.
"""

import argparse
from pprint import pprint
import socket
import sys

import gi
gi.require_version('Gst', '1.0')
gi.require_version('GstNet', '1.0')
from gi.repository import Gst, GstNet, GObject, GLib

from lib.connection import Connection


def mk_video_src(args, videocaps):
    # make video source part of pipeline
    # args come from command line
    # videocaps come from voctocore

    if args.video_source == 'dv':
        video_src = """
            dv1394src name=videosrc {attribs} !
        dvdemux name=demux !
        queue max-size-time=4000000000 !
        dvdec !
            """

    elif args.video_source == 'hdv':
        video_src = """
            hdv1394src {attribs} name=videosrc !
        tsdemux !
        queue max-size-time=4000000000 !
        decodebin !
            """

    elif args.video_source == 'hdmi2usb':
        # https://hdmi2usb.tv
        # Note: this code works with 720p
        video_src = """
            v4l2src {attribs} name=videosrc !
                queue max-size-time=4000000000 !
        image/jpeg,width=1280,height=720 !
                jpegdec !
            """

    elif args.video_source == 'ximage':
        # startx=0 starty=0 endx=1919 endy=1079 !
        video_src = """
            ximagesrc {attribs} name=videosrc
                   use-damage=false !
            """

    elif args.video_source == 'blackmagic':
        video_src = """
            decklinkvideosrc name=videosrc {attribs} !
            """

    elif args.video_source == 'png':
        video_src = """
            multifilesrc {attribs}
                caps="image/png" !
            pngdec !
            """
        # tip: --video-attribs "loop=true"
        # tip: --video-attribs "location=sc1204a.png"

    elif args.video_source == 'file':
        video_src = """
            multifilesrc {attribs} !
            decodebin name=src
            src. !
            queue !
            """

    elif args.video_source == 'test':

        video_src = """
    videotestsrc name=videosrc {attribs} !
    """
        # things to render as text ontop of test video
        video_src += """
    clockoverlay
        text="Source: {hostname}\nCaps: {videocaps}\nAttribs: {attribs}\n"
        halignment=left line-alignment=left !
            """.format(hostname=socket.gethostname(),
                    videocaps=videocaps,
                    attribs=args.video_attribs)

    elif args.video_source == 'spacescope':
        # Stereo visualizer
        # pair up with test beep for a handy AV sync test.
        video_src = """
      audio_tee. ! queue !
    spacescope shader=none style=lines {attribs} !
           """