Pelco Developer Network (PDN)

Audio Usage

Any provided sample code is meant to be a reference implementation focused on educating developers about Pelco devices and Endura. Though there are exceptions, in general Pelco sample code is NOT intended for immediate production use without modification.

There are several reasons for this:

  • By only including the minimum code, this helps our readers focus on the main concepts for understanding our technology more easily, more quickly, and more effectively.
  • Every situation is different, with different requirements and resources. Realistically we are unable to account for every situation without making our samples more complicated and less accessible.
For frequently asked questions and answers pertaining to Audio Usage, refer to
Frequently Asked Questions in the Sales and Support area of pelco.com. You can also contact us through the Contact link on pelco.com.
The code samples shown here may differ from the actual downloadable code samples, for better ease of understanding.

Entries

General

Refreshing an Existing Session

Refer to the Pelco Device Control URL and Pelco Device Port Number sections below for important details about the code sample.
  1. Download the G711 RTP Audio Sample Release Date: 11/17/2009 Size: 193 KB 
    Use this sample code for audio playback and capture using the AudioInput and AudioOutput services.
  2. Create a new instance of either the AudioOutput Web Service Reference or AudioInput Web Service Reference service proxy and set its control URL.
  3. Assuming a successful connection with the desired stream, examine the returned StreamParameters instance's session id value.
  4. Using the session id value make a RefreshSession call to either the AudioOutput Web Service Reference or AudioInput Web Service Reference service proxy along with the desired time out length.
C# Example

 

using System;
using System.Net;
using System.Text.RegularExpressions;

namespace Pelco.Endura.Sample
{
  public class AudioSample
  {
    private int asyncId, streamPos, cameraPort, localPort, capturePort;
    private bool asyncIdSpecified, streamPosSpecified, asyncId2, asyncIdSpecified2;
    private string sessionId, sessionId2;
    private IPAddress cameraIp, localIp;

    public AudioSample()
    {
      cameraIp = IPAddress.Parse("172.16.5.102");
      cameraPort = 49152;
    }

    private void RefreshAudioSession()
    {
      try
      {
        //Assume that you have the session id already from a successful Connect call
        // from AudioInput or AudioOutput
        Pelco.Endura.Services.AudioOutput.AudioOutput ao = 
            new Pelco.Endura.Services.AudioOutput.AudioOutput();
        ao.Url = "http://" + cameraIp.ToString() + ":" + 
            cameraPort.ToString() + "/control/AudioOutput-1";
        ao.RefreshSession(sessionId, 300);
      }
      catch (Exception)
      {
      }
    }
  }
}

 

Output

Playing a Stream

Refer to the Pelco Device Control URL and Pelco Device Port Number sections below for important details about the code sample.
  1. Download the G711 RTP Audio Sample Release Date: 11/17/2009 Size: 193 KB 
    Use this sample code for audio playback and capture using the AudioInput and AudioOutput services.
  2. Import the needed namespaces as shown below, especially the Microsoft.DirectX.DirectSound namespace.
  3. Initialize the PelcoG711RTP audio handler as well as the camera's IP address and port number.
  4. Create a new instance of the AudioOutput Web Service Reference service proxy and set its control URL.
  5. Create an empty instance of the a StreamQuery and use it in a call to AudioOutput instance's Query method. If there are any available audio streams, this call will return StreamParameter instances pertaining to each one. Use the 1st available stream.
  6. Connect to the 1st available stream.
  7. Assuming a successful connection, examine the returned StreamParameters instance.
  8. Create a new instance of the StreamControl service proxy and make a call to its Play using information from the previously mentioned StreamParameter's instance.
  9. Call the PelcoG711RTP audio handler's StartPlayback method.
  10. Once finished with playback, make a call to the AudioOutput Web Service Reference's Disconnect action.
  11. Finally make a call to the PelcoG711RTP audio handler's StopPlayback method.
C# Example

 

using System;
using System.Net;
using System.Text.RegularExpressions;
using Microsoft.DirectX.DirectSound;

namespace Pelco.Endura.Sample
{
  public class AudioSample
  {
    private int asyncId, streamPos, cameraPort;
    private bool asyncIdSpecified, streamPosSpecified;
    private string sessionId; private IPAddress cameraIp;
    private Pelco.Endura.Audio.PelcoG711RTP audioHandler = 
        new Pelco.Endura.Audio.PelcoG711RTP();

    public AudioSample()
    {
      cameraIp = IPAddress.Parse("172.16.5.102");
      cameraPort = 49152;
      this.audioHandler = new Pelco.Endura.Audio.PelcoG711RTP();
    }

    private void ConnectAudioPlayback()
    {
      try
      {
        Pelco.Endura.Services.AudioOutput.AudioOutput ao = 
            new Pelco.Endura.Services.AudioOutput.AudioOutput();
        ao.Url = "http://" + cameraIp.ToString() + ":" + 
            cameraPort.ToString() + "/control/AudioOutput-1";
        Pelco.Endura.Services.AudioOutput.StreamQuery sq = 
            new Pelco.Endura.Services.AudioOutput.StreamQuery();
        Pelco.Endura.Services.AudioOutput.StreamCatalog sc = 
            ao.Query(sq, ref asyncId, ref asyncIdSpecified);

        if (sc.entries.Length < 1)
          return;

        Pelco.Endura.Services.AudioOutput.StreamParameters sp = sc.entries0;
        sp.streamSession.transportURL = 
            "rtp://" + localIp.ToString() + ":" + playbackPort.ToString();
        ao.Connect(ref sp, ref asyncId, ref asyncIdSpecified);
        sessionId = sp.streamSession.sessionId;

        Match m = Regex.Match(sp.streamSession.transportURL, @"rtp://(.):(.)");
        IPEndPoint ep = new IPEndPoint(IPAddress.Parse(m.Groups1.Value), 
            Convert.ToInt32(m.Groups2.Value));

        Pelco.Endura.Services.StreamControl.StreamControl sControl = 
            new Pelco.Endura.Services.StreamControl.StreamControl(); 
        sControl.Url = "http://" + cameraIp.ToString() + ":" + 
            cameraPort.ToString() + "/control/StreamControl-1"; 
        sControl.Play(sessionId, 1, 0, 1, asyncId, out streamPos, out streamPosSpecified); 
        audioHandler.StartPlayback(ep, 0.5f); 
        Console.WriteLine("Audio Playback TransportUrl: " + sp.streamSession.transportURL);
        System.Threading.Thread.Sleep(5000); 

        if (sessionId == null) 
          return;

        Pelco.Endura.Services.AudioOutput.AudioOutput ao = 
            new Pelco.Endura.Services.AudioOutput.AudioOutput(); 
        ao.Url = "http://" + cameraIp.ToString() + ":" + 
            cameraPort.ToString() + "/control/AudioOutput-1"; 
        ao.Disconnect(sessionId, ref asyncId, ref asyncIdSpecified);
        sessionId = null; 
        audioHandler.StopPlayback(); 
      } 
      catch (Exception) 
      { 
      } 
    }
  }
}

 

Input

Capturing a Stream

DO NOT set the sourceURI attribute within StreamSession, or Pelco devices such as cameras will assume your application is an actual Endura device. This will result in various problems unless your application or client provides an implementation of Pelco's AudioOutput Web Service Reference. Instead set the sourceURL attribute so Pelco devices will allow a socket connection instead of an AudioOutput Web Service. The example below illustrates these points.
  1. Download the G711 RTP Audio Sample Release Date: 11/17/2009 Size: 193 KB 
    Use this sample code for audio playback and capture using the AudioInput and AudioOutput services.
  2. Import the needed namespaces as shown below, especially the Microsoft.DirectX.DirectSound namespace.
  3. Initialize the PelcoG711RTP audio handler as well as the camera's IP address, the local clients IP address, and respective port numbers.
  4. Create a new instance of the AudioInput Web Service Reference service proxy and set its control URL.
  5. Create an empty instance of the a StreamQuery and use it in a call to AudioInput Web Service Reference instance's Query method. If there are any available audio streams, this call will return StreamParameter instances pertaining to each one. Use the 1st available stream.
  6. Examine the previously mentioned StreamParameter instance, and use its information for the audio handler's StartCapture method call.
  7. Make needed changes to the same StreamParameter instance's quality related settings, then make a call to the AudioInput Web Service Reference service proxy's Connect method.
  8. Once finished with the capture, make a call to the the AudioInput Web Service Reference service proxy's Disconnect method, and to call the audio handler's StopCapture method call.
C# Example

 

using System;
using System.Net;
using System.Text.RegularExpressions;
using Microsoft.DirectX.DirectSound;

namespace Pelco.Endura.Sample
{
  public class AudioSample
  {
    private int asyncId, streamPos, cameraPort, localPort, capturePort;
    private bool asyncIdSpecified, streamPosSpecified, asyncId2, asyncIdSpecified2;
    private string sessionId, sessionId2;
    private IPAddress cameraIp, localIp;
    private Pelco.Endura.Audio.PelcoG711RTP audioHandler = 
        new Pelco.Endura.Audio.PelcoG711RTP();

    public AudioSample()
    {
      cameraIp = IPAddress.Parse("172.16.5.102");
      cameraPort = 49152;
      this.audioHandler = new Pelco.Endura.Audio.PelcoG711RTP();

      localIp = IPAddress.Parse("172.16.5.100");
      localPort = 9006;
      capturePort = localPort + 4;
    }

    private void ConnectAudioCapture()
    {
      try
      {
        Pelco.Endura.Services.AudioInput.AudioInput ai = 
            new Pelco.Endura.Services.AudioInput.AudioInput();
        ai.Url = "http://" + cameraIp.ToString() + ":" + 
            cameraPort.ToString() + "/control/AudioInput-1";
        Pelco.Endura.Services.AudioInput.StreamQuery sq = 
            new Pelco.Endura.Services.AudioInput.StreamQuery();
        Pelco.Endura.Services.AudioInput.StreamCatalog sc = 
            ai.Query(sq, ref asyncId, ref asyncIdSpecified);

        if (sc.entries.Length < 1)
          return;

        Pelco.Endura.Services.AudioInput.StreamParameters sp = 
            new Pelco.Endura.Services.AudioInput.StreamParameters();
        sp.streamSession = new Pelco.Endura.Services.AudioInput.StreamSession();
        sp.streamSession.sourceURL = "udp://" + localIp.ToString() + 
            ":" + capturePort.ToString();
        sp.streamSession.outputURI = sp.streamSession.sourceURL;
        sp.streamSession.transportURL = "rtp://" + cameraIp.ToString() + 
            ":" + capturePort.ToString();

        Match m = Regex.Match(sp.streamSession.transportURL, @"rtp://(.*):(.*)");
        IPEndPoint ep = new IPEndPoint(IPAddress.Parse(m.Groups[1].Value), 
            Convert.ToInt32(m.Groups[2].Value));
        audioHandler.StartCapture(ep, cboCaptureDevice.SelectedIndex);

        sp.streamSession.refreshInterval = 900;
        sp.preAlarmDurationInManualRecord = 30;
        sp.audioQuality = new Pelco.Endura.Services.AudioInput.AudioQuality();
        sp.audioQuality.bitrate = "64000";
        sp.audioQuality.format = "G711";
        sp.audioQuality.framerate = "20";
        sp.audioQuality.samplerate = "8000";
        sp.audioQuality.samplesize = "16";
        sp.audioQuality.volume = "100";

        ai.Connect(ref sp, ref asyncId2, ref asyncIdSpecified2);
        sessionId2 = sp.streamSession.sessionId;

        Console.WriteLine("Audio Capture TransportUrl: " + sp.streamSession.transportURL);
        System.Threading.Thread.Sleep(5000);

        Pelco.Endura.Services.AudioInput.AudioInput ai = 
            new Pelco.Endura.Services.AudioInput.AudioInput();
        ai.Url = "http://" + cameraIp.ToString() + ":" + 
            cameraPort.ToString() + "/control/AudioInput-1";
        ai.Disconnect(sessionId2, ref asyncId2, ref asyncIdSpecified2);
        sessionId2 = null;
        audioHandler.StopCapture();
      }
      catch (Exception)
      {
      }
    }
  }
}

 

Appendix

Pelco Device Port Number

Pelco device port numbers numbers will rarely change. However, a device port number may change when a device restarts. While the port number still is unlikely to change when the device restarts, it is still good to account for it.

Pelco Device Control URL

Be aware that all examples utilize a method of determining the device's control URL in a way that is not suitable for production. This was done to keep our example code simple, and focused on the main task being demonstrated.