# fetchinfo.py - fetchinfo command
#
# Copyright 2007 Nokia
# Author: Daniel d'Andrada T. de Carvalho <daniel.carvalho@indt.org.br>
#
# This file is part of mediahandler-skeleton.
#
# mediahandler-skeleton is free software: you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# mediahandler-skeleton is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with mediahandler-proxy. If not, see <http://www.gnu.org/licenses/>.

from __future__ import with_statement

import subprocess
from command import Command
import threading
import shared

class FetchInfo(Command):

    __thumbnail_lock = threading.Lock()

    def __init__(self, sock):
        Command.__init__(self, 'fetch-info')

        self.sock = sock

        # Command parameters
        self.file_path = None
        self.thumbnail_path = None

        # Informartion gathered during execution
        self.video_codec = None
        self.audio_codec = "-"
        self.frame_rate = None
        self.width = None
        self.height = None
        self.length = None
        self.file_size = None
        self.video_bitrate = None

    def parse(self, command):

        params = command.split('|')

        if len(params) != 1 and len(params) != 2:
            return False

        self.file_path = params[0]

        if len(params) == 2:
            self.thumbnail_path = params[1]

        return True

    def execute(self):
        print 'thumbnail path:', self.thumbnail_path

        # Replace with real code here
        self.video_codec = "MPEG4"
        self.audio_codec = "MP3"
        self.frame_rate = "25"
        self.width = "320"
        self.height = "240"
        self.length = "123"
        self.file_size = "4000000"
        self.video_bitrate = "256000"

        self._send_back_media_info()

        if self.thumbnail_path:
            self._make_thumbnail()

        return 'ok'

    def _send_back_media_info(self):
        if not self.sock:
            return

        if self.video_codec:
            self.sock.send('<video-codec>' + self.video_codec + '</video-codec>')

        if self.audio_codec:
            self.sock.send('<audio-codec>' + self.audio_codec + '</audio-codec>')

        if self.frame_rate:
            self.sock.send('<fps>' + self.frame_rate + '</fps>')

        if self.width:
            self.sock.send('<width>' + self.width + '</width>')

        if self.height:
            self.sock.send('<height>' + self.height + '</height>')

        if self.length:
            self.sock.send('<length>' + self.length + '</length>')

        if self.file_size:
            self.sock.send('<file-size>' + self.file_size + '</file-size>')

        if self.video_bitrate:
            self.sock.send('<video-bit-rate>' + self.video_bitrate + '</video-bit-rate>')

    def _make_thumbnail(self):

        with FetchInfo.__thumbnail_lock:
            # We don't want a console window to popup
            # This startupinfo stuff will to the trick
            startupinfo = subprocess.STARTUPINFO()
            startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

            subprocess.call(r'copy /Y "%s" "%s"' %\
                    (shared.thumbnail_filepath, self.thumbnail_path),
                    shell=True,
                    startupinfo=startupinfo)
