Debian Codegraph @

lib/ninix/dll/mciaudio.rb

raw
1 # -*- coding: utf-8 -*-
2 #
3 # mciaudio.rb - a MCIAUDIO compatible Saori module for ninix
4 # Copyright (C) 2002-2019 by Shyouzou Sugitani <[email protected]>
5 # Copyright (C) 2002, 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
17 begin
18 require "gst"
19 rescue LoadError
20 Gst = nil
21 end
22
23 require_relative "../home"
24 require_relative "../dll"
25 require_relative "../logging"
26
27
28 module Mciaudio
29
30 class Saori < DLL::SAORI
31
32 def initialize
33 super()
34 @player = Gst::ElementFactory.make('playbin', 'player')
35 fakesink = Gst::ElementFactory.make('fakesink', 'fakesink')
36 @player.set_property('video-sink', fakesink)
37 bus = @player.bus
38 bus.add_watch do |bus, message|
39 on_message(bus, message)
40 true
41 end
42 @filepath = nil
43 @__sakura = nil
44 end
45
46 def need_ghost_backdoor(sakura)
47 @__sakura = sakura
48 end
49
50 def check_import
51 unless @__sakura.nil? or Gst.nil?
52 return 1
53 else
54 return 0
55 end
56 end
57
58 def finalize
59 @player.set_state(Gst::State::NULL)
60 @player = nil
61 @filepath = nil
62 return 1
63 end
64
65 def execute(argv)
66 return RESPONSE[400] if argv.nil?
67 argc = argv.length
68 case argc
69 when 1
70 fail "assert" if @player.nil?
71 case argv[0]
72 when 'stop'
73 @player.set_state(Gst::State::NULL)
74 when 'play'
75 case @player.get_state(timeout=Gst::SECOND)[1]
76 when Gst::State::PAUSED
77 @player.set_state(Gst::State::PLAYING)
78 return RESPONSE[204]
79 when Gst::State::PLAYING
80 @player.set_state(Gst::State::PAUSED)
81 return RESPONSE[204]
82 end
83 if not @filepath.nil? and File.exist?(@filepath)
84 @player.set_property(
85 'uri', 'file://' + URI.escape(@filepath))
86 @player.set_state(Gst::State::PLAYING)
87 end
88 end
89 when 2
90 if argv[0] == 'load'
91 @player.set_state(Gst::State::NULL)
92 filename = Home.get_normalized_path(argv[1])
93 if File.absolute_path(filename) == filename
94 return RESPONSE[400]
95 end
96 @filepath = File.join(@__sakura.get_prefix(),
97 'ghost/master',
98 @dir,
99 filename)
100 end
101 end
102 return RESPONSE[204]
103 end
104
105 def on_message(bus, message)
106 return if message.nil? # XXX: workaround for Gst Version < 0.11
107 case message.type
108 when Gst::MessageType::EOS
109 @player.set_state(Gst::State::NULL)
110 when Gst::MessageType::ERROR
111 @player.set_state(Gst::State::NULL)
112 err, debug = message.parse_error()
113 Logging::Logging.error("Error: #{err}, #{debug}")
114 end
115 end
116 end
117 end