File: plaympeg.c

package info (click to toggle)
smpeg 0.3.3-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,628 kB
  • ctags: 1,202
  • sloc: cpp: 12,775; sh: 8,115; ansic: 1,317; makefile: 139
file content (189 lines) | stat: -rw-r--r-- 5,791 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
   plaympeg - Sample MPEG player using the SMPEG library
   Copyright (C) 1999 Loki Entertainment Software
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <stdlib.h>
#include <string.h>

#include "smpeg.h"

void usage(char *argv0)
{
    printf(
"Usage: %s [options] file ...\n"
"Where the options are one of:\n"
"	--noaudio	Don't play audio stream\n"
"	--novideo	Don't play video stream\n"
"	--fullscreen	Play MPEG in fullscreen mode\n"
"	--double or -2	Play MPEG at double size\n"
"	--loop or -l	Play MPEG over and over\n"
"	--volume N or -v N Set audio volume to N (0-100)\n", argv0);
}

void update(SDL_Surface *screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
{
    if ( screen->flags & SDL_DOUBLEBUF ) {
        SDL_Flip(screen);
    } else {
        SDL_UpdateRect(screen, x, y, w, h);
    }
}

int main(int argc, char *argv[])
{
    int use_audio, use_video;
    int fullscreen;
    int doublesize;
    int loop_play;
    int i, done;
    int volume;
    SMPEG *mpeg;
    SMPEG_Info info;
    char *basefile;

    /* Initialize SDL */
    if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) {
        fprintf(stderr, "Couldn't init SDL: %s\n", SDL_GetError());
        exit(1);
    }

    /* Get the command line options */
    use_audio = 1;
    use_video = 1;
    fullscreen = 0;
    doublesize = 0;
    loop_play = 0;
    volume = 100;
    for ( i=1; argv[i] && (argv[i][0] == '-'); ++i ) {
        if ( strcmp(argv[i], "--noaudio") == 0 ) {
            use_audio = 0;
        } else
        if ( strcmp(argv[i], "--novideo") == 0 ) {
            use_video = 0;
        } else
        if ( strcmp(argv[i], "--fullscreen") == 0 ) {
            fullscreen = 1;
        } else
        if ((strcmp(argv[i], "--double") == 0)||(strcmp(argv[i], "-2") == 0)) {
            doublesize = 1;
        } else
        if ((strcmp(argv[i], "--loop") == 0) || (strcmp(argv[i], "-l") == 0)) {
            loop_play = 1;
        } else
        if ((strcmp(argv[i], "--volume") == 0)||(strcmp(argv[i], "-v") == 0)) {
            ++i;
            if ( argv[i] ) {
                volume = atoi(argv[i]);
            }
        } else
        if ((strcmp(argv[i], "--help") == 0) || (strcmp(argv[i], "-h") == 0)) {
            usage(argv[0]);
            exit(0);
        } else {
            fprintf(stderr, "Warning: Unknown option: %s\n", argv[i]);
        }
    }
    /* If there were no arguments just print the usage */
    if (argc == 1) {
        usage(argv[0]);
        exit(0);
     }

    /* Play the mpeg files! */
    for ( ; argv[i]; ++i ) {
        /* Create the MPEG stream */
        mpeg = SMPEG_new(argv[i], &info, 1);
        if ( SMPEG_error(mpeg) ) {
            fprintf(stderr, "%s: %s\n", argv[i], SMPEG_error(mpeg));
            SMPEG_delete(mpeg);
            continue;
        }
        SMPEG_enableaudio(mpeg, use_audio);
        SMPEG_enablevideo(mpeg, use_video);
        SMPEG_setvolume(mpeg, volume);

        /* Print information about the video */
        basefile = strrchr(argv[i], '/');
        if ( basefile ) {
            ++basefile;
        } else {
            basefile = argv[i];
        }
        if ( info.has_audio && info.has_video ) {
            printf("%s: MPEG system stream (audio/video)\n", basefile);
        } else if ( info.has_audio ) {
            printf("%s: MPEG audio stream\n", basefile);
        } else if ( info.has_video ) {
            printf("%s: MPEG video stream\n", basefile);
        }
        if ( info.has_video ) {
            printf("\tVideo %dx%d resolution\n", info.width, info.height);
        }

        /* Set up video display if needed */
        if ( info.has_video && use_video ) {
            SDL_Surface *screen;
            Uint32 video_flags;

            if ( doublesize ) {
                SMPEG_double(mpeg, 1);
                info.width *= 2;
                info.height *= 2;
            }
            video_flags = SDL_SWSURFACE;
            if ( fullscreen ) {
                video_flags = SDL_FULLSCREEN|SDL_DOUBLEBUF|SDL_HWSURFACE;
                SDL_ShowCursor(0);
            }
            screen = SDL_SetVideoMode(info.width, info.height, 0, video_flags);
            SMPEG_setdisplay(mpeg, screen, NULL, update);
        }

        /* Set any special playback parameters */
        if ( loop_play ) {
            SMPEG_loop(mpeg, 1);
        }

        /* Play it, and wait for playback to complete */
        SMPEG_play(mpeg);
        done = 0;
        while ( ! done && (SMPEG_status(mpeg) == SMPEG_PLAYING) ) {
            SDL_Event event;

            while ( SDL_PollEvent(&event) ) {
                switch (event.type) {
                    case SDL_KEYDOWN:
                        if ( event.key.keysym.sym == SDLK_ESCAPE ) {
                            done = 1;
                        }
                        break;
                    case SDL_QUIT:
                        done = 1;
                        break;
                    default:
                        break;
                }
            }
            SDL_Delay(1000/2);
        }
        SMPEG_delete(mpeg);
    }
    SDL_Quit();

    exit(0);
}