Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sc_gst.py
blob: 192afe95b790a7a010795f24104609c573ab0266 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# -*- coding: utf-8 -*-

# Copyright 2012 Activity Central, Inc., Ariel Calzada - ariel@activitycentral.com
# Copyright 2012 Activity Central, Inc., Flavio Danesse - flavio@activitycentral.com
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# gobject used for subclassing and finally for managing signals
import gobject

# gstreamer
import gst

# os
import os

# time lib used for saving time of video
import time

class ScreencastGstreamer(gobject.GObject):

    __gsignals__ = {
    "update-info": (gobject.SIGNAL_RUN_FIRST,
        gobject.TYPE_NONE, (gobject.TYPE_STRING,gobject.TYPE_STRING,))
    }

    def __init__(self, videofile):
        """ Constructor
        """
        
        super(ScreencastGstreamer, self).__init__()
        
        self._audioitems = False
        self._startclock = False
        self._videofile = videofile
        self._pipeline = False
        self._timer = False
        self._oggmux = False
        self._videoitems = False
        self._filesink = False

    def stop(self):
        """ Stop recording
        """
        
        if self._timer:
            gobject.source_remove(self._timer)
            self._timer = False
            
        self._pipeline.set_state(gst.STATE_PAUSED)
        self._pipeline.set_state(gst.STATE_NULL)

    def start(self, recordsound):
        """ Start recording
        """
        
        if self._pipeline:
            del(self._pipeline)
            
        if self._timer:
            gobject.source_remove(self._timer)
            self._timer = None
            
        self.createPipeline()
        
        self.addItems(recordsound)
        self.linkItems(recordsound)
        
        self._pipeline.set_state(gst.STATE_PLAYING)
        self._startclock = time.time()
        self._timer = gobject.timeout_add(1000, self.updateInfo)

    def addItems(self, recordsound):
        """ Add items to pipeline
        """

        for videoitem in self._videoitems:
            self._pipeline.add(videoitem)

        if recordsound:
            for audioitem in self._audioitems:
                self._pipeline.add(audioitem)

        self._pipeline.add(self._oggmux)
        self._pipeline.add(self._filesink)

    def linkItems(self, recordsound):
        """ Link items to pipeline
        """

        if recordsound:
            l1 = self._videoitems
            l1.append(self._oggmux)
            gst.element_link_many(*l1)

            l2 = self._audioitems
            l2.append(self._oggmux)
            gst.element_link_many(*l2)

            gst.element_link_many(
                self._oggmux,
                self._filesink
            )

        else:
            l = self._videoitems
            l.append(self._oggmux)
            l.append(self._filesink)
            gst.element_link_many(*l)


    def updateInfo(self):
        """ Update info
        """
        
        videofilesize = "0 Kb"
        videolength = "%s s" % ( int(time.time() - self._startclock))

        if os.path.exists(self._videofile):
            videofilesize = "%s Kb" % ( int(int(os.path.getsize(self._videofile)) / 1024))

        self.emit('update-info', videofilesize, videolength)

        return True


    def createVideoItems(self):
        """ Create video items
        """
        
        ximagesrc = gst.element_factory_make('ximagesrc', "ximagesrc")
        ximagesrc.set_property('startx', 0)
        ximagesrc.set_property('endx', 1200)
        ximagesrc.set_property('starty', 0)
        ximagesrc.set_property('endy', 900)

        ffmpegcolorspace = gst.element_factory_make('ffmpegcolorspace', "ffmpegcolorspace")

        videoscale = gst.element_factory_make("videoscale", "videoscale")

        videocaps = gst.element_factory_make("capsfilter", "videocapsfilter")
        videocaps.set_property("caps", gst.Caps("video/x-raw-yuv,width=640,height=480"))

        theoraenc = gst.element_factory_make('theoraenc', 'theoraenc')

        videoqueue = gst.element_factory_make('queue', "videoqueue")
        videoqueue.set_property('max-size-buffers', 8000)
        videoqueue.set_property('max-size-bytes', 0)
        videoqueue.set_property('max-size-time', 0)

        self._videoitems = [ ximagesrc, ffmpegcolorspace, videoscale, videocaps, theoraenc, videoqueue ]

    def createAudioItems(self):
        """ Create audio items
        """

        autoaudiosrc = gst.element_factory_make('autoaudiosrc', "autoaudiosrc")

        audiorate = gst.element_factory_make("audiorate", "audiorate")
        audiorate.set_property('silent', True)
        audiorate.set_property('tolerance', 1000)

        audiocaps = gst.element_factory_make("capsfilter", "audiocapsfilter")
        audiocaps.set_property("caps", gst.Caps("audio/x-raw-int,rate=8000,channels=1,width=16,depth=16"))

        audioconvert = gst.element_factory_make('audioconvert', "audioconvert")

        vorbisenc = gst.element_factory_make('vorbisenc', "vorbisenc")

        soundqueue = gst.element_factory_make('queue', "audioqueue")
        soundqueue.set_property('max-size-buffers', 10000)
        soundqueue.set_property('max-size-bytes', 0)
        soundqueue.set_property('max-size-time', 0)

        self._audioitems = [ autoaudiosrc, audiorate, audiocaps, audioconvert, vorbisenc, soundqueue ]

    def createPipeline(self):
        """ Create pipeline

            gst-launch-0.10 ximagesrc startx=0 endx=1200 starty=0 endy=900 ! \
               ffmpegcolorspace ! \
               videoscale ! \
               video/x-raw-yuv,width=640,height=480 ! \
               theoraenc ! \
               queue max-size-buffers=10000 max-size-bytes=0 max-size-time=0 ! \
               mux. oggmux name=mux \
               autoaudiosrc ! \
               audiorate silent=True tolerance=1000 ! \
               audio/x-raw-int,width=16,depth=16,rate=8000,channels=1 ! \
               audioconvert ! \
               vorbisenc ! \
               queue max-size-buffers=10000 max-size-bytes=0 max-size-time=0 ! \
               mux. mux. ! \
               filesink location=video.ogg
        """
        
        self._pipeline = gst.Pipeline("player")
        
        self.createVideoItems()
        self.createAudioItems()
        
        self._oggmux = gst.element_factory_make('oggmux', "oggmux")
        self._filesink = gst.element_factory_make('filesink', "filesink")
        self._filesink.set_property("location", self._videofile)