summaryrefslogtreecommitdiffstats
path: root/mpeglib/example/yaf/yafxplayer/inputDecoderXPlayer.cpp
blob: 0d1bd03469d3f7d516d02358b83fa93db32f9d09 (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
237
238
239
/*
  generic Implementation of a cd-player
  Copyright (C) 1998  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 "inputDecoderXPlayer.h"
#include "commandTableXPlayer.h"
#include <iostream>
using namespace std;

#define DEBUG cout << "Command:1 Msg:"


InputDecoderXPlayer::InputDecoderXPlayer(YafOutputStream* yafOutput) : 
  InputDecoder() {

  output=new OutputInterface(&cout);
  output->setProtocolSyntax(true);
  this->yafOutput=yafOutput;
  ct=new CommandTableXPlayer();
  
  setDecodertqStatus(_DECODER_STATUS_IDLE);
  appendCommandTable(ct);
  majorMode=_PLAYER_MAJOR_MODE_OFF;
  setRuntimeInfo(false);
  // As default we expect a user which does not want to know
  // about the player status.
  // This is *not* true if the user is a controlling GUI.
  // A GUI should alwas send :
  // "MajorModeInfo on"
  setMajorModeInfo(true);   // as default we don't inform about the state!
  isOn=false;
} 

InputDecoderXPlayer::~InputDecoderXPlayer(){
  delete ct;
  delete output;
}

void InputDecoderXPlayer::setMajorModeInfo(int lDisplayMajorMode) {
  this->lDisplayMajorMode=lDisplayMajorMode;
}

int InputDecoderXPlayer::getMajorModeInfo() {
  return lDisplayMajorMode;
}



int InputDecoderXPlayer::getOn() {
  return isOn;
}

 
void InputDecoderXPlayer::setOn(int lOn) {
  isOn=lOn;
}




  
void InputDecoderXPlayer::setMajorMode(int mode) {
  const char* ptr;
  majorMode=mode;

  if (lDisplayMajorMode==false) {
    cout << "lDisplayMajorMode false"<<endl;
    return;
  }
  output->lock();
  output->clearBuffer();
  output->appendBuffer("Command:0 Msg:player-status ");
  if (majorMode == _PLAYER_MAJOR_MODE_OFF) {
    ptr="off";
  } else if (majorMode == _PLAYER_MAJOR_MODE_ON) {
    ptr="on";
  } else if (majorMode == _PLAYER_MAJOR_MODE_OPEN_TRACK) {
    ptr="open";
  } else if (majorMode == _PLAYER_MAJOR_MODE_CLOSE_TRACK) {
    ptr="close";
  } else if (majorMode == _PLAYER_MAJOR_MODE_PLAYING) {
    ptr="playing";
  } else if (majorMode == _PLAYER_MAJOR_MODE_PAUSE) {            
    ptr="pause";
  } else {
    ptr="unknown";
  }
  output->appendBuffer(ptr);
  if (majorMode == _PLAYER_MAJOR_MODE_OFF) {
    char val[40];
    long bytes=yafOutput->getBytesCounter();
    long allWrite=yafOutput->getAllWriteCounter();
    
    snprintf(val,40,"%ld %ld",bytes,allWrite);
    output->appendBuffer(" ");
    output->appendBuffer(val);
  }
  output->flushBuffer();  
  output->unlock();
}



int InputDecoderXPlayer::getMajorMode() {
  return majorMode;
}



void InputDecoderXPlayer::doSomething(){
  DEBUG << "Decoder did something" << endl;
  // after decoding is ready we close the file

  InputDecoder::doSomething();
}



const char* InputDecoderXPlayer::processCommand(int command,const char* args){

  if (command == _PLAYER_OFF) {
    if (isOn == true) {
      isOn=false;
      processCommand(_PLAYER_PAUSE,"");
      processCommand(_PLAYER_CLOSE,"");
     
      setMajorMode(_PLAYER_MAJOR_MODE_OFF);
      yafOutput->setBytesCounter(0);
    }
    return"";
  }
  
  if (command == _PLAYER_ON) {
    if (isOn == false) {
      setMajorMode(_PLAYER_MAJOR_MODE_ON);
      isOn=true;
    }
    return"";
  }


  if (command == _PLAYER_OPEN) {
    setMajorMode(_PLAYER_MAJOR_MODE_OPEN_TRACK);
    return"";
  }
  
  if (command == _PLAYER_CLOSE) {
    setMajorMode(_PLAYER_MAJOR_MODE_CLOSE_TRACK);
    return"";
  }
 
  if (command == _PLAYER_PLAY) {
    setDecodertqStatus(_DECODER_STATUS_WORKING);
    setMajorMode(_PLAYER_MAJOR_MODE_PLAYING);
    return"";
  }
 
  if (command == _PLAYER_PAUSE) {
    setMajorMode(_PLAYER_MAJOR_MODE_PAUSE);
    return"";
  }   

  if (command == _PLAYER_VERBOSE) {
    if (strcmp(args,"off")==0) {
      setMajorModeInfo(false); 
    } else {
      setMajorModeInfo(true); 
    }
    return"";
  } 

  if (command == _PLAYER_OUTPUTFILE) {
    int ret;
    if (yafOutput->isOpenStream() == true) {
      return "already output file selected";
    }    
    yafOutput->setStreamFile(args);
    // now we have set the fifo. But we must say the parent
    // process that we start with waiting

    cout << "Command:0 Msg:fileopen before"<<endl;
    ret=yafOutput->openStream();
    cout << "Command:0 Msg:fileopen after"<<endl;

    if (ret < 0) {
      return "cannot open outfile";
    }
    return "";
  }
  if (command == _PLAYER_CLOSEOUTPUTFILE) {
    if (yafOutput->isOpenStream() == false) {
      return "no output file selected";
    }
    yafOutput->closeStream();
    return "";
  }

  if (command == _PLAYER_INTERNALAUDIO) {
    if (strcmp("on",args)==0) {
      yafOutput->internalDevice(true);
      return "";
    }
    yafOutput->internalDevice(false);
    return "";
  }
   
  if (command == _PLAYER_SLEEP) {
    int nSec;
    sscanf(args,"%d",&nSec);  // convert string to int
    sleep(nSec);
    return "";
  }
  if (command == _PLAYER_CLEAR) {
    yafOutput->setBytesCounter(0);
    return "";
  }
    
  return InputDecoder::processCommand(command,args);
}