houxiao
2016-12-26 e53992ed5cc1615ac99ac3ba2146de0175d3c770
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
#include "PL_RTSPServer.h"
 
#include <liveMedia.hh>
#include <BasicUsageEnvironment.hh>
 
class MyH264FramedSource;
 
struct RTSPServer_Internal
{
    uint8_t* buffer;
    size_t buffSize;
 
    bool payError;
    pthread_t live_daemon_thid;
    pthread_mutex_t* frame_mutex;
    bool live_daemon_running;
    
    UsageEnvironment* env;
 
    // To make the second and subsequent client for each stream reuse the same
    // input stream as the first client (rather than playing the file from the
    // start for each client), change the following "False" to "True":
    Boolean reuseFirstSource;
 
    // To stream *only* MPEG-1 or 2 video "I" frames
    // (e.g., to reduce network bandwidth),
    // change the following "False" to "True":
    Boolean iFramesOnly;
    
    UserAuthenticationDatabase* authDB;
 
    RTSPServer* rtspServer;//#todo delete
    
    char descriptionString[1024];
    
    MyH264FramedSource* pMyH264FramedSource;
    
    RTSPServer_Internal() : 
        buffer(nullptr), buffSize(0), 
        payError(true), live_daemon_thid(0), frame_mutex(nullptr), live_daemon_running(false), 
        env(nullptr), reuseFirstSource(False), iFramesOnly(False), authDB(nullptr), 
        rtspServer(nullptr)
    {
        pthread_mutex_init(frame_mutex, NULL);
    }
    
    ~RTSPServer_Internal()
    {
        if (frame_mutex != nullptr)
        {
            pthread_mutex_destroy(frame_mutex);
            delete frame_mutex;
            frame_mutex = nullptr;
        }
    }
    
    void reset()
    {
        buffer = nullptr;
        buffSize = 0;
 
        payError = true;
 
        if (frame_mutex != nullptr)
        {
            pthread_mutex_destroy(frame_mutex);
            delete frame_mutex;
            frame_mutex = nullptr;
        }
        
        frame_mutex = new pthread_mutex_t;
        pthread_mutex_init(frame_mutex, NULL);
 
        live_daemon_thid = 0;
        live_daemon_running = false;
        
        env = nullptr;
        reuseFirstSource = False;
        iFramesOnly = False;
        authDB = nullptr; 
        rtspServer = nullptr;
        
        strcpy(descriptionString, "Session streamed by \"testOnDemandRTSPServer\"");
        
        pMyH264FramedSource = nullptr;
    }
};
 
 
class MyH264FramedSource : public FramedSource
{
public:
    static MyH264FramedSource* createNew(UsageEnvironment& _env, RTSPServer_Internal& _in)
    {
        return new MyH264FramedSource(_env, _in);
    }
    
    // deliver frame to the sink
    bool deliverFrame()
    {
        int ret = false;
        if (isCurrentlyAwaitingData()) 
        {
            fDurationInMicroseconds = 0;
            fFrameSize = 0;
            
            if (in.buffSize > fMaxSize) 
            {
                fFrameSize = fMaxSize;
                fNumTruncatedBytes = in.buffSize - fMaxSize;
            } 
            else 
            {
                fFrameSize = in.buffSize;
            }
            
            if (fFrameSize > 0)
            {
                memcpy(fTo, in.buffer, fFrameSize);
                
                int ret = pthread_mutex_unlock(in.frame_mutex);
                if(ret != 0)
                {
                    printf("pthread_mutex_unlock frame_mutex: %s/n", strerror(ret));
                    return false;
                }
                
                ret = true;
            }
        }
 
        return ret;
    }
 
protected:
    MyH264FramedSource(UsageEnvironment& _env, RTSPServer_Internal& _in) : 
        FramedSource(_env), env(_env), in(_in)
    {
    }
    
    virtual ~MyH264FramedSource()
    {
    }
 
    // overide FramedSource
    virtual void doGetNextFrame()
    {
        printf("MyH264FramedSource::doGetNextFrame\n");
        
        int ret = pthread_mutex_lock(in.frame_mutex);
        if(ret != 0)
        {
            printf("pthread_mutex_lock frame_mutex: %s/n", strerror(ret));
            return;
        }
 
        // deliverFrame
        //if (fFrameSize > 0)
        //{
            // send Frame to the consumer
            FramedSource::afterGetting(this);            
        //}
    }
    
    virtual void doStopGettingFrames()
    {
        FramedSource::doStopGettingFrames();
    }
    
private:
    UsageEnvironment& env;
    RTSPServer_Internal& in;
};
 
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;
}
 
void* live_daemon_thd(void* arg)
{
    RTSPServer_Internal* in = (RTSPServer_Internal*)arg;
    
    // Begin by setting up our usage environment:
    TaskScheduler* scheduler = BasicTaskScheduler::createNew();
    in->env = BasicUsageEnvironment::createNew(*scheduler);
 
#ifdef ACCESS_CONTROL
    // To implement client access control to the RTSP server, do the following:
    in->authDB = new UserAuthenticationDatabase;
    in->authDB->addUserRecord("username1", "password1"); // replace these with real strings
    // Repeat the above with each <username>, <password> that you wish to allow
    // access to the server.
#endif
 
    // Create the RTSP server:
    in->rtspServer = RTSPServer::createNew(*(in->env), 8554, in->authDB);
    if (in->rtspServer == NULL)
    {
        *(in->env) << "Failed to create RTSP server: " << in->env->getResultMsg() << "\n";
        return nullptr;
    }
 
    // Set up each of the possible streams that can be served by the
    // RTSP server.  Each such stream is implemented using a
    // "ServerMediaSession" object, plus one or more
    // "ServerMediaSubsession" objects for each audio/video substream.
    
    char const* streamName = "plH264Encoder";
    ServerMediaSession* sms = ServerMediaSession::createNew(*(in->env), streamName, streamName, in->descriptionString);
    in->pMyH264FramedSource = MyH264FramedSource::createNew(*(in->env), *in);
    sms->addSubsession(in->pMyH264FramedSource);
    in->rtspServer->addServerMediaSession(sms);
    
    // announceStream
    char* url = in->rtspServer->rtspURL(sms);
    *(in->env) << "Play this stream using the URL " << url << "\n";
    delete[] url;
    
    // Also, attempt to create a HTTP server for RTSP-over-HTTP tunneling.
    // Try first with the default HTTP port (80), and then with the alternative HTTP
    // port numbers (8000 and 8080).
 
    if (in->rtspServer->setUpTunnelingOverHTTP(80))
        *(in->env) << "\n(We use port " << in->rtspServer->httpServerPortNum() << " for optional RTSP-over-HTTP tunneling.)\n";
    else
        *(in->env) << "\n(RTSP-over-HTTP tunneling is not available.)\n";
 
    in->live_daemon_running = true;
    in->env->taskScheduler().doEventLoop(); // does not return
    in->live_daemon_running = false;
}
 
bool PL_RTSPServer::init(void* args)
{
    RTSPServer_Internal* in = (RTSPServer_Internal*)internal;
    in->reset();
    
    int ret = pthread_mutex_lock(in->frame_mutex);
    if(ret != 0)
    {
        printf("pthread_mutex_lock frame_mutex: %s/n", strerror(ret));
        return false;
    }
    
    ret = pthread_create(&(in->live_daemon_thid), NULL, live_daemon_thd, in);
    if(ret != 0)
    {
        printf("pthread_create: %s/n", strerror(ret));
        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;
 
    in->buffer = pm.buffer;
    in->buffSize = pm.buffSize;
    
    return in->pMyH264FramedSource->deliverFrame();
}
 
bool PL_RTSPServer::gain(PipeMaterial& pm)
{
    RTSPServer_Internal* in = (RTSPServer_Internal*)internal;
 
    pm.buffer = nullptr;
    pm.buffSize = 0;
    pm.former = this;
    return true;
}