This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathbehavior_video_generator_full.cpp
127 lines (94 loc) · 3.45 KB
/
behavior_video_generator_full.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
#include "stdafx.h"
#include "sciter-x.h"
#include "sciter-x-behavior.h"
#include "sciter-x-threads.h"
#include "sciter-x-video-api.h"
#include <thread>
namespace sciter
{
/*
BEHAVIOR: video_generated_stream
- provides synthetic video frames.
- this code is here solely for the demo purposes - how
to connect your own video frame stream with the rendering site
COMMENTS:
<video style="behavior:video-generator video" />
SAMPLE:
See: samples/video/video-generator-behavior.htm
*/
struct video_generated_stream_full : public event_handler
{
sciter::om::hasset<sciter::video_destination> rendering_site;
// ctor
video_generated_stream_full() {}
virtual ~video_generated_stream_full() {}
virtual bool subscription(HELEMENT he, UINT& event_groups)
{
event_groups = HANDLE_BEHAVIOR_EVENT; // we only handle VIDEO_BIND_RQ here
return true;
}
virtual void attached(HELEMENT he) {
he = he;
}
virtual void detached(HELEMENT he) { asset_release(); }
virtual bool on_event(HELEMENT he, HELEMENT target, BEHAVIOR_EVENTS type, UINT_PTR reason)
{
if (type != VIDEO_BIND_RQ)
return false;
// we handle only VIDEO_BIND_RQ requests here
//printf("VIDEO_BIND_RQ %d\n",reason);
if (!reason)
return true; // first phase, consume the event to mark as we will provide frames
rendering_site = (sciter::video_destination*) reason;
sciter::om::hasset<sciter::video_destination> fsite;
if (rendering_site->asset_get_interface(VIDEO_DESTINATION_INAME, fsite.target()))
{
std::thread(generation_thread, fsite).detach();
}
return true;
}
static void generation_thread(sciter::om::hasset<sciter::video_destination> rendering_site) {
// simulate video stream
sciter::sync::sleep(100);
const int VIDEO_WIDTH = 800;
const int VIDEO_HEIGHT = 600;
// let's pretend that we have 800*600 video frames
rendering_site->start_streaming(VIDEO_WIDTH, VIDEO_HEIGHT, COLOR_SPACE_RGB32);
unsigned int *frame = new unsigned int[VIDEO_WIDTH*VIDEO_HEIGHT];
unsigned color = rand();
srand((unsigned int)(UINT_PTR)(sciter::video_destination*)rendering_site);
int start = 0;
auto generate_fill_color = [&]() {
color = 0xff000000 |
((unsigned(rand()) & 0xff) << 16) |
((unsigned(rand()) & 0xff) << 8) |
((unsigned(rand()) & 0xff) << 0);
};
generate_fill_color();
while (rendering_site->is_alive())
{
std::this_thread::sleep_for(std::chrono::milliseconds(30)); // simulate 33 FPS rate
++start;
if (start > 80) {
start = 0;
generate_fill_color();
}
for (int n = start; n < VIDEO_WIDTH*VIDEO_HEIGHT; n += 80) {
frame[n] = color;
}
rendering_site->render_frame((const unsigned char*)frame, sizeof(unsigned int) * VIDEO_WIDTH * VIDEO_HEIGHT);
}
delete[] frame;
}
};
struct video_generated_stream_full_factory : public behavior_factory {
video_generated_stream_full_factory() : behavior_factory("video-generator-full") {
}
// the only behavior_factory method:
virtual event_handler* create(HELEMENT he) {
return new video_generated_stream_full();
}
};
// instantiating and attaching it to the global list
video_generated_stream_full_factory video_generated_stream_factory_full_instance;
}