src/pyams_media/utility.py
changeset 0 fd39db613f8b
child 4 36c3db8c50a4
equal deleted inserted replaced
-1:000000000000 0:fd39db613f8b
       
     1 #
       
     2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 
       
    16 # import standard library
       
    17 
       
    18 # import interfaces
       
    19 from pyams_media.interfaces import IMediaConversionUtility, CONVERTER_HANDLER_KEY, CUSTOM_AUDIO_TYPES, \
       
    20     CUSTOM_VIDEO_TYPES
       
    21 from pyams_utils.interfaces.zeo import IZEOConnection
       
    22 from zope.intid.interfaces import IIntIds
       
    23 
       
    24 # import packages
       
    25 from persistent import Persistent
       
    26 from pyams_utils.registry import get_utility
       
    27 from pyams_zmq.socket import zmq_socket, zmq_response
       
    28 from pyramid.threadlocal import get_current_registry
       
    29 from zope.container.contained import Contained
       
    30 from zope.interface import implementer
       
    31 from zope.schema.fieldproperty import FieldProperty
       
    32 
       
    33 
       
    34 @implementer(IMediaConversionUtility)
       
    35 class MediaConversionUtility(Persistent, Contained):
       
    36     """Medias conversions utility"""
       
    37 
       
    38     zeo_connection = FieldProperty(IMediaConversionUtility['zeo_connection'])
       
    39 
       
    40     video_formats = FieldProperty(IMediaConversionUtility['video_formats'])
       
    41     video_frame_size = FieldProperty(IMediaConversionUtility['video_frame_size'])
       
    42     video_audio_sampling = FieldProperty(IMediaConversionUtility['video_audio_sampling'])
       
    43     video_audio_bitrate = FieldProperty(IMediaConversionUtility['video_audio_bitrate'])
       
    44     video_quantisation = FieldProperty(IMediaConversionUtility['video_quantisation'])
       
    45 
       
    46     audio_formats = FieldProperty(IMediaConversionUtility['audio_formats'])
       
    47 
       
    48     def check_media_conversion(self, media):
       
    49         """Check if conversion is needed for given media"""
       
    50         content_type = media.content_type.decode() if media.content_type else None
       
    51         if self.audio_formats and \
       
    52            (content_type.startswith('audio/') or (content_type in CUSTOM_AUDIO_TYPES)):
       
    53             requested_formats = self.audio_formats
       
    54         elif self.video_formats and \
       
    55            (content_type.startswith('video/') or (content_type in CUSTOM_VIDEO_TYPES)):
       
    56             requested_formats = self.video_formats
       
    57         else:
       
    58             requested_formats = ()
       
    59         for format in requested_formats:
       
    60             self.convert(media, format)
       
    61 
       
    62     def _get_socket(self):
       
    63         registry = get_current_registry()
       
    64         handler = registry.settings.get(CONVERTER_HANDLER_KEY, False)
       
    65         if handler:
       
    66             return zmq_socket(handler)
       
    67 
       
    68     def convert(self, media, format):
       
    69         """Send conversion request for given media"""
       
    70         socket = self._get_socket()
       
    71         if socket is None:
       
    72             return [501, "No socket handler defined in configuration file"]
       
    73         if not self.zeo_connection:
       
    74             return [502, "Missing ZEO connection"]
       
    75         zeo = get_utility(IZEOConnection, self.zeo_connection)
       
    76         intids = get_utility(IIntIds)
       
    77         settings = {'zeo': zeo.get_settings(),
       
    78                     'media': intids.register(media),
       
    79                     'format': format}
       
    80         socket.send_json(['convert', settings])
       
    81         return zmq_response(socket)
       
    82 
       
    83     def test_process(self):
       
    84         """Send test request to conversion process"""
       
    85         socket = self._get_socket()
       
    86         if socket is None:
       
    87             return [501, "No socket handler defined in configuration file"]
       
    88         if not self.zeo_connection:
       
    89             return [502, "Missing ZEO connection"]
       
    90         socket.send_json(['test', {}])
       
    91         return zmq_response(socket)