forked from mozilla/webrtc-standalone
/
renderSDL2.cpp
144 lines (120 loc) · 3.42 KB
/
renderSDL2.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
#include <string.h>
#include "SDL.h"
#include <stdio.h>
#define LOG(format, ...) fprintf(stderr, format, ##__VA_ARGS__);
static void
quit(int rc)
{
SDL_Quit();
exit(rc);
}
namespace render {
struct State {
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
int width;
int height;
State() : window(0), renderer(0), texture(0), width(0), height(0) {}
};
static State* sState = nullptr;
void
Initialize()
{
if (sState) {
return;
}
sState = new State;
}
void
Shutdown()
{
SDL_DestroyRenderer(sState->renderer);
delete sState;
sState = 0;
}
void
Draw(const unsigned char* aImage, int size, int aWidth, int aHeight)
{
if (!sState) {
return;
}
if (!sState->window) {
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
return;
}
sState->window = SDL_CreateWindow("WebRTC Player",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
aWidth, aHeight,
0);
// SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
if (!sState->window) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s\n", SDL_GetError());
quit(3);
}
sState->renderer = SDL_CreateRenderer(sState->window, -1, 0);
if (!sState->renderer) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError());
quit(4);
}
sState->width = aWidth;
sState->height = aHeight;
}
if (!sState->texture) {
sState->texture = SDL_CreateTexture(sState->renderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING, aWidth, aHeight);
if (!sState->texture) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
quit(5);
}
}
if ((sState->width != aWidth) || (sState->height != aHeight)) {
if (sState->texture) {
SDL_DestroyTexture(sState->texture); sState->texture = 0;
}
sState->texture = SDL_CreateTexture(sState->renderer,
SDL_PIXELFORMAT_IYUV,
SDL_TEXTUREACCESS_STREAMING,
aWidth, aHeight);
SDL_SetWindowSize(sState->window, aWidth, aHeight);
sState->width = aWidth;
sState->height = aHeight;
}
Uint8* dst = nullptr;
void* pixels = nullptr;
int pitch = 0;
if (SDL_LockTexture(sState->texture, NULL, &pixels, &pitch) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't lock texture: %s\n", SDL_GetError());
quit(5);
}
dst = (Uint8*)pixels;
memcpy(dst, aImage, size);
SDL_UnlockTexture(sState->texture);
SDL_RenderClear(sState->renderer);
SDL_RenderCopy(sState->renderer, sState->texture, NULL, NULL);
SDL_RenderPresent(sState->renderer);
}
bool
KeepRunning()
{
bool result = true;
if (sState->window) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE) {
result = false;
}
break;
case SDL_QUIT:
result = false;
break;
}
}
}
return result;
}
} // namespace standalone