-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cpp
226 lines (183 loc) · 5.81 KB
/
player.cpp
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
GleedMovie Example
It plays a classical Big Buck Bunny trailer in WebM format (bunny.webm)
You can press '0' on the keyboard to restart the movie from the beginning.
*/
#include <iostream>
#include <fstream>
#include <SDL3/SDL.h>
#include <gleed.h>
int main()
{
char file_to_play[32] = {0};
printf("Select the file number you would like to play: \n");
printf("(1) bunny.webm (VP8 video, Vorbis audio)\n");
printf("(2) hl2.webm (VP8 video, Opus audio)\n");
printf("(3) beach.webm (VP9 video only)\n");
printf("(4) ocean.webm (VP9 video, Opus audio)\n");
printf("> ");
int file_sel = 0;
scanf("%d", &file_sel);
if (file_sel == 1)
{
SDL_strlcpy(file_to_play, "bunny.webm", sizeof(file_to_play));
}
else if (file_sel == 2)
{
SDL_strlcpy(file_to_play, "hl2.webm", sizeof(file_to_play));
}
else if (file_sel == 3)
{
SDL_strlcpy(file_to_play, "beach.webm", sizeof(file_to_play));
}
else if (file_sel == 4)
{
SDL_strlcpy(file_to_play, "ocean.webm", sizeof(file_to_play));
}
else
{
printf("Invalid file selection\n");
return 1;
}
/* Standard SDL initialization */
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO))
{
std::cerr << "Failed to initialize SDL: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Window *window = SDL_CreateWindow("Gleed Player Example", 800, 600, 0);
if (!window)
{
std::cerr << "Failed to create window: " << SDL_GetError() << std::endl;
return 1;
}
/*
Open a WebM movie from file
This will parse the WebM file, and pre-select first available video and audio tracks.
*/
GleedMovie *movie = GleedOpen(file_to_play);
if (!movie)
{
/* Any GleedMovie error can be retrieved with GleedGetError() */
std::cerr << GleedGetError() << std::endl;
return 1;
}
int w, h;
GleedGetVideoSize(movie, &w, &h);
/*
Resize the window to the video size, but not larger than 1920x1080
*/
if (w <= 1920)
{
SDL_SetWindowSize(window, w, h);
}
/*
We will use that renderer to draw video frames
*/
SDL_Renderer *renderer = SDL_CreateRenderer(window, nullptr);
if (!renderer)
{
std::cerr << "Failed to create renderer: " << SDL_GetError() << std::endl;
return 1;
}
/*
And this device will serve as an output for movie audio
*/
SDL_AudioDeviceID audio_device = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, NULL);
if (!audio_device)
{
std::cerr << "Failed to open audio device: " << SDL_GetError() << std::endl;
return 1;
}
/*
Create a player instance from a movie
*/
GleedMoviePlayer *player = GleedCreatePlayer(movie);
/*
Set the audio output device for the player
This will automatically bind the audio stream to the device
and player will put audio samples during update
*/
GleedSetPlayerAudioOutput(player, audio_device);
/*
For a better user experience, we will preload the audio stream
*/
GleedPreloadAudioStream(movie);
/*
This texture will hold our video frame data
You can pass your own texture, but it must have same format and it's recommended to use a streaming texture
*/
SDL_Texture *video_frame = GleedCreatePlaybackTexture(
movie, renderer);
GleedSetPlayerVideoOutputTexture(player, video_frame);
bool running = true;
char title[128];
SDL_Event ev;
while (running)
{
while (SDL_PollEvent(&ev))
{
if (ev.type == SDL_EVENT_QUIT)
{
running = false;
break;
}
if (ev.type == SDL_EVENT_KEY_DOWN)
{
if (ev.key.key == SDLK_SPACE)
{
/* Very simple pausing control */
if (GleedIsPlayerPaused(player))
GleedResumePlayer(player);
else
GleedPausePlayer(player);
}
}
}
/*
Update the player, you should call it each application's frame
Second argument expects a delta time, but here we let player decide it
*/
GleedMoviePlayerUpdateResult upd = GleedUpdatePlayer(player, GLEED_PLAYER_TIME_DELTA_AUTO);
/*
Crash on error
*/
if (upd == GLEED_PLAYER_UPDATE_ERROR)
{
std::cerr << "Error updating player: " << GleedGetError() << std::endl;
break;
}
/*
Exit when movie is finished
*/
if (GleedHasPlayerFinished(player))
{
printf("Move finished, duration = %.2f seconds\n", GleedGetPlayerCurrentTimeSeconds(player));
running = false;
}
SDL_RenderClear(renderer);
/*
Render the current video frame
Note that depending on movie frame rate, this texture may not be updated after each player update
*/
SDL_RenderTexture(renderer, video_frame, NULL, NULL);
SDL_RenderPresent(renderer);
/*
Some debug info in the window title
*/
snprintf(title, 128, "GleedPlayer (movie %s, time %.2f)",
file_to_play,
GleedGetPlayerCurrentTimeSeconds(player));
SDL_SetWindowTitle(window, title);
SDL_Delay(8); // 120 fps kinda of
}
/* Don't forget to free movie resources after finishing playback */
GleedFreePlayer(player);
GleedFreeMovie(movie, true);
SDL_DestroyTexture(video_frame);
SDL_CloseAudioDevice(audio_device);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}