我的部落格中講述過WMS9,可以實現流媒體伺服器,但沒有WME(windows media encoder)的支援,總覺得缺少點什麼,這裡就講述一下如何通過.net實現WME擷取裝置視頻音頻流,並推送到遠程(本地當然可以)流媒體廣播發行端點。
一.下載了wme9的SDK,安裝完畢:這一步主要體驗一下WME能夠實現那些功能。
讀者可以自行安裝試試。
二.建立.net winform Application,添加引用 using WMEncoderLib;
WMEncoderLib是一個assemble,通過它,我們可以方便實現WME的操作。
三.擷取本地螢幕,並轉換為視頻訊號 IWMEncSource SrcAud = SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO);
IWMEncSource SrcVid = SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);
SrcAud.SetInput( " Default_Audio_Device " , " Device " , "" );
SrcVid.SetInput( " ScreenCapture1 " , " ScreenCap " , "" );
四.啟動Encoder,推送到遠程WMS伺服器 // Set up the push broadcast.
IWMEncPushDistribution PushDist = (IWMEncPushDistribution)Encoder.Broadcast;
string strServerName = this .TB_ServerName.Text.Trim() + " : " + this .TB_Port.Text.Trim();
string strPubPoint = " TmpBroadCastPubPoint " ;
// Remove the publishing point when the broadcast is over.
PushDist.AutoRemovePublishingPoint = true ;
// Set the push distribution variables.
PushDist.ServerName = strServerName;
PushDist.PublishingPoint = strPubPoint;
// PushDist.Template = strPubTemplate;
// Initialize the encoding process.
Encoder.PrepareToEncode( true );
注意:在這裡,直接推送到遠程伺服器,如果伺服器不在同一域內,或在不可信任地區,推送時應該會需要驗證使用者名稱和密碼。因此我們需要建立一個委託: Encoder.OnAcquireCredentials += new _IWMEncoderEvents_OnAcquireCredentialsEventHandler(OnUserAuth);
public void OnUserAuth( string bstrRealm, string bstrSite, ref object pvarUser, ref object pvarPassword, ref object plFlags)
... {
pvarUser = "administrator";
pvarPassword = "622316";
plFlags = 4;
}
五.到這裡就差不多了,完整代碼貼出來: using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WMEncoderLib;
namespace Project2
... {
public partial class Form1 : Form
...{
static bool bDone;
private WMEncoder Encoder;
public Form1()
...{
InitializeComponent();
Encoder = new WMEncoder();
Encoder.OnStateChange += new _IWMEncoderEvents_OnStateChangeEventHandler(OnWmeStatusChange);
Encoder.OnError += new _IWMEncoderEvents_OnErrorEventHandler(Encoder_OnError);
Encoder.OnAcquireCredentials += new _IWMEncoderEvents_OnAcquireCredentialsEventHandler(OnUserAuth);
}
private void Btn_Start_Click(object sender, EventArgs e)
...{
this.StartBroadCast();
}
private void Btn_Close_Click(object sender, EventArgs e)
...{
this.StopBroadCast();