diff --git a/ca.py b/ca.py new file mode 100755 index 00000000..95cea342 --- /dev/null +++ b/ca.py @@ -0,0 +1,279 @@ +#!/bin/env python +# -*- coding: utf-8 -*- +''' + python-matterhorn-ca + ~~~~~~~~~~~~~~~~~~~~ + + :copyright: 2013, Lars Kiesow + :license: FreeBSD and LGPL, see license.* for more details. +''' + +# Set default encoding to UTF-8 +import sys +reload(sys) +sys.setdefaultencoding('utf8') + +import os +import time +import urllib +import urllib2 +import dateutil.tz +from xml.dom.minidom import parseString +from base64 import b64decode +import logging +import icalendar +from datetime import datetime +import os.path + +ignore_tz = False +admin_server_url = 'http://localhost:8080' +admin_server_user = 'digestadmin' +admin_server_passwd = 'opencast' +update_frequency = 60 +capture_dir = '/home/pi/recordings/' + + +def get_url_opener(): + mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() + mgr.add_password(None, admin_server_url, admin_server_user, + admin_server_passwd) + return urllib2.build_opener(urllib2.HTTPBasicAuthHandler(mgr), + urllib2.HTTPDigestAuthHandler(mgr)) + + + +def register_ca(address='http://localhost:8080',status='idle'): + params = {'address':address, 'state':status} + req = urllib2.Request('%s/capture-admin/agents/pica' % admin_server_url, + urllib.urlencode(params)) + req.add_header('X-Requested-Auth', 'Digest') + + u = get_url_opener().open(req) + print u.read() + u.close() + + +def recording_state(rid, status='upcoming'): + params = {'state':status} + req = urllib2.Request('%s/capture-admin/recordings/%s' % \ + (admin_server_url, rid), + urllib.urlencode(params)) + req.add_header('X-Requested-Auth', 'Digest') + + u = get_url_opener().open(req) + print u.read() + u.close() + + +def get_schedule(): + req = urllib2.Request('%s/recordings/calendars?agentid=pica' % \ + admin_server_url) + req.add_header('X-Requested-Auth', 'Digest') + + u = get_url_opener().open(req) + vcal = u.read() + u.close() + + cal = None + try: + cal = icalendar.Calendar.from_string(vcal) + except: + cal = icalendar.Calendar.from_ical(vcal) + events = [] + for event in cal.walk('vevent'): + dtstart = unix_ts(event.get('dtstart').dt.astimezone(dateutil.tz.tzutc())) + dtend = unix_ts(event.get('dtend').dt.astimezone(dateutil.tz.tzutc())) + uid = event.get('uid').decode() + + # Ignore events that have already ended + if dtend > get_timestamp(): + events.append( (dtstart,dtend,uid,event) ) + + return sorted(events, key=lambda x: x[0]) + + +def unix_ts(dt): + epoch = datetime(1970, 1, 1, 0, 0, tzinfo = dateutil.tz.tzutc()) + delta = (dt - epoch) + return delta.days * 24 * 3600 + delta.seconds + + +def get_timestamp(): + if ignore_tz: + return unix_ts(datetime.now()) + return unix_ts(datetime.now(dateutil.tz.tzutc())) + + +def get_config_params(properties): + param = '' + wdef = 'full' + for prop in properties.split('\n'): + if prop.startswith('org.opencastproject.workflow.config'): + k,v = prop.split('=',1) + k = k.split('.')[-1] + param += '-F "%s=%s" ' % (k, v) + elif prop.startswith('org.opencastproject.workflow.definition'): + wdef = prop.split('=',1)[-1] + return wdef, param + + +def default_recording_command(rec_dir, rec_name, rec_duration): + rec_cmd = ('raspivid -t %(duration)i000 -n -b 4000000 -hf -vf -fps 25 -o - | ' \ + + 'ffmpeg ' \ + + '-f alsa -i hw:2 ' \ + + '-r 25 -i pipe:0 -c:v copy -t %(duration)i ' \ + + '"%(rec_dir)s/%(rec_name)s.mkv"') % \ + { 'duration':rec_duration, + 'rec_dir':rec_dir, + 'rec_name':rec_name} + print rec_cmd + os.system(rec_cmd) + return [('presenter/source', '%s/%s.mkv' % (rec_dir,rec_name))] + + +recording_command = default_recording_command + + +def start_capture(schedule): + now = get_timestamp() + print '%i: start_recording...' % now + duration = schedule[1] - now + recording_id = schedule[2] + recording_name = 'recording-%s-%i' % (recording_id, now) + recording_dir = '%s/%s' % (capture_dir, recording_name) + os.mkdir(recording_dir) + + # Set state + register_ca(status='capturing') + recording_state(recording_id,'capturing') + + ''' + rec_cmd = ('raspivid -t %(duration)i000 -n -vf -o - | ' \ + + 'ffmpeg -i silence.mkv -acodec copy ' \ + + '-r 30 -i pipe:0 -vcodec copy -t %(duration)i ' \ + + '"%(rec_dir)s/%(rec_name)s.mkv"') % \ + { 'duration':duration, + 'rec_dir':recording_dir, + 'rec_name':recording_name} + os.system(rec_cmd) + ''' + tracks = recording_command(recording_dir, recording_name, duration) + + # Put metadata files on disk + attachments = schedule[-1].get('attach') + workflow_config='' + for a in attachments: + value = b64decode(a.decode()) + if value.startswith('<'): + if '' in value: + f = open('%s/episode.xml' % recording_dir, 'w') + f.write(value) + f.close() + else: + f = open('%s/series.xml' % recording_dir, 'w') + f.write(value) + f.close() + else: + workflow_def, workflow_config = get_config_params(value) + f = open('%s/recording.properties' % recording_dir, 'w') + f.write(value) + f.close() + + # Upload everything + register_ca(status='uploading') + recording_state(recording_id,'uploading') + + rec_data = { + 'user':admin_server_user, + 'passwd':admin_server_passwd, + 'url':admin_server_url, + 'rec_dir':recording_dir, + 'rec_name':recording_name, + 'rec_id':recording_id, + 'workflow_def':workflow_def, + 'workflow_config':workflow_config } + + # create mediapackage + curlcmd = ('curl -f --digest -u %(user)s:%(passwd)s ' \ + + '-H "X-Requested-Auth: Digest" ' \ + + '"%(url)s/ingest/createMediaPackage" ' \ + + '-o "%(rec_dir)s/mediapackage.xml"') % rec_data + print curlcmd + os.system(curlcmd) + + # add episode dc catalog + if os.path.isfile('%s/episode.xml' % recording_dir): + curlcmd = ('curl -f --digest -u %(user)s:%(passwd)s ' \ + + '-H "X-Requested-Auth: Digest" "%(url)s/ingest/addDCCatalog" ' \ + + '-F "mediaPackage=<%(rec_dir)s/mediapackage.xml" ' \ + + '-F "flavor=dublincore/episode" ' \ + + '-F "dublinCore=@%(rec_dir)s/episode.xml" ' \ + + '-o "%(rec_dir)s/mediapackage-new.xml"') % rec_data + print curlcmd + os.system(curlcmd) + os.rename('%s/mediapackage-new.xml' % recording_dir, + '%s/mediapackage.xml' % recording_dir) + + # add series dc catalog + if os.path.isfile('%s/series.xml' % recording_dir): + curlcmd = ('curl -f --digest -u %(user)s:%(passwd)s ' \ + + '-H "X-Requested-Auth: Digest" "%(url)s/ingest/addDCCatalog" ' \ + + '-F "mediaPackage=<%(rec_dir)s/mediapackage.xml" ' \ + + '-F "flavor=dublincore/series" ' \ + + '-F "dublinCore=@%(rec_dir)s/series.xml" ' \ + + '-o "%(rec_dir)s/mediapackage-new.xml"') % rec_data + print curlcmd + os.system(curlcmd) + os.rename('%s/mediapackage-new.xml' % recording_dir, + '%s/mediapackage.xml' % recording_dir) + + # add track + for (flavor, track) in tracks: + track_data = rec_data.copy() + track_data['flavor'] = flavor + track_data['track'] = track + curlcmd = ('curl -f --digest -u %(user)s:%(passwd)s ' \ + + '-H "X-Requested-Auth: Digest" "%(url)s/ingest/addTrack" ' \ + + '-F "flavor=%(flavor)s" ' \ + + '-F "mediaPackage=<%(rec_dir)s/mediapackage.xml" ' \ + + '-F "BODY1=@%(track)s" ' \ + + '-o "%(rec_dir)s/mediapackage-new.xml"') % track_data + print curlcmd + os.system(curlcmd) + os.rename('%s/mediapackage-new.xml' % recording_dir, + '%s/mediapackage.xml' % recording_dir) + + # ingest + curlcmd = ('curl -f --digest -u %(user)s:%(passwd)s ' \ + + '-H "X-Requested-Auth: Digest" "%(url)s/ingest/ingest" ' \ + + '-F "mediaPackage=<%(rec_dir)s/mediapackage.xml" ' \ + + '-F "workflowDefinitionId=%(workflow_def)s" ' \ + + '-F "workflowInstanceId=%(rec_id)s" ' \ + + '%(workflow_config)s ' \ + + '-o "%(rec_dir)s/worflowInstance.xml"') % rec_data + print curlcmd + os.system(curlcmd) + + # Update state + recording_state(recording_id,'upload_finished') + register_ca(status='idle') + + +def control_loop(): + last_update = 0 + schedule = [] + while True: + if len(schedule) and schedule[0][0] <= get_timestamp(): + start_capture(schedule[0]) + if get_timestamp() - last_update > update_frequency: + schedule = get_schedule() + last_update = get_timestamp() + print '%i: updated schedule' % get_timestamp() + print ' > starting timestamps: ', [ x[0] for x in schedule ] + time.sleep(1.0) + + +if __name__ == '__main__': + register_ca() + print get_schedule() + control_loop() diff --git a/license.bsd b/license.bsd new file mode 100644 index 00000000..a8e18799 --- /dev/null +++ b/license.bsd @@ -0,0 +1,31 @@ + +Copyright 2011 Lars Kiesow. All rights reserved. +http://www.larskiesow.de + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +THE POSSIBILITY OF SUCH DAMAGE. + +The views and conclusions contained in the software and documentation +are those of the authors and should not be interpreted as representing +official policies, either expressed or implied, of everyone working on +this project. diff --git a/license.lgpl b/license.lgpl new file mode 100644 index 00000000..65c5ca88 --- /dev/null +++ b/license.lgpl @@ -0,0 +1,165 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + + This version of the GNU Lesser General Public License incorporates +the terms and conditions of version 3 of the GNU General Public +License, supplemented by the additional permissions listed below. + + 0. Additional Definitions. + + As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the GNU +General Public License. + + "The Library" refers to a covered work governed by this License, +other than an Application or a Combined Work as defined below. + + An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + + A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + + The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + + The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + + 1. Exception to Section 3 of the GNU GPL. + + You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + + 2. Conveying Modified Versions. + + If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + + a) under this License, provided that you make a good faith effort to + ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or + + b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + + 3. Object Code Incorporating Material from Library Header Files. + + The object code form of an Application may incorporate material from +a header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + + a) Give prominent notice with each copy of the object code that the + Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the object code with a copy of the GNU GPL and this license + document. + + 4. Combined Works. + + You may convey a Combined Work under terms of your choice that, +taken together, effectively do not restrict modification of the +portions of the Library contained in the Combined Work and reverse +engineering for debugging such modifications, if you also do each of +the following: + + a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. + + b) Accompany the Combined Work with a copy of the GNU GPL and this license + document. + + c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. + + d) Do one of the following: + + 0) Convey the Minimal Corresponding Source under the terms of this + License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + + 1) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (a) uses at run time + a copy of the Library already present on the user's computer + system, and (b) will operate properly with a modified version + of the Library that is interface-compatible with the Linked + Version. + + e) Provide Installation Information, but only if you would otherwise + be required to provide such information under section 6 of the + GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the + Application with a modified version of the Linked Version. (If + you use option 4d0, the Installation Information must accompany + the Minimal Corresponding Source and Corresponding Application + Code. If you use option 4d1, you must provide the Installation + Information in the manner specified by section 6 of the GNU GPL + for conveying Corresponding Source.) + + 5. Combined Libraries. + + You may place library facilities that are a work based on the +Library side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + + a) Accompany the combined library with a copy of the same work based + on the Library, uncombined with any other library facilities, + conveyed under the terms of this License. + + b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + + 6. Revised Versions of the GNU Lesser General Public License. + + The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + + Each version is given a distinguishing version number. If the +Library as you received it specifies that a certain numbered version +of the GNU Lesser General Public License "or any later version" +applies to it, you have the option of following the terms and +conditions either of that published version or of any later version +published by the Free Software Foundation. If the Library as you +received it does not specify a version number of the GNU Lesser +General Public License, you may choose any version of the GNU Lesser +General Public License ever published by the Free Software Foundation. + + If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/readme.mk b/readme.mk new file mode 100644 index 00000000..6a29cce0 --- /dev/null +++ b/readme.mk @@ -0,0 +1,40 @@ +PiCA – Matterhorn Capture Agent +=============================== + +**PiCA** is a [Opencast Matterhorn][1] Capture Agent based on the [Raspberry Pi][2] +mini computer and its recently released [camera module][3]. It has a Full HD +(1080p30) camera, a hardware h264 encoder, extensible storage (SD Card/USB +storage), a network connector, etc. and is easily adapted to suit one's needs +as it's running on Linux (among others). + +[1]: http://opencast.org/matterhorn +[2]: http://raspberrypi.org +[3]: http://www.raspberrypi.org/archives/3890 + + +Features +-------- + +* Hardware costs only about **100$**! +* Full HD recording (1080p30) +* Hardware h264 encoder +* Free Open Source Software (BSD/LGPL) +* Code easily maintainable (less than 300 lines of Python for the whole CA) + + +Todo +---- + +* Improve audio recording +* Confidence monitoring +* Control page (manually start a recording, etc.) +* Make installation easier + * Write a installation script + * Provide a operating system image +* Create daemon mode + + +More Information +---------------- + +Visit: http://larskiesow.de/pyCA/piCA