ฉันกำลังเขียนแอปสำหรับแพลตฟอร์ม windows โดยใช้ FFmpeg และเป็น golang wrapper goav แต่ฉันมีปัญหาในการทำความเข้าใจวิธีใช้ตัวชี้ C เพื่อเข้าถึงอาร์เรย์
ฉันกำลังพยายามเก็บสตรีมไว้ในคลาส AVFormatContext เพื่อใช้งานในระหว่างเดินทาง และในที่สุดก็เพิ่มเฟรมให้กับพื้นผิวใน OpenGl เพื่อสร้างเครื่องเล่นวิดีโอที่มีการเปลี่ยนภาพที่ยอดเยี่ยม
ฉันคิดว่าการทำความเข้าใจวิธีการส่งและเข้าถึงข้อมูล C จะทำให้การเขียนโค้ดง่ายขึ้นมาก
ฉันได้แยกส่วนที่เกี่ยวข้องทั้งหมดของโค้ด C, Wrapper และโค้ดของฉันออกตามที่แสดงด้านล่าง:
รหัส C - libavformat/avformat.h
typedef struct AVFormatContext {
unsigned int nb_streams;
AVStream **streams;
}
กระดาษห่อ Golang goav
package avutil
//#cgo pkg-config: libavformat
//#include <libavformat/avformat.h>
import "C"
import (
"unsafe"
)
type Context C.struct_AVFormatContext;
func (ctxt *Context) StreamsGet(i uintptr) *Stream {
streams := (**Stream)(unsafe.Pointer(ctxt.streams));
// I think this is where it's going wrong, I'm brand new to this stuff
return (*Stream)(unsafe.Pointer(uintptr(unsafe.Pointer(streams)) + i*unsafe.Sizeof(*streams)));
}
รหัส Golang ของฉัน
package main
import "github.com/giorgisio/goav/avformat"
func main() {
ctx := &avformat.Context{} // the actual function to initiate this does an mallocz for the streams
stream := ctx.StreamsGet(0)
//do stuff with stream...
}
ใน C ดูเหมือนว่าฉันต้องทำเพียงแค่ streams[i] แต่นั่นจะไม่ได้ผล ดังนั้นฉันจึงเพิ่มฟังก์ชันให้กับ wrapper โดยใช้เทคนิคจากคำถามของฉัน ที่นี่ อย่างไรก็ตาม ฉันไม่ได้รับข้อมูล ดูเหมือนว่าฉันได้รับตัวชี้ไปยังที่ไหนสักแห่งในหน่วยความจำแบบสุ่ม แล้วฉันจะเข้าถึงองค์ประกอบเหล่านี้จาก golang ได้อย่างไร? แหล่งข้อมูลใด ๆ ก็จะเป็นประโยชน์เช่นกัน ฉันจะลงทุนเวลาพอสมควรกับเรื่องนี้