summaryrefslogtreecommitdiff
path: root/sys/dxr3/gstdxr3videosink.c
blob: d760f9de1fae765dd8e05e257d11e85bebdf8519 (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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* GStreamer DXR3 Hardware MPEG video decoder plugin
 * Copyright (C) <2002> Rehan Khwaja <rehankhwaja@yahoo.com>
 *
 * 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
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */


#include <string.h>
#include <fcntl.h>
#include "gstdxr3videosink.h"
#include <sys/ioctl.h>
#include <linux/em8300.h>


enum {
  ARG_0,
  ARG_TV_MODE,
  ARG_DEVICE,
  ARG_ASPECT_RATIO
  /* TODO -
   * DIGITAL/ANALOG AUDIO OUT
   * OVERLAY MODE
   * SET BCS??  what's that?
   * OVERLAY ATTRIBUTES - where on the screen to put image
   * OVERLAY WINDOW - as above
   * OVERLAY KEYCOLOR
   * OVERLAY SIGNAL MODE
   * PLAYMODE - pausing, playing, slow forward etc
   */
};

static void	gst_dxr3_video_sink_class_init	(GstDxr3VideoSinkClass *klass);
static void	gst_dxr3_video_sink_init		(GstDxr3VideoSink *example);

static void	gst_dxr3_video_sink_chain		(GstPad *pad, GstBuffer *buf);

static void	gst_dxr3_video_sink_set_property	(GObject *object, guint prop_id,
						 const GValue *value, GParamSpec *pspec);
static void	gst_dxr3_video_sink_get_property	(GObject *object, guint prop_id,
						 GValue *value, GParamSpec *pspec);
static gboolean	gst_dxr3_video_sink_handle_event	(GstPad *pad, GstEvent *event);
static void	gst_dxr3_video_sink_set_clock		(GstElement *element, GstClock *clock);


static GstElementClass *parent_class = NULL;


#define GST_TYPE_DXR3_VIDEO_SINK_ASPECT_RATIOS (gst_dxr3_video_sink_aspect_ratios_get_type())
static GType
gst_dxr3_video_sink_aspect_ratios_get_type(void) {
  static GType dxr3_video_sink_aspect_ratio_type = 0;
  static GEnumValue dxr3_video_sink_aspect_ratios[] = {
    {0, "0", "4:3"},
    {1, "1", "16:9"},
    {0, NULL, NULL},
  };
  if (!dxr3_video_sink_aspect_ratio_type) {
    dxr3_video_sink_aspect_ratio_type = g_enum_register_static("GstDxr3VideoSinkAspectRatios", dxr3_video_sink_aspect_ratios);
  }
  return dxr3_video_sink_aspect_ratio_type;
}

#define GST_TYPE_DXR3_VIDEO_SINK_TV_MODES (gst_dxr3_video_sink_tv_modes_get_type())
static GType
gst_dxr3_video_sink_tv_modes_get_type(void) {
  static GType dxr3_video_sink_tv_mode_type = 0;
  static GEnumValue dxr3_video_sink_tv_modes[] = {
    {0, "0", "NTSC"},
    {1, "1", "PAL"},
    {0, NULL, NULL},
  };
  if (!dxr3_video_sink_tv_mode_type) {
    dxr3_video_sink_tv_mode_type = g_enum_register_static("GstDxr3VideoSinkTvModes", dxr3_video_sink_tv_modes);
  }
  return dxr3_video_sink_tv_mode_type;
}

static void
gst_dxr3_video_sink_set_clock(GstElement *element, GstClock *clock)
{
  GstDxr3VideoSink *dxr3_video_sink;

  dxr3_video_sink = GST_DXR3_VIDEO_SINK(element);
  dxr3_video_sink->clock = clock;
}

GType
gst_dxr3_video_sink_get_type(void)
{
  static GType dxr3_video_sink_type = 0;

  if (!dxr3_video_sink_type) {
    static const GTypeInfo dxr3_video_sink_info = {
      sizeof(GstDxr3VideoSinkClass),      NULL,
      NULL,
      (GClassInitFunc)gst_dxr3_video_sink_class_init,
      NULL,
      NULL,
      sizeof(GstDxr3VideoSink),
      0,
      (GInstanceInitFunc)gst_dxr3_video_sink_init,
    };
    dxr3_video_sink_type = g_type_register_static(GST_TYPE_ELEMENT, "GstDxr3VideoSink", &dxr3_video_sink_info, 0);
  }
  return dxr3_video_sink_type;
}

static gchar *
gst_dxr3_video_sink_get_control_device(GstDxr3VideoSink *dxr3_video_sink)
{
  return g_strdup_printf("/dev/em8300-%d", dxr3_video_sink->device_number);
}

static gchar *
gst_dxr3_video_sink_get_video_device(GstDxr3VideoSink *dxr3_video_sink)
{
  return g_strdup_printf("/dev/em8300_mv-%d", dxr3_video_sink->device_number);
}

static void
gst_dxr3_video_sink_class_init (GstDxr3VideoSinkClass *klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

  gobject_class = (GObjectClass*)klass;
  gstelement_class = (GstElementClass*)klass;

  parent_class = g_type_class_ref(GST_TYPE_ELEMENT);

  g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_TV_MODE,
    g_param_spec_enum("tv-mode","tv-mode","sets NTSC/PAL output format",
                     GST_TYPE_DXR3_VIDEO_SINK_TV_MODES, 0, G_PARAM_READWRITE));

  g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_DEVICE,
    g_param_spec_int("device","device","sets/returns board number",
                     0,3,0,G_PARAM_READWRITE));

  g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_ASPECT_RATIO,
    g_param_spec_enum("aspect-ratio", "aspect-ratio", "sets/returns aspect ratio",
                     GST_TYPE_DXR3_VIDEO_SINK_ASPECT_RATIOS, 0, G_PARAM_READWRITE));

  gobject_class->set_property = gst_dxr3_video_sink_set_property;
  gobject_class->get_property = gst_dxr3_video_sink_get_property;
}

static void
gst_dxr3_video_sink_open_device(GstDxr3VideoSink *dxr3_video_sink)
{
  gchar *video_device_path;

  video_device_path = gst_dxr3_video_sink_get_video_device(dxr3_video_sink);
  if (dxr3_video_sink->device){
    fclose(dxr3_video_sink->device);
  }
  /* this is what disksink uses */
  dxr3_video_sink->device = fopen(video_device_path, "w");

  g_free(video_device_path);
}

static void
gst_dxr3_video_sink_init(GstDxr3VideoSink *dxr3_video_sink)
{
  dxr3_video_sink->sinkpad = gst_pad_new_from_template (
		  GST_PAD_TEMPLATE_GET (dxr3_video_sink_factory), "video_sink");
  gst_pad_set_chain_function(dxr3_video_sink->sinkpad, gst_dxr3_video_sink_chain);
  gst_element_add_pad(GST_ELEMENT(dxr3_video_sink),dxr3_video_sink->sinkpad);

  dxr3_video_sink->device_number = 0;
  dxr3_video_sink->device = 0;
  /* FIXME - should only have device open when necessary
   * which probably requires handling events?
   * also, when is this device closed?
   */
  gst_dxr3_video_sink_open_device(dxr3_video_sink);

  GST_FLAG_SET (GST_ELEMENT(dxr3_video_sink), GST_ELEMENT_EVENT_AWARE);
  gst_pad_set_event_function(dxr3_video_sink->sinkpad, gst_dxr3_video_sink_handle_event);

  dxr3_video_sink->clock = NULL;
  GST_ELEMENT(dxr3_video_sink)->setclockfunc = gst_dxr3_video_sink_set_clock;
}

static gboolean
gst_dxr3_video_sink_handle_event(GstPad *pad, GstEvent *event)
{
  GstEventType type;

  type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;

  switch (type) {
    case GST_EVENT_SEEK:
      g_print("seek event\n\n");
      break;
    case GST_EVENT_NEW_MEDIA:
      g_print("new media event\n\n");
      break;
    case GST_EVENT_FLUSH:
      g_print("flush event\n\n");
      break;
    default:
      g_print("event\n\n");
      gst_pad_event_default (pad, event);
      break;
  }

  return TRUE;
}

static void
gst_dxr3_video_sink_chain (GstPad *pad, GstBuffer *buf)
{
  GstDxr3VideoSink *dxr3_video_sink;
  long pts;

  /* Some of these checks are of dubious value, since if there were not
   * already true, the chain function would never be called.
   */
  g_return_if_fail(pad != NULL);
  g_return_if_fail(GST_IS_PAD(pad));
  g_return_if_fail(buf != NULL);

  dxr3_video_sink = GST_DXR3_VIDEO_SINK(gst_pad_get_parent (pad));

  g_return_if_fail(dxr3_video_sink != NULL);
  g_return_if_fail(GST_IS_DXR3_VIDEO_SINK(dxr3_video_sink));

  /* Copy the data in the incoming buffer onto the device. */
  fwrite(GST_BUFFER_DATA (buf), 1, GST_BUFFER_SIZE (buf), dxr3_video_sink->device);
  pts = (long)GST_BUFFER_TIMESTAMP(buf);
  if (-1 == ioctl((int)dxr3_video_sink->device, EM8300_IOCTL_VIDEO_SETPTS, &pts)){
    GST_DEBUG(0, "FAILED call to EM8300_IOCTL_VIDEO_SETPTS\n");
  }

  gst_buffer_unref (buf);
}

static void
gst_dxr3_video_sink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
  GstDxr3VideoSink *dxr3_video_sink;
  int device;
  gchar *device_path;
  int tv_mode_ioctl, aspect_ratio_ioctl;

  g_return_if_fail(GST_IS_DXR3_VIDEO_SINK(object));

  dxr3_video_sink = GST_DXR3_VIDEO_SINK(object);

  switch (prop_id) {
    case ARG_TV_MODE:
      device_path = gst_dxr3_video_sink_get_control_device(dxr3_video_sink);
      device = open(device_path, O_WRONLY);
      if (device == -1){
        GST_DEBUG(0, "failed to open control device\n");
	break;
      }
      switch(g_value_get_int(value)){
      case 0:
        tv_mode_ioctl = EM8300_VIDEOMODE_NTSC;
	break;
      case 1:
        tv_mode_ioctl = EM8300_VIDEOMODE_PAL;
	break;
      case 2:
        tv_mode_ioctl = EM8300_VIDEOMODE_PAL60;
	break;
      }
      if (-1 == ioctl(device, EM8300_IOCTL_SET_VIDEOMODE, &tv_mode_ioctl)){
        GST_DEBUG (0,"failed to set tv-mode\n");
      }
      close(device);
      break;
    case ARG_DEVICE:
      dxr3_video_sink->device_number = g_value_get_int(value);
      gst_dxr3_video_sink_open_device(dxr3_video_sink);
      break;
    case ARG_ASPECT_RATIO:
      device_path = gst_dxr3_video_sink_get_control_device(dxr3_video_sink);
      device = open(device_path, O_WRONLY);
      if (device == -1){
        GST_DEBUG(0, "failed to open control device\n");
	break;
      }
      switch(g_value_get_enum(value)){
      case 0:
        aspect_ratio_ioctl = EM8300_ASPECTRATIO_4_3;
	break;
      case 1:
        aspect_ratio_ioctl = EM8300_ASPECTRATIO_16_9;
	break;
      }
      if (-1 == ioctl(device, EM8300_IOCTL_SET_ASPECTRATIO, &aspect_ratio_ioctl)){
        GST_DEBUG(0, "failed to set aspect-ratio\n");
      }
      break;
    default:
      break;
  }
}

static void
gst_dxr3_video_sink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
  GstDxr3VideoSink *dxr3_video_sink;
  gint device;
  gchar *device_path;
  int tv_mode_ioctl, aspect_ratio_ioctl;

  g_return_if_fail(GST_IS_DXR3_VIDEO_SINK(object));
  dxr3_video_sink = GST_DXR3_VIDEO_SINK(object);

  switch (prop_id) {
    case ARG_TV_MODE:
      device_path = gst_dxr3_video_sink_get_control_device(dxr3_video_sink);
      tv_mode_ioctl = 0;
      device = open(device_path, O_WRONLY);
      if (-1 == device){
        GST_DEBUG(0, "failed to open control device\n");
	break;
      }
      if (-1 == ioctl(device, EM8300_IOCTL_GET_VIDEOMODE, &tv_mode_ioctl)){
        GST_DEBUG(0, "failed to get tv-mode\n");
      }
      close(device);

      switch(tv_mode_ioctl){
      case EM8300_VIDEOMODE_NTSC:
        g_value_set_enum(value, 0);
	break;
      case EM8300_VIDEOMODE_PAL:
        g_value_set_enum(value, 1);
	break;
      case EM8300_VIDEOMODE_PAL60:
        g_value_set_enum(value, 2);
	break;
      }
      break;
    case ARG_DEVICE:
      g_value_set_int(value, dxr3_video_sink->device_number);
      break;
    case ARG_ASPECT_RATIO:
      device_path = gst_dxr3_video_sink_get_control_device(dxr3_video_sink);
      aspect_ratio_ioctl = 0;
      device = open(device_path, O_WRONLY);
      if (-1 == device){
        GST_DEBUG(0, "failed to open control device\n");
	break;
      }
      if (-1 == ioctl(device, EM8300_IOCTL_GET_ASPECTRATIO, &aspect_ratio_ioctl)){
        GST_DEBUG(0, "failed to get aspect ratio\n");
      }
      close(device);

      switch(aspect_ratio_ioctl){
      case EM8300_ASPECTRATIO_4_3:
        g_value_set_enum(value, 0);
	break;
      case EM8300_ASPECTRATIO_16_9:
        g_value_set_enum(value, 1);
        break;
      }
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}