-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvlccapture.cpp
130 lines (103 loc) · 3.59 KB
/
vlccapture.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
#include "vlccapture.h"
#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/videoio/videoio.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "vlc/vlc.h"
#include "vlc/libvlc.h"
#include "SDL.h"
#include "SDL_mutex.h"
//#define VIDEO_WIDTH 960
//#define VIDEO_HEIGHT 540
#define VIDEO_WIDTH 1024
#define VIDEO_HEIGHT 576
struct ctx
{
cv::Mat* image;
SDL_mutex* mutex;
uchar* pixels;
};
void* lock(void *data, void**p_pixels)
{
struct ctx *ctx = (struct ctx*)data;
// WaitForSingleObject(ctx->mutex, INFINITE);
SDL_LockMutex(ctx->mutex);
// pixel will be stored on image pixel space
*p_pixels = ctx->pixels;
return NULL;
}
void display(void *data, void *id){
(void) data;
assert(id == NULL);
}
void unlock(void *data, void *id, void *const *p_pixels){
// get back data structure
struct ctx *ctx = (struct ctx*)data;
/* VLC just rendered the video, but we can also render stuff */
cv::Mat img = *ctx->image;
// show rendered image
imshow("test", img);
SDL_UnlockMutex(ctx->mutex);
std::cout << "Image" << img.cols << std::endl;
// ReleaseMutex(ctx->mutex);
}
int lvc_capture(){
// VLC pointers
libvlc_instance_t *vlcInstance;
libvlc_media_player_t *mp;
libvlc_media_t *media;
const char * const vlc_args[] = {
"-I", "dummy", // Don't use any interface
"--ignore-config", // Don't use VLC's config
"--extraintf=logger", // Log anything
"--verbose=2", // Be much more verbose then normal for debugging purpose
// "--no-audio",
};
vlcInstance = libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args);
if ( vlcInstance == NULL ) {
std::cout << "Create Media Stream Error" << std::endl;
return 0;
}
// Read a distant video stream
media = libvlc_media_new_location(vlcInstance, "rtsp://192.168.5.7:554/s2");
// media = libvlc_media_new_location(vlcInstance, "rtsp://192.168.5.5:8554/CH001.sdp");
// Read a local video file
// media = libvlc_media_new_path(vlcInstance, "/home/shiomi/QtProjects/NpDetectio/detected_images/video/camera1.avi");
if ( media == NULL ) {
std::cout << "Media Stream is Null" << std::endl;
return 0;
}
mp = libvlc_media_player_new_from_media(media);
libvlc_media_release(media);
struct ctx* context = ( struct ctx* )malloc( sizeof( *context ) );
// context->mutex = CreateMutex(NULL, false, NULL);
context->mutex = SDL_CreateMutex();
context->image = new cv::Mat(VIDEO_HEIGHT, VIDEO_WIDTH, CV_8UC3);
// context->image = new Mat(VIDEO_HEIGHT, VIDEO_WIDTH, CV_8UC4);
context->pixels = (unsigned char *)context->image->data;
// show blank image
// namedWindow("test");
imshow("test", *context->image);
libvlc_video_set_callbacks(mp, lock, unlock, display, context);
libvlc_video_set_format(mp, "RV24", VIDEO_WIDTH, VIDEO_HEIGHT, VIDEO_WIDTH * 24 / 8); // pitch = width * BitsPerPixel / 8
// libvlc_video_set_format(mp, "RV32", VIDEO_WIDTH, VIDEO_HEIGHT, VIDEO_WIDTH<<2); //
libvlc_media_player_play(mp);
int ii = 0;
int key = 0;
while(key != 27)
{
// ii++;
// if (ii > 5)
// {
// libvlc_media_player_play(mp);
// }
// float fps = libvlc_media_player_get_fps(mp);
// printf("fps:%f\r\n",fps);
key = cv::waitKey(100); // wait 100ms for Esc key
}
libvlc_media_player_stop(mp);
libvlc_media_player_release( mp );
libvlc_release( vlcInstance );
return 0;
}