code cleanup of synchronization in player
Stefan Schuermans

Stefan Schuermans commited on 2014-01-03 22:42:57
Showing 2 changed files, with 201 additions and 102 deletions.

... ...
@@ -124,121 +124,27 @@ void Player::sendInfo(const std::string &sync, Info &info)
124 124
     halt();
125 125
 
126 126
   // seek to other movie if not correct one
127
-  if (m_curEntry != m_playlistTracker.m_list.end() &&
127
+  if (m_curEntry == m_playlistTracker.m_list.end() ||
128 128
       !checkName(m_curEntry->m_name, info.name)) {
129
-    // find movie by name
130
-    PlaylistIt it = m_playlistTracker.m_list.begin();
131
-    while (it != m_playlistTracker.m_list.end()) {
132
-      if (checkName(it->m_name, info.name))
133
-        break;
134
-      ++it;
135
-    }
136
-    // movie found -> seek to it
137
-    if (it != m_playlistTracker.m_list.end()) {
129
+    // movie found -> change has been done
130
+    if (seekToMovie(info.name))
138 131
       change = true;
139
-      m_curValid = false;
140
-      m_curEntry = it;
141
-      m_curFrame = 0;
142
-      m_remainTime = Time::zero;
143
-      if (m_curEntry->m_pObj->m_pMovie) {
144
-        stBlinkenFrame *pFrame =
145
-          BlinkenMovieGetFrame(m_curEntry->m_pObj->m_pMovie, m_curFrame);
146
-        if (pFrame) {
147
-          m_remainTime.fromMs(BlinkenFrameGetDuration(pFrame));
148
-          m_curValid = true;
149
-        }
150
-      }
151
-      m_curFrameStart = Time::zero;
152
-      m_curFrameEnd = m_remainTime;
153
-      m_nextTime = Time::now() + m_remainTime;
154
-    }
155
-    // movie not found -> do not seek to position
156
-    else {
132
+    // movie not found -> do not seek to position (wrong movie anyway)
133
+    else
157 134
       seekPos = false;
158 135
   }
159
-  }
160
-
161
-  Time now = Time::now();
162 136
 
137
+  // seek to requested position in movie
163 138
   if (m_curValid && seekPos) {
164
-
165
-    // get frame count of current movie
166
-    int frameCnt = BlinkenMovieGetFrameCnt(m_curEntry->m_pObj->m_pMovie);
167
-
168
-    // get remaining frame time and current position in movie
169
-    if (!m_halted) { // remaining frame time always up-to-date in halted mode
170
-      m_remainTime = m_nextTime - now;
171
-      if (m_remainTime < Time::zero)
172
-        m_remainTime = Time::zero;
173
-    }
174
-    Time pos = m_curFrameEnd - m_remainTime;
175
-
176
-    // seek to earlier position in movie if needed
177
-    while (pos > info.pos + m_maxDeviation) {
139
+    if (seekToPos(info.pos))
178 140
       change = true;
179
-      Time frame = m_curFrameEnd - m_curFrameStart;
180
-      Time shown = frame - m_remainTime;
181
-      Time goback = pos - info.pos;
182
-      // correct by just showing current frame some time longer
183
-      if (goback <= shown) {
184
-        pos -= goback;
185
-        m_remainTime += goback;
186
-        break;
187 141
   }
188
-      // go back to begin of current frame
189
-      goback -= shown;
190
-      pos -= shown;
191
-      m_remainTime = frame;
192
-      // go back to end of previous frame
193
-      if (m_curFrame <= 0)
194
-        break;
195
-      m_curFrame--;
196
-      m_curFrameEnd = m_curFrameStart;
197
-      frame = Time::zero;
198
-      stBlinkenFrame *pFrame =
199
-        BlinkenMovieGetFrame(m_curEntry->m_pObj->m_pMovie, m_curFrame);
200
-      if (pFrame)
201
-        frame.fromMs(BlinkenFrameGetDuration(pFrame));
202
-      m_curFrameStart = m_curFrameEnd - frame;
203
-      m_remainTime = Time::zero;
204
-    } // while (pos > info.pos + m_maxDeviation)
205
-
206
-    // seek to later position in movie if needed
207
-    while (pos + m_maxDeviation < info.pos) {
208
-      change = true;
209
-      Time frame = m_curFrameEnd - m_curFrameStart;
210
-      Time go = info.pos - pos;
211
-      // correct by just showing current frame some time shorter
212
-      if (go <= m_remainTime) {
213
-        pos += go;
214
-        m_remainTime -= go;
215
-        break;
216
-      }
217
-      // go to end of current frame
218
-      go -= m_remainTime;
219
-      pos += m_remainTime;
220
-      m_remainTime = Time::zero;
221
-      // go to begin of next frame
222
-      if (m_curFrame >= frameCnt)
223
-        break;
224
-      m_curFrame++;
225
-      m_curFrameStart = m_curFrameEnd;
226
-      frame = Time::zero;
227
-      stBlinkenFrame *pFrame =
228
-        BlinkenMovieGetFrame(m_curEntry->m_pObj->m_pMovie, m_curFrame);
229
-      if (pFrame)
230
-        frame.fromMs(BlinkenFrameGetDuration(pFrame));
231
-      m_curFrameEnd = m_curFrameStart + frame;
232
-      m_remainTime = frame;
233
-    } // while (pos + m_maxDeviation < info.pos)
234
-
235
-  } // if (m_curValid)
236 142
 
237 143
   // send frame and update timed callback if something changed
238 144
   if (change) {
239 145
     sendFrame();
240 146
     if (m_curValid && !m_halted) {
241
-      m_nextTime = now + m_remainTime; // remaining time changed -> update
147
+      m_nextTime = Time::now() + m_remainTime; // remainTime changed -> update
242 148
       m_mgrs.m_callMgr.requestTimeCall(this, m_nextTime);
243 149
     }
244 150
   }
... ...
@@ -392,5 +298,170 @@ void Player::sendFrame()
392 298
     m_fileOutStream.setFrame(NULL);
393 299
 }
394 300
 
301
+/**
302
+ * @brief seek to movie by name
303
+ * @param[in] name name of movie to seek to (name cmp according to SyncRecv)
304
+ * @return true if movie was found and seeked to, false otherwise
305
+ */
306
+bool Player::seekToMovie(const std::string &name)
307
+{
308
+  // find movie by name
309
+  PlaylistIt it = m_playlistTracker.m_list.begin();
310
+  while (it != m_playlistTracker.m_list.end()) {
311
+    if (checkName(it->m_name, name))
312
+      break;
313
+    ++it;
314
+  }
315
+
316
+  // movie not found -> give up, return error
317
+  if (it == m_playlistTracker.m_list.end())
318
+    return false;
319
+
320
+  // seek to movie
321
+  m_curValid = false;
322
+  m_curEntry = it;
323
+  m_curFrame = 0;
324
+  m_remainTime = Time::zero;
325
+  if (m_curEntry->m_pObj->m_pMovie) {
326
+    stBlinkenFrame *pFrame =
327
+      BlinkenMovieGetFrame(m_curEntry->m_pObj->m_pMovie, m_curFrame);
328
+    if (pFrame) {
329
+      m_remainTime.fromMs(BlinkenFrameGetDuration(pFrame));
330
+      m_curValid = true;
331
+    }
332
+  }
333
+  m_curFrameStart = Time::zero;
334
+  m_curFrameEnd = m_remainTime;
335
+  m_nextTime = Time::now() + m_remainTime;
336
+
337
+  return true;
338
+}
339
+
340
+/**
341
+ * @brief seek to position in current movie
342
+ * @param[in] tgtPos target position in current movie to seek to
343
+ * @return true if movie position has been changed, false otherwise
344
+ */
345
+bool Player::seekToPos(const Time &tgtPos)
346
+{
347
+  bool change = false;
348
+
349
+  // get remaining frame time and current position in movie
350
+  if (!m_halted) { // remaining frame time always up-to-date in halted mode
351
+    m_remainTime = m_nextTime - Time::now();
352
+    if (m_remainTime < Time::zero)
353
+      m_remainTime = Time::zero;
354
+  }
355
+  Time pos = m_curFrameEnd - m_remainTime;
356
+
357
+  // seek to earlier position in movie if needed
358
+  if (pos > tgtPos + m_maxDeviation) {
359
+    Time goback = pos - tgtPos;
360
+    if (seekBackwards(goback))
361
+      change = true;
362
+  } // if (pos > tgtPos + m_maxDeviation)
363
+
364
+  // seek to later position in movie if needed
365
+  if (pos + m_maxDeviation < tgtPos) {
366
+    Time go = tgtPos - pos;
367
+    if (seekForwards(go))
368
+      change = true;
369
+  } // if (pos + m_maxDeviation < tgtPos)
370
+
371
+  return change;
372
+}
373
+
374
+/**
375
+ * @brief seek backwards in current movie
376
+ * @param[in] goback time to seek backwards in current movie
377
+ * @return true if movie position has been changed, false otherwise
378
+ */
379
+bool Player::seekBackwards(Time goback)
380
+{
381
+  bool change = false;
382
+
383
+  // time of current frame
384
+  Time frame = m_curFrameEnd - m_curFrameStart;
385
+
386
+  // time the current frame has already been shown
387
+  Time shown = frame - m_remainTime;
388
+
389
+  while (goback > Time::zero) {
390
+
391
+    // correct by just showing current frame some time longer
392
+    if (goback <= shown) {
393
+      m_remainTime += goback;
394
+      break;
395
+    }
396
+
397
+    // go back to begin of current frame
398
+    goback -= shown;
399
+    m_remainTime = frame;
400
+
401
+    // go back to end of previous frame
402
+    if (m_curFrame <= 0)
403
+      break;
404
+    m_curFrame--;
405
+    m_curFrameEnd = m_curFrameStart;
406
+    frame = Time::zero;
407
+    stBlinkenFrame *pFrame =
408
+      BlinkenMovieGetFrame(m_curEntry->m_pObj->m_pMovie, m_curFrame);
409
+    if (pFrame)
410
+      frame.fromMs(BlinkenFrameGetDuration(pFrame));
411
+    m_curFrameStart = m_curFrameEnd - frame;
412
+    m_remainTime = Time::zero;
413
+    shown = frame;
414
+
415
+  } // while (goback > Time::zero)
416
+
417
+  return change;
418
+}
419
+
420
+/**
421
+ * @brief seek forwards in current movie
422
+ * @param[in] go time to seek forwards in current movie
423
+ * @return true if movie position has been changed, false otherwise
424
+ */
425
+bool Player::seekForwards(Time go)
426
+{
427
+  bool change = false;
428
+
429
+  // frame count of current movie
430
+  int frameCnt = BlinkenMovieGetFrameCnt(m_curEntry->m_pObj->m_pMovie);
431
+
432
+  // time of current frame
433
+  Time frame = m_curFrameEnd - m_curFrameStart;
434
+
435
+  while (go > Time::zero) {
436
+    change = true;
437
+
438
+    // correct by just showing current frame some time shorter
439
+    if (go <= m_remainTime) {
440
+      m_remainTime -= go;
441
+      break;
442
+    }
443
+
444
+    // go to end of current frame
445
+    go -= m_remainTime;
446
+    m_remainTime = Time::zero;
447
+
448
+    // go to begin of next frame
449
+    if (m_curFrame >= frameCnt)
450
+      break;
451
+    m_curFrame++;
452
+    m_curFrameStart = m_curFrameEnd;
453
+    frame = Time::zero;
454
+    stBlinkenFrame *pFrame =
455
+      BlinkenMovieGetFrame(m_curEntry->m_pObj->m_pMovie, m_curFrame);
456
+    if (pFrame)
457
+      frame.fromMs(BlinkenFrameGetDuration(pFrame));
458
+    m_curFrameEnd = m_curFrameStart + frame;
459
+    m_remainTime = frame;
460
+
461
+  } // while (go > Time::zero)
462
+
463
+  return change;
464
+}
465
+
395 466
 } // namespace Blinker
396 467
 
... ...
@@ -96,6 +96,34 @@ protected:
96 96
   /// send current frame to output stream
97 97
   void sendFrame();
98 98
 
99
+  /**
100
+   * @brief seek to movie by name
101
+   * @param[in] name name of movie to seek to (name cmp according to SyncRecv)
102
+   * @return true if movie was found and seeked to, false otherwise
103
+   */
104
+  bool seekToMovie(const std::string &name);
105
+
106
+  /**
107
+   * @brief seek to position in current movie
108
+   * @param[in] tgtPos target position in current movie to seek to
109
+   * @return true if movie position has been changed, false otherwise
110
+   */
111
+  bool seekToPos(const Time &tgtPos);
112
+
113
+  /**
114
+   * @brief seek backwards in current movie
115
+   * @param[in] goback time to seek backwards in current movie
116
+   * @return true if movie position has been changed, false otherwise
117
+   */
118
+  bool seekBackwards(Time goback);
119
+
120
+  /**
121
+   * @brief seek forwards in current movie
122
+   * @param[in] go time to seek forwards in current movie
123
+   * @return true if movie position has been changed, false otherwise
124
+   */
125
+  bool seekForwards(Time go);
126
+
99 127
 protected:
100 128
   OutStreamFile   m_fileOutStream;   ///< output stream name file
101 129
   InStreamFile    m_fileHaltStream;  /**< halt stream name file
102 130