-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathVideoPlayer.cpp
More file actions
294 lines (265 loc) · 6.6 KB
/
Copy pathVideoPlayer.cpp
File metadata and controls
294 lines (265 loc) · 6.6 KB
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
// Copyright © 2015 CCP ehf.
#include "StdAfx.h"
#include "VideoPlayer.h"
#include "VideoController.h"
#include "IAudioSinkExposed.h"
#ifndef WITH_TESTS
namespace
{
// --------------------------------------------------------------------------------------
// Description:
// A specual deleter class for VideoPlayer. Since deleting a still-playing video can
// be time consuming (for example if the parser is stuck in a stream reading function)
// we delete the controller on a separate thread and clean up the stream and audio sink
// on the main thread afterwards.
// See also:
// VideoPlayer
// --------------------------------------------------------------------------------------
class VideoPlayerDeleter: public IBlueEvents
{
public:
static void DeletePlayer( VideoController* video, IBlueStream* stream, IAudioSinkExposed* audioSink )
{
auto data = CCP_NEW( "PlayerData" ) PlayerData;
data->m_video.reset( video );
data->m_stream = stream;
data->m_audioSink = audioSink;
data->m_deleted = 0;
data->m_thread = CcpThread( &PlayerData::DeleteMe, data );
static VideoPlayerDeleter s_deleter;
s_deleter.m_players.push_back( data );
}
protected:
virtual void OnTick( Be::Time, Be::Time, void* )
{
for( size_t i = 0; i < m_players.size(); )
{
if( m_players[i]->m_deleted )
{
if( m_players[i]->m_thread.joinable() )
{
m_players[i]->m_thread.join();
}
CCP_DELETE m_players[i];
m_players.erase( m_players.begin() + i );
}
else
{
++i;
}
}
}
private:
VideoPlayerDeleter()
:m_players( "VideoPlayerDeleter::m_players" )
{
BeOS->RegisterForTicks( this, nullptr );
}
struct PlayerData
{
void DeleteMe()
{
m_video.reset();
m_deleted = 1;
}
std::unique_ptr<VideoController, TrackableDelete<VideoController>> m_video;
IBlueStreamPtr m_stream;
IAudioSinkExposedPtr m_audioSink;
CcpAtomic<uint32_t> m_deleted;
CcpThread m_thread;
};
TrackableStdVector<PlayerData*> m_players;
};
}
#endif
VideoPlayer::VideoPlayer( IRoot* lockobj )
:m_lastUpdatedTimeStamp( std::numeric_limits<uint64_t>::max() ),
m_lastState( VideoController::State( -1 ) ),
m_looped( false )
{
#ifndef WITH_TESTS
BeOS->RegisterForTicks( this, nullptr );
#endif
}
VideoPlayer::~VideoPlayer()
{
#ifndef WITH_TESTS
BeOS->UnregisterForTicks( this, nullptr );
VideoPlayerDeleter::DeletePlayer( m_video.release(), m_stream, m_audioSink );
#else
m_video.reset();
#endif
}
bool VideoPlayer::OnModified( Be::Var* value )
{
m_lastUpdatedTimeStamp = std::numeric_limits<uint64_t>::max();
return true;
}
BlueStdResult VideoPlayer::Create( IBlueStream* stream, IAudioSinkExposed* audioSink, unsigned audioTrack, bool looped )
{
if( !stream )
{
return BlueStdResult( BLUE_STD_RESULT_VALUE_ERROR, "need a valid stream" );
}
m_video.reset( CCP_NEW( "VideoPlayer.m_video" ) VideoController( stream, audioSink, audioTrack, looped ) );
m_stream = stream;
m_audioSink = audioSink;
m_looped = looped;
return BlueStdResult( BLUE_STD_RESULT_OK );
}
uint64_t VideoPlayer::GetMediaTime() const
{
return m_video ? m_video->GetMediaTime() : 0u;
}
uint64_t VideoPlayer::GetDuration() const
{
return m_video ? m_video->GetParser().GetDuration() : 0u;
}
uint64_t VideoPlayer::GetDownloadedMediaTime() const
{
return m_video ? m_video->GetParser().GetDownloadedMediaTime() : 0u;
}
VideoPlayerResult VideoPlayer::Update()
{
if( !m_video )
{
return "video player is not initialized";
}
m_video->Update();
VideoController::Errors errors;
m_video->GetErrors( errors );
if( !errors )
{
if( m_lastError != errors )
{
m_lastError = errors;
if( m_onError )
{
m_onError.CallVoid( this );
}
}
return errors;
}
auto state = m_video->GetState();
if( m_lastState != state )
{
if( m_onStateChange )
{
m_onStateChange.CallVoid( this );
}
m_lastState = state;
}
if( !!m_bgraTexture )
{
auto frame = m_video->GetVideoFrame();
if( frame && m_lastUpdatedTimeStamp != frame->timeStamp )
{
if( !m_bgraTexture->IsValid() || m_bgraTexture->GetWidth() != frame->width || m_bgraTexture->GetHeight() != frame->height )
{
m_onCreateTextures.CallVoid( this, frame->width, frame->height );
}
if( !!m_bgraTexture && m_bgraTexture->GetWidth() == frame->width && m_bgraTexture->GetHeight() == frame->height )
{
m_bgraTexture->UpdateSubresource( 0, 0, frame->width, frame->height, frame->bgra, 4 * frame->width );
m_bgraTexture->SetAverageColor( frame->averageColor.red, frame->averageColor.green, frame->averageColor.blue, frame->averageColor.alpha );
}
m_lastUpdatedTimeStamp = frame->timeStamp;
}
}
else if( m_onCreateTextures )
{
auto frame = m_video->GetVideoFrame();
if( frame )
{
m_onCreateTextures.CallVoid( this, frame->width, frame->height );
}
}
return errors;
}
VideoController::State VideoPlayer::GetState() const
{
if( !m_video )
{
return VideoController::UNINITIALIZED;
}
return m_video->GetState();
}
void VideoPlayer::Pause()
{
if( m_video )
{
m_video->Pause();
}
}
void VideoPlayer::Resume()
{
if( m_video )
{
m_video->Resume();
}
}
bool VideoPlayer::IsPaused() const
{
if( m_video )
{
return m_video->IsPaused();
}
return false;
}
void VideoPlayer::Seek( uint64_t time )
{
if( m_video )
{
m_video->Seek( time );
}
}
VideoPlayerResult VideoPlayer::GetVideoInfo( std::map<std::string, uint32_t>& metadata )
{
if( !m_video )
{
return VideoPlayerResult( "video not initialized" );
}
if( !m_video->GetParser().IsMetadataAvailable() )
{
return VideoPlayerResult( "video information is not yet parsed" );
}
auto& data = m_video->GetParser().GetVideoMetadata();
metadata["width"] = data.width;
metadata["height"] = data.height;
metadata["hasAlpha"] = data.hasAlpha ? 1 : 0;
return VideoPlayerResult();
}
VideoPlayerResult VideoPlayer::Validate()
{
return m_lastError;
}
namespace
{
void ClearTexture( ITriTextureRes* texture, uint8_t fillColor )
{
if( texture && texture->GetWidth() && texture->GetHeight() )
{
CcpMallocBuffer zeros( "VideoPlayer::ClearTextures", 4 * texture->GetWidth() * texture->GetHeight() );
memset( zeros.get(), fillColor, zeros.size() );
texture->UpdateSubresource( 0, 0, texture->GetWidth(), texture->GetHeight(), zeros.get(), 4 * texture->GetWidth() );
texture->SetAverageColor( 0, 0, 0, 0 );
}
}
}
void VideoPlayer::ClearTextures()
{
ClearTexture( m_bgraTexture, 0 );
}
void VideoPlayer::OnTick( Be::Time realTime, Be::Time simTime, void* cookie )
{
Update();
}
void VideoPlayer::SetBgraTexture( ITriTextureRes* texture )
{
m_bgraTexture = texture;
m_lastUpdatedTimeStamp = std::numeric_limits<uint64_t>::max();
}
ITriTextureRes* VideoPlayer::GetBgraTexture() const
{
return m_bgraTexture;
}