title: How to better implement start and stop video recording Web Services using Axis Media Parser API?
body: <p>I have a C# Web Service which records data from an IP camera. This service works fine when I record data during a specified amount of time, for example 10 seconds. But my objective is to achieve data recording for an unspecified amount of time and let the user press to stop recording. So I modified my code creating a new Web Service (stopRecording) to change the value of a global variable that acts as a mutex. Obviously this is wrong because I test it but I don´t know how to proceed. Can anybody help me? I would really appreciate it. </p>
  
  <p>Down here I leave the relevant code. </p>
  
  <pre><code>    [WebMethod]
      public string startRecording(string ipAddress)
      {
  
          // Connection preset for H.264 (HTTP API 3.0)
          string Url = "axrtsp:://" + ipAddress + "/axis-media/media.amp?videocodec=h264";            
          string UserName = "username";
          string Password = "pass";
  
          string Path = "C:/directory/subdirectory/";
          string Filename = "myRecordedFile.bin";
  
          string FilePath = Path + Filename;
          // Open binary output file to write parsed video frames into
          using (FileStream outFileStream = new FileStream(FilePath, FileMode.Create))            
  
          using (outFile = new BinaryWriter(outFileStream))
          {
  
              try
              {
  
                  // Register for events like OnVideoSample, OnAudioSample, OnTriggerData and OnError
                  ...
  
                  // Set connection and media properties
                  ...
  
                  // Get absolute time from Axis device
                  ...
  
                  // Connect to the device                   
                  int cookieID;
                  int numberOfStreams;
                  object buffer;
                  parser.Connect(out cookieID, out numberOfStreams, out buffer);
  
                  // Write media type information to out file (buffer is an array of bytes)
                  byte[] mediaTypeBuffer = (byte[])buffer;
                  outFile.Write(mediaTypeBuffer.Length);
                  outFile.Write(mediaTypeBuffer, 0, mediaTypeBuffer.Length);
  
                  // Start the media stream and registered event handlers will be called
                  parser.Start();
                  Debug.WriteLine("Will start recording...");  
                  do {
  
                      Debug.WriteLine("recording...");         //want to record during an unspecified time               
  
                  } while (record); //my mutex variable that doesn´t make the thing even when I call the stopRecording Web Service. The program remains overlooping
  
                  System.Diagnostics.Debug.Write("Finish recording... never reached!!!!! ");  
                  // Stop the stream
                  parser.Stop();
  
                  // Unregister event handlers
                  ...
              }
              catch (COMException e)
              {
                  Console.WriteLine("Exception for {0}, {1}", parser.MediaURL, e.Message);
              }
              // Inform the GC that COM object no longer will be used
              Marshal.FinalReleaseComObject(parser);
              parser = null;
              Console.WriteLine("Stream stopped");
          }
  
          return "Recording from camera " + Url;
      }
  
      [WebMethod]
      public string stopRecording()
      {
          System.Diagnostics.Debug.Write("I want to stop recording..."); 
          record = false;
  
          return "Stop";
      }
  </code></pre>