onPublish is fired when the mediaCaster is first started. This is the same for both the API methods and the GUI (starting streams from the Stream Files page).
onUnPublish is fired when the mediaCaster is shut down.
To capture when the mediaCaster actually starts or stops the connection and stream from the remote server, you will also need to implement the IMediaCasterNotify2 interface. This interface provides event methods that will fire for all of the mediaCaster events.
The ones you will primarily be interested in are
onConnectStart - fired when a connection or reconnection is started but before it actually starts
onConnectSuccess - fired once the connection is successfully made
onConnectFailure - fired if the connection attempt fails
onStreamStart - fired when the stream actually starts running
onStreamStop - fired when the stream stops running
When a mediaCaster first starts, you will see the following event order (assuming a successful start)
onConnectStart -> onConnectSuccess -> onStreamStart
If the mediaCaster resets, you will see onStreamStop -> onConnectStart -> onConnectSuccess hopefully followed by onStreamStart
If the remote stream cannot be located then you will see onConnectStart -> onConnectSuccess repeated each time the mediaCaster resets until it shuts down.
If the remote server cannot be located, you will see onConnectStart -> onConnectFailure repeated each time it resets.
You set your module to use the interface by calling the appInstance.addMediaCasterListener form your module. This only needs to be done once and probably from the onAppStart method.
ModuleLoopUntilLive in the Module Collection shows how to use the interface and also detect in the onPublish & onUnPublish methods if the workflow should be handed off to the mediaCaster listener.