Debian Codegraph @

buffer.go

raw
1 // Package v4l, a facade to the Video4Linux video capture interface
2 // Copyright (C) 2016 Zoltán Korándi <[email protected]>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 //go:build linux
18 // +build linux
19
20 package v4l
21
22 import "io"
23
24 // A Buffer holds the raw image data of a frame captured from a Device. It
25 // implements io.Reader, io.ByteReader, io.ReaderAt, and io.Seeker. A call to
26 // Capture, Close, or TurnOff on the corresponding Device may cause the contents
27 // of the buffer to go away.
28 type Buffer struct {
29 d *device
30 n uint64
31 pos int
32 seq uint32
33 }
34
35 // Size returns the total number of bytes in the buffer. As long as the data is
36 // available, the return value is constant and unaffected by calls to the
37 // methods of Buffer. If the data is no longer available, it returns 0.
38 func (b *Buffer) Size() int64 {
39 return int64(len(b.source()))
40 }
41
42 // Len returns the number of unread bytes in the buffer. If the data is no
43 // longer available, it returns 0.
44 func (b *Buffer) Len() int {
45 src := b.source()
46 if src == nil {
47 return 0
48 }
49 return len(src) - b.pos
50 }
51
52 // SeqNum returns the sequence number of the frame in the buffer as reported by
53 // the kernel.
54 func (b *Buffer) SeqNum() uint32 {
55 return b.seq
56 }
57
58 // Read reads up to len(dst) bytes into dst, and returns the number of bytes
59 // read, along with any error encountered.
60 func (b *Buffer) Read(dst []byte) (int, error) {
61 src := b.source()
62 if src == nil {
63 return 0, ErrBufferGone
64 }
65 if b.pos == len(src) {
66 return 0, io.EOF
67 }
68 n := copy(dst, src[b.pos:])
69 b.pos += n
70 return n, nil
71 }
72
73 // ReadAt reads up to len(dst) bytes into dst starting at the specified offset,
74 // and returns the number of bytes read, along with any error encountered.
75 // The seek offset is unaffected by ReadAt.
76 func (b *Buffer) ReadAt(dst []byte, offset int64) (int, error) {
77 src := b.source()
78 if src == nil {
79 return 0, ErrBufferGone
80 }
81 if offset < 0 {
82 return 0, Error("negative offset")
83 }
84 if offset >= int64(len(src)) {
85 return 0, io.EOF
86 }
87 n := copy(dst, src[offset:])
88 if n < len(dst) {
89 return n, io.EOF
90 }
91 return n, nil
92 }
93
94 // ReadByte returns the next byte in the buffer.
95 func (b *Buffer) ReadByte() (byte, error) {
96 src := b.source()
97 if src == nil {
98 return 0, ErrBufferGone
99 }
100 if b.pos == len(src) {
101 return 0, io.EOF
102 }
103 x := src[b.pos]
104 b.pos++
105 return x, nil
106 }
107
108 // Seek sets the seek offset.
109 func (b *Buffer) Seek(offset int64, whence int) (int64, error) {
110 src := b.source()
111 if src == nil {
112 return 0, ErrBufferGone
113 }
114 var i int64
115 switch whence {
116 case io.SeekStart:
117 i = offset
118 case io.SeekCurrent:
119 i = int64(b.pos) + offset
120 case io.SeekEnd:
121 i = int64(len(src)) + offset
122 default:
123 return 0, Error("invalid whence")
124 }
125 if i < 0 {
126 return 0, Error("negative offset")
127 }
128 if i > int64(len(src)) {
129 i = int64(len(src))
130 }
131 b.pos = int(i)
132 return i, nil
133 }
134
135 // source returns the underlying byte slice of the buffer, or nil, if it's no
136 // longer available.
137 func (b *Buffer) source() []byte {
138 if b.d.nCaptures != b.n || b.d.bufIndex == noBuffer {
139 return nil
140 }
141 return b.d.buffers[b.d.bufIndex]
142 }