libmtp 1.1.23

tracks.c
#include "common.h"
#include <stdlib.h>
static void dump_trackinfo(LIBMTP_track_t *track)
{
printf("Track ID: %u\n", track->item_id);
if (track->title != NULL)
printf(" Title: %s\n", track->title);
if (track->artist != NULL)
printf(" Artist: %s\n", track->artist);
if (track->genre != NULL)
printf(" Genre: %s\n", track->genre);
if (track->composer != NULL)
printf(" Composer: %s\n", track->composer);
if (track->album != NULL)
printf(" Album: %s\n", track->album);
if (track->date != NULL)
printf(" Date: %s\n", track->date);
if (track->filename != NULL)
printf(" Origfilename: %s\n", track->filename);
printf(" Track number: %d\n", track->tracknumber);
printf(" Duration: %d milliseconds\n", track->duration);
#ifdef __WIN32__
printf(" File size %I64u bytes\n", track->filesize);
#else
printf(" File size %llu bytes\n", (long long unsigned int) track->filesize);
#endif
printf(" Filetype: %s\n", LIBMTP_Get_Filetype_Description(track->filetype));
if (track->samplerate != 0) {
printf(" Sample rate: %u Hz\n", track->samplerate);
}
if (track->nochannels != 0) {
printf(" Number of channels: %u\n", track->nochannels);
}
if (track->wavecodec != 0) {
printf(" WAVE fourCC code: 0x%08X\n", track->wavecodec);
}
if (track->bitrate != 0) {
printf(" Bitrate: %u bits/s\n", track->bitrate);
}
if (track->bitratetype != 0) {
if (track->bitratetype == 1) {
printf(" Bitrate type: Constant\n");
} else if (track->bitratetype == 2) {
printf(" Bitrate type: Variable (VBR)\n");
} else if (track->bitratetype == 3) {
printf(" Bitrate type: Free\n");
} else {
printf(" Bitrate type: Unknown/Erroneous value\n");
}
}
if (track->rating != 0) {
printf(" User rating: %u (out of 100)\n", track->rating);
}
if (track->usecount != 0) {
printf(" Use count: %u times\n", track->usecount);
}
}
static void
dump_tracks(LIBMTP_mtpdevice_t *device, uint32_t storageid, int leaf)
{
LIBMTP_file_t *files;
/* Get track listing. */
files = LIBMTP_Get_Files_And_Folders(device,
storageid,
leaf);
if (files == NULL) {
LIBMTP_Dump_Errorstack(device);
LIBMTP_Clear_Errorstack(device);
} else {
LIBMTP_file_t *file, *tmp;
file = files;
while (file != NULL) {
/* Please don't print these */
if (file->filetype == LIBMTP_FILETYPE_FOLDER) {
dump_tracks(device, storageid, file->item_id);
} else if (LIBMTP_FILETYPE_IS_TRACK(file->filetype)) {
LIBMTP_track_t *track;
track = LIBMTP_Get_Trackmetadata(device, file->item_id);
dump_trackinfo(track);
LIBMTP_destroy_track_t(track);
}
tmp = file;
file = file->next;
LIBMTP_destroy_file_t(tmp);
}
}
}
int main (int argc, char **argv)
{
LIBMTP_raw_device_t *rawdevices;
int numrawdevices;
LIBMTP_error_number_t err;
int i;
LIBMTP_Init();
fprintf(stdout, "Attempting to connect device(s)\n");
err = LIBMTP_Detect_Raw_Devices(&rawdevices, &numrawdevices);
switch(err)
{
case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
fprintf(stdout, "mtp-tracks: No Devices have been found\n");
return 0;
case LIBMTP_ERROR_CONNECTING:
fprintf(stderr, "mtp-tracks: There has been an error connecting. Exit\n");
return 1;
case LIBMTP_ERROR_MEMORY_ALLOCATION:
fprintf(stderr, "mtp-tracks: Memory Allocation Error. Exit\n");
return 1;
/* Unknown general errors - This should never execute */
case LIBMTP_ERROR_GENERAL:
default:
fprintf(stderr, "mtp-tracks: Unknown error, please report "
"this to the libmtp developers\n");
return 1;
/* Successfully connected at least one device, so continue */
case LIBMTP_ERROR_NONE:
fprintf(stdout, "mtp-tracks: Successfully connected\n");
fflush(stdout);
break;
}
/* Iterate through connected MTP devices */
for (i = 0; i < numrawdevices; i++) {
LIBMTP_mtpdevice_t *device;
LIBMTP_devicestorage_t *storage;
char *friendlyname;
device = LIBMTP_Open_Raw_Device_Uncached(&rawdevices[i]);
if (device == NULL) {
fprintf(stderr, "Unable to open raw device %d\n", i);
continue;
}
/* Echo the friendly name so we know which device we are working with */
friendlyname = LIBMTP_Get_Friendlyname(device);
if (friendlyname == NULL) {
printf("Friendly name: (NULL)\n");
} else {
printf("Friendly name: %s\n", friendlyname);
LIBMTP_FreeMemory(friendlyname);
}
LIBMTP_Dump_Errorstack(device);
LIBMTP_Clear_Errorstack(device);
/* Loop over storages */
for (storage = device->storage; storage != 0; storage = storage->next) {
dump_tracks(device, storage->id, LIBMTP_FILES_AND_FOLDERS_ROOT);
}
LIBMTP_Release_Device(device);
}
printf("OK.\n");
exit (0);
}
LIBMTP_FreeMemory
void LIBMTP_FreeMemory(void *mem)
Definition libmtp.c:9472
LIBMTP_Open_Raw_Device_Uncached
LIBMTP_mtpdevice_t * LIBMTP_Open_Raw_Device_Uncached(LIBMTP_raw_device_t *rawdevice)
Definition libmtp.c:1921
LIBMTP_Release_Device
void LIBMTP_Release_Device(LIBMTP_mtpdevice_t *device)
Definition libmtp.c:2513
LIBMTP_Dump_Errorstack
void LIBMTP_Dump_Errorstack(LIBMTP_mtpdevice_t *device)
Definition libmtp.c:2659
LIBMTP_Clear_Errorstack
void LIBMTP_Clear_Errorstack(LIBMTP_mtpdevice_t *device)
Definition libmtp.c:2632
LIBMTP_Get_Friendlyname
char * LIBMTP_Get_Friendlyname(LIBMTP_mtpdevice_t *device)
Definition libmtp.c:3745
LIBMTP_Detect_Raw_Devices
LIBMTP_error_number_t LIBMTP_Detect_Raw_Devices(LIBMTP_raw_device_t **, int *)
Definition libopenusb1-glue.c:592
LIBMTP_destroy_file_t
void LIBMTP_destroy_file_t(LIBMTP_file_t *file)
Definition libmtp.c:4323
LIBMTP_Get_Files_And_Folders
LIBMTP_file_t * LIBMTP_Get_Files_And_Folders(LIBMTP_mtpdevice_t *device, uint32_t const storage, uint32_t const parent)
Definition libmtp.c:4593
LIBMTP_Get_Filetype_Description
char const * LIBMTP_Get_Filetype_Description(LIBMTP_filetype_t intype)
Definition libmtp.c:807
LIBMTP_Init
void LIBMTP_Init(void)
Definition libmtp.c:774
LIBMTP_Get_Trackmetadata
LIBMTP_track_t * LIBMTP_Get_Trackmetadata(LIBMTP_mtpdevice_t *device, uint32_t const trackid)
Definition libmtp.c:5196
LIBMTP_destroy_track_t
void LIBMTP_destroy_track_t(LIBMTP_track_t *track)
Definition libmtp.c:4773
LIBMTP_FILETYPE_IS_TRACK
#define LIBMTP_FILETYPE_IS_TRACK(a)
Definition libmtp.h:183
LIBMTP_track_t
struct LIBMTP_track_struct LIBMTP_track_t
Definition libmtp.h:466
LIBMTP_error_number_t
LIBMTP_error_number_t
Definition libmtp.h:447
LIBMTP_file_t
struct LIBMTP_file_struct LIBMTP_file_t
Definition libmtp.h:465
LIBMTP_devicestorage_t
struct LIBMTP_devicestorage_struct LIBMTP_devicestorage_t
Definition libmtp.h:471
LIBMTP_mtpdevice_t
struct LIBMTP_mtpdevice_struct LIBMTP_mtpdevice_t
Definition libmtp.h:464
LIBMTP_raw_device_t
struct LIBMTP_raw_device_struct LIBMTP_raw_device_t
Definition libmtp.h:460
LIBMTP_devicestorage_struct::next
LIBMTP_devicestorage_t * next
Definition libmtp.h:800
LIBMTP_devicestorage_struct::id
uint32_t id
Definition libmtp.h:791
LIBMTP_file_struct::next
LIBMTP_file_t * next
Definition libmtp.h:702
LIBMTP_file_struct::item_id
uint32_t item_id
Definition libmtp.h:695
LIBMTP_file_struct::filetype
LIBMTP_filetype_t filetype
Definition libmtp.h:701
LIBMTP_mtpdevice_struct::storage
LIBMTP_devicestorage_t * storage
Definition libmtp.h:656
LIBMTP_track_struct::title
char * title
Definition libmtp.h:712
LIBMTP_track_struct::genre
char * genre
Definition libmtp.h:715
LIBMTP_track_struct::composer
char * composer
Definition libmtp.h:714
LIBMTP_track_struct::samplerate
uint32_t samplerate
Definition libmtp.h:721
LIBMTP_track_struct::bitratetype
uint16_t bitratetype
Definition libmtp.h:725
LIBMTP_track_struct::date
char * date
Definition libmtp.h:717
LIBMTP_track_struct::filesize
uint64_t filesize
Definition libmtp.h:728
LIBMTP_track_struct::nochannels
uint16_t nochannels
Definition libmtp.h:722
LIBMTP_track_struct::item_id
uint32_t item_id
Definition libmtp.h:709
LIBMTP_track_struct::usecount
uint32_t usecount
Definition libmtp.h:727
LIBMTP_track_struct::tracknumber
uint16_t tracknumber
Definition libmtp.h:719
LIBMTP_track_struct::filename
char * filename
Definition libmtp.h:718
LIBMTP_track_struct::album
char * album
Definition libmtp.h:716
LIBMTP_track_struct::duration
uint32_t duration
Definition libmtp.h:720
LIBMTP_track_struct::bitrate
uint32_t bitrate
Definition libmtp.h:724
LIBMTP_track_struct::rating
uint16_t rating
Definition libmtp.h:726
LIBMTP_track_struct::wavecodec
uint32_t wavecodec
Definition libmtp.h:723
LIBMTP_track_struct::filetype
LIBMTP_filetype_t filetype
Definition libmtp.h:730
LIBMTP_track_struct::artist
char * artist
Definition libmtp.h:713