Debian Codegraph @

lib/ninix/dll/mciaudior.rb

raw
1 # -*- coding: utf-8 -*-
2 #
3 # mciaudior.rb - a MCIAUDIOR compatible Saori module for ninix
4 # Copyright (C) 2003-2019 by Shyouzou Sugitani <[email protected]>
5 # Copyright (C) 2003 by MATSUMURA Namihiko <[email protected]>
6 #
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License (version 2) as
9 # published by the Free Software Foundation. It is distributed in the
10 # hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
11 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 # PURPOSE. See the GNU General Public License for more details.
13 #
14
15 require "uri"
16 require "pathname"
17
18 begin
19 require "gst"
20 rescue LoadError
21 Gst = nil
22 end
23
24 require_relative "../home"
25 require_relative "../dll"
26 require_relative "../logging"
27
28
29 module Mciaudior
30
31 class Saori < DLL::SAORI
32
33 def initialize
34 super()
35 @player = Gst::ElementFactory.make('playbin', 'player')
36 fakesink = Gst::ElementFactory.make('fakesink', 'fakesink')
37 @player.set_property('video-sink', fakesink)
38 bus = @player.bus
39 bus.add_watch do |bus, message|
40 on_message(bus, message)
41 true
42 end
43 @filepath = nil
44 @loop = false
45 end
46
47 def check_import
48 Gst.nil? ? 0 : 1
49 end
50
51 def finalize
52 @player.set_state(Gst::State::NULL)
53 @player = nil
54 @filepath = nil
55 return 1
56 end
57
58 def execute(argv)
59 return RESPONSE[400] if argv.nil?
60 argc = argv.length
61 case argc
62 when 1
63 fail "assert" if @player.nil?
64 case argv[0]
65 when 'stop'
66 @player.set_state(Gst::State::NULL)
67 when 'play', 'loop'
68 @loop = true if argv[0] == 'loop'
69 case @player.get_state(timeout=Gst::SECOND)[1]
70 when Gst::State::PAUSED
71 @player.set_state(Gst::State::PLAYING)
72 return RESPONSE[204]
73 when Gst::State::PLAYING
74 @player.set_state(Gst::State::PAUSED)
75 return RESPONSE[204]
76 end
77 if not @filepath.nil? and File.exist?(@filepath)
78 @player.set_property(
79 'uri', 'file://' + URI.escape(@filepath))
80 @player.set_state(Gst::State::PLAYING)
81 end
82 end
83 when 2
84 if argv[0] == 'load'
85 @player.set_state(Gst::State::NULL)
86 filename = Home.get_normalized_path(argv[1])
87 if not Pathname(filename).absolute?
88 @filepath = File.join(@dir, filename)
89 else
90 @filepath = filename
91 end
92 end
93 end
94 return RESPONSE[204]
95 end
96
97 def on_message(bus, message)
98 return if message.nil? # XXX: workaround for Gst Version < 0.11
99 case message.type
100 when Gst::MessageType::EOS
101 @player.set_state(Gst::State::NULL)
102 @player.set_state(Gst::State::PLAYING) if @loop
103 when Gst::MessageType::ERROR
104 @player.set_state(Gst::State::NULL)
105 err, debug = message.parse_error()
106 Logging::Logging.error("Error: #{err}, #{debug}")
107 @loop = false
108 end
109 end
110 end
111 end