nareix / joy4

Golang audio/video library and streaming server
MIT License
2.66k stars 501 forks source link

Exception Handling About RTMP server #137

Closed ohjiwoo123 closed 1 year ago

ohjiwoo123 commented 1 year ago

if I use this url for RTMP network stream rtmp://localhost:1935/app/test First, Using OBS publish the data. Second, Using example/rtmp_publish/main.go, publishing the same url rtmp://localhost:1935/app/test I think that this is definitly issue. Connection becomes unstable. There is no Exception Handling about Same Stream Key. Is there Any Good Idea?

server.HandlePublish = func(conn *rtmp.Conn) {
        logger.Info("server.HandlePublish start Call")
        streams, _ := conn.Streams()

        l.Lock()
        // if key(rtmp url exists), conn.close()
        ch, ok := channels[conn.URL.Path]
        if ok {
            logger.Errorf("Already published: %s", conn.URL.Path)
            conn.Close() // add this line. 
            return
        }
        if ch == nil {
            logger.Info("after inside of HandlePublish ch is nil")
            ch = &Channel{}
            ch.que = pubsub.NewQueue()
            ch.que.WriteHeader(streams)
            channels[conn.URL.Path] = ch
        } else {
            ch = nil
        }
        l.Unlock()
        if ch == nil {
            return
        }

        avutil.CopyPackets(ch.que, conn)
        l.Lock()
        delete(channels, conn.URL.Path)
        l.Unlock()
        ch.que.Close()
    }

I tried add conn.Close() When Already Same StreamKey("test") Published. But It is not solution of solving this problem.

ref : https://github.com/nareix/joy4/blob/master/examples/http_flv_and_rtmp_server/main.go https://github.com/nareix/joy4/blob/master/format/rtmp/rtmp.go

ohjiwoo123 commented 1 year ago

I Found the Solution