Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unexpected signal during runtime execution #50

Closed
xuxiangyang opened this issue Oct 25, 2016 · 3 comments
Closed

unexpected signal during runtime execution #50

xuxiangyang opened this issue Oct 25, 2016 · 3 comments

Comments

@xuxiangyang
Copy link

xuxiangyang commented Oct 25, 2016

I am using master gmf with golang 1.7, I got this error when concurrency convert flv to mjpeg

fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0xaf7bab]

runtime stack:
runtime.throw(0x19d1577, 0x2a)
        /usr/local/go/src/runtime/panic.go:566 +0x95
runtime.sigpanic()
        /usr/local/go/src/runtime/sigpanic_unix.go:12 +0x2cc

goroutine 14747 [syscall, locked to thread]:
runtime.cgocall(0x895260, 0xc420358738, 0xc400000000)
        /usr/local/go/src/runtime/cgocall.go:131 +0x110 fp=0xc420358708 sp=0xc4203586c8
square_snapshot/vendor/github.com/3d0c/gmf._Cfunc_av_read_frame(0x7f6cd73fa8c0, 0xc420189560, 0xc400000000)
        ??:0 +0x4d fp=0xc420358738 sp=0xc420358708
square_snapshot/vendor/github.com/3d0c/gmf.(*FmtCtx).GetNewPackets.func1(0xc420187680, 0xc4204f3f20)
        /go/src/square_snapshot/vendor/github.com/3d0c/gmf/format.go:332 +0x157 fp=0xc4203587b0 sp=0xc420358738
runtime.goexit()
        /usr/local/go/src/runtime/asm_amd64.s:2086 +0x1 fp=0xc4203587b8 sp=0xc4203587b0
created by square_snapshot/vendor/github.com/3d0c/gmf.(*FmtCtx).GetNewPackets

what could I do with this error?

@3d0c
Copy link
Owner

3d0c commented Oct 25, 2016

@xuxiangyang Hi.
Could You provide code, which causes this SEGSEGV?

@xuxiangyang
Copy link
Author

xuxiangyang commented Oct 31, 2016

sure, Like this

package snapshot

import (
    "errors"
    "github.com/3d0c/gmf"
    "io/ioutil"
    "os"
)

func Grab(url string) (*os.File, error) {
    inputCtx, err := gmf.NewInputCtx(url)
    if err != nil {
        return nil, err
    }
    defer inputCtx.CloseInputAndRelease()

    srcVideoStream, err := inputCtx.GetBestStream(gmf.AVMEDIA_TYPE_VIDEO)
    if err != nil {
        return nil, err
    }

    codec, err := gmf.FindEncoder("mjpeg")
    if err != nil {
        return nil, err
    }

    cc := gmf.NewCodecCtx(codec)
    defer gmf.Release(cc)
    cc.SetPixFmt(gmf.AV_PIX_FMT_YUVJ420P)
    cc.SetWidth(srcVideoStream.CodecCtx().Width())
    cc.SetHeight(srcVideoStream.CodecCtx().Height())
    cc.SetTimeBase(srcVideoStream.CodecCtx().TimeBase().AVR())

    if codec.IsExperimental() {
        cc.SetStrictCompliance(gmf.FF_COMPLIANCE_EXPERIMENTAL)
    }

    if err := cc.Open(nil); err != nil {
        return nil, err
    }

    for packet := range inputCtx.GetNewPackets() {
        if packet.StreamIndex() != srcVideoStream.Index() {
            continue
        }

        ist, err := inputCtx.GetStream(packet.StreamIndex())
        if err != nil {
            return nil, err
        }
        for frame := range packet.Frames(ist.CodecCtx()) {
            if p, ready, _ := frame.EncodeNewPacket(cc); ready {
                defer gmf.Release(p)
                return newFile(p.Data())
            }
        }
    }
    return nil, errors.New("can not get Packets")
}

func newFile(b []byte) (*os.File, error) {
    fp, err := ioutil.TempFile("", "snapshot_")
    if err != nil {
        return nil, err
    }
    defer fp.Close()
    _, err = fp.Write(b)
    if err != nil {
        return nil, err
    } else {
        return fp, nil
    }
}

I concomitantly use Grab func

3d0c added a commit that referenced this issue Nov 1, 2016
Global map moved inside Packet structure
@3d0c
Copy link
Owner

3d0c commented Nov 1, 2016

@xuxiangyang Fixed via #51 Please pull latest master and test.

I don't know what type of synchronization do you use, but here is my main function which i tested:

package main

import (
    "errors"
    "io/ioutil"
    "log"
    "os"
    "sync"

    "github.com/3d0c/gmf"
)

func Grab(url string, wg *sync.WaitGroup) (*os.File, error) {
    defer wg.Done()

    inputCtx, err := gmf.NewInputCtx(url)
    if err != nil {
        return nil, err
    }
    defer inputCtx.CloseInputAndRelease()

    srcVideoStream, err := inputCtx.GetBestStream(gmf.AVMEDIA_TYPE_VIDEO)
    if err != nil {
        return nil, err
    }

    codec, err := gmf.FindEncoder("mjpeg")
    if err != nil {
        return nil, err
    }

    cc := gmf.NewCodecCtx(codec)
    defer gmf.Release(cc)
    cc.SetPixFmt(gmf.AV_PIX_FMT_YUVJ420P)
    cc.SetWidth(srcVideoStream.CodecCtx().Width())
    cc.SetHeight(srcVideoStream.CodecCtx().Height())
    cc.SetTimeBase(srcVideoStream.CodecCtx().TimeBase().AVR())

    if codec.IsExperimental() {
        cc.SetStrictCompliance(gmf.FF_COMPLIANCE_EXPERIMENTAL)
    }

    if err := cc.Open(nil); err != nil {
        return nil, err
    }

    for packet := range inputCtx.GetNewPackets() {
        if packet.StreamIndex() != srcVideoStream.Index() {
            continue
        }

        ist, err := inputCtx.GetStream(packet.StreamIndex())
        if err != nil {
            return nil, err
        }
        for frame := range packet.Frames(ist.CodecCtx()) {
            if p, ready, _ := frame.EncodeNewPacket(cc); ready {
                defer gmf.Release(p)
                return newFile(p.Data())
            }
        }
    }
    return nil, errors.New("can not get Packets")
}

func newFile(b []byte) (*os.File, error) {
    log.Println("len(b):", len(b))
    fp, err := ioutil.TempFile("./", "snapshot_")
    if err != nil {
        return nil, err
    }
    defer fp.Close()

    n, err := fp.Write(b)
    log.Println(n, "written to", fp.Name())

    if err != nil {
        return nil, err
    } else {
        return fp, nil
    }
}

func main() {
    // runtime.GOMAXPROCS(runtime.NumCPU())

    src := []string{"tmp/vid480.mp4", "tmp/vid481.mp4", "tmp/vid482.mp4", "tmp/vid483.mp4", "tmp/vid484.mp4", "tmp/vid485.mp4"}

    wg := new(sync.WaitGroup)

    for _, url := range src {
        wg.Add(1)
        go func(url string) {
            fp, err := Grab(url, wg)
            log.Println(fp, err)
        }(url)
    }

    wg.Wait()
}

@3d0c 3d0c closed this as completed Nov 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants