Beispiel #1
0
        protected override void WndProc(ref Message ThisMsg)
        {
            if (ThisMsg.Msg == WM_COPYDATA)
            {
                COPYDATASTRUCT mystr  = new COPYDATASTRUCT();
                Type           mytype = mystr.GetType();
                mystr = (COPYDATASTRUCT)ThisMsg.GetLParam(mytype);

                string msg   = GetMPCCommand((MPCAPI_COMMAND)mystr.dwData);
                string param = GetMPCCommandParamStr((MPCAPI_COMMAND)mystr.dwData, mystr.lpData);

                Debug.WriteLine(msg + " " + param);

                MPCAPI_COMMAND command = (MPCAPI_COMMAND)mystr.dwData.ToInt32();

                switch (command)
                {
                case MPCAPI_COMMAND.CMD_CONNECT:
                    MediaPlayerHandle = Convert.ToInt32(mystr.lpData);
                    NotifyConnected();
                    break;

                case MPCAPI_COMMAND.CMD_STATE:
                    MPC_LOADSTATE loadState = (MPC_LOADSTATE)int.Parse(mystr.lpData);
                    switch (loadState)
                    {
                    case MPC_LOADSTATE.MLS_CLOSED:
                        VideoFileName         = "";
                        VideoDurationMs       = 0;
                        VideoCurrentlyPlaying = false;
                        VideoCurrentlyOpen    = false;
                        NotifyClosed();
                        break;

                    case MPC_LOADSTATE.MLS_LOADING:
                        NotifyLoading();
                        break;

                    case MPC_LOADSTATE.MLS_LOADED:
                        VideoCurrentlyOpen = true;
                        PausedPositionMs   = 0;
                        NotifyLoaded();
                        // SendGetNowPlaying(); // request title info
                        break;

                    case MPC_LOADSTATE.MLS_CLOSING:
                        VideoCurrentlyPlaying = false;
                        NotifyClosing();
                        break;

                    default:
                        break;
                    }
                    break;

                case MPCAPI_COMMAND.CMD_PLAYMODE:
                    MPC_PLAYSTATE playState = (MPC_PLAYSTATE)int.Parse(mystr.lpData);
                    switch (playState)
                    {
                    case MPC_PLAYSTATE.PS_PLAY:
                        VideoCurrentlyPlaying  = true;
                        LastReportedPositionMs = -1;         // invalidate the reported position; paused position is better for now
                        ResyncTime();
                        NotifyPlay();
                        break;

                    case MPC_PLAYSTATE.PS_PAUSE:
                        PausedPositionMs      = CurrentPositionMs;    // set the paused position
                        VideoCurrentlyPlaying = false;
                        SendGetCurrentPosition();
                        NotifyPause();
                        break;

                    case MPC_PLAYSTATE.PS_STOP:
                        VideoCurrentlyPlaying = false;
                        PausedPositionMs      = 0;
                        NotifyStop();
                        break;

                    case MPC_PLAYSTATE.PS_UNUSED:
                        break;

                    default:
                        break;
                    }
                    break;

                case MPCAPI_COMMAND.CMD_NOWPLAYING:
                    // Par 1 : title
                    // Par 2 : author
                    // Par 3 : description
                    // Par 4 : complete filename (path included)
                    // Par 5 : duration in seconds
                    string[] parms    = mystr.lpData.Split('|');
                    int      duration = (int)(double.Parse(parms[4]) * 1000);
                    VideoFileName   = System.IO.Path.GetFileName(parms[3]);
                    VideoDurationMs = duration;
                    NotifyNowPlaying(parms[0], parms[1], parms[2], parms[3], duration);
                    ResyncTime();
                    break;

                case MPCAPI_COMMAND.CMD_LISTSUBTITLETRACKS:
                    break;

                case MPCAPI_COMMAND.CMD_LISTAUDIOTRACKS:
                    break;

                case MPCAPI_COMMAND.CMD_CURRENTPOSITION:
                {
                    try
                    {
                        int currentPos = int.Parse(mystr.lpData) * 1000;
                        ContinueResync(currentPos);
                    }
                    catch
                    {
                        LastReportedPositionMs = (int)(double.Parse(mystr.lpData) * 1000);
                        LastReportedDateTime   = DateTime.UtcNow;
                        PausedPositionMs       = LastReportedPositionMs;
                        needToDoResyncs        = false;
                    }
                }
                break;

                case MPCAPI_COMMAND.CMD_NOTIFYSEEK:
                {
                    try
                    {
                        int currentPos = int.Parse(mystr.lpData) * 1000;
                        LastReportedDateTime   = DateTime.UtcNow;
                        LastReportedPositionMs = currentPos;
                        PausedPositionMs       = currentPos;
                        ResyncTime();
                    }
                    catch
                    {
                        LastReportedPositionMs = (int)(float.Parse(mystr.lpData) * 1000);
                        LastReportedDateTime   = DateTime.UtcNow;
                        PausedPositionMs       = LastReportedPositionMs;
                        needToDoResyncs        = false;
                    }
                }
                break;

                case MPCAPI_COMMAND.CMD_NOTIFYENDOFSTREAM:
                    break;

                case MPCAPI_COMMAND.CMD_PLAYLIST:
                    break;

                case MPCAPI_COMMAND.CMD_DISCONNECT:
                    VideoFileName         = "";
                    VideoDurationMs       = 0;
                    VideoCurrentlyPlaying = false;
                    VideoCurrentlyOpen    = false;
                    MediaPlayerHandle     = 0;
                    NotifyDisconnected();
                    break;

                default:
                    Debug.WriteLine("Unhandled Command");
                    break;
                }

                return;
            }

            base.WndProc(ref ThisMsg);
        }
Beispiel #2
0
        public string GetMPCCommandParamStr(MPCAPI_COMMAND cmd, string lpData)
        {
            string param;

            if (lpData == null)
            {
                param = "(none)";
            }
            else
            {
                switch (cmd)
                {
                case MPCAPI_COMMAND.CMD_STATE:
                    MPC_LOADSTATE loadState = (MPC_LOADSTATE)int.Parse(lpData);

                    switch (loadState)
                    {
                    case MPC_LOADSTATE.MLS_CLOSED:
                        param = "MLS_CLOSED";
                        break;

                    case MPC_LOADSTATE.MLS_LOADING:
                        param = "MLS_LOADING";
                        break;

                    case MPC_LOADSTATE.MLS_LOADED:
                        param = "MLS_LOADED";
                        break;

                    case MPC_LOADSTATE.MLS_CLOSING:
                        param = "MLS_CLOSING";
                        break;

                    default:
                        param = "Unknown MPC_LOADSTATE: " + lpData;
                        break;
                    }

                    break;

                case MPCAPI_COMMAND.CMD_PLAYMODE:
                    MPC_PLAYSTATE playState = (MPC_PLAYSTATE)int.Parse(lpData);

                    switch (playState)
                    {
                    case MPC_PLAYSTATE.PS_PLAY:
                        param = "PS_PLAY";
                        break;

                    case MPC_PLAYSTATE.PS_PAUSE:
                        param = "PS_PAUSE";
                        break;

                    case MPC_PLAYSTATE.PS_STOP:
                        param = "PS_STOP";
                        break;

                    case MPC_PLAYSTATE.PS_UNUSED:
                        param = "PS_UNUSED";
                        break;

                    default:
                        param = "Unknown MPC_PLAYSTATE: " + lpData;
                        break;
                    }

                    break;

                default:
                    param = lpData;
                    break;
                }
            }
            return(param);
        }
        private void SendCommand(MPCAPI_COMMAND command, string p)
        {
            IntPtr lpData = Marshal.StringToCoTaskMemAuto(p);

            COPYDATASTRUCT cds;

            cds.dwData = new UIntPtr((uint) command);
            cds.lpData = lpData;
            cds.cbData = p.Length * Marshal.SystemDefaultCharSize;

            if (Handle == null)
                throw new InvalidOperationException("Unexpected Command: MPC not connected");

            User32Dll.SendMessage((IntPtr) Handle, (uint) WM.COPYDATA, _wHandle, ref cds);
        }
Beispiel #4
0
        public string GetMPCCommand(MPCAPI_COMMAND cmd)
        {
            string name;

            switch (cmd)
            {
            case MPCAPI_COMMAND.CMD_CONNECT:
                name = "CMD_CONNECT";
                break;

            case MPCAPI_COMMAND.CMD_STATE:
                name = "CMD_STATE";
                break;

            case MPCAPI_COMMAND.CMD_PLAYMODE:
                name = "CMD_PLAYMODE";
                break;

            case MPCAPI_COMMAND.CMD_NOWPLAYING:
                name = "CMD_NOWPLAYING";
                break;

            case MPCAPI_COMMAND.CMD_LISTSUBTITLETRACKS:
                name = "CMD_LISTSUBTITLETRACKS";
                break;

            case MPCAPI_COMMAND.CMD_LISTAUDIOTRACKS:
                name = "CMD_LISTAUDIOTRACKS";
                break;

            case MPCAPI_COMMAND.CMD_CURRENTPOSITION:
                name = "CMD_CURRENTPOSITION";
                break;

            case MPCAPI_COMMAND.CMD_NOTIFYSEEK:
                name = "CMD_NOTIFYSEEK";
                break;

            case MPCAPI_COMMAND.CMD_NOTIFYENDOFSTREAM:
                name = "CMD_NOTIFYENDOFSTREAM";
                break;

            case MPCAPI_COMMAND.CMD_VERSION:
                name = "CMD_VERSION";
                break;

            case MPCAPI_COMMAND.CMD_PLAYLIST:
                name = "CMD_PLAYLIST";
                break;

            case MPCAPI_COMMAND.CMD_DISCONNECT:
                name = "CMD_DISCONNECT";
                break;

            default:
                name = String.Format("Unknown Command: 0x{0:X8}", (int)cmd);
                break;
            }

            return(name);
        }
Beispiel #5
0
        private unsafe IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            if (msg == WinAPI.WM_COPYDATA)
            {
                WinAPI.COPYDATASTRUCT cds = (WinAPI.COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(WinAPI.COPYDATASTRUCT));

                if (cds.cbData > 0)
                {
                    MPCAPI_COMMAND nCmd   = (MPCAPI_COMMAND)cds.dwData;
                    string         mpcMSG = new String((char *)cds.lpData, 0, cds.cbData / 2 - 1);

                    switch (nCmd)
                    {
                    case MPCAPI_COMMAND.CMD_CONNECT:
                        isHooked  = true;
                        m_hWndMPC = new IntPtr(int.Parse(mpcMSG));
                        break;

                    case MPCAPI_COMMAND.CMD_CURRENTPOSITION:
                    case MPCAPI_COMMAND.CMD_NOTIFYSEEK:
                        m_currentFilePosition = double.Parse(mpcMSG);
                        if (!m_currentFileWatched && m_watchedWhen == MPC_WATCHED.DURING_TICKS &&
                            m_currentFileTick > m_currentFileLength * m_watchedWhenPerc)
                        {
                            OnFileWatched(this, new FileWatchedArgs(m_currentFilePath));
                            m_currentFileWatched = true;
                            m_PlayTimer.Stop();
                        }
                        break;

                    case MPCAPI_COMMAND.CMD_NOWPLAYING:
                        string[] playing = mpcMSG.Split('|');
                        m_currentFileTitle    = !string.IsNullOrWhiteSpace(playing[0]) ? playing[0] : System.IO.Path.GetFileName(playing[3]);
                        m_currentFilePath     = playing[3];
                        m_currentFileLength   = double.Parse(playing[4]);
                        m_currentFileWatched  = false;
                        m_currentFilePosition = m_currentFileTick = 0;
                        LoadConfig();
                        SetTitle();
                        break;

                    case MPCAPI_COMMAND.CMD_PLAYMODE:
                        m_currentPlayState = (MPC_PLAYSTATE)int.Parse(mpcMSG);
                        SetTitle();
                        switch (m_currentPlayState)
                        {
                        case MPC_PLAYSTATE.PS_PLAY:
                            if (!m_currentFileWatched)
                            {
                                m_PlayTimer.IsEnabled = true;
                            }
                            break;

                        case MPC_PLAYSTATE.PS_PAUSE:
                        case MPC_PLAYSTATE.PS_STOP:
                            m_PlayTimer.IsEnabled = false;
                            break;
                        }
                        break;

                    case MPCAPI_COMMAND.CMD_STATE:
                        MPC_LOADSTATE mpcLS = (MPC_LOADSTATE)int.Parse(mpcMSG);
                        switch (mpcLS)
                        {
                        case MPC_LOADSTATE.MLS_CLOSED:
                            IsMPCAlive();
                            break;

                        case MPC_LOADSTATE.MLS_CLOSING:
                            if (m_watchedWhen == MPC_WATCHED.AFTER_FINISHED && !m_currentFileWatched &&
                                m_currentFileTick > m_currentFileLength * m_watchedWhenPerc)
                            {
                                OnFileWatched(this, new FileWatchedArgs(m_currentFilePath));
                                m_currentFileWatched = true;
                                m_PlayTimer.Stop();
                            }
                            break;
                        }
                        break;
                    }
                }

                handled = true;
            }
            else
            {
                handled = false;
            }

            return(IntPtr.Zero);
        }