public virtual void Seek(long position, ResponseListener listener)
        {
            IMediaControl mediaControl = null;

            if (Service != null)
                mediaControl = Service as IMediaControl;

            if (mediaControl != null)
                mediaControl.Seek(position, listener);
            else if (listener != null)
                listener.OnError(ServiceCommandError.NotSupported());
        }
        public void GetVolume(ResponseListener listener)
        {
            const string method = "GetVolume";
            const string instanceId = "0";
            const string channel = "Master";

            var parameters = new Dictionary<string, string> { { "Channel", channel } };

            var payload = GetMethodBody(RENDERING_CONTROL_URN, instanceId, method, parameters);

            var responseListener = new ResponseListener
            (
                loadEventArg =>
                {
                    var currentVolume = Util.ParseData((String)loadEventArg, "CurrentVolume");
                    var iVolume = 0;
                    try
                    {
                        iVolume = int.Parse(currentVolume);
                    }
                    // ReSharper disable once EmptyGeneralCatchClause
                    catch
                    {

                    }
                    var fVolume = (float)(iVolume / 100.0);

                    Util.PostSuccess(listener, fVolume);
                },
                serviceCommandError =>
                {
                    if (listener != null)
                        listener.OnError(serviceCommandError);
                }
            );

            var request = new ServiceCommand(this, method, payload, responseListener);
            request.Send();
        }
        public void UnPinWebApp(string webAppId, ResponseListener listener)
        {
            if (string.IsNullOrEmpty(webAppId))
            {
                if (listener != null)
                {
                    listener.OnError(new ServiceCommandError(-1, "You must provide a valid web app id"));
                }
                return;
            }

            const string uri = "ssap://webapp/removePinnedWebApp";
            var payload = new JsonObject();

            try
            {
                payload.Add("webAppId", JsonValue.CreateStringValue(webAppId));
            }
            // ReSharper disable once EmptyGeneralCatchClause
            catch (Exception)
            {

            }

            var responseListener = new ResponseListener
            (
                loadEventArg =>
                {
                    var loadEventArgs = loadEventArg as LoadEventArgs;
                    if (loadEventArgs == null) return;
                    var jsonObj = (JsonObject)(loadEventArgs.Load.GetPayload());
                    if (jsonObj.ContainsKey("pairingType"))
                    {
                        NotifyPairingRequired();
                    }
                    else
                    {
                        listener.OnSuccess(loadEventArg);
                    }
                },
                serviceCommandError => Util.PostError(listener, serviceCommandError)
            );

            ServiceCommand request = new UrlServiceSubscription(this, uri, payload, true, responseListener);
            request.Send();
        }
        public void DisplayImage(string url, string mimeType, string title, string description, string iconSrc,
            ResponseListener listener)
        {
            if (ServiceDescription.Version != null && ServiceDescription.Version.Equals("4.0.0"))
            {
                var dlnaService = GetDlnaService();

                if (dlnaService != null)
                {
                    var mediaPlayer = dlnaService.GetApi<IMediaPlayer>();

                    if (mediaPlayer != null)
                    {
                        mediaPlayer.DisplayImage(url, mimeType, title, description, iconSrc, listener);
                        return;
                    }
                }

                JsonObject ps = null;
                try
                {

                    ps = new JsonObject
                    {
                        {"target", JsonValue.CreateStringValue(url)},
                        {"title", JsonValue.CreateStringValue(title ?? string.Empty)},
                        {"description", JsonValue.CreateStringValue(description ?? string.Empty)},
                        {"mimeType", JsonValue.CreateStringValue(mimeType ?? string.Empty)},
                        {"iconSrc", JsonValue.CreateStringValue(iconSrc ?? string.Empty)},
                    };
                }
                catch (Exception ex)
                {
                    Util.PostError(listener, new ServiceCommandError(-1, ex.Message));
                }

                if (ps != null)
                    DisplayMedia(ps, listener);
            }
            else
            {
                const string webAppId = "MediaPlayer";
                var webAppLaunchListener = new ResponseListener
                    (
                        loadEventArg =>
                        {
                            var loadEventArgs = loadEventArg as LoadEventArgs;
                            if (loadEventArgs != null)
                            {
                                var webAppSession = (loadEventArgs.Load.GetPayload()) as WebAppSession;
                                if (webAppSession != null)
                                    webAppSession.DisplayImage(url, mimeType, title, description, iconSrc, listener);
                            }
                        },
                        serviceCommandError =>
                        {
                            listener.OnError(serviceCommandError);
                        });

                var webappResponseListener = new ResponseListener
                    (
                        loadEventArg =>
                        {
                            var loadEventArgs = loadEventArg as LoadEventArgs;
                            if (loadEventArgs != null)
                            {
                                var webAppSession = (loadEventArgs.Load.GetPayload()) as WebAppSession;
                                if (webAppSession != null)
                                    webAppSession.DisplayImage(url, mimeType, title, description, iconSrc, listener);
                            }
                        },
                        serviceCommandError =>
                        {
                            GetWebAppLauncher().LaunchWebApp(webAppId, webAppLaunchListener);
                        });

                GetWebAppLauncher().JoinWebApp(webAppId, webappResponseListener);
            }
        }
        private void Connect(bool joinOnly, ResponseListener connectionListener)
        {
            if (Socket != null && Socket.State == State.Connecting)
            {
                if (connectionListener != null)
                    connectionListener.OnError(new ServiceCommandError(0,
                        "You have a connection request pending,  please wait until it has finished"));
                return;
            }

            if (IsConnected())
            {
                if (connectionListener != null)
                    connectionListener.OnSuccess(null);
                return;
            }

            MConnectionListener = new ResponseListener
            (
                loadEventArg =>
                {
                    var finalConnectionListener = new ResponseListener
                    (
                        loadEventArg2 =>
                        {
                            Connected = true;

                            if (connectionListener != null)
                                connectionListener.OnSuccess(loadEventArg2);
                        },
                        serviceCommandError =>
                        {
                            DisconnectFromWebApp();

                            if (connectionListener != null)
                                connectionListener.OnError(serviceCommandError);
                        }
                    );

                    Service.ConnectToWebApp(this, joinOnly, finalConnectionListener);
                },
                serviceCommandError =>
                {
                    if (Socket != null)
                        DisconnectFromWebApp();

                    if (connectionListener != null)
                    {
                        if (serviceCommandError == null)
                            serviceCommandError = new ServiceCommandError(0, "Unknown error connecting to web app");

                        connectionListener.OnError(serviceCommandError);
                    }
                }
            );

            if (Socket != null)
            {
                if (Socket.IsConnected())
                    MConnectionListener.OnSuccess(null);
                else
                    Socket.Connect();
            }
            else
            {
                //var uri = WebOstvServiceSocketClient.GetUri(Service);
                //Socket = new WebOstvServiceSocketClient(Service, uri);
                //Socket.Listener = mSocketListener;
                //Socket.Connect();

                var uri = WebOstvServiceSocketClient.GetUri(Service);
                if (WebOstvServiceSocketClient.SocketCache.ContainsKey(uri.ToString()))
                {
                    Socket = WebOstvServiceSocketClient.SocketCache[uri.ToString()];
                    if (mSocketListener != null)
                    {
                        Socket.Listener = mSocketListener;
                    }
                    if (Socket.IsConnected())
                        MConnectionListener.OnSuccess(null);
                    else
                    {
                        Socket.Connect();
                    }
                    //MConnectionListener.OnSuccess(null);
                }
                else
                {
                    Socket = new WebOstvServiceSocketClient(Service, uri)
                    {
                        Listener = mSocketListener
                    };
                    Socket.Connect();
                    WebOstvServiceSocketClient.SocketCache.Add(uri.ToString(), Socket);
                }
            }
        }
Esempio n. 6
0
 public static void PostError(ResponseListener listener, ServiceCommandError error)
 {
     if (listener == null)
         return;
     listener.OnError(error);
 }
        public void FastForward(ResponseListener listener)
        {
            IMediaControl mediaControl = null;

            if (Service != null)
                mediaControl = Service as IMediaControl;

            if (mediaControl != null)
                mediaControl.FastForward(listener);
            else if (listener != null)
                listener.OnError(ServiceCommandError.NotSupported());
        }
        public override void Seek(long position, ResponseListener listener)
        {
            if (position < 0)
            {
                if (listener != null)
                    listener.OnError(new ServiceCommandError(0, null));

                return;
            }

            var requestIdNumber = GetNextId();
            var requestId = String.Format("req{0}", requestIdNumber);

            JsonObject message = null;
            try
            {
                message = new JsonObject {{"contentType", JsonValue.CreateStringValue(NamespaceKey + "mediaCommand")}};
                var mediaCommandObject = new JsonObject
                {
                    {"type", JsonValue.CreateStringValue("seek")},
                    {"position", JsonValue.CreateNumberValue((int)(position/1000))},
                    {"requestId", JsonValue.CreateStringValue(requestId)}
                };
                message.Add("mediaCommand", mediaCommandObject);
            }
            catch (Exception)
            {
                if (listener != null)
                    listener.OnError(new ServiceCommandError(0, null));
            }

            var command = new ServiceCommand(null, null, null, listener);

            mActiveCommands.TryAdd(requestId, command);

            SendMessage(message, listener);
        }
        public IServiceSubscription SubscribeWebAppStatus(ResponseListener listener)
        {
            if (listener != null)
                listener.OnError(ServiceCommandError.NotSupported());

            return null;
        }
 public virtual void Close(ResponseListener listener)
 {
     if (listener != null)
         listener.OnError(ServiceCommandError.NotSupported());
 }
        public virtual IServiceSubscription SubscribePlayState(ResponseListener listener)
        {
            IMediaControl mediaControl = null;

            if (Service != null)
                mediaControl = Service as IMediaControl;

            if (mediaControl != null)
                return mediaControl.SubscribePlayState(listener);
            if (listener != null)
                listener.OnError(ServiceCommandError.NotSupported());

            return null;
        }
 public IServiceSubscription SubscribeMediaInfo(ResponseListener listener)
 {
     listener.OnError(ServiceCommandError.NotSupported());
     return null;
 }
 public virtual void SendMessage(JsonObject message, ResponseListener listener)
 {
     if (listener != null)
     {
         listener.OnError(ServiceCommandError.NotSupported());
     }
 }
        public override void GetPosition(ResponseListener listener)
        {
            var requestIdNumber = GetNextId();
            var requestId = String.Format("req{0}", requestIdNumber);

            JsonObject message = null;
            try
            {
                message = new JsonObject {{"contentType", JsonValue.CreateStringValue(NamespaceKey + "mediaCommand")}};
                var mediaCommandObject = new JsonObject
                {
                    {"type", JsonValue.CreateStringValue("getPosition")},
                    {"requestId", JsonValue.CreateStringValue(requestId)}
                };
                message.Add("mediaCommand", mediaCommandObject);
            }
            catch (Exception)
            {
                if (listener != null)
                    listener.OnError(new ServiceCommandError(0, null));
            }

            var commandResponseListener = new ResponseListener
            (
                loadEventArg =>
                {
                    try
                    {
                        var position = ((loadEventArg as LoadEventArgs).Load.GetPayload() as JsonObject).GetNamedNumber("position");

                        if (listener != null)
                            listener.OnSuccess(position * 1000);
                    }
                    catch (Exception)
                    {
                        if (listener != null) listener.OnError(new ServiceCommandError(0, null));
                    }
                },
                serviceCommandError =>
                {
                    if (listener != null)
                        listener.OnError(serviceCommandError);
                }
            );

            var command = new ServiceCommand(null, null, null, commandResponseListener);
            mActiveCommands.TryAdd(requestId, command);

            var messageResponseListener = new ResponseListener
            (
                loadEventArg =>
                {

                },
                serviceCommandError =>
                {
                    if (listener != null)
                        listener.OnError(serviceCommandError);
                }
            );

            SendMessage(message, messageResponseListener);
        }
        public void VolumeUp(ResponseListener listener)
        {
            var responseListener = new ResponseListener
            (
                loadEventArg =>
                {
                    var volume = (float)loadEventArg;
                    if (volume >= 1.0)
                    {
                        if (listener != null)
                        {
                            listener.OnSuccess(loadEventArg);
                        }
                    }
                    else
                    {
                        var newVolume = (float)(volume + 0.01);

                        if (newVolume > 1.0)
                            newVolume = (float)1.0;

                        SetVolume(newVolume, listener);

                        Util.PostSuccess(listener, null);
                    }
                },
                serviceCommandError =>
                {
                    if (listener != null)
                        listener.OnError(serviceCommandError);
                }
            );

            GetVolume(responseListener);
        }
        public override void PlayMedia(String url, String mimeType, String title, String description, String iconSrc,
            bool shouldLoop, ResponseListener listener)
        {
            var requestIdNumber = GetNextId();
            var requestId = String.Format("req{0}", requestIdNumber);

            JsonObject message = null;
            try
            {
                message = new JsonObject {{"contentType", JsonValue.CreateStringValue(NamespaceKey + "mediaCommand")}};
                var mediaCommandObject = new JsonObject
                {
                    {"mimeType", JsonValue.CreateStringValue(mimeType)},
                    {"requestId", JsonValue.CreateStringValue(requestId)},
                    {"iconURL", JsonValue.CreateStringValue(iconSrc)},
                    {"title", JsonValue.CreateStringValue(title)},
                    {"type", JsonValue.CreateStringValue("playMedia")},
                    {"description", JsonValue.CreateStringValue(description)},
                    {"mediaURL", JsonValue.CreateStringValue(url)},
                    {"shouldLoop", JsonValue.CreateBooleanValue(shouldLoop)},
                };

                message.Add("mediaCommand", mediaCommandObject);
            }
            catch (Exception)
            {
                if (listener != null)
                    listener.OnError(new ServiceCommandError(0, null));
            }

            var responseListener = new ResponseListener
            (
                loadEventArg => Util.PostSuccess(listener, new MediaLaunchObject(LaunchSession, GetMediaControl())),
                serviceCommandError => Util.PostError(listener, serviceCommandError)
            );

            var command = new ServiceCommand(Socket, null, null, responseListener);

            mActiveCommands.TryAdd(requestId, command);

            var messageResponseListener = new ResponseListener
            (
                loadEventArg => { },
                serviceCommandError => Util.PostError(listener, serviceCommandError)
            );

            SendP2PMessage(message, messageResponseListener);
        }
        private void GetPositionInfo(ResponseListener listener)
        {
            const string method = "GetPositionInfo";
            const string instanceId = "0";

            string payload = GetMethodBody(AV_TRANSPORT_URN, instanceId, method, null);

            var responseListener = new ResponseListener
            (
            loadEventArg =>
            {
                if (listener != null)
                {
                    var s = LoadEventArgs.GetValue<string>(loadEventArg);

                    listener.OnSuccess(s);
                }
            },
            serviceCommandError =>
            {
                if (listener != null)
                    listener.OnError(serviceCommandError);
            }
            );

            var request = new ServiceCommand(this, method, payload, responseListener);
            request.Send();
        }
        public override void SendMessage(String message, ResponseListener listener)
        {
            if (string.IsNullOrEmpty(message))
            {
                if (listener != null)
                    listener.OnError(new ServiceCommandError(0, null));

                return;
            }

            SendP2PMessage(message, listener);
        }
        // ReSharper disable once UnusedMember.Local
        private void GetProtocolInfo(ResponseListener listener)
        {
            const string method = "GetProtocolInfo";
            const string instanceId = "0";

            var payload = GetMethodBody(AV_TRANSPORT_URN, instanceId, method, null);

            var responseListener = new ResponseListener
            (
                loadEventArg =>
                {
                    if (listener != null)
                    {
                        listener.OnSuccess(loadEventArg);
                    }
                },
                serviceCommandError =>
                {
                    if (listener != null)
                    {
                        listener.OnError(serviceCommandError);
                    }
                }
            );

            var request = new ServiceCommand(this, method, payload, responseListener);
            request.Send();
        }
        private void SendP2PMessage(Object message, ResponseListener listener)
        {
            var payload = new JsonObject();

            try
            {
                payload.Add("type", JsonValue.CreateStringValue("p2p"));
                payload.Add("to", JsonValue.CreateStringValue(GetFullAppId()));
                if (message is JsonObject)
                    //todo: check if this is the fix
                    payload.Add("payload", (message as JsonObject));
                else
                    payload.Add("payload", JsonValue.CreateStringValue(message.ToString()));
            }
            catch (Exception ex)
            {
                throw ex;
            }

            if (IsConnected())
            {
                Socket.SendMessage(payload, null);

                if (listener != null)
                    listener.OnSuccess(null);
            }
            else
            {
                var connectListener = new ResponseListener
                (
                loadEventArg => SendP2PMessage(message, listener),
                serviceCommandError =>
                {
                    if (listener != null)
                        listener.OnError(serviceCommandError);
                }
                );

                Connect(connectListener);
            }
        }
        public void GetMediaInfo(ResponseListener listener)
        {
            var responseListener = new ResponseListener
            (
                loadEventArg =>
                {
                    //todo: implement this
                    throw new NotImplementedException();
                    /*
                                    var positionInfoXml = args as string;
                                    var trackMetaData = Util.ParseData(positionInfoXml, "TrackMetaData");

                                    var info = DlnaMediaInfoParser.GetMediaInfo(trackMetaData);
                                    if (listener != null)
                                    {
                                        listener.OnSuccess(info);
                                    }
                    */
                },
                serviceCommandError =>
                {
                    if (listener != null)
                        listener.OnError(serviceCommandError);
                }
            );
            GetPositionInfo(responseListener);
        }
        public ServiceCommand IsWebAppPinned(bool isSubscription, string webAppId, ResponseListener listener)
        {
            if (string.IsNullOrEmpty(webAppId))
            {
                if (listener != null)
                {
                    listener.OnError(new ServiceCommandError(-1, "You must provide a valid web app id"));
                }
                return null;
            }

            const string uri = "ssap://webapp/isWebAppPinned";
            var payload = new JsonObject();

            try
            {
                payload.Add("webAppId", JsonValue.CreateStringValue(webAppId));
            }
            // ReSharper disable once EmptyGeneralCatchClause
            catch (Exception)
            {

            }

            var responseListener = new ResponseListener
            (
                loadEventArg =>
                {
                    var loadEventArgs = loadEventArg as LoadEventArgs;
                    if (loadEventArgs == null) return;
                    var jsonObj = (JsonObject)(loadEventArgs.Load.GetPayload());

                    var status = jsonObj.GetNamedBoolean("pinned");

                    if (listener != null)
                        listener.OnSuccess(status);
                },
                serviceCommandError => Util.PostError(listener, serviceCommandError)
            );

            var request = isSubscription ? new UrlServiceSubscription(this, uri, payload, true, responseListener) : new ServiceCommand(this, uri, payload, responseListener);

            request.Send();
            return request;
        }
        public void GetMute(ResponseListener listener)
        {
            const string method = "GetMute";
            const string instanceId = "0";
            const string channel = "Master";

            var parameters = new Dictionary<string, string> { { "Channel", channel } };

            var payload = GetMethodBody(RENDERING_CONTROL_URN, instanceId, method, parameters);

            var responseListener = new ResponseListener
            (
                loadEventArg =>
                {
                    var currentMute = Util.ParseData((String)loadEventArg, "CurrentMute");
                    var isMute = bool.Parse(currentMute);

                    Util.PostSuccess(listener, isMute);
                },
                serviceCommandError =>
                {
                    if (listener != null)
                        listener.OnError(serviceCommandError);
                }
            );

            var request = new ServiceCommand(this, method, payload, responseListener);
            request.Send();
        }
        public void CloseWebApp(LaunchSession launchSession, ResponseListener listener)
        {
            if (launchSession == null || launchSession.AppId == null || launchSession.AppId.Length == 0)
            {
                Util.PostError(listener, new ServiceCommandError(0, null));
                return;
            }

            var webAppSession = WebAppSessions.ContainsKey(launchSession.AppId) ? WebAppSessions[launchSession.AppId] : null;

            if (webAppSession != null && webAppSession.IsConnected())
            {
                var serviceCommand = new JsonObject();
                var closeCommand = new JsonObject();

                try
                {
                    serviceCommand.Add("type", JsonValue.CreateStringValue("close"));

                    closeCommand.Add("contentType", JsonValue.CreateStringValue("connectsdk.serviceCommand"));
                    closeCommand.Add("serviceCommand", serviceCommand);
                }
                catch (Exception ex)
                {
                    throw ex;
                }

                var responseListener = new ResponseListener
                (
                    loadEventArg =>
                    {
                        webAppSession.DisconnectFromWebApp();

                        if (listener != null)
                            listener.OnSuccess(loadEventArg);
                    },
                    serviceCommandError =>
                    {
                        webAppSession.DisconnectFromWebApp();

                        if (listener != null)
                            listener.OnError(serviceCommandError);
                    }
                );

                webAppSession.SendMessage(closeCommand, responseListener);
            }
            else
            {
                //if (webAppSession != null)
                //    webAppSession.DisconnectFromWebApp();

                const string uri = "ssap://webapp/closeWebApp";
                var payload = new JsonObject();

                try
                {
                    if (launchSession.AppId != null)
                        payload.Add("webAppId", JsonValue.CreateStringValue(launchSession.AppId));
                    if (launchSession.SessionId != null)
                        payload.Add("sessionId", JsonValue.CreateStringValue(launchSession.SessionId));
                }
                catch (Exception e)
                {
                    throw e;
                }

                var request = new ServiceCommand(this, uri, payload, listener);
                request.Send();
            }
        }
        public void GetPosition(ResponseListener listener)
        {
            var responseListener = new ResponseListener
            (
            loadEventArg =>
            {
                var s = LoadEventArgs.GetValue<string>(loadEventArg);
                var strDuration = Util.ParseData(s, "RelTime");

                var d = DateTime.ParseExact(strDuration, "HH:mm:ss", CultureInfo.InvariantCulture);
                var dmin = DateTime.ParseExact("00:00:00", "HH:mm:ss", CultureInfo.InvariantCulture);
                var longValue = d.Subtract(dmin).TotalMilliseconds;

                if (listener != null)
                {
                    listener.OnSuccess(longValue);
                }
            },
            serviceCommandError =>
            {
                if (listener != null)
                {
                    listener.OnError(serviceCommandError);
                }
            }
            );

            GetPositionInfo(responseListener);
        }
 public void PowerOn(ResponseListener listener)
 {
     if (listener != null)
         listener.OnError(ServiceCommandError.NotSupported());
 }
        public virtual void GetPlayState(ResponseListener listener)
        {
            IMediaControl mediaControl = null;

            if (Service != null)
                mediaControl = Service as IMediaControl;

            if (mediaControl != null)
                mediaControl.GetPlayState(listener);
            else if (listener != null)
                listener.OnError(ServiceCommandError.NotSupported());
        }