Reworked AboutDialog.
[demorecorder] / src / PlayPipeline.vala
1 /*  Demo Recorder for MAEMO 5
2 *   Copyright (C) 2010 Dru Moore <usr@dru-id.co.uk>
3 *   This program is free software; you can redistribute it and/or modify
4 *   it under the terms of the GNU General Public License version 2,
5 *   or (at your option) any later version, as published by the Free
6 *   Software Foundation
7 *
8 *   This program is distributed in the hope that it will be useful,
9 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 *   GNU General Public License for more details
12 *
13 *   You should have received a copy of the GNU General Public
14 *   License along with this program; if not, write to the
15 *   Free Software Foundation, Inc.,
16 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 */
18 namespace IdWorks {
19
20 public class PlayPipeline : Gst.Pipeline {
21
22   Gst.Element src;
23   Gst.Element sink_element;
24   Gst.Query duration_query;
25   Gst.Query position_query;
26   int64 position;
27   int64 duration;
28   
29   public signal void position_duration(int64 position, int64 duration);
30   public signal void end_of_stream();
31   public signal void stream_error(string msg);
32   
33   public PlayPipeline(string name, Gst.Element src) {
34     //set the tag handling delegate
35     //set some default values
36     this.name = name;
37     this.src = src;
38     this.position = 0;
39     this.duration = 0;
40     //this.name = name;
41     //define the queries
42     duration_query = new Gst.Query.duration(Gst.Format.TIME);
43     position_query = new Gst.Query.position(Gst.Format.TIME);
44     
45     construct_pipeline();
46   }
47   
48   public bool set_src(MixerBin src) {
49     this.set_state(Gst.State.NULL);
50     if (null != this.src) {
51       clear_src();
52     }
53     this.src = src;
54     this.add(this.src);
55     this.src.link(this.sink_element);
56     return true;
57   }
58   
59   public void clear_src() {
60     if (null != this.src) {
61       this.set_state(Gst.State.NULL);
62       this.src.unlink(this.sink_element);
63       this.remove(this.src);
64       this.src = null;
65     }
66   }
67   
68   private void construct_pipeline() {
69     if (null != this.src) {
70       this.add(src);
71     }
72     sink_element     = Gst.ElementFactory.make("autoaudiosink", "output");
73     this.add(sink_element);
74     
75     if (null != this.src) {
76       if (!src.link(sink_element)) {
77         stdout.printf("failed to link src to sink\n");
78         stdout.flush();}
79     }
80     Gst.Bus bus = this.src.get_bus();
81     bus.add_signal_watch();
82     bus.connect("message:eos", bus_callback, this);
83     bus.connect("message:error", bus_callback, this);
84     bus.connect("message:state-changed", bus_callback, this);
85     uint watch_id = bus.add_watch(bus_callback);
86   }
87   
88   public bool bus_callback (Gst.Bus bus, Gst.Message message) {
89     stdout.printf("bus_callback\n");
90     stdout.flush();
91     switch (message.type) {
92       case Gst.MessageType.ERROR:
93         GLib.Error err;
94         string debug;
95         message.parse_error (out err, out debug);
96         stdout.printf("%s\n", err.message);
97         stdout.flush();
98         stream_error(err.message);
99         break;
100       case Gst.MessageType.EOS:
101         stdout.printf ("end of stream\n");
102         stdout.flush();
103         end_of_stream();
104         break;
105       case Gst.MessageType.STATE_CHANGED:
106         Gst.State oldstate;
107         Gst.State newstate;
108         Gst.State pending;
109         message.parse_state_changed (out oldstate, out newstate, out pending);
110         stdout.printf ("%s->%s:%s\n", oldstate.to_string(), newstate.to_string(), pending.to_string());
111         stdout.flush();
112         break;
113       default:
114         break;
115     }
116     return false;
117   }
118   
119   public void get_duration_info() {
120     bool duration_result,position_result;
121     Gst.Format format = Gst.Format.TIME;
122     duration_result = this.query(this.duration_query);
123     position_result = this.query(this.position_query);
124     if ( duration_result && position_result  ) {
125       this.duration_query.parse_duration(out format, out this.duration);
126       this.position_query.parse_position(out format, out this.position);
127       this.position_duration(this.position,this.duration);
128     }
129   }
130   
131   public void play() {
132     this.set_state(Gst.State.PLAYING);
133   }
134   
135   public void pause() {
136     this.set_state(Gst.State.PAUSED);
137   }
138   
139   public void stop() {
140     this.set_state(Gst.State.READY);
141     clear_src();
142   }
143   /*
144   public void move_to(int64 newloc) {
145     if (0 > newloc) newloc = 0;
146     //this.src.move_to(newloc);
147     //this.play();
148   }
149   
150   public void seek(int percent) {
151     move_to((int64) this.position + (this.duration * percent / 100));
152   }
153   
154   public void seek_forward(int percent) {
155     seek(percent);
156   }
157   
158   public void seek_backward(int percent) {
159     seek(-percent);
160   }
161   */
162 }
163
164 }