Beispiel #1
0
        public async Task <byte[]> GetImage(string Id, string type, int width, int height, string format)
        {
            ServerListItem server = settings.GetServer();

            if (server == null)
            {
                return(null);
            }

            Uri url = new Uri("http://" + server + "/mediabrowser/Items/" + Id + "/Images/" + type + "?Width=" + width + "&Height=" + height + "&Format=" + format);

            HttpClient httpClient = new HttpClient();

            byte[] data = null;

            try
            {
                data = await httpClient.GetByteArrayAsync(url);
            }
            catch (Exception e)
            {
                data = null;
            }

            return(data);
        }
Beispiel #2
0
        public async Task <SessionInfo> GetSessionInfo()
        {
            ServerListItem server = settings.GetServer();

            if (server == null)
            {
                return(null);
            }

            Uri url = new Uri("http://" + server + "/mediabrowser/Sessions?DeviceId=" + settings.GetDeviceId() + "&format=json");

            HttpClient httpClient = new HttpClient();

            string authorization = await GetAuthorizationHeader();

            httpClient.DefaultRequestHeaders.Add("Authorization", authorization);

            string authToken = await Authenticate();

            httpClient.DefaultRequestHeaders.Add("X-MediaBrowser-Token", authToken);

            HttpResponseMessage itemResponce = await httpClient.GetAsync(url);

            itemResponce.EnsureSuccessStatusCode();

            string responceText = await itemResponce.Content.ReadAsStringAsync();

            JArray sessionInfoList = JArray.Parse(responceText);

            SessionInfo info = null;

            if (sessionInfoList.Count > 0)
            {
                JObject sessionInfo = (JObject)sessionInfoList[0];

                // build the responce object
                info = new SessionInfo();

                info.Id = (string)sessionInfo["Id"];

                JObject transcodingInfo = (JObject)sessionInfo["TranscodingInfo"];
                if (transcodingInfo != null)
                {
                    info.AudioCodec           = (transcodingInfo["AudioCodec"] != null) ? (string)transcodingInfo["AudioCodec"] : "";
                    info.IsAudioDirect        = (transcodingInfo["IsAudioDirect"] != null) ? (bool)transcodingInfo["IsAudioDirect"] : false;
                    info.VideoCodec           = (transcodingInfo["VideoCodec"] != null) ? (string)transcodingInfo["VideoCodec"] : "";
                    info.IsVideoDirect        = (transcodingInfo["IsVideoDirect"] != null) ? (bool)transcodingInfo["IsVideoDirect"] : false;
                    info.Container            = (transcodingInfo["Container"] != null) ? (string)transcodingInfo["Container"] : "";
                    info.Bitrate              = (transcodingInfo["Bitrate"] != null) ? (int)transcodingInfo["Bitrate"] : 0;
                    info.Framerate            = (transcodingInfo["Framerate"] != null) ? (double)transcodingInfo["Framerate"] : 0;
                    info.CompletionPercentage = (transcodingInfo["CompletionPercentage"] != null) ? (double)transcodingInfo["CompletionPercentage"] : 0;
                    info.Width         = (transcodingInfo["Width"] != null) ? (int)transcodingInfo["Width"] : 0;
                    info.Height        = (transcodingInfo["Height"] != null) ? (int)transcodingInfo["Height"] : 0;
                    info.AudioChannels = (transcodingInfo["AudioChannels"] != null) ? (int)transcodingInfo["AudioChannels"] : 0;
                }
            }

            return(info);
        }
        public async Task <bool> UpdateTileNotifications()
        {
            MetroEventSource.Log.Info("Setting Tile Notifications Started");

            await DeleteOldImages();

            TileUpdateManager.CreateTileUpdaterForApplication().EnableNotificationQueue(true);

            AppSettings settings = new AppSettings();
            ApiClient   client   = new ApiClient();

            ServerListItem server = settings.GetServer();

            if (server == null)
            {
                return(false);
            }

            List <MediaItem> items = await client.GetResentItems();

            foreach (MediaItem item in items)
            {
                string itemImage = "http://" + server + "/mediabrowser/Items/" + item.Id + "/Images/Thumb?Width=310&Height=150";
                string name      = item.Name;
                string imageName = "TileImage-" + item.Id + ".jpg";

                // get image data and save it
                byte[] image = await client.GetImage(item.Id, "Thumb", 310, 150, "jpg");

                if (image != null)
                {
                    var storageFileTask = await ApplicationData.Current.LocalFolder.CreateFileAsync(imageName, CreationCollisionOption.ReplaceExisting);

                    await FileIO.WriteBytesAsync(storageFileTask, image);

                    XmlDocument tileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWide310x150Image);

                    //XmlDocument tileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWide310x150ImageAndText01);
                    //XmlNodeList tileTextAttributes = tileXml.GetElementsByTagName("text");
                    //tileTextAttributes[0].InnerText = name;

                    XmlNodeList tileImageAttributes = tileXml.GetElementsByTagName("image");
                    ((XmlElement)tileImageAttributes[0]).SetAttribute("src", "ms-appdata:///local/" + imageName);
                    ((XmlElement)tileImageAttributes[0]).SetAttribute("alt", "item thumb");

                    TileNotification tileNotification = new TileNotification(tileXml);

                    tileNotification.ExpirationTime = DateTimeOffset.UtcNow.AddDays(90);

                    TileUpdateManager.CreateTileUpdaterForApplication().Update(tileNotification);
                }
            }

            MetroEventSource.Log.Info("Setting Tile Notifications Ended");

            return(true);
        }
Beispiel #4
0
        public async Task <List <MediaItem> > GetResentItems()
        {
            List <MediaItem> recentItems = new List <MediaItem>();

            string userId = "";

            try
            {
                userId = await GetUserID();
            }
            catch (Exception e)
            {
                return(recentItems);
            }

            ServerListItem server = settings.GetServer();

            if (server == null)
            {
                return(recentItems);
            }

            Uri url = new Uri("http://" + server + "/mediabrowser/Users/" + userId + "/Items/Latest?Limit=5&IsPlayed=false&format=json");
            //&IncludeItemTypes=Movie

            HttpClient httpClient = new HttpClient();

            string authorization = await GetAuthorizationHeader();

            httpClient.DefaultRequestHeaders.Add("Authorization", authorization);

            string authToken = await Authenticate();

            httpClient.DefaultRequestHeaders.Add("X-MediaBrowser-Token", authToken);

            HttpResponseMessage itemResponce = await httpClient.GetAsync(url);

            itemResponce.EnsureSuccessStatusCode();

            string itemResponceText = await itemResponce.Content.ReadAsStringAsync();

            JArray items = JArray.Parse(itemResponceText);

            foreach (JObject itemInfo in items)
            {
                MediaItem mediaItem = new MediaItem();

                mediaItem.Id   = (string)itemInfo["Id"];
                mediaItem.Name = (string)itemInfo["Name"];

                recentItems.Add(mediaItem);
            }

            return(recentItems);
        }
Beispiel #5
0
        public async Task <bool> SetupWebSocket()
        {
            bool worked = false;

            try
            {
                ServerListItem server = settings.GetServer();
                if (server == null)
                {
                    return(false);
                    //throw new Exception("Server not set");
                }

                Uri serverUri = new Uri("ws://" + server + "/mediabrowser");
                webSocket = new MessageWebSocket();
                webSocket.Control.MessageType = SocketMessageType.Utf8;

                webSocket.MessageReceived += MessageReceived;

                webSocket.Closed += Closed;

                await webSocket.ConnectAsync(serverUri);

                DataWriter messageWriter = new DataWriter(webSocket.OutputStream);

                string deviceName = settings.GetDeviceName();
                string value      = "SPMB";
                if (string.IsNullOrEmpty(deviceName) == false)
                {
                    value = "SPMB-" + settings.GetDeviceName();
                }

                string identityMessage = "{\"MessageType\":\"Identity\", \"Data\":\"Windows RT|" + settings.GetDeviceId() + "|0.0.1|" + value + "\"}";
                messageWriter.WriteString(identityMessage);
                await messageWriter.StoreAsync();

                worked = true;
            }
            catch (Exception e)
            {
                MetroEventSource.Log.Info("Error Creating WebSocket - " + e.Message);
                string errorString = "Error Creating WebSocket : " + e.Message;
                App.AddNotification(new Notification()
                {
                    Title = "Error Creating Web Socket", Message = errorString
                });
            }

            return(worked);
        }
Beispiel #6
0
        public async Task <bool> SetCapabilities()
        {
            bool        worked = false;
            SessionInfo sessionInfo;

            sessionInfo = await GetSessionInfo();

            if (sessionInfo == null)
            {
                return(false);
            }

            ServerListItem server = settings.GetServer();

            if (server == null)
            {
                throw new Exception("Server not set");
            }

            Uri url = new Uri("http://" + server + "/mediabrowser/Sessions/Capabilities?Id=" + sessionInfo.Id + "&PlayableMediaTypes=Video&SupportedCommands=Play");

            HttpClient httpClient = new HttpClient();

            string authorization = await GetAuthorizationHeader();

            httpClient.DefaultRequestHeaders.Add("Authorization", authorization);

            string authToken = await Authenticate();

            httpClient.DefaultRequestHeaders.Add("X-MediaBrowser-Token", authToken);

            JObject jsonData = new JObject();

            jsonData.Add("Id", sessionInfo.Id);
            jsonData.Add("PlayableMediaTypes", "Video");

            HttpContent myContent = new StringContent(jsonData.ToString(), Encoding.UTF8, "application/json");

            var responce = await httpClient.PostAsync(url, myContent);

            responce.EnsureSuccessStatusCode();

            return(worked);
        }
Beispiel #7
0
        public async void PlaybackCheckinStopped(string itemId, long position)
        {
            MetroEventSource.Log.Info("Sending PlaybackCheckinStopped for " + itemId + " @ " + position);

            HttpClient httpClient = new HttpClient();

            string authorization = await GetAuthorizationHeader();

            httpClient.DefaultRequestHeaders.Add("Authorization", authorization);

            string authToken = await Authenticate();

            httpClient.DefaultRequestHeaders.Add("X-MediaBrowser-Token", authToken);

            JObject jsonData = new JObject();

            JArray queueable = new JArray();

            //queueable.Add("Video");
            jsonData.Add("QueueableMediaTypes", queueable);
            jsonData.Add("CanSeek", true);
            jsonData.Add("ItemId", itemId);
            jsonData.Add("MediaSourceId", "");
            jsonData.Add("IsPaused", false);
            jsonData.Add("IsMuted", false);
            jsonData.Add("PositionTicks", (position * 1000 * 10000));
            jsonData.Add("PlayMethod", "Transcode");

            string playbackData = jsonData.ToString();

            ServerListItem server = settings.GetServer();

            if (server == null)
            {
                throw new Exception("Server not set");
            }

            Uri         url       = new Uri("http://" + server + "/mediabrowser/Sessions/Playing/Stopped");
            HttpContent myContent = new StringContent(playbackData, Encoding.UTF8, "application/json");
            var         responce  = await httpClient.PostAsync(url, myContent);

            //responce.EnsureSuccessStatusCode();
        }
        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            ObservableCollection<ServerListItem> list = serverList.ItemsSource as ObservableCollection<ServerListItem>;

            ServerListItem server = new ServerListItem();
            server.host = setting_server.Text.Trim();
            server.port = setting_port.Text.Trim();

            list.Add(server);
            int selectedIndex = serverList.SelectedIndex;
            if (selectedIndex == -1 && list.Count > 0)
            {
                serverList.SelectedIndex = 0;
            }
        }
Beispiel #9
0
        public async Task <MediaItem> GetItemInfo(string itemId)
        {
            MediaItem item = new MediaItem();

            string userId = await GetUserID();

            ServerListItem server = settings.GetServer();

            if (server == null)
            {
                throw new Exception("Server not set");
            }

            Uri url = new Uri("http://" + server + "/mediabrowser/Users/" + userId + "/Items/" + itemId + "?format=json");

            HttpClient httpClient = new HttpClient();

            string authorization = await GetAuthorizationHeader();

            httpClient.DefaultRequestHeaders.Add("Authorization", authorization);

            string authToken = await Authenticate();

            httpClient.DefaultRequestHeaders.Add("X-MediaBrowser-Token", authToken);

            HttpResponseMessage itemResponce = await httpClient.GetAsync(url);

            itemResponce.EnsureSuccessStatusCode();

            string itemResponceText = await itemResponce.Content.ReadAsStringAsync();

            JObject itemInfo = JObject.Parse(itemResponceText);

            if (itemInfo["RunTimeTicks"] != null)
            {
                long runTimeSeconds = (long)itemInfo["RunTimeTicks"];
                runTimeSeconds = (runTimeSeconds / 1000) / 10000;
                item.Duration  = runTimeSeconds;
            }
            else
            {
                item.Duration = 0;
            }

            item.Id           = (itemInfo["Id"] != null) ? (string)itemInfo["Id"] : "";
            item.Name         = (itemInfo["Name"] != null) ? (string)itemInfo["Name"] : "";
            item.Year         = (itemInfo["ProductionYear"] != null) ? (int)itemInfo["ProductionYear"] : 0;
            item.Series       = (itemInfo["SeriesName"] != null) ? (string)itemInfo["SeriesName"] : "";
            item.Type         = (itemInfo["Type"] != null) ? (string)itemInfo["Type"] : "";
            item.Season       = (itemInfo["ParentIndexNumber"] != null) ? (int)itemInfo["ParentIndexNumber"] : 0;
            item.EpisodeIndex = (itemInfo["IndexNumber"] != null) ? (int)itemInfo["IndexNumber"] : 0;

            item.SeriesId = (itemInfo["SeriesId"] != null) ? (string)itemInfo["SeriesId"] : "";

            // extract media streams
            JArray streams = (JArray)itemInfo["MediaStreams"];

            item.mediaStreams = new List <MediaStreamInfo>();

            foreach (JObject stream in streams)
            {
                MediaStreamInfo mInfo = new MediaStreamInfo();

                mInfo.Index                = (int)stream["Index"];
                mInfo.Type                 = (string)stream["Type"];
                mInfo.Language             = (string)stream["Language"];
                mInfo.Codec                = (string)stream["Codec"];
                mInfo.IsTextSubtitleStream = (bool)stream["IsTextSubtitleStream"];

                item.mediaStreams.Add(mInfo);
            }

            return(item);
        }