//Loop back audio through media-stream
nsresult
MediaEngineWebrtcAudioSource::Start(SourceMediaStream* aStream, TrackID aID)
{
  const int DEFAULT_PORT = 55555;
  printf("\n MediaEngineWebrtcAudioSource : Start: Entered ");
  if (false == mInitDone || mState != kAllocated) {
    return NULL;
  }

  if(!aStream)
  	return NULL;

  mTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
  if (!mTimer) {
    return NULL;
  }

  mSource = aStream;

  AudioSegment* segment = new AudioSegment();
  segment->Init(CHANNELS);
  //segment->InsertNullDataAtStart(1);
  mSource->AddTrack(aID, PLAYOUT_SAMPLE_FREQUENCY, 0, segment);
  mSource->AdvanceKnownTracksTime(STREAM_TIME_MAX);
  mTrackID = aID;

  printf("\n Starting the audio engine ");
  mVoEBase->SetLocalReceiver(mChannel,DEFAULT_PORT);
  mVoEBase->SetSendDestination(mChannel,DEFAULT_PORT,"127.0.0.1");

  if(-1 == mVoEXmedia->SetExternalPlayoutStatus(true)) {
    printf("\n SetExternalPlayoutStatus failed %d ", mVoEBase->LastError() );
  	return NULL;
  } 
  //loopback audio
  mVoEBase->StartPlayout(mChannel);
  mVoEBase->StartSend(mChannel);
  mVoEBase->StartReceive(mChannel);
   
  mState = kStarted;
  // call every 10 milliseconds
  mTimer->InitWithCallback(this, 10, nsITimer::TYPE_REPEATING_SLACK);
  return NS_OK;
}
void
MediaEngineWebRTCAudioSource::Process(const int channel,
  const webrtc::ProcessingTypes type, sample* audio10ms,
  const int length, const int samplingFreq, const bool isStereo)
{
  ReentrantMonitorAutoEnter enter(mMonitor);
  if (mState != kStarted)
    return;

  nsRefPtr<SharedBuffer> buffer = SharedBuffer::Create(length * sizeof(sample));

  sample* dest = static_cast<sample*>(buffer->Data());
  memcpy(dest, audio10ms, length * sizeof(sample));

  AudioSegment segment;
  segment.Init(CHANNELS);
  segment.AppendFrames(
    buffer.forget(), length, 0, length, AUDIO_FORMAT_S16
  );
  mSource->AppendToTrack(mTrackID, &segment);

  return;
}