1 /* 2 * Copyright 2009-2010 Stephan Aßmus <superstippi@gmx.de> 3 * All rights reserved. Distributed under the terms of the MIT license. 4 */ 5 6 #include "PlaylistItem.h" 7 8 #include <stdio.h> 9 10 #include <Catalog.h> 11 #include <Locale.h> 12 13 #include "AudioTrackSupplier.h" 14 #include "TrackSupplier.h" 15 #include "VideoTrackSupplier.h" 16 17 #undef B_TRANSLATION_CONTEXT 18 #define B_TRANSLATION_CONTEXT "MediaPlayer-PlaylistItem" 19 20 21 PlaylistItem::Listener::Listener() 22 { 23 } 24 25 26 PlaylistItem::Listener::~Listener() 27 { 28 } 29 30 31 void PlaylistItem::Listener::ItemChanged(const PlaylistItem* item) 32 { 33 } 34 35 36 // #pragma mark - 37 38 39 // #define DEBUG_INSTANCE_COUNT 40 #ifdef DEBUG_INSTANCE_COUNT 41 static vint32 sInstanceCount = 0; 42 #endif 43 44 45 PlaylistItem::PlaylistItem() 46 : 47 fPlaybackFailed(false), 48 fTrackSupplier(NULL) 49 { 50 #ifdef DEBUG_INSTANCE_COUNT 51 atomic_add(&sInstanceCount, 1); 52 printf("%p->PlaylistItem::PlaylistItem() (%ld)\n", this, sInstanceCount); 53 #endif 54 } 55 56 57 PlaylistItem::~PlaylistItem() 58 { 59 #ifdef DEBUG_INSTANCE_COUNT 60 atomic_add(&sInstanceCount, -1); 61 printf("%p->PlaylistItem::~PlaylistItem() (%ld)\n", this, sInstanceCount); 62 #endif 63 } 64 65 66 TrackSupplier* 67 PlaylistItem::GetTrackSupplier() 68 { 69 if (fTrackSupplier == NULL) 70 fTrackSupplier = _CreateTrackSupplier(); 71 72 return fTrackSupplier; 73 } 74 75 76 void 77 PlaylistItem::ReleaseTrackSupplier() 78 { 79 delete fTrackSupplier; 80 fTrackSupplier = NULL; 81 } 82 83 84 bool 85 PlaylistItem::HasTrackSupplier() const 86 { 87 return fTrackSupplier != NULL; 88 } 89 90 91 BString 92 PlaylistItem::Name() const 93 { 94 BString name; 95 if (GetAttribute(ATTR_STRING_NAME, name) != B_OK) 96 name = B_TRANSLATE_CONTEXT("<unnamed>", "PlaylistItem-name"); 97 return name; 98 } 99 100 101 BString 102 PlaylistItem::Author() const 103 { 104 BString author; 105 if (GetAttribute(ATTR_STRING_AUTHOR, author) != B_OK) 106 author = B_TRANSLATE_CONTEXT("<unknown>", "PlaylistItem-author"); 107 return author; 108 } 109 110 111 BString 112 PlaylistItem::Album() const 113 { 114 BString album; 115 if (GetAttribute(ATTR_STRING_ALBUM, album) != B_OK) 116 album = B_TRANSLATE_CONTEXT("<unknown>", "PlaylistItem-album"); 117 return album; 118 } 119 120 121 BString 122 PlaylistItem::Title() const 123 { 124 BString title; 125 if (GetAttribute(ATTR_STRING_TITLE, title) != B_OK) 126 title = B_TRANSLATE_CONTEXT("<untitled>", "PlaylistItem-title"); 127 return title; 128 } 129 130 131 int32 132 PlaylistItem::TrackNumber() const 133 { 134 int32 trackNumber; 135 if (GetAttribute(ATTR_INT32_TRACK, trackNumber) != B_OK) 136 trackNumber = 0; 137 return trackNumber; 138 } 139 140 141 bigtime_t 142 PlaylistItem::Duration() 143 { 144 bigtime_t duration; 145 if (GetAttribute(ATTR_INT64_DURATION, duration) != B_OK) { 146 duration = this->_CalculateDuration(); 147 SetAttribute(ATTR_INT64_DURATION, duration); 148 } 149 150 return duration; 151 } 152 153 154 int64 155 PlaylistItem::LastFrame() const 156 { 157 int64 lastFrame; 158 if (GetAttribute(ATTR_INT64_FRAME, lastFrame) != B_OK) 159 lastFrame = 0; 160 return lastFrame; 161 } 162 163 164 float 165 PlaylistItem::LastVolume() const 166 { 167 float lastVolume; 168 if (GetAttribute(ATTR_FLOAT_VOLUME, lastVolume) != B_OK) 169 lastVolume = -1; 170 return lastVolume; 171 } 172 173 174 status_t 175 PlaylistItem::SetLastFrame(int64 value) 176 { 177 return SetAttribute(ATTR_INT64_FRAME, value); 178 } 179 180 181 status_t 182 PlaylistItem::SetLastVolume(float value) 183 { 184 return SetAttribute(ATTR_FLOAT_VOLUME, value); 185 } 186 187 188 void 189 PlaylistItem::SetPlaybackFailed() 190 { 191 fPlaybackFailed = true; 192 } 193 194 195 //! You must hold the Playlist lock. 196 bool 197 PlaylistItem::AddListener(Listener* listener) 198 { 199 if (listener && !fListeners.HasItem(listener)) 200 return fListeners.AddItem(listener); 201 return false; 202 } 203 204 205 //! You must hold the Playlist lock. 206 void 207 PlaylistItem::RemoveListener(Listener* listener) 208 { 209 fListeners.RemoveItem(listener); 210 } 211 212 213 void 214 PlaylistItem::_NotifyListeners() const 215 { 216 BList listeners(fListeners); 217 int32 count = listeners.CountItems(); 218 for (int32 i = 0; i < count; i++) { 219 Listener* listener = (Listener*)listeners.ItemAtFast(i); 220 listener->ItemChanged(this); 221 } 222 } 223 224 225 bigtime_t PlaylistItem::_CalculateDuration() 226 { 227 // To be overridden in subclasses with more efficient methods 228 TrackSupplier* supplier = GetTrackSupplier(); 229 230 AudioTrackSupplier* au = supplier->CreateAudioTrackForIndex(0); 231 VideoTrackSupplier* vi = supplier->CreateVideoTrackForIndex(0); 232 233 bigtime_t duration = max_c(au == NULL ? 0 : au->Duration(), 234 vi == NULL ? 0 : vi->Duration()); 235 236 delete vi; 237 delete au; 238 ReleaseTrackSupplier(); 239 240 return duration; 241 } 242 243