summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/util/timeStampArray.cpp
blob: 730dc280f2bbf91f50baaf5521bff886d553e80f (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
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
/*
  class for managing byte positions and associated time positions
  Copyright (C) 1999  Martin Vogt

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as published by
  the Free Software Foundation.

  For more information look at the file COPYRIGHT in this package

 */


#include <iostream>

#include "timeStampArray.h"

using namespace std;


TimeStampArray::TimeStampArray(char* aName,int entries) {

  writePos=0;
  readPos=0;
  fillgrade=0;
  lastWritePos=0;
  this->entries=entries;
  if (entries < 1) {
    cout << "TimeStampArray entries must be >= 1";
    exit(0);
  }


  abs_thread_mutex_init(&writeInMut);
  abs_thread_mutex_init(&changeMut);

  

  name=strdup(aName);
  int i;
  tStampArray=new TimeStamp*[entries];

  for(i=0;i<entries;i++) {
    tStampArray[i]=new TimeStamp();
  }

}


TimeStampArray::~TimeStampArray() {

  int i;
  for(i=0;i<entries;i++) {
    delete tStampArray[i];
  } 
  delete [] tStampArray;
  if (name != NULL) {
    free(name);  // allocated with strdup
  }
  abs_thread_mutex_destroy(&writeInMut);
  abs_thread_mutex_destroy(&changeMut);
}

TimeStamp*  TimeStampArray::getReadTimeStamp() {
  return tStampArray[readPos];
}


int TimeStampArray::getFillgrade() {
  return fillgrade;
}

int TimeStampArray::insertTimeStamp(TimeStamp* src,long key,int len) {
  if (src == NULL) {
     return true;
  }
  lockStampArray();
  int back=true;
  src->copyTo(tStampArray[writePos]);
  tStampArray[writePos]->setKey(key,len);
  /*
  if (fillgrade > 0) {
    if (tStampArray[lastWritePos]->getKey() == key) {
      unlockStampArray();
      return;
    }
  }
  */

  lastWritePos=writePos;
  writePos++;
  fillgrade++;
  if (writePos == entries) {
    writePos=0;
  }
  if (fillgrade == entries) {
    cout << name<<" TimeStampArray::array overfull forward"<<endl;
    internalForward();
    back=false;
  }
  unlockStampArray();
  return back;
}


int TimeStampArray::bytesUntilNext(long key) {
  lockStampArray();
  TimeStamp* current=tStampArray[readPos];
  int back=current->getKey()-key;
  unlockStampArray();
  return back;
}

TimeStamp* TimeStampArray::getTimeStamp(long key) {
  lockStampArray();
  TimeStamp* back=tStampArray[readPos];
  if (key > back->getKey()+back->getKeyLen()) {
    if (fillgrade > 1) {
      internalForward();
      unlockStampArray();
      return getTimeStamp(key);
    }
  } 

  /*
  if (back->getKey() > key) {
    cout << "key "<<key<<" too big"<<back->getKey()-key<<endl;
    back->print("key access");
  } 
  */
  unlockStampArray();
  /* maybe we should return NULL here to indicate
     that there is no valid timestamp */
  /* This would need a check for every getTimeStamp call
     I think returning the last available stamp is ok
  */
  return back;
}


void TimeStampArray::forward() {
  lockStampArray();
  internalForward();
  unlockStampArray();
}



void TimeStampArray::clear() {
  lockStampArray();
  writePos=0;
  readPos=0;
  fillgrade=0;
  unlockStampArray();
}
 

void TimeStampArray::lockStampArray() {

  abs_thread_mutex_lock(&changeMut);
  abs_thread_mutex_lock(&writeInMut);
  abs_thread_mutex_unlock(&changeMut);

}


void TimeStampArray::unlockStampArray() {
  abs_thread_mutex_unlock(&writeInMut); 
}


void TimeStampArray::internalForward() {
  readPos++;
  fillgrade--;
  if (readPos == entries) {
    readPos=0;
  }
}