Esempio n. 1
0
int eMPEGStreamInformation::getStructureEntryFirst(off_t &offset, unsigned long long &data)
{
	//eDebug("[eMPEGStreamInformation] {%d} getStructureEntryFirst(offset=%llu)", gettid(), offset);
	if (m_structure_read_fd < 0)
	{
		eDebug("[eMPEGStreamInformation] getStructureEntryFirst failed because of no m_structure_read_fd");
		return -1;
	}

	if ((m_structure_cache_entries == 0) ||
	    (structureCacheOffset(0) > offset) ||
	    (structureCacheOffset(m_structure_cache_entries - 1) <= offset))
	{
		int l = ::lseek(m_structure_read_fd, 0, SEEK_END) / entry_size;
		if (l == 0)
		{
			eDebug("[eMPEGStreamInformation] getStructureEntryFirst failed because file size is zero");
			return -1;
		}
		m_structure_file_entries = l;

		/* do a binary search */
		int count = l;
		int i = 0;
		const int structure_cache_size = MAPSIZE / entry_size;
		while (count > (structure_cache_size/4))
		{
			int step = count >> 1;
			::lseek(m_structure_read_fd, (i + step) * entry_size, SEEK_SET);
			unsigned long long d;
			if (::read(m_structure_read_fd, &d, sizeof(d)) < (ssize_t)sizeof(d))
			{
				eDebug("[eMPEGStreamInformation] getStructureEntryFirst read error at entry %d", i+step);
				return -1;
			}
			d = be64toh(d);
			if (d < (unsigned long long)offset)
			{
				i += step + 1;
				count -= step + 1;
			} else
				count = step;
		}
		//eDebug("[eMPEGStreamInformation] getStructureEntryFirst i=%d size=%d count=%d", i, l, count);

		if (i + structure_cache_size > l)
		{
			i = l - structure_cache_size; // Near end of file, just fetch the last
		}
		if (i < 0)
			i = 0;
		int num = moveCache(i);
		if ((num < structure_cache_size) && (structureCacheOffset(num - 1) <= offset))
		{
			eDebug("[eMPEGStreamInformation] offset %jd is past EOF of structure file", (intmax_t)offset);
			data = 0;
			return 1;
		}
	}
Esempio n. 2
0
int eMPEGStreamInformation::getStructureEntryFirst(off_t &offset, unsigned long long &data)
{
	//eDebug("[eMPEGStreamInformation] {%d} getStructureEntryFirst(offset=%lld)", gettid(), offset);
	if (m_structure_read_fd < 0)
	{
		eDebug("[eMPEGStreamInformation] getStructureEntryFirst failed because of no m_structure_read_fd");
		return -1;
	}

	if ((m_structure_cache_entries == 0) ||
	    (structureCacheOffset(0) > offset) ||
	    (structureCacheOffset(m_structure_cache_entries - 1) <= offset))
	{
		int l = ::lseek(m_structure_read_fd, 0, SEEK_END) / entry_size;
		if (l == 0)
		{
			eDebug("[eMPEGStreamInformation] getStructureEntryFirst failed because file size is zero");
			return -1;
		}
		m_structure_file_entries = l;

		/* do a binary search */
		int count = l;
		int i = 0;
		const int structure_cache_size = MAPSIZE / entry_size;
		while (count > (structure_cache_size/4))
		{
			int step = count >> 1;
// Read entry at top end of current range (== i+step-1)
			::lseek(m_structure_read_fd, (i + step - 1) * entry_size, SEEK_SET);
			unsigned long long d;
			if (::read(m_structure_read_fd, &d, sizeof(d)) < (ssize_t)sizeof(d))
			{
				eDebug("[eMPEGStreamInformation] getStructureEntryFirst read error at entry %d", i+step);
				return -1;
			}
			d = be64toh(d);
			if (d < (unsigned long long)offset)
			{
// Move start of range to *be* the last test (+1 more may be too high!!)
// and remove tested count
				i += step;
				count -= step;
			} else
// Keep start of range but change range to that below test
				count = step;
		}
		//eDebug("[eMPEGStreamInformation] getStructureEntryFirst i=%d size=%d count=%d", i, l, count);

		if (i + structure_cache_size > l)
		{
			i = l - structure_cache_size; // Near end of file, just fetch the last
		}
		if (i < 0)
			i = 0;
		int num = moveCache(i);
		if ((num < structure_cache_size) && (structureCacheOffset(num - 1) <= offset))
		{
			eDebug("[eMPEGStreamInformation] offset %lld is past EOF of structure file", offset);
			data = 0;
			return 1;
		}
	}