summaryrefslogtreecommitdiff
path: root/streamdevice.c
blob: 48cfa22b3c82486ac4b2d2e8fd5c5e0d1735baeb (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
240
/* 
 * streamdevice.c: streaming network device
 *
 * See the README file for copyright information and how to reach the author.
 *
 */
 
#include "vdr/player.h"
#include "streamdevice.h"
#include "osdworker.h"
#include "tsworker.h"
#include "netosd.h"
#include "clientcontrol.h"

cStreamDevice::cStreamDevice(void)
{
    dsyslog("[ffnetdev] Device: Constructor cStreamDevice \n");
    //m_Remux = new cPES2TSRemux(TS_VPID, TS_APID);   
    m_Remux = new cPES2PESRemux();  
    m_Playing = false; 
    m_PlayState = psPlay;
    m_PlayMode  = pmNone;
}

cStreamDevice::~cStreamDevice(void)
{
  dsyslog("[ffnetdev] Device: Destructor cStreamDevice \n");
  m_PlayState = psPlay;
  cOSDWorker::Exit();
  cTSWorker::Exit();
  cClientControl::Exit();
  DELETENULL(m_Remux);
}


void cStreamDevice::MakePrimaryDevice(bool On)
{
  dsyslog("[ffnetdev] Device: ffnetdev becomes primary device. Registering our OSD provider...\n");
  new cNetOSDProvider();
}

int cStreamDevice::ProvidesCa(const cChannel *Channel) const
{
   return 0;
}

bool cStreamDevice::HasDecoder(void) const
{
   return true; // We can decode MPEG2
}

bool cStreamDevice::CanReplay(void) const
{
   return true;  // We can replay
}

bool cStreamDevice::SetPlayMode(ePlayMode PlayMode)
{
   dsyslog("[ffnetdev] Device: Setting playmode. Mode: %d\n",PlayMode);
   cOSDWorker::SendPlayMode(PlayMode);
      
   m_PlayMode = PlayMode;
   if (PlayMode == pmNone)
   {
      m_PlayState = psBufferReset;
      m_Remux->ClearInput();
      m_Remux->ClearOutput();
   }
   else
   {
      while (((m_PlayState == psBufferReset) || (m_PlayState == psBufferReseted)) && 
             (!cClientControl::PlayStateReq()) && (cTSWorker::HaveStreamClient()))
         cCondWait::SleepMs(10);
      dsyslog("[ffnetdev] PlayStateReq\n");   
      m_PlayState = psPlay;
   }
   
   cControl *pControl = cControl::Control();
   if (pControl)
   {
      pControl->GetReplayMode(m_Playing, m_Forward, m_Speed);
      if (m_Speed == -1)
         m_Speed = 0;
      if (!cClientControl::SendPlayState(PlayMode, m_Playing, m_Forward, m_Speed))
         m_PlayState = psPlay;
   }
   else
   {
      m_Playing = false;
      m_Forward = true;
      m_Speed = 0;
      if (!cClientControl::SendPlayState(PlayMode, m_Playing, m_Forward, m_Speed))
         m_PlayState = psPlay;
   }
   return true;
}

void cStreamDevice::TrickSpeed(int Speed)
{
   dsyslog("[ffnetdev] Device: Trickspeed. Speed: %d\n", Speed);
     
   cControl *pControl = cControl::Control();
   if (pControl)
      pControl->GetReplayMode(m_Playing, m_Forward, m_Speed);
   
   m_Speed = Speed;
   if (!cClientControl::SendPlayState(m_PlayMode, m_Playing, m_Forward, m_Speed))
      m_PlayState = psPlay;
}

void cStreamDevice::Clear(void)
{
   dsyslog("[ffnetdev] Device: Clear\n");

// workaround for clearing in vdrviewer (speed-change resets the buffer)
   if (!cClientControl::SendPlayState(m_PlayMode, m_Playing, m_Forward, 1))
      m_PlayState = psPlay;

   if (!cClientControl::SendPlayState(m_PlayMode, m_Playing, m_Forward, m_Speed))
      m_PlayState = psPlay;

//    cDevice::Clear();
}
void cStreamDevice::Play(void)
{
   dsyslog("[ffnetdev] Device: Play.\n");
   
   m_PlayMode = pmAudioVideo;
   m_Playing = true;
   m_Forward = true;
   m_Speed = 0;
   if (!cClientControl::SendPlayState(m_PlayMode, m_Playing, m_Forward, m_Speed))
      m_PlayState = psPlay;
    
//    cDevice::Play();
}

void cStreamDevice::Freeze(void)
{
    dsyslog("[ffnetdev] Device: Freeze.\n");
    
    m_Playing = false;
    cClientControl::SendSFreeze();
      
//    cDevice::Freeze();
}

void cStreamDevice::Mute(void)
{
   dsyslog("[ffnetdev] Device: Mute.\n");
   
   m_PlayMode = pmVideoOnly;
   if (!cClientControl::SendPlayState(m_PlayMode, m_Playing, m_Forward, m_Speed))
      m_PlayState = psPlay;
      
//    cDevice::Mute();
}

void cStreamDevice::SetVolumeDevice(int Volume)
{
  dsyslog("[ffnetdev] Device: Setting volume to %d (not implemented).\n", Volume);
}

void cStreamDevice::StillPicture(const uchar *Data, int Length)
{
   dsyslog("[ffnetdev] Device: StillPicture %d Bytes.\n", Length);
   cClientControl::SendStillPicture(Data, Length);
}

bool cStreamDevice::Poll(cPoller &Poller, int TimeoutMs)
{
   //dsyslog("[ffnetdev] Device: Poll TimeoutMs: %d ....\n",TimeoutMs);
   return true;
}
/* ----------------------------------------------------------------------------
 */
#if VDRVERSNUM < 10342
int cStreamDevice::PlayAudio(const uchar *Data, int Length)
#else
int cStreamDevice::PlayAudio(const uchar *Data, int Length, uchar Id)
#endif
{
   if (cTSWorker::HaveStreamClient()) 
   {
       while ((((!m_Playing) && (m_Remux->InputFree() < Length)) || 
               ((m_Playing) && ((m_Remux->InputFree() < Length) || 
                                (m_Remux->Fill() > TCP_SEND_SIZE * 10)))) && 
               (cTSWorker::HaveStreamClient()))
           cCondWait::SleepMs(1);
       int result=m_Remux->Put(Data, Length);
       if (result!=Length) {
         dsyslog("[ffnetdev] Device: Did not put all in input buffer(audio). result:%d Length: %d Skipping Audio PES packet...\n", result, Length );
         // Delete part of data already written to buffer 
         m_Remux->DelInput(result);
         return (0);
        }
        else
	{
            return ( Length );
	}
   }
   else
   {
      m_Remux->ClearInput();
//      usleep(10000);
      return ( Length );
   }
  
}

/* ----------------------------------------------------------------------------
 */
int cStreamDevice::PlayVideo(const uchar *Data, int Length)
{
   if (cTSWorker::HaveStreamClient()) 
   {
       while ((((!m_Playing) && (m_Remux->InputFree() < Length)) || 
               ((m_Playing) && ((m_Remux->InputFree() < Length) || 
                                (m_Remux->Fill() > TCP_SEND_SIZE * 10)))) && 
               (cTSWorker::HaveStreamClient()))
           cCondWait::SleepMs(1);
       int result=m_Remux->Put(Data, Length);
       if (result!=Length) {
         dsyslog("[ffnetdev] Device: Did not put all in input buffer(video). result:%d Length: %d Skipping Video PES packet...\n", result, Length );
         // Delete part of data already written to buffer 
         m_Remux->DelInput(result);
         return (0);
       }
       else
       {
           return ( Length );
       }
   }
   else
   {
      m_Remux->ClearInput();
//      usleep(10000);
      return ( Length );
   }
}