9a4c755c1043ba80ca373470c192828bdf5cc51b
1 /* GStreamer
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
20 /**
21 * Copyright (C) 2001 Billy Biggs <vektor@dumbterm.net>.
22 *
23 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or (at
26 * your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful, but
29 * WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 * General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 */
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <time.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <sys/ioctl.h>
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <sys/types.h>
52 #include <dirent.h>
53 #include <errno.h>
54 //#include <linux/cdrom.h>
55 #include <assert.h>
57 #include <dvdreadsrc.h>
59 #include <dvdread/dvd_reader.h>
60 #include <dvdread/ifo_types.h>
61 #include <dvdread/ifo_read.h>
62 #include <dvdread/nav_read.h>
63 #include <dvdread/nav_print.h>
65 struct _DVDReadSrcPrivate {
66 GstElement element;
67 /* pads */
68 GstPad *srcpad;
70 /* location */
71 gchar *location;
73 gboolean new_seek;
75 int title, chapter, angle;
76 int pgc_id, start_cell, cur_cell, cur_pack;
77 int ttn, pgn, next_cell;
78 dvd_reader_t *dvd;
79 dvd_file_t *dvd_title;
80 ifo_handle_t *vmg_file;
81 tt_srpt_t *tt_srpt;
82 ifo_handle_t *vts_file;
83 vts_ptt_srpt_t *vts_ptt_srpt;
84 pgc_t *cur_pgc;
85 };
88 GstElementDetails dvdreadsrc_details = {
89 "DVD Source",
90 "Source/File/DVD",
91 "Access a DVD title/chapter/angle using libdvdread",
92 "Erik Walthinsen <omega@cse.ogi.edu>",
93 };
96 /* DVDReadSrc signals and args */
97 enum {
98 /* FILL ME */
99 LAST_SIGNAL
100 };
102 enum {
103 ARG_0,
104 ARG_LOCATION,
105 ARG_TITLE,
106 ARG_CHAPTER,
107 ARG_ANGLE
108 };
110 static void dvdreadsrc_base_init (gpointer g_class);
111 static void dvdreadsrc_class_init (DVDReadSrcClass *klass);
112 static void dvdreadsrc_init (DVDReadSrc *dvdreadsrc);
114 static void dvdreadsrc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
115 static void dvdreadsrc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
117 /*static GstData * dvdreadsrc_get (GstPad *pad); */
118 static void dvdreadsrc_loop (GstElement *element);
119 /*static GstBuffer * dvdreadsrc_get_region (GstPad *pad,gulong offset,gulong size); */
121 static GstElementStateReturn dvdreadsrc_change_state (GstElement *element);
124 static GstElementClass *parent_class = NULL;
125 /*static guint dvdreadsrc_signals[LAST_SIGNAL] = { 0 }; */
127 GType
128 dvdreadsrc_get_type (void)
129 {
130 static GType dvdreadsrc_type = 0;
132 if (!dvdreadsrc_type) {
133 static const GTypeInfo dvdreadsrc_info = {
134 sizeof(DVDReadSrcClass),
135 dvdreadsrc_base_init,
136 NULL,
137 (GClassInitFunc)dvdreadsrc_class_init,
138 NULL,
139 NULL,
140 sizeof(DVDReadSrc),
141 0,
142 (GInstanceInitFunc)dvdreadsrc_init,
143 };
144 dvdreadsrc_type = g_type_register_static (GST_TYPE_ELEMENT, "DVDReadSrc", &dvdreadsrc_info, 0);
145 }
146 return dvdreadsrc_type;
147 }
149 static void
150 dvdreadsrc_base_init (gpointer g_class)
151 {
152 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
154 gst_element_class_set_details (element_class, &dvdreadsrc_details);
155 }
157 static void
158 dvdreadsrc_class_init (DVDReadSrcClass *klass)
159 {
160 GObjectClass *gobject_class;
161 GstElementClass *gstelement_class;
163 gobject_class = (GObjectClass*)klass;
164 gstelement_class = (GstElementClass*)klass;
166 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
168 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_LOCATION,
169 g_param_spec_string("location","location","location",
170 NULL, G_PARAM_READWRITE));
171 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_TITLE,
172 g_param_spec_int("title","title","title",
173 0,G_MAXINT,0,G_PARAM_READWRITE));
174 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_CHAPTER,
175 g_param_spec_int("chapter","chapter","chapter",
176 0,G_MAXINT,0,G_PARAM_READWRITE));
177 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_ANGLE,
178 g_param_spec_int("angle","angle","angle",
179 0,G_MAXINT,0,G_PARAM_READWRITE));
181 gobject_class->set_property = GST_DEBUG_FUNCPTR(dvdreadsrc_set_property);
182 gobject_class->get_property = GST_DEBUG_FUNCPTR(dvdreadsrc_get_property);
184 gstelement_class->change_state = dvdreadsrc_change_state;
185 }
187 static void
188 dvdreadsrc_init (DVDReadSrc *dvdreadsrc)
189 {
190 dvdreadsrc->priv = g_new(DVDReadSrcPrivate, 1);
191 dvdreadsrc->priv->srcpad = gst_pad_new ("src", GST_PAD_SRC);
192 gst_element_add_pad (GST_ELEMENT (dvdreadsrc), dvdreadsrc->priv->srcpad);
193 gst_element_set_loop_function (GST_ELEMENT(dvdreadsrc), GST_DEBUG_FUNCPTR(dvdreadsrc_loop));
195 dvdreadsrc->priv->location = g_strdup("/dev/dvd");
196 dvdreadsrc->priv->new_seek = FALSE;
197 dvdreadsrc->priv->title = 1;
198 dvdreadsrc->priv->chapter = 1;
199 dvdreadsrc->priv->angle = 1;
200 }
202 /* FIXME: this code is not being used */
203 #ifdef PLEASEFIXTHISCODE
204 static void
205 dvdreadsrc_destroy (DVDReadSrc *dvdreadsrc)
206 {
207 /* FIXME */
208 g_print("FIXME\n");
209 g_free(dvdreadsrc->priv);
210 }
211 #endif
213 static void
214 dvdreadsrc_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
215 {
216 DVDReadSrc *src;
217 DVDReadSrcPrivate *priv;
219 /* it's not null if we got it, but it might not be ours */
220 g_return_if_fail (GST_IS_DVDREADSRC (object));
222 src = DVDREADSRC (object);
223 priv = src->priv;
225 switch (prop_id) {
226 case ARG_LOCATION:
227 /* the element must be stopped in order to do this */
228 /*g_return_if_fail(!GST_FLAG_IS_SET(src,GST_STATE_RUNNING)); */
230 if (priv->location)
231 g_free (priv->location);
232 /* clear the filename if we get a NULL (is that possible?) */
233 if (g_value_get_string (value) == NULL)
234 priv->location = g_strdup("/dev/dvd");
235 /* otherwise set the new filename */
236 else
237 priv->location = g_strdup (g_value_get_string (value));
238 break;
239 case ARG_TITLE:
240 priv->title = g_value_get_int (value) - 1;
241 priv->new_seek = TRUE;
242 break;
243 case ARG_CHAPTER:
244 priv->chapter = g_value_get_int (value) - 1;
245 priv->new_seek = TRUE;
246 break;
247 case ARG_ANGLE:
248 priv->angle = g_value_get_int (value) - 1;
249 break;
250 default:
251 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252 break;
253 }
255 }
257 static void
258 dvdreadsrc_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
259 {
260 DVDReadSrc *src;
261 DVDReadSrcPrivate *priv;
263 /* it's not null if we got it, but it might not be ours */
264 g_return_if_fail (GST_IS_DVDREADSRC (object));
266 src = DVDREADSRC (object);
267 priv = src->priv;
269 switch (prop_id) {
270 case ARG_LOCATION:
271 g_value_set_string (value, priv->location);
272 break;
273 case ARG_TITLE:
274 g_value_set_int (value, priv->title + 1);
275 break;
276 case ARG_CHAPTER:
277 g_value_set_int (value, priv->chapter + 1);
278 break;
279 case ARG_ANGLE:
280 g_value_set_int (value, priv->angle + 1);
281 break;
282 default:
283 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
284 break;
285 }
286 }
288 /**
289 * Returns true if the pack is a NAV pack. This check is clearly insufficient,
290 * and sometimes we incorrectly think that valid other packs are NAV packs. I
291 * need to make this stronger.
292 */
293 static int
294 is_nav_pack( unsigned char *buffer )
295 {
296 return ( buffer[ 41 ] == 0xbf && buffer[ 1027 ] == 0xbf );
297 }
299 static int
300 _open(DVDReadSrcPrivate *priv, const gchar *location)
301 {
302 g_return_val_if_fail(priv != NULL, -1);
303 g_return_val_if_fail(location != NULL, -1);
305 /**
306 * Open the disc.
307 */
308 priv->dvd = DVDOpen( location );
309 if( !priv->dvd ) {
310 fprintf( stderr, "Couldn't open DVD: %s\n", location );
311 return -1;
312 }
315 /**
316 * Load the video manager to find out the information about the titles on
317 * this disc.
318 */
319 priv->vmg_file = ifoOpen( priv->dvd, 0 );
320 if( !priv->vmg_file ) {
321 fprintf( stderr, "Can't open VMG info.\n" );
322 DVDClose( priv->dvd );
323 return -1;
324 }
325 priv->tt_srpt = priv->vmg_file->tt_srpt;
327 return 0;
328 }
330 static int
331 _close(DVDReadSrcPrivate *priv)
332 {
333 ifoClose( priv->vts_file );
334 ifoClose( priv->vmg_file );
335 DVDCloseFile( priv->dvd_title );
336 DVDClose( priv->dvd );
337 return 0;
338 }
340 static int
341 _seek(DVDReadSrcPrivate *priv, int title, int chapter, int angle)
342 {
343 /**
344 * Make sure our title number is valid.
345 */
346 fprintf( stderr, "There are %d titles on this DVD.\n",
347 priv->tt_srpt->nr_of_srpts );
348 if( title < 0 || title >= priv->tt_srpt->nr_of_srpts ) {
349 fprintf( stderr, "Invalid title %d.\n", title + 1 );
350 ifoClose( priv->vmg_file );
351 DVDClose( priv->dvd );
352 return -1;
353 }
356 /**
357 * Make sure the chapter number is valid for this title.
358 */
359 fprintf( stderr, "There are %d chapters in this title.\n",
360 priv->tt_srpt->title[ title ].nr_of_ptts );
362 if( chapter < 0 || chapter >= priv->tt_srpt->title[ title ].nr_of_ptts ) {
363 fprintf( stderr, "Invalid chapter %d\n", chapter + 1 );
364 ifoClose( priv->vmg_file );
365 DVDClose( priv->dvd );
366 return -1;
367 }
370 /**
371 * Make sure the angle number is valid for this title.
372 */
373 fprintf( stderr, "There are %d angles in this title.\n",
374 priv->tt_srpt->title[ title ].nr_of_angles );
375 if( angle < 0 || angle >= priv->tt_srpt->title[ title ].nr_of_angles ) {
376 fprintf( stderr, "Invalid angle %d\n", angle + 1 );
377 ifoClose( priv->vmg_file );
378 DVDClose( priv->dvd );
379 return -1;
380 }
383 /**
384 * Load the VTS information for the title set our title is in.
385 */
386 priv->vts_file = ifoOpen( priv->dvd, priv->tt_srpt->title[ title ].title_set_nr );
387 if( !priv->vts_file ) {
388 fprintf( stderr, "Can't open the title %d info file.\n",
389 priv->tt_srpt->title[ title ].title_set_nr );
390 ifoClose( priv->vmg_file );
391 DVDClose( priv->dvd );
392 return -1;
393 }
396 /**
397 * Determine which program chain we want to watch. This is based on the
398 * chapter number.
399 */
400 priv->ttn = priv->tt_srpt->title[ title ].vts_ttn;
401 priv->vts_ptt_srpt = priv->vts_file->vts_ptt_srpt;
402 priv->pgc_id = priv->vts_ptt_srpt->title[ priv->ttn - 1 ].ptt[ chapter ].pgcn;
403 priv->pgn = priv->vts_ptt_srpt->title[ priv->ttn - 1 ].ptt[ chapter ].pgn;
404 priv->cur_pgc = priv->vts_file->vts_pgcit->pgci_srp[ priv->pgc_id - 1 ].pgc;
405 priv->start_cell = priv->cur_pgc->program_map[ priv->pgn - 1 ] - 1;
408 /**
409 * We've got enough info, time to open the title set data.
410 */
411 priv->dvd_title = DVDOpenFile( priv->dvd, priv->tt_srpt->title[ title ].title_set_nr,
412 DVD_READ_TITLE_VOBS );
413 if( !priv->dvd_title ) {
414 fprintf( stderr, "Can't open title VOBS (VTS_%02d_1.VOB).\n",
415 priv->tt_srpt->title[ title ].title_set_nr );
416 ifoClose( priv->vts_file );
417 ifoClose( priv->vmg_file );
418 DVDClose( priv->dvd );
419 return -1;
420 }
422 return 0;
423 }
425 static void
426 dvdreadsrc_loop (GstElement *element)
427 {
428 DVDReadSrc *dvdreadsrc;
429 DVDReadSrcPrivate *priv;
431 g_return_if_fail (element != NULL);
432 g_return_if_fail (GST_IS_DVDREADSRC (element));
434 dvdreadsrc = DVDREADSRC (element);
435 priv = dvdreadsrc->priv;
436 g_return_if_fail (GST_FLAG_IS_SET (dvdreadsrc, DVDREADSRC_OPEN));
438 /**
439 * Playback by cell in this pgc, starting at the cell for our chapter.
440 */
441 priv->next_cell = priv->start_cell;
442 for( priv->cur_cell = priv->start_cell; priv->next_cell < priv->cur_pgc->nr_of_cells; ) {
444 priv->cur_cell = priv->next_cell;
446 /* Check if we're entering an angle block. */
447 if( priv->cur_pgc->cell_playback[ priv->cur_cell ].block_type
448 == BLOCK_TYPE_ANGLE_BLOCK ) {
449 int i;
451 priv->cur_cell += priv->angle;
452 for( i = 0;; ++i ) {
453 if( priv->cur_pgc->cell_playback[ priv->cur_cell + i ].block_mode
454 == BLOCK_MODE_LAST_CELL ) {
455 priv->next_cell = priv->cur_cell + i + 1;
456 break;
457 }
458 }
459 } else {
460 priv->next_cell = priv->cur_cell + 1;
461 }
464 /**
465 * We loop until we're out of this cell.
466 */
467 for( priv->cur_pack = priv->cur_pgc->cell_playback[ priv->cur_cell ].first_sector;
468 priv->cur_pack < priv->cur_pgc->cell_playback[ priv->cur_cell ].last_sector; ) {
470 dsi_t dsi_pack;
471 unsigned int next_vobu, next_ilvu_start, cur_output_size;
472 GstBuffer *buf;
473 unsigned char *data;
474 int len;
476 /* create the buffer */
477 /* FIXME: should eventually use a bufferpool for this */
478 buf = gst_buffer_new ();
479 g_return_if_fail (buf);
481 /* allocate the space for the buffer data */
482 data = g_malloc (1024 * DVD_VIDEO_LB_LEN);
483 GST_BUFFER_DATA (buf) = data;
485 g_return_if_fail (GST_BUFFER_DATA (buf) != NULL);
487 /**
488 * Read NAV packet.
489 */
490 len = DVDReadBlocks( priv->dvd_title, priv->cur_pack, 1, data );
491 if( len == 0 ) {
492 fprintf( stderr, "Read failed for block %d\n", priv->cur_pack );
493 _close(priv);
494 gst_element_set_eos (GST_ELEMENT (dvdreadsrc));
495 return;
496 }
497 assert( is_nav_pack( data ) );
500 /**
501 * Parse the contained dsi packet.
502 */
503 navRead_DSI( &dsi_pack, &(data[ DSI_START_BYTE ]) );
504 assert( priv->cur_pack == dsi_pack.dsi_gi.nv_pck_lbn );
505 /*navPrint_DSI(&dsi_pack); */
508 /**
509 * Determine where we go next. These values are the ones we mostly
510 * care about.
511 */
512 next_ilvu_start = priv->cur_pack
513 + dsi_pack.sml_agli.data[ priv->angle ].address;
514 cur_output_size = dsi_pack.dsi_gi.vobu_ea;
517 /**
518 * If we're not at the end of this cell, we can determine the next
519 * VOBU to display using the VOBU_SRI information section of the
520 * DSI. Using this value correctly follows the current angle,
521 * avoiding the doubled scenes in The Matrix, and makes our life
522 * really happy.
523 *
524 * Otherwise, we set our next address past the end of this cell to
525 * force the code above to go to the next cell in the program.
526 */
527 if( dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL ) {
528 next_vobu = priv->cur_pack
529 + ( dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
530 } else {
531 next_vobu = priv->cur_pack + cur_output_size + 1;
532 }
534 assert( cur_output_size < 1024 );
535 priv->cur_pack++;
537 /**
538 * Read in and output cursize packs.
539 */
540 len = DVDReadBlocks( priv->dvd_title, priv->cur_pack,
541 cur_output_size, data );
542 if( len != cur_output_size ) {
543 fprintf( stderr, "Read failed for %d blocks at %d\n",
544 cur_output_size, priv->cur_pack );
545 _close(priv);
546 gst_element_set_eos (GST_ELEMENT (dvdreadsrc));
547 return;
548 }
550 GST_BUFFER_SIZE(buf) = cur_output_size * DVD_VIDEO_LB_LEN;
551 gst_pad_push(priv->srcpad, GST_DATA (buf));
552 priv->cur_pack = next_vobu;
553 }
554 }
555 }
557 #if 0
558 static int
559 _read(DVDReadSrcPrivate *priv, int angle, int new_seek, GstBuffer *buf)
560 {
561 unsigned char *data;
563 data = GST_BUFFER_DATA(buf);
565 /**
566 * Playback by cell in this pgc, starting at the cell for our chapter.
567 */
568 if (new_seek) {
569 priv->next_cell = priv->start_cell;
570 priv->cur_cell = priv->start_cell;
571 }
572 if (priv->next_cell < priv->cur_pgc->nr_of_cells) {
574 priv->cur_cell = priv->next_cell;
576 /* Check if we're entering an angle block. */
577 if( priv->cur_pgc->cell_playback[ priv->cur_cell ].block_type
578 == BLOCK_TYPE_ANGLE_BLOCK ) {
579 int i;
581 priv->cur_cell += angle;
582 for( i = 0;; ++i ) {
583 if( priv->cur_pgc->cell_playback[ priv->cur_cell + i ].block_mode
584 == BLOCK_MODE_LAST_CELL ) {
585 priv->next_cell = priv->cur_cell + i + 1;
586 break;
587 }
588 }
589 } else {
590 priv->next_cell = priv->cur_cell + 1;
591 }
594 /**
595 * We loop until we're out of this cell.
596 */
597 if (priv->new_cell) {
598 priv->cur_pack = priv->cur_pgc->cell_playback[ priv->cur_cell ].first_sector;
599 priv->new_cell = FALSE;
600 }
602 if (priv->cur_pack < priv->cur_pgc->cell_playback[ priv->cur_cell ].last_sector; ) {
604 dsi_t dsi_pack;
605 unsigned int next_vobu, next_ilvu_start, cur_output_size;
606 int len;
609 /**
610 * Read NAV packet.
611 */
612 len = DVDReadBlocks( priv->title, priv->cur_pack, 1, data );
613 if( len == 0 ) {
614 fprintf( stderr, "Read failed for block %d\n", priv->cur_pack );
615 _close(priv);
616 return -1;
617 }
618 assert( is_nav_pack( data ) );
621 /**
622 * Parse the contained dsi packet.
623 */
624 navRead_DSI( &dsi_pack, &(data[ DSI_START_BYTE ]), sizeof(dsi_t) );
625 assert( priv->cur_pack == dsi_pack.dsi_gi.nv_pck_lbn );
628 /**
629 * Determine where we go next. These values are the ones we mostly
630 * care about.
631 */
632 next_ilvu_start = priv->cur_pack
633 + dsi_pack.sml_agli.data[ angle ].address;
634 cur_output_size = dsi_pack.dsi_gi.vobu_ea;
637 /**
638 * If we're not at the end of this cell, we can determine the next
639 * VOBU to display using the VOBU_SRI information section of the
640 * DSI. Using this value correctly follows the current angle,
641 * avoiding the doubled scenes in The Matrix, and makes our life
642 * really happy.
643 *
644 * Otherwise, we set our next address past the end of this cell to
645 * force the code above to go to the next cell in the program.
646 */
647 if( dsi_pack.vobu_sri.next_vobu != SRI_END_OF_CELL ) {
648 next_vobu = priv->cur_pack
649 + ( dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
650 } else {
651 next_vobu = priv->cur_pack + cur_output_size + 1;
652 }
654 assert( cur_output_size < 1024 );
655 priv->cur_pack++;
657 /**
658 * Read in and output cursize packs.
659 */
660 len = DVDReadBlocks( priv->title, priv->cur_pack,
661 cur_output_size, data );
662 if( len != cur_output_size ) {
663 fprintf( stderr, "Read failed for %d blocks at %d\n",
664 cur_output_size, priv->cur_pack );
665 _close(priv);
666 return -1;
667 }
669 GST_BUFFER_SIZE(buf) = cur_output_size * DVD_VIDEO_LB_LEN;
670 priv->cur_pack = next_vobu;
671 }
672 } else {
673 return -1;
674 }
676 return 0;
677 }
679 static GstData *
680 dvdreadsrc_get (GstPad *pad)
681 {
682 DVDReadSrc *dvdreadsrc;
683 DVDReadSrcPrivate *priv;
684 GstBuffer *buf;
686 g_return_val_if_fail (pad != NULL, NULL);
687 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
689 dvdreadsrc = DVDREADSRC (gst_pad_get_parent (pad));
690 priv = dvdreadsrc->priv;
691 g_return_val_if_fail (GST_FLAG_IS_SET (dvdreadsrc, DVDREADSRC_OPEN),NULL);
693 /* create the buffer */
694 /* FIXME: should eventually use a bufferpool for this */
695 buf = gst_buffer_new ();
696 g_return_val_if_fail (buf, NULL);
698 /* allocate the space for the buffer data */
699 GST_BUFFER_DATA (buf) = g_malloc (1024 * DVD_VIDEO_LB_LEN);
700 g_return_val_if_fail (GST_BUFFER_DATA (buf) != NULL, NULL);
702 if (priv->new_seek) {
703 _seek(priv, priv->titleid, priv->chapid, priv->angle);
704 }
706 /* read it in from the file */
707 if (_read (priv, priv->angle, priv->new_seek, buf)) {
708 gst_element_signal_eos (GST_ELEMENT (dvdreadsrc));
709 return NULL;
710 }
712 if (priv->new_seek) {
713 priv->new_seek = FALSE;
714 }
716 return buf;
717 }
718 #endif
720 /* open the file, necessary to go to RUNNING state */
721 static gboolean
722 dvdreadsrc_open_file (DVDReadSrc *src)
723 {
724 g_return_val_if_fail (src != NULL, FALSE);
725 g_return_val_if_fail (GST_IS_DVDREADSRC(src), FALSE);
726 g_return_val_if_fail (!GST_FLAG_IS_SET (src, DVDREADSRC_OPEN), FALSE);
728 if (_open(src->priv, src->priv->location))
729 return FALSE;
730 if (_seek(src->priv,
731 src->priv->title,
732 src->priv->chapter,
733 src->priv->angle))
734 return FALSE;
736 GST_FLAG_SET (src, DVDREADSRC_OPEN);
738 return TRUE;
739 }
741 /* close the file */
742 static void
743 dvdreadsrc_close_file (DVDReadSrc *src)
744 {
745 g_return_if_fail (GST_FLAG_IS_SET (src, DVDREADSRC_OPEN));
747 _close(src->priv);
749 GST_FLAG_UNSET (src, DVDREADSRC_OPEN);
750 }
752 static GstElementStateReturn
753 dvdreadsrc_change_state (GstElement *element)
754 {
755 g_return_val_if_fail (GST_IS_DVDREADSRC (element), GST_STATE_FAILURE);
757 GST_DEBUG ("gstdvdreadsrc: state pending %d", GST_STATE_PENDING (element));
759 /* if going down into NULL state, close the file if it's open */
760 if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
761 if (GST_FLAG_IS_SET (element, DVDREADSRC_OPEN))
762 dvdreadsrc_close_file (DVDREADSRC (element));
763 /* otherwise (READY or higher) we need to open the file */
764 } else {
765 if (!GST_FLAG_IS_SET (element, DVDREADSRC_OPEN)) {
766 if (!dvdreadsrc_open_file (DVDREADSRC (element)))
767 return GST_STATE_FAILURE;
768 }
769 }
771 /* if we haven't failed already, give the parent class a chance to ;-) */
772 if (GST_ELEMENT_CLASS (parent_class)->change_state)
773 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
775 return GST_STATE_SUCCESS;
776 }
778 static gboolean
779 plugin_init (GstPlugin *plugin)
780 {
781 if (!gst_element_register (plugin, "dvdreadsrc", GST_RANK_NONE, GST_TYPE_DVDREADSRC))
782 return FALSE;
784 return TRUE;
785 }
787 GST_PLUGIN_DEFINE (
788 GST_VERSION_MAJOR,
789 GST_VERSION_MINOR,
790 "dvdreadsrc",
791 "Access a DVD with dvdread",
792 plugin_init,
793 VERSION,
794 "GPL",
795 GST_COPYRIGHT,
796 GST_PACKAGE,
797 GST_ORIGIN)