Silverlight与WCF之间的通信(6)silverlight+wcf+tcp视频通信[1]单工模式

其实严格来讲,这个实现并非真正意义上的视频通信,既不是P2P的,也没有很高的性能,因为基本上是两个客户端同时往服务器上传递视频信息,然后由服务器进行中转到对方。

重点在于两点

  • IIS根目录下放clientaccesspolicy.xml文件
  • 注意服务中定义数据量大小2147483646,否则有可能传递不了

这边是单向的客户端定时向服务器传递带聊天标识的数据流:

 

ExpandedBlockStart.gif 代码
[DataContract] 
    
public   class  UserVideo 
    { 
        [DataMember] 
        
public   string  UserName {  get set ; } 
        [DataMember] 
        
public   string  PartnerName {  set get ; } 
        [DataMember] 
        
public   byte [] VideoByte {  set get ; } 
    } 

 

 

服务契约只有两个,一个是用来存储视频流,一个是用来提供视频流的

 

ExpandedBlockStart.gif 代码
[ServiceContract] 
    
public   interface  IChatService 
    { 
        [OperationContract] 
        
void  SendVideo(UserVideo userVideo); 

        [OperationContract] 
        List
< UserVideo >  GetVideos( string  userName, string  partnerName); 
    }

 

 

实现也很简单就是往一个静态的List中添加对象

 

ExpandedBlockStart.gif 代码
public   class  ChatService : IChatService 
    { 
        
private   static  List < UserVideo >  listVideos  =   new  List < UserVideo > ();         

        
public   void  SendVideo(UserVideo userVideo) 
        { 
            listVideos.Add(userVideo); 
        } 

        
public  List < UserVideo >  GetVideos( string  userName, string  partnerName) 
        { 
            var list 
=  listVideos.Where(m  =>  m.UserName  ==  userName  &&  m.PartnerName  ==  partnerName).ToList(); 
            listVideos.RemoveAll(m 
=>  m.PartnerName  ==  partnerName  &&  m.UserName  ==  userName); 
            
return  list; 
        } 
    }

 

接下来是host方法

 

ExpandedBlockStart.gif 代码
public   class  MyHost 
    { 
        
static  ServiceHost host  =   null
        
public   static   void  Open() 
        { 
            host 
=   new  ServiceHost( typeof (ChatService)); 
            host.Open(); 
        } 
        
public   static   void  Close() 
        { 
            
if  (host  !=   null   &&  host.State  ==  CommunicationState.Opened) 
            { 
                host.Close(); 
            } 
            host 
=   null
        } 
    }

 

最后是启动服务进行监听

 

ExpandedBlockStart.gif 代码
public   class  Program 
    { 
        
static   void  Main( string [] args) 
        { 
            MyHost.Open(); 
            System.Console.WriteLine(
" 服务已经启动...   敲任意键停止服务 " ); 
            System.Console.ReadLine(); 
            MyHost.Close(); 
        } 
    }

 

客户端方法:

开始视频播放

this .btnStartVideo.Click  +=  (o, ev)  =>  
            { 
                
if  (source  !=   null
                { 
                    source.Stop(); 
                    source.VideoCaptureDevice 
=  CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice(); 
                    VideoBrush vBrush 
=   new  VideoBrush(); 
                    vBrush.SetSource(source); 
                    
this .rectangleUser.Fill  =  vBrush; 
                    
if  (CaptureDeviceConfiguration.AllowedDeviceAccess  ||  CaptureDeviceConfiguration.RequestDeviceAccess()) 
                    { 
                        source.Start(); 
                    } 
                } 
            };

 

向服务器发送数据流

this .btnSendVideo.Click  +=  (o, ev)  =>  
            { 
                timerForSend 
=   new  System.Windows.Threading.DispatcherTimer(); 
                timerForSend.Interval 
=   new  TimeSpan( 0 0 0 0 150 ); 
                timerForSend.Tick 
+=  (o1, e1)  =>  { 
                    proxy 
=   new  ChatServiceClient(); 
                    WriteableBitmap bmp 
=   new  WriteableBitmap( this .rectangleUser,  null ); 
                    MemoryStream ms 
=   new  MemoryStream(); 
                    EncodeJpeg(bmp, ms); 
                    UserVideo userVideo 
=   new  UserVideo(); 
                    userVideo.PartnerName 
=   this .Partner; 
                    userVideo.UserName 
=   this .User; 
                    userVideo.VideoByte 
=  ms.GetBuffer(); 
                    proxy.SendVideoCompleted 
+=  (o2, e2)  =>  { }; 
                    proxy.SendVideoAsync(userVideo); 
                }; 
                timerForSend.Start(); 
            };

 

从服务期上取得数据流

 

ExpandedBlockStart.gif 代码
void  ReceiveVideo() 
        { 
            timerForReceive 
=   new  System.Windows.Threading.DispatcherTimer(); 
            timerForReceive.Interval 
=   new  TimeSpan( 0 0 0 0 100 ); 
            timerForReceive.Tick 
+=  (o, e)  =>  { 
                proxy 
=   new  ChatServiceClient(); 
                proxy.GetVideosCompleted 
+=  (se, ev)  =>  
                { 
                    
if  (ev.Error  ==   null
                    { 
                        
foreach  (ChatService.UserVideo video  in  ev.Result) 
                        { 
                            
this .imagePartner.Dispatcher.BeginInvoke(()  =>  { 
                                MemoryStream ms 
=   new  MemoryStream(video.VideoByte); 
                                BitmapImage bitmap 
=   new  BitmapImage(); 
                                bitmap.SetSource(ms); 
                                imagePartner.Source 
=  bitmap; 
                                ms.Close(); 
                            }); 
                        } 
                    } 
                }; 
                proxy.GetVideosAsync(User, Partner); 
            }; 
            timerForReceive.Start(); 
        }          

 

 

image

 

单工模式的缺点显而易见,请求--》答复比较耗时,我们下一步将会改造成双工模式的tcp通信。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值