houxiao
2017-01-06 d4109b2cef809daba0f95f244029456613383f01
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
#include "PL_RTSPClient.h"
#include "MaterialBuffer.h"
#include "logger.h"
#include <pthread.h>
 
void rtsp_client_sdp_callback(void* arg, const char* val);
void rtsp_client_fmtp_callback(void* arg, const char* val);
void rtsp_client_frame_callback(void* arg, uint8_t* buffer, size_t buffSize, timeval presentationTime);
void rtsp_client_continue_callback(void* arg);
//struct PL_RTSPClient_Config;
#include "live555/testProgs/testRTSPClient.hpp"
 
struct RTSPClient_Internal
{
    PL_RTSPClient_Config rtspConfig;
    pthread_t live_daemon_thid;
    char eventLoopWatchVariable;
    bool live_daemon_running;
    pthread_mutex_t* frame_mutex;
    pthread_mutex_t* continue_mutex;
    
    MB_Frame lastFrame;
    
    RTSPClient_Internal() : 
        rtspConfig(), live_daemon_thid(0), 
        eventLoopWatchVariable(0), live_daemon_running(false), 
        frame_mutex(new pthread_mutex_t), continue_mutex(new pthread_mutex_t), 
        lastFrame()
    {
        pthread_mutex_init(frame_mutex, NULL);
        pthread_mutex_init(continue_mutex, NULL);
    }
    
    ~RTSPClient_Internal()
    {
        if (frame_mutex != nullptr)
        {
            pthread_mutex_destroy(frame_mutex);
            delete frame_mutex;
            frame_mutex = nullptr;
        }
        
        if (continue_mutex != nullptr)
        {
            pthread_mutex_destroy(continue_mutex);
            delete continue_mutex;
            continue_mutex = nullptr;
        }
    }
    
    void reset()
    {
        PL_RTSPClient_Config _rtspConfig;
        rtspConfig = _rtspConfig;
        live_daemon_thid = 0;
        eventLoopWatchVariable = 0;
        live_daemon_running = false;
 
        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);
        
        if (continue_mutex != nullptr)
        {
            pthread_mutex_destroy(continue_mutex);
            delete continue_mutex;
            continue_mutex = nullptr;
        }
        
        continue_mutex = new pthread_mutex_t;
        pthread_mutex_init(continue_mutex, NULL);
        
        MB_Frame _lastFrame;
        lastFrame = _lastFrame;
    }
};
 
static void* live_daemon_thd(void* arg)
{
    RTSPClient_Internal* in = (RTSPClient_Internal*)arg;
    
    TaskScheduler* scheduler = BasicTaskScheduler::createNew();
    UsageEnvironment* env = BasicUsageEnvironment::createNew(*scheduler);
 
    usage(*env, in->rtspConfig.progName.c_str());
    
    openURL(*env, in->rtspConfig);
    
    in->live_daemon_running = true;
    env->taskScheduler().doEventLoop(&(in->eventLoopWatchVariable));
    in->live_daemon_running = false;
}
 
PipeLineElem* create_PL_RTSPClient()
{
    return new PL_RTSPClient;
}
 
PL_RTSPClient::PL_RTSPClient() : internal(new RTSPClient_Internal)
{
}
 
PL_RTSPClient::~PL_RTSPClient()
{
    delete (RTSPClient_Internal*)internal;
    internal= nullptr;
}
 
bool PL_RTSPClient::init(void* args)
{
    if (args == nullptr)
        return false;
 
    const PL_RTSPClient_Config* config = reinterpret_cast<const PL_RTSPClient_Config*>(args);
    RTSPClient_Internal* in = (RTSPClient_Internal*)internal;
    in->reset();
    in->rtspConfig = *config;
    in->rtspConfig.args = this;
    
    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_mutex_lock(in->continue_mutex);
    if(ret != 0)
    {
        printf("pthread_mutex_lock continue_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_RTSPClient::finit()
{
    RTSPClient_Internal* in = (RTSPClient_Internal*)internal;
    
    in->eventLoopWatchVariable = 1;
    pthread_join(in->live_daemon_thid, NULL);
}
 
bool PL_RTSPClient::pay(const PipeMaterial& pm)
{
    RTSPClient_Internal* in = (RTSPClient_Internal*)internal;
    return in->live_daemon_running;
}
 
bool PL_RTSPClient::gain(PipeMaterial& pm)
{
    RTSPClient_Internal* in = (RTSPClient_Internal*)internal;
 
    int ret = pthread_mutex_unlock(in->continue_mutex);
    if(ret != 0)
    {
        printf("pthread_mutex_unlock continue_mutex: %s/n", strerror(ret));
        return false;
    }
 
    ret = pthread_mutex_lock(in->frame_mutex);
    if(ret != 0)
    {
        printf("pthread_mutex_lock: %s/n", strerror(ret));
        return false;
    }
    
    pm.type = PipeMaterial::PMT_FRAME;
    pm.buffer = &(in->lastFrame);
    pm.buffSize = 0;
    pm.former = this;
 
    return true;
}
 
void rtsp_client_sdp_callback(void* arg, const char* val)
{
    if (arg == nullptr || val == nullptr)
        return;
 
    PL_RTSPClient* client = (PL_RTSPClient*)arg;
 
    if (client->manager == nullptr)
        return;
    
    client->manager->set_param(PLGP_RTSP_SDP, val);
}
 
void rtsp_client_fmtp_callback(void* arg, const char* val)
{
    if (arg == nullptr || val == nullptr)
        return;
 
    PL_RTSPClient* client = (PL_RTSPClient*)arg;
 
    if (client->manager == nullptr)
        return;
    
    client->manager->set_param(PLGP_RTSP_FMTP, val);
}
 
void rtsp_client_frame_callback(void* arg, uint8_t* buffer, size_t buffSize, timeval presentationTime)
{
    if (arg == nullptr || buffer == nullptr || buffSize == 0)
        return;
 
    PL_RTSPClient* client = (PL_RTSPClient*)arg;
    RTSPClient_Internal* in = (RTSPClient_Internal*)(client->internal);
 
    in->lastFrame.type = MB_Frame::MBFT_H264_NALU;
    in->lastFrame.buffer = buffer;
    in->lastFrame.buffSize = buffSize;
    in->lastFrame.width = 0;
    in->lastFrame.height = 0;
    in->lastFrame.pts = presentationTime;
    
    int ret = pthread_mutex_unlock(in->frame_mutex);
    if(ret != 0)
    {
        printf("pthread_mutex_unlock frame_mutex: %s/n", strerror(ret));
    }
}
 
void rtsp_client_continue_callback(void* arg)
{
    if (arg == nullptr)
        return;
 
    PL_RTSPClient* client = (PL_RTSPClient*)arg;
    RTSPClient_Internal* in = (RTSPClient_Internal*)(client->internal);
 
    int ret = pthread_mutex_lock(in->continue_mutex);
    if(ret != 0)
    {
        printf("pthread_mutex_unlock continue_mutex: %s/n", strerror(ret));
    }
}