paulo@0: /* paulo@0: * Copyright (C) 2003 by the libdvdnav project paulo@0: * paulo@0: * This file is part of libdvdnav, a DVD navigation library. paulo@0: * paulo@0: * libdvdnav is free software; you can redistribute it and/or modify paulo@0: * it under the terms of the GNU General Public License as published by paulo@0: * the Free Software Foundation; either version 2 of the License, or paulo@0: * (at your option) any later version. paulo@0: * paulo@0: * libdvdnav is distributed in the hope that it will be useful, paulo@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of paulo@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the paulo@0: * GNU General Public License for more details. paulo@0: * paulo@0: * You should have received a copy of the GNU General Public License paulo@0: * along with this program; if not, write to the Free Software paulo@0: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA paulo@0: * paulo@0: * $Id: menus.c 1135 2008-09-06 21:55:51Z rathann $ paulo@0: * paulo@0: */ paulo@0: paulo@0: #include paulo@0: #include paulo@0: #include paulo@0: #include paulo@0: #include paulo@0: #include paulo@0: #include "dvd_types.h" paulo@0: #include paulo@0: #include paulo@0: #include /* For vm_cmd_t */ paulo@0: #include "dvdnav.h" paulo@0: #include "dvdnav_events.h" paulo@0: paulo@0: /* shall we use libdvdnav's read ahead cache? */ paulo@0: #define DVD_READ_CACHE 1 paulo@0: paulo@0: /* which is the default language for menus/audio/subpictures? */ paulo@0: #define DVD_LANGUAGE "en" paulo@0: paulo@0: #ifdef WIN32 paulo@0: #define S_IRWXG 0 paulo@0: #endif paulo@0: paulo@0: int main(int argc, char **argv) { paulo@0: dvdnav_t *dvdnav; paulo@0: uint8_t mem[DVD_VIDEO_LB_LEN]; paulo@0: int finished = 0; paulo@0: int output_fd = 0; paulo@0: int dump = 0, tt_dump = 0; paulo@0: paulo@0: /* open dvdnav handle */ paulo@0: printf("Opening DVD...\n"); paulo@0: if (dvdnav_open(&dvdnav, "/dev/dvd") != DVDNAV_STATUS_OK) { paulo@0: printf("Error on dvdnav_open\n"); paulo@0: return 1; paulo@0: } paulo@0: paulo@0: /* set read ahead cache usage */ paulo@0: if (dvdnav_set_readahead_flag(dvdnav, DVD_READ_CACHE) != DVDNAV_STATUS_OK) { paulo@0: printf("Error on dvdnav_set_readahead_flag: %s\n", dvdnav_err_to_string(dvdnav)); paulo@0: return 2; paulo@0: } paulo@0: paulo@0: /* set the language */ paulo@0: if (dvdnav_menu_language_select(dvdnav, DVD_LANGUAGE) != DVDNAV_STATUS_OK || paulo@0: dvdnav_audio_language_select(dvdnav, DVD_LANGUAGE) != DVDNAV_STATUS_OK || paulo@0: dvdnav_spu_language_select(dvdnav, DVD_LANGUAGE) != DVDNAV_STATUS_OK) { paulo@0: printf("Error on setting languages: %s\n", dvdnav_err_to_string(dvdnav)); paulo@0: return 2; paulo@0: } paulo@0: paulo@0: /* set the PGC positioning flag to have position information relatively to the paulo@0: * whole feature instead of just relatively to the current chapter */ paulo@0: if (dvdnav_set_PGC_positioning_flag(dvdnav, 1) != DVDNAV_STATUS_OK) { paulo@0: printf("Error on dvdnav_set_PGC_positioning_flag: %s\n", dvdnav_err_to_string(dvdnav)); paulo@0: return 2; paulo@0: } paulo@0: paulo@0: paulo@0: /* the read loop which regularly calls dvdnav_get_next_block paulo@0: * and handles the returned events */ paulo@0: printf("Reading...\n"); paulo@0: while (!finished) { paulo@0: int result, event, len; paulo@0: uint8_t *buf = mem; paulo@0: paulo@0: /* the main reading function */ paulo@0: #if DVD_READ_CACHE paulo@0: result = dvdnav_get_next_cache_block(dvdnav, &buf, &event, &len); paulo@0: #else paulo@0: result = dvdnav_get_next_block(dvdnav, buf, &event, &len); paulo@0: #endif paulo@0: paulo@0: if (result == DVDNAV_STATUS_ERR) { paulo@0: printf("Error getting next block: %s\n", dvdnav_err_to_string(dvdnav)); paulo@0: return 3; paulo@0: } paulo@0: paulo@0: switch (event) { paulo@0: case DVDNAV_BLOCK_OK: paulo@0: /* We have received a regular block of the currently playing MPEG stream. paulo@0: * A real player application would now pass this block through demuxing paulo@0: * and decoding. We simply write it to disc here. */ paulo@0: paulo@0: if (!output_fd) { paulo@0: printf("Opening output...\n"); paulo@0: output_fd = open("libdvdnav.mpg", O_CREAT | O_WRONLY, S_IRWXU | S_IRWXG); paulo@0: if (output_fd == -1) { paulo@0: printf("Error opening output\n"); paulo@0: return 4; paulo@0: } paulo@0: } paulo@0: paulo@0: if (dump || tt_dump) paulo@0: write(output_fd, buf, len); paulo@0: paulo@0: break; paulo@0: case DVDNAV_NOP: paulo@0: /* Nothing to do here. */ paulo@0: break; paulo@0: case DVDNAV_STILL_FRAME: paulo@0: /* We have reached a still frame. A real player application would wait paulo@0: * the amount of time specified by the still's length while still handling paulo@0: * user input to make menus and other interactive stills work. paulo@0: * A length of 0xff means an indefinite still which has to be skipped paulo@0: * indirectly by some user interaction. */ paulo@0: { paulo@0: dvdnav_still_event_t *still_event = (dvdnav_still_event_t *)buf; paulo@0: if (still_event->length < 0xff) paulo@0: printf("Skipping %d seconds of still frame\n", still_event->length); paulo@0: else paulo@0: printf("Skipping indefinite length still frame\n"); paulo@0: dvdnav_still_skip(dvdnav); paulo@0: } paulo@0: break; paulo@0: case DVDNAV_WAIT: paulo@0: /* We have reached a point in DVD playback, where timing is critical. paulo@0: * Player application with internal fifos can introduce state paulo@0: * inconsistencies, because libdvdnav is always the fifo's length paulo@0: * ahead in the stream compared to what the application sees. paulo@0: * Such applications should wait until their fifos are empty paulo@0: * when they receive this type of event. */ paulo@0: printf("Skipping wait condition\n"); paulo@0: dvdnav_wait_skip(dvdnav); paulo@0: break; paulo@0: case DVDNAV_SPU_CLUT_CHANGE: paulo@0: /* Player applications should pass the new colour lookup table to their paulo@0: * SPU decoder */ paulo@0: break; paulo@0: case DVDNAV_SPU_STREAM_CHANGE: paulo@0: /* Player applications should inform their SPU decoder to switch channels */ paulo@0: break; paulo@0: case DVDNAV_AUDIO_STREAM_CHANGE: paulo@0: /* Player applications should inform their audio decoder to switch channels */ paulo@0: break; paulo@0: case DVDNAV_HIGHLIGHT: paulo@0: /* Player applications should inform their overlay engine to highlight the paulo@0: * given button */ paulo@0: { paulo@0: dvdnav_highlight_event_t *highlight_event = (dvdnav_highlight_event_t *)buf; paulo@0: printf("Selected button %d\n", highlight_event->buttonN); paulo@0: } paulo@0: break; paulo@0: case DVDNAV_VTS_CHANGE: paulo@0: /* Some status information like video aspect and video scale permissions do paulo@0: * not change inside a VTS. Therefore this event can be used to query such paulo@0: * information only when necessary and update the decoding/displaying paulo@0: * accordingly. */ paulo@0: break; paulo@0: case DVDNAV_CELL_CHANGE: paulo@0: /* Some status information like the current Title and Part numbers do not paulo@0: * change inside a cell. Therefore this event can be used to query such paulo@0: * information only when necessary and update the decoding/displaying paulo@0: * accordingly. */ paulo@0: { paulo@0: int32_t tt = 0, ptt = 0; paulo@0: uint32_t pos, len; paulo@0: char input = '\0'; paulo@0: paulo@0: dvdnav_current_title_info(dvdnav, &tt, &ptt); paulo@0: dvdnav_get_position(dvdnav, &pos, &len); paulo@0: printf("Cell change: Title %d, Chapter %d\n", tt, ptt); paulo@0: printf("At position %.0f%% inside the feature\n", 100 * (double)pos / (double)len); paulo@0: paulo@0: dump = 0; paulo@0: if (tt_dump && tt != tt_dump) paulo@0: tt_dump = 0; paulo@0: paulo@0: if (!dump && !tt_dump) { paulo@0: fflush(stdin); paulo@0: while ((input != 'a') && (input != 's') && (input != 'q') && (input != 't')) { paulo@0: printf("(a)ppend cell to output\n(s)kip cell\nappend until end of (t)itle\n(q)uit\n"); paulo@0: scanf("%c", &input); paulo@0: } paulo@0: paulo@0: switch (input) { paulo@0: case 'a': paulo@0: dump = 1; paulo@0: break; paulo@0: case 't': paulo@0: tt_dump = tt; paulo@0: break; paulo@0: case 'q': paulo@0: finished = 1; paulo@0: } paulo@0: } paulo@0: } paulo@0: break; paulo@0: case DVDNAV_NAV_PACKET: paulo@0: /* A NAV packet provides PTS discontinuity information, angle linking information and paulo@0: * button definitions for DVD menus. Angles are handled completely inside libdvdnav. paulo@0: * For the menus to work, the NAV packet information has to be passed to the overlay paulo@0: * engine of the player so that it knows the dimensions of the button areas. */ paulo@0: { paulo@0: pci_t *pci; paulo@0: paulo@0: /* Applications with fifos should not use these functions to retrieve NAV packets, paulo@0: * they should implement their own NAV handling, because the packet you get from these paulo@0: * functions will already be ahead in the stream which can cause state inconsistencies. paulo@0: * Applications with fifos should therefore pass the NAV packet through the fifo paulo@0: * and decoding pipeline just like any other data. */ paulo@0: pci = dvdnav_get_current_nav_pci(dvdnav); paulo@0: dvdnav_get_current_nav_dsi(dvdnav); paulo@0: paulo@0: if(pci->hli.hl_gi.btn_ns > 0) { paulo@0: int button; paulo@0: paulo@0: printf("Found %i DVD menu buttons...\n", pci->hli.hl_gi.btn_ns); paulo@0: paulo@0: for (button = 0; button < pci->hli.hl_gi.btn_ns; button++) { paulo@0: btni_t *btni = &(pci->hli.btnit[button]); paulo@0: printf("Button %i top-left @ (%i,%i), bottom-right @ (%i,%i)\n", paulo@0: button + 1, btni->x_start, btni->y_start, paulo@0: btni->x_end, btni->y_end); paulo@0: } paulo@0: paulo@0: button = 0; paulo@0: while ((button <= 0) || (button > pci->hli.hl_gi.btn_ns)) { paulo@0: printf("Which button (1 to %i): ", pci->hli.hl_gi.btn_ns); paulo@0: scanf("%i", &button); paulo@0: } paulo@0: paulo@0: printf("Selecting button %i...\n", button); paulo@0: /* This is the point where applications with fifos have to hand in a NAV packet paulo@0: * which has traveled through the fifos. See the notes above. */ paulo@0: dvdnav_button_select_and_activate(dvdnav, pci, button); paulo@0: } paulo@0: } paulo@0: break; paulo@0: case DVDNAV_HOP_CHANNEL: paulo@0: /* This event is issued whenever a non-seamless operation has been executed. paulo@0: * Applications with fifos should drop the fifos content to speed up responsiveness. */ paulo@0: break; paulo@0: case DVDNAV_STOP: paulo@0: /* Playback should end here. */ paulo@0: { paulo@0: finished = 1; paulo@0: } paulo@0: break; paulo@0: default: paulo@0: printf("Unknown event (%i)\n", event); paulo@0: finished = 1; paulo@0: break; paulo@0: } paulo@0: #if DVD_READ_CACHE paulo@0: dvdnav_free_cache_block(dvdnav, buf); paulo@0: #endif paulo@0: } paulo@0: paulo@0: /* destroy dvdnav handle */ paulo@0: if (dvdnav_close(dvdnav) != DVDNAV_STATUS_OK) { paulo@0: printf("Error on dvdnav_close: %s\n", dvdnav_err_to_string(dvdnav)); paulo@0: return 5; paulo@0: } paulo@0: close(output_fd); paulo@0: paulo@0: return 0; paulo@0: }