2012-10-09 110 views
3

我正在訪問正在流式傳輸到另一個程序的Live555的視頻服務器。我想發送rtsp暫停和播放命令到視頻服務器,以停止流媒體到任何其他程序。這可能嗎?我的代碼似乎沒有做任何事情。我可以連接到服務器,服務器驗證,我收到了一個完整的暫停命令:如何使用Live555暫停和播放流

VideoServer.h

//must make this store session so we can access the session in the static 
//callbacks 
class MyRTSPClient: public RTSPClient{ 
protected: 
    MyRTSPClient(UsageEnvironment& env, char const* rtspURL, 
    int verbosityLevel, char const* applicationName, portNumBits tunnelOverHTTPPortNum): 
    RTSPClient(env, rtspURL, verbosityLevel, applicationName, tunnelOverHTTPPortNum) 
    { 

    } 

public: 

    MediaSession* session_; 

    bool sessionStarted_; 

    static MyRTSPClient* createNew(UsageEnvironment& env, char const* rtspURL, 
            int verbosityLevel = 0, 
            char const* applicationName = NULL, 
            portNumBits tunnelOverHTTPPortNum = 0) 
    { 
     return new MyRTSPClient(env, rtspURL, verbosityLevel, applicationName,  tunnelOverHTTPPortNum); 
     } 

}; 


class VideoServer 
{ 

public: 

    VideoServer(); 


private: 
    TaskScheduler* scheduler_; 
    UsageEnvironment* env_; 
    MyRTSPClient* rtspClient_; 
    char eventLoopWatchVariable; 

    //Asynchronously start the connection 
    void StartConnection(); 

    static void callbackDESCRIBE(RTSPClient* rtspClient, int resultCode, char* resultString); 

    static void callbackPAUSEPLAY(RTSPClient* rtspClient, int resultCode, char* resultString); 
}; 

VideoServer.cpp

VideoServer::VideoServer() 
{ 
    eventLoopWatchVariable = 0; 
    scheduler_ = BasicTaskScheduler::createNew(); 
    env_ = BasicUsageEnvironment::createNew(*scheduler_); 

    //create rtsp client with default params and our url and environment 
    rtspClient_ = MyRTSPClient::createNew(*env_, 
     MINI_HARV_AXIS_RTSP_URL, 4, "jtalon5"); 

    //call description to initialize the session 

    if (rtspClient_ == NULL) { 
    std::cout << "Failed to create a RTSP client for URL \"" << 
    MINI_HARV_AXIS_RTSP_URL << std::endl; 
    return; 
    } 
    std::cout << "made the client!" << std::endl; 
    // Next, send a RTSP "DESCRIBE" command, to get a SDP description for the stream. 
    // Note that this command - like all RTSP commands - is sent asynchronously; we do not block, waiting for a response. 
    // Instead, the following function call returns immediately, and we handle the RTSP response later, from within the event loop: 
    rtspClient_->sendDescribeCommand(callbackDESCRIBE); 

    //start doEventLoop in separate thread so it is not blocking 
    boost::thread thr1(&MiniHarvAxisInterface::StartConnection, this); 

} 

void VideoServer::StartConnection() 
{ 
    env_->taskScheduler().doEventLoop(&eventLoopWatchVariable); 
} 

void MiniHarvAxisInterface::callbackDESCRIBE(RTSPClient* rtspClient, int resultCode, char* resultString) 
{ 
    std::cout << "describe" << resultString << std::endl; 
    UsageEnvironment& env = rtspClient->envir(); 

    char* const sdpDescription = resultString; 
    ((MyRTSPClient*)rtspClient)->session_ = MediaSession::createNew(env, sdpDescription); 
    ((MyRTSPClient*)rtspClient)->sessionStarted_ = true; 

    if(((MyRTSPClient*)rtspClient)->session_ == NULL) 
    std::cout << "did not create session" << std::endl; 
    else 
    std::cout << "created session" << std::endl; 

    rtspClient->sendPauseCommand(*((MyRTSPClient*)rtspClient)->session_, &callbackPAUSEPLAY); 
} 

void MiniHarvAxisInterface::callbackPAUSEPLAY(RTSPClient* rtspClient, int resultCode, char* resultString) 
{ 
    //do nothing 
} 

看來,如果我只能暫停並播放我在此過程中創建的流。這是使用Live555的情況嗎?

回答

0

是的,這在Live555中是可行的。

默認情況下LIVE555什麼也不做,除非你重新定義pauseStream功能(見pauseStream功能評論請在下面LIVE555源):

// default implementation: do nothing 

你必須創建你自己的會話類,你必須重新定義pauseStream功能如圖所示在下面的例子:

你的媒體會話.h文件中應該是這個樣子:

#include <MediaSubsession.hh> 
class YOURMediaSubsession: public MediaSubsession { 

... //You can leave this empty if you like 

private: 

... 

    virtual void pauseStream(unsigned /*clientSessionId*/,void* /*streamToken*/); 
}; 

你的媒體會話.cpp文件應該是這個樣子:

#include <YOURMediaSubsession.hh> 
void YOURMediaSubsession::pauseStream(unsigned /*clientSessionId*/, 
        void* /*streamToken*/) { 
    // default implementation: do nothing 
} 

的你可以添加任何你在這個函數一樣,它是否可以停止/終止所有流,或獲取編碼器,以保持編碼而同一個框架可以讓您將流暫停預設時間的錯覺完全取決於您。

注意,我可以在代碼中看到,您使用的是默認MediaSession類作爲對您的代碼在這裏:

((MyRTSPClient*)rtspClient)->session_ = MediaSession::createNew(env, sdpDescription); 

您將需要根據MediaSession類來構建自己的YOURMediaSubsession類來重新定義如上所示的pauseStream函數,然後你暫停,而不是live555。它應該看起來更像:

((MyRTSPClient*)rtspClient)->session_ = YOURMediaSubsession::createNew(env, sdpDescription);