-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVideoPlayer.cpp
250 lines (233 loc) · 4.61 KB
/
VideoPlayer.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
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
#include "VideoPlayer.h"
#include "renderer/backend/Device.h"
using namespace video;
Player* Player::create(const std::string& path)
{
auto ret = new (std::nothrow) Player();
if (ret&&ret->init_(path))
{
ret->autorelease();
return ret;
}
delete ret;
return nullptr;
}
Player::Player()
{
}
Player::~Player()
{
CC_SAFE_RELEASE_NULL(decoder);
}
bool Player::init_(const std::string& path)
{
decoder = Decoder::create(path);
if (!decoder)
return false;
decoder->retain();
if (!decoder->setup())
return false;
const auto size = decoder->getVideoTargetSize();
auto texture = new (std::nothrow) cocos2d::Texture2D();
if (!texture)
return false;
texture->autorelease();
cocos2d::backend::TextureDescriptor desc;
desc.width = size.width;
desc.height = size.height;
desc.textureUsage = cocos2d::TextureUsage::WRITE;
desc.textureFormat = cocos2d::backend::PixelFormat::RGBA8888;
#ifdef CC_VERSION
texture->updateTextureDescriptor(desc);
#else
auto tex = cocos2d::backend::Device::getInstance()->newTexture(desc);
tex->autorelease();
texture->initWithBackendTexture(tex);
#endif // CC_VERSION
initWithTexture(texture);
setContentSize(size);
_running = true;
update(0);
return true;
}
void Player::vplay()
{
if (mode == PlayMode::MANUAL)
return;
seek(0);
if (!isPlaying)
{
if (mode == PlayMode::REALTIME)
scheduleUpdate();
isPlaying = true;
}
autoUpdate = true;
}
void Player::vstop()
{
if (mode == PlayMode::MANUAL)
return;
if (isPlaying)
{
if (mode == PlayMode::REALTIME)
unscheduleUpdate();
isPlaying = false;
seek(0);
}
autoUpdate = false;
}
void Player::vpause()
{
if (mode == PlayMode::MANUAL)
return;
if (isPlaying&&autoUpdate)
{
if (mode == PlayMode::REALTIME)
unscheduleUpdate();
isPlaying = false;
}
}
void Player::vresume()
{
if (mode == PlayMode::MANUAL)
return;
if (!isPlaying&&autoUpdate)
{
if (mode == PlayMode::REALTIME)
scheduleUpdate();
isPlaying = true;
}
}
void Player::playerSeekTime(double sec)
{
if (decoder->playerSeekTime(sec))
{
currentTime = sec;
}
}
void Player::seek(uint32_t frame)
{
if (decoder->seek(frame))
{
currentTime = frame / decoder->getVideoFrameRateForPlayer();
}
}
void Player::update(float dt)
{
cocos2d::Sprite::update(dt);
if (dt >= 0)
{
playerSeekTime(currentTime);
currentTime += dt;
}
else
{
currentTime += 1.0 / decoder->getVideoFrameRateForPlayer();
}
auto ret = decoder->read(&vbuf);
texureDirty = ret != 0 && vbuf;
if (decoder->tell() == decoder->getTotalFrames() - 1/* || !vbuf*/)
{
vstop();
if (videoEndCallback)
videoEndCallback();
if (autoRemove)
{
// stop texture update
vbuf = nullptr;
// avoid memory conflict
scheduleOnce([this](float dt_)
{
removeFromParentAndCleanup(true);
}, 0, "remove");
}
}
}
void Player::setPlayMode(PlayMode m)
{
if (m != PlayMode::REALTIME)
{
unscheduleUpdate();
}
mode = m;
}
void Player::draw(cocos2d::Renderer* renderer, const cocos2d::Mat4 &transform, uint32_t flags)
{
if (!_texture)
return;
if (isPlaying)
{
if (mode == PlayMode::STEP)
{
update(step);
}
else if (mode == PlayMode::FRAME)
{
update(-1);
}
if (texureDirty && vbuf)
{
#ifdef CC_VERSION
const int length = _contentSize.width * _contentSize.height;
size_t dataSize = 0;
cocos2d::PixelFormat format;
if (decoder->getReadFormat() == AV_PIX_FMT_RGBA)
{
dataSize = length * 4;
format = cocos2d::PixelFormat::RGBA8;
}
else
{
dataSize = length * 3;
format = cocos2d::PixelFormat::RGB8;
}
_texture->updateWithData(
vbuf, dataSize,
format,
cocos2d::PixelFormat::RGBA8888,
_texture->getPixelsWide(),
_texture->getPixelsHigh(),
false);
#else
_texture->updateWithData(vbuf, 0, 0,
_texture->getPixelsWide(),
_texture->getPixelsHigh());
#endif // CC_VERSION
}
}
cocos2d::Sprite::draw(renderer, transform, flags);
}
void Player::setVideoEndCallback(const std::function<void()>& func)
{
videoEndCallback = func;
}
void Player::saveCurrentFrame(const std::string& path)
{
if (!vbuf)
return;
auto im = new cocos2d::Image();
const int length = _contentSize.width * _contentSize.height;
const size_t dataSize = length * 4;
std::vector<uint8_t> buffer;
uint8_t* buf;
if (decoder->getReadFormat() == AV_PIX_FMT_RGBA)
{
buf = vbuf;
}
else
{
buffer.resize(dataSize);
buf = buffer.data();
for (int i = 0; i < length; ++i)
{
buf[i * 4] = vbuf[i * 3];
buf[i * 4 + 1] = vbuf[i * 3 + 1];
buf[i * 4 + 2] = vbuf[i * 3 + 2];
buf[i * 4 + 3] = 255;
}
}
im->initWithRawData(buf, dataSize,
_contentSize.width, _contentSize.height, 32);
im->saveToFile(path);
delete im;
}