-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathrtsp_service.cpp
More file actions
249 lines (203 loc) · 5.53 KB
/
Copy pathrtsp_service.cpp
File metadata and controls
249 lines (203 loc) · 5.53 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <liveMedia.hh>
#include <BasicUsageEnvironment.hh>
#include <GroupsockHelper.hh>
#include <sys/types.h>
#include <sys/syscall.h>
#include "capture_slice.h"
#if 0
#else
static UsageEnvironment *_env = 0;
#define SINK_PORT 3030
#define VIDEO_WIDTH 320
#define VIDEO_HEIGHT 240
#define FRAME_PER_SEC 25
#define FPS FRAME_PER_SEC
#define KBITS 300
pid_t gettid()
{
return syscall(SYS_gettid);
}
// 使用 webcam + x264
class WebcamFrameSource : public FramedSource
{
capture_t *mp_capture;
int m_started;
void *mp_token;
public:
WebcamFrameSource (UsageEnvironment &env)
: FramedSource(env)
{
fprintf(stderr, "[%d] %s .... calling\n", gettid(), __func__);
mp_capture = capture_open("/dev/video0", VIDEO_WIDTH, VIDEO_HEIGHT, FPS, KBITS);
if (!mp_capture) {
fprintf(stderr, "%s: open /dev/video0 err\n", __func__);
exit(-1);
}
m_started = 0;
mp_token = 0;
}
~WebcamFrameSource ()
{
fprintf(stderr, "[%d] %s .... calling\n", gettid(), __func__);
if (m_started) {
envir().taskScheduler().unscheduleDelayedTask(mp_token);
}
if (mp_capture)
capture_close(mp_capture);
}
protected:
virtual void doGetNextFrame ()
{
if (m_started) return;
m_started = 1;
// 根据 fps, 计算等待时间
double delay = 1000.0 / FRAME_PER_SEC;
int to_delay = delay * 1000; // us
mp_token = envir().taskScheduler().scheduleDelayedTask(to_delay,
getNextFrame, this);
}
private:
static void getNextFrame (void *ptr)
{
((WebcamFrameSource*)ptr)->getNextFrame1();
}
void dump(const void *data, int len)
{
unsigned char *p = (unsigned char*)data;
for (int i = 0; i < len; i++) {
fprintf(stderr, "%02x ", p[i]);
}
fprintf(stderr, "\n");
}
void getNextFrame1 ()
{
void *outbuf;
int64_t pts;
int outlen = capture_next_slice(mp_capture, &outbuf, &pts);
if (outlen <= 0) {
m_started = 0;
fprintf(stderr, "fifo_ empty!!\n");
return;
}
// save outbuf
gettimeofday(&fPresentationTime, 0);
fFrameSize = outlen;
if (fFrameSize > fMaxSize) {
fNumTruncatedBytes = fFrameSize - fMaxSize;
fFrameSize = fMaxSize;
}
else {
fNumTruncatedBytes = 0;
}
memmove(fTo, outbuf, fFrameSize);
// notify
afterGetting(this);
m_started = 0;
}
};
class WebcamOndemandMediaSubsession : public OnDemandServerMediaSubsession
{
public:
static WebcamOndemandMediaSubsession *createNew (UsageEnvironment &env, FramedSource *source)
{
return new WebcamOndemandMediaSubsession(env, source);
}
protected:
WebcamOndemandMediaSubsession (UsageEnvironment &env, FramedSource *source)
: OnDemandServerMediaSubsession(env, True) // reuse the first source
{
fprintf(stderr, "[%d] %s .... calling\n", gettid(), __func__);
mp_source = source;
mp_sdp_line = 0;
}
~WebcamOndemandMediaSubsession ()
{
fprintf(stderr, "[%d] %s .... calling\n", gettid(), __func__);
if (mp_sdp_line) free(mp_sdp_line);
}
private:
static void afterPlayingDummy (void *ptr)
{
fprintf(stderr, "[%d] %s .... calling\n", gettid(), __func__);
// ok
WebcamOndemandMediaSubsession *This = (WebcamOndemandMediaSubsession*)ptr;
This->m_done = 0xff;
}
static void chkForAuxSDPLine (void *ptr)
{
WebcamOndemandMediaSubsession *This = (WebcamOndemandMediaSubsession *)ptr;
This->chkForAuxSDPLine1();
}
void chkForAuxSDPLine1 ()
{
//fprintf(stderr, "[%d] %s .... calling\n", gettid(), __func__);
if (mp_dummy_rtpsink->auxSDPLine())
m_done = 0xff;
else {
int delay = 100*1000; // 100ms
nextTask() = envir().taskScheduler().scheduleDelayedTask(delay,
chkForAuxSDPLine, this);
}
}
protected:
virtual const char *getAuxSDPLine (RTPSink *sink, FramedSource *source)
{
fprintf(stderr, "[%d] %s .... calling\n", gettid(), __func__);
if (mp_sdp_line) return mp_sdp_line;
mp_dummy_rtpsink = sink;
mp_dummy_rtpsink->startPlaying(*source, 0, 0);
//mp_dummy_rtpsink->startPlaying(*source, afterPlayingDummy, this);
chkForAuxSDPLine(this);
m_done = 0;
envir().taskScheduler().doEventLoop(&m_done);
mp_sdp_line = strdup(mp_dummy_rtpsink->auxSDPLine());
mp_dummy_rtpsink->stopPlaying();
return mp_sdp_line;
}
virtual RTPSink *createNewRTPSink(Groupsock *rtpsock, unsigned char type, FramedSource *source)
{
fprintf(stderr, "[%d] %s .... calling\n", gettid(), __func__);
return H264VideoRTPSink::createNew(envir(), rtpsock, type);
}
virtual FramedSource *createNewStreamSource (unsigned sid, unsigned &bitrate)
{
fprintf(stderr, "[%d] %s .... calling\n", gettid(), __func__);
bitrate = 500;
return H264VideoStreamFramer::createNew(envir(), new WebcamFrameSource(envir()));
}
private:
FramedSource *mp_source; // 对应 WebcamFrameSource
char *mp_sdp_line;
RTPSink *mp_dummy_rtpsink;
char m_done;
};
int main (int argc, char **argv)
{
// env
TaskScheduler *scheduler = BasicTaskScheduler::createNew();
_env = BasicUsageEnvironment::createNew(*scheduler);
// rtsp server
RTSPServer *rtspServer = RTSPServer::createNew(*_env, 8554);
if (!rtspServer) {
fprintf(stderr, "ERR: create RTSPServer err\n");
::exit(-1);
}
// add live stream
do {
WebcamFrameSource *webcam_source = 0;
ServerMediaSession *sms = ServerMediaSession::createNew(*_env, "webcam", 0, "Session from /dev/video0");
sms->addSubsession(WebcamOndemandMediaSubsession::createNew(*_env, webcam_source));
rtspServer->addServerMediaSession(sms);
char *url = rtspServer->rtspURL(sms);
*_env << "using url \"" << url << "\"\n";
delete [] url;
} while (0);
// run loop
_env->taskScheduler().doEventLoop();
return 1;
}
#endif//