How does the RTMP streaming media protocol Internet live/on-demand platform EasyDSS use ReverseProxy to interrupt the flv stream to achieve the effect of automatically stopping the broadcast?

Friends who follow Qingxi video should know that we have mentioned in other blog posts that EasyGBS can be set to automatically stop broadcasting after more than three minutes in the demo platform. In fact, during the development of EasyDSS, we also encountered this Kind of requirement: automatically shut down after playing for a period of time.

EasyDSS Watermark.png

The internal design of the flv stream of EasyDSS is: the kernel provides flv services, and the application layer performs reverse proxy to provide flv services for the browser layer. That is, when the browser requests the flv stream, it first passes through the application layer. After the application layer receives the flv stream request, it reverse proxy the request to the kernel, and finally provides the flv request.

DSS7.png

According to user needs, it is necessary to automatically interrupt the flv stream according to the configured interruption time in the reverse proxy of the application layer. The application layer is developed with Go, and the following code is used for reverse proxy:

proxy := &httputil.ReverseProxy{
   Director:     director,
   Transport:    tranport,
   ErrorHandler: errHandle,
}
// 反向代理
proxy.ServeHTTP(c.Writer, c.Request)

Transport is the stream used for data transmission. You only need to get the response data in the transmission channel. After the interruption time, close the response to interrupt the stream transmission.
Looking at the Transport code in the Go language, it does not provide an interface to get the response, so the Transport interface needs to be repackaged to get the response data.

// 可关闭 Transport
type ShutDownTransport struct {
   Trans    *http.Transport
   response *http.Response
}

// 覆盖上层 Transport
func (t *ShutDownTransport) RoundTrip(req *http.Request) (*http.Response, error) {
   res, err := t.Trans.RoundTrip(req)
   t.response = res
   return res, err
}

// 实现关闭方法
func (t *ShutDownTransport) ShutDown(d time.Duration) {
   time.AfterFunc(d, func() {
      res := t.response
      if res != nil {
         if res.Body != nil {
            res.Body.Close()
         }
      }
   })
}

The above code uses the combination mode to combine http.Transport and http.Response into a structure and implements the RoundTrip() method. The RoundTrip() method is a method that the agent needs to implement, from which the corresponding response can be obtained.

Start a timing operation in the ShutDown() method. After d time, call the response.Body.Close() method to forcibly interrupt the stream transmission to achieve this function. For more video-related solutions, you can visit TSINGSEE Qingxi Video for understanding, and please feel free to consult us.

DSS2.png

Guess you like

Origin blog.csdn.net/EasyDSS/article/details/108648494