File: gst-inspect.rb

package info (click to toggle)
ruby-gnome 4.3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,320 kB
  • sloc: ruby: 55,206; ansic: 29,012; xml: 333; sh: 229; cpp: 45; makefile: 42
file content (487 lines) | stat: -rwxr-xr-x 12,366 bytes parent folder | download | duplicates (7)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#!/usr/bin/env ruby
#
# This sample code is a port of gstreamer/tools/gst-inspect.c. It is
# licensed under the terms of the GNU Library General Public License,
# version 2 or (at your option) later.
#
# The followin is the original code's copyright notation:
#   Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
#                 2000 Wim Taymans <wtay@chello.be>
#                 2004 Thomas Vander Stichele <thomas@apestaart.org>
#
#
# Copyright (C) 2013  Ruby-GNOME2 Project Team
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

require "optparse"
require "ostruct"

require "gst"

def parse(argv)
  options = OpenStruct.new
  options.print_all = false
  options.print_auto_install_info = false

  opts = OptionParser.new do |opts|
    opts.banner += " [ELEMENT-NAME|PLUGIN-NAME]"

    opts.version = Gst.version

    opts.separator("")
    opts.on("-a", "--print-all", "Print all elements") do
      options.print_all = true
    end

    opts.on("--print-plugin-auto-install-info",
            "Print a machine-parsable list of features",
            "the specified plugin provides.",
            "Useful in connection with external",
            "automatic plugin installation mechanisms") do
      options.print_auto_install_info = true
    end
  end
  opts.parse!(argv)

  [options, argv.first]
end

class Inspector
  def initialize
    @prefix = ""
  end

  def print_list(print_all)
    plugins = {}
    n_features = 0
    each_feature do |plugin, feature|
      plugins[plugin.name] = nil
      n_features += 1
      if print_all
        if feature.is_a?(Gst::ElementFactory)
          print_element_factory(feature, true)
        end
      else
        print_feature(plugin, feature)
      end
    end
    puts
    puts("Total count: #{plugins.size} plugins, #{n_features} features")
  end

  def print_element(name)
    factory = Gst::ElementFactory.find(name)
    if factory
      print_element_factory(factory, false)
    else
      registry = Gst::Registry.get
      type_find_factory = registry.find_feature(name, Gst::TypeFindFactory)
      if type_find_factory
        # TODO
      else
        plugin = registry.find_plugin(name)
        print_plugin_info(plugin)
        # TODO: print_plugin_features(plugin)
      end
    end
  end

  private
  def puts(*args)
    if args.empty?
      super(@prefix)
    else
      super(*(args.collect {|arg| "#{@prefix}#{arg}"}))
    end
  end

  def prefix(new_prefix)
    prefix, @prefix = @prefix, new_prefix
    yield
  ensure
    @prefix = prefix
  end

  def each_feature
    registry = Gst::Registry.get
    registry.plugins.sort_by {|plugin| plugin.name}.each do |plugin|
      features = registry.get_features(plugin.name)
      features.sort_by {|feature| feature.name}.each do |feature|
        yield(plugin, feature)
      end
    end
  end

  def print_factory_details_info(factory)
    puts("Factory Details:",
         "  Rank:\t\t#{factory.rank.nick} (#{factory.rank.to_i})",
         "  Long name:\t#{factory.long_name}",
         "  Class:\t#{factory.klass}",
         "  Description:\t#{factory.description}",
         "  Author(s):\t#{factory.author}",
         "")
  end

  def print_plugin_info(plugin)
    return if plugin.nil?

    puts("Plugin Details:",
         "  Name:\t\t\t#{plugin.name}",
         "  Description:\t\t#{plugin.description}",
         "  Filename:\t\t#{plugin.filename || '(null)'}",
         "  Version:\t\t#{plugin.version}",
         "  License:\t\t#{plugin.license}",
         "  Source module:\t#{plugin.source}")
    release_date = plugin.release_date_string
    if release_date
      puts("  Source release date:\t#{release_date}")
    end
    puts("  Binary package:\t#{plugin.package}",
         "  Origin URL:\t\t#{plugin.origin}",
         "")
  end

  def print_hierarchy(element)
    ancestors = []
    type = element.gtype
    while type
      ancestors << type
      type = type.parent
    end
    ancestors.reverse.each_with_index do |klass, i|
      if i.zero?
        mark = ""
      else
        mark = " " + ("      " * (i - 1)) + "+----"
      end
      puts("#{mark}#{klass.name}")
    end
    puts
  end

  def print_interfaces(element)
    interfaces = element.gtype.interfaces
    return if interfaces.empty?

    puts("Implemented Interfaces:")
    interfaces.each do |interface|
      puts("  #{interface.name}")
    end
    puts
  end

  def print_caps(caps)
    if caps.any?
      puts("ANY")
      return
    end
    if caps.empty?
      puts("EMPTY")
      return
    end

    caps.structures.each do |structure|
      puts(structure.name)
      structure.fields.each do |name, value|
        puts("  %15s: %s" % [name, value.serialize])
      end
    end
  end

  def print_pad_template_info(template)
    prefix("#{@prefix}  ") do
      puts("#{template.direction.nick.upcase} template: '#{template.name_template}'")
      prefix("#{@prefix}  ") do
        if template.presence == Gst::PadPresence::REQUEST
          puts("Availability: On request")
        else
          puts("Availability: #{template.presence.nick.capitalize}")
        end
      end
    end

    caps = template.caps
    return if caps.nil?
    prefix("#{@prefix}    ") do
      puts("Capabilities:")
      prefix("#{@prefix}  ") do
        print_caps(caps)
      end
    end
    puts
  end

  def print_pad_templates_info(element, factory)
    puts("Pad Templates:")
    templates = factory.static_pad_templates

    if templates.empty?
      puts("  none")
      return
    end

    templates.each do |template|
      print_pad_template_info(template)
    end
  end

  def print_element_flag_info(element)
    puts("Element Flags:")
    puts("  no flags set")
    puts

    return unless element.is_a?(Gst::Bin)
    puts("Bin Flags:")
    puts("  no flags set")
    puts
  end

  def print_implementation_info(element)
    puts("Element Implementation:")
    puts("  not supported")
    puts
  end

  def print_clocking_info(element)
    if !element.flags.require_clock? and
        !element.flags.provide_clock? and
        element.clock.nil?
      puts("Element has no clocking capabilities.")
      return
    end

    puts("Clocking Interaction:")
    puts("  element requires a clock") if element.flags.require_clock?
    if element.flags.provide_clock?
      clock = element.clock
      if clock.nil?
        puts("  element is supported tot provide a clock but returned nil")
      else
        puts("  element provides a clock: #{clock.name}")
      end
    end
    puts
  end

  def print_index_info(element)
    if element.flags.indexable?
      puts("Indexing capabilities:")
      puts("  element can do indexing")
    else
      puts("Element has no indexing capabilities.")
    end
    puts
  end

  def uri_type(element)
    case element.uri_type
    when Gst::URIType::SRC
      "source"
    when Gst::URIType::SINK
      "sink"
    else
      "unknown"
    end
  end

  def print_uri_handler_info(element)
    if element.is_a?(Gst::URIHandler)
      puts("URI handling capabilities:")
      puts("  Element can act as #{uri_type(element)}.")
      protocols = element.protocols
      if protocols.empty?
        puts("  No supported URI protocols")
      else
        puts("  Supported URI protocols:")
        protocols.each do |protocol|
          puts("    #{protocol}")
        end
      end
    else
      puts("Element has no URI handling capabilities.")
    end
    puts
  end

  def print_pad_info(element)
    puts("Pads:")
    pads = element.pads
    if pads.empty?
      puts("  none")
      return
    end

    pads.each do |pad|
      prefix("#{@prefix}  ") do
        puts("#{pad.direction.nick.upcase}: '#{pad.name}'")
        prefix("#{@prefix}  ") do
          template = pad.template
          puts("Pad Template: '#{template.name}'") if template

          caps = pad.caps
          break if caps.nil?
          puts("Capabilities:")
          prefix("#{@prefix}  ") do
            print_caps(caps)
          end
        end
      end
    end
    puts
  end

  def print_element_properties_info(element)
    puts("Element Properties:")
    properties = element.class.properties
    if properties.empty?
      puts(" none")
      return
    end

    properties.each do |name|
      param = element.class.property(name)
      puts("  %-20s: %s" % [param.name, param.blurb])

      flags = []
      flags << "readable" if param.readable?
      flags << "writable" if param.writable?
      # flags << "controllable" if param.controllable?

      prefix("#{@prefix}#{' ' * 23} ") do
        puts("flags: #{flags.join(', ')}")
        description = ""
        type_name = param.value_type.name
        description << "#{type_name}."
        if param.readable?
          case param.gtype
          when GLib::Type["GParamObject"]
            # do nothing
          else
            default_value = element.get_property(name)
            description << " Default: #{default_value.inspect}"
          end
        else
          description << " Write only"
        end
        puts(description)
      end
    end
    puts
  end

  def print_signal_info(element, signal)
    args = ["object(#{element.class})"]
    signal.param_types.each_with_index do |name, i|
      args << "arg#{i}(#{name})"
    end
    args = args.join(", ")
    signature = "{|#{args}| ...} # => #{signal.return_type}"
    puts("  #{signal.name.inspect}: #{signature}")
  end

  def print_signals_info(element)
    signals = []
    actions = []
    element.class.signals.each do |name|
      signal = element.class.signal(name)
      next if signal.owner >= Gst::Element
      if signal.action?
        actions << signal
      else
        signals << signal
      end
    end

    [[signals, "Signals"],
     [actions, "Actions"]].each do |target_signals, description|
      unless target_signals.empty?
        puts("Element #{description}:")
        target_signals.each do |signal|
          print_signal_info(element, signal)
        end
      end
    end
    puts
  end

  def print_children_info(element)
    return unless element.is_a?(Gst::Bin)
    children = element.children
    return if children.empty?

    puts("Children:")
    children.each do |child|
      puts("  #{child.name}")
    end
  end

  def print_element_factory(factory, print_names)
    factory = factory.load
    if factory.nil?
      puts("element plugin (#{factory.name}) couldn't be loaded\n")
      return
    end

    element = factory.create

    prefix(print_names ? "#{factory.name}: " : "") do
      print_factory_details_info(factory)
      print_plugin_info(factory.plugin)

      print_hierarchy(element)
      print_interfaces(element)

      print_pad_templates_info(element, factory)
      puts

      print_element_flag_info(element)
      print_implementation_info(element)
      print_clocking_info(element)
      print_index_info(element)
      print_uri_handler_info(element)
      print_pad_info(element)
      print_element_properties_info(element)
      print_signals_info(element)
      print_children_info(element)
    end
  end

  def print_feature(plugin, feature)
    prefix("#{plugin.name}: #{feature.name}") do
      case feature
      when Gst::ElementFactory
        puts(": #{feature.long_name}")
      when Gst::TypeFindFactory
        if feature.extensions.empty?
          message = "no extensions"
        else
          message = feature.extensions.join(", ")
        end
        puts(": #{message}")
      else
        puts(" (#{feature.gtype})")
      end
    end
  end
end

options, element_name = parse(ARGV)
inspector = Inspector.new
if element_name
  inspector.print_element(element_name)
else
  inspector.print_list(options.print_all)
end