Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: How to timeout the stream after X second play

  1. #11
    Join Date
    Dec 2007
    Posts
    25,641

    Default

    You will have to work through it. One correction tho, in IMediaStreamActionNotify2 you should start the timer in the onPlay and stop the timer in onStop

    Richard

  2. #12

    Default

    Quote Originally Posted by rrlanham View Post
    You will have to work through it. One correction tho, in IMediaStreamActionNotify2 you should start the timer in the onPlay and stop the timer in onStop

    Richard
    Hello People,

    I´m trying to do something similiar but is not working.

    Actually what I want to do is to start the stream at the 0 second of the music and the Wowza by him self stop the streaming after 30 seconds.

    I´m new at WMS and still am trying to understand the idea of extending Wowza.

    One thing that I notice is that the onPlay e onStop methods from MyMediaStreamListener only are called when somebody press the button on the the player.

    What can I do?

    I am send the codes that I Have until now.


    MyMediaStreamListener with the WatchDog code to
    Code:
    package br.com.imusica.stream;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.util.Date;
    import java.util.List;
    import java.util.Timer;
    import java.util.TimerTask;
    
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpStatus;
    import org.apache.commons.httpclient.methods.PostMethod;
    
    import br.com.imusica.util.Reader;
    
    import com.wowza.wms.amf.AMFPacket;
    import com.wowza.wms.application.IApplicationInstance;
    import com.wowza.wms.stream.IMediaStream;
    import com.wowza.wms.stream.IMediaStreamActionNotify2;
    import com.wowza.wms.stream.MediaStreamMap;
    
    public class MyMediaStreamListener implements IMediaStreamActionNotify2 {
        
        private Tests novoTests;
        private Reader server_;
        
        public MyMediaStreamListener(Tests tests) {
            novoTests = tests;
            server_ = new Reader();
            getServer_().findServers("/usr/local/WowzaMediaServer/content/Imusica.conf");
        }
        
        public void onMetaData(IMediaStream stream, AMFPacket metaDataPacket) {
            novoTests.log("------------- onMetaData --------------");
        }
        
        public void onPauseRaw(IMediaStream stream, boolean isPause, double location) {
            novoTests.log("------------- onPauseRaw --------------");
            novoTests.log("valor = " + isPause);
            novoTests.log("location = " + location);
            novoTests.log("-------------------------------------");
            
        }
        
        public void onPause(IMediaStream stream, boolean isPause, double location) {
            novoTests.log("-------------ON PAUSE--------------");
            novoTests.log("valor = " + isPause);
            novoTests.log("location = " + location);
            novoTests.log("-------------------------------------");
        }
        
        public void onPlay(IMediaStream stream, String streamName, double playStart, double playLen, int playReset) {
            
            novoTests.log("onPublish - stream name: " + stream.getName());
            novoTests.log("query string: " + stream.getQueryStr());
            novoTests.log("video codec: " + stream.getPublishVideoCodecId());
            StreamWatchDog watchdog = new StreamWatchDog();
            IApplicationInstance appInstance;
            try {
                try {
                    appInstance = stream.getClient().getAppInstance();
                }
                catch (Exception ex) {
                    appInstance = stream.getRTPStream().getAppInstance();
                }
                
                watchdog.appInstance = appInstance;
                watchdog.streamName = streamName;
                watchdog.start();
                appInstance.getProperties().setProperty(streamName, watchdog);
            }
            catch (Exception ex) {
            }
            
            novoTests.log("-------------ON PLAY-------------- " + " IMediaStream: " + stream.getName() + " StreamName: " + streamName + " PlayStart: " + playStart + " PlayLen: " + playLen + " PlayReset: " + playReset);
            
            int id = 0;
            String queryStr = "";
            String sessionid = "";
            String portalHost = "";
            String userAgentId = "0";
            String ip = "";
            String extension = "";
            String ehWap = "0";
            
            if (stream.getRTPStream() == null) {
                
                queryStr = stream.getQueryStr();
                
                // buscando o id do fonograma na query string
                if (stream.getQueryStr().split("=").length >= 2) {
                    id = Integer.parseInt(stream.getQueryStr().split("=")[1]);
                }
                
                // buscando o session id do usuário na query string
                if (stream.getQueryStr().split("=").length >= 3) {
                    sessionid = stream.getQueryStr().split("=")[2];
                }
                
                // buscando a url do portal (para fazer as requisições necessárias) na query string
                if (stream.getQueryStr().split("=").length >= 4) {
                    portalHost = stream.getQueryStr().split("=")[3];
                }
                
                // buscando o userAgentId na query string
                if (stream.getQueryStr().split("=").length >= 5) {
                    userAgentId = stream.getQueryStr().split("=")[4];
                }
                
                ip = stream.getClient().getIp();
                extension = stream.getExt();
                
            }
            else {
                ehWap = "1";
                queryStr = stream.getRTPStream().getStreamQueryStr();
                
                // buscando o id do fonograma na query string
                if (stream.getRTPStream().getStreamQueryStr().split("=").length >= 2) {
                    id = Integer.parseInt(stream.getRTPStream().getStreamQueryStr().split("=")[1]);
                }
                
                // buscando o session id do usuário na query string
                if (stream.getRTPStream().getStreamQueryStr().split("=").length >= 3) {
                    sessionid = stream.getRTPStream().getStreamQueryStr().split("=")[2];
                }
                
                // buscando a url do portal (para fazer as requisições necessárias) na query string
                if (stream.getQueryStr().split("=").length >= 4) {
                    portalHost = stream.getQueryStr().split("=")[3];
                }
                
                // buscando o userAgentId na query string
                if (stream.getQueryStr().split("=").length >= 5) {
                    userAgentId = stream.getQueryStr().split("=")[4];
                }
                
                ip = stream.getRTPStream().getRTSPOriginIpAddress();
                extension = stream.getRTPStream().getStreamExt();
            }
            
            if (!extension.equalsIgnoreCase("flv")) {
                
                HttpClient httpClient = new HttpClient();
                PostMethod post = new PostMethod("http://" + portalHost + "/playHistoryController/saveBegin/");
                
                post.addParameter("phonogramId", new Integer(id).toString());
                post.addParameter("sessionId", sessionid);
                post.addParameter("ipAddress", ip);
                post.addParameter("userAgentId", userAgentId);
                post.addParameter("wap", ehWap);
                
                try {
                    int returnCode = httpClient.executeMethod(post);
                    BufferedReader br = null;
                    String readLine = "";
                    String idHistory = "";
                    if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
                        novoTests.log("The Post method is not implemented by this URI");
                        // still consume the response body
                        post.getResponseBodyAsString();
                    }
                    else {
                        
                        br = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream()));
                        novoTests.log("-------------RESPONSE BODY SAVE BEGIN-------------------");
                        novoTests.log("-------------RETURN CODE = " + returnCode + "-------------------");
                        while (((readLine = br.readLine()) != null)) {
                            // {"idHistory":id}
                            idHistory = readLine.substring(14, (readLine.length() - 2));
                            novoTests.log("ID HISTORY = " + idHistory);
                            novoTests.log(readLine);
                        }
                        novoTests.log("---------------------------------------------------------");
                    }
                    
                    if (stream.getRTPStream() == null) {
                        queryStr = queryStr + "=" + idHistory;
                        stream.setQueryStr(queryStr);
                        
                    }
                    else {
                        queryStr = queryStr + "=" + idHistory;
                        stream.setQueryStr(queryStr);
                    }
                }
                catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
        
        public void onUnPublish(IMediaStream stream, String streamName, boolean isRecord, boolean isAppend) {
            
        }
        
        public void onPublish(IMediaStream stream, String streamName, boolean isRecord, boolean isAppend) {
            
        }
        
        public void onSeek(IMediaStream stream, double location) {
            // TODO Auto-generated method stub
            novoTests.log("--------------------- onSeek ---------------------");
        }
        
        public void onStop(IMediaStream stream) {
            novoTests.log("onUNPublish");
            StreamWatchDog watchdog = (StreamWatchDog) stream.getClient().getAppInstance().getProperties().getProperty(stream.getName());
            if (watchdog != null) {
                watchdog.stop();
            }
            
            // TODO Auto-generatedmethod stub
            String sessionid = "";
            String portalHost = "";
            String idHistory = "";
            String userAgentId = "0";
            int time = 0;
            String extension = "";
            if (stream.getRTPStream() == null) {
                // buscando o session id do usuário na query string
                if (stream.getQueryStr().split("=").length >= 3) {
                    sessionid = stream.getQueryStr().split("=")[2];
                }
                
                // buscando a url do portal (para fazer as requisições necessárias) na query string
                if (stream.getQueryStr().split("=").length >= 4) {
                    portalHost = stream.getQueryStr().split("=")[3];
                }
                
                // buscando o userAgentId na query string
                if (stream.getQueryStr().split("=").length >= 5) {
                    userAgentId = stream.getQueryStr().split("=")[4];
                }
                
                // buscando o idHistory da execução na query string
                if (stream.getQueryStr().split("=").length >= 6) {
                    idHistory = stream.getQueryStr().split("=")[5];
                }
                
                time = (int) stream.getElapsedTime().getTimeSeconds();
                extension = stream.getExt();
            }
            else {
                // buscando o session id do usuário na query string
                if (stream.getQueryStr().split("=").length >= 3) {
                    sessionid = stream.getQueryStr().split("=")[2];
                }
                
                // buscando a url do portal (para fazer as requisições necessárias) na query string
                if (stream.getQueryStr().split("=").length >= 4) {
                    portalHost = stream.getQueryStr().split("=")[3];
                }
                
                // buscando o userAgentId na query string
                if (stream.getQueryStr().split("=").length >= 5) {
                    userAgentId = stream.getQueryStr().split("=")[4];
                }
                
                // buscando o idHistory da execução na query string
                if (stream.getQueryStr().split("=").length >= 6) {
                    idHistory = stream.getQueryStr().split("=")[5];
                }
                
                time = (int) stream.getElapsedTime().getTimeSeconds();
                extension = stream.getRTPStream().getStreamExt();
            }
            
            novoTests.log("onStop");
            Integer tempo = new Integer(time);
            novoTests.log("-------------ON STOP--------------");
            novoTests.log("QUERY STRING = " + stream.getQueryStr() + "-------------------------");
            novoTests.log("-------SESSION ID = " + sessionid + "---------");
            novoTests.log("-------URL PORTAL = " + portalHost + "---------");
            novoTests.log("-------USER AGENT ID = " + userAgentId + "---------");
            novoTests.log("-------HISTORY ID = " + idHistory + "---------");
            novoTests.log("-------TEMPO = " + tempo.toString() + "---------");
            
            // post para php
            
            if (!extension.equalsIgnoreCase("flv")) {
                HttpClient httpClient = new HttpClient();
                PostMethod post = new PostMethod("http://" + portalHost + "/playHistoryController/saveEnd/");
                
                // post.addParameter("sessionId", sessionid);
                post.addParameter("time", tempo.toString());
                post.addParameter("idHistory", idHistory);
                if (stream.getRTPStream() == null) {
                    try {
                        int returnCode = httpClient.executeMethod(post);
                        BufferedReader br = null;
                        String readLine;
                        
                        if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
                            
                            // still consume the response body
                            post.getResponseBodyAsString();
                        }
                        else {
                            br = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream()));
                            novoTests.log("------------RESPONSE BODY SAVE END-------------");
                            // novoTests.log("url = " + this.getServer_().getCoreServer_() +
                            // "/playHistoryController/saveEnd/");
                            novoTests.log("--------RETURN CODE = " + returnCode + "---------------");
                            while (((readLine = br.readLine()) != null)) {
                                novoTests.log(readLine);
                            }
                            novoTests.log("----------------------------------------");
                        }
                    }
                    catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
                else {
                    novoTests.log("-----------------ENTROU----------------------");
                    post.addParameter("sessionId", sessionid);
                    post.addParameter("wap", "1");
                    try {
                        int returnCode = httpClient.executeMethod(post);
                        BufferedReader br = null;
                        String readLine;
                        
                        if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
                            
                            // still consume the response body
                            post.getResponseBodyAsString();
                        }
                        else {
                            br = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream()));
                            novoTests.log("------------RESPONSE BODY SAVE END-------------");
                            // novoTests.log("url = " + this.getServer_().getCoreServer_() +
                            // "/playHistoryController/saveEnd/");
                            novoTests.log("--------RETURN CODE = " + returnCode + "---------------");
                            while (((readLine = br.readLine()) != null)) {
                                novoTests.log(readLine);
                            }
                            novoTests.log("----------------------------------------");
                        }
                    }
                    catch (Exception e1) {
                        e1.printStackTrace();
                    }
                }
            }
        }
        
        public void setServer_(Reader server) {
            server_ = server;
        }
        
        public Reader getServer_() {
            return server_;
        }
        
        private class StreamWatchDog {
            
            public Timer mTimer;
            public TimerTask mTask;
            public String streamName;
            public IApplicationInstance appInstance;
            long streamLastSeq;
            boolean isMissing = false;
            long currSeq = 0;
            String msg;
            
            public StreamWatchDog() {
                mTask = new TimerTask() {
                    
                    public void run() {
                        novoTests.log("Run StreamWatchDog");
                        MediaStreamMap mediamap = appInstance.getStreams();
                        IMediaStream stream = mediamap.getStream(streamName);
                        
                        List packets = stream.getPlayPackets();
                        if (packets.size() == 0) {
                            msg = "Stream not started";
                        }
                        else {
                            AMFPacket packet = (AMFPacket) packets.get(packets.size() - 1);
                            currSeq = packet.getSeq();
                            if (currSeq != streamLastSeq) {
                                streamLastSeq = currSeq;
                                msg = "Stream OK";
                            }
                            else {
                                msg = "Stream Appears Stalled";
                            }
                        }
                        appInstance.broadcastMsg("streamStats", streamName, currSeq, msg, new Date());
                    }
                };
            }
            
            public void start() {
                
                if (mTimer == null) {
                    mTimer = new Timer();
                }
                mTimer.schedule(mTask, 10000, 10000);
                novoTests.log("Start StreamWatchDog");
            }
            
            public void stop() {
                if (mTimer != null) {
                    mTimer.cancel();
                    mTimer = null;
                    novoTests.log("Stop StreamWatchDog");
                }
            }
        }
    }

    And the WAP classe that implements IModuleOnRTPSession
    Code:
    package br.com.imusica.stream;
    
    import com.wowza.wms.module.IModuleOnRTPSession;
    import com.wowza.wms.module.ModuleBase;
    import com.wowza.wms.rtp.model.RTPSession;
    
    public class Wap extends ModuleBase implements IModuleOnRTPSession {
        
        public void onRTPSessionCreate(RTPSession rtpSession) {
            getLogger().info("onRTPSessionCreate: " + rtpSession.getSessionId());
            String someCookie = rtpSession.getQueryStr();
            getLogger().info("----------" + someCookie + "---------------");
        }
        
        public void onRTPSessionDestroy(RTPSession rtpSession) {
            getLogger().info("onRTPSessionDestroy: " + rtpSession.getSessionId());
            String someCookie = rtpSession.getQueryStr();
            getLogger().info("----------" + someCookie + "---------------");
        }
        
    }

  3. #13
    Join Date
    Dec 2007
    Posts
    25,641

    Default

    There isn't a way to do this for RTSP.

    For vod streaming with Flash RTMP client, take a look at this example:
    http://www.wowza.com/forums/content....insert-Midroll

    For live streaming, you can use scheduler, and this works across all clients:
    http://www.wowza.com/forums/content....t-in-smil-file

    Richard

  4. #14

    Default

    Ok. Thanks for the answer. This resolved most part of my doubts.

    Another question. Can I do the samething with HTTP?! Do you have any references about that!?

    Best Regards
    Bernardo

  5. #15
    Join Date
    Dec 2007
    Posts
    25,641

    Default

    Bernardo,

    Yes, sorry, I should have included that:

    http://www.wowza.com/forums/content....HTTP-streaming

    Richard

  6. #16

    Default

    Richard,

    Thanks for the last post. It will help for sure.

    I see that for wowza he wave three type for HTTP streaming: iOS, Flash HTTP and Smooth Streaming.

    Do I have to implement all of them or depends on the devices that I have?

    Another question. At smil file what do I have o especify? I read the article that you send to me but didn´t understand the part of the smil file.

    Best Regards
    Bernardo

  7. #17
    Join Date
    Dec 2007
    Posts
    25,641

    Default

    Take a look at the tutorial that applies to what you are doing from this list:

    http://www.wowza.com/forums/content.php?4

    Then take a look at the configuration and playback steps for each of these client types.

    Richard

  8. #18
    Join Date
    Dec 2012
    Posts
    1

    Default

    Quote Originally Posted by rrlanham View Post
    There isn't a way to do this for RTSP.

    For vod streaming with Flash RTMP client, take a look at this example:
    http://www.wowza.com/forums/content....insert-Midroll

    For live streaming, you can use scheduler, and this works across all clients:
    http://www.wowza.com/forums/content....t-in-smil-file

    Richard
    Are there any plans to add support for this ? or is it possible in the latest version (3.5). We have over 3 million video/audio assets and we're currently looking to move away from a custom RTSP proxy we build (proxying Darwin) solution which restricts the end users viewing duration to x seconds for previewing the content. Using this custom proxy we don't need to have separate preview renditions saving a significant amount of disk space so we were hoping to extend wowza to do something similar. Anyway we're trailing Wowza 3.5 at the moment and it's this is the last thing preventing us from be able to migrate our streaming solution to Wowza.

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •