Reworked track settings.
[demorecorder] / src / RecordPipeline.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 RecordPipeline : 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   int64 start_time;
33   Gst.Query duration_query;
34   Gst.Query position_query;
35   int64 position;
36   int64 duration;
37   
38   public bool bouncing;
39   
40   public signal void position_duration(int64 position, int64 duration);
41   public signal void end_of_stream();
42   public signal void stream_error(string msg);
43   
44   public RecordPipeline(string name, string location, Gst.Element src, Gst.Caps caps, string encoder_element) {
45     //this.bouncing = false;
46     this.name = name;
47     this.location = location;
48     this.caps = caps;
49     this.src = src;
50     this.encoder_element = encoder_element;
51     //set the tag handling delegate
52     //this.handle_tags_delegate = this.handle_tags;
53     //set some default values
54     this.start_time = 0;
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     this.construct_pipeline();
59   }
60   
61   public bool set_src(Gst.Element src) {
62     this.set_state(Gst.State.NULL);
63     if (null != this.src) {
64       clear_src();
65     }
66     this.src = src;
67     this.add(this.src);
68     this.src.link(this.queue);
69     return true;
70   }
71   
72   public void clear_src() {
73     if (null != this.src) {
74       this.set_state(Gst.State.NULL);
75       this.src.unlink(this.queue);
76       this.remove(this.src);
77       this.src = null;
78     }
79   }
80   
81   public bool set_location(string location) {
82     this.set_state(Gst.State.NULL);
83     this.location = location;
84     this.filesink.set_property("location", location);
85     return true;
86   }
87   
88   public string get_location() {
89     return this.location;
90   }
91   
92   public void get_duration_info() {
93     if (bouncing) {
94       bool duration_result,position_result;
95       Gst.Format format = Gst.Format.TIME;
96       duration_result = this.query(this.duration_query);
97       position_result = this.query(this.position_query);
98       if ( duration_result && position_result  ) {
99         this.duration_query.parse_duration(out format, out this.duration);
100         this.position_query.parse_position(out format, out this.position);
101         this.position_duration(this.position,this.duration);
102         if (this.position >= this.duration) {
103           this.end_of_stream();
104         }
105       }
106     }
107     else {
108       this.position_duration(Time.get_current_time() - start_time, 0);
109     }
110   }
111   
112   public void record() {
113     start_time = Time.get_current_time();
114     this.set_state(Gst.State.PLAYING);
115   }
116   
117   public void stop() {
118     //this.end_of_stream();
119     this.set_state(Gst.State.READY);
120   }
121   
122   private void construct_pipeline() {
123     //this.name= name;
124     
125     if (null != this.src) {
126       this.add(this.src);
127     }
128     
129     this.converter = Gst.ElementFactory.make("audioconvert", "converter");
130     this.add(this.converter);
131     
132     this.queue = Gst.ElementFactory.make("queue", "buffer");
133     this.add(this.queue);
134     
135     this.resampler = Gst.ElementFactory.make("audioresample", "resampler");
136     this.resampler.set_property("quality", 10);
137     this.add(resampler);
138     this.encoder = Gst.ElementFactory.make(encoder_element, "encoder");
139     this.add(this.encoder);
140     if (null != this.encoder) {
141 //       stdout.printf("Encoder added!\n");
142 //       stdout.flush();
143     }
144     this.filesink = Gst.ElementFactory.make("filesink", "fileoutput");
145     if (null != this.filesink) {
146       this.filesink.set_property("location", location);
147 //       stdout.printf("Location set!\n");
148 //       stdout.flush();
149       this.add(this.filesink);
150 //       stdout.printf("Filesink added!\n");
151 //       stdout.flush();
152     }
153     
154     if (null != this.src && this.src.link(this.queue)) {
155 //       stdout.printf("Source linked!\n");
156 //       stdout.flush();
157     }
158     if (this.queue.link(this.converter)) {
159 //       stdout.printf("Queue linked!\n");
160 //       stdout.flush();
161     }
162     if (this.converter.link(this.resampler)) {
163 //       stdout.printf("Converter linked!\n");
164 //       stdout.flush();
165     }
166     if (this.resampler.link_filtered(this.encoder, caps)) {
167 //       stdout.printf("Resampler linked!\n");
168 //       stdout.flush();
169     }
170     if (this.encoder.link(this.filesink)) {
171 //       stdout.printf("Encoder linked!\n");
172 //       stdout.flush();
173     }
174   }
175   
176   
177 }
178
179 }