aboutsummaryrefslogtreecommitdiff
path: root/src/views/video/msink.c
blob: 8e85c6ab8961cd49568c41dc9339c91701dbfe16 (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

#include <gst/gst.h>
#include <gst/video/video.h>

#include "msink.h"

static GstStaticPadTemplate sinktemplate
= GST_STATIC_PAD_TEMPLATE ("sink",
                           GST_PAD_SINK,
                           GST_PAD_ALWAYS,
                           GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{ IYUV, I420, YV12 }") ";" \
                                            GST_VIDEO_CAPS_RGBx ";" \
                                            GST_VIDEO_CAPS_BGRx ";" \
                                            GST_VIDEO_CAPS_RGB ";" \
                                            GST_VIDEO_CAPS_BGR));
static GstStaticPadTemplate sinktemplate_rgb
= GST_STATIC_PAD_TEMPLATE ("sink",
                           GST_PAD_SINK,
                           GST_PAD_ALWAYS,
                           GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB));
                           /*GST_STATIC_CAPS (GST_VIDEO_CAPS_RGBx ";" \
                                            GST_VIDEO_CAPS_BGRx ";" \
                                            GST_VIDEO_CAPS_RGB ";" \
                                            GST_VIDEO_CAPS_BGR));*/
static GstStaticPadTemplate sinktemplate_yuv
= GST_STATIC_PAD_TEMPLATE ("sink",
                           GST_PAD_SINK,
                           GST_PAD_ALWAYS,
                           GST_STATIC_CAPS (GST_VIDEO_CAPS_YUV ("{ IYUV, I420, YV12 }")));

G_DEFINE_TYPE (MGstVideoSink, m_gst_video_sink, GST_TYPE_BASE_SINK)

static void
m_gst_video_sink_init (MGstVideoSink *sink)
{
  sink->w = sink->h = 0;
  sink->frameskip = 1;
}

static GstFlowReturn
m_gst_video_sink_render (GstBaseSink *bsink, GstBuffer *buffer)
{
  MGstVideoSink *sink;
  static int n = 1;

  sink = M_GST_VIDEO_SINK (bsink);

  if (buffer == NULL || G_UNLIKELY (!GST_IS_BUFFER (buffer))) {
    return FALSE;
  }

  // take ownership
  gst_buffer_ref (buffer);

  if (--n==0) {
    sink->frame_cb (buffer, sink->user_data);
    n = sink->frameskip;
  }
  else {
    gst_buffer_unref (buffer);
  }

  return GST_FLOW_OK;
}

static gboolean
m_gst_video_sink_set_caps (GstBaseSink *bsink,
                             GstCaps     *caps)
{

  MGstVideoSink        *sink;
  GstStructure           *structure;

  sink = M_GST_VIDEO_SINK (bsink);

  if (sink->yuv_mode) {
    gst_caps_intersect 
      (gst_static_pad_template_get_caps (&sinktemplate_yuv),
       caps);
  } else {
    gst_caps_intersect 
      (gst_static_pad_template_get_caps (&sinktemplate_rgb),
       caps);
  }

  structure = gst_caps_get_structure (caps, 0);

  gst_structure_get_int (structure, "width",  &sink->w);
  gst_structure_get_int (structure, "height", &sink->h);

  //sink->ready_cb(sink->user_data);
  return TRUE;
}

static GstCaps*
m_gst_video_sink_get_caps (GstBaseSink *bsink)
{
  MGstVideoSink        *sink;

  sink = M_GST_VIDEO_SINK (bsink);

  if (sink->yuv_mode)
    return gst_static_pad_template_get_caps (&sinktemplate_yuv);
  else
    return gst_static_pad_template_get_caps (&sinktemplate_rgb);
}


static void
m_gst_video_sink_class_init (MGstVideoSinkClass *klass)
{
  GstBaseSinkClass *gstbase_sink_class = GST_BASE_SINK_CLASS (klass);
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);

  gstbase_sink_class->render   = m_gst_video_sink_render;
  gstbase_sink_class->preroll  = m_gst_video_sink_render;
  gstbase_sink_class->set_caps = m_gst_video_sink_set_caps;
  gstbase_sink_class->get_caps = m_gst_video_sink_get_caps;

  gst_element_class_add_pad_template 
    (element_class,
     gst_static_pad_template_get (&sinktemplate));
}

GstElement *
m_gst_video_sink_new ()
{
  GstElement *sink = g_object_new (M_GST_TYPE_VIDEO_SINK, NULL);
  M_GST_VIDEO_SINK (sink)->yuv_mode = 0;

  return sink;
}

GstElement *
m_gst_video_sink_yuv_new ()
{
  GstElement *sink = g_object_new (M_GST_TYPE_VIDEO_SINK, NULL);
  M_GST_VIDEO_SINK (sink)->yuv_mode = 1;

  return sink;
}