private void CreateUserVideo(int m_UserID) { //用户视频横列显示规则 int userCount = GetUserCount(); if (userCount > 0 && userCount <= 1) { m_ColUserVideo = 1; } else if (userCount > 1 && userCount <= 4) { m_ColUserVideo = 2; } else if (userCount > 4) { m_ColUserVideo = 3; } int m_PnlCount = pnl_video.Controls.Count; Panel m_ShowWnd = new Panel(); m_ShowWnd.Name = m_UserID.ToString(); m_ShowWnd.Width = (pnl_video.Width) / m_ColUserVideo; m_ShowWnd.Height = m_ShowWnd.Width * 4 / 5; m_ShowWnd.Top = m_PnlCount / m_ColUserVideo * m_ShowWnd.Height + m_PnlCount / m_ColUserVideo * 5; m_ShowWnd.Left = m_PnlCount % m_ColUserVideo * m_ShowWnd.Width + m_PnlCount % m_ColUserVideo * 5; m_ShowWnd.BackColor = Color.Black; m_ShowWnd.DoubleClick += new EventHandler(ShowWnd_DoubleClick); m_ShowWnd.MouseEnter += new EventHandler(m_ShowWnd_MouseEnter); m_ShowWnd.MouseLeave += new EventHandler(m_ShowWnd_MouseLeave); pnl_video.Controls.Add(m_ShowWnd); AnyChatCoreSDK.SetVideoPos(m_UserID, m_ShowWnd.Handle, 0, 0, m_ShowWnd.Width, m_ShowWnd.Height); AnyChatCoreSDK.UserCameraControl(m_UserID, true); AnyChatCoreSDK.UserSpeakControl(m_UserID, true); }
//操作双方音视频设备 public void OpenCameraAndSpeak(int userId, bool isopen) { if (isopen) { //打开呼叫者音视频 AnyChatCoreSDK.UserCameraControl(userId, true); AnyChatCoreSDK.UserSpeakControl(userId, true); if (userId == m_UserId) { AnyChatCoreSDK.SetVideoPos(userId, pic_suserVideo.Handle, 0, 0, pic_suserVideo.Width, pic_suserVideo.Height); } else { AnyChatCoreSDK.SetVideoPos(userId, pic_tuserVideo.Handle, 0, 0, pic_tuserVideo.Width, pic_tuserVideo.Height); } } else { AnyChatCoreSDK.UserCameraControl(userId, false); AnyChatCoreSDK.UserSpeakControl(userId, false); } //音量刷新 timer_speak.Enabled = isopen; }
private void ShowWnd_DoubleClick(object sender, EventArgs e) { Control ShowWnd = (Control)sender; if (ShowWnd.Width != m_VideoWidthMax) { foreach (Control C in pnl_video.Controls) { if (C.Name != ShowWnd.Name) { C.Visible = false; } } ShowWnd.Top = (pnl_video.Height - m_VideoHeightMax) / 2; ShowWnd.Left = (pnl_video.Width - m_VideoWidthMax) / 2;; ShowWnd.Width = m_VideoWidthMax; ShowWnd.Height = m_VideoHeightMax; } else { foreach (Control C in pnl_video.Controls) { if (C.Name != ShowWnd.Name) { C.Visible = true; } } int m_TempIndex = 0; for (int i = 0; i < m_MemberItem.Count; i++) { if (ShowWnd.Name == m_MemberItem[i].m_UserID.ToString()) { m_TempIndex = m_MemberItem[i].m_Index; break; } } ShowWnd.Width = (pnl_video.Width) / m_ColUserVideo; ShowWnd.Height = ShowWnd.Width * 4 / 5; ShowWnd.Top = m_TempIndex / m_ColUserVideo * ShowWnd.Height + m_TempIndex / m_ColUserVideo * 5; ShowWnd.Left = m_TempIndex % m_ColUserVideo * ShowWnd.Width + m_TempIndex % m_ColUserVideo * 5; } AnyChatCoreSDK.SetVideoPos(Convert.ToInt32(ShowWnd.Name), ShowWnd.Handle, 0, 0, ShowWnd.Width, ShowWnd.Height); }
// 打开远程用户的音频、视频 public void OpenRemoteUserVideo() { if (g_otherUserId != -1) { return; } // 获取当前房间用户列表 int usercount = 0; AnyChatCoreSDK.GetOnlineUser(null, ref usercount); if (usercount > 0) { int[] useridarray = new int[usercount]; AnyChatCoreSDK.GetOnlineUser(useridarray, ref usercount); for (int i = 0; i < usercount; i++) { // 判断该用户的视频是否已打开 int usercamerastatus = 0; if (AnyChatCoreSDK.QueryUserState(useridarray[i], AnyChatCoreSDK.BRAC_USERSTATE_CAMERA, ref usercamerastatus, sizeof(int)) != 0) { continue; } if (usercamerastatus == 2) { g_otherUserId = useridarray[i]; //获取控件的句柄 IntPtr mHandle = ((HwndSource)PresentationSource.FromVisual(this.remoteVideoImage)).Handle; //获取控件的位置 Window window = Window.GetWindow(this.remoteVideoImage); Point point = this.remoteVideoImage.TransformToAncestor(window).Transform(new Point(0, 0)); //显示视频 AnyChatCoreSDK.SetVideoPos(g_otherUserId, mHandle, (int)point.X, (int)point.Y, (int)(this.remoteVideoImage.Width + point.X), (int)(this.remoteVideoImage.Height + point.Y)); AnyChatCoreSDK.UserSpeakControl(g_otherUserId, true); AnyChatCoreSDK.UserCameraControl(g_otherUserId, true); break; } } } }
private void pnl_local_MouseDoubleClick(object sender, MouseEventArgs e) { Control ShowWnd = (Control)sender; if (ShowWnd.Width != m_VideoWidthMax) { foreach (Control C in pnl_video.Controls) { if (C.Name != ShowWnd.Name) { C.Visible = false; } } ShowWnd.Top = (pnl_video.Height - m_VideoHeightMax) / 2; ShowWnd.Left = (pnl_video.Width - m_VideoWidthMax) / 2;; ShowWnd.Width = m_VideoWidthMax; ShowWnd.Height = m_VideoHeightMax; pnl_video.Controls.Add(ShowWnd); } else { foreach (Control C in pnl_video.Controls) { if (C.Name != ShowWnd.Name) { C.Visible = true; } } ShowWnd.Width = 167; ShowWnd.Height = 140; ShowWnd.Top = 471; ShowWnd.Left = 3; this.Controls.Add(ShowWnd); } //AnyChatCoreSDK.SetVideoPos(Convert.ToInt32(ShowWnd.Name), ShowWnd.Handle, 0, 0, ShowWnd.Width, ShowWnd.Height); int ret = AnyChatCoreSDK.SetVideoPos(-1, pnl_local.Handle, 0, 0, ShowWnd.Width, ShowWnd.Height); }
protected override void WndProc(ref Message m) { if (m.Msg == ANYCHATAPI.AnyChatCoreSDK.WM_GV_CONNECT) { ///连接 int succed = m.WParam.ToInt32(); if (succed == 1) { Print("连接成功!"); } else { Print("连接失败,错误号:" + m.LParam.ToInt32()); } } else if (m.Msg == ANYCHATAPI.AnyChatCoreSDK.WM_GV_LOGINSYSTEM) { ///登录系统 int userid = m.WParam.ToInt32(); if (m.LParam.ToInt32() == 0) { Print("登录服务器成功,自己的用户编号为:" + userid.ToString()); m_myUserID = userid; AnyChatCoreSDK.EnterRoom(m_RoomID, "", 0); } else { Print("登录服务器失败,代码出错为:" + m.LParam.ToInt32()); } } else if (m.Msg == ANYCHATAPI.AnyChatCoreSDK.WM_GV_ENTERROOM) { ///进入房间 if (m.LParam.ToInt32() == 0) { int roomid = m.WParam.ToInt32(); Print("进入房间成功,房间编号为:" + roomid.ToString()); m_RoomID = roomid; int ret = ANYCHATAPI.AnyChatCoreSDK.SetVideoPos(-1, pictureBox2.Handle, 0, 0, pictureBox2.Width, pictureBox2.Height); ret = ANYCHATAPI.AnyChatCoreSDK.UserCameraControl(-1, true); ret = ANYCHATAPI.AnyChatCoreSDK.UserSpeakControl(-1, true); } else { Print("申请进入房间失败,出错代码为:" + m.LParam.ToInt32()); } } else if (m.Msg == ANYCHATAPI.AnyChatCoreSDK.WM_GV_ONLINEUSER) { ///目前在房间的用户列表 int usrcnt = m.WParam.ToInt32(); Print("在编号为:" + m.LParam.ToInt32() + " 的房间共有" + usrcnt + "位在线用户"); int cnt = 0; ANYCHATAPI.AnyChatCoreSDK.GetOnlineUser(null, ref cnt); int [] userArr = new int[cnt]; ANYCHATAPI.AnyChatCoreSDK.GetOnlineUser(userArr, ref cnt); if (userArr.Length >= 1 && !videoOpenTag) { m_tempUserID = userArr[0]; int ret = ANYCHATAPI.AnyChatCoreSDK.SetVideoPos(userArr[0], pictureBox1.Handle, 0, 0, pictureBox1.Width, pictureBox1.Height); ret = ANYCHATAPI.AnyChatCoreSDK.UserCameraControl(userArr[0], true); ret = ANYCHATAPI.AnyChatCoreSDK.UserSpeakControl(userArr[0], true); if (ret == 0) { videoOpenTag = true; } } } else if (m.Msg == ANYCHATAPI.AnyChatCoreSDK.WM_GV_USERATROOM) { ///新用户加入或退出房间 int userID = m.WParam.ToInt32(); int boEntered = m.LParam.ToInt32(); if (boEntered == 1 && !videoOpenTag) { int ret = -1; m_others.Add(userID); Print("用户编号为:" + userID + " 进入房间"); m_tempUserID = userID; ret = AnyChatCoreSDK.SetVideoPos(userID, this.pictureBox1.Handle, 0, 0, pictureBox1.Width, pictureBox1.Height); ret = AnyChatCoreSDK.UserCameraControl(userID, true); ret = AnyChatCoreSDK.UserSpeakControl(userID, true); if (ret == 0) { videoOpenTag = true; } } else { m_others.Remove(userID); Print("用户编号为:" + userID + " 离开房间"); } } base.WndProc(ref m); }
protected override void WndProc(ref Message m) { if (m.Msg == AnyChatCoreSDK.WM_GV_CONNECT) { ///连接 int succed = m.WParam.ToInt32(); if (succed == 1) { Print("连接成功!"); } else { Print("连接失败,错误号:" + m.LParam.ToInt32()); } } else if (m.Msg == AnyChatCoreSDK.WM_GV_LOGINSYSTEM) { ///登录系统 int userid = m.WParam.ToInt32(); if (m.LParam.ToInt32() == 0) { //Print("登录服务器成功,自己的用户编号为:" + userid.ToString()); Print("登录服务器成功,自己的用户账号为:" + frmLogin.m_UserName); m_myUserID = userid; StringBuilder userName = new StringBuilder(30); int ret = AnyChatCoreSDK.GetUserName(userid, userName, 30); AnyChatCoreSDK.EnterRoom(m_RoomID, "", 0); } else { Print("登录服务器失败,代码出错为:" + m.LParam.ToInt32()); } } else if (m.Msg == AnyChatCoreSDK.WM_GV_ENTERROOM) { ///进入房间 if (m.LParam.ToInt32() == 0) { int roomid = m.WParam.ToInt32(); Print("进入房间成功,房间编号为:" + roomid.ToString()); m_RoomID = roomid; int ret = AnyChatCoreSDK.SetVideoPos(-1, pnl_local.Handle, 0, 0, pnl_local.Width, pnl_local.Height); ret = AnyChatCoreSDK.UserCameraControl(-1, true); ret = AnyChatCoreSDK.UserSpeakControl(-1, true); } else { Print("申请进入房间失败,出错代码为:" + m.LParam.ToInt32()); } } else if (m.Msg == AnyChatCoreSDK.WM_GV_ONLINEUSER) { ///目前在房间的用户列表 int usrcnt = m.WParam.ToInt32(); Print("在编号为:" + m.LParam.ToInt32() + " 的房间共有" + usrcnt + "位在线用户"); Print("当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")); int cnt = 0; //在线用户数量 AnyChatCoreSDK.GetOnlineUser(null, ref cnt); //获取在线用户数量 int[] userArr = new int[cnt]; //在线用户ID AnyChatCoreSDK.GetOnlineUser(userArr, ref cnt); //获取在线用户ID数组 if (userArr.Length >= 1) { for (int i = 0; i < userArr.Length; i++) { m_others.Add(userArr[i]); //存入在线用户列表中(开发包带的用户在线ID数组) UpdateUserList(userArr[i], i); //添加到在线用户列表(自定义,带用户属性结构) } UpdateOnlineUser(); } else { dgv_onlineuser.Rows.Clear(); DataGridViewRow dgvr = new DataGridViewRow(); dgv_onlineuser.Rows.Add(dgvr); dgvr = dgv_onlineuser.Rows[dgv_onlineuser.Rows.Count - 1]; dgvr.Cells["gvc_username"].Value = "大家"; dgvr.Cells["gvc_audio"].Value = AnyChatCSharpDemo.Properties.Resources.深灰背景; dgvr.Cells["gvc_video"].Value = AnyChatCSharpDemo.Properties.Resources.深灰背景; dgvr.Cells["gvc_character"].Value = AnyChatCSharpDemo.Properties.Resources.深灰背景; dgvr = new DataGridViewRow(); dgv_onlineuser.Rows.Add(dgvr); dgvr = dgv_onlineuser.Rows[dgv_onlineuser.Rows.Count - 1]; dgvr.Cells["gvc_username"].Value = frmLogin.m_UserName;; dgvr.Cells["gvc_character"].Value = AnyChatCSharpDemo.Properties.Resources.主持人_浅灰; dgvr.Cells["gvc_audio"].Value = AnyChatCSharpDemo.Properties.Resources.麦克; dgvr.Cells["gvc_video"].Value = AnyChatCSharpDemo.Properties.Resources.摄像头; } } else if (m.Msg == AnyChatCoreSDK.WM_GV_USERATROOM) { ///新用户加入或退出房间 int userID = m.WParam.ToInt32(); int boEntered = m.LParam.ToInt32(); if (boEntered == 1) { m_others.Add(userID); UpdateUserList(userID, m_others.Count - 1); Print("用户:" + GetUserNameByID(userID) + " 进入房间"); } else { m_others.Remove(userID); for (int i = 0; i < m_MemberItem.Count; i++) { if (m_MemberItem[i].m_UserID == userID) { m_MemberItem.RemoveAt(i); break; } } Print("用户:" + GetUserNameByID(userID) + " 离开房间"); } UpdateOnlineUser(); //更新用户列表 this.Invalidate(); //重绘界面,防止界面假死 } base.WndProc(ref m); }
// 委托,可以操作界面 public void NotifyMessageCallbackDelegate(int dwNotifyMsg, int wParam, int lParam, int userValue) { switch (dwNotifyMsg) { case AnyChatCoreSDK.WM_GV_CONNECT: if (wParam != 0) { msglabel.Content = "连接服务器成功"; } else { msglabel.Content = "连接服务器失败"; } break; case AnyChatCoreSDK.WM_GV_LOGINSYSTEM: if (lParam == 0) { g_selfUserId = wParam; msglabel.Content = "登录成功"; } else { msglabel.Content = "登录失败, ErrorCode:" + lParam; } break; case AnyChatCoreSDK.WM_GV_ENTERROOM: if (lParam == 0) { msglabel.Content = "进入房间成功"; //获取控件的句柄 IntPtr mHandle = ((HwndSource)PresentationSource.FromVisual(this.localVideoImage)).Handle; //获取控件的位置 Window window = Window.GetWindow(this.localVideoImage); Point point = this.localVideoImage.TransformToAncestor(window).Transform(new Point(0, 0)); //显示视频 AnyChatCoreSDK.SetVideoPos(-1, mHandle, (int)point.X, (int)point.Y, (int)(this.localVideoImage.Width + point.X), (int)(this.localVideoImage.Height + point.Y)); AnyChatCoreSDK.UserSpeakControl(-1, true); AnyChatCoreSDK.UserCameraControl(-1, true); } else { msglabel.Content = "进入房间失败, ErrorCode:" + lParam; } break; case AnyChatCoreSDK.WM_GV_ONLINEUSER: OpenRemoteUserVideo(); break; case AnyChatCoreSDK.WM_GV_USERATROOM: if (lParam != 0) // 其它用户进入房间 { OpenRemoteUserVideo(); } else // 其它用户离开房间 { if (wParam == g_otherUserId) { g_otherUserId = -1; OpenRemoteUserVideo(); } } break; case AnyChatCoreSDK.WM_GV_LINKCLOSE: msglabel.Content = "网络连接关闭, ErrorCode:" + lParam; break; default: break; } }
//操作双方音视频设备 public void OpenCameraAndSpeak(int userId, bool isopen) { if (isopen) { #region 打开屏幕共享功能的逻辑代码 /* * //本人 * if (userId == m_UserId) * { * int deviceNum = 0; * AnyChatCoreSDK.EnumVideoCapture(null, ref deviceNum); * IntPtr[] deviceList = new IntPtr[deviceNum]; * * AnyChatCoreSDK.EnumVideoCapture(deviceList, ref deviceNum); * for (int idx = 0; idx < deviceNum; idx++) * { * IntPtr intPtr = deviceList[idx]; * int len = 100; * byte[] byteArray = new byte[len]; * Marshal.Copy(intPtr, byteArray, 0, len); * string m_DeviceName = Encoding.Default.GetString(byteArray); * m_DeviceName = m_DeviceName.Substring(0, m_DeviceName.IndexOf('\0')); * * if (m_DeviceName.IndexOf("Native Screen Camera") > 0) * { * AnyChatCoreSDK.SelectVideoCapture(m_DeviceName); * * //设置视频参数 * int screenWidth = 1920; * int screenHeight = 1080; * AnyChatCoreSDK.SetSDKOption(AnyChatCoreSDK.BRAC_SO_LOCALVIDEO_WIDTHCTRL, ref screenWidth, sizeof(int)); * AnyChatCoreSDK.SetSDKOption(AnyChatCoreSDK.BRAC_SO_LOCALVIDEO_HEIGHTCTRL, ref screenHeight, sizeof(int)); * * int bitrateCtrl = 0; * AnyChatCoreSDK.SetSDKOption(AnyChatCoreSDK.BRAC_SO_LOCALVIDEO_BITRATECTRL, ref bitrateCtrl, sizeof(int)); * * int fpsCtrl = 15; * AnyChatCoreSDK.SetSDKOption(AnyChatCoreSDK.BRAC_SO_LOCALVIDEO_FPSCTRL, ref fpsCtrl, sizeof(int)); * * int gopCtrol = 60; * AnyChatCoreSDK.SetSDKOption(AnyChatCoreSDK.BRAC_SO_LOCALVIDEO_GOPCTRL, ref gopCtrol, sizeof(int)); * * int presetCtrl = 3; * AnyChatCoreSDK.SetSDKOption(AnyChatCoreSDK.BRAC_SO_LOCALVIDEO_PRESETCTRL, ref presetCtrl, sizeof(int)); * * int qualityCtrl = 3; * AnyChatCoreSDK.SetSDKOption(AnyChatCoreSDK.BRAC_SO_LOCALVIDEO_QUALITYCTRL, ref qualityCtrl, sizeof(int)); * * int m_AppLocal = 1; * AnyChatCoreSDK.SetSDKOption(AnyChatCoreSDK.BRAC_SO_LOCALVIDEO_APPLYPARAM, ref m_AppLocal, sizeof(int)); * * break; * } * } * } */ #endregion //打开呼叫者音视频 AnyChatCoreSDK.UserCameraControl(userId, true); AnyChatCoreSDK.UserSpeakControl(userId, true); if (userId == m_UserId) { AnyChatCoreSDK.SetVideoPos(userId, pic_suserVideo.Handle, 0, 0, pic_suserVideo.Width, pic_suserVideo.Height); } else { AnyChatCoreSDK.SetVideoPos(userId, pic_tuserVideo.Handle, 0, 0, pic_tuserVideo.Width, pic_tuserVideo.Height); } } else { AnyChatCoreSDK.UserCameraControl(userId, false); AnyChatCoreSDK.UserSpeakControl(userId, false); } //音量刷新 timer_speak.Enabled = isopen; }