-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathVideoController.cpp
More file actions
311 lines (284 loc) · 6.79 KB
/
Copy pathVideoController.cpp
File metadata and controls
311 lines (284 loc) · 6.79 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright © 2015 CCP ehf.
#include "StdAfx.h"
#include "VideoController.h"
CCP_STATS_DECLARE( videoplayerInitialBufferingTime, "VideoPlayer/initialBuffering", false, CST_COUNTER_HIGH, "total initial buffering time in MS" );
CCP_STATS_DECLARE( videoplayerBufferingTime, "VideoPlayer/buffering", false, CST_COUNTER_HIGH, "total during-playback buffering time in MS" );
CCP_STATS_DECLARE( videoplayerDestructor, "VideoPlayer/destructorTime", false, CST_TIME, "time to destroy a controller" );
namespace
{
uint64_t AFTER_BUFFERING_TIME = 2 * 1000000000;
uint64_t DROP_FRAME_THRESHOLD = 50000000;
}
VideoController::VideoController( ICcpStream* stream, IAudioSink* audioSink, unsigned audioTrack, bool looped )
:m_state( UNINITIALIZED ),
m_paused( false ),
m_audioSink( audioSink ),
m_seekOffset( 0 )
{
if( m_audioSink )
{
m_audioSink->Pause();
}
m_mediaTime.Pause();
m_parser.reset( CreateVideoContainerParser( stream, audioSink ? STREAM_AUDIO_VIDEO : STREAM_VIDEO, audioTrack, looped ) );
m_state = PARSING_METADATA;
}
VideoController::~VideoController()
{
#ifndef WITH_TESTS
CCP_STATS_SCOPED_TIME( videoplayerDestructor );
#endif
if( m_audioDecoder )
{
m_audioDecoder->GetDecodedQueue().SetComplete();
}
if( m_videoDecoder )
{
m_videoDecoder->GetDecodedQueue().SetComplete();
}
if( m_audioSink )
{
m_audioSink->Close();
}
m_parser->CompleteQueues();
m_audioDecoder.reset();
m_videoDecoder.reset();
m_parser.reset();
}
void VideoController::GetErrors( Errors& errors )
{
errors.parserError = m_parser ? m_parser->GetError() : PARSER_ERROR_OK;
errors.audioDecoderError = m_audioDecoder ? m_audioDecoder->GetError() : DECODER_ERROR_OK;
errors.videoDecoderError = m_videoDecoder ? m_videoDecoder->GetError() : DECODER_ERROR_OK;
}
void VideoController::Pause()
{
if( m_paused )
{
return;
}
m_paused = true;
if( m_audioSink )
{
m_audioSink->Pause();
}
m_mediaTime.Pause();
}
void VideoController::Resume()
{
if( !m_paused )
{
return;
}
m_paused = false;
if( m_audioSink )
{
m_audioSink->Resume();
}
m_mediaTime.Resume();
}
void VideoController::Seek( uint64_t time )
{
if( m_state <= INITIAL_BUFFERING || m_state > FINISHING_BUFFERING )
{
return;
}
if( m_audioSink )
{
m_audioSink->Pause();
}
m_mediaTime.Pause();
m_seekOffset = time;
m_parser->Seek( time );
m_state = INITIAL_BUFFERING;
if( m_audioDecoder )
{
m_audioDecoder->GetDecodedQueue().Clear();
}
if( m_videoDecoder )
{
m_videoDecoder->GetDecodedQueue().Clear();
}
}
bool VideoController::IsPaused() const
{
return m_paused;
}
IVideoContainerParser& VideoController::GetParser()
{
return *m_parser;
}
void VideoController::Update()
{
switch( m_state )
{
case UNINITIALIZED:
case DONE:
return;
case PARSING_METADATA:
if( m_parser->GetError() != PARSER_ERROR_OK )
{
m_state = DONE;
return;
}
if( !m_parser->IsMetadataAvailable() )
{
return;
}
if( m_parser->GetVideoQueue() )
{
m_videoDecoder.reset( CreateVideoDecoder( m_parser->GetVideoMetadata(), *m_parser->GetVideoQueue() ) );
}
if( m_parser->GetAudioQueue() )
{
m_audioDecoder.reset( CreateAudioDecoder( m_parser->GetAudioMetadata(), *m_parser->GetAudioQueue() ) );
if( !m_audioDecoder )
{
CCP_LOGWARN( "Video file contains an unsupported audio codec" );
}
}
if( !m_audioDecoder )
{
m_audioSink = nullptr;
}
m_state = INITIAL_BUFFERING;
m_bufferingTimer.Start();
case INITIAL_BUFFERING:
if( ( m_audioDecoder && !m_audioDecoder->GetDecodedQueue().IsFull() ) ||
( m_videoDecoder && !m_videoDecoder->GetDecodedQueue().IsFull() ) ||
!( ( m_parser->GetAudioQueue() && m_parser->GetAudioQueue()->IsFull() ) ||
( m_parser->GetVideoQueue() && m_parser->GetVideoQueue()->IsFull() ) ) )
{
return;
}
CCP_STATS_ADD( videoplayerInitialBufferingTime, int32_t( m_bufferingTimer.GetTime() / 1000000 ) );
m_state = PLAYING;
if( m_audioSink )
{
m_audioSink->Open( m_parser->GetAudioMetadata(), m_audioDecoder->GetDecodedQueue() );
}
m_mediaTime.Start();
if( m_audioSink )
{
m_audioSink->Resume();
}
m_mediaTime.Resume();
Update();
break;
case BUFFERING:
RemoveExpiredFrames();
if( NeedsBuffering() )
{
return;
}
m_state = FINISHING_BUFFERING;
CCP_STATS_ADD( videoplayerBufferingTime, int32_t( m_bufferingTimer.GetTime() / 1000000 ) );
m_bufferingTimer.Start();
CCP_LOG( "VideoController: starting buffering followup pause" );
case FINISHING_BUFFERING:
RemoveExpiredFrames();
if( m_bufferingTimer.GetTime() < AFTER_BUFFERING_TIME )
{
return;
}
m_state = PLAYING;
if( m_audioSink )
{
m_audioSink->Resume();
}
m_mediaTime.Resume();
CCP_LOG( "VideoController: resuming playback after buffering" );
case PLAYING:
if( m_paused )
{
return;
}
if( m_videoDecoder && m_audioDecoder )
{
auto mt = GetMediaTime();
if( mt > DROP_FRAME_THRESHOLD )
{
m_videoDecoder->SetDropFrameTime( mt - DROP_FRAME_THRESHOLD );
}
else
{
m_videoDecoder->SetDropFrameTime( 0 );
}
}
RemoveExpiredFrames();
if( IsDone() )
{
m_state = DONE;
CCP_LOG( "VideoController: playback is finished" );
}
else if( NeedsBuffering() )
{
CCP_LOG( "VideoController: starting buffering because audio/video queues are running low on frames" );
m_state = BUFFERING;
if( m_audioSink )
{
m_audioSink->Pause();
}
m_mediaTime.Pause();
m_bufferingTimer.Start();
}
}
}
VideoFrame* VideoController::GetVideoFrame()
{
if( m_state < PLAYING )
{
return nullptr;
}
return m_videoDecoder->GetDecodedQueue().Peek();
}
uint64_t VideoController::GetMediaTime()
{
if( m_state == INITIAL_BUFFERING )
{
return m_seekOffset;
}
return m_audioSink ? m_audioSink->GetTime() : m_seekOffset + m_mediaTime.GetTime();
}
void VideoController::RemoveExpiredFrames()
{
if( m_videoDecoder )
{
uint64_t mediaTime = GetMediaTime();
while( true )
{
auto frame = m_videoDecoder->GetDecodedQueue().Peek();
auto nextFrame = m_videoDecoder->GetDecodedQueue().Peek( 1 );
if( !frame )
{
break;
}
if( frame->timeStamp > mediaTime || ( ( !nextFrame || nextFrame->timeStamp > mediaTime ) && !m_videoDecoder->GetDecodedQueue().IsComplete() ) )
{
break;
}
m_videoDecoder->GetDecodedQueue().Pop();
}
}
}
bool VideoController::NeedsBuffering() const
{
return ( m_videoDecoder && !m_videoDecoder->GetDecodedQueue().IsComplete() && m_videoDecoder->GetDecodedQueue().Size() == 0 ) ||
( m_audioDecoder && !m_audioDecoder->GetDecodedQueue().IsComplete() && m_audioDecoder->GetDecodedQueue().Size() < 10 );
}
bool VideoController::IsDone() const
{
if( m_videoDecoder )
{
auto frame = m_videoDecoder->GetDecodedQueue().Peek();
return !frame && m_videoDecoder->GetDecodedQueue().IsComplete() && ( !m_audioSink || m_audioSink->IsDone() );
}
else if( m_audioSink )
{
return m_audioSink->IsDone();
}
else
{
return true;
}
}