-
Notifications
You must be signed in to change notification settings - Fork 42
/
OpenCVStream.cpp
125 lines (103 loc) · 3.56 KB
/
OpenCVStream.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
/**
* Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd
* Author: Jacob Chen <jacob-chen@iotwrt.com>
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "OpenCVStream.h"
#include "PipeLines.h"
#include <sstream>
#include <sys/ioctl.h>
#include <sys/mman.h>
OpenCVStream::OpenCVStream()
{
is_streaming__ = false;
OpenCVFaceDect* face_dect = new OpenCVFaceDect();
face_dect->Initialize("./blobs/haarcascade_frontalface_alt.xml");
effect_lists.push_back(face_dect);
}
OpenCVStream::~OpenCVStream()
{
if (is_streaming__) {
StreamOFF();
}
}
void OpenCVStream::StreamON()
{
boost::lock_guard<boost::mutex> guard(state_mutex__);
if (is_streaming__) {
return;
}
sink_pipeline__ = new GstAppSinkPipeline();
sink_pipeline__->Initialize(CreateAppSinkPipeline());
sink_pipeline__->set_is_streaming(true);
sink_pipeline__->SetPipelineState(GST_STATE_PLAYING);
src_pipeline__ = new GstAppSrcPipeline();
src_pipeline__->Initialize(CreateAppSrcPipeline());
src_pipeline__->SetPipelineState(GST_STATE_PLAYING);
process_thread__ = boost::thread(&OpenCVStream::Process, this);
is_streaming__ = true;
}
void OpenCVStream::StreamOFF()
{
boost::lock_guard<boost::mutex> guard(state_mutex__);
if (!is_streaming__) {
return;
}
is_streaming__ = false;
process_thread__.join();
src_pipeline__->SetPipelineState(GST_STATE_PAUSED);
sink_pipeline__->SetPipelineState(GST_STATE_PAUSED);
sink_pipeline__->set_is_streaming(false);
delete src_pipeline__;
delete sink_pipeline__;
}
void OpenCVStream::Process()
{
GstSample* sample;
GstMapInfo map;
GstStructure* s;
GstBuffer* buffer;
GstCaps* caps;
GstMemory* mem;
int height, width, size;
int fd;
void* map_data;
while (is_streaming__) {
if (sink_pipeline__->GetIsNewFrameAvailable()) {
sink_pipeline__->GetLatestSample(&sample);
caps = gst_sample_get_caps(sample);
buffer = gst_sample_get_buffer(sample);
s = gst_caps_get_structure(caps, 0);
gst_structure_get_int(s, "height", &height);
gst_structure_get_int(s, "width", &width);
size = gst_buffer_get_size(buffer);
/* Since gstreamer don't support map buffer to write,
* we have to use mmap directly
*/
mem = gst_buffer_peek_memory(buffer, 0);
fd = gst_dmabuf_memory_get_fd(mem);
map_data = mmap64(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
std::list<OpenCVEffect*>::iterator itor = effect_lists.begin();
while (itor != effect_lists.end()) {
/* assume BGR */
(*itor)->Process((void*)map_data, width, height);
itor++;
}
munmap(map_data, size);
/* will auto released */
gst_buffer_ref(buffer);
src_pipeline__->SendBUF(buffer);
sink_pipeline__->ReleaseFrameBuffer();
/* g_print("%s\n", gst_caps_to_string(caps)); */
}
}
}
//gst_sample_ref(&sample);
//gst_sample_unref(&sample);