Project

General

Profile

Statistics
| Branch: | Revision:

gdp-if / gstreamer-log-plugin / gdpsrc.py @ master

History | View | Annotate | Download (3.12 KB)

1 6da8df3b Nitesh Mor
#!/usr/bin/env python
2
3
import gi
4
gi.require_version('Gst', '1.0')
5
gi.require_version('GstBase', '1.0')
6
7
from gi.repository import GObject, Gst, GstBase
8 2cef81c6 Nitesh Mor
9
import os
10
import gdp
11
import sys
12 3fc93a78 Nitesh Mor
import cPickle
13 6da8df3b Nitesh Mor
14
GObject.threads_init()
15
Gst.init(None)
16
17 b87469aa Nitesh Mor
class GDPSrc(GstBase.BaseSrc):
18 cd949eab Nitesh Mor
19 b87469aa Nitesh Mor
    __gtype_name__ = "GDPSrc"
20 cd949eab Nitesh Mor
    __gstmetadata__ = ('Global Data Plane Source',
21
                        'GDPSrc',
22
                        'A source element to read data from a GDP log',
23
                        'Nitesh Mor')
24 6da8df3b Nitesh Mor
    __gsttemplates__ = Gst.PadTemplate.new("src",
25
                                            Gst.PadDirection.SRC,
26
                                            Gst.PadPresence.ALWAYS,
27
                                            Gst.Caps.new_any())
28
29 f2c0470b Nitesh Mor
    __gproperties__ = {
30
                "logname": (str, "GDP Logname",
31
                           "Name of GDP log that we should read from",
32
                           "logname_not_set",
33
                            GObject.PARAM_READWRITE),
34
                      }
35
36 6da8df3b Nitesh Mor
    def __init__(self, *args):
37 76a7530d Nitesh Mor
38 6da8df3b Nitesh Mor
        print("initializing")
39 76a7530d Nitesh Mor
40 6da8df3b Nitesh Mor
        GstBase.BaseSrc.__init__(self, *args)
41
        self.set_format(Gst.Format.BUFFERS)
42 3fc93a78 Nitesh Mor
        self.set_blocksize(65000)
43 76a7530d Nitesh Mor
44 5d929576 Nitesh Mor
        self.logname = None
45
        self.lh = None
46 3fc93a78 Nitesh Mor
47 76a7530d Nitesh Mor
        self.cap = None
48
        self.buflist = []
49 46afd724 Nitesh Mor
50 f2c0470b Nitesh Mor
    def do_set_property(self, prop, value):
51
        if prop.name == "logname":
52
            self.logname = value
53
        else:
54
            raise AttributeError
55
56
    def do_get_property(self, prop):
57
        if prop.name == "logname":
58
            return self.logname
59
        else:
60
            raise AttributeError
61 5d929576 Nitesh Mor
62 46afd724 Nitesh Mor
    def do_start(self):
63
        print "do_start"
64 f2c0470b Nitesh Mor
65 5d929576 Nitesh Mor
        self.lh = gdp.GDP_GCL(gdp.GDP_NAME(self.logname), gdp.GDP_MODE_RO)
66
        self.lh.subscribe(1, 0, None)
67
68 46afd724 Nitesh Mor
        new_event = Gst.Event.new_stream_start("some-stream")
69
        self.send_event(new_event)
70
        return True
71
72 5d929576 Nitesh Mor
73 46afd724 Nitesh Mor
    def do_get_caps(self, caps):
74 76a7530d Nitesh Mor
        print "do_get_caps", caps, self.cap
75
        return self.cap
76 46afd724 Nitesh Mor
77 5d929576 Nitesh Mor
78 46afd724 Nitesh Mor
    def do_event(self, event):
79 76a7530d Nitesh Mor
        print "do_event", event, event.type
80 46afd724 Nitesh Mor
        return True
81 6da8df3b Nitesh Mor
82 5d929576 Nitesh Mor
83 6da8df3b Nitesh Mor
    def do_fill(self, offset, size, buf):
84
85 46afd724 Nitesh Mor
        data = self.__load_data()
86 76a7530d Nitesh Mor
        if data is None:
87
            return Gst.FlowReturn.EOS
88 6da8df3b Nitesh Mor
89 3dcfe5d7 Nitesh Mor
        assert buf.get_size()>=len(data)
90 46afd724 Nitesh Mor
        buf.resize(0, len(data))
91
        ret = buf.fill(0, data)
92 6da8df3b Nitesh Mor
        return Gst.FlowReturn.OK
93
94 988f5ab4 Nitesh Mor
95 76a7530d Nitesh Mor
    def __load_data(self):
96 6da8df3b Nitesh Mor
97 76a7530d Nitesh Mor
        timeout = {'tv_sec':1, 'tv_nsec':0, 'tv_accuracy':0.0}
98 988f5ab4 Nitesh Mor
99 76a7530d Nitesh Mor
        if self.cap is None or len(self.buflist)==0:
100 988f5ab4 Nitesh Mor
101 76a7530d Nitesh Mor
            event = self.lh.get_next_event(timeout)
102 2cef81c6 Nitesh Mor
            if event is None:
103 76a7530d Nitesh Mor
                return None
104
105 3fc93a78 Nitesh Mor
            datum = event["datum"]
106 76a7530d Nitesh Mor
            (__cap, self.buflist) = cPickle.loads(datum["data"])
107
            self.cap = Gst.Caps.from_string(__cap)
108
            self.set_caps(self.cap)
109 3fc93a78 Nitesh Mor
110 1be200b4 Nitesh Mor
        if len(self.buflist)>0:
111
            data = self.buflist[0]
112
            self.buflist = self.buflist[1:]
113
            return data
114
        else:
115
            return self.__load_data()
116 988f5ab4 Nitesh Mor
117
118 b87469aa Nitesh Mor
Gst.Element.register(None, "gdpsrc", Gst.Rank.NONE, GDPSrc)
119 6da8df3b Nitesh Mor
120
if __name__ == '__main__':
121
122 f2c0470b Nitesh Mor
    sys.stderr.write("Use gst-launch-wrapper, do not use directly!")
123
    sys.exit(-1)