⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dvddrive.cc

📁 Linux下比较早的基于命令行的DVD播放器
💻 CC
字号:
//// Copyright (c) 2003 by Istv醤 V醨adi//// This file is part of dxr3Player, a DVD player written specifically // for the DXR3 (aka Hollywood+) decoder card.// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program 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 General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA//------------------------------------------------------------------------------#include "DVDDrive.h"#include "util/POSIX.h"#include "util/Log.h"#include <libmpdvdkit/dvd_reader.h>#include <libmpdvdkit/ifo_read.h>#include <linux/cdrom.h>#include <cstring>//------------------------------------------------------------------------------using dvd::DVDDrive;using dvd::FileHandler;//------------------------------------------------------------------------------inline DVDDrive::FileHandler::FileHandler(dvd_file_t* file) :    file(file){    assert(file!=0);}//------------------------------------------------------------------------------DVDDrive::FileHandler::~FileHandler(){    DVDCloseFile(file);}//------------------------------------------------------------------------------size_t DVDDrive::FileHandler::getLength(){    return static_cast<size_t>(DVDFileSize(file));}//------------------------------------------------------------------------------void DVDDrive::FileHandler::readSectors(void* dest, size_t offset,                                         size_t numSectors){    ssize_t numRead = DVDReadBlocks(file,                                    static_cast<int>(offset),                                    numSectors,                                    reinterpret_cast<unsigned char*>(dest));    if (numRead!=(ssize_t)numSectors) abort();}//------------------------------------------------------------------------------//------------------------------------------------------------------------------bool DVDDrive::isDVDDrive(const char* path){    bool isdvd = false;    if (path!=0 && strlen(path)>0) {        int fd = ::open(path, O_RDONLY|O_NONBLOCK);        if (fd>=0) {            int capabilities = ioctl(fd, CDROM_GET_CAPABILITY);            if (capabilities>0) {                isdvd = (capabilities&CDC_DVD)!=0;            }            ::close(fd);        }    }            return isdvd;}//------------------------------------------------------------------------------char* DVDDrive::findDVD(const char* path){    static const char* const paths[] = {        "/dev/dvd",        "/dev/hda",        "/dev/hdb",        "/dev/hdc",        "/dev/hdd",        0    };    const char* dvdpath = 0;    if (isDVDDrive(path)) {        dvdpath = path;    }     for(size_t i = 0; paths[i]!=0 && dvdpath==0; ++i) {        if (isDVDDrive(paths[i])) {            dvdpath = paths[i];        }    }    if (dvdpath==0) {        Log::fatal("No DVD drive found!\n");        exit(1);    }    Log::normal("Using %s as the DVD drive.\n", dvdpath);    return strdup(dvdpath);}//------------------------------------------------------------------------------DVDDrive::DVDDrive(const char* p) :    path(findDVD(p)),    reader(0){    for(size_t i = 0; i<sizeof(ifoHandles)/sizeof(ifoHandles[0]); ++i) {        ifoHandles[i] = 0;    }}//------------------------------------------------------------------------------DVDDrive::~DVDDrive(){    assert(!isOpen());    free(path);}//------------------------------------------------------------------------------bool DVDDrive::hasDisk(){    if (isOpen()) return true;    int controlFD = POSIX::open(path, O_RDONLY|O_NONBLOCK);    int x = POSIX::ioctl(controlFD, CDROM_DRIVE_STATUS, 0);    POSIX::close(controlFD);    return x==CDS_DISC_OK;}//------------------------------------------------------------------------------bool DVDDrive::isOpen() const{    return reader!=0;}//------------------------------------------------------------------------------bool DVDDrive::open(){    assert(!isOpen());    if (hasDisk()) {        reader = DVDOpen(path);        if (!readIFOHandles()) {            close();        }    }    return isOpen();}//------------------------------------------------------------------------------void DVDDrive::getID(unsigned char* dest) const{    assert(isOpen());    if (DVDDiscID(reader, dest)!=0) {        Log::fatal("dvd::DVDDrive::getID: DVDDiscID failed\n");    }}//------------------------------------------------------------------------------const ifo_handle_t* DVDDrive::getIFOHandle(unsigned titleNo) const{    assert(ifoHandles[titleNo]!=0);    return ifoHandles[titleNo];}//------------------------------------------------------------------------------FileHandler* DVDDrive::openFile(unsigned titleNo, bool isMenu) const{    assert(isOpen());    assert(ifoHandles[titleNo]!=0);    dvd_file_t* file =         DVDOpenFile(reader, (int)titleNo,                     isMenu ? DVD_READ_MENU_VOBS : DVD_READ_TITLE_VOBS);    return (file==0) ? 0 : new FileHandler(file);}//------------------------------------------------------------------------------void DVDDrive::close(){    assert(isOpen());    closeIFOHandles();        DVDClose(reader);    reader = 0;}//------------------------------------------------------------------------------void DVDDrive::eject(){    assert(!isOpen());        int controlFD = POSIX::open(path, O_RDONLY|O_NONBLOCK);    int x = POSIX::ioctl(controlFD, CDROM_DRIVE_STATUS, 0);    if (x==CDS_TRAY_OPEN) {        Log::debug("dvd::DVDDrive::eject: trying to close tray\n");        ioctl(controlFD, CDROMCLOSETRAY);    } else {        Log::debug("dvd::DVDDrive::eject: ejecting DVD\n");        POSIX::ioctl(controlFD, CDROMEJECT, 0);    }    POSIX::close(controlFD);}//------------------------------------------------------------------------------bool DVDDrive::readIFOHandles(){    if (!isOpen()) return false;    ifo_handle_t* ifoHandle = ifoOpen(reader, 0);    if (ifoHandle==0) return false;    ifoHandles[0] = ifoHandle;    size_t numVTSs = ifoHandle->vmgi_mat->vmg_nr_of_title_sets;        Log::debug("dvd::DVD::readIFOHandles: number of VTSs: %u\n",               numVTSs);    for(size_t i = 1; i<=numVTSs; i++) {        ifoHandle = ifoOpen(reader, (int)i);        if (ifoHandle==0) return false;        ifoHandles[i] = ifoHandle;    }    return true;}//------------------------------------------------------------------------------void DVDDrive::closeIFOHandles(){    for(size_t i = 0; i<sizeof(ifoHandles)/sizeof(ifoHandles[0]); ++i) {        ifoClose(ifoHandles[i]);        ifoHandles[i] = 0;    }}//------------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -