diff options
author | Keuin <[email protected]> | 2022-09-15 01:36:12 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2022-09-15 02:01:33 +0800 |
commit | 12c856a10cb9c59ae97504ce0fcd9fdb044bdd14 (patch) | |
tree | 70a3b1619c488768a9c2c688ee4632e3551844e2 /bilibili | |
parent | 4e742159af79cf0f6a14da630a362a0344a5a121 (diff) |
Use io.CopyN to utilize zero copy technique.
Diffstat (limited to 'bilibili')
-rw-r--r-- | bilibili/streaming.go | 29 |
1 files changed, 22 insertions, 7 deletions
diff --git a/bilibili/streaming.go b/bilibili/streaming.go index 0bda347..21bca01 100644 --- a/bilibili/streaming.go +++ b/bilibili/streaming.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "github.com/keuin/slbr/common" + "io" "net/http" "os" "strings" @@ -16,8 +17,7 @@ func (b Bilibili) CopyLiveStream( roomId common.RoomId, stream StreamingUrlInfo, out *os.File, - buffer []byte, - readChunkSize int, + bufSize int64, ) (err error) { url := stream.URL if !strings.HasPrefix(url, "https://") && @@ -53,15 +53,30 @@ func (b Bilibili) CopyLiveStream( defer func() { _ = resp.Body.Close() }() b.logger.Info("Copying live stream...") - // blocking copy - n, err := common.CopyToFileWithBuffer(ctx, out, resp.Body, buffer, false, uint(len(buffer)/readChunkSize)) - if err != nil && !errors.Is(err, context.Canceled) { - b.logger.Error("Stream copying was interrupted unexpectedly: %v", err) + var n int64 + + // blocking copy +copyLoop: + for err == nil { + select { + case <-ctx.Done(): + // cancelled + err = ctx.Err() + break copyLoop + default: + var sz int64 + sz, err = io.CopyN(out, resp.Body, bufSize) + n += sz + } } - if err == nil { + if errors.Is(err, context.Canceled) { + b.logger.Info("Stop copying...") + } else if errors.Is(err, io.EOF) { b.logger.Info("The live is ended. (room %v)", roomId) + } else { + b.logger.Error("Stream copying was interrupted unexpectedly: %v", err) } b.logger.Info("Total downloaded: %v", common.PrettyBytes(uint64(n))) |