summaryrefslogtreecommitdiff
path: root/init.js
blob: d66e4b4a140030e486dee1c5b6221c2a83c5bcb7 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
imports.gi.versions.GLib = '2.0';
imports.gi.versions.GObject = '2.0';
imports.gi.versions.Gio = '2.0';

imports.gi.versions.Gdk = '3.0';
imports.gi.versions.Gtk = '3.0';

imports.gi.versions.Clutter = '1.0';
imports.gi.versions.Gst = '0.10';
imports.gi.versions.GstBase = '0.10';
imports.gi.versions.GstVideo = '0.10';

const Mainloop = imports.mainloop;

const IcecastClient = imports.icecastClient;
const Panel = imports.panel;

const GLib = imports.gi.GLib;
const GObject = imports.gi.GObject;
const Gio = imports.gi.Gio;

const Gdk = imports.gi.Gdk;
const Gtk = imports.gi.Gtk;

const Clutter = imports.gi.Clutter;
const Mx = imports.gi.Mx;

const Gst = imports.gi.Gst;
const GstInterfaces = imports.gi.GstInterfaces;
const ClutterGst = imports.gi.ClutterGst;

let application = null;
let style = null;
let stage = null;
let stageLayout = null;

_init();

function _initApplication() {
    Gtk.init(null, null);

    let settings = Gtk.Settings.get_default();
    settings.gtk_application_prefer_dark_theme = true;

    application = new Gtk.Application({ application_id: 'org.gnome.LiveMeetFeed' });
}

function _initGStreamer() {
    Gst.init(null, null);
    Gst.debug_set_active(true);
    Gst.debug_set_default_threshold(Gst.DebugLevel.WARNING);
    Gst.debug_set_colored(false);
}

function _initStyle() {
    style = Mx.Style.get_default();
    style.load_from_file('default.css');
}

function _initStage() {
    Clutter.init(null, null);

    stage = Clutter.Stage.get_default();
    stage.set_user_resizable(true);
    stage.set_color(new Clutter.Color({ red:   0,
                                        green: 0,
                                        blue:  0}));
    stage.connect('delete-event',
                  function() {
                      Mainloop.quit('');
                      return true;
                  });
    stage.set_title("Live Meet Feed");
}

function _init() {
    _initApplication();
    _initGStreamer();
    _initStyle();
    _initStage();

    let pipeline = Gst.Pipeline.new('feed-pipeline');
    let bus = pipeline.get_bus();

    let texture = new Clutter.Texture({ name: 'video-texture' });
    texture.show();

    let frame = new Mx.Frame({ child: texture,
                               name: 'video-frame' });
    frame.x_fill = true;
    frame.y_fill = false;
    frame.y_align = Mx.Align.MIDDLE;

    frame.add_constraint(new Clutter.BindConstraint({ source:     stage,
                                                      coordinate: Clutter.BindCoordinate.SIZE }));
    frame.add_constraint(new Clutter.BindConstraint({ source:     stage,
                                                      coordinate: Clutter.BindCoordinate.POSITION }));
    stage.add_actor(frame);

    let source = Gst.ElementFactory.make('camerabin', 'camera-source');
    source.video_capture_height = 0;
    source.video_capture_width = 0;
    bus.add_signal_watch();
    pipeline.add(source);

    let sink = new ClutterGst.VideoSink({ texture: texture });
    sink.async = false;
    source.viewfinder_sink = sink;

    let caps = Gst.caps_from_string('video/x-raw-yuv; video/x-raw-rgb');
    source.filter_caps = caps;

    let videoEncoder = Gst.ElementFactory.make('theoraenc', 'video-encoder');
    videoEncoder.speed_level = 2;
    source.video_encoder = videoEncoder;

    let audioEncoder = Gst.ElementFactory.make('vorbisenc', 'audio-encoder');
    source.audio_encoder = audioEncoder;

    let bin = Gst.Bin.new('output-bin');

    let muxer = Gst.ElementFactory.make('oggmux', 'muxer');
    bin.add(muxer);

    let tee = Gst.ElementFactory.make('tee', 'output-split');
    bin.add(tee);
    muxer.link(tee);

    let transmitter = Gst.ElementFactory.make('shout2send', 'feed-transmitter');
    transmitter.ip = 'livefeed.lab.bos.redhat.com';
    transmitter.port = 80;
    transmitter.password = 'sodoubt';
    transmitter.mount = 'test.ogg' ;
    transmitter.async = false;
    bin.add(transmitter);
    tee.link(transmitter);

    let muxerSink = muxer.get_request_pad('sink_%d');
    let ghostSink = Gst.GhostPad.new('sink', muxerSink);
    bin.add_pad(ghostSink);

    let teeSource = tee.get_request_pad('src%d');
    let ghostSource = Gst.GhostPad.new('src', teeSource);
    bin.add_pad(ghostSource);

    source.video_muxer = bin;

    source.mode = 1;
    source.flags = 0x000000d9 | 0x20;
    source.filename = "test.ogg"

    pipeline.set_state(Gst.State.READY);
    /* FIXME: figure out how to get notified when input caps are available
     */
    Mainloop.timeout_add(150,
                         function() {
                             let capsSize = source.video_source_caps.get_size();
                             let maxCameraWidth;
                             let maxCameraHeight;

                             maxCameraWidth = 0;
                             maxCameraHeight = 0;
                             for (let i = 0; i < capsSize; i++) {
                                 let [hasWidth, width] = source.video_source_caps.get_structure(i).get_int('width');
                                 let [hasHeight, height] = source.video_source_caps.get_structure(i).get_int('height');
                                 if (!hasWidth || !hasHeight) {
                                     continue;
                                 }

                                 if (width > 512) {
                                     continue;
                                 }

                                 if (height > 512) {
                                     continue;
                                 }

                                 if (width > maxCameraWidth) {
                                     maxCameraWidth = width;
                                 }

                                 if (height > maxCameraHeight) {
                                     maxCameraHeight = height;
                                 }
                             }

                             let panel = new Panel.Panel();
                             stage.connect('notify::allocation',
                                           function() {
                                               panel.actor.set_size(stage.width, -1);

                                               let x = 0;
                                               let y = stage.height - panel.actor.height;

                                               panel.actor.set_position(x, y);
                                           });
                             stage.add_actor(panel.actor);

                             panel.connect('toggle-fullscreen',
                                           function () {
                                               stage.set_fullscreen(!stage.get_fullscreen());
                                           });
                             let [minimumPanelHeight, naturalPanelHeight] = panel.actor.get_preferred_height(maxCameraWidth);

                             stage.set_size(maxCameraWidth, maxCameraHeight + naturalPanelHeight);
                             stage.show_all();

                             pipeline.set_state(Gst.State.PLAYING);

                             panel.connect('toggle-broadcast',
                                           function () {

                                               Gst.debug_set_default_threshold(Gst.DebugLevel.WARNING);
                                               source.emit('capture-start');
                                           });
                         });

    Mainloop.run('');
}