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
|
/*
* File: livereceiver.cpp
* Author: savop
*
* Created on 4. Juni 2009, 13:28
*/
#include <vdr/thread.h>
#include <vdr/remux.h>
#include <vdr/device.h>
#include <vdr/channels.h>
#include <vdr/ringbuffer.h>
#include "livereceiver.h"
cLiveReceiver* cLiveReceiver::newInstance(cChannel* Channel, int Priority){
cDevice *Device = cDevice::GetDevice(Channel, Priority, true);
if(!Device){
ERROR("No matching device found to serve this channel!");
return NULL;
}
cLiveReceiver *Receiver = new cLiveReceiver(Channel, Device);
if(Receiver){
MESSAGE("Receiver for channel \"%s\" created successfully.", Channel->Name());
return Receiver;
}
else {
ERROR("Failed to create receiver!");
return NULL;
}
}
cLiveReceiver::cLiveReceiver(cChannel *Channel, cDevice *Device)
: cReceiver( Channel->GetChannelID(), 0, Channel->Vpid(), Channel->Apids(), Channel->Dpids(), Channel->Spids()),
mDevice(Device), mChannel(Channel){
this->mLiveBuffer = NULL;
this->mOutputBuffer = NULL;
this->mFrameDetector = NULL;
}
cLiveReceiver::~cLiveReceiver(void){
if(this->IsAttached())
this->Detach();
}
void cLiveReceiver::open(UpnpOpenFileMode){
this->mLiveBuffer = new cRingBufferLinear(RECEIVER_LIVEBUFFER_SIZE, RECEIVER_RINGBUFFER_MARGIN, true, "Live TV buffer");
this->mOutputBuffer = new cRingBufferLinear(RECEIVER_OUTPUTBUFFER_SIZE, RECEIVER_RINGBUFFER_MARGIN, true, "Streaming buffer");
this->mLiveBuffer->SetTimeouts(0, 100);
this->mOutputBuffer->SetTimeouts(0, 500);
this->mFrameDetector = new cFrameDetector(this->mChannel->Vpid(), this->mChannel->Vtype());
this->mPatPmtGenerator.SetChannel(this->mChannel);
this->mDevice->SwitchChannel(this->mChannel, false);
this->mDevice->AttachReceiver(this);
}
void cLiveReceiver::Activate(bool On){
if(On){
this->Start();
MESSAGE("Live receiver started.");
}
else {
if(this->Running()){
this->Cancel(2);
}
MESSAGE("Live receiver stopped");
}
}
void cLiveReceiver::Receive(uchar* Data, int Length){
if(this->Running()){
int bytesWrote = this->mLiveBuffer->Put(Data, Length);
if(bytesWrote != Length && this->Running()){
this->mLiveBuffer->ReportOverflow(Length - bytesWrote);
}
}
}
void cLiveReceiver::Action(void){
MESSAGE("Started buffering...");
while(this->Running()){
int bytesRead;
//MESSAGE("Buffer is filled with %d bytes", this->mLiveBuffer->Available());
uchar* bytes = this->mLiveBuffer->Get(bytesRead);
if(bytes){
int count = this->mFrameDetector->Analyze(bytes, bytesRead);
if(count){
//MESSAGE("%d bytes analyzed", count);
//MESSAGE("%2.2f FPS", this->mFrameDetector->FramesPerSecond());
if(!this->Running() && this->mFrameDetector->IndependentFrame())
break;
if(this->mFrameDetector->Synced()){
//MESSAGE("Frame detector synced to data stream");
if(this->mFrameDetector->IndependentFrame()){
this->mOutputBuffer->Put(this->mPatPmtGenerator.GetPat(), TS_SIZE);
int i = 0;
while(uchar* pmt = this->mPatPmtGenerator.GetPmt(i)){
this->mOutputBuffer->Put(pmt, TS_SIZE);
}
}
int bytesWrote = this->mOutputBuffer->Put(bytes, count);
if(bytesWrote != count){
this->mLiveBuffer->ReportOverflow(count - bytesWrote);
}
//MESSAGE("Wrote %d to output buffer", bytesWrote);
if(bytesWrote){
this->mLiveBuffer->Del(bytesWrote);
}
else {
cCondWait::SleepMs(100);
}
}
else {
ERROR("Cannot sync to stream");
}
}
}
}
MESSAGE("Receiver was detached from device");
}
int cLiveReceiver::read(char* buf, size_t buflen){
int bytesRead;
if(!this->IsAttached())
bytesRead = -1;
else {
while(!this->mOutputBuffer->Available()){
WARNING("No data, waiting...");
cCondWait::SleepMs(50);
if(!this->IsAttached()){
MESSAGE("Lost device...");
return 0;
}
}
uchar* buffer = this->mOutputBuffer->Get(bytesRead);
if(buffer){
if(buflen > (size_t)bytesRead){
memcpy(buf,(char*)buffer,bytesRead);
this->mOutputBuffer->Del(bytesRead);
}
else {
memcpy(buf,(char*)buffer,buflen);
this->mOutputBuffer->Del(buflen);
}
}
}
MESSAGE("Read %d bytes from live feed", bytesRead);
return bytesRead;
}
int cLiveReceiver::seek(off_t, int){
ERROR("Seeking not supported on broadcasts");
return 0;
}
int cLiveReceiver::write(char*, size_t){
ERROR("Writing not allowed on broadcasts");
return 0;
}
void cLiveReceiver::close(){
MESSAGE("Closing live receiver");
this->Detach();
delete this->mOutputBuffer; this->mOutputBuffer = NULL;
delete this->mLiveBuffer; this->mLiveBuffer = NULL;
this->mFrameDetector = NULL;
MESSAGE("Live receiver closed.");
}
|