Seize / joy4

Golang audio/video library and streaming server
MIT License
13 stars 4 forks source link

write: broken pipe #18

Open moneyzmey opened 5 years ago

moneyzmey commented 5 years ago

Hi ! I try to use rtmp client to youtube And got error "write: broken pipe"

this error I catch on func: func (self *Conn) WritePacket(pkt av.Packet) -> self.writeAVTag(tag, int32(timestamp));

Could you help me with it ?

a-rose commented 5 years ago

Hi,

Can you give my the command line that you are using ? I can try to have a look later. Cheers

moneyzmey commented 5 years ago

May I have your email address ?

a-rose commented 5 years ago

I'd rather keep the discussion public; you can just remove the Url and Key from the command line, if that's what bothers you ;)

moneyzmey commented 5 years ago

It's ok. I don't worry about my keys, because I can change the at any time.

So what I try to do: I trying to catch incoming packets, push them to the queue and after that read from the queue and push to the different rtmp connects.

my example:

package main

import (
    "fmt"
    "github.com/Seize/joy4/av/avutil"
    "github.com/Seize/joy4/av/pubsub"
    "github.com/Seize/joy4/format/rtmp"
    "log"
    "sync"
    "time"
)

const(
    urlYoutube = "rtmp://a.rtmp.youtube.com/live2/{MY_RTMP_KEY}"
    urlMixer = "rtmp://ingest-ams.mixer.com:1935/beam/{MY_RTMP_KEY}"
)

func main()  {

    rtmp.Debug = true

    server := &rtmp.Server{}

    l := &sync.RWMutex{}
    type Channel struct {
        que *pubsub.Queue
    }
    channels := map[string]*Channel{}

    startPublish := func(path string) {

        time.Sleep(time.Second * 2)

        conn, err := rtmp.Dial(urlMixer); if err != nil{
            fmt.Println(err)
            return
        }

        l.RLock()
        ch := channels[path]
        l.RUnlock()

        if ch != nil {
            cursor := ch.que.Latest()

            //demuxer := &pktque.FilterDemuxer{Demuxer: cursor, Filter: &pktque.WaitKeyFrame{}}

            if err := avutil.CopyFile(conn, cursor); err != nil{
                fmt.Println("error on copy to packets to connection: ", err)
                panic("go panic")
            }
        }

    }

    /// publisher
    server.HandlePublish = func(conn *rtmp.Conn) {

        path := conn.URL.Path

        fmt.Println(fmt.Sprintf("Come HandlePublish(%s)", conn.URL.Path))

        streams, _ := conn.Streams()

        l.Lock()
        ch := channels[conn.URL.Path]
        if ch == nil {
            ch = &Channel{}
            ch.que = pubsub.NewQueue()

            if err := ch.que.WriteHeader(streams); err != nil{
                log.Println("err on writing headers to queue: ", err)
                return
            }

            channels[conn.URL.Path] = ch
        } else {
            ch = nil
        }
        l.Unlock()

        if ch == nil {
            return
        }

        go startPublish(path)

        if err := avutil.CopyFile(ch.que, conn); err != nil{
            fmt.Println("error on copy: ", err)
            return
        }

        fmt.Println("Leaving HandlePublish()")

        l.Lock()
        delete(channels, conn.URL.Path)
        l.Unlock()

        ch.que.Close()

    }

    server.ListenAndServe()

}

I'm try stream packets to:

  1. Twitch (works fine)
  2. Youtube (error broken pipe)
  3. Mixer (error broken pipe)

On connection to Mixer platform, got error on command(connect) message. For fix this I change AMF object on func writeConnect(rtmp.go:596)

FROM:

    flvio.AMFMap{
        "app":           path,
        "flashVer":      "MAC 22,0,0,192",
        "tcUrl":         getTcUrl(self.URL),
        "fpad":          false,
        "capabilities":  15,
        "audioCodecs":   4071,
        "videoCodecs":   252,
        "videoFunction": 1,
    }

TO:

        flvio.AMFMap{
            "app":           path,
            "flashVer":      "FMS.3.1",
            //"type":            "nonprivate",
            //"flashVer":      "MAC 22,0,0,192",
            "tcUrl":         getTcUrl(self.URL),
            //"fpad":          false,
            //"capabilities":  15,
            //"audioCodecs":   4071,
            //"videoCodecs":   252,
            //"videoFunction": 1,
        },

After that message connect goes fine.

But after command messages on AV packet got error: broken pipe.

Also I try to debug rtmp server. You can get dump HERE

my system configuration:

System Software Overview:

      System Version: macOS 10.14.3 (18D42)
      Kernel Version: Darwin 18.2.0
      Boot Volume: Без названия
      Boot Mode: Normal
      Computer Name: Mac mini
      User Name: moneyzmey
      Secure Virtual Memory: Enabled
      System Integrity Protection: Enabled
      Time since boot: 25 days 17:26

my go version:

go version go1.11.5 darwin/amd64

For streaming to rtmp server i use OBS (version: OBS 22.0.3 (mac))

My encoder setting you can get from obs log

OBS LOG:

09:33:58.967: ---------------------------------
09:33:58.967: [x264 encoder: 'streaming_h264'] preset: medium
09:33:58.967: [x264 encoder: 'streaming_h264'] profile: main
09:33:58.967: [x264 encoder: 'streaming_h264'] settings:
09:33:58.967:   rate_control: CBR
09:33:58.967:   bitrate:      4500
09:33:58.967:   buffer size:  4500
09:33:58.967:   crf:          0
09:33:58.967:   fps_num:      30
09:33:58.967:   fps_den:      1
09:33:58.967:   width:        1920
09:33:58.967:   height:       1080
09:33:58.967:   keyint:       60
09:33:58.967: 
09:33:58.978: [CoreAudio AAC: 'Track1']: settings:
09:33:58.978:   mode:          AAC
09:33:58.978:   bitrate:       160
09:33:58.978:   sample rate:   44100
09:33:58.978:   cbr:           on
09:33:58.978:   output buffer: 1536
09:33:58.978: [rtmp stream: 'adv_stream'] Connecting to RTMP URL rtmp://0.0.0.0/live...
09:33:58.980: HandShake: client signature does not match!
09:33:58.982: [rtmp stream: 'adv_stream'] Connection to rtmp://0.0.0.0/live successful
09:33:59.003: ==== Streaming Start ===============================================
09:34:33.090: [rtmp stream: 'adv_stream'] User stopped the stream
09:34:33.090: Output 'adv_stream': stopping
09:34:33.090: Output 'adv_stream': Total frames output: 968
09:34:33.090: Output 'adv_stream': Total drawn frames: 1024
09:34:33.093: ==== Streaming Stop ================================================
moneyzmey commented 5 years ago

Do you have any idea ?

a-rose commented 5 years ago

Hi !

I haven't had time to look into this. I just tried to reproduce the problem with Youtube, and I also get the "write: broken pipe" when I stream with Obs. I don't know yet what causes the problem, but when I tried streaming with ffmpeg (ffmpeg -stream_loop -1 -re <MY_FILE> -c copy -f flv rtmp://0.0.0.0:1935) it worked correctly. I'll have to keep looking.

Cheers

moneyzmey commented 5 years ago

HI! Thanks a lot, I'll continue my investigation also

ghost commented 5 years ago

I've experienced the broken pipe issue as well for youtube. It seems higher bitrates are what cause it. For me, anything over 1000 causes the connection to fail. I hope that helps narrow it down

moneyzmey commented 5 years ago

The main problem that bitrate 1000 is too low for live videos. Also youtube have table with bitrate and resolution: https://support.google.com/youtube/answer/2853702?hl=en you can check it here

ghost commented 5 years ago

I know it’s too low. If I try to do higher the pipe gets disconnected

On Mon, Jun 24, 2019 at 10:09 AM moneyzmey notifications@github.com wrote:

The main problem that bitrate 1000 is too low for live videos. Also youtube have table with bitrate and resolution: https://support.google.com/youtube/answer/2853702?hl=en you can check it here

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/Seize/joy4/issues/18?email_source=notifications&email_token=AACJ5LCTZQMLVSWQ5F47CBDP4DIQXA5CNFSM4G237XDKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODYNBQIQ#issuecomment-505026594, or mute the thread https://github.com/notifications/unsubscribe-auth/AACJ5LAMYYGS7LMUVRJ4FU3P4DIQXANCNFSM4G237XDA .

moneyzmey commented 5 years ago

I try to investigate this issue when have time. I'll let you know if problem will be solved