summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/output/dspX11OutputStream.cpp
blob: 05503ffa4186d8046b817b6b83d68ec0b3a5889f (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
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
/*
  concret OutputClass
  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 "dspX11OutputStream.h"
#include "../util/audio/dspWrapper.h"
#include "windowOut.h"
#include "avSyncer.h"
#include "yuvDumper.h"

#include <iostream>

using namespace std;

DspX11OutputStream::DspX11OutputStream(int bufferSize) {
  dspWrapper=new DSPWrapper();
  x11Window=new WindowOut();
  avSyncer=new AVSyncer(bufferSize);
  yuvDumper=new YUVDumper();

  audioTime=new AudioTime();

  lPerformance=false;
  lneedInit=false;
  lVideoInit=false;
  lBufferSet=false;
  lYUVDump=false;
}


DspX11OutputStream::~DspX11OutputStream() {
  delete dspWrapper;
  delete x11Window;

  delete avSyncer;
  delete audioTime;
  delete yuvDumper;
}



int DspX11OutputStream::audioSetup(int frequency,int stereo,
				   int sign,int big,int sixteen) {

  dspWrapper->audioSetup(stereo,sixteen,sign,big,frequency);
  audioTime->setFormat(stereo,sixteen,frequency,sign,big);
  avSyncer->audioSetup(frequency,stereo,sign,big,sixteen);
  if (dspWrapper->isOpenDevice() == true) {
    if (lBufferSet==false) {
      int size=dspWrapper->getAudioBufferSize();
      avSyncer->setAudioBufferSize(size);
    }
  }
  return true;
}


void DspX11OutputStream::audioClose(void) {
  avSyncer->audioClose();
  dspWrapper->closeDevice();
}


void DspX11OutputStream::audioOpen() {
  if (dspWrapper->isOpenDevice() == false) {
    dspWrapper->openDevice();
    if (lBufferSet==false) {
      int size=dspWrapper->getAudioBufferSize();
      avSyncer->setAudioBufferSize(size);
    }    
  }
}




int DspX11OutputStream::audioPlay(TimeStamp* startStamp,
				  TimeStamp* endStamp,char *buffer, int size) {

  if (lneedInit) {
    cout << "FIXME. work on audioFrames!!"<<endl;
    lneedInit=false;
  }

  if (lPerformance==false) {
    //
    // Now feed data smoothly into the dsp/avSyncer
    //
    int pos=0;
    int rest=size;
    int inc=getPreferredDeliverSize();
    int len;

    while (rest > 0) {
      len=rest;
      if (len>inc) {
	len=inc;
      }
      if (dspWrapper->isOpenDevice()) {
	if (dspWrapper->audioPlay(buffer,len) != len) {
	  cout << "write error to dsp"<<endl;
	  lneedInit=true;
	  return size-rest;
	}
      }
      
      avSyncer->audioPlay(startStamp,endStamp,buffer,len);
      buffer+=len;
      rest-=len;
    }
    return size;
  } else {
    return size;
  }
}


int DspX11OutputStream::getPreferredDeliverSize() {
  if (avSyncer->getPreferredDeliverSize() <= 500) {
    return 500;
  }
  return avSyncer->getPreferredDeliverSize();
}


int DspX11OutputStream::openWindow(int width, int height,const char *title) {
  int back=x11Window->openWindow(width,height,title);
  setOutputInit(true);
  if (lYUVDump) {
    yuvDumper->openWindow(width,height,title);
  }
  return back;
}

int DspX11OutputStream::x11WindowId() {
  return x11Window->x11WindowId();
}


void DspX11OutputStream::closeWindow() {
  x11Window->closeWindow();
}

void DspX11OutputStream::flushWindow() {
  x11Window->flushWindow();
}


PictureArray* DspX11OutputStream::lockPictureArray() {
  return x11Window->lockPictureArray();
}


void DspX11OutputStream::unlockPictureArray(PictureArray* pictureArray) {


  
  YUVPicture* pic=pictureArray->getYUVPictureCallback();

  if (lYUVDump) {
    yuvDumper->unlockPictureArray(pictureArray);
  }
  if (avSyncer->syncPicture(pic)==false) {
    return;
  }

  x11Window->unlockPictureArray(pictureArray);


}

int DspX11OutputStream::getFrameusec() {
  return avSyncer->getFrameusec();
}

  

int DspX11OutputStream::getOutputInit() {
  return lVideoInit;
}


void DspX11OutputStream::setOutputInit(int lInit) {
  this->lVideoInit=lInit;
}
 
 

void DspX11OutputStream::config(const char* key,
				const char* value,void* user_data) {

  cout << "key:"<<key<<endl;
  if (strcmp(key,"-s")==0) {
    avSyncer->config(key,value,user_data);
  }
  if (strcmp(key,"-b")==0) {
    lBufferSet=true;
    int size=strtol(value,(char **)NULL,10);
    cout << "simulated audio buffersize:"<<size<<" bytes"<<endl;
    avSyncer->setAudioBufferSize(size);
  }
  if (strcmp(key,"-p")==0) {
    lPerformance=true;
    avSyncer->config(key,value,user_data);
  }  
  if (strcmp(key,"yufDump")==0) {
    int method=atoi(value);
    switch(method) {
    case 2:
      yuvDumper->setMethod(_DUMP_YUV_AS_STREAM);
      break;
    default:
      cout << "unknown dump method"<<endl;
    }
    lYUVDump=true;
  }  
  x11Window->config(key,value,user_data);
}


AVSyncer* DspX11OutputStream::getAVSyncer() {
  return avSyncer;
}