dynamic_mp3.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 01 Jun 2013 19:52:15 -0700
changeset 625 1d489d8ec2e0
parent 617 87116a42526e
child 629 87c536d57d92
permissions -rw-r--r--
SDL_LoadMUS_RW() now takes an argument telling whether or not the data source should be freed when done.
Fixes crashes cleaning up the data source for music when loading fails.
slouken@314
     1
/*
slouken@518
     2
  SDL_mixer:  An audio mixer library based on the SDL library
slouken@601
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@314
     4
slouken@518
     5
  This software is provided 'as-is', without any express or implied
slouken@518
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@518
     7
  arising from the use of this software.
slouken@314
     8
slouken@518
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@518
    10
  including commercial applications, and to alter it and redistribute it
slouken@518
    11
  freely, subject to the following restrictions:
slouken@314
    12
slouken@518
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@518
    14
     claim that you wrote the original software. If you use this software
slouken@518
    15
     in a product, an acknowledgment in the product documentation would be
slouken@518
    16
     appreciated but is not required.
slouken@518
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@518
    18
     misrepresented as being the original software.
slouken@518
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@314
    20
*/
slouken@314
    21
slouken@314
    22
#ifdef MP3_MUSIC
slouken@314
    23
slouken@314
    24
#include "SDL_loadso.h"
slouken@314
    25
slouken@314
    26
#include "dynamic_mp3.h"
slouken@314
    27
slouken@314
    28
smpeg_loader smpeg = {
slouken@617
    29
    0, NULL
slouken@314
    30
};
slouken@314
    31
slouken@314
    32
#ifdef MP3_DYNAMIC
slouken@314
    33
int Mix_InitMP3()
slouken@314
    34
{
slouken@617
    35
    if ( smpeg.loaded == 0 ) {
slouken@617
    36
        smpeg.handle = SDL_LoadObject(MP3_DYNAMIC);
slouken@617
    37
        if ( smpeg.handle == NULL ) {
slouken@617
    38
            return -1;
slouken@617
    39
        }
slouken@617
    40
        smpeg.SMPEG_actualSpec =
slouken@617
    41
            (void (*)( SMPEG *, SDL_AudioSpec * ))
slouken@617
    42
            SDL_LoadFunction(smpeg.handle, "SMPEG_actualSpec");
slouken@617
    43
        if ( smpeg.SMPEG_actualSpec == NULL ) {
slouken@617
    44
            SDL_UnloadObject(smpeg.handle);
slouken@617
    45
            return -1;
slouken@617
    46
        }
slouken@617
    47
        smpeg.SMPEG_delete =
slouken@617
    48
            (void (*)( SMPEG* ))
slouken@617
    49
            SDL_LoadFunction(smpeg.handle, "SMPEG_delete");
slouken@617
    50
        if ( smpeg.SMPEG_delete == NULL ) {
slouken@617
    51
            SDL_UnloadObject(smpeg.handle);
slouken@617
    52
            return -1;
slouken@617
    53
        }
slouken@617
    54
        smpeg.SMPEG_enableaudio =
slouken@617
    55
            (void (*)( SMPEG*, int ))
slouken@617
    56
            SDL_LoadFunction(smpeg.handle, "SMPEG_enableaudio");
slouken@617
    57
        if ( smpeg.SMPEG_enableaudio == NULL ) {
slouken@617
    58
            SDL_UnloadObject(smpeg.handle);
slouken@617
    59
            return -1;
slouken@617
    60
        }
slouken@617
    61
        smpeg.SMPEG_enablevideo =
slouken@617
    62
            (void (*)( SMPEG*, int ))
slouken@617
    63
            SDL_LoadFunction(smpeg.handle, "SMPEG_enablevideo");
slouken@617
    64
        if ( smpeg.SMPEG_enablevideo == NULL ) {
slouken@617
    65
            SDL_UnloadObject(smpeg.handle);
slouken@617
    66
            return -1;
slouken@617
    67
        }
slouken@617
    68
        smpeg.SMPEG_new_rwops =
slouken@625
    69
            (SMPEG* (*)(SDL_RWops *, SMPEG_Info*, int, int))
slouken@617
    70
            SDL_LoadFunction(smpeg.handle, "SMPEG_new_rwops");
slouken@617
    71
        if ( smpeg.SMPEG_new_rwops == NULL ) {
slouken@617
    72
            SDL_UnloadObject(smpeg.handle);
slouken@617
    73
            return -1;
slouken@617
    74
        }
slouken@617
    75
        smpeg.SMPEG_play =
slouken@617
    76
            (void (*)( SMPEG* ))
slouken@617
    77
            SDL_LoadFunction(smpeg.handle, "SMPEG_play");
slouken@617
    78
        if ( smpeg.SMPEG_play == NULL ) {
slouken@617
    79
            SDL_UnloadObject(smpeg.handle);
slouken@617
    80
            return -1;
slouken@617
    81
        }
slouken@617
    82
        smpeg.SMPEG_playAudio =
slouken@617
    83
            (int (*)( SMPEG *, Uint8 *, int ))
slouken@617
    84
            SDL_LoadFunction(smpeg.handle, "SMPEG_playAudio");
slouken@617
    85
        if ( smpeg.SMPEG_playAudio == NULL ) {
slouken@617
    86
            SDL_UnloadObject(smpeg.handle);
slouken@617
    87
            return -1;
slouken@617
    88
        }
slouken@617
    89
        smpeg.SMPEG_rewind =
slouken@617
    90
            (void (*)( SMPEG* ))
slouken@617
    91
            SDL_LoadFunction(smpeg.handle, "SMPEG_rewind");
slouken@617
    92
        if ( smpeg.SMPEG_rewind == NULL ) {
slouken@617
    93
            SDL_UnloadObject(smpeg.handle);
slouken@617
    94
            return -1;
slouken@617
    95
        }
slouken@617
    96
        smpeg.SMPEG_setvolume =
slouken@617
    97
            (void (*)( SMPEG*, int ))
slouken@617
    98
            SDL_LoadFunction(smpeg.handle, "SMPEG_setvolume");
slouken@617
    99
        if ( smpeg.SMPEG_setvolume == NULL ) {
slouken@617
   100
            SDL_UnloadObject(smpeg.handle);
slouken@617
   101
            return -1;
slouken@617
   102
        }
slouken@617
   103
        smpeg.SMPEG_skip =
slouken@617
   104
            (void (*)( SMPEG*, float ))
slouken@617
   105
            SDL_LoadFunction(smpeg.handle, "SMPEG_skip");
slouken@617
   106
        if ( smpeg.SMPEG_skip == NULL ) {
slouken@617
   107
            SDL_UnloadObject(smpeg.handle);
slouken@617
   108
            return -1;
slouken@617
   109
        }
slouken@617
   110
        smpeg.SMPEG_status =
slouken@617
   111
            (SMPEGstatus (*)( SMPEG* ))
slouken@617
   112
            SDL_LoadFunction(smpeg.handle, "SMPEG_status");
slouken@617
   113
        if ( smpeg.SMPEG_status == NULL ) {
slouken@617
   114
            SDL_UnloadObject(smpeg.handle);
slouken@617
   115
            return -1;
slouken@617
   116
        }
slouken@617
   117
        smpeg.SMPEG_stop =
slouken@617
   118
            (void (*)( SMPEG* ))
slouken@617
   119
            SDL_LoadFunction(smpeg.handle, "SMPEG_stop");
slouken@617
   120
        if ( smpeg.SMPEG_stop == NULL ) {
slouken@617
   121
            SDL_UnloadObject(smpeg.handle);
slouken@617
   122
            return -1;
slouken@617
   123
        }
slouken@617
   124
    }
slouken@617
   125
    ++smpeg.loaded;
slouken@314
   126
slouken@617
   127
    return 0;
slouken@314
   128
}
slouken@314
   129
void Mix_QuitMP3()
slouken@314
   130
{
slouken@617
   131
    if ( smpeg.loaded == 0 ) {
slouken@617
   132
        return;
slouken@617
   133
    }
slouken@617
   134
    if ( smpeg.loaded == 1 ) {
slouken@617
   135
        SDL_UnloadObject(smpeg.handle);
slouken@617
   136
    }
slouken@617
   137
    --smpeg.loaded;
slouken@314
   138
}
slouken@314
   139
#else
slouken@314
   140
int Mix_InitMP3()
slouken@314
   141
{
slouken@617
   142
    if ( smpeg.loaded == 0 ) {
slouken@617
   143
        smpeg.SMPEG_actualSpec = SMPEG_actualSpec;
slouken@617
   144
        smpeg.SMPEG_delete = SMPEG_delete;
slouken@617
   145
        smpeg.SMPEG_enableaudio = SMPEG_enableaudio;
slouken@617
   146
        smpeg.SMPEG_enablevideo = SMPEG_enablevideo;
slouken@617
   147
        smpeg.SMPEG_new_rwops = SMPEG_new_rwops;
slouken@617
   148
        smpeg.SMPEG_play = SMPEG_play;
slouken@617
   149
        smpeg.SMPEG_playAudio = SMPEG_playAudio;
slouken@617
   150
        smpeg.SMPEG_rewind = SMPEG_rewind;
slouken@617
   151
        smpeg.SMPEG_setvolume = SMPEG_setvolume;
slouken@617
   152
        smpeg.SMPEG_skip = SMPEG_skip;
slouken@617
   153
        smpeg.SMPEG_status = SMPEG_status;
slouken@617
   154
        smpeg.SMPEG_stop = SMPEG_stop;
slouken@617
   155
    }
slouken@617
   156
    ++smpeg.loaded;
slouken@314
   157
slouken@617
   158
    return 0;
slouken@314
   159
}
slouken@314
   160
void Mix_QuitMP3()
slouken@314
   161
{
slouken@617
   162
    if ( smpeg.loaded == 0 ) {
slouken@617
   163
        return;
slouken@617
   164
    }
slouken@617
   165
    if ( smpeg.loaded == 1 ) {
slouken@617
   166
    }
slouken@617
   167
    --smpeg.loaded;
slouken@314
   168
}
slouken@314
   169
#endif /* MP3_DYNAMIC */
slouken@314
   170
slouken@314
   171
#endif /* MP3_MUSIC */