Android StageFrightMediaScanner source code parsing
1. Introduction
In Android, StageFrightMediaScanner processes multimedia files.
In addition, the supported multimedia file types are defined in StageFrightMediaScanner.
File Location
FrameworksavmedialibstagefrightStagefrightMediaScanner. cpp
Compilation target
Libstagefright. so
2. processFile
ProcessFile does not process any data. It mainly calls processFileInternal.
In addition, we can see that the beginFile and endFile methods of MediaScannerClient are called in processFile. In terms of time, google does not implement the beginFile and endFile methods.
(To be honest, Android5.0 is really bad, and many functions are not completely developed)
MediaScanResult StagefrightMediaScanner: processFile (const char * path, const char * mimeType, MediaScannerClient & client) {// MediaScannerClient is not implemented at all, so do not care about the client. setLocale (locale (); client. beginFile (); MediaScanResult result = processFileInternal (path, mimeType, client); client. endFile (); return result ;}
3. processFileInternal
ProcessFileInternal can be said to be the final node for MediaScanner to process multimedia files
In this function, you can call MediaMetadataRetriever to obtain multimedia information.
The procedure of calling MediaMetadataRetriever to obtain media file information is as follows:
(1) MediaMetadataRetriever. setDataSource (file)
(2) MediaMetadataRetriever. extractMetadata (key)
MediaScanResult StagefrightMediaScanner::processFileInternal( const char *path, const char * /* mimeType */, MediaScannerClient &client) { const char *extension = strrchr(path, '.'); ///check file type if (!extension) { return MEDIA_SCAN_RESULT_SKIPPED; } if (!FileHasAcceptableExtension(extension)) { return MEDIA_SCAN_RESULT_SKIPPED; } //---------------------------------------- ///Init & setDataSource MediaMetadataRetriever sp
mRetriever(new MediaMetadataRetriever); int fd = open(path, O_RDONLY | O_LARGEFILE); status_t status; if (fd < 0) { // couldn't open it locally, maybe the media server can? status = mRetriever->setDataSource(NULL /* httpService */, path); } else { status = mRetriever->setDataSource(fd, 0, 0x7ffffffffffffffL); close(fd); } //---------------------------------------- ///get MIMETYPE const char *value; if ((value = mRetriever->extractMetadata( METADATA_KEY_MIMETYPE)) != NULL) { status = client.setMimeType(value); if (status) { return MEDIA_SCAN_RESULT_ERROR; } } //---------------------------------------- ......... ///get metadata for (size_t i = 0; i < kNumEntries; ++i) { const char *value; if ((value = mRetriever->extractMetadata(kKeyMap[i].key)) != NULL) { status = client.addStringTag(kKeyMap[i].tag, value); if (status != OK) { return MEDIA_SCAN_RESULT_ERROR; } } } return MEDIA_SCAN_RESULT_OK;}