• Live Smooth Streaming: How-to: Start, Stop & Shutdown a Publishing Point Programmatically

    Published by on April 16th, 2009 9:05 am under Adaptive Streaming, IIS7, Live Smooth Streaming, Media, RSCA, Silverlight, Silverlight 2, Silverlight 3, Smooth Streaming

    14 Comments

    During the last months I have been working in a project highly related to multimedia and iis-smoothclient development technologies such as Silverlight. And in the last few weeks, I started to look to some of the new IIS Media services such as Smooth Streaming and Live Smooth Streaming in order to gather as much knowledge about them to perform some spikes for the project.

    One of the things that came up while spiking was finding a way to start, stop & shutdown a publishing point via code.

    Microsoft.Web.Administration to the Rescue

    ServerManagerWith IIS7 a new API to administer IIS from managed code was introduced. This API is really simple to use and with a few lines of code you can manipulate the server configuration as any other information available in IIS7. (if you want to taste the power of this API, I recommend you to read this post from Carlos Aguilar Mares).

    So, I used the Microsoft.Web.Administration.dll assembly, that can be found at IIS Directory (%windir%\System32\inetSrv), to perform the operations over the publishing point. But it was not so easy to do it, as there is no information available yet about what RSCA function has to be called  and which parameters need to be used in order get the desired results.

    Then, I started to look at the different IIS configuration files (%windir%\System32\inetsrv\config) searching for clues and I came up with the Microsoft.Web.Management.Media.LiveStreaming.dll assembly (this assembly can be found in the GAC after installing the Live Smooth Streaming bits).

    In that moment I summoned my best friend Reflector and together went to the depths of this assembly until we found some code that might be useful for our objective.

    After doing some tests, I ended up with the following method:

    private static void ExecuteRscaFunction(ConfigurationElement workerProcess,
                                            string siteName, string applicationPath,
                                            string fileName, string functionName)
    {
        ConfigurationMethod configurationMethod = workerProcess.Methods["GetCustomData"];
    
        ConfigurationMethodInstance instance = configurationMethod.CreateInstance();
        instance.Input["guidIdOfFunctionCall"] = "Media_LiveStreaming_Control";
    
        string currentLogicalPath = applicationPath;
    
        if (!currentLogicalPath.EndsWith("/", StringComparison.OrdinalIgnoreCase))
        {
            currentLogicalPath = currentLogicalPath + "/";
        }
    
        string fullPath = string.Concat(currentLogicalPath, fileName);
        string parameters = string.Format(CultureInfo.InvariantCulture, "{0};{1};{2}",
                                          functionName, siteName, fullPath);
        instance.Input["parametersOfFunctionCall"] = parameters;
        instance.Execute();
    }

    The most important thing of the method above is the functionName parameter. This parameter can be one the following values depending on what you want to accomplish:

    • StartPublishingPoint. This is used to start a publishing point.
    • StopPublishingPoint. This is used to stop the live source of the publishing point.
    • ShutdownPublishingPoint. This is used to shutdown a publishing point.

    To understand where the others parameters should come from, let’s take a look at the following example: image So, if we want to the start the LiveSmoothStream publishing point associated to the SmoothStreaming application from the Default Web Site, we should call the ExecuteRscaFunction method in this way:

    ExecuteRscaFunction(workerProcess,
                        "Default Web Site", "/SmoothStreaming",
                        "LiveSmoothStream.isml", "StartPublishingPoint");

    The remaining parameter (workerProcess) must be retrieved from the application pool associated to the application.

    Note: You can use the Microsoft.Web.Administration API to get all the values previously mentioned.

    To see if the function is working you can add the following lines of code at the end of the method to parse the output and print it on the console.

    string rawOutput = instance.Output["data"] as string;
    
    byte[] bytes = Convert.FromBase64String(rawOutput);
    ASCIIEncoding encoding = new ASCIIEncoding();
    
    Console.WriteLine(encoding.GetString(bytes));

    The output after executing the Stop, Shutdown and Start functions:

    image

    Hope this helps!. If you want to learn more about Smooth Streaming you might find useful the following links:

    Happy streaming!.

    kick it on DotNetKicks.com

    Tags: , , , , , , ,

    • http://blog.johndeutscher.com John Deutscher

      Nice Post E-Z! I’ll track back to you today.

    • Pawan Venugopal

      Hi,

      Thanks for the great post. I do have a scenario where i would like some help. I have a application when the user uploads a video file from a web page and the file gets encoded using adaptive streaming (expression encoder 3 IIS Smooth Streaming). Now i need to know how to publish this encoded file to a video playlist so than when the user clicks on this video it starts playing. The main part is that when ever i upload a video it should get automatically updates to the playlist.

      Any help with this is greatly Appreciated.

      -Pawan

    • Mike

      // ADD REFERENCES TO THE
      // C:\Windows\assembly\GAC_MSIL\Microsoft.Web.Administration\7.0.0.0__31bf3856ad364e35\Microsoft.Web.Administration.dll
      // C:\Windows\assembly\GAC_MSIL\Microsoft.Web.Management.Media.LiveStreaming\4.0.938.54__31bf3856ad364e35\Microsoft.Web.Management.Media.LiveStreaming.dll

      class PublishingPointController
      {
      private const string POOL_NAME = “YOUR_IIS_APP_POOL_NAME”;

      public PublishingPointController(string siteName, string applicationPath)
      {
      SiteName = siteName;
      ApplicationPath = applicationPath;
      }

      private string _siteName;
      public string SiteName
      {
      get { return _siteName; }
      set { _siteName = value; }
      }

      private string _applicationPath;
      public string ApplicationPath
      {
      get { return _applicationPath; }
      set { _applicationPath = value; }
      }

      // call relatively from your website root – “http://localhost/dir/file.isml” == “dir/file.isml”… see the article
      // http://blogs.southworks.net/ejadib/2009/04/16/live-smooth-streaming-how-to-start-stop-shutdown-a-publishing-point-programmatically/
      public string StartPublishingPoint(string fileName)
      {
      string res = ExecuteRscaFunction(SiteName, ApplicationPath, fileName, “StartPublishingPoint”);

      return res;
      }

      public string StopPublishingPoint(string fileName)
      {
      string res = ExecuteRscaFunction(SiteName, ApplicationPath, fileName, “StopPublishingPoint”);

      return res;
      }

      public string ShutdownPublishingPoint(string fileName)
      {
      string res = ExecuteRscaFunction(SiteName, ApplicationPath, fileName, “ShutdownPublishingPoint”);

      return res;
      }

      private static string ExecuteRscaFunction(string siteName, string applicationPath,
      string fileName, string functionName)
      {
      ServerManager serverManager = new ServerManager();
      ConfigurationElement workerProcess = serverManager.ApplicationPools[POOL_NAME].WorkerProcesses[0];
      if (workerProcess == null)
      throw new Exception(“PublishingPointController: Couldn’t gate Worker Process at pos 0″);

      ConfigurationMethod configurationMethod = workerProcess.Methods[“GetCustomData”];

      ConfigurationMethodInstance instance = configurationMethod.CreateInstance();
      instance.Input[“guidIdOfFunctionCall”] = “Media_LiveStreaming_Control”;

      string currentLogicalPath = applicationPath;

      if (!currentLogicalPath.EndsWith(“/”, StringComparison.OrdinalIgnoreCase))
      {
      currentLogicalPath = currentLogicalPath + “/”;
      }

      string fullPath = string.Concat(currentLogicalPath, fileName);
      string parameters = string.Format(CultureInfo.InvariantCulture, “{0};{1};{2}”,
      functionName, siteName, fullPath);
      instance.Input[“parametersOfFunctionCall”] = parameters;
      instance.Execute();

      string rawOutput = instance.Output[“data”] as string;

      byte[] bytes = Convert.FromBase64String(rawOutput);
      ASCIIEncoding encoding = new ASCIIEncoding();

      string result = encoding.GetString(bytes);

      return result;
      }
      }

    • Mike

      You are freaking kiddin’ me…

    • Zahid Rasool

      I’m get NullRefrence Exception on this line ” ConfigurationMethodInstance instance = section.Methods[“GetPublishingPoints”].CreateInstance(); “



Map

Categories