/// <summary> /// Implements the ShareStreamedAudio function specified in the IPeerChannel service contract above. /// </summary> /// <param name="mdp">the audio packet to send</param> public void ShareStream(StreamedPacket mdp) { //The audio can be in one of two states: Initial, meaning it is ready to receive a new audio stream, //or communicating meaning it is either receiving or transmitting. switch (streamedState) { case StreamedStateType.Initial: //Initialize the data packet; streamedState = StreamedStateType.Communicating; mLastChunk = -1; ////write in the textbox that someone is sending us an audio file. //ShareTextMessage(mdp.senderNodeName, String.Format(" is Sending you an audio file")); break; case StreamedStateType.Communicating: //update the most recent packet received so we know that the sender hasn't //disappeared from the network, or suddenly stopped sending. mChunk = mdp.packetNumber; //found the end of the stream, reset. if (mdp.endOfStream) { streamedState = StreamedStateType.Initial; } break; } //notify anyone registered for the event. if (null != StreamChanged) { StreamChanged(this, new StreamedChangedEventArgs(mdp)); } }
public StreamingHelper(IPeerChannelWrapper participant, string nodeName, ShareStreamHandler s) { mNodeName = nodeName; streamedState = StreamedStateType.Initial; mChunk = 0; mLastChunk = -1; del = s; ////////////////////////////////////////////////////////////////////////// // Start up an Audio Watchdog timer so if we don't receive a packet after the hard-coded // 20000ms we'll timeout and assume the sending node was lost. ////////////////////////////////////////////////////////////////////////// mWatchDog = new Timer(new TimerCallback(Watchdog)); mWatchDog.Change(0, 20000);//every 20000ms get a callback }
int mLastChunk, mChunk; //These member variables should never have the same number..otherwise the incomming audio has timed out and the sender is presumed to be disconnected. /// <summary> /// The callback setup in the constructor. This will get called once every n number of seconds(hard coded in the constructor) /// If the numbers are the same, it means we didn't receive a packet inbetween calls. Therefore we presume that the sending node /// has either stopped sending, or we do not have a connection with them. /// </summary> /// <param name="state"></param> public void Watchdog(object state) { if (streamedState == StreamedStateType.Communicating) { //could time out... if (mChunk == mLastChunk) { //timed out..reset the stream back go back to the initial state. streamedState = StreamedStateType.Initial; //notify anyone else that wants to know. if (null != mReceiveTimeoutChanged) { mReceiveTimeoutChanged(this, new EventArgs()); } } else { mLastChunk = mChunk;//new data received update it. } } }