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