forked from tasanakorn/rpi-mmal-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
124 lines (99 loc) · 3.76 KB
/
main.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
/* Modified: by ckevar on Dec 27, 2019, 12:26 PM
*
* File: main.c
* Author: tas
*
* Created on May 22, 2013, 1:52 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include "bcm_host.h"
#include "interface/vcos/vcos.h"
#include "interface/mmal/mmal.h"
#include "interface/mmal/util/mmal_default_components.h"
#include "interface/mmal/util/mmal_connection.h"
#define MMAL_CAMERA_PREVIEW_PORT 0
#define MMAL_CAMERA_VIDEO_PORT 1
#define MMAL_CAMERA_CAPTURE_PORT 2
int main(int argc, char** argv) {
MMAL_COMPONENT_T *camera = 0;
MMAL_COMPONENT_T *preview = 0;
MMAL_ES_FORMAT_T *format;
MMAL_STATUS_T status;
MMAL_PORT_T *camera_preview_port = NULL, *camera_video_port = NULL, *camera_still_port = NULL;
MMAL_PORT_T *preview_input_port = NULL;
MMAL_CONNECTION_T *camera_preview_connection = 0;
printf("Running...\n");
bcm_host_init();
status = mmal_component_create(MMAL_COMPONENT_DEFAULT_CAMERA, &camera);
if (status != MMAL_SUCCESS) {
printf("Error: create camera %x\n", status);
return -1;
}
camera_preview_port = camera->output[MMAL_CAMERA_PREVIEW_PORT];
camera_video_port = camera->output[MMAL_CAMERA_VIDEO_PORT];
camera_still_port = camera->output[MMAL_CAMERA_CAPTURE_PORT];
{
MMAL_PARAMETER_CAMERA_CONFIG_T cam_config = {
{ MMAL_PARAMETER_CAMERA_CONFIG, sizeof (cam_config)},
.max_stills_w = 1280,
.max_stills_h = 720,
.stills_yuv422 = 0,
.one_shot_stills = 1,
.max_preview_video_w = 1280,
.max_preview_video_h = 720,
.num_preview_video_frames = 3,
.stills_capture_circular_buffer_height = 0,
.fast_preview_resume = 0,
.use_stc_timestamp = MMAL_PARAM_TIMESTAMP_MODE_RESET_STC
};
mmal_port_parameter_set(camera->control, &cam_config.hdr);
}
format = camera_preview_port->format;
format->encoding = MMAL_ENCODING_OPAQUE;
format->encoding_variant = MMAL_ENCODING_I420;
format->es->video.width = 1280;
format->es->video.height = 720;
format->es->video.crop.x = 0;
format->es->video.crop.y = 0;
format->es->video.crop.width = 1280;
format->es->video.crop.height = 720;
status = mmal_port_format_commit(camera_preview_port);
if (status != MMAL_SUCCESS) {
printf("Error: camera viewfinder format couldn't be set\n");
return -1;
}
status = mmal_component_enable(camera);
status = mmal_component_create(MMAL_COMPONENT_DEFAULT_VIDEO_RENDERER, &preview);
if (status != MMAL_SUCCESS) {
printf("Error: unable to create preview (%u)\n", status);
return -1;
}
preview_input_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 = 0;
param.set |= MMAL_DISPLAY_SET_FULLSCREEN;
param.fullscreen = 1;
status = mmal_port_parameter_set(preview_input_port, ¶m.hdr);
if (status != MMAL_SUCCESS && status != MMAL_ENOSYS) {
printf("Error: unable to set preview port parameters (%u)\n", status);
return -1;
}
}
status = mmal_connection_create(&camera_preview_connection, camera_preview_port, preview_input_port, MMAL_CONNECTION_FLAG_TUNNELLING | MMAL_CONNECTION_FLAG_ALLOCATION_ON_INPUT);
if (status != MMAL_SUCCESS) {
printf("Error: unable to create connection (%u)\n", status);
return -1;
}
status = mmal_connection_enable(camera_preview_connection);
if (status != MMAL_SUCCESS) {
printf("Error: unable to enable connection (%u)\n", status);
return -1;
}
while (1);
return 0;
}