Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pthread_video_timer.cpp « platform - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1a321d7f1a4038c8986dfe283dd6e428e3303071 (plain)
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
#include "../std/target_os.hpp"

#ifndef OMIM_OS_WINDOWS_NATIVE

#include "video_timer.hpp"

#include "../base/logging.hpp"

#include <pthread.h>
#include <sys/time.h>
#include <sys/errno.h>


class PThreadVideoTimer : public VideoTimer
{
private:

  pthread_t m_handle;
  pthread_mutex_t m_mutex;
  pthread_cond_t m_cond;
  int m_frameRate;

public:
  PThreadVideoTimer(VideoTimer::TFrameFn frameFn)
    : VideoTimer(frameFn), m_frameRate(60)
  {
    ::pthread_mutex_init(&m_mutex, 0);
    ::pthread_cond_init(&m_cond, 0);
  }

  ~PThreadVideoTimer()
  {
    stop();

    ::pthread_mutex_destroy(&m_mutex);
    ::pthread_cond_destroy(&m_cond);
  }

  static void * TimerProc(void * p)
  {
    PThreadVideoTimer * t = reinterpret_cast<PThreadVideoTimer*>(p);

    ::timeval prevtv;
    ::gettimeofday(&prevtv, 0);

    ::timeval curtv;

    int64_t interval = 1000000000 / t->m_frameRate;
    int64_t halfInterval = interval / 2;

    while (true)
    {
      ::pthread_mutex_lock(&t->m_mutex);

      t->perform();

      ::gettimeofday(&curtv, 0);

      int64_t sec = (int64_t)curtv.tv_sec - (int64_t)prevtv.tv_sec;
      int64_t nsec = ((int64_t)curtv.tv_usec - (int64_t)prevtv.tv_usec) * 1000;

      int64_t nsecDiff = sec * (int64_t)1000000000 + nsec;
      int64_t ceiledDiff = ((nsecDiff + interval - 1) / interval) * interval;

      /// how much we should wait

      if ((ceiledDiff - nsecDiff) < halfInterval)
        /// less than a half-frame left, should wait till next frame
        ceiledDiff += interval;

      ::timespec ts;

      ts.tv_sec = prevtv.tv_sec + (prevtv.tv_usec * 1000 + ceiledDiff) / 1000000000;
      ts.tv_nsec = (prevtv.tv_usec * 1000 + ceiledDiff) % 1000000000;

      ::pthread_cond_timedwait(&t->m_cond, &t->m_mutex, &ts);

      ::gettimeofday(&prevtv, 0);

      if (t->m_state == EStopped)
      {
        ::pthread_mutex_unlock(&t->m_mutex);
        break;
      }

      ::pthread_mutex_unlock(&t->m_mutex);
    }

    ::pthread_exit(0);

    return 0;
  }

  void start()
  {
    if (m_state == EStopped)
    {
      ::pthread_create(&m_handle, 0, &TimerProc, reinterpret_cast<void*>(this));
      m_state = ERunning;
    }
  }

  void resume()
  {
    if (m_state == EPaused)
    {
      m_state = EStopped;
      start();
    }
  }

  void pause()
  {
    stop();
    m_state = EPaused;
  }

  void stop()
  {
    if (m_state == ERunning)
    {
      ::pthread_mutex_lock(&m_mutex);
      m_state = EStopped;
      ::pthread_cond_signal(&m_cond);
      ::pthread_mutex_unlock(&m_mutex);
      ::pthread_join(m_handle, 0);
    }
  }

  void perform()
  {
    m_frameFn();
  }
};

VideoTimer * CreatePThreadVideoTimer(VideoTimer::TFrameFn frameFn)
{
  return new PThreadVideoTimer(frameFn);
}

#endif