summaryrefslogtreecommitdiff
path: root/samples/DecodeBinTranscoder.cs
blob: 414cb36a487f8e2cf34a8240f054da16a0bbdd09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//
// DecodeBinTranscoder.cs: sample transcoder using DecodeBin binding
//
// Authors:
//   Aaron Bockover (abockover@novell.com)
//
// (C) 2006 Novell, Inc.
//

using System;
using Gst;
using Gst.CorePlugins;
using Gst.BasePlugins;

public delegate void ErrorHandler(object o, ErrorArgs args);
public delegate void ProgressHandler(object o, ProgressArgs args);

public class ErrorArgs : EventArgs 
{
    public string Error;
}

public class ProgressArgs : EventArgs
{
    public long Duration;
    public long Position;
}

public class DecodeBinTranscoder : IDisposable
{
    private Pipeline pipeline;
    private FileSrc filesrc;
    private FileSink filesink;
    private Element audioconvert;
    private Element encoder;
    private DecodeBin decodebin;
    
    private uint progress_timeout;
    
    public event EventHandler Finished;
    public event ErrorHandler Error;
    public event ProgressHandler Progress;
    
    public DecodeBinTranscoder()
    {
        ConstructPipeline();
    }
    
    public void Transcode(string inputFile, string outputFile)
    {
        filesrc.Location = inputFile;
        filesink.Location = outputFile;
        
        pipeline.SetState(State.Playing);
        progress_timeout = GLib.Timeout.Add(250, OnProgressTimeout);
    }
    
    public void Dispose()
    {
        pipeline.Dispose();
    }
    
    protected virtual void OnFinished()
    {
        EventHandler handler = Finished;
        if(handler != null) {
            handler(this, new EventArgs());
        }
    }
        
    protected virtual void OnError(string error)
    {
        ErrorHandler handler = Error;
        if(handler != null) {
            ErrorArgs args = new ErrorArgs();
            args.Error = error;
            handler(this, args);
        }
    }
    
    protected virtual void OnProgress(long position, long duration)
    {
        ProgressHandler handler = Progress;
        if(handler != null) {
            ProgressArgs args = new ProgressArgs();
            args.Position = position;
            args.Duration = duration;
            handler(this, args);
        }
    }

    private void ConstructPipeline()
    {
        pipeline = new Pipeline("pipeline");
        
        filesrc = ElementFactory.Make("filesrc", "filesrc") as FileSrc;
        filesink = ElementFactory.Make("filesink", "filesink") as FileSink;
        audioconvert = ElementFactory.Make("audioconvert", "audioconvert");
        encoder = ElementFactory.Make("wavenc", "wavenc");
        decodebin = ElementFactory.Make("decodebin", "decodebin") as DecodeBin;
        decodebin.NewDecodedPad += OnNewDecodedPad;
        
        pipeline.Add (filesrc, decodebin, audioconvert, encoder, filesink);
        
        filesrc.Link(decodebin);
        audioconvert.Link(encoder);
        encoder.Link(filesink);
        
        pipeline.Bus.AddWatch(new BusFunc(OnBusMessage));
    }
    
    private void OnNewDecodedPad(object o, DecodeBin.NewDecodedPadArgs args)
    {
        Pad sinkpad = audioconvert.GetStaticPad("sink");

        if(sinkpad.IsLinked) {
            return;
        }

        Caps caps = args.Pad.Caps;
        Structure structure = caps[0];
        
        if(!structure.Name.StartsWith("audio")) {
            return;
        }
        
        args.Pad.Link(sinkpad);
    }
    
    private bool OnBusMessage(Bus bus, Message message)
    {
        switch(message.Type) {
            case MessageType.Error:
                string msg;
		Enum err;
                message.ParseError(out err, out msg);
                GLib.Source.Remove(progress_timeout);
                OnError(msg);
                break;
            case MessageType.Eos:
                pipeline.SetState(State.Null);
                GLib.Source.Remove(progress_timeout);
                OnFinished();
                break;
        }

        return true;
    }
    
    private bool OnProgressTimeout()
    {
        long duration, position;
	Gst.Format fmt = Gst.Format.Time;
        
        if(pipeline.QueryDuration(ref fmt, out duration) && fmt == Gst.Format.Time && encoder.QueryPosition(ref fmt, out position) && fmt == Gst.Format.Time) {
            OnProgress(position, duration);
        }
        
        return true;
    }
    
    private static GLib.MainLoop loop;
    
    public static void Main(string [] args)
    {
        if(args.Length < 2) {
            Console.WriteLine("Usage: mono decodebin-transcoder.exe <input-file> <output-file>");
            return;
        }
    
        Gst.Application.Init();
        loop = new GLib.MainLoop();
    
        DecodeBinTranscoder transcoder = new DecodeBinTranscoder();
        
        transcoder.Error += delegate(object o, ErrorArgs eargs) {
            Console.WriteLine("Error: {0}", eargs.Error);
            transcoder.Dispose();
            loop.Quit();
        };
        
        transcoder.Finished += delegate {
            Console.WriteLine("\nFinished");
            transcoder.Dispose();
            loop.Quit();
        };
        
        transcoder.Progress += delegate(object o, ProgressArgs pargs) {
            Console.Write("\rEncoding: {0} / {1} ({2:00.00}%) ", 
                new TimeSpan((pargs.Position / (long) Clock.Second) * TimeSpan.TicksPerSecond), 
                new TimeSpan((pargs.Duration / (long) Clock.Second) * TimeSpan.TicksPerSecond),
                ((double)pargs.Position / (double)pargs.Duration) * 100.0);
        };
        
        transcoder.Transcode(args[0], args[1]);
        
        loop.Run();
    }
}