houxiao
2017-08-09 d9ffa50c7e8d6b8c3157690aef8e2a70af1d1695
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "PL_RTSPServer.h"
#include "MaterialBuffer.h"
#include "logger.h"
 
#include <liveMedia/liveMedia.hh>
#include <BasicUsageEnvironment/BasicUsageEnvironment.hh>
 
#include "FFmpegRTSPServer/IEncoder.h"
#include "FFmpegRTSPServer/LiveRTSPServer.h"
#include "FFmpegRTSPServer/H264FramedSource.h"
#include "FFmpegRTSPServer/LiveServerMediaSubsession.h"
#include "PreAllocBufferQueue.h"
#include "MediaHelper.h"
 
struct RTSPServer_Internal
{
    RTSPServerConfig config;
 
    pthread_t live_daemon_thid;
    bool live_daemon_running;
    
    MESAI::LiveRTSPServer* server;
 
    PreAllocBufferQueue* frameQueue;
    pthread_mutex_t* queue_mutex;
    pthread_mutex_t* queue_empty_mutex;
 
    bool auxLineSet;
 
    RTSPServer_Internal() : 
        config(),
        live_daemon_thid(0), live_daemon_running(false),
        server(nullptr),
        frameQueue(nullptr), queue_mutex(new pthread_mutex_t), queue_empty_mutex(new pthread_mutex_t), //#todo from config
        auxLineSet(false)
    {
        pthread_mutex_init(queue_mutex, NULL);
    }
    
    ~RTSPServer_Internal()
    {
        reset();
    }
    
    void reset()
    {
        RTSPServerConfig _config;
        config =_config;
 
        if (frameQueue != nullptr)
        {
            delete frameQueue;
            frameQueue = nullptr;
        }
 
        if (queue_mutex != nullptr)
        {
            pthread_mutex_destroy(queue_mutex);
            delete queue_mutex;
            queue_mutex = nullptr;
        }
        
        queue_mutex = new pthread_mutex_t;
        pthread_mutex_init(queue_mutex, NULL);
 
        if (queue_empty_mutex != nullptr)
        {
            pthread_mutex_destroy(queue_empty_mutex);
            delete queue_empty_mutex;
            queue_empty_mutex = nullptr;
        }
 
        queue_empty_mutex = new pthread_mutex_t;
        pthread_mutex_init(queue_empty_mutex, NULL);
 
        live_daemon_thid = 0;
        live_daemon_running = false;
        
        server = nullptr; //#todo delete
 
        auxLineSet = false;
    }
};
 
PipeLineElem* create_PL_RTSPServer()
{
    return new PL_RTSPServer;
}
 
PL_RTSPServer::PL_RTSPServer() : internal(new RTSPServer_Internal)
{
}
 
PL_RTSPServer::~PL_RTSPServer()
{
    delete (RTSPServer_Internal*)internal;
    internal = nullptr;
}
 
struct DeliverFrameCallback
{
    RTSPServer_Internal* in;
    PreAllocBufferQueue::Buffer* lastBuffer;
 
    DeliverFrameCallback(RTSPServer_Internal* _in)
            : in(_in) , lastBuffer(nullptr)
    {
    }
 
    ~DeliverFrameCallback()
    {
        if (lastBuffer != nullptr)
        {
            in->frameQueue->Release(lastBuffer);
            lastBuffer = nullptr;
        }
    }
 
    static bool deliverFrame(void* args, uint8_t*& buffer, size_t& buffSize, timeval& pts)
    {
        DeliverFrameCallback* _this = (DeliverFrameCallback*)args;
 
        if (_this->in->frameQueue->Empty())
        {
            int ret = pthread_mutex_lock(_this->in->queue_empty_mutex);
            if (ret != 0)
            {
                LOG_WARN << "pthread_mutex_lock queue_empty_mutex, ret=" << ret << std::endl;
            }
        }
 
        ScopeLocker<pthread_mutex_t>(_this->in->queue_mutex);
 
        if (_this->lastBuffer != nullptr)
        {
            // this can not happen
            _this->in->frameQueue->Release(_this->lastBuffer);
            _this->lastBuffer = nullptr;
        }
 
        _this->lastBuffer = _this->in->frameQueue->Dequeue();
        if (_this->lastBuffer == nullptr)
            return false;
 
        buffer = _this->lastBuffer->buffer;
        buffSize = _this->lastBuffer->buffSize;
 
        LOG_INFO << "DeliverFrameCallback buffSize=" << buffSize << LOG_ENDL;
        //static size_t f = 0;
        //static FILE *pFile = fopen("/data/bb.264", "wb");
        //fwrite(buffer, sizeof(char), buffSize, pFile);
        //if (++f > 30){
        //    fclose(pFile);
        //    exit(0);
        //}
 
        gettimeofday(&pts, NULL);
        return (_this->lastBuffer != nullptr);
    }
 
    static void releaseFrame(void* args)
    {
        DeliverFrameCallback* _this = (DeliverFrameCallback*)args;
 
        if (_this->lastBuffer != nullptr)
        {
            ScopeLocker<pthread_mutex_t>(_this->in->queue_mutex);
            _this->in->frameQueue->Release(_this->lastBuffer);
            _this->lastBuffer = nullptr;
        }
    }
};
 
static void* live_daemon_thd(void* arg)
{
    RTSPServer_Internal* in = (RTSPServer_Internal*)arg;
 
    in->server = new MESAI::LiveRTSPServer(nullptr, 8554, 8080);
 
    in->server->init();
 
    MESAI::H264FramedSource::FrameCallbacks cbs;
    cbs.args = new DeliverFrameCallback(in);//#todo delete
    cbs.deliverFrameCallback = DeliverFrameCallback::deliverFrame;
    cbs.releaseFrameCallback = DeliverFrameCallback::releaseFrame;
    in->server->framedSource = new MESAI::H264FramedSource(*in->server->env, cbs);
    
    in->live_daemon_running = true;
    in->server->run(); // does not return
    //#todo delete framedSource
    in->live_daemon_running = false;
}
 
bool PL_RTSPServer::init(void* args)
{
    RTSPServer_Internal* in = (RTSPServer_Internal*)internal;
 
    if (args)
    {
        RTSPServerConfig* config = (RTSPServerConfig*)args;
        in->config = *config;
    }
 
    PreAllocBufferQueue::Config qcfg;
    qcfg.multithreadSafe = false;
    qcfg.fullQueueDropFront = true;
    qcfg.fullQueueSync = false;
    qcfg.count = 32;
    qcfg.maxBuffSize = 100000;
    in->frameQueue = new PreAllocBufferQueue(qcfg);
 
    int ret = pthread_create(&(in->live_daemon_thid), NULL, live_daemon_thd, in);
    if(ret != 0)
    {
        LOG_ERROR << "pthread_create: " << strerror(ret) << std::endl;
        return false;
    }
 
    return true;
}
 
void PL_RTSPServer::finit()
{
    RTSPServer_Internal* in = (RTSPServer_Internal*)internal;
 
    pthread_join(in->live_daemon_thid, NULL);
}
 
bool PL_RTSPServer::pay(const PipeMaterial& pm)
{
    RTSPServer_Internal* in = (RTSPServer_Internal*)internal;
 
    if (pm.buffer == nullptr)
        return false;
    
    if (pm.type != PipeMaterial::PMT_FRAME)
    {
        LOG_ERROR << "PL_RTSPServer::pay only support PMT_FRAME" << std::endl;
        return false;
    }
 
    if (!in->auxLineSet)
    {
        std::string spsStr(this->manager->get_param(PLGP_ENC_SPS_B64));
        std::string ppsStr(this->manager->get_param(PLGP_ENC_PPS_B64));
 
        if (!spsStr.empty() && !ppsStr.empty())
        {
            MESAI::H264FramedSource* framedSource = dynamic_cast<MESAI::H264FramedSource*>(in->server->framedSource);
            framedSource->spsBase64 = spsStr;
            framedSource->ppsBase64 = ppsStr;
 
            in->auxLineSet = true;
        }
    }
 
    MB_Frame* frame = (MB_Frame*)pm.buffer;
    if (frame->buffer == nullptr || frame->buffSize == 0)
        return false;
 
    ScopeLocker<pthread_mutex_t>(in->queue_mutex);
    //if (in->frameQueue->Full())
    //    LOG_WARN << "PL_RTSPServer::pay may lost data" << std::endl;
 
    PreAllocBufferQueue::Buffer* qbuff = in->frameQueue->Enqueue();
    if (qbuff == nullptr)
    {
        LOG_WARN << "PL_RTSPServer::pay may lost data size=" << frame->buffSize << std::endl;
        int ret = pthread_mutex_unlock(in->queue_empty_mutex);
        if (ret != 0)
        {
            LOG_WARN << "pthread_mutex_unlock queue_empty_mutex, ret=" << ret << std::endl;
        }
        return false;
    }
 
    memcpy(qbuff->buffer, frame->buffer, frame->buffSize);
    qbuff->buffSize = frame->buffSize;
 
    //static size_t f = 0;
    //static FILE *pFile = fopen("/data/aa.264", "wb");
    //fwrite(qbuff->buffer, sizeof(char), frame->buffSize, pFile);
    //if (++f > 400){
    //    fclose(pFile);
    //    exit(0);
    //}
 
    int ret = pthread_mutex_unlock(in->queue_empty_mutex);
    if (ret != 0)
    {
        LOG_WARN << "pthread_mutex_unlock queue_empty_mutex, ret=" << ret << std::endl;
    }
    return true;
}
 
bool PL_RTSPServer::gain(PipeMaterial& pm)
{
    RTSPServer_Internal* in = (RTSPServer_Internal*)internal;
 
    pm.type = PipeMaterial::PMT_NONE;
    pm.buffer = nullptr;
    pm.buffSize = 0;
    pm.former = this;
    return true;
}