Added About Dialog
[demorecorder] / src / EncodePipeline.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 EncodePipeline : Gst.Pipeline {
21     
22     Gst.Element src;
23     Gst.Element queue;
24     Gst.Element resampler;
25     Gst.Element encoder;
26     Gst.Element converter;
27     Gst.Element filesink;
28     //string name;
29     string location;
30     //string encoder_element;
31     Gst.Caps caps;
32     Gst.Query duration_query;
33     Gst.Query position_query;
34     int64 position;
35     int64 old_position;
36     int64 duration;
37     int same_position_counter;
38     
39     public signal void position_duration(int64 position, int64 duration);
40     public signal void end_of_stream();
41     public signal void stream_error(string msg);
42     
43     public EncodePipeline(string name, string location, Gst.Element src, Gst.Element encoder, Gst.Caps caps) {
44       this.name = name;
45       this.location = location;
46       this.caps = caps;
47       this.src = src;
48       this.encoder = encoder;
49       //set the tag handling delegate
50       //this.handle_tags_delegate = this.handle_tags;
51       //set some default values
52       this.position=0;
53       this.duration=0;
54       this.construct_pipeline();
55       //define the queries
56       duration_query = new Gst.Query.duration(Gst.Format.TIME);
57       position_query = new Gst.Query.position(Gst.Format.TIME);
58     }
59     
60     public bool set_src(Gst.Element src) {
61       this.set_state(Gst.State.NULL);
62       if (null != this.src) {
63         clear_src();
64       }
65       this.src = src;
66       this.add(this.src);
67       this.src.link(this.queue);
68       return true;
69     }
70     
71     public void clear_src() {
72       if (null != this.src) {
73         this.set_state(Gst.State.NULL);
74         this.src.unlink(this.queue);
75         this.remove(this.src);
76         this.src = null;
77       }
78     }
79     
80     public bool set_encoder(Gst.Element encoder) {
81       this.set_state(Gst.State.NULL);
82       if (null != this.encoder) {
83         clear_encoder();
84       }
85       this.encoder = encoder;
86       this.add(this.encoder);
87       this.converter.link(this.encoder);
88       this.encoder.link(this.filesink);
89       return true;
90     }
91     
92     public void clear_encoder() {
93       if (null != this.encoder) {
94         this.set_state(Gst.State.NULL);
95         this.converter.unlink(this.encoder);
96         this.encoder.unlink(this.filesink);
97         this.remove(this.encoder);
98         this.encoder = null;
99       }
100     }
101     
102     public bool set_location(string location) {
103       this.set_state(Gst.State.NULL);
104       this.location = location;
105       this.filesink.set_property("location", location);
106       return true;
107     }
108     
109     public string get_location() {
110       return this.location;
111     }
112     
113     public void get_duration_info() {
114       bool duration_result,position_result;
115       Gst.Format format = Gst.Format.TIME;
116       duration_result = this.query(this.duration_query);
117       position_result = this.query(this.position_query);
118       if ( duration_result && position_result  ) {
119         this.duration_query.parse_duration(out format, out this.duration);
120         this.position_query.parse_position(out format, out this.position);
121         if (old_position == this.position) {
122           ++same_position_counter;
123         }
124         old_position = this.position;
125         this.position_duration(this.position,this.duration);
126         if (
127           (this.position >= this.duration) ||
128           ((5 < same_position_counter) && ((this.position + 100 * Time.Nanoseconds.MILLISECOND) >= this.duration))
129           ) {
130           this.end_of_stream();
131         }
132       }
133     }
134     
135     public void encode() {
136       this.set_state(Gst.State.PLAYING);
137     }
138     
139     public void stop() {
140       this.end_of_stream();
141       this.set_state(Gst.State.READY);
142     }
143     
144     private void construct_pipeline() {
145       ///this.name= name;
146       
147       if (null != this.src) {
148         this.add(this.src);
149       }
150       
151       this.queue = Gst.ElementFactory.make("queue", "buffer");
152       this.add(this.queue);
153       
154       this.converter = Gst.ElementFactory.make("audioconvert", "converter");
155       this.add(this.converter);
156       
157       
158       //this.resampler = Gst.ElementFactory.make("audioresample", "resampler");
159       //this.resampler.set_property("quality", 10);
160       //this.add(resampler);
161       //this.encoder = Gst.ElementFactory.make(encoder_element, "encoder");
162       this.add(this.encoder);
163       if (null != this.encoder) {
164 //         stdout.printf("Encoder added!\n");
165 //         stdout.flush();
166       }
167       this.filesink = Gst.ElementFactory.make("filesink", "fileoutput");
168       if (null != this.filesink) {
169         this.filesink.set_property("location", location);
170 //         stdout.printf("Location set!\n");
171 //         stdout.flush();
172         this.add(this.filesink);
173 //         stdout.printf("Filesink added!\n");
174 //         stdout.flush();
175       }
176       
177       if (null != this.src && this.src.link(this.queue)) {
178 //         stdout.printf("Source linked!\n");
179 //         stdout.flush();
180       }
181       if (this.queue.link(this.converter)) {
182 //         stdout.printf("Queue linked!\n");
183 //         stdout.flush();
184       }
185       /*
186       if (this.converter.link(this.resampler)) {
187         stdout.printf("Converter linked!\n");
188         stdout.flush();
189       }
190       if (this.resampler.link_filtered(this.encoder, caps)) {
191         stdout.printf("Resampler linked!\n");
192         stdout.flush();
193       }
194       */
195       if (this.converter.link(this.encoder)) {
196 //         stdout.printf("Converter linked!\n");
197 //         stdout.flush();
198       }
199       if (this.encoder.link(this.filesink)) {
200 //         stdout.printf("Encoder linked!\n");
201 //         stdout.flush();
202       }
203     }
204     
205     
206   }
207   
208 }