using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Timers;
using DPSDK_RES = System.Int32;


namespace DPSDK_CSharpDemo
{
    public partial class CSharpDemo : Form
    {
        int m_fileSize = 0;
        int m_timeFileSize = 0;
        bool m_bdownloadByTime = false;

        public delegate void DelDownloadProgressCallback(int iProgress, CSharpDemo userData);
        DelDownloadProgressCallback delDownloadProgressCallback = new DelDownloadProgressCallback(DownloadProgressCallback);

        public static void DownloadProgressCallback(int iProgress, CSharpDemo userdata)
        {
            userdata.SetProgressBar(iProgress);
        }

        public void SetProgressBar(int iProgress)
        {
            // At the end of the download, the iCurrentSize will be set 0
            if (iProgress >= 0)
            {
                this.downloadBar.Value = iProgress;
                this.lbTip.Text = "Download progress:" + Convert.ToString(iProgress) + "%";
            }
            else
            {
                if (iProgress == -1)
                {
                    textPlaybackResult.Text = "Download Finish!";
                    m_iDownloadMediaSessionID = IntPtr.Zero;
                }
                else if (iProgress == -2)
                {
                    //数据异常，不显示进度
                    //textPlaybackResult.Text = "Download error";
                }
            }
        }

        private void InitProgressBar()
        {
            this.downloadBar.Visible = true;
            this.downloadBar.Minimum = 0;
            this.downloadBar.Maximum = 100;
            this.downloadBar.Value = 0;
            this.lbTip.Text = "Download progress:";
        }

        // play back progress
        private void StartTimer()
        {
            m_timer.Elapsed += new ElapsedEventHandler(OnProgressTimer);
            m_timer.Enabled = true;
            m_timer.Start();
        }

        private void StopTimer()
        {
            m_timer.Elapsed -= new ElapsedEventHandler(OnProgressTimer);
            m_timer.Enabled = false;
            m_timer.Stop();
        }

        private void ResetPlaybackProgressBar(int iWindowID, bool bVisible)
        {
            switch (iWindowID)
            {
                case 1:
                    this.playbackProgressBar.Visible = bVisible;
                    this.playbackProgressBar.Minimum = 0;
                    this.playbackProgressBar.Maximum = 100;
                    this.playbackProgressBar.Value = 0;
                    break;
                case 2:
                    this.playbackProgressBar2.Visible = bVisible;
                    this.playbackProgressBar2.Minimum = 0;
                    this.playbackProgressBar2.Maximum = 100;
                    this.playbackProgressBar2.Value = 0;
                    break;
                case 3:
                    this.playbackProgressBar3.Visible = bVisible;
                    this.playbackProgressBar3.Minimum = 0;
                    this.playbackProgressBar3.Maximum = 100;
                    this.playbackProgressBar3.Value = 0;
                    break;
                case 4:
                    this.playbackProgressBar4.Visible = bVisible;
                    this.playbackProgressBar4.Minimum = 0;
                    this.playbackProgressBar4.Maximum = 100;
                    this.playbackProgressBar4.Value = 0;
                    break;
                default:
                    break;
            }
        }

        private Int64 GetPlayedTime(IntPtr playSessionID)
        {
            IntPtr pTime = IntPtr.Zero;
            int iRet = DPSDK_GetPlayedTime(m_iSessionID, playSessionID, ref pTime);
            if (DPSDK_SUCCESS != iRet)
            {
                textPlaybackResult.Text = "DPSDK_GetPlayedTime failed! Error Code:" + iRet.ToString();
            }
            Int64 iPlayedTime = pTime.ToInt64();
            if (iPlayedTime < 0)
            {
                //textPlaybackResult.Text = "DPSDK_GetPlayedTime time error!";
                iPlayedTime = 0;
            }
            return iPlayedTime;
        }


        private void OnProgressTimer(object sender, ElapsedEventArgs e)
        {
            if (m_mapPlaybackTime.Count == 0)
            {
                StopTimer();
                return;
            }

            foreach (KeyValuePair<int, Int64> pair in m_mapPlaybackTime)
            {
                int iWindowID = pair.Key;
                Int64 iTotalTime = Convert.ToInt64(m_mapPlaybackTime[iWindowID]);

                switch (iWindowID)
                {
                    case 1:
                        if (m_bLocalPlayback)
                        {
                            float pPos = 0;
                            DPSDK_GetLocalPlaybackPos(m_iSessionID, m_iPlaybackMediaSessionID, ref pPos);
                            SetPlaybackProgressBar(iWindowID, pPos);
                        }
                        else
                        {
                            SetPlaybackProgressBar(iWindowID, GetPlayedTime(m_iPlaybackMediaSessionID), iTotalTime);
                        }
                        break;
                    case 2:
                        if (m_bLocalPlayback2)
                        {
                            float pPos = 0;
                            DPSDK_GetLocalPlaybackPos(m_iSessionID, m_iPlaybackMediaSessionID2, ref pPos);
                            SetPlaybackProgressBar(iWindowID, pPos);
                        }
                        else
                        { 
                            SetPlaybackProgressBar(iWindowID, GetPlayedTime(m_iPlaybackMediaSessionID2), iTotalTime);
                        }
                        break;
                    case 3:
                        if (m_bLocalPlayback3)
                        {
                            float pPos = 0;
                            DPSDK_GetLocalPlaybackPos(m_iSessionID, m_iPlaybackMediaSessionID3, ref pPos);
                            SetPlaybackProgressBar(iWindowID, pPos);
                        }
                        else
                        {
                            SetPlaybackProgressBar(iWindowID, GetPlayedTime(m_iPlaybackMediaSessionID3), iTotalTime);
                        }
                        break;
                    case 4:
                        if (m_bLocalPlayback4)
                        {
                            float pPos = 0;
                            DPSDK_GetLocalPlaybackPos(m_iSessionID, m_iPlaybackMediaSessionID4, ref pPos);
                            SetPlaybackProgressBar(iWindowID, pPos);
                        }
                        else
                        {
                            SetPlaybackProgressBar(iWindowID, GetPlayedTime(m_iPlaybackMediaSessionID4), iTotalTime);
                        }
                        break;
                    default:
                        break;
                }
            }
        }

        // local file play back
        private void SetPlaybackProgressBar(int iWindowId, float fPos)
        {
            switch (iWindowId)
            {
                case 1:
                    this.playbackProgressBar.Value = (int)(fPos * 100);
                    break;
                case 2:
                    this.playbackProgressBar2.Value = (int)(fPos * 100);
                    break;
                case 3:
                    this.playbackProgressBar3.Value = (int)(fPos * 100);
                    break;
                case 4:
                    this.playbackProgressBar4.Value = (int)(fPos * 100);
                    break;
                default:
                    break;
            }
        }

        // file play back
        private void SetPlaybackProgressBar(int iWindowID, Int64 iPlayedTime, Int64 iTotalTime)
        {
            Double numPercent = (Double)iPlayedTime / (Double)iTotalTime * 100;

            switch (iWindowID)
            {
                case 1:
                    this.playbackProgressBar.Value = (int)numPercent;
                    break;
                case 2:
                    this.playbackProgressBar2.Value = (int)numPercent;
                    break;
                case 3:
                    this.playbackProgressBar3.Value = (int)numPercent;
                    break;
                case 4:
                    this.playbackProgressBar4.Value = (int)numPercent;
                    break;
                default:
                    break;
            }
        }

        public Dictionary<int, Int64> m_mapPlaybackTime = new Dictionary<int, Int64>();
        private void AddToTimeMap(int iKey, Int64 iValue)
        {
            if (!m_mapPlaybackTime.ContainsKey(iKey))
            {
                m_mapPlaybackTime.Add(iKey, iValue);
            }
            else
            {
                m_mapPlaybackTime[iKey] = iValue;
            }
        }

        private void RemoveFromTimeMap(int iKey)
        {
            if (m_mapPlaybackTime.ContainsKey(iKey))
            {
                m_mapPlaybackTime.Remove(iKey);
            }
        }

        public Dictionary<int, DPSDK_PLAYBACK_BY_TIME_PARAM> m_mapPlayback = new Dictionary<int, DPSDK_PLAYBACK_BY_TIME_PARAM>();

        public void FuncReStartPlayback(string id, int status, int eventType)
        {
            // on-line:1, Off-line:2, see DPSDK_DEV_STATUS
            if (status == 2 || eventType == DPSDK_EVENT_SERVER_OFFLINE)
            {
                RecordPauseTime(id, status, eventType);
            }
            else if (status == 1 || eventType == DPSDK_EVENT_RELOGIN_SUCCESS)
            {
                ReStartPlayback(id, status, eventType);
            }
        }

        public void ReStartPlayback(string id, int status, int eventType)
        {
            if (eventType == DPSDK_EVENT_DEVICE_STATUS)
            {
                foreach (KeyValuePair<int, DPSDK_PLAYBACK_BY_TIME_PARAM> pair in m_mapPlayback)
                {
                    DPSDK_PLAYBACK_BY_TIME_PARAM tmpParam = pair.Value;
                    int tmpWindowID = pair.Key;
                    if (id == TakeDeviceID(tmpParam.szCodeId))
                    {
                        ReStartPlaybackByTime(tmpWindowID, tmpParam);
                    }
                }
            }
            else if (eventType == DPSDK_EVENT_CHANNEL_STATUS)
            {
                foreach (KeyValuePair<int, DPSDK_PLAYBACK_BY_TIME_PARAM> pair in m_mapPlayback)
                {
                    DPSDK_PLAYBACK_BY_TIME_PARAM tmpParam = pair.Value;
                    int tmpWindowID = pair.Key;
                    if (id == tmpParam.szCodeId)
                    {
                        ReStartPlaybackByTime(tmpWindowID, tmpParam);
                    }
                }
            }
            else if (eventType == DPSDK_EVENT_RELOGIN_SUCCESS)
            {
                foreach (KeyValuePair<int, DPSDK_PLAYBACK_BY_TIME_PARAM> pair in m_mapPlayback)
                {
                    DPSDK_PLAYBACK_BY_TIME_PARAM tmpParam = pair.Value;
                    int tmpWindowID = pair.Key;
                    ReStartPlaybackByTime(tmpWindowID, tmpParam);
                }
            }
        }

        public void ReStartPlaybackByTime(int num, DPSDK_PLAYBACK_BY_TIME_PARAM param)
        {
            switch (num)
            {
                case 1:
                    {
                        DPSDK_StopPlayback(m_iSessionID, m_iPlaybackMediaSessionID);
                        m_iPlaybackMediaSessionID = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                        DPSDK_StartPlaybackByTimeEx(m_iSessionID, ref param,  m_iPlaybackMediaSessionID);
                    }
                    break;
                case 2:
                    {
                        DPSDK_StopPlayback(m_iSessionID, m_iPlaybackMediaSessionID2);
                        m_iPlaybackMediaSessionID2 = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                        DPSDK_StartPlaybackByTimeEx(m_iSessionID, ref param,  m_iPlaybackMediaSessionID2);
                    }
                    break;
                case 3:
                    {
                        DPSDK_StopPlayback(m_iSessionID, m_iPlaybackMediaSessionID3);
                        m_iPlaybackMediaSessionID3 = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                        DPSDK_StartPlaybackByTimeEx(m_iSessionID, ref param,  m_iPlaybackMediaSessionID3);
                    }
                    break;
                case 4:
                    {
                        DPSDK_StopPlayback(m_iSessionID, m_iPlaybackMediaSessionID4);
                        m_iPlaybackMediaSessionID4 = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                        DPSDK_StartPlaybackByTimeEx(m_iSessionID, ref param, m_iPlaybackMediaSessionID4);
                    }
                    break;
                default:
                    break;
            }

        }

        public void RecordPauseTime(string id, int status, int eventType)
        {
            if (eventType == DPSDK_EVENT_DEVICE_STATUS)
            {
                foreach (KeyValuePair<int, DPSDK_PLAYBACK_BY_TIME_PARAM> pair in m_mapPlayback)
                {
                    DPSDK_PLAYBACK_BY_TIME_PARAM tmpParam = pair.Value;
                    int tmpWindowID = pair.Key;
                    if (id == TakeDeviceID(tmpParam.szCodeId))
                    {
                        IntPtr tmpTime = IntPtr.Zero;
                        Int64 curTime = GetPauseTime(tmpWindowID, tmpTime);
                        tmpParam.tPlayTime = curTime;
                    }

                    m_mapPlayback[tmpWindowID] = tmpParam;
                }
            }
            else if (eventType == DPSDK_EVENT_CHANNEL_STATUS)
            {
                foreach (KeyValuePair<int, DPSDK_PLAYBACK_BY_TIME_PARAM> pair in m_mapPlayback)
                {
                    DPSDK_PLAYBACK_BY_TIME_PARAM tmpParam = pair.Value;
                    int tmpWindowID = pair.Key;
                    if (id == tmpParam.szCodeId)
                    {
                        IntPtr tmpTime = IntPtr.Zero;
                        Int64 curTime = GetPauseTime(tmpWindowID, tmpTime);
                        tmpParam.tPlayTime = curTime;
                    }

                    m_mapPlayback[tmpWindowID] = tmpParam;
                }
            }
            else if (eventType == DPSDK_EVENT_SERVER_OFFLINE)
            {
                foreach (KeyValuePair<int, DPSDK_PLAYBACK_BY_TIME_PARAM> pair in m_mapPlayback)
                {
                    DPSDK_PLAYBACK_BY_TIME_PARAM tmpParam = pair.Value;
                    int tmpWindowID = pair.Key;

                    IntPtr tmpTime = IntPtr.Zero;
                    Int64 curTime = GetPauseTime(tmpWindowID, tmpTime);
                    tmpParam.tPlayTime = curTime;

                    m_mapPlayback[tmpWindowID] = tmpParam;
                }
            }
        }

        public Int64 GetPauseTime(int num, IntPtr time)
        {
            Int64 res = 0;
            switch (num)
            {
                case 1:
                    {
                        res = GetPlayedTime(m_iPlaybackMediaSessionID);
                        DPSDK_PlaybackPause(m_iSessionID, m_iPlaybackMediaSessionID);
                    }
                    break;
                case 2:
                    {
                        res = GetPlayedTime(m_iPlaybackMediaSessionID2);
                        DPSDK_PlaybackPause(m_iSessionID, m_iPlaybackMediaSessionID2);
                    }
                    break;
                case 3:
                    {
                        res = GetPlayedTime(m_iPlaybackMediaSessionID3);
                        DPSDK_PlaybackPause(m_iSessionID, m_iPlaybackMediaSessionID3);
                    }
                    break;
                case 4:
                    {
                        res = GetPlayedTime(m_iPlaybackMediaSessionID4);
                        DPSDK_PlaybackPause(m_iSessionID, m_iPlaybackMediaSessionID4);
                    }
                    break;
                default:
                    break;
            }

            return res;
        }

        private void trackBarPlaybackVolume_ValueChanged(object sender, EventArgs e)
        {
            labelPlaybackVolume.Text = trackBarPlaybackVolume.Value.ToString();
        }

        void fEventDownloadCallback(IntPtr iEventType, IntPtr iMediaSessionID, IntPtr pData, IntPtr pUserParam)
        {
            switch (iEventType.ToInt32())
            {
                case DPSDK_EVENT_DOWNLOAD_FILE_STARTPLAYBACK:
                    {
                        //Start download by file
                        if (pData != IntPtr.Zero)
                        {
                            textPlaybackResult.Text = "Start Download Record By File.";
                        }
                    }
                    break;
                case DPSDK_EVENT_DOWNLOAD_TIME_STARTPLAYBACK:
                    {
                        //Start download by time
                        if (pData != IntPtr.Zero)
                        {
                            textPlaybackResult.Text = "Start Download Record By Time.";
                        }
                    }
                    break;
                case DPSDK_EVENT_DOWNLOAD_PROGRESS:
                    {
                        //Download progress
                        if (pData != IntPtr.Zero)
                        {
                            DPSDK_DOWNLOAD_NOTIFY struDownloadNotify = new DPSDK_DOWNLOAD_NOTIFY();
                            struDownloadNotify = (DPSDK_DOWNLOAD_NOTIFY)Marshal.PtrToStructure(pData, typeof(DPSDK_DOWNLOAD_NOTIFY));
                            int iProgress = 0;
                            if (struDownloadNotify.struProgress.uiTotalSize > 0)
                            {
                                iProgress = (int)((100 * struDownloadNotify.struProgress.uiDownloadSize) / struDownloadNotify.struProgress.uiTotalSize);
                            }
                            else
                            {
                                iProgress = -2;
                            }
                            if (struDownloadNotify.iResult == -1)
                            {
                                iProgress = -1;
                            }
                            this.BeginInvoke(delDownloadProgressCallback, iProgress, this);
                        }
                    }
                    break;
                case DPSDK_EVENT_DOWNLOAD_CUT_FILE:
                    {
                        //when the download file is so big that it may cut into pieces, it is not appropriate to set the end message here.
                    }
                    break;
                default:
                    break;
            }
        }

        private Int64 ParseDateTime(DateTime dtTime)
        {
            DateTime CurTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            TimeSpan toNow = new TimeSpan(dtTime.Ticks - CurTime.Ticks);
            return Convert.ToInt64(toNow.TotalSeconds);
        }

        private Int64 GetTimeStamp(String strDateTime)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));  
            System.Globalization.DateTimeFormatInfo dtFormat = new System.Globalization.DateTimeFormatInfo();
            dtFormat.LongDatePattern = "yyyy/MM/dd HH:mm:ss";
            DateTime dtTemp = DateTime.Parse(strDateTime, dtFormat);
            Int64 timeStamp = Convert.ToInt64(dtTemp.Subtract(dtStart).TotalSeconds);
            return timeStamp;
        }

        private String TimeStampToDateTime(Int64 iTimeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));  
            iTimeStamp = iTimeStamp * 1000;
            DateTime targetDt = dtStart.AddMilliseconds(iTimeStamp);
            return targetDt.ToString("yyyy/MM/dd HH:mm:ss");
        }

        private void btnPlaybackQueryRecord_Click(object sender, EventArgs e)
        {
            listViewPlaybackRecordInfo.Items.Clear();
            m_fileSize = 0;
            m_timeFileSize = 0;

            DPSDK_QUERY_RECORD_PARAM pQueryRecord = new DPSDK_QUERY_RECORD_PARAM();
            pQueryRecord.szCameraId = textPlaybackDeviceID.Text;
            pQueryRecord.iRecordType = (DPSDK_RECORD_TYPE)Convert.ToInt32(comboBoxPlaybackQueryRecordType.Text.Substring(0, comboBoxPlaybackQueryRecordType.Text.IndexOf("-")));
            pQueryRecord.iSourceType = (DPSDK_SOURCE_TYPE)(Convert.ToInt32(comboBoxPlaybackSourceType.Text.Substring(0, comboBoxPlaybackSourceType.Text.IndexOf("-"))) + 1);
            pQueryRecord.iStreamType = (DPSDK_STREAM_TYPE)Convert.ToInt32(comboBoxPlaybackStreamType.Text.Substring(0, comboBoxPlaybackStreamType.Text.IndexOf("-")));
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            pQueryRecord.tBeginTime = Convert.ToInt64(datePlaybackStartTime.Value.Subtract(dtStart).TotalSeconds);
            pQueryRecord.tEndTime = Convert.ToInt64(datePlaybackEndTime.Value.Subtract(dtStart).TotalSeconds);

            if (pQueryRecord.iSourceType == DPSDK_SOURCE_TYPE.DPSDK_SOURCE_TYPE_CENTER)
            {
                pQueryRecord.uiPage = 1;
                pQueryRecord.szSession = System.Guid.NewGuid().ToString("N");
            }

            int iMaxCount = 1000;
            uint uiBufLen = (uint)(Marshal.SizeOf(typeof(DPSDK_RECORD_INFO_LIST)) - Marshal.SizeOf(typeof(IntPtr)) + Marshal.SizeOf(typeof(DPSDK_SINGLE_RECORD_INFO)) * iMaxCount);
            IntPtr pRecordList = Marshal.AllocHGlobal((int)uiBufLen);
            DPSDK_RES iRet = DPSDK_QueryRecord(m_iSessionID, ref pQueryRecord, pRecordList, uiBufLen);

            if (iRet == DPSDK_SUCCESS)
            {
                textPlaybackResult.Text = "QueryRecord success";
                DPSDK_RECORD_INFO_LIST stuRecordList = new DPSDK_RECORD_INFO_LIST();
                stuRecordList = (DPSDK_RECORD_INFO_LIST)Marshal.PtrToStructure(pRecordList, typeof(DPSDK_RECORD_INFO_LIST));

                // center record
                if (pQueryRecord.iSourceType == DPSDK_SOURCE_TYPE.DPSDK_SOURCE_TYPE_CENTER && stuRecordList.iPageCount > 0)
                {
                    // first page
                    int iStartNumber = listViewPlaybackRecordInfo.Items.Count;
                    for (int i = 0; i < stuRecordList.iRetCount; i++)
                    {
                        DPSDK_SINGLE_RECORD_INFO pSingleRecord = new DPSDK_SINGLE_RECORD_INFO();
                        IntPtr ptr = new IntPtr(pRecordList.ToInt32() + Marshal.SizeOf(typeof(DPSDK_RECORD_INFO_LIST)) - Marshal.SizeOf(typeof(IntPtr)) + Marshal.SizeOf(typeof(DPSDK_SINGLE_RECORD_INFO)) * i);
                        pSingleRecord = (DPSDK_SINGLE_RECORD_INFO)Marshal.PtrToStructure(ptr, typeof(DPSDK_SINGLE_RECORD_INFO));

                        ListViewItem item = listViewPlaybackRecordInfo.Items.Add(Convert.ToString(iStartNumber + i + 1));
                        item.SubItems.Add(pSingleRecord.iSourceType.ToString());
                        item.SubItems.Add(pSingleRecord.iRecordType.ToString());
                        //item.SubItems.Add(pSingleRecord.iStartTime.ToString());
                        //item.SubItems.Add(pSingleRecord.iEndTime.ToString());
                        item.SubItems.Add(TimeStampToDateTime(pSingleRecord.iStartTime));
                        item.SubItems.Add(TimeStampToDateTime(pSingleRecord.iEndTime));
                        item.SubItems.Add(pSingleRecord.szName);
                        item.SubItems.Add(pSingleRecord.iLength.ToString());
                        item.SubItems.Add(pSingleRecord.iStreamType.ToString());
                        item.SubItems.Add(pSingleRecord.bLocked.ToString());
                        item.SubItems.Add(pSingleRecord.szSSId);
                        item.SubItems.Add(pSingleRecord.iFileHandle.ToString());
                        item.SubItems.Add(pSingleRecord.szDiskId);
                    }

                    // remain pages
                    for (int i = 1; i < stuRecordList.iPageCount; ++i)
                    {
                        uint uiBufLenEx = (uint)(Marshal.SizeOf(typeof(DPSDK_RECORD_INFO_LIST)) - Marshal.SizeOf(typeof(IntPtr)) + Marshal.SizeOf(typeof(DPSDK_SINGLE_RECORD_INFO)) * iMaxCount);
                        IntPtr pRecordListEx = Marshal.AllocHGlobal((int)uiBufLenEx);
                        pQueryRecord.uiPage += 1;
                        DPSDK_RES iRetEx = DPSDK_QueryRecord(m_iSessionID, ref pQueryRecord, pRecordListEx, uiBufLenEx);

                        if (iRetEx == DPSDK_SUCCESS)
                        {
                            DPSDK_RECORD_INFO_LIST stuRecordListEx = new DPSDK_RECORD_INFO_LIST();
                            stuRecordListEx = (DPSDK_RECORD_INFO_LIST)Marshal.PtrToStructure(pRecordListEx, typeof(DPSDK_RECORD_INFO_LIST));

                            int iStartNumberEx = listViewPlaybackRecordInfo.Items.Count;
                            for (int k = 0; k < stuRecordListEx.iRetCount; k++)
                            {
                                DPSDK_SINGLE_RECORD_INFO pSingleRecord = new DPSDK_SINGLE_RECORD_INFO();
                                IntPtr ptr = new IntPtr(pRecordList.ToInt32() + Marshal.SizeOf(typeof(DPSDK_RECORD_INFO_LIST)) - Marshal.SizeOf(typeof(IntPtr)) + Marshal.SizeOf(typeof(DPSDK_SINGLE_RECORD_INFO)) * i);
                                pSingleRecord = (DPSDK_SINGLE_RECORD_INFO)Marshal.PtrToStructure(ptr, typeof(DPSDK_SINGLE_RECORD_INFO));

                                ListViewItem item = listViewPlaybackRecordInfo.Items.Add(Convert.ToString(iStartNumberEx + k + 1));
                                item.SubItems.Add(pSingleRecord.iSourceType.ToString());
                                item.SubItems.Add(pSingleRecord.iRecordType.ToString());
                                //item.SubItems.Add(pSingleRecord.iStartTime.ToString());
                                //item.SubItems.Add(pSingleRecord.iEndTime.ToString());
                                item.SubItems.Add(TimeStampToDateTime(pSingleRecord.iStartTime));
                                item.SubItems.Add(TimeStampToDateTime(pSingleRecord.iEndTime));
                                item.SubItems.Add(pSingleRecord.szName);
                                item.SubItems.Add(pSingleRecord.iLength.ToString());
                                item.SubItems.Add(pSingleRecord.iStreamType.ToString());
                                item.SubItems.Add(pSingleRecord.bLocked.ToString());
                                item.SubItems.Add(pSingleRecord.szSSId);
                                item.SubItems.Add(pSingleRecord.iFileHandle.ToString());
                                item.SubItems.Add(pSingleRecord.szDiskId);
                            }

                            Marshal.FreeHGlobal(pRecordListEx);
                        }
                    }
                }

                // device record
                if (pQueryRecord.iSourceType == DPSDK_SOURCE_TYPE.DPSDK_SOURCE_TYPE_DEVICE)
                {
                    int iStartNumber = listViewPlaybackRecordInfo.Items.Count;
                    for (int i = 0; i < (int)stuRecordList.iRetCount; i++)
                    {
                        DPSDK_SINGLE_RECORD_INFO pSingleRecord = new DPSDK_SINGLE_RECORD_INFO();
                        IntPtr ptr = new IntPtr(pRecordList.ToInt32() + Marshal.SizeOf(typeof(DPSDK_RECORD_INFO_LIST)) - Marshal.SizeOf(typeof(IntPtr)) + Marshal.SizeOf(typeof(DPSDK_SINGLE_RECORD_INFO)) * i);
                        pSingleRecord = (DPSDK_SINGLE_RECORD_INFO)Marshal.PtrToStructure(ptr, typeof(DPSDK_SINGLE_RECORD_INFO));

                        ListViewItem item = listViewPlaybackRecordInfo.Items.Add(Convert.ToString(iStartNumber + i + 1));
                        item.SubItems.Add(pSingleRecord.iSourceType.ToString());
                        item.SubItems.Add(pSingleRecord.iRecordType.ToString());
                        //item.SubItems.Add(pSingleRecord.iStartTime.ToString());
                        //item.SubItems.Add(pSingleRecord.iEndTime.ToString());
                        item.SubItems.Add(TimeStampToDateTime(pSingleRecord.iStartTime));
                        item.SubItems.Add(TimeStampToDateTime(pSingleRecord.iEndTime));
                        item.SubItems.Add(pSingleRecord.szName);
                        item.SubItems.Add(pSingleRecord.iLength.ToString());
                        item.SubItems.Add(pSingleRecord.iStreamType.ToString());
                        item.SubItems.Add(pSingleRecord.bLocked.ToString());
                        item.SubItems.Add(pSingleRecord.szSSId);
                        item.SubItems.Add(pSingleRecord.iFileHandle.ToString());
                        item.SubItems.Add(pSingleRecord.szDiskId);
                    }
                }
            }
            else
            {
                textPlaybackResult.Text = "QueryRecord failed, error code = " + iRet.ToString();
            }
            Marshal.FreeHGlobal(pRecordList);
        }

        private void btnPlaybackQueryRecordDate_Click(object sender, EventArgs e)
        {
            DPSDK_QUERY_RECORD_DATE_PARAM pQueryDateInfo = new DPSDK_QUERY_RECORD_DATE_PARAM();
            pQueryDateInfo.szCameraId = textPlaybackDeviceID.Text;
            pQueryDateInfo.iSourceType = (DPSDK_SOURCE_TYPE)(Convert.ToInt32(comboBoxPlaybackSourceType.Text.Substring(0, comboBoxPlaybackSourceType.Text.IndexOf("-"))) + 1);
            pQueryDateInfo.iYear = datePlaybackStartTime.Value.Year;
            pQueryDateInfo.iMonth = datePlaybackStartTime.Value.Month;
            DPSDK_RECORD_DATE_INFO pRecordDate = new DPSDK_RECORD_DATE_INFO();
            DPSDK_RES iRet = DPSDK_QueryRecordDate(m_iSessionID, ref pQueryDateInfo, ref pRecordDate);
            if (iRet == DPSDK_SUCCESS)
            {
                string strRecordDays = "";
                for (int i = 0; i < 31; i++)
                {
                    strRecordDays += pRecordDate.RecordDays[i].ToString();
                }
                textPlaybackResult.Text = "QueryRecordDate success, RecordDays = " + strRecordDays;
            }
            else
            {
                textPlaybackResult.Text = "QueryRecordDate failed, error code = " + iRet.ToString();
            }
        }

        private void btnPlaybackGetRecordStatus_Click(object sender, EventArgs e)
        {
            DPSDK_RECORD_STATUS_INFO pRecordInfo = new DPSDK_RECORD_STATUS_INFO();
            DPSDK_RES iRet = DPSDK_GetRecordStatus(m_iSessionID, textPlaybackDeviceID.Text, ref pRecordInfo);
            if (iRet == DPSDK_SUCCESS)
            {
                textPlaybackResult.Text = "GetRecordStatus success, iRecordStatus = " + pRecordInfo.iRecordStatus.ToString();
            }
            else
            {
                textPlaybackResult.Text = "GetRecordStatus failed, error code = " + iRet.ToString();
            }
        }

        private void btnPlaybackLockRecordFile_Click(object sender, EventArgs e)
        {
            DPSDK_LOCK_RECORD_FILE_PARAM pLockFileInfo = new DPSDK_LOCK_RECORD_FILE_PARAM();
            pLockFileInfo.szCameraId = textPlaybackDeviceID.Text;
            if (listViewPlaybackRecordInfo.SelectedIndices.Count > 0)
            {
                pLockFileInfo.szFilename = listViewPlaybackRecordInfo.SelectedItems[0].SubItems[5].Text;
                pLockFileInfo.szFilename = pLockFileInfo.szFilename.Replace("efs://", "");
            }
            else
            {
                AppData.ShowMsgInfo("Please select one record!");
                return;
            }
            DPSDK_LOCK_RECORD_FILE_RESULT pResult = new DPSDK_LOCK_RECORD_FILE_RESULT();
            DPSDK_RES iRet = DPSDK_LockRecordFile(m_iSessionID, ref pLockFileInfo, ref pResult);
            if (iRet == DPSDK_SUCCESS)
            {
                textPlaybackResult.Text = "LockRecordFile success, iLockNum = " + pResult.iLockNum.ToString();
            }
            else
            {
                textPlaybackResult.Text = "LockRecordFile failed, error code = " + iRet.ToString();
            }
        }

        private void btnPlaybackUnlockRecordFile_Click(object sender, EventArgs e)
        {
            DPSDK_UNLOCK_RECORD_FILE_PARAM pUnlockFileInfo = new DPSDK_UNLOCK_RECORD_FILE_PARAM();
            pUnlockFileInfo.szCameraId = textPlaybackDeviceID.Text;
            if (listViewPlaybackRecordInfo.SelectedIndices.Count > 0)
            {
                pUnlockFileInfo.szFilename = listViewPlaybackRecordInfo.SelectedItems[0].SubItems[5].Text;
                pUnlockFileInfo.szFilename = pUnlockFileInfo.szFilename.Replace("efs://", "");
            }
            else
            {
                AppData.ShowMsgInfo("Please select one record!");
                return;
            }
            pUnlockFileInfo.bForce = (byte)(checkBoxPlaybackForce.CheckState == CheckState.Checked ? 1 : 0);
            DPSDK_LOCK_RECORD_FILE_RESULT pResult = new DPSDK_LOCK_RECORD_FILE_RESULT();
            DPSDK_RES iRet = DPSDK_UnlockRecordFile(m_iSessionID, ref pUnlockFileInfo, ref pResult);
            if (iRet == DPSDK_SUCCESS)
            {
                textPlaybackResult.Text = "UnlockRecordFile success, iLockNum = " + pResult.iLockNum.ToString();
            }
            else
            {
                textPlaybackResult.Text = "UnlockRecordFile failed, error code = " + iRet.ToString();
            }
        }

        private void btnPlaybackQueryLockRecord_Click(object sender, EventArgs e)
        {
            DPSDK_QUERY_LOCK_RECORD_PARAM pQueryLockRecord = new DPSDK_QUERY_LOCK_RECORD_PARAM();
            pQueryLockRecord.szUserId = textPlaybackUserID.Text;
            pQueryLockRecord.szCameraId = textPlaybackDeviceID.Text;
            pQueryLockRecord.iSourceType = (DPSDK_SOURCE_TYPE)(Convert.ToInt32(comboBoxPlaybackSourceType.Text.Substring(0, comboBoxPlaybackSourceType.Text.IndexOf("-"))) + 1);
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            pQueryLockRecord.tStartTime = Convert.ToInt64(datePlaybackStartTime.Value.Subtract(dtStart).TotalSeconds);
            pQueryLockRecord.tEndTime = Convert.ToInt64(datePlaybackEndTime.Value.Subtract(dtStart).TotalSeconds);

            uint uiBufLen = (uint)(64 + Marshal.SizeOf(typeof(int)) + Marshal.SizeOf(typeof(DPSDK_SINGLE_RECORD_LOCK_INFO)) * 10);
            IntPtr pLockRecordList = Marshal.AllocHGlobal((int)uiBufLen);
            DPSDK_RES iRet = DPSDK_QueryLockRecord(m_iSessionID, ref pQueryLockRecord, pLockRecordList, uiBufLen);
            if (iRet == DPSDK_SUCCESS)
            {
                DPSDK_RECORD_LOCK_INFO_LIST stuLockRecordList = new DPSDK_RECORD_LOCK_INFO_LIST();
                stuLockRecordList = (DPSDK_RECORD_LOCK_INFO_LIST)Marshal.PtrToStructure(pLockRecordList, typeof(DPSDK_RECORD_LOCK_INFO_LIST));
                textPlaybackResult.Text = "QueryLockRecord success, iRetCount = " + stuLockRecordList.iRetCount.ToString();
            }
            else
            {
                textPlaybackResult.Text = "QueryLockRecord failed, error code = " + iRet.ToString();
            }
            Marshal.FreeHGlobal(pLockRecordList);
        }

        private void btnPlaybackStopRemoteRecord_Click(object sender, EventArgs e)
        {
            DPSDK_PTZOPERATE_STOPREMOTERECORD_PARAM pStopRemoteRecordParam = new DPSDK_PTZOPERATE_STOPREMOTERECORD_PARAM();
            pStopRemoteRecordParam.szChannelId = textPlaybackDeviceID.Text;
            pStopRemoteRecordParam.iStreamType = Convert.ToInt32(comboBoxPlaybackStreamType.Text.Substring(0, comboBoxPlaybackStreamType.Text.IndexOf("-")));
            pStopRemoteRecordParam.bForce = (byte)(checkBoxPlaybackForce.CheckState == CheckState.Checked ? 1 : 0);
            DPSDK_PTZOPERATE_REMOTERECORD_RESULT pStopRemoteRecordResult = new DPSDK_PTZOPERATE_REMOTERECORD_RESULT();
            DPSDK_RES iRet = DPSDK_StopRemoteRecord(m_iSessionID, ref pStopRemoteRecordParam, ref pStopRemoteRecordResult);
            if (iRet == DPSDK_SUCCESS)
            {
                textPlaybackResult.Text = "StopRemoteRecord success";
            }
            else
            {
                textPlaybackResult.Text = "StopRemoteRecord failed, error code = " + iRet.ToString();
            }
        }

        private void btnPlaybackStartRemoteRecord_Click(object sender, EventArgs e)
        {
            if (textPlaybackDuration.Text == "")
            {
                AppData.ShowMsgInfo("Please input correct params!");
                return;
            }
            DPSDK_PTZOPERATE_STARTREMOTERECORD_PARAM pStartRemoteRecordParam = new DPSDK_PTZOPERATE_STARTREMOTERECORD_PARAM();
            pStartRemoteRecordParam.szChannelId = textPlaybackDeviceID.Text;
            pStartRemoteRecordParam.iStreamType = Convert.ToInt32(comboBoxPlaybackStreamType.Text.Substring(0, comboBoxPlaybackStreamType.Text.IndexOf("-")));
            pStartRemoteRecordParam.iRecordDuration = Convert.ToInt32(textPlaybackDuration.Text);
            DPSDK_PTZOPERATE_REMOTERECORD_RESULT pStartRemoteRecordResult = new DPSDK_PTZOPERATE_REMOTERECORD_RESULT();
            DPSDK_RES iRet = DPSDK_StartRemoteRecord(m_iSessionID, ref pStartRemoteRecordParam, ref pStartRemoteRecordResult);
            if (iRet == DPSDK_SUCCESS)
            {
                textPlaybackResult.Text = "StartRemoteRecord success";
            }
            else
            {
                textPlaybackResult.Text = "StartRemoteRecord failed, error code = " + iRet.ToString();
            }
        }

        private Int64 GetPlayTime(Int64 tBegin, Int64 tEnd, int iDirection)
        {
            Int64 tPlayTime = 0;
            int iCount = listViewPlaybackRecordInfo.Items.Count;

            for (int i = 0; i < iCount; i++)
            {
                tPlayTime = Convert.ToInt64(GetTimeStamp(listViewPlaybackRecordInfo.Items[i].SubItems[3].Text));
                if (tPlayTime >= tBegin && tPlayTime <= tEnd)
                {
                    return tPlayTime;
                }
            }

            return tBegin;
        }

        private int GetFileSizeByBeginEnd(Int64 tBegin, Int64 tEnd)
        {
            double iTotalSize = 0;
            double preSize = 0;
            double postSize = 0;
            int iCount = listViewPlaybackRecordInfo.Items.Count;
            for (int i = 0; i < iCount; i++)
            {
                double tmpBeginTime = Convert.ToDouble(GetTimeStamp(listViewPlaybackRecordInfo.Items[i].SubItems[3].Text));
                double tmpEndTime = Convert.ToDouble(GetTimeStamp(listViewPlaybackRecordInfo.Items[i].SubItems[4].Text));
                double tmpFileSize = Convert.ToDouble(listViewPlaybackRecordInfo.Items[i].SubItems[6].Text);
                if (tBegin > tmpEndTime)
                {
                    continue;
                }
                if (tEnd < tmpBeginTime)
                {
                    break;
                }

                if (tBegin <= tmpBeginTime && tEnd >= tmpEndTime)
                {
                    iTotalSize += tmpFileSize;
                }
                else if (tBegin > tmpBeginTime && tBegin < tmpEndTime)
                {
                    double iRatioPre = (tmpEndTime - tBegin) / (tmpEndTime - tmpBeginTime);
                    preSize = iRatioPre * tmpFileSize;
                }
                else if (tEnd > tmpBeginTime && tEnd < tmpEndTime)
                {
                    double iRatioPost = (tEnd - tmpBeginTime) / (tmpEndTime - tmpBeginTime);
                    postSize = iRatioPost * tmpFileSize;
                }
            }

            iTotalSize = iTotalSize + preSize + postSize;
            return (int)iTotalSize;
        }

        private void btnPlaybackByTime_Click(object sender, EventArgs e)
        {
            textPlaybackResult.Text = "";
            string strDisPlayWindows = comPlayBackWindows.Text;
            if (strDisPlayWindows.Length < 0)
            {
                textPlaybackResult.Text = AppData._CS("Display Windows cannot be empty");
                comPlayBackWindows.Focus();
                return;
            }

            //Stops the window currently playing
            btnPlaybackStopVideo_Click(sender, e);

            string strXmlData = textPlaybackResult.Text;
            ArrayList arrDisPlayWindows = new ArrayList();
            StringSplit(strDisPlayWindows, ":", ref arrDisPlayWindows);

            string strWindowNumList = arrDisPlayWindows[1].ToString();
            ArrayList arrWindowNumList = new ArrayList();
            StringSplit(strWindowNumList, ",", ref arrWindowNumList);
            if (arrWindowNumList.Count > 0)
            {
                DPSDK_PLAYBACK_BY_TIME_PARAM pPlaybackParam = new DPSDK_PLAYBACK_BY_TIME_PARAM();
                pPlaybackParam.iDirection = Convert.ToInt32(comboBoxPlaybackDirection.Text.Substring(0, comboBoxPlaybackDirection.Text.IndexOf("-")));
                pPlaybackParam.szCodeId = textPlaybackDeviceID.Text;
                string DeviceID = TakeDeviceID(textPlaybackDeviceID.Text);
                pPlaybackParam.szDeviceCode = DeviceID;
                DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
                pPlaybackParam.tBeginTime = Convert.ToInt64(datePlaybackStartTime.Value.Subtract(dtStart).TotalSeconds);
                pPlaybackParam.tEndTime = Convert.ToInt64(datePlaybackEndTime.Value.Subtract(dtStart).TotalSeconds);
                pPlaybackParam.tPlayTime = GetPlayTime(pPlaybackParam.tBeginTime, pPlaybackParam.tEndTime, pPlaybackParam.iDirection);
                pPlaybackParam.iRecordSource = Convert.ToInt32(comboBoxPlaybackSourceType.Text.Substring(0, comboBoxPlaybackSourceType.Text.IndexOf("-"))) + 1;
                pPlaybackParam.iStreamType = Convert.ToInt32(comboBoxPlaybackStreamType.Text.Substring(0, comboBoxPlaybackStreamType.Text.IndexOf("-")));
                pPlaybackParam.iRecordType = Convert.ToInt32(comboBoxPlaybackQueryRecordType.Text.Substring(0, comboBoxPlaybackQueryRecordType.Text.IndexOf("-")));
                foreach (ListViewItem item in listViewPlaybackRecordInfo.Items)
                {
                    Int64 itemStartTime = GetTimeStamp(item.SubItems[3].Text);
                    Int64 itemEndTime = GetTimeStamp(item.SubItems[4].Text);
                    if (itemStartTime >= pPlaybackParam.tBeginTime && itemEndTime <= pPlaybackParam.tEndTime)
                    {
                        pPlaybackParam.szSSId = item.SubItems[9].Text;
                        pPlaybackParam.uFileHandle = Convert.ToUInt64(item.SubItems[10].Text);
                        break;
                    }
                }

                DPSDK_RES iRet = -1;
                for (int i = 0; i < arrWindowNumList.Count; i++)
                {
                    switch (Convert.ToInt32(arrWindowNumList[i]))
                    {
                        case 1: // Window 1  play
                            pPlaybackParam.pHWnd = pictureBoxPlaybackVideo.Handle;
                            m_iPlaybackMediaSessionID = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                            iRet = DPSDK_StartPlaybackByTimeEx(m_iSessionID, ref pPlaybackParam, m_iPlaybackMediaSessionID);
                            if (iRet == DPSDK_SUCCESS)
                            {
                                strXmlData = strXmlData + "start playback by time in Window 1 success \r\n";
                                btnPlaybackPausePlayback.Text = "PausePlayback";

                                m_mapPlayback.Add(1, pPlaybackParam);
                                m_strPlaybackChannelID = pPlaybackParam.szCodeId;
                            }
                            else
                            {
                                strXmlData = strXmlData + "start playback by time in Window 1 failed, iRet = " + iRet.ToString() + "\r\n";
                                m_iPlaybackMediaSessionID = IntPtr.Zero;
                            }
                            break;

                        case 2: // Window 2 play
                            pPlaybackParam.pHWnd = pictureBoxPlaybackVideo2.Handle;
                            m_iPlaybackMediaSessionID2 = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                            iRet = DPSDK_StartPlaybackByTimeEx(m_iSessionID, ref pPlaybackParam, m_iPlaybackMediaSessionID2);
                            if (iRet == DPSDK_SUCCESS)
                            {
                                strXmlData = strXmlData + "start playback by time in Window 2 success \r\n";
                                btnPlaybackPausePlayback.Text = "PausePlayback";

                                m_mapPlayback.Add(2, pPlaybackParam);
                                m_strPlaybackChannelID2 = pPlaybackParam.szCodeId;
                            }
                            else
                            {
                                strXmlData = strXmlData + "start playback by time in Window 2 failed, iRet = " + iRet.ToString() + "\r\n";
                                m_iPlaybackMediaSessionID2 = IntPtr.Zero;
                            }
                            break;

                        case 3: // Window 3 play
                            pPlaybackParam.pHWnd = pictureBoxPlaybackVideo3.Handle;
                            m_iPlaybackMediaSessionID3 = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                            iRet = DPSDK_StartPlaybackByTimeEx(m_iSessionID, ref pPlaybackParam, m_iPlaybackMediaSessionID3);
                            if (iRet == DPSDK_SUCCESS)
                            {
                                strXmlData = strXmlData + "start playback by time in Window 3 success \r\n";
                                btnPlaybackPausePlayback.Text = "PausePlayback";

                                m_mapPlayback.Add(3, pPlaybackParam);
                                m_strPlaybackChannelID3 = pPlaybackParam.szCodeId;
                            }
                            else
                            {
                                strXmlData = strXmlData + "start playback by time in Window 3 failed, iRet = " + iRet.ToString() + "\r\n";
                                m_iPlaybackMediaSessionID3 = IntPtr.Zero;
                            }
                            break;

                        case 4: // Window 4 play
                            pPlaybackParam.pHWnd = pictureBoxPlaybackVideo4.Handle;
                            m_iPlaybackMediaSessionID4 = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                            iRet = DPSDK_StartPlaybackByTimeEx(m_iSessionID, ref pPlaybackParam, m_iPlaybackMediaSessionID4);
                            if (iRet == DPSDK_SUCCESS)
                            {
                                strXmlData = strXmlData + "start playback by time in Window 4 success \r\n";
                                btnPlaybackPausePlayback.Text = "PausePlayback";

                                m_mapPlayback.Add(4, pPlaybackParam);
                                m_strPlaybackChannelID4 = pPlaybackParam.szCodeId;
                            }
                            else
                            {
                                strXmlData = strXmlData + "start playback by time in Window 4 failed, iRet = " + iRet.ToString() + "\r\n";
                                m_iPlaybackMediaSessionID4 = IntPtr.Zero;
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
            textPlaybackResult.Text = strXmlData;
        }

        private void btnPlaybackFile_Click(object sender, EventArgs e)
        {
            textPlaybackResult.Text = "";
            string strDisPlayWindows = comPlayBackWindows.Text;
            if (strDisPlayWindows.Length < 0)
            {
                textPlaybackResult.Text = AppData._CS("Display Windows cannot be empty");
                comPlayBackWindows.Focus();
                return;
            }

            // Stops the window currently playing
            btnPlaybackStopVideo_Click(sender, e);

            string strXmlData = textPlaybackResult.Text;

            ArrayList arrDisPlayWindows = new ArrayList();
            StringSplit(strDisPlayWindows, ":", ref arrDisPlayWindows);

            string strWindowNumList = arrDisPlayWindows[1].ToString();
            ArrayList arrWindowNumList = new ArrayList();
            StringSplit(strWindowNumList, ",", ref arrWindowNumList);

            if (arrWindowNumList.Count > 0)
            {
                DPSDK_PLAYBACK_BY_FILE_PARAM pPlaybackParam = new DPSDK_PLAYBACK_BY_FILE_PARAM();
                pPlaybackParam.iDirection = Convert.ToInt32(comboBoxPlaybackDirection.Text.Substring(0, comboBoxPlaybackDirection.Text.IndexOf("-")));
                pPlaybackParam.szCodeId = textPlaybackDeviceID.Text;
                pPlaybackParam.iRecordSource = Convert.ToInt32(comboBoxPlaybackSourceType.Text.Substring(0, comboBoxPlaybackSourceType.Text.IndexOf("-"))) + 1;
                if (listViewPlaybackRecordInfo.SelectedIndices.Count > 0)
                {
                    pPlaybackParam.szFilename = listViewPlaybackRecordInfo.SelectedItems[0].SubItems[5].Text;
                    pPlaybackParam.szFilename = pPlaybackParam.szFilename.Replace("efs://", "");
                    pPlaybackParam.tBeginTime = Convert.ToInt64(GetTimeStamp(listViewPlaybackRecordInfo.SelectedItems[0].SubItems[3].Text));
                    pPlaybackParam.tEndTime = Convert.ToInt64(GetTimeStamp(listViewPlaybackRecordInfo.SelectedItems[0].SubItems[4].Text));
                    pPlaybackParam.szSSId = listViewPlaybackRecordInfo.SelectedItems[0].SubItems[9].Text;
                    pPlaybackParam.uFileHandle = Convert.ToUInt64(listViewPlaybackRecordInfo.SelectedItems[0].SubItems[10].Text);
                    pPlaybackParam.szDiskId = listViewPlaybackRecordInfo.SelectedItems[0].SubItems[11].Text;
                }
                else
                {
                    AppData.ShowMsgInfo("Please select one record!");
                    return;
                }

                DPSDK_RES iRet = -1;
                for (int i = 0; i < arrWindowNumList.Count; i++)
                {
                    switch (Convert.ToInt32(arrWindowNumList[i]))
                    {
                        case 1: // Window 1  play
                            pPlaybackParam.pHWnd = pictureBoxPlaybackVideo.Handle;
                            m_iPlaybackMediaSessionID = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                            iRet = DPSDK_StartPlaybackByFileEx(m_iSessionID, ref pPlaybackParam, m_iPlaybackMediaSessionID, IntPtr.Zero);
                            if (iRet == DPSDK_SUCCESS)
                            {
                                m_bLocalPlayback = false;
                                if (pPlaybackParam.iDirection == 0)
                                {
                                    ResetPlaybackProgressBar(1, true);
                                    AddToTimeMap(1, pPlaybackParam.tEndTime - pPlaybackParam.tBeginTime);
                                    if (!m_timer.Enabled)
                                    {
                                        StartTimer();
                                    }
                                }
                                else
                                {
                                    ResetPlaybackProgressBar(1, false);
                                }

                                strXmlData = strXmlData + "start playback by file in Window 1 success \r\n";
                                btnPlaybackPausePlayback.Text = "PausePlayback";
                                m_strPlaybackChannelID = pPlaybackParam.szCodeId;
                            }
                            else
                            {
                                strXmlData = strXmlData + "start playback by file in Window 1 failed, iRet = " + iRet.ToString() + "\r\n";
                                m_iPlaybackMediaSessionID = IntPtr.Zero;
                            }
                            break;

                        case 2: // Window 2 play
                            pPlaybackParam.pHWnd = pictureBoxPlaybackVideo2.Handle;
                            m_iPlaybackMediaSessionID2 = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                            iRet = DPSDK_StartPlaybackByFileEx(m_iSessionID, ref pPlaybackParam, m_iPlaybackMediaSessionID2, IntPtr.Zero);
                            if (iRet == DPSDK_SUCCESS)
                            {
                                m_bLocalPlayback2 = false;
                                if (pPlaybackParam.iDirection == 0)
                                {
                                    ResetPlaybackProgressBar(2, true);
                                    AddToTimeMap(2, pPlaybackParam.tEndTime - pPlaybackParam.tBeginTime);
                                    if (!m_timer.Enabled)
                                    {
                                        StartTimer();
                                    }
                                }
                                else
                                {
                                    ResetPlaybackProgressBar(2, false);
                                }

                                strXmlData = strXmlData + "start playback by file in Window 2 success \r\n";
                                btnPlaybackPausePlayback.Text = "PausePlayback";
                                m_strPlaybackChannelID2 = pPlaybackParam.szCodeId;
                            }
                            else
                            {
                                strXmlData = strXmlData + "start playback by file in Window 2 failed, iRet = " + iRet.ToString() + "\r\n";
                                m_iPlaybackMediaSessionID2 = IntPtr.Zero;
                            }
                            break;

                        case 3: // Window 3 play
                            pPlaybackParam.pHWnd = pictureBoxPlaybackVideo3.Handle;
                            m_iPlaybackMediaSessionID3 = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                            iRet = DPSDK_StartPlaybackByFileEx(m_iSessionID, ref pPlaybackParam, m_iPlaybackMediaSessionID3, IntPtr.Zero);
                            if (iRet == DPSDK_SUCCESS)
                            {
                                m_bLocalPlayback3 = false;
                                if (pPlaybackParam.iDirection == 0)
                                {
                                    ResetPlaybackProgressBar(3, true);
                                    AddToTimeMap(3, pPlaybackParam.tEndTime - pPlaybackParam.tBeginTime);
                                    if (!m_timer.Enabled)
                                    {
                                        StartTimer();
                                    }
                                }
                                else
                                {
                                    ResetPlaybackProgressBar(3, false);
                                }

                                strXmlData = strXmlData + "start playback by file in Window 3 success \r\n";
                                btnPlaybackPausePlayback.Text = "PausePlayback";
                                m_strPlaybackChannelID3 = pPlaybackParam.szCodeId;
                            }
                            else
                            {
                                strXmlData = strXmlData + "start playback by file in Window 4 failed, iRet = " + iRet.ToString() + "\r\n";
                                m_iPlaybackMediaSessionID3 = IntPtr.Zero;
                            }
                            break;

                        case 4: // Window 4 play
                            pPlaybackParam.pHWnd = pictureBoxPlaybackVideo4.Handle;
                            m_iPlaybackMediaSessionID4 = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                            iRet = DPSDK_StartPlaybackByFileEx(m_iSessionID, ref pPlaybackParam, m_iPlaybackMediaSessionID4, IntPtr.Zero);
                            if (iRet == DPSDK_SUCCESS)
                            {
                                m_bLocalPlayback4 = false;
                                if (pPlaybackParam.iDirection == 0)
                                {
                                    ResetPlaybackProgressBar(4, true);
                                    AddToTimeMap(4, pPlaybackParam.tEndTime - pPlaybackParam.tBeginTime);
                                    if (!m_timer.Enabled)
                                    {
                                        StartTimer();
                                    }
                                }

                                strXmlData = strXmlData + "start playback by file in Window 4 success \r\n";
                                btnPlaybackPausePlayback.Text = "PausePlayback";
                                m_strPlaybackChannelID4 = pPlaybackParam.szCodeId;
                            }
                            else
                            {
                                strXmlData = strXmlData + "start playback by file in Window 4 failed, iRet = " + iRet.ToString() + "\r\n";
                                m_iPlaybackMediaSessionID4 = IntPtr.Zero;
                            }
                            break;
                        default:
                            break;
                    }
                }
            }

            textPlaybackResult.Text = strXmlData;
        }

        private void btnPlaybackLocalFile_Click(object sender, EventArgs e)
        {
            textPlaybackResult.Text = "";
            string strDisPlayWindows = comPlayBackWindows.Text;
            if (strDisPlayWindows.Length < 0)
            {
                textPlaybackResult.Text = AppData._CS("Display Windows cannot be empty");
                comPlayBackWindows.Focus();
                return;
            }

            if (Convert.ToInt32(comboBoxPlaybackDirection.Text.Substring(0, comboBoxPlaybackDirection.Text.IndexOf("-"))) == 1)
            {
                textPlaybackResult.Text = AppData._CS("This direction parameter is not supported");
                return;
            }

            // Stops the window currently playing
            btnPlaybackStopVideo_Click(sender, e);

            string strXmlData = textPlaybackResult.Text;

            ArrayList arrDisPlayWindows = new ArrayList();
            StringSplit(strDisPlayWindows, ":", ref arrDisPlayWindows);

            string strWindowNumList = arrDisPlayWindows[1].ToString();
            ArrayList arrWindowNumList = new ArrayList();
            StringSplit(strWindowNumList, ",", ref arrWindowNumList);
            if (arrWindowNumList.Count > 0)
            {
                DPSDK_PLAYBACK_BY_LOCALFILE_PARAM pPlaybackParam = new DPSDK_PLAYBACK_BY_LOCALFILE_PARAM();

                if (File.Exists(textLocalFilePath.Text))
                {
                    pPlaybackParam.szFilePath = textLocalFilePath.Text;
                }
                else
                {
                    AppData.ShowMsgInfo("Please enter the correct file path!");
                    return;
                }

                DPSDK_RES iRet = -1;

                for (int i = 0; i < arrWindowNumList.Count; i++)
                {
                    switch (Convert.ToInt32(arrWindowNumList[i]))
                    {
                        case 1: // Window 1  play
                            pPlaybackParam.pHWnd = pictureBoxPlaybackVideo.Handle;
                            m_iPlaybackMediaSessionID = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                            iRet = DPSDK_StartPlaybackByLocalFileEx(m_iSessionID, ref pPlaybackParam, m_iPlaybackMediaSessionID);
                            if (iRet == DPSDK_SUCCESS)
                            {
                                m_bLocalPlayback = true;
                                ResetPlaybackProgressBar(1, true);
                                IntPtr time = IntPtr.Zero;
                                DPSDK_GetLocalPlaybackFileTime(m_iSessionID, m_iPlaybackMediaSessionID, ref time);
                                Int64 iTotalTime = time.ToInt64();

                                AddToTimeMap(1, iTotalTime);
                                if (!m_timer.Enabled)
                                {
                                    StartTimer();
                                }

                                strXmlData = strXmlData + "start playback by local file in Window 1 success \r\n";
                                btnPlaybackPausePlayback.Text = "PausePlayback";
                            }
                            else
                            {
                                strXmlData = strXmlData + "start playback by local file in Window 1 failed, iRet = " + iRet.ToString() + "\r\n";
                                m_iPlaybackMediaSessionID = IntPtr.Zero;
                            }
                            break;

                        case 2: // Window 2 play
                            pPlaybackParam.pHWnd = pictureBoxPlaybackVideo2.Handle;
                            m_iPlaybackMediaSessionID2 = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                            iRet = DPSDK_StartPlaybackByLocalFileEx(m_iSessionID, ref pPlaybackParam, m_iPlaybackMediaSessionID2);
                            if (iRet == DPSDK_SUCCESS)
                            {
                                m_bLocalPlayback2 = true;
                                ResetPlaybackProgressBar(2, true);
                                IntPtr time = IntPtr.Zero;
                                DPSDK_GetLocalPlaybackFileTime(m_iSessionID, m_iPlaybackMediaSessionID2, ref time);
                                Int64 iTotalTime = time.ToInt64();

                                AddToTimeMap(2, iTotalTime);
                                if (!m_timer.Enabled)
                                {
                                    StartTimer();
                                }

                                strXmlData = strXmlData + "start playback by local file in Window 2 success \r\n";
                                btnPlaybackPausePlayback.Text = "PausePlayback";
                            }
                            else
                            {
                                strXmlData = strXmlData + "start playback by local file in Window 2 failed, iRet = " + iRet.ToString() + "\r\n";
                                m_iPlaybackMediaSessionID2 = IntPtr.Zero;
                            }
                            break;

                        case 3: // Window 3 play
                            pPlaybackParam.pHWnd = pictureBoxPlaybackVideo3.Handle;
                            m_iPlaybackMediaSessionID3 = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                            iRet = DPSDK_StartPlaybackByLocalFileEx(m_iSessionID, ref pPlaybackParam, m_iPlaybackMediaSessionID3);
                            if (iRet == DPSDK_SUCCESS)
                            {
                                m_bLocalPlayback3 = true;
                                ResetPlaybackProgressBar(3, true);
                                IntPtr time = IntPtr.Zero;
                                DPSDK_GetLocalPlaybackFileTime(m_iSessionID, m_iPlaybackMediaSessionID3, ref time);
                                Int64 iTotalTime = time.ToInt64();

                                AddToTimeMap(3, iTotalTime);
                                if (!m_timer.Enabled)
                                {
                                    StartTimer();
                                }

                                strXmlData = strXmlData + "start playback by local file in Window 3 success \r\n";
                                btnPlaybackPausePlayback.Text = "PausePlayback";
                            }
                            else
                            {
                                strXmlData = strXmlData + "start playback by local file in Window 4 failed, iRet = " + iRet.ToString() + "\r\n";
                                m_iPlaybackMediaSessionID3 = IntPtr.Zero;
                            }
                            break;

                        case 4: // Window 4 play
                            pPlaybackParam.pHWnd = pictureBoxPlaybackVideo4.Handle;
                            m_iPlaybackMediaSessionID4 = (IntPtr)UniqueNumberGenerator.GetUniqueNum();
                            iRet = DPSDK_StartPlaybackByLocalFileEx(m_iSessionID, ref pPlaybackParam, m_iPlaybackMediaSessionID4);
                            if (iRet == DPSDK_SUCCESS)
                            {
                                m_bLocalPlayback4 = true;
                                ResetPlaybackProgressBar(4, true);
                                IntPtr time = IntPtr.Zero;
                                DPSDK_GetLocalPlaybackFileTime(m_iSessionID, m_iPlaybackMediaSessionID4, ref time);
                                Int64 iTotalTime = time.ToInt64();

                                AddToTimeMap(4, iTotalTime);
                                if (!m_timer.Enabled)
                                {
                                    StartTimer();
                                }

                                strXmlData = strXmlData + "start playback by local file in Window 4 success \r\n";
                                btnPlaybackPausePlayback.Text = "PausePlayback";
                            }
                            else
                            {
                                strXmlData = strXmlData + "start playback by local file in Window 4 failed, iRet = " + iRet.ToString() + "\r\n";
                                m_iPlaybackMediaSessionID4 = IntPtr.Zero;
                            }
                            break;
                        default:
                            break;
                    }
                }
            }

            textPlaybackResult.Text = strXmlData;
        }

        private void btnPlaybackStopVideo_Click(object sender, EventArgs e)
        {
            textPlaybackResult.Text = "";

            string strDisPlayWindows = comPlayBackWindows.Text;
            if (strDisPlayWindows.Length < 0)
            {
                textPlaybackResult.Text = AppData._CS("Display Windows cannot be empty");
                comPlayBackWindows.Focus();
                return;
            }

            string strXmlData = "";

            ArrayList arrDisPlayWindows = new ArrayList();
            StringSplit(strDisPlayWindows, ":", ref arrDisPlayWindows);

            string strWindowNumList = arrDisPlayWindows[1].ToString();
            ArrayList arrWindowNumList = new ArrayList();
            StringSplit(strWindowNumList, ",", ref arrWindowNumList);
            if (arrWindowNumList.Count > 0)
            {
                DPSDK_RES iRet = -1;
                // Stops the window currently playing
                for (int i = 0; i < arrWindowNumList.Count; i++)
                {
                    switch (Convert.ToInt32(arrWindowNumList[i]))
                    {
                        case 1:
                            if (m_iPlaybackMediaSessionID != IntPtr.Zero)
                            {
                                iRet = DPSDK_StopPlayback(m_iSessionID, m_iPlaybackMediaSessionID);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    strXmlData = strXmlData + "Stop playback in Window 1 success \r\n";
                                    pictureBoxPlaybackVideo.Image = null;
                                    if (m_mapPlayback.ContainsKey(1))
                                    {
                                        m_mapPlayback.Remove(1);
                                    }
                                    m_strPlaybackChannelID = "";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "stop playback in Window 1 failed, iRet = " + iRet.ToString() + "\r\n";
                                }

                                m_iPlaybackMediaSessionID = IntPtr.Zero;

                                m_bLocalPlayback = false;
                                ResetPlaybackProgressBar(1, false);
                                RemoveFromTimeMap(1);
                                ResetPlaybackDrawID(1);
                            }
                            break;
                        case 2:
                            if (m_iPlaybackMediaSessionID2 != IntPtr.Zero)
                            {
                                iRet = DPSDK_StopPlayback(m_iSessionID, m_iPlaybackMediaSessionID2);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    strXmlData = strXmlData + "Stop playback in Window 2 success \r\n";
                                    pictureBoxPlaybackVideo2.Image = null;
                                    if (m_mapPlayback.ContainsKey(2))
                                    {
                                        m_mapPlayback.Remove(2);
                                    }
                                    m_strPlaybackChannelID2 = "";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "stop playback in Window 2 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                                m_iPlaybackMediaSessionID2 = IntPtr.Zero;

                                m_bLocalPlayback2 = false;
                                ResetPlaybackProgressBar(2, false);
                                RemoveFromTimeMap(2);
                                ResetPlaybackDrawID(2);
                            }
                            break;
                        case 3:
                            if (m_iPlaybackMediaSessionID3 != IntPtr.Zero)
                            {
                                iRet = DPSDK_StopPlayback(m_iSessionID, m_iPlaybackMediaSessionID3);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    strXmlData = strXmlData + "Stop playback in Window 3 success \r\n";
                                    pictureBoxPlaybackVideo3.Image = null;
                                    if (m_mapPlayback.ContainsKey(3))
                                    {
                                        m_mapPlayback.Remove(3);
                                    }
                                    m_strPlaybackChannelID3 = "";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "stop playback in Window 3 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                                m_iPlaybackMediaSessionID3 = IntPtr.Zero;

                                m_bLocalPlayback3 = false;
                                ResetPlaybackProgressBar(3, false);
                                RemoveFromTimeMap(3);
                                ResetPlaybackDrawID(3);
                            }
                            break;
                        case 4:
                            if (m_iPlaybackMediaSessionID4 != IntPtr.Zero)
                            {
                                iRet = DPSDK_StopPlayback(m_iSessionID, m_iPlaybackMediaSessionID4);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    strXmlData = strXmlData + "Stop playback in Window 4 success \r\n";
                                    pictureBoxPlaybackVideo4.Image = null;
                                    if (m_mapPlayback.ContainsKey(4))
                                    {
                                        m_mapPlayback.Remove(4);
                                    }
                                    m_strPlaybackChannelID4 = "";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "stop playback in Window 4 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                                m_iPlaybackMediaSessionID4 = IntPtr.Zero;

                                m_bLocalPlayback4 = false;
                                ResetPlaybackProgressBar(4, false);
                                RemoveFromTimeMap(4);
                                ResetPlaybackDrawID(4);
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
            textPlaybackResult.Text = strXmlData;
        }

        private void btnPlaybackPausePlayback_Click(object sender, EventArgs e)
        {
            if (btnPlaybackPausePlayback.Text == "PausePlayback")
            {
                DPSDK_RES iRet = DPSDK_PlaybackPause(m_iSessionID, m_iPlaybackMediaSessionID);
                if (iRet == DPSDK_SUCCESS)
                {
                    btnPlaybackPausePlayback.Text = "ResumePlayback";
                    textPlaybackResult.Text = "PlaybackPause success";
                }
                else
                {
                    textPlaybackResult.Text = "PlaybackPause failed, error code = " + iRet.ToString();
                }
            }
            else
            {
                DPSDK_RES iRet = DPSDK_PlaybackResume(m_iSessionID, m_iPlaybackMediaSessionID);
                if (iRet == DPSDK_SUCCESS)
                {
                    btnPlaybackPausePlayback.Text = "PausePlayback";
                    textPlaybackResult.Text = "PlaybackResume success";
                }
                else
                {
                    textPlaybackResult.Text = "PlaybackResume failed, error code = " + iRet.ToString();
                }
            }
        }

        private void btnPlaybackFrameStep_Click(object sender, EventArgs e)
        {
            DPSDK_RES iRet = DPSDK_PlaybackFrameStep(m_iSessionID, m_iPlaybackMediaSessionID);
            if (iRet == DPSDK_SUCCESS)
            {
                textPlaybackResult.Text = "PlaybackFrameStep success";
            }
            else
            {
                textPlaybackResult.Text = "PlaybackFrameStep failed, error code = " + iRet.ToString();
            }
        }

        private DPSDK_PLAYBACK_SPEED GetSpeed(int iSpeedFormat)
        {
            DPSDK_PLAYBACK_SPEED iSpeed = DPSDK_PLAYBACK_SPEED.DPSDK_PB_NORMAL;
            switch (iSpeedFormat)
            {
                case 0:
                    iSpeed = DPSDK_PLAYBACK_SPEED.DPSDK_PB_NORMAL;
                    break;
                case 1:
                    iSpeed = DPSDK_PLAYBACK_SPEED.DPSDK_PB_NORMAL_FAST2;
                    break;
                case 2:
                    iSpeed = DPSDK_PLAYBACK_SPEED.DPSDK_PB_NORMAL_FAST4;
                    break;
                case 3:
                    iSpeed = DPSDK_PLAYBACK_SPEED.DPSDK_PB_NORMAL_FAST8;
                    break;
                case 4:
                    iSpeed = DPSDK_PLAYBACK_SPEED.DPSDK_PB_NORMAL_FAST16;
                    break;
                case 5:
                    iSpeed = DPSDK_PLAYBACK_SPEED.DPSDK_PB_NORMAL_SLOW2;
                    break;
                case 6:
                    iSpeed = DPSDK_PLAYBACK_SPEED.DPSDK_PB_NORMAL_SLOW4;
                    break;
                case 7:
                    iSpeed = DPSDK_PLAYBACK_SPEED.DPSDK_PB_NORMAL_SLOW8;
                    break;
                case 8:
                    iSpeed = DPSDK_PLAYBACK_SPEED.DPSDK_PB_NORMAL_SLOW16;
                    break;
                default:
                    break;
            }
            return iSpeed;
        }

        private void btnPlaybackSeek_Click(object sender, EventArgs e)
        {
            DPSDK_PLAYBACK_SEEK_PARAM pPlaybackSeekParam = new DPSDK_PLAYBACK_SEEK_PARAM();
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            pPlaybackSeekParam.tBeginTime = Convert.ToInt64(datePlaybackStartTime.Value.Subtract(dtStart).TotalSeconds);
            pPlaybackSeekParam.tEndTime = Convert.ToInt64(datePlaybackEndTime.Value.Subtract(dtStart).TotalSeconds);
            pPlaybackSeekParam.iSpeed = GetSpeed(comboBoxPlaybackSpeed.SelectedIndex);
            pPlaybackSeekParam.iDirection = Convert.ToInt32(comboBoxPlaybackDirection.Text.Substring(0, comboBoxPlaybackDirection.Text.IndexOf("-")));
            DPSDK_RES iRet = DPSDK_PlaybackSeek(m_iSessionID, m_iPlaybackMediaSessionID, ref pPlaybackSeekParam);
            if (iRet == DPSDK_SUCCESS)
            {
                textPlaybackResult.Text = "PlaybackSeek success";
            }
            else
            {
                textPlaybackResult.Text = "PlaybackSeek failed, error code = " + iRet.ToString();
            }
        }

        private void btnPlaybackGetPlayedTime_Click(object sender, EventArgs e)
        {
            IntPtr pTime = IntPtr.Zero;
            DPSDK_RES iRet = DPSDK_GetPlayedTime(m_iSessionID, m_iPlaybackMediaSessionID, ref pTime);
            if (iRet == DPSDK_SUCCESS)
            {
                textPlaybackResult.Text = "GetPlayedTime success, pTime = " + pTime.ToInt64();
            }
            else
            {
                textPlaybackResult.Text = "GetPlayedTime failed, error code = " + iRet.ToString();
            }
        }

        private void btnPlaybackGetProviderType_Click(object sender, EventArgs e)
        {
            IntPtr pProviderType = IntPtr.Zero;
            DPSDK_RES iRet = DPSDK_GetProviderType(m_iSessionID, m_iPlaybackMediaSessionID, ref pProviderType);
            if (iRet == DPSDK_SUCCESS)
            {
                textPlaybackResult.Text = "GetProviderType success, pProviderType = " + pProviderType.ToInt32();
            }
            else
            {
                textPlaybackResult.Text = "GetProviderType failed, error code = " + iRet.ToString();
            }
        }

        private void btnPlaybackSetPlaybackSpeed_Click(object sender, EventArgs e)
        {
            DPSDK_PLAYBACK_SPEED iSpeed = GetSpeed(comboBoxPlaybackSpeed.SelectedIndex);
            DPSDK_RES iRet = DPSDK_SetPlaybackSpeed(m_iSessionID, m_iPlaybackMediaSessionID, iSpeed);
            if (iRet == DPSDK_SUCCESS)
            {
                textPlaybackResult.Text = "SetPlaybackSpeed success";
            }
            else
            {
                textPlaybackResult.Text = "SetPlaybackSpeed failed, error code = " + iRet.ToString();
            }
        }

        private void btnPlaybackDownloadRecordByFile_Click(object sender, EventArgs e)
        {
            if (m_iDownloadMediaSessionID.ToInt32() > 0)
            {
                DPSDK_RES iRet = DPSDK_StopDownloadRecord(m_iSessionID, m_iDownloadMediaSessionID);
                if (iRet == DPSDK_SUCCESS)
                {
                    textPlaybackResult.Text = "StopDownloadRecord success";
                }
                else
                {
                    textPlaybackResult.Text = "StopDownloadRecord failed, error code = " + iRet.ToString();
                }

                m_iDownloadMediaSessionID = IntPtr.Zero;
                btnPlaybackDownloadRecordByFile.Text = "DownloadRecordByFile";
                btnPlaybackPauseDownloadRecord.Text = "PauseDownloadRecord";
                InitProgressBar();
            }
            else
            {
                m_bdownloadByTime = false;
                InitProgressBar();

                if (textPlaybackSplitSize.Text == "")
                {
                    AppData.ShowMsgInfo("Please input correct params!");
                    return;
                }
                DPSDK_DOWNLOAD_BY_FILE_PARAM pDownloadByFileParam = new DPSDK_DOWNLOAD_BY_FILE_PARAM();
                if (listViewPlaybackRecordInfo.SelectedIndices.Count > 0)
                {
                    pDownloadByFileParam.szFilename = listViewPlaybackRecordInfo.SelectedItems[0].SubItems[5].Text;
                    pDownloadByFileParam.szFilename = pDownloadByFileParam.szFilename.Replace("efs://", "");
                    pDownloadByFileParam.tBeginTime = Convert.ToInt64(GetTimeStamp(listViewPlaybackRecordInfo.SelectedItems[0].SubItems[3].Text));
                    pDownloadByFileParam.tEndTime = Convert.ToInt64(GetTimeStamp(listViewPlaybackRecordInfo.SelectedItems[0].SubItems[4].Text));
                    pDownloadByFileParam.szSSId = listViewPlaybackRecordInfo.SelectedItems[0].SubItems[9].Text;
                    pDownloadByFileParam.uFileHandle = Convert.ToUInt64(listViewPlaybackRecordInfo.SelectedItems[0].SubItems[10].Text);
                    pDownloadByFileParam.szDiskId = listViewPlaybackRecordInfo.SelectedItems[0].SubItems[11].Text;
                    m_fileSize = Convert.ToInt32(listViewPlaybackRecordInfo.SelectedItems[0].SubItems[6].Text);
                }
                else
                {
                    AppData.ShowMsgInfo("Please select one record!");
                    return;
                }
                pDownloadByFileParam.szChannelID = textPlaybackChannelID.Text;
                pDownloadByFileParam.iSourceType = (DPSDK_SOURCE_TYPE)(Convert.ToInt32(comboBoxPlaybackSourceType.Text.Substring(0, comboBoxPlaybackSourceType.Text.IndexOf("-")))+1);
                pDownloadByFileParam.szChannelName = textPlaybackChannelName.Text;
                pDownloadByFileParam.szDownloadPath = textPlaybackDownloadPath.Text;
                pDownloadByFileParam.iNameRule = (DPSDK_RECORD_FILE_NAME_RULE)Convert.ToInt32(comboBoxPlaybackNameRule.Text.Substring(0, comboBoxPlaybackNameRule.Text.IndexOf("-")));
                pDownloadByFileParam.szDownloadFileName = textPlaybackDownloadFileName.Text;
                pDownloadByFileParam.iSplitFileSize = Convert.ToInt32(textPlaybackSplitSize.Text);
                pDownloadByFileParam.iFileFormat = (DPSDK_DOWNLOAD_RECORD_FILE_FORMAT)Convert.ToInt32(comboBoxPlaybackFileFormat.Text.Substring(0, comboBoxPlaybackFileFormat.Text.IndexOf("-")));
                pDownloadByFileParam.fEventCallBack = m_fEventDownloadCallback;
                DPSDK_RES iRet = DPSDK_StartDownloadRecordByFile(m_iSessionID, ref pDownloadByFileParam, ref m_iDownloadMediaSessionID);

                if (iRet == DPSDK_SUCCESS)
                {
                    btnPlaybackDownloadRecordByFile.Text = "StopDownloadRecord";
                    textPlaybackResult.Text = "StartDownloadRecordByFile success";
                }
                else
                {
                    textPlaybackResult.Text = "StartDownloadRecordByFile failed, error code = " + iRet.ToString();
                }
            }
        }

        private void btnPlaybackDownloadRecordByTime_Click(object sender, EventArgs e)
        {
            if (m_iDownloadMediaSessionID.ToInt32() > 0)
            {
                DPSDK_RES iRet = DPSDK_StopDownloadRecord(m_iSessionID, m_iDownloadMediaSessionID);
                if (iRet == DPSDK_SUCCESS)
                {
                    textPlaybackResult.Text = "StopDownloadRecord success";
                }
                else
                {
                    textPlaybackResult.Text = "StopDownloadRecord failed, error code = " + iRet.ToString();
                }

                m_iDownloadMediaSessionID = IntPtr.Zero;
                btnPlaybackDownloadRecordByTime.Text = "DownloadRecordByTime";
                btnPlaybackPauseDownloadRecord.Text = "PauseDownloadRecord";
                InitProgressBar();
            }
            else
            {
                m_bdownloadByTime = true;
                InitProgressBar();

                if (textPlaybackSplitSize.Text == "")
                {
                    AppData.ShowMsgInfo("Please input correct params!");
                    return;
                }

                DPSDK_DOWNLOAD_BY_TIME_PARAM pDownloadByTimeParam = new DPSDK_DOWNLOAD_BY_TIME_PARAM();
                pDownloadByTimeParam.szChannelID = textPlaybackChannelID.Text;
                pDownloadByTimeParam.tBeginTime = ParseDateTime(datePlaybackStartTime.Value);
                pDownloadByTimeParam.tEndTime = ParseDateTime(datePlaybackEndTime.Value);
                pDownloadByTimeParam.iSourceType = (DPSDK_SOURCE_TYPE)(Convert.ToInt32(comboBoxPlaybackSourceType.Text.Substring(0, comboBoxPlaybackSourceType.Text.IndexOf("-"))) + 1);
                pDownloadByTimeParam.iStreamType = (DPSDK_STREAM_TYPE)Convert.ToInt32(comboBoxPlaybackStreamType.Text.Substring(0, comboBoxPlaybackStreamType.Text.IndexOf("-")));
                pDownloadByTimeParam.iRecordType = (DPSDK_RECORD_TYPE)Convert.ToInt32(comboBoxPlaybackQueryRecordType.Text.Substring(0, comboBoxPlaybackRecordType.Text.IndexOf("-")));
                pDownloadByTimeParam.szChannelName = textPlaybackChannelName.Text;
                pDownloadByTimeParam.szDownloadPath = textPlaybackDownloadPath.Text;
                pDownloadByTimeParam.iNameRule = (DPSDK_RECORD_FILE_NAME_RULE)Convert.ToInt32(comboBoxPlaybackNameRule.Text.Substring(0, comboBoxPlaybackNameRule.Text.IndexOf("-")));
                pDownloadByTimeParam.szDownloadFileName = textPlaybackDownloadFileName.Text;
                pDownloadByTimeParam.iSplitFileSize = Convert.ToInt32(textPlaybackSplitSize.Text);
                pDownloadByTimeParam.iFileFormat = (DPSDK_DOWNLOAD_RECORD_FILE_FORMAT)Convert.ToInt32(comboBoxPlaybackFileFormat.Text.Substring(0, comboBoxPlaybackFileFormat.Text.IndexOf("-")));
                pDownloadByTimeParam.fEventCallBack = m_fEventDownloadCallback;
                m_timeFileSize = GetFileSizeByBeginEnd(pDownloadByTimeParam.tBeginTime, pDownloadByTimeParam.tEndTime);
                foreach (ListViewItem item in listViewPlaybackRecordInfo.Items)
                {
                    Int64 itemStartTime = GetTimeStamp(item.SubItems[3].Text);
                    Int64 itemEndTime = GetTimeStamp(item.SubItems[4].Text);
                    if (itemStartTime >= pDownloadByTimeParam.tBeginTime && itemEndTime <= pDownloadByTimeParam.tEndTime)
                    {
                        pDownloadByTimeParam.szSSId = item.SubItems[9].Text;
                        pDownloadByTimeParam.uFileHandle = Convert.ToUInt64(item.SubItems[10].Text);
                        break;
                    }
                }

                DPSDK_RES iRet = DPSDK_StartDownloadRecordByTime(m_iSessionID, ref pDownloadByTimeParam, ref m_iDownloadMediaSessionID);

                if (iRet == DPSDK_SUCCESS)
                {
                    btnPlaybackDownloadRecordByTime.Text = "StopDownloadRecord";
                    textPlaybackResult.Text = "StartDownloadRecordByTime success";
                }
                else
                {
                    textPlaybackResult.Text = "StartDownloadRecordByTime failed, error code = " + iRet.ToString();
                }
            }
        }

        private void btnPlaybackPauseDownloadRecord_Click(object sender, EventArgs e)
        {
            if (btnPlaybackPauseDownloadRecord.Text == "PauseDownloadRecord")
            {
                DPSDK_RES iRet = DPSDK_PauseDownloadRecord(m_iSessionID, m_iDownloadMediaSessionID);
                if (iRet == DPSDK_SUCCESS)
                {
                    btnPlaybackPauseDownloadRecord.Text = "ResumeDownloadRecord";
                    textPlaybackResult.Text = "PauseDownloadRecord success";
                }
                else
                {
                    textPlaybackResult.Text = "PauseDownloadRecord failed, error code = " + iRet.ToString();
                }
            }
            else
            {
                DPSDK_RES iRet = DPSDK_ResumeDownloadRecord(m_iSessionID, m_iDownloadMediaSessionID);
                if (iRet == DPSDK_SUCCESS)
                {
                    btnPlaybackPauseDownloadRecord.Text = "PauseDownloadRecord";
                    textPlaybackResult.Text = "ResumeDownloadRecord success";
                }
                else
                {
                    textPlaybackResult.Text = "ResumeDownloadRecord failed, error code = " + iRet.ToString();
                }
            }
        }

        private void btnPlaybackGetRecordInfo_Click(object sender, EventArgs e)
        {
            int nFileNum = 100;
            uint uiBufLen = (uint)(Marshal.SizeOf(typeof(DPSDK_DOWNLOAD_RECORD_INFO)) - 8 + DPSDK_FILE_PATH_LEN * nFileNum);
            IntPtr pDownloadInfo = Marshal.AllocHGlobal((int)uiBufLen);
            DPSDK_RES iRet = DPSDK_GetDownloadRecordInfo(m_iSessionID, m_iDownloadMediaSessionID, pDownloadInfo, uiBufLen);
            if (iRet == DPSDK_SUCCESS)
            {
                DPSDK_DOWNLOAD_RECORD_INFO stuDownloadInfo = new DPSDK_DOWNLOAD_RECORD_INFO();
                stuDownloadInfo = (DPSDK_DOWNLOAD_RECORD_INFO)Marshal.PtrToStructure(pDownloadInfo, typeof(DPSDK_DOWNLOAD_RECORD_INFO));
                textPlaybackResult.Text = "GetDownloadRecordInfo success, FileCount = " + stuDownloadInfo.iFileCount.ToString() + ", DownloadStatus = " + stuDownloadInfo.iDownloadStatus.ToString() + ", DownloadMode = " + stuDownloadInfo.iDownloadMode.ToString();
            }
            else
            {
                textPlaybackResult.Text = "GetDownloadRecordInfo failed, error code = " + iRet.ToString();
            }
            Marshal.FreeHGlobal(pDownloadInfo);
        }

        private void btnPlaybackOpenSound_Click(object sender, EventArgs e)
        {
            DPSDK_RES iRet = DPSDK_OpenSound(m_iSessionID, m_iPlaybackMediaSessionID);
            if (iRet == DPSDK_SUCCESS)
            {
                textPlaybackResult.Text = "Open Sound success";
            }
            else
            {
                textPlaybackResult.Text = "Open Sound failed, error code = " + iRet.ToString();
            }
        }

        private void btnPlaybackCloseSound_Click(object sender, EventArgs e)
        {
            DPSDK_RES iRet = DPSDK_CloseSound(m_iSessionID, m_iPlaybackMediaSessionID);
            if (iRet == DPSDK_SUCCESS)
            {
                textPlaybackResult.Text = "Close Sound success";
            }
            else
            {
                textPlaybackResult.Text = "Close Sound failed, error code = " + iRet.ToString();
            }
        }

        private void btnPlaybackOpenShareSound_Click(object sender, EventArgs e)
        {
            DPSDK_RES iRet = DPSDK_OpenSoundShare(m_iSessionID, m_iPlaybackMediaSessionID);
            if (iRet == DPSDK_SUCCESS)
            {
                textPlaybackResult.Text = "Open Sound Share success";
            }
            else
            {
                textPlaybackResult.Text = "Open Sound Share failed, error code = " + iRet.ToString();
            }
        }

        private void btnPlaybackCloseShareSound_Click(object sender, EventArgs e)
        {
            DPSDK_RES iRet = DPSDK_CloseSoundShare(m_iSessionID, m_iPlaybackMediaSessionID);
            if (iRet == DPSDK_SUCCESS)
            {
                textPlaybackResult.Text = "Close Sound Share success";
            }
            else
            {
                textPlaybackResult.Text = "Close Sound Share failed, error code = " + iRet.ToString();
            }
        }

        private void btnPlaybackIsOpenSound_Click(object sender, EventArgs e)
        {
            IntPtr pIsOpenSound = IntPtr.Zero;
            DPSDK_RES iRet = DPSDK_IsOpenSoundState(m_iSessionID, m_iPlaybackMediaSessionID, ref pIsOpenSound);
            if (iRet == DPSDK_SUCCESS)
            {
                textPlaybackResult.Text = "Is Open Sound State success, bIsOpen = " + pIsOpenSound.ToString();
            }
            else
            {
                textPlaybackResult.Text = "Is Open Sound State failed, error code = " + iRet.ToString();
            }
        }

        private void btnPlaybackSetVolume_Click(object sender, EventArgs e)
        {
            DPSDK_RES iRet = DPSDK_SetVolume(m_iSessionID, m_iPlaybackMediaSessionID, (uint)trackBarPlaybackVolume.Value);
            if (iRet == DPSDK_SUCCESS)
            {
                textPlaybackResult.Text = "Set Volume success";
            }
            else
            {
                textPlaybackResult.Text = "Set Volume failed, error code = " + iRet.ToString();
            }
        }

        private void btnPlaybackGetVolum_Click(object sender, EventArgs e)
        {
            IntPtr pVolume = IntPtr.Zero;
            DPSDK_RES iRet = DPSDK_GetVolume(m_iSessionID, m_iPlaybackMediaSessionID, ref pVolume);
            if (iRet == DPSDK_SUCCESS)
            {
                textPlaybackResult.Text = "Set Volume success, volum = " + pVolume.ToString();
                labelPlaybackVolume.Text = pVolume.ToString();
                trackBarPlaybackVolume.Value = pVolume.ToInt32();
            }
            else
            {
                textPlaybackResult.Text = "Set Volume failed, error code = " + iRet.ToString();
            }
        }

        private void btnPlaybackGetPictureBuf_Click(object sender, EventArgs e)
        {
            IntPtr pWidth = IntPtr.Zero;
            IntPtr pHeight = IntPtr.Zero;
            DPSDK_RES iRet = DPSDK_GetPictureSize(m_iSessionID, m_iPlaybackMediaSessionID, ref pWidth, ref pHeight);
            if (iRet != DPSDK_SUCCESS)
            {
                textPlaybackResult.Text = "Get Picture Size failed, error code = " + iRet.ToString();
                return;
            }

            int iBufsize = GetPicBuffSize(comboBoxPlaybackPicFormat.SelectedIndex, pHeight.ToInt32(), pWidth.ToInt32());
            byte[] pPicBuf = new byte[iBufsize];
            IntPtr pPicSize = IntPtr.Zero;
            iRet = DPSDK_GetPictureBuf(m_iSessionID, m_iPlaybackMediaSessionID, ref pPicBuf[0], (IntPtr)iBufsize, ref pPicSize, (uint)comboBoxPlaybackPicFormat.SelectedIndex);
            if (iRet == DPSDK_SUCCESS)
            {
                textPlaybackResult.Text = "Get Picture To Buffer success. BufSize = " + iBufsize.ToString() + ", FactPicSize = " + pPicSize.ToString();
                MemoryStream ms = new MemoryStream(pPicBuf);
                pictureBoxPlaybackPicture.Image = Image.FromStream(ms);

                textPlaybackWidth.Text = pWidth.ToString();
                textPlaybackHeight.Text = pHeight.ToString();
                textPlaybackSize.Text = pPicSize.ToString();
            }
            else
            {
                textPlaybackResult.Text = "Get Picture To Buffer failed, error code = " + iRet.ToString();
            }
        }

        private void btnPlaybackGet24BitPictureFile_Click(object sender, EventArgs e)
        {
            uint uiPicFormat = (uint)comboBoxPlaybackPicFormat.SelectedIndex;
            string strPicturePath = textPlaybackPicPath.Text;
            DPSDK_RES iRet = DPSDK_Get24BitPictureFile(m_iSessionID, m_iPlaybackMediaSessionID, uiPicFormat, strPicturePath);
            if (iRet == DPSDK_SUCCESS)
            {
                pictureBoxPlaybackPicture.Image = Image.FromFile(strPicturePath);
                textPlaybackResult.Text = "Get Picture To File success.";
            }
            else
            {
                textPlaybackResult.Text = "Get Picture To File failed, error code = " + iRet.ToString();
            }
        }

        private void btnPlaybackConvertToJpegFile_Click(object sender, EventArgs e)
        {
            if (m_iConvertType >= 0)
            {
                return;
            }
            m_iConvertType = 1;
        }

        private void btnPlaybackConvertToBmpFile_Click(object sender, EventArgs e)
        {
            if (m_iConvertType >= 0)
            {
                return;
            }
            m_iConvertType = 0;
        }

        private void MediaEventNotify(IntPtr pNotify)
        {
            DPSDK_EVENT_PARAM struNotify = (DPSDK_EVENT_PARAM)Marshal.PtrToStructure(pNotify, typeof(DPSDK_EVENT_PARAM));
            if (struNotify.iSessionID == m_iPlaybackMediaSessionID.ToInt32())
            {
                DPSDK_SCREENSHOT pShotSrc = (DPSDK_SCREENSHOT)Marshal.PtrToStructure(struNotify.pBuf, typeof(DPSDK_SCREENSHOT));
                if (m_iConvertType == 0)
                {
                    DPSDK_CONVERT_BMP pConvertBMP = new DPSDK_CONVERT_BMP();
                    pConvertBMP.pBuf = pShotSrc.pBuf;
                    pConvertBMP.lSize = pShotSrc.lSize;
                    pConvertBMP.lWidth = pShotSrc.lWidth;
                    pConvertBMP.lHeight = pShotSrc.lHeight;
                    pConvertBMP.lType = pShotSrc.lType;
                    pConvertBMP.szFileName = textPlaybackPicPath.Text + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".bmp";
                    DPSDK_RES iRet = DPSDK_ConvertToBmpFile(m_iSessionID, m_iPlaybackMediaSessionID, ref pConvertBMP);
                    if (iRet == DPSDK_SUCCESS)
                    {
                        pictureBoxPlaybackPicture.Image = Image.FromFile(pConvertBMP.szFileName);
                        textPlaybackResult.Text = "ConvertToBmpFile success.";
                    }
                    else
                    {
                        textPlaybackResult.Text = "ConvertToBmpFile failed, error code = " + iRet.ToString();
                    }
                    m_iConvertType = -1;
                }
                else if (m_iConvertType == 1)
                {
                    DPSDK_CONVERT_JPEG pConvertJPEG = new DPSDK_CONVERT_JPEG();
                    pConvertJPEG.pBuf = pShotSrc.pBuf;
                    pConvertJPEG.lSize = pShotSrc.lSize;
                    pConvertJPEG.lWidth = pShotSrc.lWidth;
                    pConvertJPEG.lHeight = pShotSrc.lHeight;
                    pConvertJPEG.iQuality = 100;
                    pConvertJPEG.lType = pShotSrc.lType;
                    pConvertJPEG.szFileName = textPlaybackPicPath.Text + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".jpg";
                    DPSDK_RES iRet = DPSDK_ConvertToJpegFile(m_iSessionID, m_iPlaybackMediaSessionID, ref pConvertJPEG);
                    if (iRet == DPSDK_SUCCESS)
                    {
                        pictureBoxPlaybackPicture.Image = Image.FromFile(pConvertJPEG.szFileName);
                        textPlaybackResult.Text = "ConvertToJpegFile success.";
                    }
                    else
                    {
                        textPlaybackResult.Text = "ConvertToJpegFile failed, error code = " + iRet.ToString();
                    }
                    m_iConvertType = -1;
                }
            }
        }

        private void ResetPlaybackDrawID(int iWindowID)
        {
            int iIndex = iWindowID - 1;
            m_vecPlaybackDrawTitleID[iIndex] = IntPtr.Zero;
            m_vecPlaybackDrawLocalTimeID[iIndex] = IntPtr.Zero;
            m_vecPlaybackDrawChannelNameID[iIndex] = IntPtr.Zero;
        }

        private void checkBoxPlaybackShowTitle_CheckedChanged(object sender, EventArgs e)
        {
            string strOSDInfo = textBoxPlaybackOSDTitle.Text;
            string strTitleX = titleX.Text;
            string strTitleY = titleY.Text;
            int iX, iY;
            if (strOSDInfo == "" || !int.TryParse(strTitleX, out iX) || !int.TryParse(strTitleY, out iY))
            {
                textPlaybackResult.Text = AppData._CS("Invalid parameter");
                return;
            }

            byte[] strUFT8OSDInfo = Encoding.GetEncoding("utf-8").GetBytes(strOSDInfo);
            if (strUFT8OSDInfo.Length > DPSDK_MEMO_LEN)
            {
                textPlaybackResult.Text = AppData._CS("Exceeded maximum character length");
                return;
            }

            CheckBox checkBox = (CheckBox)sender;
            bool bDraw = checkBox.Checked ? true : false;

            string strDisPlayWindows = comPlayBackWindows.Text;
            if (strDisPlayWindows.Length < 0)
            {
                textPlaybackResult.Text = AppData._CS("Display Windows cannot be empty");
                comPlayBackWindows.Focus();
                return;
            }

            string strXmlData = "";
            ArrayList arrDisPlayWindows = new ArrayList();
            StringSplit(strDisPlayWindows, ":", ref arrDisPlayWindows);

            string strWindowNumList = arrDisPlayWindows[1].ToString();
            ArrayList arrWindowNumList = new ArrayList();
            StringSplit(strWindowNumList, ",", ref arrWindowNumList);
            if (arrWindowNumList.Count > 0)
            {
                DPSDK_RES iRet = -1;
                DPSDK_EXTRA_OSD_PARAM pExtraOSDParam = new DPSDK_EXTRA_OSD_PARAM();
                pExtraOSDParam.iX = iX;
                pExtraOSDParam.iY = iY;
                pExtraOSDParam.szOSDInfo = Encoding.UTF8.GetString(strUFT8OSDInfo);

                for (int i = 0; i < arrWindowNumList.Count; i++)
                {
                    switch (Convert.ToInt32(arrWindowNumList[i]))
                    {
                        case 1: // Window 1 
                            if (bDraw)
                            {
                                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawTitleID[0];
                                if (iDrawID == IntPtr.Zero)
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                                }
                                else
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                                }
                          
                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPlaybackDrawTitleID[0] = iDrawID;
                                    strXmlData = strXmlData + "Draw title in Window 1 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Draw title in Window 1 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            else
                            {
                                pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_DELETE;
                                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawTitleID[0];
                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPlaybackDrawTitleID[0] = IntPtr.Zero;
                                    strXmlData = strXmlData + "Clear title in Window 1 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Clear title in Window 1 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            break;

                        case 2: // Window 2 
                            if (bDraw)
                            {
                                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawTitleID[1];
                                if (iDrawID == IntPtr.Zero)
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                                }
                                else
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                                }

                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID2, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPlaybackDrawTitleID[1] = iDrawID;
                                    strXmlData = strXmlData + "Draw title in Window 2 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Draw title in Window 2 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            else
                            {
                                pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_DELETE;
                                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawTitleID[1];
                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID2, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPlaybackDrawTitleID[1] = IntPtr.Zero;
                                    strXmlData = strXmlData + "Clear title in Window 2 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Clear title in Window 2 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            break;

                        case 3: // Window 3 
                            if (bDraw)
                            {
                                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawTitleID[2];
                                if (iDrawID == IntPtr.Zero)
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                                }
                                else
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                                }

                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID3, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPlaybackDrawTitleID[2] = iDrawID;
                                    strXmlData = strXmlData + "Draw title in Window 3 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Draw title in Window 3 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            else
                            {
                                pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_DELETE;
                                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawTitleID[2];
                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID3, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPlaybackDrawTitleID[2] = IntPtr.Zero;
                                    strXmlData = strXmlData + "Clear title in Window 3 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Clear title in Window 3 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            break;

                        case 4: // Window 4 
                            if (bDraw)
                            {
                                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawTitleID[3];
                                if (iDrawID == IntPtr.Zero)
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                                }
                                else
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                                }

                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID4, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPlaybackDrawTitleID[3] = iDrawID;
                                    strXmlData = strXmlData + "Draw title in Window 4 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Draw title in Window 4 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            else
                            {
                                pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_DELETE;
                                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawTitleID[3];
                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID4, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPlaybackDrawTitleID[3] = IntPtr.Zero;
                                    strXmlData = strXmlData + "Clear title in Window 4 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Clear title in Window 4 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            break;

                        default:
                            break;
                    }
                }
            }
            textPlaybackResult.Text = strXmlData;
        }

        private void StartLocalTimer()
        {
            m_localTimer.Elapsed += new ElapsedEventHandler(UpdateLocalTimeInfo);
            m_localTimer.Enabled = true;
            m_localTimer.Start();
        }

        private void StopLocalTimer()
        {
            m_localTimer.Elapsed -= new ElapsedEventHandler(UpdateLocalTimeInfo);
            m_localTimer.Enabled = false;
            m_localTimer.Stop();
        }

        private void checkBoxPlaybackShowLocalTime_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox checkBox = (CheckBox)sender;
            bool bDrawLocalTime = checkBox.Checked ? true : false;
            SetDrawFlags(bDrawLocalTime);
            if (bDrawLocalTime)
            {
                if (!m_localTimer.Enabled)
                {
                    StartLocalTimer();
                }
            }
            else
            {
                ClearLocalTime();

                if (!m_bPlaybackDrawLocalTime && !m_bPlaybackDrawLocalTime2
                    && !m_bPlaybackDrawLocalTime3 && !m_bPlaybackDrawLocalTime4)
                {
                    StopLocalTimer();
                }
            }
        }

        private void SetDrawFlags(bool bDraw)
        {
            string strDisPlayWindows = comPlayBackWindows.Text;
            if (strDisPlayWindows.Length < 0)
            {
                textPlaybackResult.Text = AppData._CS("Display Windows cannot be empty");
                comPlayBackWindows.Focus();
                return;
            }

            ArrayList arrDisPlayWindows = new ArrayList();
            StringSplit(strDisPlayWindows, ":", ref arrDisPlayWindows);

            string strWindowNumList = arrDisPlayWindows[1].ToString();
            ArrayList arrWindowNumList = new ArrayList();
            StringSplit(strWindowNumList, ",", ref arrWindowNumList);
            if (arrWindowNumList.Count > 0)
            {
                for (int i = 0; i < arrWindowNumList.Count; i++)
                {
                    switch (Convert.ToInt32(arrWindowNumList[i]))
                    {
                        case 1: // Window 1 
                            m_bPlaybackDrawLocalTime = bDraw;
                            break;
                        case 2:
                            m_bPlaybackDrawLocalTime2 = bDraw;
                            break;
                        case 3:
                            m_bPlaybackDrawLocalTime3 = bDraw;
                            break;
                        case 4:
                            m_bPlaybackDrawLocalTime4 = bDraw;
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        private string TransToGBK(string str)
        {
            Encoding gbkEncoding = Encoding.GetEncoding("GBK");
            byte[] gbkBytes = gbkEncoding.GetBytes(str);
            return gbkEncoding.GetString(gbkBytes);
        }

        private void DrawLocalTime()
        {
            string strLocalTime = DateTime.Now.ToLocalTime().ToString();

            string strLocalTimeX = localTimeX.Text;
            string strLocalTimeY = localTimeY.Text;
            int iX, iY;
            if (strLocalTime == "" || !int.TryParse(strLocalTimeX, out iX) || !int.TryParse(strLocalTimeY, out iY))
            {
                textPlaybackResult.Text = AppData._CS("Invalid parameter");
                return;
            }
            byte[] utf8Bytes = Encoding.UTF8.GetBytes(strLocalTime);

            string strXmlData = "";
            DPSDK_RES iRet = -1;
            DPSDK_EXTRA_OSD_PARAM pExtraOSDParam = new DPSDK_EXTRA_OSD_PARAM();
            pExtraOSDParam.iX = iX;
            pExtraOSDParam.iY = iY;

            pExtraOSDParam.szOSDInfo = Encoding.UTF8.GetString(utf8Bytes);

            if (m_bPlaybackDrawLocalTime)
            {
                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawLocalTimeID[0];
                if(iDrawID == IntPtr.Zero)
                {
                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                }
                else
                {
                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                }

                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID, ref iDrawID, ref pExtraOSDParam);
                if (iRet == DPSDK_SUCCESS)
                {
                    m_vecPlaybackDrawLocalTimeID[0] = iDrawID;
                    strXmlData = strXmlData + "Draw Local Time in Window 1 success \r\n";
                }
                else
                {
                    strXmlData = strXmlData + "Draw Local TIme in Window 1 failed, iRet = " + iRet.ToString() + "\r\n";
                }
            }

            if (m_bPlaybackDrawLocalTime2)
            {
                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawLocalTimeID[1];
                if (iDrawID == IntPtr.Zero)
                {
                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                }
                else
                {
                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                }

                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID2, ref iDrawID, ref pExtraOSDParam);
                if (iRet == DPSDK_SUCCESS)
                {
                    m_vecPlaybackDrawLocalTimeID[1] = iDrawID;
                    strXmlData = strXmlData + "Draw Local Time in Window 2 success \r\n";
                }
                else
                {
                    strXmlData = strXmlData + "Draw Local TIme in Window 2 failed, iRet = " + iRet.ToString() + "\r\n";
                }
            }

            if (m_bPlaybackDrawLocalTime3)
            {
                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawLocalTimeID[2];
                if (iDrawID == IntPtr.Zero)
                {
                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                }
                else
                {
                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                }

                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID3, ref iDrawID, ref pExtraOSDParam);
                if (iRet == DPSDK_SUCCESS)
                {
                    m_vecPlaybackDrawLocalTimeID[2] = iDrawID;
                    strXmlData = strXmlData + "Draw Local Time in Window 3 success \r\n";
                }
                else
                {
                    strXmlData = strXmlData + "Draw Local TIme in Window 3 failed, iRet = " + iRet.ToString() + "\r\n";
                }
            }

            if (m_bPlaybackDrawLocalTime4)
            {
                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawLocalTimeID[3];
                if (iDrawID == IntPtr.Zero)
                {
                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                }
                else
                {
                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                }

                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID4, ref iDrawID, ref pExtraOSDParam);
                if (iRet == DPSDK_SUCCESS)
                {
                    m_vecPlaybackDrawLocalTimeID[3] = iDrawID;
                    strXmlData = strXmlData + "Draw Local Time in Window 4 success \r\n";
                }
                else
                {
                    strXmlData = strXmlData + "Draw Local TIme in Window 4 failed, iRet = " + iRet.ToString() + "\r\n";
                }
            }

            textPlaybackResult.Text = strXmlData;
        }

        private void ClearLocalTime()
        {
            string strXmlData = "";
            DPSDK_RES iRet = -1;
            DPSDK_EXTRA_OSD_PARAM pExtraOSDParam = new DPSDK_EXTRA_OSD_PARAM();
            pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_DELETE;

            if (!m_bPlaybackDrawLocalTime)
            {
                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawLocalTimeID[0];
                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID, ref iDrawID, ref pExtraOSDParam);
                if (iRet == DPSDK_SUCCESS)
                {
                    m_vecPlaybackDrawLocalTimeID[0] = IntPtr.Zero;
                    strXmlData = strXmlData + "Clear Local Time in Window 1 success \r\n";
                }
                else
                {
                    strXmlData = strXmlData + "Clear Local TIme in Window 1 failed, iRet = " + iRet.ToString() + "\r\n";
                }
            }

            if (!m_bPlaybackDrawLocalTime2)
            {
                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawLocalTimeID[1];
                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID2, ref iDrawID, ref pExtraOSDParam);
                if (iRet == DPSDK_SUCCESS)
                {
                    m_vecPlaybackDrawLocalTimeID[1] = IntPtr.Zero;
                    strXmlData = strXmlData + "Clear Local Time in Window 2 success \r\n";
                }
                else
                {
                    strXmlData = strXmlData + "Clear Local TIme in Window 2 failed, iRet = " + iRet.ToString() + "\r\n";
                }
            }

            if (!m_bPlaybackDrawLocalTime3)
            {
                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawLocalTimeID[2];
                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID3, ref iDrawID, ref pExtraOSDParam);
                if (iRet == DPSDK_SUCCESS)
                {
                    m_vecPlaybackDrawLocalTimeID[2] = IntPtr.Zero;
                    strXmlData = strXmlData + "Clear Local Time in Window 3 success \r\n";
                }
                else
                {
                    strXmlData = strXmlData + "Clear Local TIme in Window 3 failed, iRet = " + iRet.ToString() + "\r\n";
                }
            }

            if (!m_bPlaybackDrawLocalTime4)
            {
                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawLocalTimeID[3];
                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID4, ref iDrawID, ref pExtraOSDParam);
                if (iRet == DPSDK_SUCCESS)
                {
                    m_vecPlaybackDrawLocalTimeID[3] = IntPtr.Zero;
                    strXmlData = strXmlData + "Clear Local Time in Window 4 success \r\n";
                }
                else
                {
                    strXmlData = strXmlData + "Clear Local TIme in Window 4 failed, iRet = " + iRet.ToString() + "\r\n";
                }
            }

            textPlaybackResult.Text = strXmlData;
        }

        private void UpdateLocalTimeInfo(object sender, ElapsedEventArgs e)
        {
            if (m_iPlaybackMediaSessionID == IntPtr.Zero && m_iPlaybackMediaSessionID2 == IntPtr.Zero
                 && m_iPlaybackMediaSessionID3 == IntPtr.Zero && m_iPlaybackMediaSessionID4 == IntPtr.Zero)
            {
                StopLocalTimer();
            }

            DrawLocalTime();
        }

        private void checkBoxPlaybackShowChannelName_CheckedChanged(object sender, EventArgs e)
        {
            string strChannelNameX = channelNameX.Text;
            string strChannelNameY = channelNameY.Text;
            int iX, iY;
            if (!int.TryParse(strChannelNameX, out iX) || !int.TryParse(strChannelNameY, out iY))
            {
                textPlaybackResult.Text = AppData._CS("Invalid parameter");
                return;
            }

            CheckBox checkBox = (CheckBox)sender;
            bool bDraw = checkBox.Checked ? true : false;

            string strDisPlayWindows = comPlayBackWindows.Text;
            if (strDisPlayWindows.Length < 0)
            {
                textPlaybackResult.Text = AppData._CS("Display Windows cannot be empty");
                comPlayBackWindows.Focus();
                return;
            }

            string strXmlData = "";
            ArrayList arrDisPlayWindows = new ArrayList();
            StringSplit(strDisPlayWindows, ":", ref arrDisPlayWindows);

            string strWindowNumList = arrDisPlayWindows[1].ToString();
            ArrayList arrWindowNumList = new ArrayList();
            StringSplit(strWindowNumList, ",", ref arrWindowNumList);
            if (arrWindowNumList.Count > 0)
            {
                DPSDK_RES iRet = -1;
                DPSDK_EXTRA_OSD_PARAM pExtraOSDParam = new DPSDK_EXTRA_OSD_PARAM();
                pExtraOSDParam.iX = iX;
                pExtraOSDParam.iY = iY;

                for (int i = 0; i < arrWindowNumList.Count; i++)
                {
                    switch (Convert.ToInt32(arrWindowNumList[i]))
                    {
                        case 1: // Window 1 
                            if (!m_bLocalPlayback && bDraw)
                            {
                                byte[] strUFT8OSDInfo = Encoding.GetEncoding("utf-8").GetBytes(GetChannelName(m_strPlaybackChannelID));
                                if (strUFT8OSDInfo.Length > DPSDK_MEMO_LEN)
                                {
                                    textPlaybackResult.Text = AppData._CS("Exceeded maximum character length");
                                    break;
                                }
                                pExtraOSDParam.szOSDInfo = m_strPlaybackChannelID;
                                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawChannelNameID[0];
                                if (iDrawID == IntPtr.Zero)
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                                }
                                else
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                                }

                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPlaybackDrawChannelNameID[0] = iDrawID;
                                    strXmlData = strXmlData + "Draw channel name in Window 1 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Draw channel name in Window 1 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            else if (!m_bLocalPlayback && !bDraw)
                            {
                                pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_DELETE;
                                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawChannelNameID[0];
                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPlaybackDrawChannelNameID[0] = IntPtr.Zero;
                                    strXmlData = strXmlData + "Clear channel name in Window 1 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Clear channel name in Window 1 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            break;

                        case 2: // Window 2 
                            if (!m_bLocalPlayback2 && bDraw)
                            {
                                byte[] strUFT8OSDInfo = Encoding.GetEncoding("utf-8").GetBytes(GetChannelName(m_strPlaybackChannelID2));
                                if (strUFT8OSDInfo.Length > DPSDK_MEMO_LEN)
                                {
                                    textPlaybackResult.Text = AppData._CS("Exceeded maximum character length");
                                    break;
                                }
                                pExtraOSDParam.szOSDInfo = m_strPlaybackChannelID2;

                                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawChannelNameID[1];
                                if (iDrawID == IntPtr.Zero)
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                                }
                                else
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                                }
             
                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID2, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPlaybackDrawChannelNameID[1] = iDrawID;
                                    strXmlData = strXmlData + "Draw channel name in Window 2 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Draw channel name in Window 2 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            else if (!m_bLocalPlayback2 && !bDraw)
                            {
                                pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_DELETE;
                                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawChannelNameID[1];
                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID2, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPlaybackDrawChannelNameID[1] = IntPtr.Zero;
                                    strXmlData = strXmlData + "Clear channel name in Window 2 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Clear channel name in Window 2 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            break;

                        case 3: // Window 3 
                            if (!m_bLocalPlayback3 && bDraw)
                            {
                                byte[] strUFT8OSDInfo = Encoding.GetEncoding("utf-8").GetBytes(GetChannelName(m_strPlaybackChannelID3));
                                if (strUFT8OSDInfo.Length > DPSDK_MEMO_LEN)
                                {
                                    textPlaybackResult.Text = AppData._CS("Exceeded maximum character length");
                                    break;
                                }
                                pExtraOSDParam.szOSDInfo = m_strPlaybackChannelID3;

                                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawChannelNameID[2];
                                if (iDrawID == IntPtr.Zero)
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                                }
                                else
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                                }

                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID3, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPlaybackDrawChannelNameID[2] = iDrawID;
                                    strXmlData = strXmlData + "Draw channel name in Window 3 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Draw channel name in Window 3 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            else if (!m_bLocalPlayback3 && !bDraw)
                            {
                                pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_DELETE;
                                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawChannelNameID[2];
                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID3, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPlaybackDrawChannelNameID[2] = IntPtr.Zero;
                                    strXmlData = strXmlData + "Clear channel name in Window 3 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Clear channel name in Window 3 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            break;

                        case 4: // Window 4 
                            if (!m_bLocalPlayback4 && bDraw)
                            {
                                byte[] strUFT8OSDInfo = Encoding.GetEncoding("utf-8").GetBytes(GetChannelName(m_strPlaybackChannelID4));
                                if (strUFT8OSDInfo.Length > DPSDK_MEMO_LEN)
                                {
                                    textPlaybackResult.Text = AppData._CS("Exceeded maximum character length");
                                    break;
                                }
                                pExtraOSDParam.szOSDInfo = m_strPlaybackChannelID4;

                                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawChannelNameID[3];
                                if (iDrawID == IntPtr.Zero)
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_ADD;
                                }
                                else
                                {
                                    pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_UPDATE;
                                }

                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID4, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPlaybackDrawChannelNameID[3] = iDrawID;
                                    strXmlData = strXmlData + "Draw channel name in Window 4 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Draw channel name in Window 4 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            else if (!m_bLocalPlayback4 && !bDraw)
                            {
                                pExtraOSDParam.iOperationType = DPSDK_EXTRA_OSD_OPERATION_TYPE.DPSDK_EXTRA_OSD_DELETE;
                                IntPtr iDrawID = (IntPtr)m_vecPlaybackDrawChannelNameID[3];
                                iRet = DPSDK_SetExtraOSDInfo(m_iSessionID, m_iPlaybackMediaSessionID4, ref iDrawID, ref pExtraOSDParam);
                                if (iRet == DPSDK_SUCCESS)
                                {
                                    m_vecPlaybackDrawChannelNameID[3] = IntPtr.Zero;
                                    strXmlData = strXmlData + "Clear channel name in Window 4 success \r\n";
                                }
                                else
                                {
                                    strXmlData = strXmlData + "Clear channel name in Window 4 failed, iRet = " + iRet.ToString() + "\r\n";
                                }
                            }
                            break;

                        default:
                            break;
                    }
                }
            }
            textPlaybackResult.Text = strXmlData;
        }
    }
}