Skip to content

Commit

Permalink
Some more Buffer and Memory Fixes (#7)
Browse files Browse the repository at this point in the history
* fix Buffer.FillBytes to be Windows-safe, make calling Map on Memorys and Buffers safe to do multiple times (with caveats)
  • Loading branch information
tinyzimmer authored Dec 31, 2020
1 parent c045acd commit f1dec81
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 60 deletions.
74 changes: 14 additions & 60 deletions gst/gst_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,66 +367,14 @@ func (b *Buffer) Extract(offset, size int64) []byte {
// FillBytes adds the given byte slice to the buffer at the given offset. The return value reflects the amount
// of data added to the buffer.
func (b *Buffer) FillBytes(offset int64, data []byte) int64 {
str := string(data)
cStr := C.CString(str)
gsize := C.gst_buffer_fill(b.Instance(), C.gsize(offset), (C.gconstpointer)(unsafe.Pointer(cStr)), C.gsize(len(str)))
return int64(gsize)
}

// FillInt8Slice adds the given slice to the buffer at the given offset. The return value reflects the amount
// of data added to the buffer.
func (b *Buffer) FillInt8Slice(offset int64, data []int8) int64 {
gsize := C.gst_buffer_fill(b.Instance(), C.gsize(offset), (C.gconstpointer)(unsafe.Pointer(&data[0])), C.gsize(len(data)))
return int64(gsize)
}

// FillInt16Slice adds the given slice to the buffer at the given offset. The return value reflects the amount
// of data added to the buffer.
func (b *Buffer) FillInt16Slice(offset int64, data []int16) int64 {
gsize := C.gst_buffer_fill(b.Instance(), C.gsize(offset), (C.gconstpointer)(unsafe.Pointer(&data[0])), C.gsize(len(data)))
return int64(gsize)
}

// FillInt32Slice adds the given slice to the buffer at the given offset. The return value reflects the amount
// of data added to the buffer.
func (b *Buffer) FillInt32Slice(offset int64, data []int32) int64 {
gsize := C.gst_buffer_fill(b.Instance(), C.gsize(offset), (C.gconstpointer)(unsafe.Pointer(&data[0])), C.gsize(len(data)))
return int64(gsize)
}

// FillInt64Slice adds the given slice to the buffer at the given offset. The return value reflects the amount
// of data added to the buffer.
func (b *Buffer) FillInt64Slice(offset int64, data []int64) int64 {
gsize := C.gst_buffer_fill(b.Instance(), C.gsize(offset), (C.gconstpointer)(unsafe.Pointer(&data[0])), C.gsize(len(data)))
return int64(gsize)
}

// FillUint8Slice adds the given slice to the buffer at the given offset. The return value reflects the amount
// of data added to the buffer.
func (b *Buffer) FillUint8Slice(offset int64, data []uint8) int64 {
gsize := C.gst_buffer_fill(b.Instance(), C.gsize(offset), (C.gconstpointer)(unsafe.Pointer(&data[0])), C.gsize(len(data)))
return int64(gsize)
}

// FillUint16Slice adds the given slice to the buffer at the given offset. The return value reflects the amount
// of data added to the buffer.
func (b *Buffer) FillUint16Slice(offset int64, data []uint16) int64 {
gsize := C.gst_buffer_fill(b.Instance(), C.gsize(offset), (C.gconstpointer)(unsafe.Pointer(&data[0])), C.gsize(len(data)))
return int64(gsize)
}

// FillUint32Slice adds the given slice to the buffer at the given offset. The return value reflects the amount
// of data added to the buffer.
func (b *Buffer) FillUint32Slice(offset int64, data []uint32) int64 {
gsize := C.gst_buffer_fill(b.Instance(), C.gsize(offset), (C.gconstpointer)(unsafe.Pointer(&data[0])), C.gsize(len(data)))
return int64(gsize)
}

// FillUint64Slice adds the given slice to the buffer at the given offset. The return value reflects the amount
// of data added to the buffer.
func (b *Buffer) FillUint64Slice(offset int64, data []uint64) int64 {
gsize := C.gst_buffer_fill(b.Instance(), C.gsize(offset), (C.gconstpointer)(unsafe.Pointer(&data[0])), C.gsize(len(data)))
return int64(gsize)
gbytes := C.g_bytes_new((C.gconstpointer)(unsafe.Pointer(&data[0])), C.gsize(len(data)))
defer C.g_bytes_unref(gbytes)
var size C.gsize
gdata := C.g_bytes_get_data(gbytes, &size)
if gdata == nil {
return 0
}
return int64(C.gst_buffer_fill(b.Instance(), C.gsize(offset), gdata, size))
}

// FindMemory looks for the memory blocks that span size bytes starting from offset in buffer. Size can be -1
Expand Down Expand Up @@ -616,9 +564,15 @@ func (b *Buffer) IterateMetaFiltered(meta *Meta, apiType glib.Type) *Meta {
}

// Map will map the data inside this buffer. This function can return nil if the memory is not read or writable.
// It is safe to call this function multiple times on a single Buffer, however it will retain the flags
// used when mapping the first time. To change between read and write access first unmap and then remap the
// buffer with the appropriate flags, or map initially with both read/write access.
//
// Unmap the Buffer after usage.
func (b *Buffer) Map(flags MapFlags) *MapInfo {
if b.mapInfo != nil {
return b.mapInfo
}
mapInfo := C.malloc(C.sizeof_GstMapInfo)
C.gst_buffer_map(
(*C.GstBuffer)(b.Instance()),
Expand Down
6 changes: 6 additions & 0 deletions gst/gst_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,15 @@ func (m *Memory) Copy(offset, size int64) *Memory {
}

// Map the data inside the memory. This function can return nil if the memory is not read or writable.
// It is safe to call this function multiple times on the same Memory, however it will retain the flags
// used when mapping the first time. To change between read and write access first unmap and then remap the
// memory with the appropriate flags, or map initially with both read/write access.
//
// Unmap the Memory after usage.
func (m *Memory) Map(flags MapFlags) *MapInfo {
if m.mapInfo != nil {
return m.mapInfo
}
mapInfo := C.malloc(C.sizeof_GstMapInfo)
C.gst_memory_map(
(*C.GstMemory)(m.Instance()),
Expand Down

0 comments on commit f1dec81

Please sign in to comment.