ssize_t eRawFile::read(off_t offset, void *buf, size_t count) { eSingleLocker l(m_lock); if (offset != m_current_offset) { m_current_offset = lseek_internal(offset); if (m_current_offset < 0) return m_current_offset; } switchOffset(m_current_offset); if (m_nrfiles >= 2) { if (m_current_offset + count > m_totallength) count = m_totallength - m_current_offset; if (count < 0) return 0; } int ret; ret = ::read(m_fd, buf, count); if (ret > 0) { m_current_offset = m_last_offset += ret; } return ret; }
eM2TSFile::eM2TSFile(const char *filename): m_lock(false), m_sync_offset(0), m_fd(::open(filename, O_RDONLY | O_LARGEFILE)), m_current_offset(0), m_length(0) { if (m_fd != -1) m_current_offset = m_length = lseek_internal(0, SEEK_END); }
eM2TSFile::eM2TSFile(const char *filename, bool cached) :m_lock(false), m_sync_offset(0), m_fd(-1), m_file(NULL), m_current_offset(0), m_length(0), m_cached(cached) { if (!m_cached) m_fd = ::open(filename, O_RDONLY | O_LARGEFILE); else m_file = ::fopen64(filename, "rb"); if (valid()) m_current_offset = m_length = lseek_internal(0, SEEK_END); }
off_t eM2TSFile::lseek(off_t offset, int whence) { eSingleLocker l(m_lock); offset = (offset % 188) + (offset * 192) / 188; if (offset != m_current_offset) m_current_offset = lseek_internal(offset, whence); return m_current_offset; }
/* move the read/write file offset */ off_t lseek(int fildes, off_t offset, int whence) { DEBUGF("lseek(fd=%d,ofs=%ld,wh=%d)\n", fildes, (long)offset, whence); struct filestr_desc * const file = GET_FILESTR(READER, fildes); if (!file) FILE_ERROR_RETURN(ERRNO, -1); off_t rc = lseek_internal(file, offset, whence); if (rc < 0) FILE_ERROR(ERRNO, rc * 10 - 2); file_error: RELEASE_FILESTR(READER, file); return rc; }
ssize_t eM2TSFile::read(off_t offset, void *b, size_t count) { eSingleLocker l(m_lock); unsigned char tmp[192*3]; unsigned char *buf = (unsigned char*)b; size_t rd=0; offset = (offset % 188) + (offset * 192) / 188; sync: if ((offset+m_sync_offset) != m_current_offset) { // eDebug("seekTo %lld", offset+m_sync_offset); m_current_offset = lseek_internal(offset+m_sync_offset, SEEK_SET); if (m_current_offset < 0) return m_current_offset; } while (rd < count) { size_t ret; ret = ::read(m_fd, tmp, 192); if (ret < 0 || ret < 192) return rd ? rd : ret; if (tmp[4] != 0x47) { if (rd > 0) { eDebug("short read at pos %lld async!!", m_current_offset); return rd; } else { int x=0; ret = ::read(m_fd, tmp+192, 384); #if 0 eDebugNoNewLine("m2ts out of sync at pos %lld, real %lld:", offset + m_sync_offset, m_current_offset); for (; x < 192; ++x) eDebugNoNewLine(" %02x", tmp[x]); eDebug(""); x=0; #else eDebug("m2ts out of sync at pos %lld, real %lld", offset + m_sync_offset, m_current_offset); #endif for (; x < 192; ++x) { if (tmp[x] == 0x47 && tmp[x+192] == 0x47) { int add_offs = (x - 4); eDebug("sync found at pos %d, sync_offset is now %d, old was %d", x, add_offs + m_sync_offset, m_sync_offset); m_sync_offset += add_offs; goto sync; } } } } memcpy(buf+rd, tmp+4, 188); rd += 188; m_current_offset += 188; } m_sync_offset %= 188; return rd; }
loff_t lseek(loff_t offset, uint32_t origin) { return lseek_internal(offset, origin); }