//---------------------------------------------------------------------------
#include "RTSPRecorderService_.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

#include <sysutils.hpp>

TRTSPRecordService *RTSPRecordService;
//---------------------------------------------------------------------------
__fastcall TRTSPRecordService::TRTSPRecordService(TComponent* Owner)
	: TService(Owner)
{
}

void __stdcall ServiceController(unsigned CtrlCode)
{
	RTSPRecordService->Controller(CtrlCode);
}

TServiceController __fastcall TRTSPRecordService::GetServiceController(void)
{
	return (TServiceController) ServiceController;
}

//---------------------------------------------------------------------------
typedef struct {
   WideString Name;
   WideString URL;
} TIPCameraInfo;

TIPCameraInfo IPCAMERAS[VIDEOGRABBERSCOUNT] = {
   {"camera1", "rtsp://192.168.0.210/axis-media/media.amp?videocodec=h264"},
   {"camera2", "rtsp://192.168.0.210/axis-media/media.amp?videocodec=h264"}
   };

   //REPLACE "C:\YourFolder\" below by the real folder where the recorded files must be stored
   // ALSO Go to Project | Options | Packages | Runtime Packages, and uncheck "Link with Runtime Packages", otherwise the service will fail to start with error 5
   //WideString STORAGEFOLDER = "C:\\YourFolder\\";
   WideString STORAGEFOLDER = "C:\\rUnit_H_DO_NOT_ERASE\\TVideoGrabber\\Shareware\\CBuilder\\DEMOS\\RTSPRecorderService\\Win32\\Debug";

//---------------------------------------------------------------------------

void __fastcall TRTSPRecordService::ServiceStart(TService *Sender, bool &Started)

{
Started = true;
return;
   for (int i = 0; i < VIDEOGRABBERSCOUNT; i ++)
   {
	  VideoGrabbers[i] = new TVideoGrabber((System::Classes::TComponent*) NULL);
	  TVideoGrabber *Vg = VideoGrabbers[i];

	  Vg->VideoRenderer = vr_None;
	  Vg->FrameGrabber = fg_Disabled; // SAVES LOT OF CPU
	  Vg->VideoSource = vs_IPCamera;
	  Vg->IPCameraURL = IPCAMERAS[i].URL;
	  Vg->RecordingMethod = rm_MP4;
	  Vg->RecordingTimer = rt_RecordToNewFile;
	  Vg->StoragePath = STORAGEFOLDER;
	  Vg->RecordingTimerInterval = 10; // 10 seconds
	  Vg->AutoFileName = fn_DateTime;
	  Vg->AutoFilePrefix = IPCAMERAS[i].Name + "_";
	  Vg->AutoFileSuffix = "_record";
	  Vg->AutoFileNameDateTimeFormat = "yyyy.mm.dd-hh.mm.ss.zzz";
	  Vg->StartRecording();
   }

}
//---------------------------------------------------------------------------

void __fastcall TRTSPRecordService::ServiceExecute(TService *Sender)
{
while (! Terminated) {
	   ServiceThread->ProcessRequests (false);
	   Sleep(50);
	}
	return;
	while (!Terminated)
	{
	   for (int i = 0; i < VIDEOGRABBERSCOUNT; i ++)
	   {
		  VideoGrabbers[i]->ContinueProcessing();
	   }
	   Sleep(50);
	   ServiceThread->ProcessRequests (false);
	}

	// !!! Stopping and destroying the TVideoGrabber instances must be performed HERE (and not from OnStop event)
	for (int i = 0; i < VIDEOGRABBERSCOUNT; i ++)
	{
	   TVideoGrabber *Vg = VideoGrabbers[i];
	   Vg->Stop();
	   Vg->ContinueProcessing();
	   delete Vg;
	}
}
//---------------------------------------------------------------------------


