2009-03-03 19:30:53 +00:00
|
|
|
/*
|
|
|
|
* GStreamer
|
|
|
|
* Copyright (c) 2005 INdT.
|
2009-11-18 16:08:25 +00:00
|
|
|
* Copyright (c) 2009 Collabora Ltd.
|
2009-03-03 19:30:53 +00:00
|
|
|
* @author Andre Moreira Magalhaes <andre.magalhaes@indt.org.br>
|
|
|
|
* @author Philippe Khalaf <burger@speedy.org>
|
2009-11-18 16:08:25 +00:00
|
|
|
* @author Olivier Crête <olivier.crete@collabora.co.uk>
|
2009-03-03 19:30:53 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-03 20:38:00 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2009-03-03 19:30:53 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* SECTION:element-mimenc
|
|
|
|
* @see_also: mimdec
|
|
|
|
*
|
|
|
|
* The MIMIC codec is used by MSN Messenger's webcam support. It creates the
|
|
|
|
* TCP header for the MIMIC codec.
|
|
|
|
*
|
2009-11-18 16:08:25 +00:00
|
|
|
* When using it to communicate directly with MSN Messenger, if the sender
|
|
|
|
* wants to stop sending, he has to send a special buffer every 4 seconds.
|
|
|
|
* When the "paused-mode" property is set to TRUE, if the element receives no
|
|
|
|
* buffer on its sink pad for 4 seconds, it will produced a special paused
|
|
|
|
* frame and will continue doing so every 4 seconds until a new buffer is
|
|
|
|
*u received on its sink pad.
|
|
|
|
*
|
2009-03-03 19:30:53 +00:00
|
|
|
* Its fourcc is ML20.
|
|
|
|
*/
|
2005-07-16 19:50:21 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <gst/gst.h>
|
|
|
|
|
|
|
|
#include "gstmimenc.h"
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY (mimenc_debug);
|
|
|
|
#define GST_CAT_DEFAULT (mimenc_debug)
|
|
|
|
|
|
|
|
#define MAX_INTERFRAMES 15
|
|
|
|
|
2009-10-12 23:29:17 +00:00
|
|
|
#define TCP_HEADER_SIZE 24
|
|
|
|
|
|
|
|
#define PAUSED_MODE_INTERVAL (4 * GST_SECOND)
|
|
|
|
|
2009-07-06 15:24:59 +00:00
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_PAUSED_MODE,
|
|
|
|
PROP_LAST
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-03-03 19:20:37 +00:00
|
|
|
static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
|
2005-07-16 19:50:21 +00:00
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2012-08-03 15:21:09 +00:00
|
|
|
GST_STATIC_CAPS ("video/x-raw, format = (string) \"RGB\", "
|
2007-11-10 05:10:35 +00:00
|
|
|
"framerate = (fraction) [1/1, 30/1], "
|
2005-07-16 19:50:21 +00:00
|
|
|
"width = (int) 320, "
|
2012-08-03 15:21:09 +00:00
|
|
|
"height = (int) 240;"
|
|
|
|
"video/x-raw, format = (string) \"RGB\", "
|
2007-11-10 05:10:35 +00:00
|
|
|
"framerate = (fraction) [1/1, 30/1], "
|
2012-08-03 15:21:09 +00:00
|
|
|
"width = (int) 160, " "height = (int) 120")
|
2009-03-03 19:20:37 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
|
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("video/x-mimic")
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2012-08-03 15:21:09 +00:00
|
|
|
static gboolean gst_mim_enc_setcaps (GstMimEnc * mimenc, GstCaps * caps);
|
|
|
|
static GstFlowReturn gst_mim_enc_chain (GstPad * pad, GstObject * parent,
|
|
|
|
GstBuffer * in);
|
|
|
|
static void gst_mim_enc_create_tcp_header (GstMimEnc * mimenc, guint8 * p,
|
|
|
|
guint32 payload_size, GstClockTime ts, gboolean keyframe, gboolean paused);
|
|
|
|
static gboolean gst_mim_enc_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event);
|
2005-07-16 19:50:21 +00:00
|
|
|
|
|
|
|
static GstStateChangeReturn
|
2009-10-12 22:36:58 +00:00
|
|
|
gst_mim_enc_change_state (GstElement * element, GstStateChange transition);
|
2005-07-16 19:50:21 +00:00
|
|
|
|
2009-10-12 22:36:58 +00:00
|
|
|
static void gst_mim_enc_set_property (GObject * object, guint prop_id,
|
2009-07-06 15:24:59 +00:00
|
|
|
const GValue * value, GParamSpec * pspec);
|
2009-10-12 22:36:58 +00:00
|
|
|
static void gst_mim_enc_get_property (GObject * object, guint prop_id,
|
2009-07-06 15:24:59 +00:00
|
|
|
GValue * value, GParamSpec * pspec);
|
2005-07-16 19:50:21 +00:00
|
|
|
|
2012-08-03 15:21:09 +00:00
|
|
|
G_DEFINE_TYPE (GstMimEnc, gst_mim_enc, GST_TYPE_ELEMENT);
|
|
|
|
|
2005-07-16 19:50:21 +00:00
|
|
|
|
|
|
|
static void
|
2012-08-03 15:21:09 +00:00
|
|
|
gst_mim_enc_class_init (GstMimEncClass * klass)
|
2005-07-16 19:50:21 +00:00
|
|
|
{
|
2012-08-03 15:21:09 +00:00
|
|
|
GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
|
2009-07-06 15:24:59 +00:00
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
2009-10-12 22:36:58 +00:00
|
|
|
gobject_class->set_property = gst_mim_enc_set_property;
|
|
|
|
gobject_class->get_property = gst_mim_enc_get_property;
|
2009-07-06 15:24:59 +00:00
|
|
|
|
2012-08-03 15:21:09 +00:00
|
|
|
gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_mim_enc_change_state);
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (mimenc_debug, "mimenc", 0, "Mimic encoder plugin");
|
|
|
|
|
|
|
|
|
2009-07-06 15:24:59 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_PAUSED_MODE,
|
|
|
|
g_param_spec_boolean ("paused-mode", "Paused mode",
|
|
|
|
"If enabled, empty frames will be generated every 4 seconds"
|
|
|
|
" when no data is received",
|
|
|
|
FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2005-07-16 19:50:21 +00:00
|
|
|
|
2016-03-04 06:50:26 +00:00
|
|
|
gst_element_class_add_static_pad_template (gstelement_class, &src_factory);
|
|
|
|
gst_element_class_add_static_pad_template (gstelement_class, &sink_factory);
|
2012-10-17 16:34:26 +00:00
|
|
|
gst_element_class_set_static_metadata (gstelement_class,
|
2009-10-12 23:36:29 +00:00
|
|
|
"Mimic Encoder",
|
2010-03-18 16:30:26 +00:00
|
|
|
"Codec/Encoder/Video",
|
2009-10-12 23:36:29 +00:00
|
|
|
"MSN Messenger compatible Mimic video encoder element",
|
|
|
|
"Andre Moreira Magalhaes <andre.magalhaes@indt.org.br>,"
|
|
|
|
"Olivier Crête <olivier.crete@collabora.co.uk");
|
2005-07-16 19:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-08-03 15:21:09 +00:00
|
|
|
gst_mim_enc_init (GstMimEnc * mimenc)
|
2005-07-16 19:50:21 +00:00
|
|
|
{
|
2009-07-21 02:08:24 +00:00
|
|
|
mimenc->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
|
2009-10-12 22:39:06 +00:00
|
|
|
gst_pad_set_chain_function (mimenc->sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_mim_enc_chain));
|
|
|
|
gst_pad_set_event_function (mimenc->sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_mim_enc_event));
|
2012-08-03 15:21:09 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (mimenc), mimenc->sinkpad);
|
2005-07-16 19:50:21 +00:00
|
|
|
|
2009-07-21 02:08:24 +00:00
|
|
|
mimenc->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
|
2012-08-03 15:21:09 +00:00
|
|
|
gst_pad_use_fixed_caps (mimenc->srcpad);
|
2005-07-16 19:50:21 +00:00
|
|
|
gst_element_add_pad (GST_ELEMENT (mimenc), mimenc->srcpad);
|
|
|
|
|
|
|
|
mimenc->enc = NULL;
|
|
|
|
|
2012-08-03 15:21:09 +00:00
|
|
|
gst_segment_init (&mimenc->segment, GST_FORMAT_TIME);
|
2009-07-06 16:17:22 +00:00
|
|
|
|
2005-07-16 19:50:21 +00:00
|
|
|
mimenc->res = MIMIC_RES_HIGH;
|
|
|
|
mimenc->buffer_size = -1;
|
|
|
|
mimenc->width = 0;
|
|
|
|
mimenc->height = 0;
|
|
|
|
mimenc->frames = 0;
|
|
|
|
}
|
|
|
|
|
2009-07-06 15:24:59 +00:00
|
|
|
static void
|
2009-10-12 22:36:58 +00:00
|
|
|
gst_mim_enc_set_property (GObject * object, guint prop_id,
|
2009-07-06 15:24:59 +00:00
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
2009-10-12 22:36:58 +00:00
|
|
|
GstMimEnc *mimenc = GST_MIM_ENC (object);
|
2009-07-06 15:24:59 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_PAUSED_MODE:
|
|
|
|
GST_OBJECT_LOCK (mimenc);
|
|
|
|
mimenc->paused_mode = g_value_get_boolean (value);
|
2009-10-12 22:47:35 +00:00
|
|
|
if (GST_STATE (object) == GST_STATE_PLAYING)
|
|
|
|
GST_WARNING_OBJECT (object, "Tried to disable paused-mode in a playing"
|
|
|
|
" encoder, it will not be stopped until it is paused");
|
2009-07-06 15:24:59 +00:00
|
|
|
GST_OBJECT_UNLOCK (mimenc);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-10-12 22:36:58 +00:00
|
|
|
gst_mim_enc_get_property (GObject * object, guint prop_id,
|
2009-07-06 15:24:59 +00:00
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
2009-10-12 22:36:58 +00:00
|
|
|
GstMimEnc *mimenc = GST_MIM_ENC (object);
|
2009-07-06 15:24:59 +00:00
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_PAUSED_MODE:
|
|
|
|
GST_OBJECT_LOCK (mimenc);
|
|
|
|
g_value_set_boolean (value, mimenc->paused_mode);
|
|
|
|
GST_OBJECT_UNLOCK (mimenc);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-03 15:21:09 +00:00
|
|
|
static void
|
|
|
|
gst_mim_enc_reset_locked (GstMimEnc * mimenc)
|
|
|
|
{
|
|
|
|
if (mimenc->enc != NULL) {
|
|
|
|
mimic_close (mimenc->enc);
|
|
|
|
mimenc->enc = NULL;
|
|
|
|
mimenc->buffer_size = -1;
|
|
|
|
mimenc->frames = 0;
|
|
|
|
mimenc->width = 0;
|
|
|
|
mimenc->height = 0;
|
|
|
|
}
|
2013-05-15 23:23:08 +00:00
|
|
|
gst_event_replace (&mimenc->pending_segment, NULL);
|
2012-08-03 15:21:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_mim_enc_reset (GstMimEnc * mimenc)
|
|
|
|
{
|
|
|
|
GST_OBJECT_LOCK (mimenc);
|
|
|
|
gst_mim_enc_reset_locked (mimenc);
|
|
|
|
GST_OBJECT_UNLOCK (mimenc);
|
|
|
|
}
|
|
|
|
|
2005-07-16 19:50:21 +00:00
|
|
|
static gboolean
|
2012-08-03 15:21:09 +00:00
|
|
|
gst_mim_enc_setcaps (GstMimEnc * mimenc, GstCaps * caps)
|
2005-07-16 19:50:21 +00:00
|
|
|
{
|
|
|
|
GstStructure *structure;
|
2012-08-03 15:21:09 +00:00
|
|
|
int height, width;
|
|
|
|
gboolean ret = FALSE;
|
2005-07-16 19:50:21 +00:00
|
|
|
|
2009-03-03 19:20:37 +00:00
|
|
|
structure = gst_caps_get_structure (caps, 0);
|
2012-08-03 15:21:09 +00:00
|
|
|
if (!gst_structure_get_int (structure, "width", &width)) {
|
2009-10-12 23:50:52 +00:00
|
|
|
GST_DEBUG_OBJECT (mimenc, "No width set");
|
2012-08-03 15:21:09 +00:00
|
|
|
return FALSE;
|
2007-11-10 05:14:27 +00:00
|
|
|
}
|
2012-08-03 15:21:09 +00:00
|
|
|
if (!gst_structure_get_int (structure, "height", &height)) {
|
2009-10-12 23:50:52 +00:00
|
|
|
GST_DEBUG_OBJECT (mimenc, "No height set");
|
2012-08-03 15:21:09 +00:00
|
|
|
return FALSE;
|
2007-11-10 05:14:27 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 23:50:52 +00:00
|
|
|
GST_OBJECT_LOCK (mimenc);
|
2008-07-04 22:07:13 +00:00
|
|
|
|
2012-08-03 15:21:09 +00:00
|
|
|
if (mimenc->width == width && mimenc->height == height) {
|
|
|
|
ret = TRUE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-10 05:14:27 +00:00
|
|
|
if (width == 320 && height == 240)
|
2009-10-12 23:50:52 +00:00
|
|
|
mimenc->res = MIMIC_RES_HIGH;
|
2007-11-10 05:14:27 +00:00
|
|
|
else if (width == 160 && height == 120)
|
2009-10-12 23:50:52 +00:00
|
|
|
mimenc->res = MIMIC_RES_LOW;
|
2007-11-10 05:14:27 +00:00
|
|
|
else {
|
2009-10-12 23:50:52 +00:00
|
|
|
GST_WARNING_OBJECT (mimenc, "Invalid resolution %dx%d", width, height);
|
2007-11-10 05:14:27 +00:00
|
|
|
goto out;
|
2005-07-16 19:50:21 +00:00
|
|
|
}
|
2007-11-10 05:14:27 +00:00
|
|
|
|
2012-08-03 15:21:09 +00:00
|
|
|
gst_mim_enc_reset_locked (mimenc);
|
|
|
|
|
2009-10-12 23:50:52 +00:00
|
|
|
mimenc->width = (guint16) width;
|
|
|
|
mimenc->height = (guint16) height;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (mimenc, "Got info from caps w : %d, h : %d",
|
|
|
|
mimenc->width, mimenc->height);
|
2007-11-10 05:14:27 +00:00
|
|
|
|
2012-08-03 15:21:09 +00:00
|
|
|
mimenc->enc = mimic_open ();
|
|
|
|
if (!mimenc->enc) {
|
|
|
|
GST_ERROR_OBJECT (mimenc, "mimic_open failed");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2009-10-12 23:50:52 +00:00
|
|
|
if (!mimic_encoder_init (mimenc->enc, mimenc->res)) {
|
|
|
|
GST_ERROR_OBJECT (mimenc, "mimic_encoder_init error");
|
|
|
|
goto out;
|
|
|
|
}
|
2008-07-04 22:07:13 +00:00
|
|
|
|
2009-10-12 23:50:52 +00:00
|
|
|
if (!mimic_get_property (mimenc->enc, "buffer_size", &mimenc->buffer_size)) {
|
|
|
|
GST_ERROR_OBJECT (mimenc, "mimic_get_property(buffer_size) error");
|
|
|
|
}
|
|
|
|
|
2012-08-03 15:21:09 +00:00
|
|
|
ret = TRUE;
|
|
|
|
|
2009-03-03 19:20:37 +00:00
|
|
|
out:
|
2012-08-03 15:21:09 +00:00
|
|
|
GST_OBJECT_UNLOCK (mimenc);
|
2007-11-10 05:14:27 +00:00
|
|
|
return ret;
|
2005-07-16 19:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2012-08-03 15:21:09 +00:00
|
|
|
gst_mim_enc_chain (GstPad * pad, GstObject * parent, GstBuffer * in)
|
2005-07-16 19:50:21 +00:00
|
|
|
{
|
2012-08-03 15:21:09 +00:00
|
|
|
GstMimEnc *mimenc = GST_MIM_ENC (parent);
|
|
|
|
GstBuffer *out = NULL;
|
|
|
|
GstMapInfo in_map;
|
|
|
|
GstMapInfo out_map;
|
2007-11-10 05:17:17 +00:00
|
|
|
GstFlowReturn res = GST_FLOW_OK;
|
2009-07-07 17:27:36 +00:00
|
|
|
gboolean keyframe;
|
2012-08-03 15:21:09 +00:00
|
|
|
gint buffer_size;
|
2008-07-04 22:07:13 +00:00
|
|
|
|
|
|
|
GST_OBJECT_LOCK (mimenc);
|
2005-07-16 19:50:21 +00:00
|
|
|
|
2012-08-03 15:21:09 +00:00
|
|
|
gst_buffer_map (in, &in_map, GST_MAP_READ);
|
2005-07-16 19:50:21 +00:00
|
|
|
|
2012-08-03 15:21:09 +00:00
|
|
|
out = gst_buffer_new_and_alloc (mimenc->buffer_size + TCP_HEADER_SIZE);
|
|
|
|
gst_buffer_map (out, &out_map, GST_MAP_READWRITE);
|
|
|
|
GST_BUFFER_TIMESTAMP (out) =
|
2009-07-07 17:22:54 +00:00
|
|
|
gst_segment_to_running_time (&mimenc->segment, GST_FORMAT_TIME,
|
2012-08-03 15:21:09 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (in));
|
|
|
|
mimenc->last_buffer = GST_BUFFER_TIMESTAMP (out);
|
2005-07-16 19:50:21 +00:00
|
|
|
buffer_size = mimenc->buffer_size;
|
2009-07-07 17:27:36 +00:00
|
|
|
keyframe = (mimenc->frames % MAX_INTERFRAMES) == 0 ? TRUE : FALSE;
|
2012-08-03 15:21:09 +00:00
|
|
|
if (!mimic_encode_frame (mimenc->enc, in_map.data,
|
|
|
|
out_map.data + TCP_HEADER_SIZE, &buffer_size, keyframe)) {
|
|
|
|
gst_buffer_unmap (in, &in_map);
|
|
|
|
gst_buffer_unmap (out, &out_map);
|
|
|
|
gst_buffer_unref (out);
|
2009-10-12 23:16:26 +00:00
|
|
|
GST_ELEMENT_ERROR (mimenc, STREAM, ENCODE, (NULL),
|
|
|
|
("mimic_encode_frame error"));
|
2007-11-10 05:17:17 +00:00
|
|
|
res = GST_FLOW_ERROR;
|
2008-07-04 22:07:13 +00:00
|
|
|
goto out_unlock;
|
2005-07-16 19:50:21 +00:00
|
|
|
}
|
2012-08-03 15:21:09 +00:00
|
|
|
gst_buffer_unmap (in, &in_map);
|
|
|
|
|
2009-10-12 23:52:35 +00:00
|
|
|
if (!keyframe)
|
2012-08-03 15:21:09 +00:00
|
|
|
GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT);
|
2009-10-12 23:52:35 +00:00
|
|
|
|
2005-07-16 19:50:21 +00:00
|
|
|
|
2013-04-08 16:40:14 +00:00
|
|
|
GST_LOG_OBJECT (mimenc, "incoming buf size %" G_GSIZE_FORMAT
|
|
|
|
", encoded size %" G_GSIZE_FORMAT, gst_buffer_get_size (in),
|
|
|
|
gst_buffer_get_size (out));
|
2005-07-16 19:50:21 +00:00
|
|
|
++mimenc->frames;
|
|
|
|
|
2009-10-12 23:17:30 +00:00
|
|
|
/* now let's create that tcp header */
|
2012-08-03 15:21:09 +00:00
|
|
|
gst_mim_enc_create_tcp_header (mimenc, out_map.data, buffer_size,
|
|
|
|
GST_BUFFER_TIMESTAMP (out), keyframe, FALSE);
|
2005-07-16 19:50:21 +00:00
|
|
|
|
2012-08-03 15:21:09 +00:00
|
|
|
gst_buffer_unmap (out, &out_map);
|
|
|
|
gst_buffer_resize (out, 0, buffer_size + TCP_HEADER_SIZE);
|
2009-07-07 17:22:54 +00:00
|
|
|
|
2013-05-15 23:23:08 +00:00
|
|
|
if (G_UNLIKELY (mimenc->pending_segment)) {
|
|
|
|
gst_pad_push_event (mimenc->srcpad, mimenc->pending_segment);
|
|
|
|
mimenc->pending_segment = FALSE;
|
|
|
|
}
|
|
|
|
|
2008-07-04 22:07:13 +00:00
|
|
|
GST_OBJECT_UNLOCK (mimenc);
|
|
|
|
|
2012-08-03 15:21:09 +00:00
|
|
|
res = gst_pad_push (mimenc->srcpad, out);
|
2009-07-07 17:22:54 +00:00
|
|
|
|
2009-03-03 19:20:37 +00:00
|
|
|
out:
|
2012-08-03 15:21:09 +00:00
|
|
|
gst_buffer_unref (in);
|
2005-07-16 19:50:21 +00:00
|
|
|
|
2007-11-10 05:17:17 +00:00
|
|
|
return res;
|
2008-07-04 22:07:13 +00:00
|
|
|
|
2009-03-03 19:20:37 +00:00
|
|
|
out_unlock:
|
2008-07-04 22:07:13 +00:00
|
|
|
GST_OBJECT_UNLOCK (mimenc);
|
|
|
|
goto out;
|
2005-07-16 19:50:21 +00:00
|
|
|
}
|
|
|
|
|
2009-10-12 23:29:17 +00:00
|
|
|
static void
|
2012-08-03 15:21:09 +00:00
|
|
|
gst_mim_enc_create_tcp_header (GstMimEnc * mimenc, guint8 * p,
|
|
|
|
guint32 payload_size, GstClockTime ts, gboolean keyframe, gboolean paused)
|
2005-07-16 19:50:21 +00:00
|
|
|
{
|
2009-03-03 19:20:37 +00:00
|
|
|
p[0] = 24;
|
2009-08-12 16:23:30 +00:00
|
|
|
p[1] = paused ? 1 : 0;
|
|
|
|
GST_WRITE_UINT16_LE (p + 2, mimenc->width);
|
|
|
|
GST_WRITE_UINT16_LE (p + 4, mimenc->height);
|
|
|
|
GST_WRITE_UINT16_LE (p + 6, keyframe ? 1 : 0);
|
|
|
|
GST_WRITE_UINT32_LE (p + 8, payload_size);
|
2009-08-18 19:48:09 +00:00
|
|
|
GST_WRITE_UINT32_LE (p + 12, paused ? 0 :
|
2009-08-12 16:23:30 +00:00
|
|
|
GST_MAKE_FOURCC ('M', 'L', '2', '0'));
|
|
|
|
GST_WRITE_UINT32_LE (p + 16, 0);
|
2012-08-03 15:21:09 +00:00
|
|
|
GST_WRITE_UINT32_LE (p + 20, ts / GST_MSECOND);
|
2005-07-16 19:50:21 +00:00
|
|
|
}
|
|
|
|
|
2012-08-03 15:21:09 +00:00
|
|
|
|
2009-07-06 16:17:22 +00:00
|
|
|
static gboolean
|
2012-08-03 15:21:09 +00:00
|
|
|
gst_mim_enc_event (GstPad * pad, GstObject * parent, GstEvent * event)
|
2009-07-06 16:17:22 +00:00
|
|
|
{
|
2012-08-03 15:21:09 +00:00
|
|
|
GstMimEnc *mimenc = GST_MIM_ENC (parent);
|
2009-07-06 16:17:22 +00:00
|
|
|
gboolean ret = TRUE;
|
2009-07-07 17:22:54 +00:00
|
|
|
gboolean forward = TRUE;
|
|
|
|
|
2009-07-06 16:17:22 +00:00
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
2013-05-15 23:23:08 +00:00
|
|
|
case GST_EVENT_FLUSH_STOP:
|
|
|
|
gst_segment_init (&mimenc->segment, GST_FORMAT_UNDEFINED);
|
|
|
|
gst_event_replace (&mimenc->pending_segment, NULL);
|
|
|
|
break;
|
2012-08-03 15:21:09 +00:00
|
|
|
case GST_EVENT_EOS:
|
|
|
|
gst_mim_enc_reset (mimenc);
|
|
|
|
break;
|
|
|
|
case GST_EVENT_CAPS:
|
2009-07-06 16:17:22 +00:00
|
|
|
{
|
2012-08-03 15:21:09 +00:00
|
|
|
GstCaps *caps;
|
|
|
|
gst_event_parse_caps (event, &caps);
|
|
|
|
|
|
|
|
ret = gst_mim_enc_setcaps (mimenc, caps);
|
2009-07-06 16:17:22 +00:00
|
|
|
|
2012-08-03 15:21:09 +00:00
|
|
|
forward = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GST_EVENT_SEGMENT:
|
|
|
|
{
|
|
|
|
gst_event_copy_segment (event, &mimenc->segment);
|
2009-07-06 16:17:22 +00:00
|
|
|
|
|
|
|
/* we need time for now */
|
2012-08-03 15:21:09 +00:00
|
|
|
if (mimenc->segment.format != GST_FORMAT_TIME)
|
2009-07-06 16:17:22 +00:00
|
|
|
goto newseg_wrong_format;
|
|
|
|
|
2013-05-15 23:23:08 +00:00
|
|
|
gst_event_replace (&mimenc->pending_segment, event);
|
2009-07-07 17:22:54 +00:00
|
|
|
forward = FALSE;
|
2009-07-06 16:17:22 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-07-07 17:22:54 +00:00
|
|
|
if (forward)
|
2012-08-03 15:21:09 +00:00
|
|
|
ret = gst_pad_event_default (pad, parent, event);
|
2009-07-07 17:22:54 +00:00
|
|
|
else
|
|
|
|
gst_event_unref (event);
|
2009-07-06 16:17:22 +00:00
|
|
|
|
|
|
|
done:
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
newseg_wrong_format:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (mimenc, "received non TIME newsegment");
|
|
|
|
gst_event_unref (event);
|
|
|
|
ret = FALSE;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-07 18:18:16 +00:00
|
|
|
static void
|
|
|
|
paused_mode_task (gpointer data)
|
|
|
|
{
|
2009-10-12 22:36:58 +00:00
|
|
|
GstMimEnc *mimenc = GST_MIM_ENC (data);
|
2009-07-07 18:18:16 +00:00
|
|
|
GstClockTime now;
|
|
|
|
GstClockTimeDiff diff;
|
|
|
|
GstFlowReturn ret;
|
|
|
|
|
2009-10-12 23:31:38 +00:00
|
|
|
GST_OBJECT_LOCK (mimenc);
|
|
|
|
|
2009-07-07 18:18:16 +00:00
|
|
|
if (!GST_ELEMENT_CLOCK (mimenc)) {
|
2009-10-12 23:31:38 +00:00
|
|
|
GST_OBJECT_UNLOCK (mimenc);
|
2009-07-07 18:18:16 +00:00
|
|
|
GST_ERROR_OBJECT (mimenc, "Element has no clock");
|
|
|
|
gst_pad_pause_task (mimenc->srcpad);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mimenc->stop_paused_mode) {
|
|
|
|
GST_OBJECT_UNLOCK (mimenc);
|
|
|
|
goto stop_task;
|
|
|
|
}
|
|
|
|
|
|
|
|
now = gst_clock_get_time (GST_ELEMENT_CLOCK (mimenc));
|
|
|
|
|
|
|
|
diff = now - GST_ELEMENT_CAST (mimenc)->base_time - mimenc->last_buffer;
|
|
|
|
if (diff < 0)
|
|
|
|
diff = 0;
|
|
|
|
|
|
|
|
if (diff > 3.95 * GST_SECOND) {
|
2009-10-12 23:29:17 +00:00
|
|
|
GstBuffer *buffer;
|
2012-08-03 15:21:09 +00:00
|
|
|
GstMapInfo out_map;
|
2009-07-07 18:18:16 +00:00
|
|
|
|
2009-11-18 16:14:28 +00:00
|
|
|
buffer = gst_buffer_new_and_alloc (TCP_HEADER_SIZE);
|
2012-08-03 15:21:09 +00:00
|
|
|
gst_buffer_map (buffer, &out_map, GST_MAP_WRITE);
|
2009-10-12 23:29:17 +00:00
|
|
|
GST_BUFFER_TIMESTAMP (buffer) = mimenc->last_buffer + PAUSED_MODE_INTERVAL;
|
2012-08-03 15:21:09 +00:00
|
|
|
gst_mim_enc_create_tcp_header (mimenc, out_map.data, 0,
|
|
|
|
GST_BUFFER_TIMESTAMP (buffer), FALSE, TRUE);
|
|
|
|
gst_buffer_unmap (buffer, &out_map);
|
2009-10-12 23:29:17 +00:00
|
|
|
|
|
|
|
mimenc->last_buffer += PAUSED_MODE_INTERVAL;
|
2009-07-07 18:18:16 +00:00
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (mimenc);
|
|
|
|
GST_LOG_OBJECT (mimenc, "Haven't had an incoming buffer in 4 seconds,"
|
|
|
|
" sending out a pause frame");
|
|
|
|
|
|
|
|
ret = gst_pad_push (mimenc->srcpad, buffer);
|
|
|
|
if (ret < 0) {
|
|
|
|
GST_WARNING_OBJECT (mimenc, "Error pushing paused header: %s",
|
|
|
|
gst_flow_get_name (ret));
|
|
|
|
goto stop_task;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
GstClockTime next_stop;
|
|
|
|
GstClockID id;
|
|
|
|
|
2009-10-12 23:29:17 +00:00
|
|
|
next_stop = now + (PAUSED_MODE_INTERVAL - MIN (diff, PAUSED_MODE_INTERVAL));
|
2009-07-07 18:18:16 +00:00
|
|
|
|
|
|
|
id = gst_clock_new_single_shot_id (GST_ELEMENT_CLOCK (mimenc), next_stop);
|
|
|
|
|
|
|
|
if (mimenc->stop_paused_mode) {
|
|
|
|
GST_OBJECT_UNLOCK (mimenc);
|
|
|
|
goto stop_task;
|
|
|
|
}
|
|
|
|
|
|
|
|
mimenc->clock_id = id;
|
|
|
|
GST_OBJECT_UNLOCK (mimenc);
|
|
|
|
|
|
|
|
gst_clock_id_wait (id, NULL);
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (mimenc);
|
|
|
|
mimenc->clock_id = NULL;
|
|
|
|
GST_OBJECT_UNLOCK (mimenc);
|
2009-07-21 01:52:59 +00:00
|
|
|
|
|
|
|
gst_clock_id_unref (id);
|
2009-07-07 18:18:16 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
stop_task:
|
|
|
|
|
|
|
|
gst_pad_pause_task (mimenc->srcpad);
|
|
|
|
}
|
|
|
|
|
2005-07-16 19:50:21 +00:00
|
|
|
static GstStateChangeReturn
|
2009-10-12 22:36:58 +00:00
|
|
|
gst_mim_enc_change_state (GstElement * element, GstStateChange transition)
|
2005-07-16 19:50:21 +00:00
|
|
|
{
|
2009-10-12 22:36:58 +00:00
|
|
|
GstMimEnc *mimenc = GST_MIM_ENC (element);
|
2009-07-07 18:18:16 +00:00
|
|
|
GstStateChangeReturn ret;
|
2009-07-10 20:42:19 +00:00
|
|
|
gboolean paused_mode;
|
2005-07-16 19:50:21 +00:00
|
|
|
|
|
|
|
switch (transition) {
|
2009-07-06 16:17:22 +00:00
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
|
|
|
GST_OBJECT_LOCK (mimenc);
|
|
|
|
gst_segment_init (&mimenc->segment, GST_FORMAT_UNDEFINED);
|
2013-05-15 23:23:08 +00:00
|
|
|
gst_event_replace (&mimenc->pending_segment, NULL);
|
2009-07-07 17:22:54 +00:00
|
|
|
mimenc->last_buffer = GST_CLOCK_TIME_NONE;
|
2009-07-07 18:18:16 +00:00
|
|
|
GST_OBJECT_UNLOCK (mimenc);
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
|
|
|
|
GST_OBJECT_LOCK (mimenc);
|
|
|
|
if (mimenc->clock_id)
|
|
|
|
gst_clock_id_unschedule (mimenc->clock_id);
|
|
|
|
mimenc->stop_paused_mode = TRUE;
|
|
|
|
GST_OBJECT_UNLOCK (mimenc);
|
|
|
|
|
|
|
|
gst_pad_pause_task (mimenc->srcpad);
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-08-03 15:21:09 +00:00
|
|
|
ret = GST_ELEMENT_CLASS (gst_mim_enc_parent_class)->change_state (element,
|
|
|
|
transition);
|
2009-07-07 18:18:16 +00:00
|
|
|
|
|
|
|
if (ret == GST_STATE_CHANGE_FAILURE)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
|
|
|
|
GST_OBJECT_LOCK (mimenc);
|
|
|
|
mimenc->stop_paused_mode = FALSE;
|
2009-07-10 20:42:19 +00:00
|
|
|
paused_mode = mimenc->paused_mode;
|
2009-08-12 16:21:33 +00:00
|
|
|
if (paused_mode) {
|
|
|
|
if (!GST_ELEMENT_CLOCK (mimenc)) {
|
|
|
|
GST_OBJECT_UNLOCK (mimenc);
|
|
|
|
GST_ELEMENT_ERROR (mimenc, RESOURCE, FAILED,
|
|
|
|
("Using paused-mode requires a clock, but no clock was provided"
|
|
|
|
" to the element"), (NULL));
|
|
|
|
return GST_STATE_CHANGE_FAILURE;
|
|
|
|
}
|
|
|
|
if (mimenc->last_buffer == GST_CLOCK_TIME_NONE)
|
|
|
|
mimenc->last_buffer = gst_clock_get_time (GST_ELEMENT_CLOCK (mimenc))
|
|
|
|
- GST_ELEMENT_CAST (mimenc)->base_time;
|
|
|
|
}
|
2009-07-06 16:17:22 +00:00
|
|
|
GST_OBJECT_UNLOCK (mimenc);
|
2009-07-10 20:42:19 +00:00
|
|
|
if (paused_mode) {
|
2012-06-20 08:34:48 +00:00
|
|
|
if (!gst_pad_start_task (mimenc->srcpad, paused_mode_task, mimenc,
|
|
|
|
NULL)) {
|
2009-07-10 20:42:19 +00:00
|
|
|
ret = GST_STATE_CHANGE_FAILURE;
|
|
|
|
GST_ERROR_OBJECT (mimenc, "Can not start task");
|
|
|
|
}
|
2009-07-07 18:18:16 +00:00
|
|
|
}
|
2009-07-06 16:17:22 +00:00
|
|
|
break;
|
2009-10-12 23:50:52 +00:00
|
|
|
case GST_STATE_CHANGE_READY_TO_NULL:
|
2012-08-03 15:21:09 +00:00
|
|
|
gst_mim_enc_reset (mimenc);
|
2009-10-12 23:50:52 +00:00
|
|
|
break;
|
|
|
|
|
2005-07-16 19:50:21 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-07-07 18:18:16 +00:00
|
|
|
return ret;
|
2005-07-16 19:50:21 +00:00
|
|
|
}
|