--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_media/utility.py Wed Sep 02 15:31:55 2015 +0200
@@ -0,0 +1,91 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_media.interfaces import IMediaConversionUtility, CONVERTER_HANDLER_KEY, CUSTOM_AUDIO_TYPES, \
+ CUSTOM_VIDEO_TYPES
+from pyams_utils.interfaces.zeo import IZEOConnection
+from zope.intid.interfaces import IIntIds
+
+# import packages
+from persistent import Persistent
+from pyams_utils.registry import get_utility
+from pyams_zmq.socket import zmq_socket, zmq_response
+from pyramid.threadlocal import get_current_registry
+from zope.container.contained import Contained
+from zope.interface import implementer
+from zope.schema.fieldproperty import FieldProperty
+
+
+@implementer(IMediaConversionUtility)
+class MediaConversionUtility(Persistent, Contained):
+ """Medias conversions utility"""
+
+ zeo_connection = FieldProperty(IMediaConversionUtility['zeo_connection'])
+
+ video_formats = FieldProperty(IMediaConversionUtility['video_formats'])
+ video_frame_size = FieldProperty(IMediaConversionUtility['video_frame_size'])
+ video_audio_sampling = FieldProperty(IMediaConversionUtility['video_audio_sampling'])
+ video_audio_bitrate = FieldProperty(IMediaConversionUtility['video_audio_bitrate'])
+ video_quantisation = FieldProperty(IMediaConversionUtility['video_quantisation'])
+
+ audio_formats = FieldProperty(IMediaConversionUtility['audio_formats'])
+
+ def check_media_conversion(self, media):
+ """Check if conversion is needed for given media"""
+ content_type = media.content_type.decode() if media.content_type else None
+ if self.audio_formats and \
+ (content_type.startswith('audio/') or (content_type in CUSTOM_AUDIO_TYPES)):
+ requested_formats = self.audio_formats
+ elif self.video_formats and \
+ (content_type.startswith('video/') or (content_type in CUSTOM_VIDEO_TYPES)):
+ requested_formats = self.video_formats
+ else:
+ requested_formats = ()
+ for format in requested_formats:
+ self.convert(media, format)
+
+ def _get_socket(self):
+ registry = get_current_registry()
+ handler = registry.settings.get(CONVERTER_HANDLER_KEY, False)
+ if handler:
+ return zmq_socket(handler)
+
+ def convert(self, media, format):
+ """Send conversion request for given media"""
+ socket = self._get_socket()
+ if socket is None:
+ return [501, "No socket handler defined in configuration file"]
+ if not self.zeo_connection:
+ return [502, "Missing ZEO connection"]
+ zeo = get_utility(IZEOConnection, self.zeo_connection)
+ intids = get_utility(IIntIds)
+ settings = {'zeo': zeo.get_settings(),
+ 'media': intids.register(media),
+ 'format': format}
+ socket.send_json(['convert', settings])
+ return zmq_response(socket)
+
+ def test_process(self):
+ """Send test request to conversion process"""
+ socket = self._get_socket()
+ if socket is None:
+ return [501, "No socket handler defined in configuration file"]
+ if not self.zeo_connection:
+ return [502, "Missing ZEO connection"]
+ socket.send_json(['test', {}])
+ return zmq_response(socket)