-
Notifications
You must be signed in to change notification settings - Fork 1
/
RaspiPreview.c
281 lines (232 loc) · 8.04 KB
/
RaspiPreview.c
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
Copyright (c) 2013, Broadcom Europe Ltd
Copyright (c) 2013, James Hughes
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include "interface/vcos/vcos.h"
#include "interface/mmal/mmal.h"
#include "interface/mmal/mmal_logging.h"
#include "interface/mmal/mmal_buffer.h"
#include "interface/mmal/util/mmal_util.h"
#include "interface/mmal/util/mmal_util_params.h"
#include "interface/mmal/util/mmal_default_components.h"
#include "interface/mmal/util/mmal_connection.h"
#include "RaspiPreview.h"
#include "RaspiCLI.h"
#define CommandPreview 1
#define CommandFullScreen 2
#define CommandOpacity 3
#define CommandDisablePreview 4
static COMMAND_LIST cmdline_commands[] =
{
{ CommandPreview, "-preview", "p", "Preview window settings <'x,y,w,h'>", 1 },
{ CommandFullScreen, "-fullscreen", "f", "Fullscreen preview mode", 0 },
{ CommandOpacity, "-opacity", "op", "Preview window opacity (0-255)", 1},
{ CommandDisablePreview,"-nopreview", "n", "Do not display a preview window", 0},
};
static int cmdline_commands_size = sizeof(cmdline_commands) / sizeof(cmdline_commands[0]);
/**
* Create the preview component, set up its ports
*
* @param state Pointer to state control struct
*
* @return MMAL_SUCCESS if all OK, something else otherwise
*
*/
MMAL_STATUS_T raspipreview_create(RASPIPREVIEW_PARAMETERS *state)
{
MMAL_COMPONENT_T *preview = 0;
MMAL_PORT_T *preview_port = NULL;
MMAL_STATUS_T status;
if (!state->wantPreview)
{
// No preview required, so create a null sink component to take its place
status = mmal_component_create("vc.null_sink", &preview);
if (status != MMAL_SUCCESS)
{
vcos_log_error("Unable to create null sink component");
goto error;
}
}
else
{
status = mmal_component_create(MMAL_COMPONENT_DEFAULT_VIDEO_RENDERER,
&preview);
if (status != MMAL_SUCCESS)
{
vcos_log_error("Unable to create preview component");
goto error;
}
if (!preview->input_num)
{
status = MMAL_ENOSYS;
vcos_log_error("No input ports found on component");
goto error;
}
preview_port = preview->input[0];
MMAL_DISPLAYREGION_T param;
param.hdr.id = MMAL_PARAMETER_DISPLAYREGION;
param.hdr.size = sizeof(MMAL_DISPLAYREGION_T);
param.set = MMAL_DISPLAY_SET_LAYER;
param.layer = PREVIEW_LAYER;
param.set |= MMAL_DISPLAY_SET_ALPHA;
param.alpha = state->opacity;
if (state->wantFullScreenPreview)
{
param.set |= MMAL_DISPLAY_SET_FULLSCREEN;
param.fullscreen = 1;
}
else
{
param.set |= (MMAL_DISPLAY_SET_DEST_RECT | MMAL_DISPLAY_SET_FULLSCREEN);
param.fullscreen = 0;
param.dest_rect = state->previewWindow;
}
status = mmal_port_parameter_set(preview_port, ¶m.hdr);
if (status != MMAL_SUCCESS && status != MMAL_ENOSYS)
{
vcos_log_error("unable to set preview port parameters (%u)", status);
goto error;
}
}
/* Enable component */
status = mmal_component_enable(preview);
if (status != MMAL_SUCCESS)
{
vcos_log_error("Unable to enable preview/null sink component (%u)", status);
goto error;
}
state->preview_component = preview;
return status;
error:
if (preview)
mmal_component_destroy(preview);
return status;
}
/**
* Destroy the preview component
*
* @param state Pointer to state control struct
*
*/
void raspipreview_destroy(RASPIPREVIEW_PARAMETERS *state)
{
if (state->preview_component)
{
mmal_component_destroy(state->preview_component);
state->preview_component = NULL;
}
}
/**
* Assign set of default parameters to the passed in parameter block
*
* @param state Pointer to parameter block
*
*/
void raspipreview_set_defaults(RASPIPREVIEW_PARAMETERS *state)
{
state->wantPreview = 1;
state->wantFullScreenPreview = 1;
state->opacity = 255;
state->previewWindow.x = 0;
state->previewWindow.y = 0;
state->previewWindow.width = 1024;
state->previewWindow.height = 768;
state->preview_component = NULL;
}
/**
* Dump parameters as human readable to stdout
*
* @param state Pointer to parameter block
*
*/
void raspipreview_dump_parameters(RASPIPREVIEW_PARAMETERS *state)
{
fprintf(stderr, "Preview %s, Full screen %s\n", state->wantPreview ? "Yes" : "No",
state->wantFullScreenPreview ? "Yes" : "No");
fprintf(stderr, "Preview window %d,%d,%d,%d\nOpacity %d\n", state->previewWindow.x,
state->previewWindow.y, state->previewWindow.width,
state->previewWindow.height, state->opacity);
};
/**
* Parse a possible command pair - command and parameter
* @param arg1 Command
* @param arg2 Parameter (could be NULL)
* @return How many parameters were used, 0,1,2
*/
int raspipreview_parse_cmdline(RASPIPREVIEW_PARAMETERS *params, const char *arg1, const char *arg2)
{
int command_id, used = 0, num_parameters;
if (!arg1)
return 0;
command_id = raspicli_get_command_id(cmdline_commands, cmdline_commands_size, arg1, &num_parameters);
// If invalid command, or we are missing a parameter, drop out
if (command_id==-1 || (command_id != -1 && num_parameters > 0 && arg2 == NULL))
return 0;
switch (command_id)
{
case CommandPreview: // Preview window
{
int tmp;
params->wantPreview = 1;
tmp = sscanf(arg2, "%d,%d,%d,%d",
¶ms->previewWindow.x, ¶ms->previewWindow.y,
¶ms->previewWindow.width, ¶ms->previewWindow.height);
// Failed to get any window parameters, so revert to full screen
if (tmp == 0)
params->wantFullScreenPreview = 1;
else
params->wantFullScreenPreview = 0;
used = 2;
break;
}
case CommandFullScreen: // Want full screen preview mode (overrides display rect)
params->wantPreview = 1;
params->wantFullScreenPreview = 1;
used = 1;
break;
case CommandOpacity: // Define preview window opacity
if (sscanf(arg2, "%u", ¶ms->opacity) != 1)
params->opacity = 255;
else
used = 2;
break;
case CommandDisablePreview: // Turn off preview output
params->wantPreview = 0;
used = 1;
break;
}
return used;
}
/**
* Display help for command line options
*/
void raspipreview_display_help()
{
fprintf(stderr, "\nPreview parameter commands\n\n");
raspicli_display_help(cmdline_commands, cmdline_commands_size);
}