1 /* 2 * Copyright 2016, Dario Casalinuovo 3 * All rights reserved. Distributed under the terms of the MIT license. 4 */ 5 6 7 #include "UrlPlaylistItem.h" 8 9 #include <MediaFile.h> 10 #include <MediaTrack.h> 11 12 #include "MediaFileTrackSupplier.h" 13 14 15 UrlPlaylistItem::UrlPlaylistItem(BUrl url) 16 : 17 fUrl(url), 18 fDuration(-1) 19 { 20 } 21 22 23 UrlPlaylistItem::UrlPlaylistItem(const UrlPlaylistItem& item) 24 { 25 fUrl = BUrl(item.Url()); 26 } 27 28 29 UrlPlaylistItem::UrlPlaylistItem(const BMessage* archive) 30 { 31 const char* url = NULL; 32 if (archive->FindString("mediaplayer:url", &url) == B_OK) 33 fUrl = BUrl(url); 34 } 35 36 37 UrlPlaylistItem::~UrlPlaylistItem() 38 { 39 } 40 41 42 PlaylistItem* 43 UrlPlaylistItem::Clone() const 44 { 45 return new UrlPlaylistItem(fUrl); 46 } 47 48 49 BArchivable* 50 UrlPlaylistItem::Instantiate(BMessage* archive) 51 { 52 if (validate_instantiation(archive, "UrlPlaylistItem")) 53 return new (std::nothrow) UrlPlaylistItem(archive); 54 55 return NULL; 56 } 57 58 59 status_t 60 UrlPlaylistItem::Archive(BMessage* into, bool deep) const 61 { 62 return into->AddString("mediaplayer:url", fUrl.UrlString()); 63 } 64 65 66 status_t 67 UrlPlaylistItem::SetAttribute(const Attribute& attribute, const BString& string) 68 { 69 return B_ERROR; 70 } 71 72 73 status_t 74 UrlPlaylistItem::GetAttribute(const Attribute& attribute, BString& string) const 75 { 76 if (attribute == ATTR_STRING_NAME) { 77 string = fUrl.UrlString(); 78 return B_OK; 79 } 80 81 return B_NOT_SUPPORTED; 82 } 83 84 85 status_t 86 UrlPlaylistItem::SetAttribute(const Attribute& attribute, const int32& value) 87 { 88 return B_NOT_SUPPORTED; 89 } 90 91 92 status_t 93 UrlPlaylistItem::GetAttribute(const Attribute& attribute, int32& value) const 94 { 95 return B_NOT_SUPPORTED; 96 } 97 98 99 status_t 100 UrlPlaylistItem::SetAttribute(const Attribute& attribute, const int64& value) 101 { 102 return B_NOT_SUPPORTED; 103 } 104 105 106 status_t 107 UrlPlaylistItem::GetAttribute(const Attribute& attribute, int64& value) const 108 { 109 if (attribute == ATTR_INT64_DURATION && fDuration >= 0) { 110 value = fDuration; 111 return B_OK; 112 } 113 return B_NOT_SUPPORTED; 114 } 115 116 117 BString 118 UrlPlaylistItem::LocationURI() const 119 { 120 return fUrl.UrlString(); 121 } 122 123 124 status_t 125 UrlPlaylistItem::GetIcon(BBitmap* bitmap, icon_size iconSize) const 126 { 127 return B_NOT_SUPPORTED; 128 } 129 130 131 status_t 132 UrlPlaylistItem::MoveIntoTrash() 133 { 134 return B_NOT_SUPPORTED; 135 } 136 137 138 status_t 139 UrlPlaylistItem::RestoreFromTrash() 140 { 141 return B_NOT_SUPPORTED; 142 } 143 144 145 bigtime_t 146 UrlPlaylistItem::_CalculateDuration() 147 { 148 if (fDuration < 0) { 149 BMediaFile mediaFile(fUrl); 150 151 if (mediaFile.InitCheck() != B_OK || mediaFile.CountTracks() < 1) 152 return 0; 153 fDuration = mediaFile.TrackAt(0)->Duration(); 154 } 155 return fDuration; 156 } 157 158 159 TrackSupplier* 160 UrlPlaylistItem::_CreateTrackSupplier() const 161 { 162 MediaFileTrackSupplier* supplier 163 = new(std::nothrow) MediaFileTrackSupplier(); 164 if (supplier == NULL) 165 return NULL; 166 167 BMediaFile* mediaFile = new(std::nothrow) BMediaFile(fUrl); 168 if (mediaFile == NULL || supplier->AddMediaFile(mediaFile) != B_OK) { 169 delete mediaFile; 170 delete supplier; 171 return NULL; 172 } 173 174 return supplier; 175 } 176 177 178 BUrl 179 UrlPlaylistItem::Url() const 180 { 181 return fUrl; 182 } 183