Files
array_init
bitflags
byteorder
cfg_if
futures_channel
futures_core
futures_executor
futures_io
futures_macro
futures_sink
futures_task
futures_util
async_await
future
lock
sink
stream
task
gio
auto
action.rsaction_group.rsaction_map.rsapp_info.rsapp_info_monitor.rsapp_launch_context.rsapplication.rsapplication_command_line.rsbuffered_input_stream.rsbuffered_output_stream.rsbytes_icon.rscancellable.rscharset_converter.rsconstants.rsconverter.rsconverter_input_stream.rsconverter_output_stream.rscredentials.rsdata_input_stream.rsdata_output_stream.rsdesktop_app_info.rsdrive.rsemblem.rsemblemed_icon.rsenums.rsfile.rsfile_attribute_matcher.rsfile_enumerator.rsfile_icon.rsfile_info.rsfile_input_stream.rsfile_io_stream.rsfile_monitor.rsfile_output_stream.rsfilename_completer.rsfilter_input_stream.rsfilter_output_stream.rsflags.rsfunctions.rsicon.rsinet_address.rsinet_address_mask.rsinet_socket_address.rsinput_stream.rsio_stream.rsloadable_icon.rsmemory_input_stream.rsmemory_output_stream.rsmenu.rsmenu_attribute_iter.rsmenu_item.rsmenu_link_iter.rsmenu_model.rsmod.rsmount.rsmount_operation.rsnetwork_address.rsnetwork_monitor.rsnetwork_service.rsnotification.rsoutput_stream.rspermission.rspollable_input_stream.rspollable_output_stream.rsproperty_action.rsproxy.rsproxy_address.rsproxy_resolver.rsremote_action_group.rsresolver.rsresource.rsseekable.rssettings.rssettings_backend.rssettings_schema.rssettings_schema_key.rssettings_schema_source.rssimple_action.rssimple_action_group.rssimple_permission.rssocket.rssocket_address.rssocket_address_enumerator.rssocket_client.rssocket_connectable.rssocket_connection.rssocket_listener.rssocket_service.rssrv_target.rssubprocess.rssubprocess_launcher.rstcp_connection.rsthemed_icon.rsthreaded_socket_service.rstls_certificate.rstls_client_connection.rstls_connection.rstls_database.rstls_file_database.rstls_interaction.rstls_password.rstls_server_connection.rsunix_input_stream.rsunix_output_stream.rsunix_socket_address.rsvfs.rsvolume.rsvolume_monitor.rszlib_compressor.rszlib_decompressor.rs
subclass
gio_sys
glib
glib_sys
gobject_sys
gstreamer
gstreamer_app
gstreamer_app_sys
gstreamer_audio
gstreamer_audio_sys
gstreamer_base
gstreamer_base_sys
gstreamer_check
gstreamer_check_sys
gstreamer_editing_services
gstreamer_editing_services_sys
gstreamer_gl
gstreamer_gl_sys
gstreamer_net
gstreamer_net_sys
gstreamer_pbutils
gstreamer_pbutils_sys
gstreamer_player
gstreamer_player_sys
gstreamer_rtp
gstreamer_rtp_sys
gstreamer_rtsp
gstreamer_rtsp_server
gstreamer_rtsp_server_sys
gstreamer_rtsp_sys
gstreamer_sdp
gstreamer_sdp_sys
gstreamer_sys
gstreamer_video
gstreamer_video_sys
gstreamer_webrtc
gstreamer_webrtc_sys
lazy_static
libc
muldiv
num_integer
num_rational
num_traits
paste
paste_impl
pin_utils
proc_macro2
proc_macro_hack
proc_macro_nested
quote
serde
serde_bytes
serde_derive
slab
syn
unicode_xid
  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
// Copyright (C) 2019 Philippe Normand <philn@igalia.com>
// Copyright (C) 2019 Guillaume Desmottes <guillaume.desmottes@collabora.com>
// Copyright (C) 2019 Sebastian Dröge <sebastian@centricular.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use glib::object::IsA;
use glib::translate::*;
use gst;
use gst_video_sys;
use std::mem;
use std::ptr;
use utils::HasStreamLock;
use video_codec_state::{InNegotiation, Readable, VideoCodecState, VideoCodecStateContext};
use VideoCodecFrame;
use VideoEncoder;

pub trait VideoEncoderExtManual: 'static {
    #[cfg(any(feature = "v1_12", feature = "dox"))]
    fn allocate_output_frame(
        &self,
        frame: &mut VideoCodecFrame,
        size: usize,
    ) -> Result<gst::FlowSuccess, gst::FlowError>;

    fn get_frame(&self, frame_number: i32) -> Option<VideoCodecFrame>;
    fn get_frames(&self) -> Vec<VideoCodecFrame>;
    fn get_oldest_frame(&self) -> Option<VideoCodecFrame>;

    fn get_allocator(&self) -> (gst::Allocator, gst::AllocationParams);

    fn finish_frame(
        &self,
        frame: Option<VideoCodecFrame>,
    ) -> Result<gst::FlowSuccess, gst::FlowError>;

    fn get_latency(&self) -> (gst::ClockTime, gst::ClockTime);
    fn set_latency(&self, min_latency: gst::ClockTime, max_latency: gst::ClockTime);

    fn get_output_state(&self) -> Option<VideoCodecState<'static, Readable>>;
    fn set_output_state(
        &self,
        caps: gst::Caps,
        reference: Option<&VideoCodecState<Readable>>,
    ) -> Result<VideoCodecState<InNegotiation>, gst::FlowError>;

    fn negotiate<'a>(
        &'a self,
        output_state: VideoCodecState<'a, InNegotiation<'a>>,
    ) -> Result<(), gst::FlowError>;
}

impl<O: IsA<VideoEncoder>> VideoEncoderExtManual for O {
    #[cfg(any(feature = "v1_12", feature = "dox"))]
    fn allocate_output_frame(
        &self,
        frame: &mut VideoCodecFrame,
        size: usize,
    ) -> Result<gst::FlowSuccess, gst::FlowError> {
        let ret: gst::FlowReturn = unsafe {
            from_glib(gst_video_sys::gst_video_encoder_allocate_output_frame(
                self.as_ref().to_glib_none().0,
                frame.to_glib_none().0,
                size,
            ))
        };
        ret.into_result()
    }

    fn get_allocator(&self) -> (gst::Allocator, gst::AllocationParams) {
        unsafe {
            let mut allocator = ptr::null_mut();
            let mut params = mem::zeroed();
            gst_video_sys::gst_video_encoder_get_allocator(
                self.as_ref().to_glib_none().0,
                &mut allocator,
                &mut params,
            );
            (from_glib_full(allocator), params.into())
        }
    }

    fn finish_frame(
        &self,
        frame: Option<VideoCodecFrame>,
    ) -> Result<gst::FlowSuccess, gst::FlowError> {
        let ret: gst::FlowReturn = unsafe {
            from_glib(gst_video_sys::gst_video_encoder_finish_frame(
                self.as_ref().to_glib_none().0,
                frame.map(|f| f.into_ptr()).unwrap_or(ptr::null_mut()),
            ))
        };
        ret.into_result()
    }

    fn get_latency(&self) -> (gst::ClockTime, gst::ClockTime) {
        let mut min_latency = gst_sys::GST_CLOCK_TIME_NONE;
        let mut max_latency = gst_sys::GST_CLOCK_TIME_NONE;

        unsafe {
            gst_video_sys::gst_video_encoder_get_latency(
                self.as_ref().to_glib_none().0,
                &mut min_latency,
                &mut max_latency,
            );

            (from_glib(min_latency), from_glib(max_latency))
        }
    }

    fn set_latency(&self, min_latency: gst::ClockTime, max_latency: gst::ClockTime) {
        unsafe {
            gst_video_sys::gst_video_encoder_set_latency(
                self.as_ref().to_glib_none().0,
                min_latency.to_glib(),
                max_latency.to_glib(),
            );
        }
    }

    fn get_frame(&self, frame_number: i32) -> Option<VideoCodecFrame> {
        let frame = unsafe {
            gst_video_sys::gst_video_encoder_get_frame(self.as_ref().to_glib_none().0, frame_number)
        };

        if frame.is_null() {
            None
        } else {
            unsafe { Some(VideoCodecFrame::new(frame, self.as_ref())) }
        }
    }

    fn get_frames(&self) -> Vec<VideoCodecFrame> {
        unsafe {
            let frames =
                gst_video_sys::gst_video_encoder_get_frames(self.as_ref().to_glib_none().0);
            let mut iter: *const glib_sys::GList = frames;
            let mut vec = Vec::new();

            while !iter.is_null() {
                let frame_ptr = Ptr::from((*iter).data);
                /* transfer ownership of the frame */
                let frame = VideoCodecFrame::new(frame_ptr, self.as_ref());
                vec.push(frame);
                iter = (*iter).next;
            }

            glib_sys::g_list_free(frames);
            vec
        }
    }

    fn get_oldest_frame(&self) -> Option<VideoCodecFrame> {
        let frame = unsafe {
            gst_video_sys::gst_video_encoder_get_oldest_frame(self.as_ref().to_glib_none().0)
        };

        if frame.is_null() {
            None
        } else {
            unsafe { Some(VideoCodecFrame::new(frame, self.as_ref())) }
        }
    }

    fn get_output_state(&self) -> Option<VideoCodecState<'static, Readable>> {
        let state = unsafe {
            gst_video_sys::gst_video_encoder_get_output_state(self.as_ref().to_glib_none().0)
        };

        if state.is_null() {
            None
        } else {
            unsafe { Some(VideoCodecState::<Readable>::new(state)) }
        }
    }

    fn set_output_state(
        &self,
        caps: gst::Caps,
        reference: Option<&VideoCodecState<Readable>>,
    ) -> Result<VideoCodecState<InNegotiation>, gst::FlowError> {
        let state = unsafe {
            let reference = match reference {
                Some(reference) => reference.as_mut_ptr(),
                None => ptr::null_mut(),
            };
            gst_video_sys::gst_video_encoder_set_output_state(
                self.as_ref().to_glib_none().0,
                caps.into_ptr(),
                reference,
            )
        };

        if state.is_null() {
            Err(gst::FlowError::NotNegotiated)
        } else {
            unsafe { Ok(VideoCodecState::<InNegotiation>::new(state, self.as_ref())) }
        }
    }

    fn negotiate<'a>(
        &'a self,
        output_state: VideoCodecState<'a, InNegotiation<'a>>,
    ) -> Result<(), gst::FlowError> {
        // Consume output_state so user won't be able to modify it anymore
        let self_ptr = self.to_glib_none().0 as *const gst_sys::GstElement;
        assert_eq!(output_state.context.get_element_as_ptr(), self_ptr);

        let ret = unsafe {
            from_glib(gst_video_sys::gst_video_encoder_negotiate(
                self.as_ref().to_glib_none().0,
            ))
        };
        if ret {
            Ok(())
        } else {
            Err(gst::FlowError::NotNegotiated)
        }
    }
}

impl HasStreamLock for VideoEncoder {
    fn get_stream_lock(&self) -> *mut glib_sys::GRecMutex {
        let encoder_sys: *const gstreamer_video_sys::GstVideoEncoder = self.to_glib_none().0;
        unsafe { &(*encoder_sys).stream_lock as *const _ as usize as *mut _ }
    }

    fn get_element_as_ptr(&self) -> *const gst_sys::GstElement {
        let encoder_sys: *const gstreamer_video_sys::GstVideoEncoder = self.to_glib_none().0;
        encoder_sys as *const gst_sys::GstElement
    }
}