-
Notifications
You must be signed in to change notification settings - Fork 1
/
VideoPlayerDraft.hpp
300 lines (241 loc) · 10.1 KB
/
VideoPlayerDraft.hpp
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "yasio/detail/byte_buffer.hpp"
#include "renderer/backend/ProgramCache.h"
#include <deque>
#if defined(_WIN32)
#include "MFMediaPlayer.h"
#include "ntcvt/ntcvt.hpp"
#include <thread>
#endif
#if defined(__ANDROID__)
#include "platform/android/jni/AndroidJavaMediaPlayer.h"
#endif
using namespace cocos2d;
// pal7xuan1.mp4
#define PS_SET_UNIFORM(ps,k,v) ps->setUniform(ps->getUniformLocation(k), &v, sizeof(v))
std::string fragNV12 = R"(
#ifdef GL_ES
varying lowp vec4 v_fragmentColor;
varying mediump vec2 v_texCoord;
#else
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
#endif
uniform sampler2D u_texture; // Y sample
uniform sampler2D u_texture1; // UV sample
void main()
{
// refer to:
// a. https://gist.github.com/crearo/0d50442145b63c6c288d1c1675909990
// b. https://github.com/tqk2811/TqkLibrary.Media.VideoPlayer/blob/38a2dce908215045cc27cffb741a6e4b8492c9cd/TqkLibrary.Media.VideoPlayer.OpenGl/Renders/NV12Render.cs#L14
float cy = v_texCoord.y + 0.01625; // why needs adjust?
vec4 uvColor = texture2D(u_texture1, vec2(v_texCoord.x, cy));
vec3 yuv = vec3(texture2D(u_texture, v_texCoord).r, uvColor.r - 0.5, uvColor.a - 0.5);
vec3 rgb = mat3( 1.0, 1.0, 1.0,
0, -0.39465, 2.03211,
1.13983, -0.58060, 0 ) * yuv;
gl_FragColor = v_fragmentColor * vec4(rgb, 1.0);
}
)";
class VideoPlayer : public cocos2d::Sprite
{
public:
bool initWithVideoFile(const std::string& videoFile) {
#if defined(__ANDROID__)
mplayer = new FJavaAndroidMediaPlayer(false, false, false);
auto fullPath = FileUtils::getInstance()->fullPathForFilename(videoFile);
mplayer->SetDataSource(fullPath);
mplayer->Prepare();
vw = mplayer->GetVideoWidth();
vh = mplayer->GetVideoHeight();
texelsCount = vw * vh * 4;
yasio::byte_buffer vbuf{ texelsCount, 0x00, std::true_type{} };
auto texture = new Texture2D();
bool succeed = texture->initWithData(vbuf.data(), vbuf.size(), PixelFormat::RGBA8, vw, vh, Vec2{ (float)vw, (float)vh }, false);
initWithTexture(texture);
setScale(1280.0f / vw);
return true;
#endif
HWND hwndGLView = Director::getInstance()->getOpenGLView()->getWin32Window();
auto hr = MFMediaPlayer::CreateInstance(hwndGLView, &mplayer);
bool ok = hr == S_OK;
if (ok) {
// Invoke at other thread
mplayer->SampleEvent = [=](uint8_t* sampleBuffer, size_t len) {
{
std::lock_guard<std::recursive_mutex> lck(_samplesMtx);
// _samples.push_front(yasio::byte_buffer{ sampleBuffer, sampleBuffer + len, std::true_type{} });
_frameData.assign(sampleBuffer, sampleBuffer + len);
_framesDirty = true;
}
// _sampleCV.notify_one();
//_lastFrameData.resize_fit(vw * vh * 4);
//int ret = libyuv::YUY2ToARGB(yuvData, vw * 2, _lastFrameData.data(), vw * 4, vw, vh);
//_frameDataDirty = true;
};
#if 0 // libyuv decoder too slow, needs do at thread to ensure GL thread 60fps
std::thread decoder([=] {
for (;;) {
std::unique_lock<std::recursive_mutex> lck(_samplesMtx);
while (_samples.empty())
_sampleCV.wait(lck);
auto samples = std::move(_samples);
lck.unlock();
if (!samples.empty()) {
auto& front = samples.front();
#if 0
_frameData.resize_fit(vw * vh * 4, 0xaf);
// libyuv::NV12ToARGB(front.data(), vw * 2, _frameData.data(), vw * 4, vw, vh);
int ret = libyuv::YUY2ToARGB(front.data(), vw * 2, _frameData.data(), vw * 4, vw, vh);
#else
_frameData = front;
#endif
samples.clear(); // clear all
_framesDirty = true;
}
//std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
});
decoder.detach();
#endif
auto fullPath = FileUtils::getInstance()->fullPathForFilename(videoFile);
auto wpath = ntcvt::from_chars(fullPath);
hr = mplayer->OpenURL(wpath.c_str());
mplayer->SetLooping(TRUE);
// mplayer->SetRate(2.5f);
}
return ok;
}
void setLoop(bool bVal) {
#if defined(__ANDROID__)
mplayer->SetLooping(bVal);
#endif
}
void draw(cocos2d::Renderer* renderer, const cocos2d::Mat4 &transform, uint32_t flags) override
{
// if (isPlaying)
// {
// if (mode == PlayMode::STEP)
// {
// update(step);
// }
// else if (mode == PlayMode::FRAME)
// {
// update(-1);
// }
// if (texureDirty && vbuf)
// {
// _texture->updateWithData(vbuf, 0, 0, _texture->getPixelsWide(), _texture->getPixelsHigh());
// }
// }
if (texureDirty)
{
// _texture->updateWithData(vbuf, 0, 0, _texture->getPixelsWide(), _texture->getPixelsHigh(), false, 0, 0);
}
#if defined(__ANDROID__)
if(mplayer) {
if(!started && mplayer->IsPrepared()) {
mplayer->SetLooping(true);
mplayer->SetVideoEnabled();
mplayer->SetAudioEnabled();
mplayer->Start();
started = true;
}
void* Buffer = nullptr;
int64 BufferCount = 0;
const int32 CurrentFramePosition = mplayer->GetCurrentPosition();
// const FTimespan Time = FTimespan::FromMilliseconds(CurrentFramePosition);
if (mplayer->GetVideoLastFrameData(Buffer, BufferCount)) {
//memcpy(vbuf.data(), Buffer, vbuf.size()); // vbuf.copy_n((char*)Buffer, BufferCount);
//_texture->updateWithData(vbuf.data(), vbuf.size(), PixelFormat::RGBA8, PixelFormat::RGBA8, vw, vh, Vec2{(float)vw, (float)vh}, 0);
_texture->updateWithData((uint8_t*)Buffer, BufferCount, PixelFormat::RGBA8, PixelFormat::RGBA8, vw, vh, Vec2{(float)vw, (float)vh}, 0);
}
}
#endif
if (_framesDirty) {
std::lock_guard<std::recursive_mutex> lck(_samplesMtx);
if (!_frameData.empty()) {
#if 0
_texture->updateWithData((uint8_t*)_frameData.data(), _frameData.size(),
PixelFormat::RGBA8, PixelFormat::RGBA8,
vw,
vh,
Vec2{ (float)vw, (float)vh },
0);
#else
if (mplayer->GetState() != PlayerState::Started)
return;
if (!_initialized) {
vw = mplayer->GetVideoWidth();
vh = mplayer->GetVideoHeight();
auto programCache = backend::ProgramCache::getInstance();
// fragNV12 = NV12_FRAG;
programCache->registerCustomProgramFactory(2022, positionTextureColor_vert, fragNV12);
auto program = programCache->getCustomProgram(2022);
setProgramState(new backend::ProgramState(program), false);
_initialized = true;
}
uint8_t* nv12Data = _frameData.data();
auto w = vw;
auto h = vh;
// refer to: https://www.cnblogs.com/nanqiang/p/10224867.html
bool needsInit = !_texture;
if (!_texture) {
_texture = new Texture2D();
_texture1 = new Texture2D();
}
_texture->updateWithData(nv12Data, w * h,
PixelFormat::L8, PixelFormat::L8,
w,
h,
false,
0);
_texture->updateWithData(nv12Data + w * h, _frameData.size(),
PixelFormat::LA8, PixelFormat::LA8,
w >> 1,
h >> 1,
false,
1);
if (needsInit) {
initWithTexture(_texture);
setScale(1280.0f / vw * 0.7);
//float myScale = 1280.0f / vw * 0.6;
//
//
//float frameWidth = vw;
//float frameHeight = vh;
//float texl_w = 1 / frameWidth;
//PS_SET_UNIFORM(_programState, "tex_w", frameWidth);
//PS_SET_UNIFORM(_programState, "tex_h", frameHeight);
//PS_SET_UNIFORM(_programState, "texl_w", texl_w);
//_texture1Location = _programState->getUniformLocation("u_texture1");
//_programState->setTexture(_texture1Location, 0, _texture1->getBackendTexture());
}
// updateProgramStateTexture(_texture);
// Force set premultipliedAlpha to true
//_texture->setPremultipliedAlpha(true);
//_texture1->setPremultipliedAlpha(true);
//updateBlendFunc();
#endif
}
_framesDirty = false;
}
cocos2d::Sprite::draw(renderer, transform, flags);
}
private:
#if defined(__ANDROID__)
FJavaAndroidMediaPlayer* mplayer = nullptr;
#endif
bool texureDirty = false;
size_t texelsCount = 0;
int vw = 0, vh = 0;
bool started = false;
MFMediaPlayer* mplayer = nullptr;
Texture2D* _texture1 = nullptr;
backend::UniformLocation _texture1Location;
bool _initialized = false; // does custom video render program state initialized
bool _framesDirty = false;
std::deque<yasio::byte_buffer> _samples; // The YUY2 samples
std::recursive_mutex _samplesMtx;
std::condition_variable_any _sampleCV;
yasio::byte_buffer _frameData; // The RGBA frame data
};