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

Fix double free and memory leaks #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/ogg.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,46 @@
typedef void (*AVCallback)(unsigned char *, int);

typedef struct {
ogg_sync_state state;
ogg_sync_state *state;
ogg_page page;
ogg_stream_state stream;
ogg_stream_state *stream;
ogg_packet packet;
} AVOgg;

AVOgg *AVOggInit() {
AVOgg *ogg = calloc(1, sizeof(AVOgg));
assert(!ogg_sync_init(&ogg->state));
ogg->state = calloc(1, sizeof(ogg_sync_state));
ogg->stream = calloc(1, sizeof(ogg_stream_state));
assert(!ogg_sync_init(ogg->state));
assert(ogg_sync_pageout(ogg->state, &ogg->page) != 1);
return ogg;
}

int AVOggRead(AVOgg *ogg, char *buffer, int buflen, AVCallback callback) {
// write buffer into ogg stream
char *oggBuf = ogg_sync_buffer(&ogg->state, buflen);
char *oggBuf = ogg_sync_buffer(ogg->state, buflen);
memcpy(oggBuf, buffer, buflen);
assert(!ogg_sync_wrote(&ogg->state, buflen));
assert(!ogg_sync_wrote(ogg->state, buflen));

// read ogg pages
while (ogg_sync_pageout(&ogg->state, &ogg->page) == 1) {
while (ogg_sync_pageout(ogg->state, &ogg->page) == 1) {
int serial = ogg_page_serialno(&ogg->page);

if (ogg_page_bos(&ogg->page))
assert(!ogg_stream_init(&ogg->stream, serial));
assert(!ogg_stream_init(ogg->stream, serial));

assert(!ogg_stream_pagein(&ogg->stream, &ogg->page));
assert(!ogg_stream_pagein(ogg->stream, &ogg->page));

// read packets
while (ogg_stream_packetout(&ogg->stream, &ogg->packet) == 1)
while (ogg_stream_packetout(ogg->stream, &ogg->packet) == 1)
callback(ogg->packet.packet, ogg->packet.bytes);
}

return 0;
}

void AVOggDestroy(AVOgg *ogg) {
ogg_sync_destroy(&ogg->state);
ogg_sync_destroy(ogg->state);
ogg_stream_destroy(ogg->stream);
free(ogg);
}