1、在thumbnail.java文件里通過調(diào)用bitmap = retriever.getFrameAtTime(-1);
這句代碼得到bitmap,
2、那么這句代碼在MediaMetadataRetriever.java 中調(diào)用
getFrameAtTime(timeUs, OPTION_CLOSEST_SYNC)這句代碼:
解釋一下timeUs,和OPTION_CLOSEST_SYNC這2個(gè)變量的含義
timeUs The time position where the frame will be retrieved.
* When retrieving the frame at the given time position, there is no
* guarentee that the data source has a frame located at the position.
* When this happens, a frame nearby will be returned. If timeUs is
* negative, time position and option will ignored, and any frame
* that the implementation considers as representative may be returned
3.由于timeUs等于-1,那么在stagefrightMetadataRetriver.cpp中通過
extractVideoFrameWithCodecFlags()函數(shù)
復(fù)制代碼 代碼如下:
if (frameTimeUs < 0) {
if (!trackMeta->findInt64(kKeyThumbnailTime, &thumbNailTime)
|| thumbNailTime < 0) {
thumbNailTime = 0;
}
options.setSeekTo(thumbNailTime, mode);
}else{
...................
}
取得thumbnailTime,
thumbnailTime是取同步幀中最大一幀數(shù)據(jù),即有可能不是視頻文件的第一個(gè)I幀。
4.extractVideoFrameWithCodecFlags()函數(shù)中,接著第3條,然后調(diào)用err = decoder->read(&buffer, &options);這句代碼,其options->seekMode為SEEK_CLOSEST_SYNC這個(gè)值
5.如果video codec是mpeg4,則調(diào)用MPEG4Extractor.cpp中的read()的函數(shù),
根據(jù)前面thumnailtime,找到此時(shí)間點(diǎn)的vidoe frame index,然后通過 video frame index,再找臨近的同步幀(即I幀)
6.SampleTable.cpp中findSyncSampleNear()函數(shù)中,找臨近同步幀,
視頻文件中會(huì)存有所有的同步幀,這個(gè)同步幀也有可能是這個(gè)同步幀數(shù)組中第一個(gè)值,也有可能在第5步中得到的video frame index,也有可能位于2個(gè)同步幀之間,那么我們通過計(jì)算找到這2個(gè)同步幀最靠近video frame index的一個(gè)同步幀
7.通過上述步驟,找到同步幀,那么根據(jù)這個(gè)同步幀生成thumbnail的bitmap。