public partial class RecordForm : Form { private MultimediaServer multimediaServer; private OMCS.Passive.Audio.MicrophoneConnector microphoneConnector; private OMCS.Passive.Video.dynamiccameraconnector dynamicCameraConnector; private IMultimediaManager multimediaManager; private BaseMaker maker; private System.Threading.Timer videoTimer; private recordmode recordMode = RecordMode.AudioAndVideo; public RecordForm(MultimediaServer server) { InitializeComponent(); this.comboBox_mode.SelectedIndex = 0; this.multimediaServer = server; this.label_port.Text = this.multimediaServer.Port.ToString(); //将服务端虚拟为一个OMCS客户端,并连接上OMCS服务器。 this.multimediaManager = MultimediaManagerFactory.GetSingleton(); this.multimediaManager.Initialize("_server", "", "127.0.0.1", this.multimediaServer.Port);//服务端以虚拟用户登录 } //在线用户列表 private void comboBox1_DropDown(object sender, EventArgs e) { List<string> list = this.multimediaServer.GetOnlineUserList(); list.Remove("_server"); //将虚拟用户排除在外 this.comboBox1.DataSource = list; } //开始录制视频 private void button1_Click(object sender, EventArgs e) { if (this.comboBox1.SelectedItem == null) { MessageBox.Show("没有选中目标用户!"); return; } string destUserID = this.comboBox1.SelectedItem.ToString(); this.recordMode = (RecordMode)this.comboBox_mode.SelectedIndex; //摄像头连接器 if (this.recordMode != RecordMode.JustAudio) { this.dynamicCameraConnector = new Passive.Video.DynamicCameraConnector(); this.dynamicCameraConnector.MaxIdleSpan4BlackScreen = 0; this.dynamicCameraConnector.ConnectEnded += new ESBasic.CbGeneric<ConnectResult>(cameraConnector1_ConnectEnded); this.dynamicCameraConnector.BeginConnect(destUserID); } //麦克风连接器 if (this.recordMode != RecordMode.JustVideo) { this.microphoneConnector = new Passive.Audio.MicrophoneConnector(); this.microphoneConnector.Mute = true; //在服务器上不播放出正在录制的声音 this.microphoneConnector.ConnectEnded += new ESBasic.CbGeneric<ConnectResult>(microphoneConnector1_ConnectEnded); this.microphoneConnector.AudioDataReceived += new CbGeneric<List<byte[]>>(microphoneConnector_AudioDataReceived); this.microphoneConnector.BeginConnect(destUserID); } this.label1.Text = string.Format("正在连接{0}的设备......" ,destUserID); this.Cursor = Cursors.WaitCursor; this.button1.Enabled = false; this.comboBox1.Enabled = false; this.comboBox_mode.Enabled = false; } //录制接收到的语音数据 void microphoneConnector_AudioDataReceived(List<byte[]> dataList) { if (this.maker != null) { foreach (byte[] audio in dataList) { if (this.recordMode == RecordMode.AudioAndVideo) { ((VideoFileMaker)this.maker).AddAudioFrame(audio); } else if (this.recordMode == RecordMode.JustAudio) { ((AudioFileMaker)this.maker).AddAudioFrame(audio); } else { } } } } void microphoneConnector1_ConnectEnded(ConnectResult obj) { this.ConnectComplete(); } void cameraConnector1_ConnectEnded(ConnectResult obj) { this.ConnectComplete(); } private int connectCompleteCount = 0; private void ConnectComplete() { ++this.connectCompleteCount; if (this.recordMode == RecordMode.AudioAndVideo) { if (this.connectCompleteCount == 2)//当语音、视频 都连接完成后,才正式启动录制。 { System.Threading.Thread.Sleep(500); this.Ready(); } } else { System.Threading.Thread.Sleep(500); this.Ready(); } } //初始化用于录制的FileMaker private void Ready() { if (this.InvokeRequired) { this.BeginInvoke(new CbGeneric(this.Ready)); } else { try { this.Cursor = Cursors.Default; if (this.recordMode == RecordMode.AudioAndVideo) { this.maker = new VideoFileMaker(); ((VideoFileMaker)this.maker).Initialize(this.dynamicCameraConnector.OwnerID + ".mp4", VideoCodecType.H264, this.dynamicCameraConnector.VideoSize.Width, this.dynamicCameraConnector.VideoSize.Height, 10, AudioCodecType.AAC, 16000, 1, true); this.videoTimer = new System.Threading.Timer(new System.Threading.TimerCallback(this.Callback), null, 0, 100); } else if (this.recordMode == RecordMode.JustAudio) { this.maker = new AudioFileMaker(); ((AudioFileMaker)this.maker).Initialize(this.microphoneConnector.OwnerID + ".mp3", AudioCodecType.MP3, 16000, 1); } else { this.maker = new SilenceVideoFileMaker(); ((SilenceVideoFileMaker)this.maker).Initialize(this.dynamicCameraConnector.OwnerID + ".mp4", VideoCodecType.H264, this.dynamicCameraConnector.VideoSize.Width, this.dynamicCameraConnector.VideoSize.Height, 10); this.videoTimer = new System.Threading.Timer(new System.Threading.TimerCallback(this.Callback), null, 0, 100); } this.label1.Text = "正在录制......"; this.label1.Visible = true; this.button1.Enabled = false; this.button2.Enabled = true; } catch (Exception ee) { MessageBox.Show(ee.Message); } } } private int callBackCount = -1; //定时获取视频帧,并录制 private void Callback(object state) { if (this.maker != null) { Bitmap bm = this.dynamicCameraConnector.GetCurrentImage(); if (bm != null) { ++this.callBackCount; if (this.recordMode == RecordMode.AudioAndVideo) { ((VideoFileMaker)this.maker).AddVideoFrame(bm); } else if (this.recordMode == RecordMode.JustVideo) { ((SilenceVideoFileMaker)this.maker).AddVideoFrame(bm); } else { } } else { } } } //停止录制 private void button2_Click(object sender, EventArgs e) { try { this.callBackCount = -1; if (this.videoTimer != null) { this.videoTimer.Dispose(); this.videoTimer = null; } this.connectCompleteCount = 0; if (this.recordMode != RecordMode.JustAudio) { this.dynamicCameraConnector.Disconnect(); this.dynamicCameraConnector = null; } if (this.recordMode != RecordMode.JustVideo) { this.microphoneConnector.Disconnect(); this.microphoneConnector = null; } this.button1.Enabled = true; this.button2.Enabled = false; this.label1.Visible = false; this.comboBox1.Enabled = true; this.comboBox_mode.Enabled = true; this.maker.Close(true); this.maker = null; MessageBox.Show("生成视频文件成功!"); } catch (Exception ee) { MessageBox.Show("生成视频文件失败!"+ ee.Message); } } }