public void TestAudioItem() { IList <IDirectoryObject> objects = new List <IDirectoryObject>(); Guid id = new Guid("11111111-aaaa-aaaa-aaaa-111111111111"); IDictionary <Guid, IList <MediaItemAspect> > aspects = new Dictionary <Guid, IList <MediaItemAspect> >(); SingleMediaItemAspect aspect1 = new SingleMediaItemAspect(MediaAspect.Metadata); aspect1.SetAttribute(MediaAspect.ATTR_TITLE, "The Track"); MediaItemAspect.SetAspect(aspects, aspect1); MultipleMediaItemAspect aspect2 = new MultipleMediaItemAspect(ProviderResourceAspect.Metadata); aspect2.SetAttribute(ProviderResourceAspect.ATTR_RESOURCE_ACCESSOR_PATH, "c:\\file.mp3"); MediaItemAspect.AddOrUpdateAspect(aspects, aspect2); SingleMediaItemAspect aspect3 = new SingleMediaItemAspect(AudioAspect.Metadata); MediaItemAspect.SetAspect(aspects, aspect3); MediaItem item = new MediaItem(id, aspects); objects.Add(MediaLibraryHelper.InstansiateMediaLibraryObject(item, null, null)); GenericDidlMessageBuilder builder = new GenericDidlMessageBuilder(); builder.BuildAll("*", objects); string xml = builder.ToString(); Console.WriteLine("XML: {0}", xml); }
private static void TransferTransientAspects(IDictionary <Guid, IList <MediaItemAspect> > aspects, IEnumerable <MediaItem> destinationMediaItems) { var transientAspects = MediaItemAspect.GetAspects(aspects).Where(mia => mia.Metadata.IsTransientAspect); foreach (MediaItemAspect aspect in transientAspects) { SingleMediaItemAspect singleAspect = aspect as SingleMediaItemAspect; if (singleAspect != null) { foreach (MediaItem destination in destinationMediaItems) { MediaItemAspect.SetAspect(destination.Aspects, singleAspect); } } else { MultipleMediaItemAspect multiAspect = aspect as MultipleMediaItemAspect; if (multiAspect != null) { foreach (MediaItem destination in destinationMediaItems) { MediaItemAspect.AddOrUpdateAspect(destination.Aspects, multiAspect); } } } } }
public void TestAlbumItem() { EndPointSettings settings = new EndPointSettings { PreferredSubtitleLanguages = "EN", PreferredAudioLanguages = "EN", DefaultSubtitleEncodings = "" }; IList <IDirectoryObject> objects = new List <IDirectoryObject>(); Guid albumId = new Guid("11111111-aaaa-aaaa-aaaa-100000000001"); IDictionary <Guid, IList <MediaItemAspect> > aspects = new Dictionary <Guid, IList <MediaItemAspect> >(); SingleMediaItemAspect aspect = new SingleMediaItemAspect(MediaAspect.Metadata); aspect.SetAttribute(MediaAspect.ATTR_TITLE, "The Album"); MediaItemAspect.SetAspect(aspects, aspect); MediaItem album = new MediaItem(albumId, aspects); MediaLibraryAlbumItem item = new TestMediaLibraryAlbumItem(album, settings); item.Initialise(); objects.Add(item); GenericDidlMessageBuilder builder = new GenericDidlMessageBuilder(); builder.BuildAll("*", objects); string xml = builder.ToString(); Console.WriteLine("XML: {0}", xml); }
protected VideoDriveHandler(DriveInfo driveInfo, IEnumerable <Guid> extractedMIATypeIds) : base(driveInfo) { IMediaAccessor mediaAccessor = ServiceRegistration.Get <IMediaAccessor>(); ResourcePath rp = LocalFsResourceProviderBase.ToResourcePath(driveInfo.Name); IResourceAccessor ra; if (!rp.TryCreateLocalResourceAccessor(out ra)) { throw new ArgumentException(string.Format("Unable to access drive '{0}'", driveInfo.Name)); } using (ra) _mediaItem = mediaAccessor.CreateLocalMediaItem(ra, mediaAccessor.GetMetadataExtractorsForMIATypes(extractedMIATypeIds)); if (_mediaItem == null) { throw new Exception(string.Format("Could not create media item for drive '{0}'", driveInfo.Name)); } MatchWithStubs(driveInfo, new MediaItem[] { _mediaItem }); IEnumerable <MediaItem> processedItems = CertificationHelper.ProcessMediaItems(new MediaItem[] { _mediaItem }); if (processedItems.Count() == 0) { _mediaItem = null; return; } _mediaItem = processedItems.First(); SingleMediaItemAspect mia = null; MediaItemAspect.TryGetAspect(_mediaItem.Aspects, MediaAspect.Metadata, out mia); mia.SetAttribute(MediaAspect.ATTR_TITLE, mia.GetAttributeValue(MediaAspect.ATTR_TITLE) + " (" + DriveUtils.GetDriveNameWithoutRootDirectory(driveInfo) + ")"); }
public MockedDatabaseMovie(MediaLibraryMovie movie, int playPercentage) { IDictionary <Guid, IList <MediaItemAspect> > movieAspects = new Dictionary <Guid, IList <MediaItemAspect> >(); MultipleMediaItemAspect resourceAspect = new MultipleMediaItemAspect(ProviderResourceAspect.Metadata); resourceAspect.SetAttribute(ProviderResourceAspect.ATTR_RESOURCE_ACCESSOR_PATH, "c:\\" + movie.Title + ".mkv"); MediaItemAspect.AddOrUpdateAspect(movieAspects, resourceAspect); MediaItemAspect.AddOrUpdateExternalIdentifier(movieAspects, ExternalIdentifierAspect.SOURCE_IMDB, ExternalIdentifierAspect.TYPE_MOVIE, movie.Imdb); MediaItemAspect.SetAttribute(movieAspects, MovieAspect.ATTR_MOVIE_NAME, movie.Title); SingleMediaItemAspect mediaItemAspect = new SingleMediaItemAspect(MediaAspect.Metadata); mediaItemAspect.SetAttribute(MediaAspect.ATTR_PLAYCOUNT, 1); mediaItemAspect.SetAttribute(MediaAspect.ATTR_LASTPLAYED, DateTime.Now); MediaItemAspect.SetAspect(movieAspects, mediaItemAspect); SingleMediaItemAspect importerAspect = new SingleMediaItemAspect(ImporterAspect.Metadata); importerAspect.SetAttribute(ImporterAspect.ATTR_DATEADDED, DateTime.Now); IDictionary <string, string> userData = new Dictionary <string, string>(); userData.Add(UserDataKeysKnown.KEY_PLAY_PERCENTAGE, playPercentage.ToString()); MediaItemAspect.SetAspect(movieAspects, importerAspect); Movie = new MediaItem(Guid.NewGuid(), movieAspects, userData); }
public static void StoreAlbum(IDictionary <Guid, IList <MediaItemAspect> > aspects, string albumName, string albumSortName) { SingleMediaItemAspect personAspect = MediaItemAspect.GetOrCreateAspect(aspects, TempAlbumAspect.Metadata); personAspect.SetAttribute(TempAlbumAspect.ATTR_NAME, albumName); personAspect.SetAttribute(TempAlbumAspect.ATTR_SORT_NAME, albumSortName); }
protected Guid GetOrAddDirectory(ImportJob importJob, IFileSystemResourceAccessor directoryAccessor, Guid parentDirectoryId, IMediaBrowsing mediaBrowsing, IImportResultHandler resultHandler) { ResourcePath directoryPath = directoryAccessor.CanonicalLocalResourcePath; MediaItem directoryItem = mediaBrowsing.LoadLocalItemAsync(directoryPath, EMPTY_MIA_ID_ENUMERATION, DIRECTORY_MIA_ID_ENUMERATION).Result; if (directoryItem != null) { SingleMediaItemAspect da; if (!MediaItemAspect.TryGetAspect(directoryItem.Aspects, DirectoryAspect.Metadata, out da)) { // This is the case if the path was formerly imported as a non-directory media item; we cannot reuse it resultHandler.DeleteMediaItemAsync(directoryPath); directoryItem = null; } } if (directoryItem == null) { // Add directory item to ML MediaItemAspect mia = new SingleMediaItemAspect(MediaAspect.Metadata); mia.SetAttribute(MediaAspect.ATTR_TITLE, directoryAccessor.ResourceName); mia.SetAttribute(MediaAspect.ATTR_SORT_TITLE, directoryAccessor.ResourceName); mia.SetAttribute(MediaAspect.ATTR_ISVIRTUAL, false); mia.SetAttribute(MediaAspect.ATTR_RECORDINGTIME, DateTime.MinValue); mia.SetAttribute(MediaAspect.ATTR_RATING, 0); mia.SetAttribute(MediaAspect.ATTR_COMMENT, null); mia.SetAttribute(MediaAspect.ATTR_LASTPLAYED, DateTime.MinValue); MediaItemAspect da = new SingleMediaItemAspect(DirectoryAspect.Metadata); IList <MediaItemAspect> aspects = new List <MediaItemAspect>(new[] { mia, da, }); return(resultHandler.UpdateMediaItemAsync(parentDirectoryId, directoryPath, aspects, importJob.JobType == ImportJobType.Refresh, importJob.BasePath).Result); } return(directoryItem.MediaItemId); }
/// <summary> /// Copies the contained person information into MediaItemAspect. /// </summary> /// <param name="aspectData">Dictionary with extracted aspects.</param> public override bool SetMetadata(IDictionary <Guid, IList <MediaItemAspect> > aspectData, bool force = false) { if (!force && !IsBaseInfoPresent) { return(false); } SingleMediaItemAspect subtitleAspect = MediaItemAspect.GetOrCreateAspect(aspectData, TempSubtitleAspect.Metadata); subtitleAspect.SetAttribute(TempSubtitleAspect.ATTR_PROVIDER, string.Join(";", DataProviders)); subtitleAspect.SetAttribute(TempSubtitleAspect.ATTR_CATEGORY, string.Join(";", Categories)); subtitleAspect.SetAttribute(TempSubtitleAspect.ATTR_NAME, Name); subtitleAspect.SetAttribute(TempSubtitleAspect.ATTR_DISPLAY_NAME, DisplayName); subtitleAspect.SetAttribute(TempSubtitleAspect.ATTR_SUBTITLEID, SubtitleId); subtitleAspect.SetAttribute(TempSubtitleAspect.ATTR_LANGUAGE, Language); int idx = 0; foreach (var mediaFile in MediaFiles) { MultipleMediaItemAspect resourceAspect = MediaItemAspect.CreateAspect(aspectData, ProviderResourceAspect.Metadata); resourceAspect.SetAttribute(ProviderResourceAspect.ATTR_RESOURCE_INDEX, idx++); resourceAspect.SetAttribute(ProviderResourceAspect.ATTR_TYPE, ProviderResourceAspect.TYPE_PRIMARY); resourceAspect.SetAttribute(ProviderResourceAspect.ATTR_MIME_TYPE, "subtitle/unknown"); resourceAspect.SetAttribute(ProviderResourceAspect.ATTR_SYSTEM_ID, mediaFile.NativeSystemId); resourceAspect.SetAttribute(ProviderResourceAspect.ATTR_RESOURCE_ACCESSOR_PATH, mediaFile.NativeResourcePath.Serialize()); } return(true); }
/// <summary> /// Adds a direcotry MediaItem to the MediaLibrary /// </summary> /// <param name="directoryAccessor">ResourceAccessor to the directory to be saved</param> /// <param name="parentDirectoryId">ID of the parent Directory</param> /// <returns></returns> private async Task <Guid> AddDirectory(IFileSystemResourceAccessor directoryAccessor, Guid parentDirectoryId) { var directoryMediaItem = await LoadLocalItem(directoryAccessor.CanonicalLocalResourcePath, PROVIDERRESOURCE_DIRECTORY_MIA_ID_ENUMERATION, EMPTY_MIA_ID_ENUMERATION).ConfigureAwait(false); if (directoryMediaItem == null) { var directoryPath = directoryAccessor.CanonicalLocalResourcePath; var mediaAspect = new SingleMediaItemAspect(MediaAspect.Metadata); mediaAspect.SetAttribute(MediaAspect.ATTR_TITLE, directoryAccessor.ResourceName); mediaAspect.SetAttribute(MediaAspect.ATTR_SORT_TITLE, directoryAccessor.ResourceName); mediaAspect.SetAttribute(MediaAspect.ATTR_ISVIRTUAL, false); mediaAspect.SetAttribute(MediaAspect.ATTR_RECORDINGTIME, DateTime.MinValue); mediaAspect.SetAttribute(MediaAspect.ATTR_RATING, 0); mediaAspect.SetAttribute(MediaAspect.ATTR_COMMENT, null); mediaAspect.SetAttribute(MediaAspect.ATTR_LASTPLAYED, DateTime.MinValue); var directoryAspect = new SingleMediaItemAspect(DirectoryAspect.Metadata); IList <MediaItemAspect> aspects = new List <MediaItemAspect>(new[] { mediaAspect, directoryAspect }); return(await UpdateMediaItem(parentDirectoryId, directoryPath, aspects, ImportJobInformation, false).ConfigureAwait(false)); } return(directoryMediaItem.MediaItemId); }
public static void StoreSeries(IDictionary <Guid, IList <MediaItemAspect> > aspects, SeriesStub series) { SingleMediaItemAspect seriesAspect = MediaItemAspect.GetOrCreateAspect(aspects, TempSeriesAspect.Metadata); seriesAspect.SetAttribute(TempSeriesAspect.ATTR_TVDBID, series.Id.HasValue ? series.Id.Value : 0); string title = !string.IsNullOrEmpty(series.Title) ? series.Title : series.ShowTitle; seriesAspect.SetAttribute(TempSeriesAspect.ATTR_NAME, title); if (!string.IsNullOrEmpty(series.SortTitle)) { seriesAspect.SetAttribute(TempSeriesAspect.ATTR_SORT_NAME, series.SortTitle); } else { seriesAspect.SetAttribute(TempSeriesAspect.ATTR_SORT_NAME, BaseInfo.GetSortTitle(title)); } seriesAspect.SetAttribute(TempSeriesAspect.ATTR_CERTIFICATION, series.Mpaa != null && series.Mpaa.Count > 0 ? series.Mpaa.First() : null); seriesAspect.SetAttribute(TempSeriesAspect.ATTR_ENDED, !string.IsNullOrEmpty(series.Status) ? series.Status.Contains("End") : false); seriesAspect.SetAttribute(TempSeriesAspect.ATTR_PLOT, !string.IsNullOrEmpty(series.Plot) ? series.Plot : series.Outline); seriesAspect.SetAttribute(TempSeriesAspect.ATTR_PREMIERED, series.Premiered.HasValue ? series.Premiered.Value : series.Year.HasValue ? series.Year.Value : default(DateTime?)); seriesAspect.SetCollectionAttribute(TempSeriesAspect.ATTR_GENRES, series.Genres); seriesAspect.SetAttribute(TempSeriesAspect.ATTR_RATING, series.Rating.HasValue ? Convert.ToDouble(series.Rating.Value) : 0.0); seriesAspect.SetAttribute(TempSeriesAspect.ATTR_VOTES, series.Votes.HasValue ? series.Votes.Value : series.Rating.HasValue ? 1 : 0); seriesAspect.SetAttribute(TempSeriesAspect.ATTR_STATION, series.Studio); }
public void TestSimpleItem() { Guid albumId = new Guid("11111111-aaaa-aaaa-aaaa-100000000001"); IDictionary <Guid, IList <MediaItemAspect> > aspects1 = new Dictionary <Guid, IList <MediaItemAspect> >(); SingleMediaItemAspect album1MIA1 = new SingleMediaItemAspect(MediaAspect.Metadata); album1MIA1.SetAttribute(MediaAspect.ATTR_TITLE, "The Album"); MediaItemAspect.SetAspect(aspects1, album1MIA1); MediaItem album1 = new MediaItem(albumId, aspects1); SingleMediaItemAspect mediaAspect1; Assert.IsTrue(MediaItemAspect.TryGetAspect(album1.Aspects, MediaAspect.Metadata, out mediaAspect1), "Media aspect"); Assert.AreEqual(false, mediaAspect1.Deleted, "Album title"); Assert.AreEqual("The Album", mediaAspect1.GetAttributeValue <string>(MediaAspect.ATTR_TITLE), "Album title"); TextWriter writer = new StringWriter(); XmlWriter serialiser = new XmlTextWriter(writer); serialiser.WriteStartElement("Test"); // Wrapper around the albums // Write the track twice album1.Serialize(serialiser); album1.Serialize(serialiser); serialiser.WriteEndElement(); Console.WriteLine("XML: {0}", writer.ToString()); //Assert.AreEqual("<MI Id=\"" + trackId + "\"><Relationship ItemType=\"" + AudioAspect.RELATIONSHIP_TRACK + "\" RelationshipType=\"" + AlbumAspect.RELATIONSHIP_ALBUM + "\" RelationshipId=\"" + albumId + "\" /></MI>", trackText.ToString(), "Track XML"); XmlReader reader = XmlReader.Create(new StringReader(writer.ToString())); reader.Read(); // Test //Console.WriteLine("Reader state Test, {0} {1}", reader.NodeType, reader.Name); // Read the track once reader.Read(); // MI //Console.WriteLine("Reader state track2, {0} {1}", reader.NodeType, reader.Name); MediaItem track2 = MediaItem.Deserialize(reader); SingleMediaItemAspect mediaAspect2; Assert.IsTrue(MediaItemAspect.TryGetAspect(track2.Aspects, MediaAspect.Metadata, out mediaAspect2), "Media aspect 2"); Assert.AreEqual("The Album", mediaAspect2.GetAttributeValue <string>(MediaAspect.ATTR_TITLE), "Album title 2"); Assert.AreEqual(false, mediaAspect2.Deleted, "Album delete state 2"); // Read the track again //Console.WriteLine("Reader state track3, {0} {1}", reader.NodeType, reader.Name); MediaItem track3 = MediaItem.Deserialize(reader); SingleMediaItemAspect mediaAspect3; Assert.IsTrue(MediaItemAspect.TryGetAspect(track3.Aspects, MediaAspect.Metadata, out mediaAspect3), "Media aspect 3"); Assert.AreEqual("The Album", mediaAspect3.GetAttributeValue <string>(MediaAspect.ATTR_TITLE), "Album title 3"); Assert.AreEqual(false, mediaAspect3.Deleted, "Album delete state 3"); reader.Read(); // Test }
public static MediaItemAspectMetadata.AttributeSpecification GetAttributeSpecification(MediaItem mediaItem, IEnumerable <MediaItemAspectMetadata.SingleAttributeSpecification> attributes, out MediaItemAspect aspect) { SingleMediaItemAspect singleAspect = null; MediaItemAspectMetadata.SingleAttributeSpecification attr = attributes.FirstOrDefault(a => MediaItemAspect.TryGetAspect(mediaItem.Aspects, a.ParentMIAM, out singleAspect)); aspect = singleAspect; return(attr); }
protected internal override void ReLoadItemsAndSubViewSpecifications(out IList <MediaItem> mediaItems, out IList <ViewSpecification> subViewSpecifications) { mediaItems = null; subViewSpecifications = null; IContentDirectory cd = ServiceRegistration.Get <IServerConnectionManager>().ContentDirectory; if (cd == null) { return; } Guid? userProfile = null; IUserManagement userProfileDataManagement = ServiceRegistration.Get <IUserManagement>(); if (userProfileDataManagement != null && userProfileDataManagement.IsValidUser) { userProfile = userProfileDataManagement.CurrentUser.ProfileId; } try { bool showVirtual = VirtualMediaHelper.ShowVirtualMedia(_necessaryMIATypeIds); mediaItems = new List <MediaItem>(cd.BrowseAsync(_directoryId, _necessaryMIATypeIds, _optionalMIATypeIds, userProfile, showVirtual).Result); ICollection <MediaItem> childDirectories = cd.BrowseAsync(_directoryId, DIRECTORY_MIA_ID_ENUMERATION, EMPTY_ID_ENUMERATION, userProfile, showVirtual).Result; subViewSpecifications = new List <ViewSpecification>(childDirectories.Count); foreach (MediaItem childDirectory in childDirectories) { SingleMediaItemAspect ma = null; MediaItemAspect.TryGetAspect(childDirectory.Aspects, MediaAspect.Metadata, out ma); IList <MultipleMediaItemAspect> pras = null; MediaItemAspect.TryGetAspects(childDirectory.Aspects, ProviderResourceAspect.Metadata, out pras); foreach (MultipleMediaItemAspect pra in pras) { MediaLibraryBrowseViewSpecification subViewSpecification = new MediaLibraryBrowseViewSpecification( (string)ma.GetAttributeValue(MediaAspect.ATTR_TITLE), childDirectory.MediaItemId, (string)pra.GetAttributeValue(ProviderResourceAspect.ATTR_SYSTEM_ID), ResourcePath.Deserialize((string)pra.GetAttributeValue(ProviderResourceAspect.ATTR_RESOURCE_ACCESSOR_PATH)), _necessaryMIATypeIds, _optionalMIATypeIds); subViewSpecifications.Add(subViewSpecification); } } } catch (UPnPRemoteException e) { ServiceRegistration.Get <ILogger>().Error("SimpleTextSearchViewSpecification.ReLoadItemsAndSubViewSpecifications: Error requesting server", e); mediaItems = null; subViewSpecifications = null; } }
public MockedDatabaseMovie(string imdbId, string tmdbId, string title, int year, int playPercentage) { IDictionary <Guid, IList <MediaItemAspect> > movieAspects = new Dictionary <Guid, IList <MediaItemAspect> >(); MultipleMediaItemAspect resourceAspect = new MultipleMediaItemAspect(ProviderResourceAspect.Metadata); resourceAspect.SetAttribute(ProviderResourceAspect.ATTR_RESOURCE_ACCESSOR_PATH, "c:\\" + title + ".mkv"); MediaItemAspect.AddOrUpdateAspect(movieAspects, resourceAspect); MediaItemAspect.AddOrUpdateExternalIdentifier(movieAspects, ExternalIdentifierAspect.SOURCE_IMDB, ExternalIdentifierAspect.TYPE_MOVIE, imdbId); MediaItemAspect.AddOrUpdateExternalIdentifier(movieAspects, ExternalIdentifierAspect.SOURCE_TMDB, ExternalIdentifierAspect.TYPE_MOVIE, tmdbId); MediaItemAspect.SetAttribute(movieAspects, MovieAspect.ATTR_MOVIE_NAME, title); SingleMediaItemAspect smia = new SingleMediaItemAspect(MediaAspect.Metadata); smia.SetAttribute(MediaAspect.ATTR_RECORDINGTIME, new DateTime(year, 1, 1)); MediaItemAspect.SetAspect(movieAspects, smia); IDictionary <string, string> userData = new Dictionary <string, string>(); userData.Add(UserDataKeysKnown.KEY_PLAY_PERCENTAGE, playPercentage.ToString()); Movie = new MediaItem(Guid.NewGuid(), movieAspects, userData); }
public MockedDatabaseEpisode(string tvDbId, int seasonIndex, List <int> episodeIndex, int playPercentage) { IDictionary <Guid, IList <MediaItemAspect> > episodeAspects = new Dictionary <Guid, IList <MediaItemAspect> >(); MultipleMediaItemAspect resourceAspect = new MultipleMediaItemAspect(ProviderResourceAspect.Metadata); resourceAspect.SetAttribute(ProviderResourceAspect.ATTR_RESOURCE_ACCESSOR_PATH, "c:\\" + tvDbId + ".mkv"); MediaItemAspect.AddOrUpdateAspect(episodeAspects, resourceAspect); MediaItemAspect.AddOrUpdateExternalIdentifier(episodeAspects, ExternalIdentifierAspect.SOURCE_TVDB, ExternalIdentifierAspect.TYPE_SERIES, tvDbId); MediaItemAspect.SetAttribute(episodeAspects, EpisodeAspect.ATTR_SEASON, seasonIndex); MediaItemAspect.SetCollectionAttribute(episodeAspects, EpisodeAspect.ATTR_EPISODE, episodeIndex); SingleMediaItemAspect smia = new SingleMediaItemAspect(MediaAspect.Metadata); MediaItemAspect.SetAspect(episodeAspects, smia); IDictionary <string, string> userData = new Dictionary <string, string>(); userData.Add(UserDataKeysKnown.KEY_PLAY_PERCENTAGE, playPercentage.ToString()); Episode = new MediaItem(Guid.NewGuid(), episodeAspects, userData); }
/// <summary> /// Constructs a dynamic <see cref="MediaItem"/> that contains the URL for the given <paramref name="stream"/>. /// </summary> /// <param name="stream">Stream.</param> private static MediaItem CreateStreamMediaItem(MyStream stream) { IDictionary <Guid, IList <MediaItemAspect> > aspects = new Dictionary <Guid, IList <MediaItemAspect> >(); MultipleMediaItemAspect providerResourceAspect = MediaItemAspect.CreateAspect(aspects, ProviderResourceAspect.Metadata); SingleMediaItemAspect mediaAspect = MediaItemAspect.GetOrCreateAspect(aspects, MediaAspect.Metadata); SingleMediaItemAspect audioAspect = MediaItemAspect.GetOrCreateAspect(aspects, AudioAspect.Metadata); providerResourceAspect.SetAttribute(ProviderResourceAspect.ATTR_TYPE, ProviderResourceAspect.TYPE_PRIMARY); providerResourceAspect.SetAttribute(ProviderResourceAspect.ATTR_RESOURCE_ACCESSOR_PATH, RawUrlResourceProvider.ToProviderResourcePath(stream.StreamUrls[0].StreamUrl).Serialize()); providerResourceAspect.SetAttribute(ProviderResourceAspect.ATTR_SYSTEM_ID, ServiceRegistration.Get <ISystemResolver>().LocalSystemId); providerResourceAspect.SetAttribute(ProviderResourceAspect.ATTR_MIME_TYPE, WEBRADIO_MIMETYPE); mediaAspect.SetAttribute(MediaAspect.ATTR_TITLE, stream.Title); MediaItemAspect.SetAttribute(aspects, ThumbnailLargeAspect.ATTR_THUMBNAIL, ImageFromLogo(stream.Logo)); var mediaItem = new MediaItem(Guid.Empty, aspects); return(mediaItem); }
public void TestAudioItem() { IList <IDirectoryObject> objects = new List <IDirectoryObject>(); BasicContainer root = new BasicContainer("TEST", new EndPointSettings { PreferredSubtitleLanguages = new string[] { "EN" }, PreferredAudioLanguages = new string[] { "EN" }, Profile = new EndPointProfile() }); Guid id = new Guid("11111111-aaaa-aaaa-aaaa-111111111111"); IDictionary <Guid, IList <MediaItemAspect> > aspects = new Dictionary <Guid, IList <MediaItemAspect> >(); SingleMediaItemAspect aspect1 = new SingleMediaItemAspect(MediaAspect.Metadata); aspect1.SetAttribute(MediaAspect.ATTR_TITLE, "The Track"); MediaItemAspect.SetAspect(aspects, aspect1); MultipleMediaItemAspect aspect2 = new MultipleMediaItemAspect(ProviderResourceAspect.Metadata); aspect2.SetAttribute(ProviderResourceAspect.ATTR_RESOURCE_ACCESSOR_PATH, "c:\\file.mp3"); MediaItemAspect.AddOrUpdateAspect(aspects, aspect2); SingleMediaItemAspect aspect3 = new SingleMediaItemAspect(AudioAspect.Metadata); MediaItemAspect.SetAspect(aspects, aspect3); MediaItem item = new MediaItem(id, aspects); objects.Add(MediaLibraryHelper.InstansiateMediaLibraryObject(item, root, "Test")); GenericDidlMessageBuilder builder = new GenericDidlMessageBuilder(); builder.BuildAll("*", objects); string xml = builder.ToString(); Console.WriteLine("XML: {0}", xml); }
public void TestExternalMediaItem() { MockDBUtils.Reset(); MockCore.SetupLibrary(); SingleTestMIA mia1 = TestCommonUtils.CreateSingleMIA("SINGLE1", Cardinality.Inline, true, true); MockCore.Management.AddMediaItemAspectStorage(mia1.Metadata); MockCore.Management.AddMediaItemAspectStorage(ProviderResourceAspect.Metadata); MockCore.Management.AddMediaItemAspectStorage(ImporterAspect.Metadata); MockCore.Management.AddMediaItemAspectStorage(ExternalIdentifierAspect.Metadata); MockCore.Management.AddMediaItemAspectStorage(MediaAspect.Metadata); IDictionary <Guid, IList <MediaItemAspect> > aspects = new Dictionary <Guid, IList <MediaItemAspect> >(); SingleMediaItemAspect aspect1 = new SingleMediaItemAspect(mia1.Metadata); aspect1.SetAttribute(mia1.ATTR_INTEGER, 1); aspect1.SetAttribute(mia1.ATTR_STRING, "one"); MediaItemAspect.SetAspect(aspects, aspect1); MediaItemAspect.AddOrUpdateExternalIdentifier(aspects, "test", ExternalIdentifierAspect.TYPE_EPISODE, "123"); MediaItemAspect.AddOrUpdateExternalIdentifier(aspects, "test", ExternalIdentifierAspect.TYPE_SERIES, "456"); MockDBUtils.AddReader(1, "SELECT MEDIA_ITEM_ID FROM M_PROVIDERRESOURCE WHERE SYSTEM_ID = @SYSTEM_ID AND PATH = @PATH", "MEDIA_ITEM_ID"); MockDBUtils.AddReader(2, "SELECT T5.MEDIA_ITEM_ID A23, T0.MEDIA_ITEM_ID A24, T1.MEDIA_ITEM_ID A25, T2.MEDIA_ITEM_ID A26, T3.MEDIA_ITEM_ID A27, T4.MEDIA_ITEM_ID A28, T0.ATTR_STRING A0, " + "T0.ATTR_INTEGER A1, T1.SYSTEM_ID A2, T1.RESOURCEINDEX A3, T1.ISPRIMARY A4, T1.MIMETYPE A5, T1.SIZE A6, T1.PATH A7, T1.PARENTDIRECTORY A8, T2.LASTIMPORTDATE A9, T2.DIRTY A10, T2.DATEADDED A11, " + "T3.SOURCE A12, T3.TYPE A13, T3.ID A14, T4.TITLE A15, T4.SORTTITLE A16, T4.RECORDINGTIME A17, T4.RATING A18, T4.COMMENT A19, T4.PLAYCOUNT A20, T4.LASTPLAYED A21, T4.ISVIRTUAL A22 FROM MEDIA_ITEMS T5 " + "LEFT OUTER JOIN M_SINGLE1 T0 ON T0.MEDIA_ITEM_ID = T5.MEDIA_ITEM_ID LEFT OUTER JOIN M_PROVIDERRESOURCE T1 ON T1.MEDIA_ITEM_ID = T5.MEDIA_ITEM_ID " + "LEFT OUTER JOIN M_IMPORTEDITEM T2 ON T2.MEDIA_ITEM_ID = T5.MEDIA_ITEM_ID LEFT OUTER JOIN M_EXTERNALIDENTIFIER T3 ON T3.MEDIA_ITEM_ID = T5.MEDIA_ITEM_ID " + "LEFT OUTER JOIN M_MEDIAITEM T4 ON T4.MEDIA_ITEM_ID = T5.MEDIA_ITEM_ID WHERE T5.MEDIA_ITEM_ID = @V0", "@V0"); string pathStr = "c:\\item.mp3"; ResourcePath path = LocalFsResourceProviderBase.ToResourcePath(pathStr); MockCore.Library.AddOrUpdateMediaItem(Guid.Empty, null, path, aspects.Values.SelectMany(x => x), false); MockCore.ShutdownLibrary(); }
public MockedDatabaseMovie(MediaLibraryMovie movie) { IDictionary <Guid, IList <MediaItemAspect> > movieAspects = new Dictionary <Guid, IList <MediaItemAspect> >(); MultipleMediaItemAspect resourceAspect = new MultipleMediaItemAspect(ProviderResourceAspect.Metadata); resourceAspect.SetAttribute(ProviderResourceAspect.ATTR_RESOURCE_ACCESSOR_PATH, "c:\\" + movie.Title + ".mkv"); MediaItemAspect.AddOrUpdateAspect(movieAspects, resourceAspect); MediaItemAspect.AddOrUpdateExternalIdentifier(movieAspects, ExternalIdentifierAspect.SOURCE_IMDB, ExternalIdentifierAspect.TYPE_MOVIE, movie.Imdb); MediaItemAspect.SetAttribute(movieAspects, MovieAspect.ATTR_MOVIE_NAME, movie.Title); SingleMediaItemAspect mediaItemAspect = new SingleMediaItemAspect(MediaAspect.Metadata); mediaItemAspect.SetAttribute(MediaAspect.ATTR_PLAYCOUNT, movie.PlayCount); mediaItemAspect.SetAttribute(MediaAspect.ATTR_LASTPLAYED, DateTime.Parse(movie.LastPlayed)); MediaItemAspect.SetAspect(movieAspects, mediaItemAspect); SingleMediaItemAspect importerAspect = new SingleMediaItemAspect(ImporterAspect.Metadata); importerAspect.SetAttribute(ImporterAspect.ATTR_DATEADDED, DateTime.Parse(movie.AddedToDb)); MediaItemAspect.SetAspect(movieAspects, importerAspect); Movie = new MediaItem(Guid.NewGuid(), movieAspects); }
public void TestMediaItemLoader_SingleMIAs_LikeFilter() { MockDBUtils.Reset(); SingleTestMIA mia1 = TestBackendUtils.CreateSingleMIA("SINGLE1", Cardinality.Inline, true, false); SingleTestMIA mia2 = TestBackendUtils.CreateSingleMIA("SINGLE2", Cardinality.Inline, false, true); SingleTestMIA mia3 = TestBackendUtils.CreateSingleMIA("SINGLE3", Cardinality.Inline, true, true); Guid itemId = new Guid("aaaaaaaa-1111-1111-1111-aaaaaaaaaaaa"); IFilter filter = new LikeFilter(mia1.ATTR_STRING, "%", null); MockReader reader = MockDBUtils.AddReader( "SELECT T0.MEDIA_ITEM_ID A4, T0.MEDIA_ITEM_ID A5, T1.MEDIA_ITEM_ID A6, T2.MEDIA_ITEM_ID A7, T0.ATTR_STRING A0, T1.ATTR_INTEGER A1, T2.ATTR_STRING A2, T2.ATTR_INTEGER A3 " + "FROM M_SINGLE1 T0 INNER JOIN M_SINGLE2 T1 ON T1.MEDIA_ITEM_ID = T0.MEDIA_ITEM_ID LEFT OUTER JOIN M_SINGLE3 T2 ON T2.MEDIA_ITEM_ID = T0.MEDIA_ITEM_ID " + " WHERE T0.ATTR_STRING LIKE @V0", "A4", "A5", "A6", "A7", "A0", "A1", "A2", "A3"); reader.AddResult(itemId, itemId, itemId, itemId, "zerozero", 11, "twotwo", 33); Guid[] requiredAspects = new Guid[] { mia1.ASPECT_ID, mia2.ASPECT_ID }; Guid[] optionalAspects = new Guid[] { mia3.ASPECT_ID }; MediaItemQuery query = new MediaItemQuery(requiredAspects, optionalAspects, filter); CompiledMediaItemQuery compiledQuery = CompiledMediaItemQuery.Compile(MockCore.Management, query); MediaItem result = compiledQuery.QueryMediaItem(); //Console.WriteLine("Query result " + result.MediaItemId + ": " + string.Join(",", result.Aspects) + ": " + result); Assert.AreEqual(itemId, result.MediaItemId, "MediaItem ID"); SingleMediaItemAspect value = null; Assert.IsTrue(MediaItemAspect.TryGetAspect(result.Aspects, mia1.Metadata, out value), "MIA1"); Assert.AreEqual("zerozero", value.GetAttributeValue(mia1.ATTR_STRING), "MIA1 string attibute"); Assert.IsTrue(MediaItemAspect.TryGetAspect(result.Aspects, mia2.Metadata, out value), "MIA2"); Assert.AreEqual(11, value.GetAttributeValue(mia2.ATTR_INTEGER), "MIA2 integer attibute"); Assert.IsTrue(MediaItemAspect.TryGetAspect(result.Aspects, mia3.Metadata, out value), "MIA3"); Assert.AreEqual("twotwo", value.GetAttributeValue(mia3.ATTR_STRING), "MIA3 string attibute"); Assert.AreEqual(33, value.GetAttributeValue(mia3.ATTR_INTEGER), "MIA3 integer attibute"); }
public void TestMediaItemLoader_SingleMIAsUnusedOptional_IdFilter() { MockDBUtils.Reset(); SingleTestMIA single1 = TestBackendUtils.CreateSingleMIA("SINGLE1", Cardinality.Inline, true, false); SingleTestMIA single2 = TestBackendUtils.CreateSingleMIA("SINGLE2", Cardinality.Inline, false, true); SingleTestMIA single3 = TestBackendUtils.CreateSingleMIA("SINGLE3", Cardinality.Inline, false, true); SingleTestMIA single4 = TestBackendUtils.CreateSingleMIA("SINGLE4", Cardinality.Inline, false, true); Guid itemId = new Guid("aaaaaaaa-1111-1111-1111-aaaaaaaaaaaa"); IList <Guid> ids = new List <Guid>(); ids.Add(itemId); IFilter filter = new MediaItemIdFilter(ids); MockReader reader = MockDBUtils.AddReader( "SELECT T0.MEDIA_ITEM_ID A4, T0.MEDIA_ITEM_ID A5, T1.MEDIA_ITEM_ID A6, T2.MEDIA_ITEM_ID A7, T3.MEDIA_ITEM_ID A8, T0.ATTR_STRING A0, T1.ATTR_INTEGER A1, T2.ATTR_INTEGER A2, " + "T3.ATTR_INTEGER A3 FROM M_SINGLE1 T0 INNER JOIN M_SINGLE2 T1 ON T1.MEDIA_ITEM_ID = T0.MEDIA_ITEM_ID LEFT OUTER JOIN M_SINGLE3 T2 ON T2.MEDIA_ITEM_ID = T0.MEDIA_ITEM_ID " + "LEFT OUTER JOIN M_SINGLE4 T3 ON T3.MEDIA_ITEM_ID = T0.MEDIA_ITEM_ID WHERE T0.MEDIA_ITEM_ID = @V0", "A4", "A5", "A6", "A7", "A8", "A0", "A1", "A2", "A3"); reader.AddResult(itemId, itemId, itemId, itemId, null, "zero", 0, 0, null); Guid[] requiredAspects = new Guid[] { single1.ASPECT_ID, single2.ASPECT_ID }; Guid[] optionalAspects = new Guid[] { single3.ASPECT_ID, single4.ASPECT_ID }; MediaItemQuery query = new MediaItemQuery(requiredAspects, optionalAspects, filter); CompiledMediaItemQuery compiledQuery = CompiledMediaItemQuery.Compile(MockCore.Management, query); MediaItem result = compiledQuery.QueryMediaItem(); Assert.AreEqual(itemId, result.MediaItemId, "MediaItem ID"); SingleMediaItemAspect value = null; Assert.IsTrue(MediaItemAspect.TryGetAspect(result.Aspects, single1.Metadata, out value), "MIA1"); Assert.IsTrue(MediaItemAspect.TryGetAspect(result.Aspects, single2.Metadata, out value), "MIA2"); Assert.IsTrue(MediaItemAspect.TryGetAspect(result.Aspects, single3.Metadata, out value), "MIA3"); Assert.IsFalse(MediaItemAspect.TryGetAspect(result.Aspects, single4.Metadata, out value), "MIA4"); }
public bool TryExtractMetadata(IResourceAccessor mediaItemAccessor, IDictionary <Guid, IList <MediaItemAspect> > extractedAspectData, bool importOnly, bool forceQuickMode) { try { if (!(mediaItemAccessor is IFileSystemResourceAccessor)) { return(false); } if (extractedAspectData.ContainsKey(VideoAspect.ASPECT_ID)) { return(false); } using (LocalFsResourceAccessorHelper rah = new LocalFsResourceAccessorHelper(mediaItemAccessor)) { if (!rah.LocalFsResourceAccessor.IsFile && rah.LocalFsResourceAccessor.ResourceExists("BDMV")) { using (IFileSystemResourceAccessor fsraBDMV = rah.LocalFsResourceAccessor.GetResource("BDMV")) if (fsraBDMV != null && fsraBDMV.ResourceExists("index.bdmv")) { MultipleMediaItemAspect providerResourceAspect = MediaItemAspect.CreateAspect(extractedAspectData, ProviderResourceAspect.Metadata); // Calling EnsureLocalFileSystemAccess not necessary; only string operation providerResourceAspect.SetAttribute(ProviderResourceAspect.ATTR_RESOURCE_INDEX, 0); providerResourceAspect.SetAttribute(ProviderResourceAspect.ATTR_PRIMARY, true); providerResourceAspect.SetAttribute(ProviderResourceAspect.ATTR_MIME_TYPE, "video/bluray"); // BluRay disc providerResourceAspect.SetAttribute(ProviderResourceAspect.ATTR_RESOURCE_ACCESSOR_PATH, mediaItemAccessor.CanonicalLocalResourcePath.Serialize()); // This line is important to keep in, if no VideoAspect is created here, the MediaItems is not detected as Video! SingleMediaItemAspect videoAspect = MediaItemAspect.GetOrCreateAspect(extractedAspectData, VideoAspect.Metadata); videoAspect.SetAttribute(VideoAspect.ATTR_ISDVD, true); MultipleMediaItemAspect videoStreamAspect = MediaItemAspect.CreateAspect(extractedAspectData, VideoStreamAspect.Metadata); videoStreamAspect.SetAttribute(VideoStreamAspect.ATTR_RESOURCE_INDEX, 0); videoStreamAspect.SetAttribute(VideoStreamAspect.ATTR_STREAM_INDEX, -1); MediaItemAspect mediaAspect = MediaItemAspect.GetOrCreateAspect(extractedAspectData, MediaAspect.Metadata); mediaAspect.SetAttribute(MediaAspect.ATTR_ISVIRTUAL, false); using (rah.LocalFsResourceAccessor.EnsureLocalFileSystemAccess()) { BDInfoExt bdinfo = new BDInfoExt(rah.LocalFsResourceAccessor.LocalFileSystemPath); string title = bdinfo.GetTitle(); mediaAspect.SetAttribute(MediaAspect.ATTR_TITLE, title ?? mediaItemAccessor.ResourceName); // Check for BD disc thumbs FileInfo thumbnail = bdinfo.GetBiggestThumb(); if (thumbnail != null) { try { using (FileStream fileStream = new FileStream(thumbnail.FullName, FileMode.Open, FileAccess.Read)) using (MemoryStream resized = (MemoryStream)ImageUtilities.ResizeImage(fileStream, ImageFormat.Jpeg, MAX_COVER_WIDTH, MAX_COVER_HEIGHT)) { MediaItemAspect.SetAttribute(extractedAspectData, ThumbnailLargeAspect.ATTR_THUMBNAIL, resized.ToArray()); } } // Decoding of invalid image data can fail, but main MediaItem is correct. catch { } } } return(true); } } } return(false); } catch { // Only log at the info level here - And simply return false. This makes the importer know that we // couldn't perform our task here if (mediaItemAccessor != null) { ServiceRegistration.Get <ILogger>().Info("BluRayMetadataExtractor: Exception reading source '{0}'", mediaItemAccessor.ResourcePathName); } return(false); } }
public void TestAddMediaItem() { MockDBUtils.Reset(); MockCore.SetupLibrary(); SingleTestMIA mia1 = TestCommonUtils.CreateSingleMIA("SINGLE1", Cardinality.Inline, true, true); MultipleTestMIA mia2 = TestCommonUtils.CreateMultipleMIA("MULTIPLE2", Cardinality.Inline, true, false); MultipleTestMIA mia3 = TestCommonUtils.CreateMultipleMIA("MULTIPLE3", Cardinality.Inline, false, true); MockCore.Management.AddMediaItemAspectStorage(mia1.Metadata); MockCore.Management.AddMediaItemAspectStorage(mia2.Metadata); MockCore.Management.AddMediaItemAspectStorage(mia3.Metadata); MockCore.Management.AddMediaItemAspectStorage(ProviderResourceAspect.Metadata); MockCore.Management.AddMediaItemAspectStorage(ImporterAspect.Metadata); MockCore.Management.AddMediaItemAspectStorage(MediaAspect.Metadata); IList <MediaItemAspect> aspects = new List <MediaItemAspect>(); SingleMediaItemAspect aspect1 = new SingleMediaItemAspect(mia1.Metadata); aspect1.SetAttribute(mia1.ATTR_INTEGER, 1); aspect1.SetAttribute(mia1.ATTR_STRING, "one"); aspects.Add(aspect1); MultipleMediaItemAspect aspect2_1 = new MultipleMediaItemAspect(mia2.Metadata); aspect2_1.SetAttribute(mia2.ATTR_STRING, "two.one"); aspects.Add(aspect2_1); MultipleMediaItemAspect aspect2_2 = new MultipleMediaItemAspect(mia2.Metadata); aspect2_2.SetAttribute(mia2.ATTR_STRING, "two.two"); aspects.Add(aspect2_2); MultipleMediaItemAspect aspect3_1 = new MultipleMediaItemAspect(mia3.Metadata); aspect3_1.SetAttribute(mia3.ATTR_INTEGER, 31); aspects.Add(aspect3_1); MultipleMediaItemAspect aspect3_2 = new MultipleMediaItemAspect(mia3.Metadata); aspect3_2.SetAttribute(mia3.ATTR_INTEGER, 32); aspects.Add(aspect3_2); MultipleMediaItemAspect aspect3_3 = new MultipleMediaItemAspect(mia3.Metadata); aspect3_3.SetAttribute(mia3.ATTR_INTEGER, 33); aspects.Add(aspect3_3); MockDBUtils.AddReader(1, "SELECT MEDIA_ITEM_ID FROM M_PROVIDERRESOURCE WHERE SYSTEM_ID = @SYSTEM_ID AND PATH = @PATH", "MEDIA_ITEM_ID"); MockDBUtils.AddReader(2, "SELECT T6.MEDIA_ITEM_ID A24, T0.MEDIA_ITEM_ID A25, T1.MEDIA_ITEM_ID A26, T2.MEDIA_ITEM_ID A27, T3.MEDIA_ITEM_ID A28, " + "T4.MEDIA_ITEM_ID A29, T5.MEDIA_ITEM_ID A30, T0.ATTR_STRING A0, T0.ATTR_INTEGER A1, T1.ATTR_ID A2, T1.ATTR_STRING A3, T2.ATTR_ID A4, T2.ATTR_INTEGER A5, " + "T3.SYSTEM_ID A6, T3.RESOURCEINDEX A7, T3.ISPRIMARY A8, T3.MIMETYPE A9, T3.SIZE A10, T3.PATH A11, T3.PARENTDIRECTORY A12, T4.LASTIMPORTDATE A13, T4.DIRTY A14, " + "T4.DATEADDED A15, T5.TITLE A16, T5.SORTTITLE A17, T5.RECORDINGTIME A18, T5.RATING A19, T5.COMMENT A20, T5.PLAYCOUNT A21, T5.LASTPLAYED A22, T5.ISVIRTUAL A23 " + "FROM MEDIA_ITEMS T6 LEFT OUTER JOIN M_SINGLE1 T0 ON T0.MEDIA_ITEM_ID = T6.MEDIA_ITEM_ID LEFT OUTER JOIN M_MULTIPLE2 T1 ON T1.MEDIA_ITEM_ID = T6.MEDIA_ITEM_ID " + "LEFT OUTER JOIN M_MULTIPLE3 T2 ON T2.MEDIA_ITEM_ID = T6.MEDIA_ITEM_ID LEFT OUTER JOIN M_PROVIDERRESOURCE T3 ON T3.MEDIA_ITEM_ID = T6.MEDIA_ITEM_ID " + "LEFT OUTER JOIN M_IMPORTEDITEM T4 ON T4.MEDIA_ITEM_ID = T6.MEDIA_ITEM_ID LEFT OUTER JOIN M_MEDIAITEM T5 ON T5.MEDIA_ITEM_ID = T6.MEDIA_ITEM_ID WHERE T6.MEDIA_ITEM_ID = @V0", "V0"); string pathStr = "c:\\item.mp3"; ResourcePath path = LocalFsResourceProviderBase.ToResourcePath(pathStr); MockCore.Library.AddOrUpdateMediaItem(Guid.Empty, null, path, aspects, false); MockCore.ShutdownLibrary(); }
private async Task <bool> TryExtractStubItemsAsync(IResourceAccessor mediaItemAccessor, ICollection <IDictionary <Guid, IList <MediaItemAspect> > > extractedStubAspectData) { // Get a unique number for this call to TryExtractMetadataAsync. We use this to make reading the debug log easier. // This MetadataExtractor is called in parallel for multiple MediaItems so that the respective debug log entries // for one call are not contained one after another in debug log. We therefore prepend this number before every log entry. var miNumber = Interlocked.Increment(ref _lastMediaItemNumber); try { _debugLogger.Info("[#{0}]: Start extracting stubs for resource '{1}'", miNumber, mediaItemAccessor); if (!IsStubResource(mediaItemAccessor)) { _debugLogger.Info("[#{0}]: Cannot extract stubs; file does not have a supported extension", miNumber); return(false); } // This MetadataExtractor only works for MediaItems accessible by an IFileSystemResourceAccessor. // Otherwise it is not possible to find a stub-file in the MediaItem's directory. if (!(mediaItemAccessor is IFileSystemResourceAccessor)) { _debugLogger.Info("[#{0}]: Cannot extract stubs; mediaItemAccessor is not an IFileSystemResourceAccessor", miNumber); return(false); } var fsra = mediaItemAccessor as IFileSystemResourceAccessor; var albumStubReader = new StubAlbumReader(_debugLogger, miNumber, true, _settings); if (fsra != null && await albumStubReader.TryReadMetadataAsync(fsra).ConfigureAwait(false)) { AlbumStub album = albumStubReader.GetAlbumStubs().FirstOrDefault(); if (album != null && album.Tracks != null && album.Tracks > 0) { for (int trackNo = 1; trackNo <= album.Tracks.Value; trackNo++) { Dictionary <Guid, IList <MediaItemAspect> > extractedAspectData = new Dictionary <Guid, IList <MediaItemAspect> >(); string title = string.Format("{0}: {1}", album.Title, "Track " + trackNo); MultipleMediaItemAspect providerResourceAspect = MediaItemAspect.CreateAspect(extractedAspectData, ProviderResourceAspect.Metadata); providerResourceAspect.SetAttribute(ProviderResourceAspect.ATTR_RESOURCE_INDEX, 0); providerResourceAspect.SetAttribute(ProviderResourceAspect.ATTR_TYPE, ProviderResourceAspect.TYPE_STUB); providerResourceAspect.SetAttribute(ProviderResourceAspect.ATTR_RESOURCE_ACCESSOR_PATH, fsra.CanonicalLocalResourcePath.Serialize()); providerResourceAspect.SetAttribute(ProviderResourceAspect.ATTR_MIME_TYPE, "audio/L16"); SingleMediaItemAspect audioAspect = MediaItemAspect.GetOrCreateAspect(extractedAspectData, AudioAspect.Metadata); audioAspect.SetAttribute(AudioAspect.ATTR_ISCD, true); audioAspect.SetAttribute(AudioAspect.ATTR_TRACK, trackNo); audioAspect.SetAttribute(AudioAspect.ATTR_TRACKNAME, title); audioAspect.SetAttribute(AudioAspect.ATTR_ENCODING, "PCM"); if (album.Cd.HasValue) { audioAspect.SetAttribute(AudioAspect.ATTR_DISCID, album.Cd.Value); } audioAspect.SetAttribute(AudioAspect.ATTR_BITRATE, 1411); // 44.1 kHz * 16 bit * 2 channel audioAspect.SetAttribute(AudioAspect.ATTR_CHANNELS, 2); audioAspect.SetAttribute(AudioAspect.ATTR_NUMTRACKS, album.Tracks.Value); audioAspect.SetAttribute(AudioAspect.ATTR_ALBUM, album.Title); if (album.Artists.Count > 0) { audioAspect.SetCollectionAttribute(AudioAspect.ATTR_ALBUMARTISTS, album.Artists); } SingleMediaItemAspect stubAspect = MediaItemAspect.GetOrCreateAspect(extractedAspectData, StubAspect.Metadata); stubAspect.SetAttribute(StubAspect.ATTR_DISC_NAME, album.DiscName); stubAspect.SetAttribute(StubAspect.ATTR_MESSAGE, album.Message); MediaItemAspect.SetAttribute(extractedAspectData, MediaAspect.ATTR_TITLE, title); MediaItemAspect.SetAttribute(extractedAspectData, MediaAspect.ATTR_SORT_TITLE, BaseInfo.GetSortTitle(title)); MediaItemAspect.SetAttribute(extractedAspectData, MediaAspect.ATTR_ISVIRTUAL, false); MediaItemAspect.SetAttribute(extractedAspectData, MediaAspect.ATTR_ISSTUB, true); MediaItemAspect.SetAttribute(extractedAspectData, MediaAspect.ATTR_RECORDINGTIME, fsra.LastChanged); extractedStubAspectData.Add(extractedAspectData); } } } else { _debugLogger.Warn("[#{0}]: No valid metadata found in album stub file", miNumber); } _debugLogger.Info("[#{0}]: Successfully finished extracting stubs", miNumber); return(true); } catch (Exception e) { ServiceRegistration.Get <ILogger>().Warn("StubAudioMetadataExtractor: Exception while extracting stubs for resource '{0}'; enable debug logging for more details.", mediaItemAccessor); _debugLogger.Error("[#{0}]: Exception while extracting stubs", e, miNumber); return(false); } }
private async Task <bool> TryExtractStubItemsAsync(IResourceAccessor mediaItemAccessor, ICollection <IDictionary <Guid, IList <MediaItemAspect> > > extractedStubAspectData) { // Get a unique number for this call to TryExtractMetadataAsync. We use this to make reading the debug log easier. // This MetadataExtractor is called in parallel for multiple MediaItems so that the respective debug log entries // for one call are not contained one after another in debug log. We therefore prepend this number before every log entry. var miNumber = Interlocked.Increment(ref _lastMediaItemNumber); try { _debugLogger.Info("[#{0}]: Start extracting stubs for resource '{1}'", miNumber, mediaItemAccessor); if (!IsStubResource(mediaItemAccessor)) { _debugLogger.Info("[#{0}]: Cannot extract stubs; file does not have a supported extension", miNumber); return(false); } // This MetadataExtractor only works for MediaItems accessible by an IFileSystemResourceAccessor. // Otherwise it is not possible to find a stub-file in the MediaItem's directory. if (!(mediaItemAccessor is IFileSystemResourceAccessor)) { _debugLogger.Info("[#{0}]: Cannot extract stubs; mediaItemAccessor is not an IFileSystemResourceAccessor", miNumber); return(false); } var fsra = mediaItemAccessor as IFileSystemResourceAccessor; var seriesStubReader = new StubSeriesReader(_debugLogger, miNumber, true, _settings); if (fsra != null && await seriesStubReader.TryReadMetadataAsync(fsra).ConfigureAwait(false)) { SeriesStub series = seriesStubReader.GetSeriesStubs().FirstOrDefault(); if (series != null && series.Episodes != null && series.Episodes.Count > 0) { Dictionary <Guid, IList <MediaItemAspect> > extractedAspectData = new Dictionary <Guid, IList <MediaItemAspect> >(); string title = string.Format("{0} S{1:00}{2}", series.Title, series.Season.Value, string.Join("", series.Episodes.Select(e => "E" + e.ToString("00")))); MultipleMediaItemAspect providerResourceAspect = MediaItemAspect.CreateAspect(extractedAspectData, ProviderResourceAspect.Metadata); providerResourceAspect.SetAttribute(ProviderResourceAspect.ATTR_RESOURCE_INDEX, 0); providerResourceAspect.SetAttribute(ProviderResourceAspect.ATTR_TYPE, ProviderResourceAspect.TYPE_STUB); providerResourceAspect.SetAttribute(ProviderResourceAspect.ATTR_RESOURCE_ACCESSOR_PATH, fsra.CanonicalLocalResourcePath.Serialize()); if (IsVhs(mediaItemAccessor)) { providerResourceAspect.SetAttribute(ProviderResourceAspect.ATTR_MIME_TYPE, "video/unknown"); MultipleMediaItemAspect videoStreamAspects = MediaItemAspect.CreateAspect(extractedAspectData, VideoStreamAspect.Metadata); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_RESOURCE_INDEX, 0); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_STREAM_INDEX, 0); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_VIDEO_TYPE, VideoStreamAspect.TYPE_SD); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_ASPECTRATIO, Convert.ToSingle(4.0 / 3.0)); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_FPS, 25); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_WIDTH, 720); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_HEIGHT, 576); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_AUDIOSTREAMCOUNT, 1); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_VIDEO_PART, 1); MultipleMediaItemAspect audioAspect = MediaItemAspect.CreateAspect(extractedAspectData, VideoAudioStreamAspect.Metadata); audioAspect.SetAttribute(VideoAudioStreamAspect.ATTR_RESOURCE_INDEX, 0); audioAspect.SetAttribute(VideoAudioStreamAspect.ATTR_STREAM_INDEX, 1); audioAspect.SetAttribute(VideoAudioStreamAspect.ATTR_AUDIOCHANNELS, 2); } else if (IsTv(mediaItemAccessor)) { providerResourceAspect.SetAttribute(ProviderResourceAspect.ATTR_MIME_TYPE, "video/unknown"); MultipleMediaItemAspect videoStreamAspects = MediaItemAspect.CreateAspect(extractedAspectData, VideoStreamAspect.Metadata); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_RESOURCE_INDEX, 0); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_STREAM_INDEX, 0); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_VIDEO_TYPE, VideoStreamAspect.TYPE_HD); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_ASPECTRATIO, Convert.ToSingle(16.0 / 9.0)); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_FPS, 25F); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_WIDTH, 1920); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_HEIGHT, 1080); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_AUDIOSTREAMCOUNT, 1); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_VIDEO_PART, 1); MultipleMediaItemAspect audioAspect = MediaItemAspect.CreateAspect(extractedAspectData, VideoAudioStreamAspect.Metadata); audioAspect.SetAttribute(VideoAudioStreamAspect.ATTR_RESOURCE_INDEX, 0); audioAspect.SetAttribute(VideoAudioStreamAspect.ATTR_STREAM_INDEX, 1); audioAspect.SetAttribute(VideoAudioStreamAspect.ATTR_AUDIOCHANNELS, 2); } else if (IsDvd(mediaItemAccessor)) { providerResourceAspect.SetAttribute(ProviderResourceAspect.ATTR_MIME_TYPE, "video/mp2t"); MultipleMediaItemAspect videoStreamAspects = MediaItemAspect.CreateAspect(extractedAspectData, VideoStreamAspect.Metadata); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_RESOURCE_INDEX, 0); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_STREAM_INDEX, 0); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_VIDEO_TYPE, VideoStreamAspect.TYPE_SD); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_ASPECTRATIO, Convert.ToSingle(16.0 / 9.0)); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_FPS, 25F); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_WIDTH, 720); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_HEIGHT, 576); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_VIDEOENCODING, "MPEG-2 Video"); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_AUDIOSTREAMCOUNT, 1); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_VIDEO_PART, 1); MultipleMediaItemAspect audioAspect = MediaItemAspect.CreateAspect(extractedAspectData, VideoAudioStreamAspect.Metadata); audioAspect.SetAttribute(VideoAudioStreamAspect.ATTR_RESOURCE_INDEX, 0); audioAspect.SetAttribute(VideoAudioStreamAspect.ATTR_STREAM_INDEX, 1); audioAspect.SetAttribute(VideoAudioStreamAspect.ATTR_AUDIOENCODING, "AC3"); audioAspect.SetAttribute(VideoAudioStreamAspect.ATTR_AUDIOCHANNELS, 6); audioAspect.SetAttribute(VideoAudioStreamAspect.ATTR_AUDIOSAMPLERATE, 48000L); } else if (IsBluray(mediaItemAccessor)) { providerResourceAspect.SetAttribute(ProviderResourceAspect.ATTR_MIME_TYPE, "video/mp4"); MultipleMediaItemAspect videoStreamAspects = MediaItemAspect.CreateAspect(extractedAspectData, VideoStreamAspect.Metadata); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_RESOURCE_INDEX, 0); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_STREAM_INDEX, 0); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_VIDEO_TYPE, VideoStreamAspect.TYPE_HD); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_ASPECTRATIO, Convert.ToSingle(16.0 / 9.0)); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_FPS, 24F); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_WIDTH, 1920); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_HEIGHT, 1080); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_VIDEOENCODING, "AVC"); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_AUDIOSTREAMCOUNT, 1); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_VIDEO_PART, 1); MultipleMediaItemAspect audioAspect = MediaItemAspect.CreateAspect(extractedAspectData, VideoAudioStreamAspect.Metadata); audioAspect.SetAttribute(VideoAudioStreamAspect.ATTR_RESOURCE_INDEX, 0); audioAspect.SetAttribute(VideoAudioStreamAspect.ATTR_STREAM_INDEX, 1); audioAspect.SetAttribute(VideoAudioStreamAspect.ATTR_AUDIOENCODING, "AC3"); audioAspect.SetAttribute(VideoAudioStreamAspect.ATTR_AUDIOCHANNELS, 6); audioAspect.SetAttribute(VideoAudioStreamAspect.ATTR_AUDIOSAMPLERATE, 48000L); } else if (IsHdDvd(mediaItemAccessor)) { providerResourceAspect.SetAttribute(ProviderResourceAspect.ATTR_MIME_TYPE, "video/wvc1"); MultipleMediaItemAspect videoStreamAspects = MediaItemAspect.CreateAspect(extractedAspectData, VideoStreamAspect.Metadata); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_RESOURCE_INDEX, 0); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_STREAM_INDEX, 0); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_VIDEO_TYPE, VideoStreamAspect.TYPE_HD); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_ASPECTRATIO, Convert.ToSingle(16.0 / 9.0)); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_FPS, 24F); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_WIDTH, 1920); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_HEIGHT, 1080); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_VIDEOENCODING, "VC1"); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_AUDIOSTREAMCOUNT, 1); videoStreamAspects.SetAttribute(VideoStreamAspect.ATTR_VIDEO_PART, 1); MultipleMediaItemAspect audioAspect = MediaItemAspect.CreateAspect(extractedAspectData, VideoAudioStreamAspect.Metadata); audioAspect.SetAttribute(VideoAudioStreamAspect.ATTR_RESOURCE_INDEX, 0); audioAspect.SetAttribute(VideoAudioStreamAspect.ATTR_STREAM_INDEX, 1); audioAspect.SetAttribute(VideoAudioStreamAspect.ATTR_AUDIOENCODING, "AC3"); audioAspect.SetAttribute(VideoAudioStreamAspect.ATTR_AUDIOCHANNELS, 6); audioAspect.SetAttribute(VideoAudioStreamAspect.ATTR_AUDIOSAMPLERATE, 48000L); } SingleMediaItemAspect videoAspect = MediaItemAspect.GetOrCreateAspect(extractedAspectData, VideoAspect.Metadata); videoAspect.SetAttribute(VideoAspect.ATTR_ISDVD, true); SingleMediaItemAspect movieAspect = MediaItemAspect.GetOrCreateAspect(extractedAspectData, EpisodeAspect.Metadata); movieAspect.SetCollectionAttribute(EpisodeAspect.ATTR_EPISODE, series.Episodes); movieAspect.SetAttribute(EpisodeAspect.ATTR_SEASON, series.Season.Value); movieAspect.SetAttribute(EpisodeAspect.ATTR_EPISODE_NAME, string.Format("{0} {1}", "Episode", string.Join(", ", series.Episodes))); movieAspect.SetAttribute(EpisodeAspect.ATTR_SERIES_NAME, series.Title); movieAspect.SetAttribute(EpisodeAspect.ATTR_SERIES_SEASON, string.Format("{0} S{1:00}", series.Title, series.Season.Value)); SingleMediaItemAspect stubAspect = MediaItemAspect.GetOrCreateAspect(extractedAspectData, StubAspect.Metadata); stubAspect.SetAttribute(StubAspect.ATTR_DISC_NAME, series.DiscName); stubAspect.SetAttribute(StubAspect.ATTR_MESSAGE, series.Message); MediaItemAspect.SetAttribute(extractedAspectData, MediaAspect.ATTR_TITLE, series.Title); MediaItemAspect.SetAttribute(extractedAspectData, MediaAspect.ATTR_SORT_TITLE, BaseInfo.GetSortTitle(series.Title)); MediaItemAspect.SetAttribute(extractedAspectData, MediaAspect.ATTR_ISVIRTUAL, false); MediaItemAspect.SetAttribute(extractedAspectData, MediaAspect.ATTR_ISSTUB, true); MediaItemAspect.SetAttribute(extractedAspectData, MediaAspect.ATTR_RECORDINGTIME, fsra.LastChanged); extractedStubAspectData.Add(extractedAspectData); } } else { _debugLogger.Warn("[#{0}]: No valid metadata found in movie stub file", miNumber); } _debugLogger.Info("[#{0}]: Successfully finished extracting stubs", miNumber); return(true); } catch (Exception e) { ServiceRegistration.Get <ILogger>().Warn("StubMovieMetadataExtractor: Exception while extracting stubs for resource '{0}'; enable debug logging for more details.", mediaItemAccessor); _debugLogger.Error("[#{0}]: Exception while extracting stubs", e, miNumber); return(false); } }
public void TestEditBigMediaItem() { MockDBUtils.Reset(); MockCore.SetupLibrary(); SingleTestMIA mia1 = TestCommonUtils.CreateSingleMIA("SINGLE1", Cardinality.Inline, true, true); MockCore.Management.AddMediaItemAspectStorage(mia1.Metadata); MultipleTestMIA mia2 = TestCommonUtils.CreateMultipleMIA("MULTIPLE2", Cardinality.Inline, true, false); MockCore.Management.AddMediaItemAspectStorage(mia2.Metadata); MultipleTestMIA mia3 = TestCommonUtils.CreateMultipleMIA("MULTIPLE3", Cardinality.Inline, false, true); MockCore.Management.AddMediaItemAspectStorage(mia3.Metadata); SingleTestMIA mia4 = TestCommonUtils.CreateSingleMIA("SINGLE4", Cardinality.Inline, true, true); MockCore.Management.AddMediaItemAspectStorage(mia4.Metadata); MockCore.Management.AddMediaItemAspectStorage(ProviderResourceAspect.Metadata); MockCore.Management.AddMediaItemAspectStorage(ImporterAspect.Metadata); MockCore.Management.AddMediaItemAspectStorage(MediaAspect.Metadata); IList <MediaItemAspect> aspects = new List <MediaItemAspect>(); SingleMediaItemAspect aspect1 = new SingleMediaItemAspect(mia1.Metadata); aspect1.SetAttribute(mia1.ATTR_INTEGER, 1); aspect1.SetAttribute(mia1.ATTR_STRING, "one"); aspects.Add(aspect1); MultipleMediaItemAspect aspect2_1 = new MultipleMediaItemAspect(mia2.Metadata); aspect2_1.SetAttribute(mia2.ATTR_ID, "2_1"); aspect2_1.SetAttribute(mia2.ATTR_STRING, "two.one"); aspects.Add(aspect2_1); MultipleMediaItemAspect aspect2_2 = new MultipleMediaItemAspect(mia2.Metadata); aspect2_2.SetAttribute(mia2.ATTR_ID, "2_2"); aspect2_2.SetAttribute(mia2.ATTR_STRING, "two.two"); aspects.Add(aspect2_2); MultipleMediaItemAspect aspect3_1 = new MultipleMediaItemAspect(mia3.Metadata); aspect3_1.SetAttribute(mia3.ATTR_ID, "3_1"); aspect3_1.SetAttribute(mia3.ATTR_INTEGER, 31); aspects.Add(aspect3_1); MultipleMediaItemAspect aspect3_2 = new MultipleMediaItemAspect(mia3.Metadata); aspect3_2.SetAttribute(mia3.ATTR_ID, "3_2"); aspect3_2.SetAttribute(mia3.ATTR_INTEGER, 32); aspects.Add(aspect3_2); MultipleMediaItemAspect aspect3_3 = new MultipleMediaItemAspect(mia3.Metadata); aspect3_3.Deleted = true; aspect3_3.SetAttribute(mia3.ATTR_INTEGER, 33); aspects.Add(aspect3_3); Guid itemId = new Guid("aaaaaaaa-1111-1111-1111-aaaaaaaaaaaa"); MockReader resourceReader = MockDBUtils.AddReader(1, "SELECT MEDIA_ITEM_ID FROM M_PROVIDERRESOURCE WHERE SYSTEM_ID = @SYSTEM_ID AND PATH = @PATH", "MEDIA_ITEM_ID"); resourceReader.AddResult(itemId); DateTime importDate; DateTime.TryParse("2014-10-11 12:34:56", out importDate); MockReader importReader = MockDBUtils.AddReader(2, "SELECT LASTIMPORTDATE A0, DIRTY A1, DATEADDED A2 FROM M_IMPORTEDITEM WHERE MEDIA_ITEM_ID = @MEDIA_ITEM_ID", "A0", "A1", "A2"); importReader.AddResult(importDate, "false", importDate); string pathStr = @"c:\item.mp3"; MockReader mraReader = MockDBUtils.AddReader(3, "SELECT SYSTEM_ID A0, RESOURCEINDEX A1, ISPRIMARY A2, MIMETYPE A3, SIZE A4, PATH A5, PARENTDIRECTORY A6 FROM M_PROVIDERRESOURCE WHERE MEDIA_ITEM_ID = @MEDIA_ITEM_ID", "MEDIA_ITEM_ID"); mraReader.AddResult("00000000-0000-0000-0000-000000000000", 0, true, "audio/mp3", 100, pathStr, Guid.Empty); MockReader mia1Reader = MockDBUtils.AddReader(4, "SELECT MEDIA_ITEM_ID FROM M_SINGLE1 WHERE MEDIA_ITEM_ID = @MEDIA_ITEM_ID", "MEDIA_ITEM_ID"); mia1Reader.AddResult(itemId); MockReader mia2Reader1 = MockDBUtils.AddReader(5, "SELECT MEDIA_ITEM_ID FROM M_MULTIPLE2 WHERE MEDIA_ITEM_ID = @MEDIA_ITEM_ID AND ATTR_ID = @ATTR_ID", "MEDIA_ITEM_ID", "ATTR_ID"); mia2Reader1.AddResult(itemId, "1_1"); MockReader mia2Reader2 = MockDBUtils.AddReader(6, "SELECT MEDIA_ITEM_ID FROM M_MULTIPLE2 WHERE MEDIA_ITEM_ID = @MEDIA_ITEM_ID AND ATTR_ID = @ATTR_ID", "MEDIA_ITEM_ID", "ATTR_ID"); mia2Reader2.AddResult(itemId, "1_1"); MockDBUtils.AddReader(7, "SELECT MEDIA_ITEM_ID FROM M_MULTIPLE2 WHERE MEDIA_ITEM_ID = @MEDIA_ITEM_ID AND ATTR_ID = @ATTR_ID", "MEDIA_ITEM_ID", "ATTR_ID"); MockDBUtils.AddReader(8, "SELECT MEDIA_ITEM_ID FROM M_MULTIPLE2 WHERE MEDIA_ITEM_ID = @MEDIA_ITEM_ID AND ATTR_ID = @ATTR_ID", "MEDIA_ITEM_ID", "ATTR_ID"); MockDBUtils.AddReader(9, "SELECT MEDIA_ITEM_ID FROM M_MULTIPLE3 WHERE MEDIA_ITEM_ID = @MEDIA_ITEM_ID AND ATTR_ID = @ATTR_ID", "MEDIA_ITEM_ID", "ATTR_ID"); MockDBUtils.AddReader(10, "SELECT MEDIA_ITEM_ID FROM M_MULTIPLE3 WHERE MEDIA_ITEM_ID = @MEDIA_ITEM_ID AND ATTR_ID = @ATTR_ID", "MEDIA_ITEM_ID", "ATTR_ID"); MockDBUtils.AddReader(11, "SELECT T7.MEDIA_ITEM_ID A26, T0.MEDIA_ITEM_ID A27, T1.MEDIA_ITEM_ID A28, T2.MEDIA_ITEM_ID A29, T3.MEDIA_ITEM_ID A30, T4.MEDIA_ITEM_ID A31, " + "T5.MEDIA_ITEM_ID A32, T6.MEDIA_ITEM_ID A33, T0.ATTR_STRING A0, T0.ATTR_INTEGER A1, T1.ATTR_ID A2, T1.ATTR_STRING A3, T2.ATTR_ID A4, T2.ATTR_INTEGER A5, T3.ATTR_STRING A6, " + "T3.ATTR_INTEGER A7, T4.SYSTEM_ID A8, T4.RESOURCEINDEX A9, T4.ISPRIMARY A10, T4.MIMETYPE A11, T4.SIZE A12, T4.PATH A13, T4.PARENTDIRECTORY A14, T5.LASTIMPORTDATE A15, T5.DIRTY A16, " + "T5.DATEADDED A17, T6.TITLE A18, T6.SORTTITLE A19, T6.RECORDINGTIME A20, T6.RATING A21, T6.COMMENT A22, T6.PLAYCOUNT A23, T6.LASTPLAYED A24, T6.ISVIRTUAL A25 FROM MEDIA_ITEMS T7 " + "LEFT OUTER JOIN M_SINGLE1 T0 ON T0.MEDIA_ITEM_ID = T7.MEDIA_ITEM_ID LEFT OUTER JOIN M_MULTIPLE2 T1 ON T1.MEDIA_ITEM_ID = T7.MEDIA_ITEM_ID " + "LEFT OUTER JOIN M_MULTIPLE3 T2 ON T2.MEDIA_ITEM_ID = T7.MEDIA_ITEM_ID LEFT OUTER JOIN M_SINGLE4 T3 ON T3.MEDIA_ITEM_ID = T7.MEDIA_ITEM_ID " + "LEFT OUTER JOIN M_PROVIDERRESOURCE T4 ON T4.MEDIA_ITEM_ID = T7.MEDIA_ITEM_ID LEFT OUTER JOIN M_IMPORTEDITEM T5 ON T5.MEDIA_ITEM_ID = T7.MEDIA_ITEM_ID " + "LEFT OUTER JOIN M_MEDIAITEM T6 ON T6.MEDIA_ITEM_ID = T7.MEDIA_ITEM_ID WHERE T7.MEDIA_ITEM_ID = @V0", "V0"); ResourcePath path = LocalFsResourceProviderBase.ToResourcePath(pathStr); MockCore.Library.AddOrUpdateMediaItem(Guid.Empty, null, path, aspects, true); MockCore.ShutdownLibrary(); }
private IList <MediaItem> GetMediaItems(ISQLDatabase database, ITransaction transaction, bool singleMode, IEnumerable <MediaItemAspectMetadata> selectedMIAs, out IList <Guid> mediaItemIds, out IDictionary <Guid, IList <Guid> > complexMediaItems) { string statementStr; IList <BindVar> bindVars; MIAQueryBuilder builder = new MIAQueryBuilder(_miaManagement, _mainSelectAttributes.Values, null, _necessaryRequestedMIAs, _optionalRequestedMIAs, _filter, _subqueryFilter, _sortInformation); using (IDbCommand command = transaction.CreateCommand()) { string mediaItemIdAlias2; IDictionary <MediaItemAspectMetadata, string> miamAliases; // Maps (selected and filtered) QueryAttributes to CompiledQueryAttributes in the SQL query IDictionary <QueryAttribute, string> qa2a; builder.GenerateSqlStatement(out mediaItemIdAlias2, out miamAliases, out qa2a, out statementStr, out bindVars); // Try to use SQL side paging, which gives best performance if supported ISQLDatabasePaging paging = database as ISQLDatabasePaging; if (paging != null) { paging.Process(ref statementStr, ref bindVars, ref _offset, ref _limit); } command.CommandText = statementStr; foreach (BindVar bindVar in bindVars) { database.AddParameter(command, bindVar.Name, bindVar.Value, bindVar.VariableType); } using (IDataReader fullReader = command.ExecuteReader()) { IList <MediaItem> result = new List <MediaItem>(); mediaItemIds = new List <Guid>(); complexMediaItems = new Dictionary <Guid, IList <Guid> >(); var records = fullReader.AsEnumerable(); if (_offset.HasValue) { records = records.Skip((int)_offset.Value); } if (_limit.HasValue) { records = records.Take((int)_limit.Value); } foreach (var reader in records) { Guid mediaItemId = database.ReadDBValue <Guid>(reader, reader.GetOrdinal(mediaItemIdAlias2)); if (mediaItemIds.Contains(mediaItemId)) { // Media item was already added to result - query results are not always unique because of JOINs used for filtering continue; } mediaItemIds.Add(mediaItemId); MediaItem mediaItem = new MediaItem(mediaItemId); foreach (SingleMediaItemAspectMetadata miam in selectedMIAs.Where(x => x is SingleMediaItemAspectMetadata)) { string name; if (!miamAliases.TryGetValue(miam, out name) || reader.IsDBNull(reader.GetOrdinal(name))) { // MIAM is not available for current media item continue; } IList <Guid> complexIds; if (!complexMediaItems.TryGetValue(miam.AspectId, out complexIds)) { complexMediaItems[miam.AspectId] = complexIds = new List <Guid>(); } complexIds.Add(mediaItemId); SingleMediaItemAspect mia = new SingleMediaItemAspect(miam); foreach (MediaItemAspectMetadata.AttributeSpecification attr in miam.AttributeSpecifications.Values) { if (attr.Cardinality == Cardinality.Inline) { QueryAttribute qa = _mainSelectAttributes[attr]; string alias = qa2a[qa]; mia.SetAttribute(attr, database.ReadDBValue(attr.AttributeType, reader, reader.GetOrdinal(alias))); } } MediaItemAspect.SetAspect(mediaItem.Aspects, mia); } result.Add(mediaItem); if (singleMode) { break; } } return(result); } } }
public IList <MediaItem> Query(ISQLDatabase database, ITransaction transaction, bool singleMode) { ILogger logger = ServiceRegistration.Get <ILogger>(); try { IList <MediaItemAspectMetadata> selectedMIAs = new List <MediaItemAspectMetadata>(_necessaryRequestedMIAs.Union(_optionalRequestedMIAs)); IList <Guid> mediaItemIds; IDictionary <Guid, IList <Guid> > complexMediaItemIds; IList <MediaItem> mediaItems = GetMediaItems(database, transaction, singleMode, selectedMIAs, out mediaItemIds, out complexMediaItemIds); //logger.Debug("CompiledMediaItemQuery::Query got media items IDs [{0}]", string.Join(",", mediaItemIds)); // TODO: Why bother looking for complex attributes on MIAs we don't have? IDictionary <Guid, IDictionary <MediaItemAspectMetadata.AttributeSpecification, IList> > complexAttributeValues = new Dictionary <Guid, IDictionary <MediaItemAspectMetadata.AttributeSpecification, IList> >(); ICollection <IList <Guid> > mediaItemIdsClusters = CollectionUtils.Cluster(mediaItemIds, CompiledFilter.MAX_IN_VALUES_SIZE); foreach (IList <Guid> mediaItemIdsCluster in mediaItemIdsClusters.Where(x => x.Count > 0)) { AddComplexAttributes(database, transaction, mediaItemIdsCluster, complexAttributeValues, complexMediaItemIds); } foreach (MediaItem mediaItem in mediaItems) { foreach (SingleMediaItemAspectMetadata miam in selectedMIAs.Where(x => x is SingleMediaItemAspectMetadata)) { // Skip complex attributes for this MIA if it's not already in the media item if (!mediaItem.Aspects.ContainsKey(miam.AspectId)) { continue; } IDictionary <MediaItemAspectMetadata.AttributeSpecification, IList> attributeValues; if (!complexAttributeValues.TryGetValue(mediaItem.MediaItemId, out attributeValues)) { continue; } SingleMediaItemAspect mia = MediaItemAspect.GetOrCreateAspect(mediaItem.Aspects, miam); foreach (MediaItemAspectMetadata.AttributeSpecification attr in miam.AttributeSpecifications.Values) { if (attr.Cardinality != Cardinality.Inline) { IList attrValues; if (attributeValues != null && attributeValues.TryGetValue(attr, out attrValues)) { mia.SetCollectionAttribute(attr, attrValues); } } } } } IDictionary <Guid, ICollection <MultipleMediaItemAspect> > multipleMiaValues = new Dictionary <Guid, ICollection <MultipleMediaItemAspect> >(); foreach (IList <Guid> mediaItemIdsCluster in mediaItemIdsClusters.Where(x => x.Count > 0)) { AddMultipleMIAs(database, transaction, _explicitRequestedMIAs, mediaItemIdsCluster, multipleMiaValues); } if (multipleMiaValues.Count > 0) { //logger.Debug("Got multiple MIAs [{0}]", string.Join(",", multipleMiaValues.Keys)); foreach (MediaItem mediaItem in mediaItems) { ICollection <MultipleMediaItemAspect> values; if (!multipleMiaValues.TryGetValue(mediaItem.MediaItemId, out values)) { continue; } foreach (MultipleMediaItemAspect value in values) { //logger.Debug("Adding MIA {0} #{1}", value.Metadata.Name, value.Index); MediaItemAspect.AddOrUpdateAspect(mediaItem.Aspects, value); } } } return(mediaItems); } catch (Exception e) { logger.Error("Unable to query", e); throw e; } }