/*@!Encoding:1252*/
includes
{
  #include "General.cin"
  #include "Avb.cin"
  #include "Media.cin"
  #include "Source.cin"
}

variables
{
  byte gSource_StreamDestinationAddress[6] = { 1, 0, 0, 0, 0, 3 };
  
  const word kSource_UniqueId = 3;
}

on sysvar Source3::MicLine
{
  if (@this == 1)
  {
    Source_StopReader();
    Source_StartCapture();
    
    @Source3::File = 0;
  }
}

on sysvar Source3::File
{
  long ret;
  char filePath[1024];
  
  if (@this == 1)
  {   
    ret = SysGetVariableString("Source3", "FilePath", filePath, elCount(filePath));

    if (Avb_StopIfError(ret, "SysGetVariableString") != 0)
    {
      return;
    }

    Source_StopReader();
    Source_StartFile(filePath);
    
    @Source3::MicLine = 0;
  }
}

on sysvar Source3::FilePath
{
  long ret;
  char filePath[1024];
  
  if (@Source3::File == 1)
  {      
    Source_StopReader();
    Source3_StartFile();
  }
}

on preStop
{ 
  Source_StopReader();
  Source_StopTalker();
}

on start
{
  long ret;
  char filePath[1024];
  
  if (@Source3::MicLine == 1)
  {
    Source_StartCapture();
  }
  else
  {
    Source3_StartFile();
  }
}

void OnSource_EndReached()
{
  
  if (@Source3::MicLine == 1)
  {
    @Source3::MicLine = 0;
    @Source3::MicLine = 1;
  }
  else if (@Source3::File == 1)
  {
    Source3_StartFile();
  }
}

void Source3_StartFile()
{
  long ret;
  char filePath[1024]; 
  
  ret = SysGetVariableString("Source3", "FilePath", filePath, elCount(filePath));

  if (Avb_StopIfError(ret, "SysGetVariableString") != 0)
  {
    return;
  }
  
  Source_StartFile(filePath);
}