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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
use super::BACKEND_BASE_TIME;
use gst;
use gst::prelude::*;
use once_cell::sync::Lazy;
use servo_media_streams::registry::{
    get_stream, register_stream, unregister_stream, MediaStreamId,
};
use servo_media_streams::{MediaOutput, MediaSocket, MediaStream, MediaStreamType};
use std::any::Any;
use std::sync::{Arc, Mutex};

pub static RTP_CAPS_OPUS: Lazy<gst::Caps> = Lazy::new(|| {
    gst::Caps::builder("application/x-rtp")
        .field("media", "audio")
        .field("encoding-name", "OPUS")
        .build()
});

pub static RTP_CAPS_VP8: Lazy<gst::Caps> = Lazy::new(|| {
    gst::Caps::builder("application/x-rtp")
        .field("media", "video")
        .field("encoding-name", "VP8")
        .build()
});

pub struct GStreamerMediaStream {
    id: Option<MediaStreamId>,
    type_: MediaStreamType,
    elements: Vec<gst::Element>,
    pipeline: Option<gst::Pipeline>,
}

impl MediaStream for GStreamerMediaStream {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_mut_any(&mut self) -> &mut dyn Any {
        self
    }

    fn set_id(&mut self, id: MediaStreamId) {
        self.id = Some(id);
    }

    fn ty(&self) -> MediaStreamType {
        self.type_
    }
}

impl GStreamerMediaStream {
    pub fn new(type_: MediaStreamType, elements: Vec<gst::Element>) -> Self {
        Self {
            id: None,
            type_,
            elements,
            pipeline: None,
        }
    }

    pub fn caps(&self) -> &gst::Caps {
        match self.type_ {
            MediaStreamType::Audio => &RTP_CAPS_OPUS,
            MediaStreamType::Video => &RTP_CAPS_VP8,
        }
    }

    pub fn caps_with_payload(&self, payload: i32) -> gst::Caps {
        match self.type_ {
            MediaStreamType::Audio => gst::Caps::builder("application/x-rtp")
                .field("media", "audio")
                .field("encoding-name", "OPUS")
                .field("payload", payload)
                .build(),
            MediaStreamType::Video => gst::Caps::builder("application/x-rtp")
                .field("media", "video")
                .field("encoding-name", "VP8")
                .field("payload", payload)
                .build(),
        }
    }

    pub fn src_element(&self) -> gst::Element {
        self.elements.last().unwrap().clone()
    }

    pub fn attach_to_pipeline(&mut self, pipeline: &gst::Pipeline) {
        assert!(self.pipeline.is_none());
        let elements: Vec<_> = self.elements.iter().collect();
        pipeline.add_many(&elements[..]).unwrap();
        gst::Element::link_many(&elements[..]).unwrap();
        for element in elements {
            element.sync_state_with_parent().unwrap();
        }
        self.pipeline = Some(pipeline.clone());
    }

    pub fn pipeline_or_new(&mut self) -> gst::Pipeline {
        if let Some(ref pipeline) = self.pipeline {
            pipeline.clone()
        } else {
            let pipeline = gst::Pipeline::with_name("gstreamermediastream fresh pipeline");
            let clock = gst::SystemClock::obtain();
            pipeline.set_start_time(gst::ClockTime::NONE);
            pipeline.set_base_time(*BACKEND_BASE_TIME);
            pipeline.use_clock(Some(&clock));
            self.attach_to_pipeline(&pipeline);
            pipeline
        }
    }

    pub fn create_video() -> MediaStreamId {
        let videotestsrc = gst::ElementFactory::make("videotestsrc")
            .property_from_str("pattern", "ball")
            .property("is-live", true)
            .build()
            .unwrap();
        Self::create_video_from(videotestsrc)
    }

    /// Attaches encoding adapters to the stream, returning the source element
    pub fn encoded(&mut self) -> gst::Element {
        let pipeline = self
            .pipeline
            .as_ref()
            .expect("GStreamerMediaStream::encoded() should not be called without a pipeline");
        let src = self.src_element();

        let capsfilter = gst::ElementFactory::make("capsfilter")
            .property("caps", self.caps())
            .build()
            .unwrap();
        match self.type_ {
            MediaStreamType::Video => {
                let vp8enc = gst::ElementFactory::make("vp8enc")
                    .property("deadline", 1i64)
                    .property("error-resilient", "default")
                    .property("cpu-used", -16i32)
                    .property("lag-in-frames", 0i32)
                    .build()
                    .unwrap();

                let rtpvp8pay = gst::ElementFactory::make("rtpvp8pay")
                    .property("picture-id-mode", "15-bit")
                    .property("mtu", 1200u32)
                    .build()
                    .unwrap();
                let queue2 = gst::ElementFactory::make("queue").build().unwrap();

                pipeline
                    .add_many(&[&vp8enc, &rtpvp8pay, &queue2, &capsfilter])
                    .unwrap();
                gst::Element::link_many(&[&src, &vp8enc, &rtpvp8pay, &queue2, &capsfilter])
                    .unwrap();
                vp8enc.sync_state_with_parent().unwrap();
                rtpvp8pay.sync_state_with_parent().unwrap();
                queue2.sync_state_with_parent().unwrap();
                capsfilter.sync_state_with_parent().unwrap();
                capsfilter
            }
            MediaStreamType::Audio => {
                let opusenc = gst::ElementFactory::make("opusenc").build().unwrap();
                let rtpopuspay = gst::ElementFactory::make("rtpopuspay")
                    .property("mtu", 1200u32)
                    .build()
                    .unwrap();
                let queue3 = gst::ElementFactory::make("queue").build().unwrap();
                pipeline
                    .add_many(&[&opusenc, &rtpopuspay, &queue3, &capsfilter])
                    .unwrap();
                gst::Element::link_many(&[&src, &opusenc, &rtpopuspay, &queue3, &capsfilter])
                    .unwrap();
                opusenc.sync_state_with_parent().unwrap();
                rtpopuspay.sync_state_with_parent().unwrap();
                queue3.sync_state_with_parent().unwrap();
                capsfilter
            }
        }
    }

    pub fn create_video_from(source: gst::Element) -> MediaStreamId {
        let videoconvert = gst::ElementFactory::make("videoconvert").build().unwrap();
        let queue = gst::ElementFactory::make("queue").build().unwrap();

        register_stream(Arc::new(Mutex::new(GStreamerMediaStream::new(
            MediaStreamType::Video,
            vec![source, videoconvert, queue],
        ))))
    }

    pub fn create_audio() -> MediaStreamId {
        let audiotestsrc = gst::ElementFactory::make("audiotestsrc")
            .property_from_str("wave", "sine")
            .property("is-live", true)
            .build()
            .unwrap();

        Self::create_audio_from(audiotestsrc)
    }

    pub fn create_audio_from(source: gst::Element) -> MediaStreamId {
        let queue = gst::ElementFactory::make("queue").build().unwrap();
        let audioconvert = gst::ElementFactory::make("audioconvert").build().unwrap();
        let audioresample = gst::ElementFactory::make("audioresample").build().unwrap();
        let queue2 = gst::ElementFactory::make("queue").build().unwrap();

        register_stream(Arc::new(Mutex::new(GStreamerMediaStream::new(
            MediaStreamType::Audio,
            vec![source, queue, audioconvert, audioresample, queue2],
        ))))
    }

    pub fn create_proxy(ty: MediaStreamType) -> (MediaStreamId, GstreamerMediaSocket) {
        let proxy_sink = gst::ElementFactory::make("proxysink").build().unwrap();
        let proxy_src = gst::ElementFactory::make("proxysrc")
            .property("proxysink", &proxy_sink)
            .build()
            .unwrap();
        let stream = match ty {
            MediaStreamType::Audio => Self::create_audio_from(proxy_src),
            MediaStreamType::Video => Self::create_video_from(proxy_src),
        };

        (stream, GstreamerMediaSocket { proxy_sink })
    }
}

impl Drop for GStreamerMediaStream {
    fn drop(&mut self) {
        if let Some(ref id) = self.id {
            unregister_stream(id);
        }
    }
}

pub struct MediaSink {
    streams: Vec<Arc<Mutex<dyn MediaStream>>>,
}

impl MediaSink {
    pub fn new() -> Self {
        MediaSink { streams: vec![] }
    }
}

impl MediaOutput for MediaSink {
    fn add_stream(&mut self, stream: &MediaStreamId) {
        let stream = get_stream(stream).expect("Media streams registry does not contain such ID");
        {
            let mut stream = stream.lock().unwrap();
            let stream = stream
                .as_mut_any()
                .downcast_mut::<GStreamerMediaStream>()
                .unwrap();
            let pipeline = stream.pipeline_or_new();
            let last_element = stream.elements.last();
            let last_element = last_element.as_ref().unwrap();
            let sink = match stream.type_ {
                MediaStreamType::Audio => "autoaudiosink",
                MediaStreamType::Video => "autovideosink",
            };
            let sink = gst::ElementFactory::make(sink).build().unwrap();
            pipeline.add(&sink).unwrap();
            gst::Element::link_many(&[last_element, &sink][..]).unwrap();

            pipeline.set_state(gst::State::Playing).unwrap();
            sink.sync_state_with_parent().unwrap();
        }
        self.streams.push(stream.clone());
    }
}

pub struct GstreamerMediaSocket {
    proxy_sink: gst::Element,
}

impl GstreamerMediaSocket {
    pub fn proxy_sink(&self) -> &gst::Element {
        &self.proxy_sink
    }
}

impl MediaSocket for GstreamerMediaSocket {
    fn as_any(&self) -> &dyn Any {
        self
    }
}