xref: /haiku/src/add-ons/media/plugins/dvd_streamer/DVDMediaIO.cpp (revision 218a8c03cbadd95d8888b218930f514fa0b4fac4)
11efb85deSBarrett17 /*
21efb85deSBarrett17  * Copyright 2019, Dario Casalinuovo. All rights reserved.
31efb85deSBarrett17  * Distributed under the terms of the MIT License.
41efb85deSBarrett17  */
51efb85deSBarrett17 
61efb85deSBarrett17 
71efb85deSBarrett17 #include "DVDMediaIO.h"
81efb85deSBarrett17 
91efb85deSBarrett17 #include <stdio.h>
101efb85deSBarrett17 #include <unistd.h>
111efb85deSBarrett17 #include <inttypes.h>
121efb85deSBarrett17 #include <sys/types.h>
131efb85deSBarrett17 #include <sys/stat.h>
141efb85deSBarrett17 #include <fcntl.h>
151efb85deSBarrett17 
161efb85deSBarrett17 #include "MediaDebug.h"
171efb85deSBarrett17 
181efb85deSBarrett17 
191efb85deSBarrett17 #define DVD_READ_CACHE 1
201efb85deSBarrett17 
211efb85deSBarrett17 #define DEFAULT_LANGUAGE "en"
221efb85deSBarrett17 
231efb85deSBarrett17 
DVDMediaIO(const char * path)241efb85deSBarrett17 DVDMediaIO::DVDMediaIO(const char* path)
251efb85deSBarrett17 	:
261efb85deSBarrett17 	BAdapterIO(
271efb85deSBarrett17 		B_MEDIA_STREAMING | B_MEDIA_SEEKABLE,
281efb85deSBarrett17 		B_INFINITE_TIMEOUT),
291efb85deSBarrett17 	fPath(path),
301efb85deSBarrett17 	fLoopThread(-1),
311efb85deSBarrett17 	fExit(false)
321efb85deSBarrett17 {
331efb85deSBarrett17 	fBuffer = (uint8_t*) malloc(DVD_VIDEO_LB_LEN);
341efb85deSBarrett17 }
351efb85deSBarrett17 
361efb85deSBarrett17 
~DVDMediaIO()371efb85deSBarrett17 DVDMediaIO::~DVDMediaIO()
381efb85deSBarrett17 {
391efb85deSBarrett17 	fExit = true;
401efb85deSBarrett17 
411efb85deSBarrett17 	status_t status;
421efb85deSBarrett17 	if (fLoopThread != -1)
431efb85deSBarrett17 		wait_for_thread(fLoopThread, &status);
441efb85deSBarrett17 
451efb85deSBarrett17 	free(fBuffer);
461efb85deSBarrett17 }
471efb85deSBarrett17 
481efb85deSBarrett17 
491efb85deSBarrett17 ssize_t
WriteAt(off_t position,const void * buffer,size_t size)501efb85deSBarrett17 DVDMediaIO::WriteAt(off_t position, const void* buffer, size_t size)
511efb85deSBarrett17 {
521efb85deSBarrett17 	return B_NOT_SUPPORTED;
531efb85deSBarrett17 }
541efb85deSBarrett17 
551efb85deSBarrett17 
561efb85deSBarrett17 status_t
SetSize(off_t size)571efb85deSBarrett17 DVDMediaIO::SetSize(off_t size)
581efb85deSBarrett17 {
591efb85deSBarrett17 	return B_NOT_SUPPORTED;
601efb85deSBarrett17 }
611efb85deSBarrett17 
621efb85deSBarrett17 
631efb85deSBarrett17 status_t
Open()641efb85deSBarrett17 DVDMediaIO::Open()
651efb85deSBarrett17 {
661efb85deSBarrett17 	fInputAdapter = BuildInputAdapter();
671efb85deSBarrett17 
681efb85deSBarrett17 	if (dvdnav_open(&fDvdNav, fPath) != DVDNAV_STATUS_OK) {
691efb85deSBarrett17 		TRACE("DVDMediaIO::Open() dvdnav_open error\n");
701efb85deSBarrett17 		return B_ERROR;
711efb85deSBarrett17 	}
721efb85deSBarrett17 
731efb85deSBarrett17 	// read ahead cache usage
741efb85deSBarrett17 	if (dvdnav_set_readahead_flag(fDvdNav, DVD_READ_CACHE) != DVDNAV_STATUS_OK) {
751efb85deSBarrett17 		TRACE("DVDMediaIO::Open() dvdnav_set_readahead_flag error: %s\n",
761efb85deSBarrett17 			dvdnav_err_to_string(fDvdNav));
771efb85deSBarrett17 		return B_ERROR;
781efb85deSBarrett17 	}
791efb85deSBarrett17 
801efb85deSBarrett17 	// set language
811efb85deSBarrett17 	if (dvdnav_menu_language_select(fDvdNav, DEFAULT_LANGUAGE) != DVDNAV_STATUS_OK ||
821efb85deSBarrett17 			dvdnav_audio_language_select(fDvdNav, DEFAULT_LANGUAGE) != DVDNAV_STATUS_OK ||
831efb85deSBarrett17 		dvdnav_spu_language_select(fDvdNav, DEFAULT_LANGUAGE) != DVDNAV_STATUS_OK) {
841efb85deSBarrett17 		TRACE("DVDMediaIO::Open() setting languages error: %s\n",
851efb85deSBarrett17 			dvdnav_err_to_string(fDvdNav));
861efb85deSBarrett17 		return B_ERROR;
871efb85deSBarrett17 	}
881efb85deSBarrett17 
891efb85deSBarrett17 	// set the PGC positioning flag
901efb85deSBarrett17 	if (dvdnav_set_PGC_positioning_flag(fDvdNav, 1) != DVDNAV_STATUS_OK) {
911efb85deSBarrett17 		TRACE("DVDMediaIO::Open() dvdnav_set_PGC_positioning_flag error: %s\n",
921efb85deSBarrett17 			dvdnav_err_to_string(fDvdNav));
931efb85deSBarrett17 		return 2;
941efb85deSBarrett17 	}
951efb85deSBarrett17 
961efb85deSBarrett17 	fLoopThread = spawn_thread(_LoopThread, "two and two are five",
971efb85deSBarrett17 		B_NORMAL_PRIORITY, this);
981efb85deSBarrett17 
991efb85deSBarrett17 	if (fLoopThread <= 0 || resume_thread(fLoopThread) != B_OK)
1001efb85deSBarrett17 		return B_ERROR;
1011efb85deSBarrett17 
1021efb85deSBarrett17 	return B_OK;
1031efb85deSBarrett17 }
1041efb85deSBarrett17 
1051efb85deSBarrett17 
1061efb85deSBarrett17 int32
_LoopThread(void * data)1071efb85deSBarrett17 DVDMediaIO::_LoopThread(void* data)
1081efb85deSBarrett17 {
1091efb85deSBarrett17 	static_cast<DVDMediaIO *>(data)->LoopThread();
1101efb85deSBarrett17 	return 0;
1111efb85deSBarrett17 }
1121efb85deSBarrett17 
1131efb85deSBarrett17 
1141efb85deSBarrett17 void
LoopThread()1151efb85deSBarrett17 DVDMediaIO::LoopThread()
1161efb85deSBarrett17 {
1171efb85deSBarrett17 	while (!fExit) {
1181efb85deSBarrett17 		int err;
1191efb85deSBarrett17 		int event;
1201efb85deSBarrett17 		int len;
1211efb85deSBarrett17 
1221efb85deSBarrett17 #if DVD_READ_CACHE
1231efb85deSBarrett17 		err = dvdnav_get_next_cache_block(fDvdNav, &fBuffer, &event, &len);
1241efb85deSBarrett17 #else
1251efb85deSBarrett17 		err = dvdnav_get_next_block(fDvdNav, fBuffer, &event, &len);
1261efb85deSBarrett17 #endif
1271efb85deSBarrett17 
1281efb85deSBarrett17 		if (err == DVDNAV_STATUS_ERR) {
1291efb85deSBarrett17 			TRACE("DVDMediaIO::LoopThread(): Error getting next block: %s\n",
1301efb85deSBarrett17 				dvdnav_err_to_string(fDvdNav));
1311efb85deSBarrett17 			continue;
1321efb85deSBarrett17 		}
1331efb85deSBarrett17 
1341efb85deSBarrett17 		HandleDVDEvent(event, len);
1351efb85deSBarrett17 
1361efb85deSBarrett17 #if DVD_READ_CACHE
1371efb85deSBarrett17 		dvdnav_free_cache_block(fDvdNav, fBuffer);
1381efb85deSBarrett17 #endif
1391efb85deSBarrett17 	}
1401efb85deSBarrett17 
1411efb85deSBarrett17 	if (dvdnav_close(fDvdNav) != DVDNAV_STATUS_OK) {
1421efb85deSBarrett17 		TRACE("DVDMediaIO::LoopThread() dvdnav_close error: %s\n",
1431efb85deSBarrett17 			dvdnav_err_to_string(fDvdNav));
1441efb85deSBarrett17 	}
1451efb85deSBarrett17 	fLoopThread = -1;
1461efb85deSBarrett17 }
1471efb85deSBarrett17 
1481efb85deSBarrett17 
1491efb85deSBarrett17 void
HandleDVDEvent(int event,int len)1501efb85deSBarrett17 DVDMediaIO::HandleDVDEvent(int event, int len)
1511efb85deSBarrett17 {
1521efb85deSBarrett17 	switch (event) {
1531efb85deSBarrett17 		case DVDNAV_BLOCK_OK:
1541efb85deSBarrett17 			fInputAdapter->Write(fBuffer, len);
1551efb85deSBarrett17 			break;
1561efb85deSBarrett17 
1571efb85deSBarrett17 		case DVDNAV_NOP:
1581efb85deSBarrett17 			break;
1591efb85deSBarrett17 
1601efb85deSBarrett17 		case DVDNAV_STILL_FRAME:
1611efb85deSBarrett17 		{
1621efb85deSBarrett17 			dvdnav_still_event_t* still_event
1631efb85deSBarrett17 				= (dvdnav_still_event_t*) fBuffer;
1641efb85deSBarrett17 			if (still_event->length < 0xff) {
1651efb85deSBarrett17 				TRACE("DVDMediaIO::HandleDVDEvent: Skipped %d "
1661efb85deSBarrett17 						"seconds of still frame\n",
1671efb85deSBarrett17 					still_event->length);
1681efb85deSBarrett17 			} else {
1691efb85deSBarrett17 				TRACE("DVDMediaIO::HandleDVDEvent: Skipped "
1701efb85deSBarrett17 					"indefinite length still frame\n");
1711efb85deSBarrett17 			}
1721efb85deSBarrett17 			dvdnav_still_skip(fDvdNav);
1731efb85deSBarrett17 			break;
1741efb85deSBarrett17 		}
1751efb85deSBarrett17 
1761efb85deSBarrett17 		case DVDNAV_WAIT:
1771efb85deSBarrett17 			TRACE("DVDMediaIO::HandleDVDEvent: Skipping wait condition\n");
1781efb85deSBarrett17 			dvdnav_wait_skip(fDvdNav);
1791efb85deSBarrett17 			break;
1801efb85deSBarrett17 
1811efb85deSBarrett17 		case DVDNAV_SPU_CLUT_CHANGE:
1821efb85deSBarrett17 			break;
1831efb85deSBarrett17 
1841efb85deSBarrett17 		case DVDNAV_SPU_STREAM_CHANGE:
1851efb85deSBarrett17 			break;
1861efb85deSBarrett17 
1871efb85deSBarrett17 		case DVDNAV_AUDIO_STREAM_CHANGE:
1881efb85deSBarrett17 			break;
1891efb85deSBarrett17 
1901efb85deSBarrett17 		case DVDNAV_HIGHLIGHT:
1911efb85deSBarrett17 		{
1921efb85deSBarrett17 			dvdnav_highlight_event_t* highlight_event
1931efb85deSBarrett17 				= (dvdnav_highlight_event_t*) fBuffer;
1941efb85deSBarrett17 			TRACE("DVDMediaIO::HandleDVDEvent: Button: %d\n",
1951efb85deSBarrett17 				highlight_event->buttonN);
1961efb85deSBarrett17 			break;
1971efb85deSBarrett17 		}
1981efb85deSBarrett17 
1991efb85deSBarrett17 		case DVDNAV_VTS_CHANGE:
2001efb85deSBarrett17 			break;
2011efb85deSBarrett17 
2021efb85deSBarrett17 		case DVDNAV_CELL_CHANGE:
2031efb85deSBarrett17 		{
2041efb85deSBarrett17 			int32_t title = 0, chapter = 0;
2051efb85deSBarrett17 			uint32_t pos, len;
2061efb85deSBarrett17 
2071efb85deSBarrett17 			dvdnav_current_title_info(fDvdNav, &title, &chapter);
2081efb85deSBarrett17 			dvdnav_get_position(fDvdNav, &pos, &len);
2091efb85deSBarrett17 			TRACE("DVDMediaIO::HandleDVDEvent: Cell: Title %d, Chapter %d\n",
2101efb85deSBarrett17 				tt, ptt);
2111efb85deSBarrett17 			TRACE("DVDMediaIO::HandleDVDEvent: At position %.0f%% inside "
2121efb85deSBarrett17 				"the feature\n", 100 * (double)pos / (double)len);
2131efb85deSBarrett17 			break;
2141efb85deSBarrett17 		}
2151efb85deSBarrett17 
2161efb85deSBarrett17 		case DVDNAV_NAV_PACKET:
2171efb85deSBarrett17 			break;
2181efb85deSBarrett17 
2191efb85deSBarrett17 		case DVDNAV_HOP_CHANNEL:
2201efb85deSBarrett17 			break;
2211efb85deSBarrett17 
2221efb85deSBarrett17 		case DVDNAV_STOP:
2231efb85deSBarrett17 			fExit = true;
2241efb85deSBarrett17 			break;
2251efb85deSBarrett17 
2261efb85deSBarrett17 		default:
2271efb85deSBarrett17 			TRACE("DVDMediaIO::HandleDVDEvent: unknown event (%i)\n",
2281efb85deSBarrett17 				event);
2291efb85deSBarrett17 			fExit = true;
2301efb85deSBarrett17 			break;
2311efb85deSBarrett17 	}
2321efb85deSBarrett17 }
2331efb85deSBarrett17 
2341efb85deSBarrett17 
2351efb85deSBarrett17 status_t
SeekRequested(off_t position)2361efb85deSBarrett17 DVDMediaIO::SeekRequested(off_t position)
2371efb85deSBarrett17 {
2381efb85deSBarrett17 	dvdnav_sector_search(fDvdNav, position, SEEK_SET);
2391efb85deSBarrett17 	return B_OK;
2401efb85deSBarrett17 }
2411efb85deSBarrett17 
2421efb85deSBarrett17 
243*cb19342bSBarrett17 void
MouseMoved(uint32 x,uint32 y)244*cb19342bSBarrett17 DVDMediaIO::MouseMoved(uint32 x, uint32 y)
245*cb19342bSBarrett17 {
246*cb19342bSBarrett17 	pci_t* pci = dvdnav_get_current_nav_pci(fDvdNav);
247*cb19342bSBarrett17 	dvdnav_mouse_select(fDvdNav, pci, x, y);
248*cb19342bSBarrett17 }
249*cb19342bSBarrett17 
250*cb19342bSBarrett17 
251*cb19342bSBarrett17 void
MouseDown(uint32 x,uint32 y)252*cb19342bSBarrett17 DVDMediaIO::MouseDown(uint32 x, uint32 y)
253*cb19342bSBarrett17 {
254*cb19342bSBarrett17 	pci_t* pci = dvdnav_get_current_nav_pci(fDvdNav);
255*cb19342bSBarrett17 	dvdnav_mouse_activate(fDvdNav, pci, x, y);
256*cb19342bSBarrett17 }
257