Ticket #7416: mediaplayer.diff
File mediaplayer.diff, 7.8 KB (added by , 13 years ago) |
---|
-
src/apps/mediaplayer/Controller.cpp
30 30 #include <Autolock.h> 31 31 #include <Bitmap.h> 32 32 #include <Debug.h> 33 #include <Directory.h> 34 #include <MediaFile.h> 33 35 #include <Path.h> 34 36 #include <Window.h> // for debugging only 35 37 36 38 #include "AutoDeleter.h" 37 39 #include "ControllerView.h" 40 #include "FilePlaylistItem.h" 38 41 #include "MainApp.h" 39 42 #include "PlaybackState.h" 40 43 #include "Settings.h" … … 42 45 43 46 // suppliers 44 47 #include "AudioTrackSupplier.h" 48 #include "MediaFileTrackSupplier.h" 45 49 #include "MediaTrackAudioSupplier.h" 46 50 #include "MediaTrackVideoSupplier.h" 47 51 #include "ProxyAudioSupplier.h" … … 288 292 289 293 fTrackSupplier = trackSupplier; 290 294 295 _BindExtraMedia(); 296 291 297 SelectAudioTrack(0); 292 298 SelectVideoTrack(0); 293 299 … … 1179 1185 } 1180 1186 1181 1187 1188 // If fTrackSupplier has only either audio or video, _BindExtraMedia() searches 1189 // alternative video or audio and addes it to fTrackSupplier as a extra media. 1182 1190 void 1191 Controller::_BindExtraMedia() 1192 { 1193 // If fTrackSupplier has already had both audio and video, there is no need 1194 // to search alternative media. 1195 if ( fTrackSupplier->CountAudioTracks() == 0 && fTrackSupplier->CountVideoTracks() == 0 1196 || fTrackSupplier->CountAudioTracks() > 0 && fTrackSupplier->CountVideoTracks() > 0 ) 1197 return; 1198 1199 // Currently, _BindExtraMedia() searches alternative media only from files. 1200 FilePlaylistItem* fileItem = dynamic_cast<FilePlaylistItem*>(fItem.Get()); 1201 if ( fileItem == NULL ) 1202 return; 1203 1204 // For example, if the media file is foo.mp3, _BindExtraMedia() searches foo.avi 1205 // and addes video track of foo.avi as a extra media. 1206 BPath mediaFilePath(&fileItem->Ref()); 1207 BString mediaFilePathString = mediaFilePath.Path(); 1208 BPath dirPath; 1209 mediaFilePath.GetParent(&dirPath); 1210 BDirectory dir(dirPath.Path()); 1211 if ( dir.InitCheck() != B_OK ) 1212 return; 1213 1214 BEntry entry; 1215 BString entryPathString; 1216 while ( dir.GetNextEntry(&entry, true) == B_OK ) { 1217 if ( !entry.IsFile() ) 1218 continue; 1219 entryPathString = BPath(&entry).Path(); 1220 if ( entryPathString != mediaFilePathString 1221 && _GetExceptExtension(entryPathString) == _GetExceptExtension(mediaFilePathString) ) { 1222 if ( _BindExtraMedia(entryPathString) == B_OK ) 1223 return; 1224 } 1225 } 1226 } 1227 1228 1229 status_t 1230 Controller::_BindExtraMedia(const BString& path) 1231 { 1232 // Currently, _BindExtraMedia() searches alternative media only from files. 1233 MediaFileTrackSupplier* supplier = dynamic_cast<MediaFileTrackSupplier*>(fTrackSupplier); 1234 if ( supplier == NULL ) 1235 return B_ERROR; 1236 1237 entry_ref ref; 1238 get_ref_for_path(path.String(), &ref); 1239 BMediaFile* mediaFile = new BMediaFile(&ref); 1240 return supplier->AddExtraMediaTracks(mediaFile); 1241 } 1242 1243 1244 BString 1245 Controller::_GetExceptExtension(const BString& path) const 1246 { 1247 int32 periodPos = path.FindLast('.'); 1248 if ( periodPos <= path.FindLast('/') ) 1249 return path; 1250 return BString(path.String(), periodPos); 1251 } 1252 1253 1254 void 1183 1255 Controller::NotifyPlayModeChanged(int32 mode) const 1184 1256 { 1185 1257 uint32 state = _PlaybackState(mode); -
src/apps/mediaplayer/Controller.h
175 175 void _NotifyVolumeChanged(float volume) const; 176 176 void _NotifyMutedChanged(bool muted) const; 177 177 178 // extra media binding 179 void _BindExtraMedia(); 180 status_t _BindExtraMedia(const BString& path); 181 BString _GetExceptExtension(const BString& path) const; 182 178 183 // overridden from PlaybackManager so that we 179 184 // can use our own Listener mechanism 180 185 virtual void NotifyPlayModeChanged(int32 mode) const; -
src/apps/mediaplayer/supplier/MediaFileTrackSupplier.cpp
22 22 MediaFileTrackSupplier::MediaFileTrackSupplier(BMediaFile* mediaFile) 23 23 : 24 24 TrackSupplier(), 25 fMediaFile(mediaFile) 25 fMediaFile(mediaFile), 26 fExtraMediaFile(NULL) 26 27 { 27 if (fMediaFile->InitCheck() != B_OK) 28 return; 29 int trackCount = fMediaFile->CountTracks(); 30 if (trackCount <= 0) 31 return; 32 33 for (int i = 0; i < trackCount; i++) { 34 BMediaTrack* track = fMediaFile->TrackAt(i); 35 media_format format; 36 status_t status = track->EncodedFormat(&format); 37 if (status != B_OK) { 38 fprintf(stderr, "MediaFileTrackSupplier: EncodedFormat failed for " 39 "track index %d, error: %s\n", i, strerror(status)); 40 fMediaFile->ReleaseTrack(track); 41 continue; 42 } 43 44 if (track->Duration() <= 0) { 45 fprintf(stderr, "MediaFileTrackSupplier: warning! track index %d " 46 "has no duration\n", i); 47 } 48 49 if (format.IsAudio()) { 50 if (!fAudioTracks.AddItem(track)) { 51 fMediaFile->ReleaseTrack(track); 52 return; 53 } 54 } else if (format.IsVideo()) { 55 if (!fVideoTracks.AddItem(track)) { 56 fMediaFile->ReleaseTrack(track); 57 return; 58 } 59 } else { 60 printf("MediaFileTrackSupplier: track index %d has unknown " 61 "type\n", i); 62 fMediaFile->ReleaseTrack(track); 63 } 64 } 28 _AddTracks(fMediaFile, true, true); 65 29 } 66 30 67 31 68 32 MediaFileTrackSupplier::~MediaFileTrackSupplier() 69 33 { 70 34 delete fMediaFile; 35 delete fExtraMediaFile; 71 36 // BMediaFile destructor will call ReleaseAllTracks() 72 37 for (int32 i = fSubTitleTracks.CountItems() - 1; i >= 0; i--) 73 38 delete reinterpret_cast<SubTitles*>(fSubTitleTracks.ItemAtFast(i)); … … 189 154 return fSubTitleTracks.AddItem(subTitles); 190 155 } 191 156 157 158 status_t 159 MediaFileTrackSupplier::AddExtraMediaTracks(BMediaFile* mediaFile) 160 { 161 delete fExtraMediaFile; 162 fExtraMediaFile = mediaFile; 163 if ( _AddTracks(mediaFile, CountAudioTracks() == 0, CountVideoTracks() == 0) != B_OK ) 164 return B_ERROR; 165 return B_OK; 166 } 167 168 169 status_t 170 MediaFileTrackSupplier::_AddTracks(BMediaFile* mediaFile, bool audio, bool video) 171 { 172 if (mediaFile->InitCheck() != B_OK) 173 return B_ERROR; 174 int trackCount = mediaFile->CountTracks(); 175 if (trackCount <= 0) 176 return B_ERROR; 177 178 status_t funcStatus = B_ERROR; 179 for (int i = 0; i < trackCount; i++) { 180 BMediaTrack* track = mediaFile->TrackAt(i); 181 media_format format; 182 status_t status = track->EncodedFormat(&format); 183 if (status != B_OK) { 184 fprintf(stderr, "MediaFileTrackSupplier: EncodedFormat failed for " 185 "track index %d, error: %s\n", i, strerror(status)); 186 mediaFile->ReleaseTrack(track); 187 continue; 188 } 189 190 if (track->Duration() <= 0) { 191 fprintf(stderr, "MediaFileTrackSupplier: warning! track index %d " 192 "has no duration\n", i); 193 } 194 195 if (format.IsAudio()) { 196 if (audio && fAudioTracks.AddItem(track)) { 197 funcStatus = B_OK; 198 } else { 199 mediaFile->ReleaseTrack(track); 200 } 201 } else if (format.IsVideo()) { 202 if (video && fVideoTracks.AddItem(track)) { 203 funcStatus = B_OK; 204 } else { 205 mediaFile->ReleaseTrack(track); 206 } 207 } else { 208 printf("MediaFileTrackSupplier: track index %d has unknown " 209 "type\n", i); 210 mediaFile->ReleaseTrack(track); 211 } 212 } 213 return funcStatus; 214 } 215 216 -
src/apps/mediaplayer/supplier/MediaFileTrackSupplier.h
41 41 42 42 bool AddSubTitles(SubTitles* subTitles); 43 43 44 status_t AddExtraMediaTracks(BMediaFile* mediaFile); 45 44 46 private: 47 status_t _AddTracks(BMediaFile* mediaFile, bool audio, bool video); 48 49 private: 45 50 BMediaFile* fMediaFile; 51 BMediaFile* fExtraMediaFile; 46 52 BList fAudioTracks; 47 53 BList fVideoTracks; 48 54 BList fSubTitleTracks;