4

For our current project we need a video chat application which keeps track of the duration of those video conversations.

Every conversation also has a limited time after which the chat terminates.

I've build already the basic module, but i've got problems implementing that timer. In a WOWZA 1-to-1 Video Chat Application are always 2 Streams:

User1 publishes a stream, which User2 plays (subscribes to)
User2 publishes a stream, which User1 plays (subscribes to)

The limited time amount is saved (before the chat) in a database.

Now what's the best way to decrease this amount?

I can't do that in a StreamListener, because there are always two stream and it would decrease twice.

Maybe some sort of Singleton?

Thanks!

rookian
  • 1,045
  • 14
  • 38

1 Answers1

1

Instead of a singleton, if your video-chat time does not change once the chat starts, you can implement a module and attach a TimerTask on play or publish event. Below is an example for you to help you get started.

import java.util.Timer;
import java.util.TimerTask;

import com.wowza.wms.amf.AMFDataList;
import com.wowza.wms.client.IClient;
import com.wowza.wms.logging.WMSLogger;
import com.wowza.wms.module.*;
import com.wowza.wms.request.RequestFunction;
import com.wowza.wms.stream.IMediaStream;
import com.wowza.wms.stream.IMediaStreamActionNotify;

public class KillAfterNSeconds extends ModuleBase implements IMediaStreamActionNotify {

    private class StreamKiller extends TimerTask{       
        private IMediaStream stream;        
        public StreamKiller(IMediaStream s){
            this.stream = s;
        }       
        @Override
        public void run() {
            try{
                // Complete all your clean up, such as :
                if(stream != null){
                    WMSLogger.getLogger("").info("Killing stream: " + stream.getName());
                    stream.getClient().shutdownClient();
                    stream.shutdown();
                    stream.close();
                }               
            }catch(Exception e){}
        }
    }

    public void publish(IClient paramIClient,
            RequestFunction paramRequestFunction, AMFDataList paramAMFDataList) {
        WMSLogger.getLogger("").info("Start running publish..");
        IMediaStream stream = getStream(paramIClient, paramRequestFunction);
        stream.addClientListener(this);
        Timer timer = new Timer();
        TimerTask task = new StreamKiller(stream);
        // replace here with the actual time to kill stream
        timer.schedule(task, 10000); 
        invokePrevious(paramIClient, paramRequestFunction, paramAMFDataList);
        WMSLogger.getLogger("").info("Finish running publish..");
        return;
    }

    @Override
    public void onUnPublish(IMediaStream stream, String streamName,
            boolean isRecord, boolean isAppend) {
        WMSLogger.getLogger("").info("Start onUnPublish..");
        double elapSeconds = stream.getElapsedTime().getTimeSeconds();
        WMSLogger.getLogger("").info("Elapsed time " + elapSeconds);
        WMSLogger.getLogger("").info("Finish running onUnPublish..");
    }

    @Override
    public void onPause(IMediaStream arg0, boolean arg1, double arg2) {     
    }
    @Override
    public void onPlay(IMediaStream arg0, String arg1, double arg2,
            double arg3, int arg4) {
    }
    @Override
    public void onPublish(IMediaStream arg0, String arg1, boolean arg2,
            boolean arg3) {
    }
    @Override
    public void onSeek(IMediaStream arg0, double arg1) {
    }
    @Override
    public void onStop(IMediaStream arg0) {     
    }

}
ikikenis
  • 340
  • 1
  • 7
  • Good approach. I'll have to track the (remaining/elapsed) time. I found out that every stream object's got a ElapsedTime object, which tracks that time. But if the chat ends _before_ that time is over, i'll have to save the difference to a database. How could I do this best? – rookian Feb 22 '13 at 13:00
  • On `publish` method, you can attach a client listener (`IMediaStreamActionNotify`) to the stream, then you can get elapsed time in `onUnPublish` function when the chat is over. I modified the answer for you. You can also add a field to module, set the field to some value when timer task runs. And then you can track whether chat session has been terminated by timer task or it has been terminated before the timer. – ikikenis Feb 22 '13 at 17:58
  • not the exact result how i've done it, but you helped me a lot, gratz on the bounty. – rookian Feb 27 '13 at 10:31