/*@!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, 1 };
  
  const word kSource_UniqueId = 1;
}

on sysvar Source1::MicLine
{
  if (@this == 1)
  {
    Source_StopReader();
    Source_StartCapture();
    
    @Source1::File = 0;
  }
}

on sysvar Source1::File
{
  long ret;
  char filePath[1024];
  
  if (@this == 1)
  {   
    ret = SysGetVariableString("Source1", "FilePath", filePath, elCount(filePath));

    if (Avb_StopIfError(ret, "SysGetVariableString") != 0)
    {
      return;
    }

    Source_StopReader();
    Source_StartFile(filePath);
    
    @Source1::MicLine = 0;
  }
}

on sysvar Source1::FilePath
{
  long ret;
  char filePath[1024];
  
  if (@Source1::File == 1)
  {      
    Source_StopReader();
    Source1_StartFile();
  }
}

on preStop
{ 
  Source_StopReader();
  Source_StopTalker();
}

on start
{
  long ret;
  char filePath[1024];
  
  if (@Source1::MicLine == 1)
  {
    Source_StartCapture();
  }
  else
  {
    Source1_StartFile();
  }
}

void OnSource_EndReached()
{
  if (@Source1::MicLine == 1)
  {
    @Source1::MicLine = 0;
    @Source1::MicLine = 1;
  }
  else if (@Source1::File == 1)
  {
    Source1_StartFile();
  }
}

void Source1_StartFile()
{
  long ret;
  char filePath[1024]; 
  
  ret = SysGetVariableString("Source1", "FilePath", filePath, elCount(filePath));

  if (Avb_StopIfError(ret, "SysGetVariableString") != 0)
  {
    return;
  }
  
  Source_StartFile(filePath);
}