-- this is an example of an early media application, that
-- has to do some possibly blocking operation on start of processing,
-- so session processing is delayed (here with timer 1)
-- later 183 is sent, and after playing a message, 200 OK is sent, 
-- and 10 seconds are recorded to a file.

import(mod_dlg);

initial state START;

transition "on invite" START - invite / { 
       log(2, run invite!); 
       -- we don't want to have the default 200 OK reply
       dlg.reply(100, trying...);
       setTimer(1, 2);
} -> runinvite;
state runinvite;

transition "go early" runinvite - timerTest(#id==1) -> reply_183;
state reply_183
      enter {
        -- this triggers "on sess start" transition
        dlg.acceptInvite(183, progress);
};

transition "on sess start" reply_183 - earlySession -> play_early_msg;
state play_early_msg
      enter {
       log(2, run start!);
       connectMedia();
       playFile(wav/default_en.wav);
};

transition "play file failed" play_early_msg - exception; test(#type==file) / {
        log(1, Error playing file!); 
        dlg.reply(500, server error); 
        stop 
} -> FIN;

transition "early message ended" play_early_msg - noAudioTest -> recording;
state recording
      enter {
        dlg.acceptInvite(200, ok);
        recordFile(/tmp/test.wav);
        setTimer(3,10);
};

transition "recording failed" recording - exception; / 
   log(1, Error recording!); stop(true) -> FIN;

transition "bye in early" (runinvite, reply_183, play_early_msg, recording) - hangup / {
	log(2, hangup event received);
        stop(false)
} -> FIN;

transition "recording timer" recording - timerTest(#id==3) / stop -> FIN;
transition "bye in recording" recording - hangup / stop -> FIN;

state FIN;
