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

Support srt benchmark #42

Closed

Conversation

xiaozhihong
Copy link

No description provided.

lastPrint = time.Now()
logger.Tf(ctx, "Stream=%v, consume Video(samples=%v, dts=%v, ts=%.3f) and Audio(samples=%v, dts=%v, ts=%.3f)",
v.stream, v.avcSamples, videoDts, float64(videoDts)/1000.0, v.aacSamples, audioDts, float64(audioDts)/1000.0)
}
Copy link
Member

@winlinvip winlinvip Feb 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这种其实Go很容易搞:

go func() {
  for {
    select {
    case <- ctx.Done():
        return
    case <-time.After(5 * time.Second):
        logger.Tf(ctx, "xxx")
    }
  }
} ()

虽然行数可能会多,但是逻辑独立,也不需要一个lastPrint的变量,比较好理解。


// Run all subscribers or players.
for i := 0; sr != "" && i < streams && ctx.Err() == nil; i++ {
r_auto := sr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go的命名是不用下划线的,要改成 rAuto

v.srtSocket.SetWriteDeadline(time.Now().Add(time.Duration(2 * time.Second)))
if _, srtError = v.srtSocket.Write(tsPacket); srtError != nil {
logger.Ef(ctx, "SRT write failed, err=%v", srtError)
return
Copy link
Member

@winlinvip winlinvip Feb 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这块可能会打印很多错误。

而且它的目的是控制113行的那个Go退出,所以这块应该用context.Context来做控制。

ctx, srtCancel := context.WithParent(ctx)

tsMuxer.OnPacket = func(pkt []byte) {
  if _, err = v.srtSocket.Write(tsPacket); err != nil {
    srtCancel()
  }
}

然后在113行判断时,只需要判断ctx是否退出了:

for ctx.Err() == nil {
}

ctx和err对象的区别是,cancel一个ctx后,依赖这些ctx的所有goroutine会退出,而err对象并不会触发退出,而是需要goroutine对象自己检查。

比如如果有些goroutine底下还调用了其他goroutine,那么ctx能把这些全部取消,也就是除了112行会检查,后续用到ctx的地方都会判断,而err必须得循环到113行检查才行。

@winlinvip
Copy link
Member

winlinvip commented Jul 8, 2024

Partially merged by 848f930

@winlinvip winlinvip closed this Jul 8, 2024
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

Successfully merging this pull request may close these issues.

2 participants