Present Perfect


Picture Gallery
Present Perfect

python and GStreamer questions

Filed under: General,Python — Thomas @ 11:21 pm

11:21 pm

Josep had a GStreamer problem last Friday and really really wanted to bug me about it because everyone else he usually bugs about them is gone :) Julien was at IBC, Andy is doing an Andy, Mike is in the US, Zaheer is in London, and Jan is in Ireland. All with varying degrees of longetivity and permanence.

Now, I’ve hardly done any code with GStreamer for a while, but I helped him trawl through the log file he gave me until we found a likely symptom for his problem. He went away again then came back later asking me to check his theory about the bug.

Not to bore you with details, but it boiled down to a simple question he had: does gst_buffer_create_sub() set the master buffer’s caps on the subbuffers it creates or not ?

Well, first of all, I have no idea. Second, why does he ask me, he’s been working here for a year now so he should be able to find his way around GStreamer – but that’s a post for a different day. Third, it’s likely that the answer is in the documentation.

But most of all, why don’t you just give it a try ?

He looked at me, puzzled, so I fired up an ipython shell. He said “Well, I don’t know Python”, and I replied “then it’s time you give it a try, because you’re about to get the answer to the question you asked me”.

I could finish of the post here and lead you to believe that it’s really easy to figure out the answer using ipython, but I’m guessing there might be more people out there who need a simple example to realize the power of trying things in ipython. So here goes – I’m pasting my session verbatim, mistakes and all, with comments in italics.

[thomas@level ~]$ ipython
import Python 2.5 (r25:51908, Apr 10 2007, 10:27:40)
Type "copyright", "credits" or "license" for more information.

IPython 0.7.2 -- An enhanced Interactive Python.
? -> Introduction to IPython's features.
%magic -> Information about IPython's 'magic' % functions.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: import gst

I’d like to do things with GStreamer, please.

In [2]: gst = gst.Bu

Let’s hit TAB and see what pops out.

gst.Buffer gst.Bus gst.BusSyncReply
gst.BufferFlag gst.BusFlags

Let’s type f, hit TAB and see what pops out.

In [2]: gst = gst.Buf
gst.Buffer gst.BufferFlag

Alright, I want a buffer.

In [2]: gst = gst.Buffer()

In [3]: caps = gst.caps_from_string('audio/x-raw-int,channels=2')

Let’s make some caps to set on the buffer.
<type 'exceptions.AttributeError'>        Traceback (most recent call last)

/home/thomas/<ipython console> in <module>()

<type 'exceptions.AttributeError'>: 'gst.Buffer' object has no attribute 'caps_from_string'
What, is my GStreamer copy borked again ?
In [4]: caps = gst.caps_from_string('audio/x-raw-int,channels=2')
<type 'exceptions.AttributeError'>        Traceback (most recent call last)

/home/thomas/<ipython console> in <module>()

<type 'exceptions.AttributeError'>: 'gst.Buffer' object has no attribute 'caps_from_string'
Oh, God, I should have slept last night, twat that I am. I created a buffer and bound it to the ‘gst’ variable that was holding my module.

In [5]: import gst

Let’s try this importing again, create a buffer and caps, and set caps on the buffer.

In [6]: b = gst.Buffer()

In [7]: caps = gst.caps_from_string('audio/x-raw-int,channels=2')

In [8]: b.set_caps(caps)

In [9]: b
Out[9]: <gst .Buffer 0x9980c0 of size 0>
In [10]: b.caps
Out[10]: <gstcaps at 0x927140>

In [11]: b.caps.to_string()
Out[11]: 'audio/x-raw-int, channels=(int)2'

All good so far, except… it’s not going to work well to create a sub-buffer from a buffer of size 0.

In [12]: b = gst.Buffer('kakapipi')

Let’s create a new buffer, and fill it with data that would make Peter proud.
In [13]: b
Out[13]: <gst .Buffer 0x998140 of size 8 and data 0x6b616b61>
Much better, a real buffer.

In [14]: b.set_caps(caps)

In [15]: c = b.create_sub(2, 4)

Now, let’s create our sub buffer.
In [16]: c
Out[16]: <gst .Buffer 0x9981c0 of size 4 and data 0x6b617069>

In [17]: c.caps

Look ma, no caps!
In [18]: c.caps.to_string()
<type 'exceptions.AttributeError'>        Traceback (most recent call last)

/home/thomas/<ipython console> in <module>()

<type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'to_string'
Seriously, there are no caps on the subbuffer.

So there’s Josep’s answer. I hope this makes getting answers from GStreamer easier for some people out there.

On a slightly related note, why is it so hard to copy and paste code or output in WordPress ? I’ve installed the code markup plug-in, disabled the advanced editor like it asked, and it still insisted on pretending anything in angle brackets was a tag that it then had to close, explicitly, in my text.


  1. […] Thomas, I think this method might even be faster than using Python. And it’s even faster if you have a local checkout of the sources handy. And I think every developer should have checkouts of all the libraries he uses to look at for answering questions. […]

    Pingback by Swfblag » Blog Archive » GStreamer questions — 2007-9-10 @ 7:53 am

  2. I’m proud!

    Comment by fons — 2007-9-10 @ 8:38 am

  3. Yes, but what if his question had been should gst_buffer_create_sub() set the master buffer’s caps on the subbuffers it creates or not? I tried to answer that question for myself by searching for “caps” in some gst manual pages, but had no luck. IPython is great, but definitely not a substitute for complete documentation.

    Comment by Björn — 2007-9-10 @ 1:56 pm

  4. The reason I’m showing how to do it with IPython is to show how one should do black box testing. You treat GStreamer as a black box and you are interested in the behaviour of that black box.

    I agree with Benjamin that it’s good to have sources checked out for libraries you use so that you can look what the code does. However, a lot of things can prevent you from getting the answer to a question like this right. In short, reading the code answers a different question.

    In the case Björn thinks of, you may be lucky and find the actual answer to that question in some design doc, but usually it’s just a question that either has not been thought of, or whose answer has not been documented.

    If his question was “How much does an apple cost” then none of the three proposed approaches would work either.

    I really just was trying to answer exactly his question (in which case IPython was a great tool to use), though we also browsed the documentation and the source code as part of the problem solving.

    Comment by Thomas — 2007-9-10 @ 5:52 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment