using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Xml;
using GrassValley.Mseries.ChanStatus;
using GrassValley.Mseries.MediaMgr;
using GrassValley.Mseries.AppServer;
//using GrassValley.Test;
using System.Collections.Specialized;
using System.Collections;
using System.Text.RegularExpressions;
using System.Threading;
using GrassValley.Mseries.ConfigMgr;
using System.IO;
using GrassValley.Mseries.Control;
using GrassValley.Mseries.TransferQueue;
using GrassValley.Mseries.AppServerLog;
using GrassValley.Mseries.Editor;
using System.Reflection;
// Leave the following "css_reference" lines in when running with CS-Script. They're
// more than just comments - they tell CS-Script which DLLs to load if the DLL
// name does not match the namespace that it implements.
//css_reference Credentials.dll;
//css_reference ServerUtil.dll;
///
/// This is a sample CS-Script app for controlling the Grass Valley K2 server.
/// The code is regular C# code, but it's written for CS-Script which can
/// run it as a script from a DOS command prompt.
///
/// Go to the K2 AppServer Developer's Guide for more information:
/// http://www.gvgdevelopers.com/K2DevGuide/K2DevGuide.html#%5B%5BCS-Script%20Sample%20Scripts%5D%5D
///
/// For more information about CS-Script go to:
/// http://www.members.optusnet.com.au/~olegshilo/
///
namespace K2Script
{
class Test
{
static int Main(string[] args)
{
try
{
// get the application's name & create a unique suite name
string appName = Environment.GetCommandLineArgs()[0];
string suiteName = appName + "_" + System.Guid.NewGuid().ToString("N");
// fill in these values for your connection (i.e. hardcode them, pass in cmdline arguments, read from file, read from console, etc.)
string host = "summit-12";
string username = "Administrator";
string password = "summit-12!";
string domain = "";
// Connect to host & pass in credentials. The "using" block disposes connection when done.
using (Connection connection = new Connection(appName, suiteName, host, username, password, domain))
{
// do work only if we have a valid connection
if (connection.IsConnected)
{
//////////////////////////
// DO WORK HERE
//////////////////////////
string channel = "C2";
using (IChanStatus _chanStatus = connection.AppServer.CreateChanStatus(appName, channel))
{
// create a controller
bool isNewController = false;
ISimpleController icontroller = connection.AppServer.CreateController(appName, channel, out isNewController);
List nameUris = new List();
List namesStartDurations = new List();
AssetInfo assetUnderTest = new AssetInfo();
string start = string.Empty, dur = string.Empty;
string name = string.Empty;
nameUris = GetAssetUrisInPlaylist("edl/cmf//summit-12/V/ApiStreamingTests/List");
foreach (string uri in nameUris)
{
name = GetClipNameFromUri(uri);
GetAssetStartPositionDuration("edl/cmf//summit-12/V/ApiStreamingTests/List", name, out start, out dur);
assetUnderTest = new AssetInfo(name, start, dur);
namesStartDurations.Add(assetUnderTest);
}
var assetsInOrder = namesStartDurations.OrderBy(AssetInfo => AssetInfo.Start).ToList();
// cast it to a player recorder and load a clip
ISimplePlayerRecorder player = (ISimplePlayerRecorder)icontroller;
player.Eject();
player.Load("edl/cmf//summit-12/V:/ApiStreamingTests/List");
// play
player.Play();
Thread.Sleep(50);
object queryValues = null;
int currentPlayerPosition = 0;
queryValues = _chanStatus.GetStatus("position");
int.TryParse(queryValues.ToString(), out currentPlayerPosition);
foreach (AssetInfo asset in assetsInOrder)
{
bool _continuePlaying = true;
while (_continuePlaying)
{
if (currentPlayerPosition < asset.Duration)
{
Thread.Sleep(50);
queryValues = _chanStatus.GetStatus("position");
int.TryParse(queryValues.ToString(), out currentPlayerPosition);
}
else
{
Console.WriteLine(string.Format("Clip {0} just played.", asset.Name));
_continuePlaying = false;
}
}
}
// eject the clip
player.Eject();
// close the channel
icontroller.CloseChannel();
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
return 0;
}
static void GetAssetStartPositionDuration(string playlist, string clipName, out string startPosition, out string duration)
{
startPosition = string.Empty;
duration = string.Empty;
string[] split = playlist.Split('/');
// Create an isntance of XmlTextReader and call Read method to read the file
string path = "\\\\" + split[split.Length - 4].ToString() + "\\" + "V$\\media\\" + split[split.Length - 2].ToString() + "\\" + split[split.Length - 1].ToString() + ".cmf\\" + split[split.Length - 1].ToString() + ".xml";
XmlTextReader textReader = new XmlTextReader(path);
textReader.Read();
bool skipReading = false;
// If the node has value
while (textReader.Read())
{
if (textReader.NodeType == XmlNodeType.Whitespace)
continue;
switch (textReader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
//Console.Write("<" + textReader.Name);
//Console.WriteLine(">");
if (textReader.Name != "Edit")
continue;
else
{
while (textReader.MoveToNextAttribute()) // Read the attributes.
{
if (textReader.Name.Equals("EditName") && textReader.Value.Equals("[GAP]"))
{
skipReading = true;
continue;
}
// If attribute does not have clip name then skip
if (textReader.Name.Equals("EditName") && !textReader.Value.Equals(clipName))
{
skipReading = true;
continue;
}
if (!skipReading)
{
// Now the atrribute should be targeting the assets properties
if (textReader.Name.Equals("StartPosition"))
{
startPosition = textReader.Value;
Console.Write("<" + textReader.Name + "='" + textReader.Value + "'");
Console.WriteLine(">");
}
if (textReader.Name.Equals("Duration"))
{
duration = textReader.Value;
Console.Write("<" + textReader.Name + "='" + textReader.Value + "'");
Console.WriteLine(">");
}
}
//Console.Write("<" + textReader.Name + "='" + textReader.Value + "'");
//Console.WriteLine(">");
}
skipReading = false;
}
continue;
case XmlNodeType.Text: //Display the text in each element.
//Console.WriteLine(textReader.Value);
continue;
case XmlNodeType.EndElement: //Display the end of the element.
//Console.Write("" + textReader.Name);
//Console.WriteLine(">");
continue;
}
}
}
static List GetAssetUrisInPlaylist(string playlist)
{
List _clipNames = new List();
string[] split = playlist.Split('/');
// Create an isntance of XmlTextReader and call Read method to read the file
string path = "\\\\" + split[split.Length - 4].ToString() + "\\" + "V$\\media\\" + split[split.Length - 2].ToString() + "\\" + split[split.Length - 1].ToString() + ".cmf\\" + split[split.Length - 1].ToString() + ".xml";
XmlTextReader textReader = new XmlTextReader(path);
textReader.Read();
// If the node has value
while (textReader.Read())
{
if (textReader.NodeType == XmlNodeType.Whitespace)
continue;
switch (textReader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
//Console.Write("<" + textReader.Name);
//Console.WriteLine(">");
if (textReader.Name != "SourceClip")
continue;
else
{
while (textReader.MoveToNextAttribute()) // Read the attributes.
{
if (textReader.Name.Equals("assetId"))
continue;
if (textReader.Name.Equals("path"))
{
_clipNames.Add(textReader.Value);
Console.Write("<" + textReader.Name + "='" + textReader.Value + "'");
Console.WriteLine(">");
}
}
}
continue;
case XmlNodeType.Text: //Display the text in each element.
//Console.WriteLine(textReader.Value);
continue;
case XmlNodeType.EndElement: //Display the end of the element.
//Console.Write("" + textReader.Name);
//Console.WriteLine(">");
continue;
}
}
if (_clipNames.Count == 0)
return null;
return _clipNames;
}
static string GetClipNameFromUri(string clipUri)
{
string tempClipName = clipUri;
string[] tempHolder;
if (clipUri.Contains("\\"))
tempClipName = clipUri.Replace('\\', '/');
tempHolder = tempClipName.Split('/');
return (tempHolder[tempHolder.Length - 1].ToString()).Replace(".cmf", "");
}
public class AssetInfo
{
public string Name;
public int Start;
public int Duration;
public AssetInfo() { }
public AssetInfo(string name, string start, string duration)
{
Name = name;
int.TryParse(start, out Start);
int.TryParse(duration, out Duration);
}
}
}
// Connection helper class: sets up credentials & hostname, connects to K2
public class Connection : IDisposable
{
private bool _connected = false;
private AppServerMgrProxy _appServerMgrProxy = null;
private IAppServer _appServer = null;
///
/// Object that connects to the specified K2 credentials
///
/// The application's name.
/// The suite's name.
/// The K2's host name.
/// The username.
/// The password.
/// The domain.
public Connection(string appname, string suitename, string host, string username, string password, string domain)
{
try
{
//////////////////////////
// SETUP
//////////////////////////
if (null == host || host.Length == 0)
{
Console.WriteLine("Empty host name passed to Connection object.");
return;
}
//////////////////////////
// CONNECT TO K2
//////////////////////////
// create an AppServerMgr proxy object
_appServerMgrProxy = new AppServerMgrProxy();
// tell it which K2 we're connecting to
_appServerMgrProxy.SetHost(host);
// give it the user credentials we want to use
if (username.Length > 0)
_appServerMgrProxy.SetUserCredentials(username, password, domain, false);
// connect to the K2's AppServerMgr
if (!_appServerMgrProxy.Connect())
{
// if the connection failed, report an error and exit
Console.WriteLine("ERROR: Could not connect to AppService on host '" + host + "'. Check the host name.");
_appServerMgrProxy = null;
return;
}
// if we got here we're connected to K2 AppService
//////////////////////////
// CREATE AN APPSERVER
//////////////////////////
// now create an AppServer
bool newConnection = false;
_appServer = _appServerMgrProxy.CreateAppServer(suitename, appname, out newConnection);
_connected = true;
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
///
/// Gets a value indicating whether this instance is connected.
///
///
/// true if this instance is connected; otherwise, false.
///
public bool IsConnected
{
get
{
return _connected;
}
}
///
/// Gets the AppServerMgr.
///
/// The AppServerMgr
public AppServerMgrProxy AppServerMgr
{
get
{
if (_appServerMgrProxy == null)
Console.WriteLine("WARNING: AppServerMgrProxy object == null!");
return _appServerMgrProxy;
}
}
///
/// Gets the AppServer
///
/// The AppServer
public IAppServer AppServer
{
get
{
if (_appServerMgrProxy == null)
Console.WriteLine("WARNING: AppServerProxy object == null!");
return _appServer;
}
}
///
/// Disposes this instance.
///
public void Dispose()
{
if (_appServer != null)
_appServer.CloseConnection();
}
}
}