Fix an URB and memory leak

This commit is contained in:
Neale Pickett 2013-09-15 21:42:33 -06:00
parent 857a549618
commit 0f33b2745d
5 changed files with 28 additions and 33 deletions

View File

@ -1,8 +1,9 @@
CFLAGS += -Wall CFLAGS += -Wall
CFLAGS += -Werror CFLAGS += -Werror
TARGETS = hdjd aac123 explore
CFLAGS += -g CFLAGS += -g
all: hdjd aac123 explore all: $(TARGETS)
hdjd: LDFLAGS += $(shell pkg-config --libs libusb-1.0) hdjd: LDFLAGS += $(shell pkg-config --libs libusb-1.0)
hdjd: LDFLAGS += $(shell pkg-config --libs alsa) hdjd: LDFLAGS += $(shell pkg-config --libs alsa)
@ -23,4 +24,4 @@ aactest: LDLIBS += $(shell pkg-config --libs alsa)
aactest: LDLIBS += -lfaad -lmp4v2 aactest: LDLIBS += -lfaad -lmp4v2
clean: clean:
rm -f hdjd *.o rm -f $(TARGETS) *.o

View File

@ -5,25 +5,13 @@
#include <neaacdec.h> #include <neaacdec.h>
#include <mp4v2/mp4v2.h> #include <mp4v2/mp4v2.h>
/* Some things I use for debugging */ #include "dump.h"
#ifdef NODUMP
# define DUMPf(fmt, args...)
#else
# define DUMPf(fmt, args...) fprintf(stderr, "%s:%s:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##args)
#endif
#define DUMP() DUMPf("")
#define DUMP_d(v) DUMPf("%s = %d", #v, v)
#define DUMP_x(v) DUMPf("%s = 0x%x", #v, v)
#define DUMP_s(v) DUMPf("%s = %s", #v, v)
#define DUMP_c(v) DUMPf("%s = '%c' (0x%02x)", #v, v, v)
#define DUMP_p(v) DUMPf("%s = %p", #v, v)
static int static int
GetAACTrack(MP4FileHandle *infile) GetAACTrack(MP4FileHandle *infile)
{ {
/* find AAC track */ /* find AAC track */
int rc;
MP4TrackId numTracks = MP4GetNumberOfTracks(infile, NULL, 0); MP4TrackId numTracks = MP4GetNumberOfTracks(infile, NULL, 0);
MP4TrackId i; MP4TrackId i;
@ -66,10 +54,9 @@ play_file(snd_pcm_t *snd, char *fn)
NeAACDecHandle hDecoder; NeAACDecHandle hDecoder;
NeAACDecConfigurationPtr config; NeAACDecConfigurationPtr config;
mp4AudioSpecificConfig mp4ASC;
unsigned char *buffer; unsigned char *buffer;
int buffer_size; uint32_t buffer_size;
unsigned long samplerate; unsigned long samplerate;
unsigned char channels; unsigned char channels;
@ -124,9 +111,7 @@ play_file(snd_pcm_t *snd, char *fn)
DUMP_d(numSamples); DUMP_d(numSamples);
for (sampleId = 1; sampleId <= numSamples; sampleId++) { for (sampleId = 1; sampleId <= numSamples; sampleId++) {
int rc; int rc;
long dur;
unsigned int sample_count; unsigned int sample_count;
unsigned int delay = 0;
NeAACDecFrameInfo frameInfo; NeAACDecFrameInfo frameInfo;
buffer = NULL; buffer = NULL;

5
alsa.c
View File

@ -82,6 +82,7 @@ alsa_read_ready()
usb_write(buf, converted); usb_write(buf, converted);
} }
} }
snd_midi_event_free(midi_event_parser);
} }
void void
@ -120,10 +121,10 @@ alsa_write(uint8_t *data, size_t datalen)
snd_seq_ev_set_direct(&ev); snd_seq_ev_set_direct(&ev);
snd_seq_ev_set_source(&ev, seq_port); snd_seq_ev_set_source(&ev, seq_port);
snd_seq_ev_set_subs(&ev); snd_seq_ev_set_subs(&ev);
if ((r = snd_seq_event_output(snd_handle, &ev)) < 0) { if ((r = snd_seq_event_output_direct(snd_handle, &ev)) < 0) {
fprintf(stderr, "ALSA couldn't write an event: %ld\n", r); fprintf(stderr, "ALSA couldn't write an event: %ld\n", r);
abort(); abort();
} }
snd_seq_drain_output(snd_handle); snd_midi_event_free(midi_event_parser);
} }

14
dump.h
View File

@ -1,11 +1,5 @@
/* obj.h: objecty and exceptiony stuff #ifndef __DUMP_H__
* #define __DUMP_H__
* Some macros to make C a bit more like C++, but without bringing in
* all of C++'s crapola.
*/
#ifndef __OBJ_H__
#define __OBJ_H__
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -18,8 +12,8 @@
# define DUMPf(fmt, args...) fprintf(stderr, "%s:%s:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##args) # define DUMPf(fmt, args...) fprintf(stderr, "%s:%s:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##args)
#endif #endif
#define DUMP() DUMPf("") #define DUMP() DUMPf("")
#define DUMP_d(v) DUMPf("%s = %d", #v, v) #define DUMP_d(v) DUMPf("%s = %d", #v, (int)v)
#define DUMP_l(v) DUMPf("%s = %ld", #v, v) #define DUMP_l(v) DUMPf("%s = %ld", #v, (long)v)
#define DUMP_x(v) DUMPf("%s = 0x%x", #v, v) #define DUMP_x(v) DUMPf("%s = 0x%x", #v, v)
#define DUMP_s(v) DUMPf("%s = %s", #v, v) #define DUMP_s(v) DUMPf("%s = %s", #v, v)
#define DUMP_c(v) DUMPf("%s = '%c' (0x%02x)", #v, v, v) #define DUMP_c(v) DUMPf("%s = '%c' (0x%02x)", #v, v, v)

16
usb.c
View File

@ -11,6 +11,9 @@
static struct libusb_device_handle *usb_dev; static struct libusb_device_handle *usb_dev;
static const struct device *d; static const struct device *d;
static int writes_pending = 0;
static int reads_pending = 0;
struct device { struct device {
uint16_t product_id; uint16_t product_id;
uint8_t ep_in; uint8_t ep_in;
@ -36,6 +39,8 @@ usb_initiate_transfer()
struct libusb_transfer *xfer = libusb_alloc_transfer(0); struct libusb_transfer *xfer = libusb_alloc_transfer(0);
libusb_fill_bulk_transfer(xfer, usb_dev, d->ep_in, buf, 256, usb_xfer_done, NULL, 0); libusb_fill_bulk_transfer(xfer, usb_dev, d->ep_in, buf, 256, usb_xfer_done, NULL, 0);
libusb_submit_transfer(xfer); libusb_submit_transfer(xfer);
DUMP();
reads_pending += 1;
} }
void void
@ -44,6 +49,9 @@ usb_xfer_done(struct libusb_transfer *xfer)
uint8_t *data = xfer->buffer; uint8_t *data = xfer->buffer;
int datalen = xfer->actual_length; int datalen = xfer->actual_length;
reads_pending -= 1;
DUMP();
alsa_write(data, datalen); alsa_write(data, datalen);
free(data); free(data);
libusb_free_transfer(xfer); libusb_free_transfer(xfer);
@ -93,6 +101,7 @@ usb_setup(char *name, size_t namelen)
printf("Opened [%s]\n", name); printf("Opened [%s]\n", name);
} }
usb_initiate_transfer();
return 0; return 0;
} }
@ -117,7 +126,10 @@ usb_fd_setup(int *nfds, fd_set *rfds, fd_set *wfds)
} }
} }
usb_initiate_transfer(); if (reads_pending + writes_pending > 10) {
fprintf(stderr, "Warning: %d+%d = %d outstanding USB transactions!\n", reads_pending, writes_pending, reads_pending + writes_pending);
}
} }
void void
@ -140,6 +152,7 @@ usb_check_fds(fd_set *rfds, fd_set *wfds)
void void
usb_write_done(struct libusb_transfer *xfer) usb_write_done(struct libusb_transfer *xfer)
{ {
writes_pending -= 1;
free(xfer->buffer); free(xfer->buffer);
libusb_free_transfer(xfer); libusb_free_transfer(xfer);
} }
@ -150,6 +163,7 @@ usb_write(uint8_t *data, size_t datalen)
struct libusb_transfer *xfer; struct libusb_transfer *xfer;
unsigned char *buf; unsigned char *buf;
writes_pending += 1;
xfer = libusb_alloc_transfer(0); xfer = libusb_alloc_transfer(0);
buf = (unsigned char *)malloc(datalen); buf = (unsigned char *)malloc(datalen);
memcpy(buf, data, datalen); memcpy(buf, data, datalen);