1 int FileSource::ReadSeek( int offset, int whence){ 2 // 參數檢查. 3 int ret=-1; 4 if (offset > m_nFileSize || !m_file.is_open()){ 5 LOGI("seek offset=%d> file size:%d,or file is close.",offset,m_nFileSize); 6 return -1; 7 } 8 if (m_bIsMultithread) 9 m_mutex.Lock(); 10 11 std::ios_base::seekdir way = (std::ios_base::seekdir)whence; 12 // 進行seek操做. 13 if (whence != AVSEEK_SIZE) { 14 //先把錯誤位置給 15 m_file.clear(); 16 ////////////////////////////// 17 switch(whence){ 18 case SEEK_SET: 19 //基於文件開始位置進行seek跳轉 20 m_file.seekg(offset,std::ios_base::beg); 21 // ret = offset; 22 break; 23 case SEEK_CUR: 24 //基於文件當前位置進行seek跳轉 25 m_file.seekg(offset,std::ios_base::cur); 26 // ret=m_file.tellg(); 27 break; 28 case SEEK_END: 29 //基於文件尾的位置進行seek跳轉 30 m_file.seekg(offset,std::ios_base::end); 31 // ret=m_file.tellg(); 32 break; 33 } 34 if (m_file.good()) 35 ret = m_file.tellg(); 36 else 37 ret = -1; 38 }else{ 39 //等於AVSEEK_SIZE,這個主要是用於獲取文件大小 40 // The whence-parameter has one more option than fseek: AVSEEK_SIZE. 41 // When this option is passed to the seek function it should return the file size (if possible). 42 // If its not possible, the function may return and do nothing -1. In my implementation pStream->Seek(...) will fail with AVSEEK_SIZE and SeekFunc will return -1. 43 ret=m_nFileSize; 44 } 45 46 if (m_bIsMultithread) 47 m_mutex.Unlock(); 48 49 return ret; 50 }