12024
|
1 /*
|
|
2 The mediastreamer library aims at providing modular media processing and I/O
|
|
3 for linphone, but also for any telephony application.
|
|
4 Copyright (C) 2001 Simon MORLAT simon.morlat@linphone.org
|
|
5
|
|
6 This library is free software; you can redistribute it and/or
|
|
7 modify it under the terms of the GNU Lesser General Public
|
|
8 License as published by the Free Software Foundation; either
|
|
9 version 2.1 of the License, or (at your option) any later version.
|
|
10
|
|
11 This library is distributed in the hope that it will be useful,
|
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
14 Lesser General Public License for more details.
|
|
15
|
|
16 You should have received a copy of the GNU Lesser General Public
|
|
17 License along with this library; if not, write to the Free Software
|
|
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
19 */
|
|
20
|
|
21
|
|
22 #include "mediastream.h"
|
|
23 #include "msvideosource.h"
|
|
24 #include "msavdecoder.h"
|
|
25 #include "msavencoder.h"
|
|
26 #include "msnosync.h"
|
|
27 #include "mssdlout.h"
|
|
28
|
|
29 #define USE_SDL
|
|
30
|
|
31 extern void create_duplex_rtpsession(RtpProfile *profile, int locport,char *remip,int remport,
|
|
32 int payload,int jitt_comp,
|
|
33 RtpSession **recvsend);
|
|
34
|
|
35 #define MAX_RTP_SIZE 5000
|
|
36
|
|
37 /* this code is not part of the library itself, it is part of the mediastream program */
|
|
38 void
|
|
39 video_stream_free (VideoStream * stream)
|
|
40 {
|
|
41 RtpSession *recvs, *sends;
|
|
42 if (stream->rtprecv != NULL)
|
|
43 {
|
|
44 recvs = ms_rtp_recv_get_session (MS_RTP_RECV (stream->rtprecv));
|
|
45 if (recvs != NULL)
|
|
46 {
|
|
47 rtp_session_destroy (recvs);
|
|
48 }
|
|
49 ms_filter_destroy (stream->rtprecv);
|
|
50 }
|
|
51 if (stream->rtpsend != NULL)
|
|
52 {
|
|
53 sends = ms_rtp_send_get_session (MS_RTP_SEND (stream->rtpsend));
|
|
54 if (sends != NULL && sends!=recvs)
|
|
55 {
|
|
56 rtp_session_destroy (sends);
|
|
57 }
|
|
58 ms_filter_destroy (stream->rtpsend);
|
|
59 }
|
|
60 if (stream->source != NULL)
|
|
61 ms_filter_destroy (stream->source);
|
|
62 if (stream->output != NULL)
|
|
63 ms_filter_destroy (stream->output);
|
|
64 if (stream->decoder != NULL)
|
|
65 ms_filter_destroy (stream->decoder);
|
|
66 if (stream->encoder != NULL)
|
|
67 ms_filter_destroy (stream->encoder);
|
|
68 if (stream->timer != NULL)
|
|
69 ms_sync_destroy (stream->timer);
|
|
70 g_free (stream);
|
|
71 }
|
|
72
|
|
73
|
|
74 VideoStream *
|
|
75 video_stream_start (RtpProfile *profile, int locport, char *remip, int remport,
|
|
76 int payload, int jitt_comp, gboolean show_local,
|
|
77 const gchar *source, const gchar *device)
|
|
78 {
|
|
79 VideoStream *stream = g_new0 (VideoStream, 1);
|
|
80 RtpSession *rtps, *rtpr;
|
|
81 PayloadType *pt;
|
|
82 gchar *format;
|
|
83 gint width = VIDEO_SIZE_CIF_W;
|
|
84 gint height = VIDEO_SIZE_CIF_H;
|
|
85 gfloat fps;
|
|
86
|
|
87 create_duplex_rtpsession(profile,locport,remip,remport,payload,jitt_comp,&rtpr);
|
|
88 rtp_session_enable_adaptive_jitter_compensation(rtpr,FALSE);
|
|
89 rtps=rtpr;
|
|
90 ms_trace("sending video to %s:%i", remip4, remport);
|
|
91
|
|
92 /* creates two rtp filters to recv send streams (remote part) */
|
|
93 rtp_session_max_buf_size_set(rtpr,MAX_RTP_SIZE);
|
|
94 stream->rtpsend = ms_rtp_send_new ();
|
|
95 if (remport>0) ms_rtp_send_set_session (MS_RTP_SEND (stream->rtpsend), rtps);
|
|
96
|
|
97
|
|
98 stream->rtprecv = ms_rtp_recv_new ();
|
|
99 ms_rtp_recv_set_session (MS_RTP_RECV (stream->rtprecv), rtpr);
|
|
100
|
|
101 pt=rtp_profile_get_payload(profile,payload);
|
|
102 if (pt==NULL){
|
|
103 g_error("videostream.c: undefined payload type.");
|
|
104 return NULL;
|
|
105 }
|
|
106 ms_trace("videostream.c: getting codecs for %s", pt->mime_type);
|
|
107
|
|
108 /* creates the filters */
|
|
109 stream->source = ms_filter_new_with_name(source);
|
|
110 if (stream->source == NULL){
|
|
111 g_error("videostream.c: failed to create video source %s.", source);
|
|
112 return NULL;
|
|
113 }
|
|
114
|
|
115 #ifdef USE_SDL
|
|
116 stream->output=ms_sdl_out_new();
|
|
117 #else
|
|
118 stream->output = MS_FILTER(ms_video_output_new ());
|
|
119 #endif
|
|
120 stream->encoder=ms_encoder_new_with_string_id(pt->mime_type);
|
|
121 g_message("Video encoder created: %x",stream->encoder);
|
|
122 stream->decoder=ms_decoder_new_with_string_id(pt->mime_type);
|
|
123 if ((stream->encoder==NULL) || (stream->decoder==NULL)){
|
|
124 /* big problem: we have not a registered codec for this payload...*/
|
|
125 video_stream_free(stream);
|
|
126 g_error("videostream.c: No codecs available for payload %i.",payload);
|
|
127 return NULL;
|
|
128 }
|
|
129
|
|
130 /* configure the filters */
|
|
131 ms_video_source_set_device(MS_VIDEO_SOURCE(stream->source), device);
|
|
132 ms_video_source_set_size(MS_VIDEO_SOURCE(stream->source), width, height);
|
|
133 ms_video_source_set_frame_rate(MS_VIDEO_SOURCE(stream->source), 8, 1);
|
|
134 fps = MS_VIDEO_SOURCE(stream->source)->frame_rate / MS_VIDEO_SOURCE(stream->source)->frame_rate_base;
|
|
135 format = ms_video_source_get_format(MS_VIDEO_SOURCE(stream->source));
|
|
136
|
|
137 ms_AVencoder_set_format (MS_AVENCODER (stream->encoder), format);
|
|
138 ms_AVencoder_set_width(MS_AVENCODER(stream->encoder), width);
|
|
139 ms_AVencoder_set_height(MS_AVENCODER(stream->encoder), height);
|
|
140 /* bitrate is based upon 30fps? adjust by our possibly lower framerate */
|
|
141 ms_AVencoder_set_bit_rate(MS_AVENCODER(stream->encoder), pt->normal_bitrate * 30 / fps );
|
|
142 ms_AVdecoder_set_format (MS_AVDECODER (stream->decoder), "YUV420P");
|
|
143 ms_AVdecoder_set_width(MS_AVDECODER(stream->decoder), width);
|
|
144 ms_AVdecoder_set_height(MS_AVDECODER(stream->decoder), height);
|
|
145 #ifdef USE_SDL
|
|
146 /* we suppose our decoder and pin1 of encoder always outputs YUV420P */
|
|
147 ms_sdl_out_set_format(MS_SDL_OUT(stream->output),"YUV420P");
|
|
148 #else
|
|
149 ms_video_output_set_size (MS_VIDEO_OUTPUT (stream->output), width, height);
|
|
150 #endif
|
|
151
|
|
152 /* and then connect all */
|
|
153 ms_filter_add_link (stream->source, stream->encoder);
|
|
154 ms_filter_add_link (stream->encoder, stream->rtpsend);
|
|
155
|
|
156 ms_filter_add_link (stream->rtprecv, stream->decoder);
|
|
157 ms_filter_add_link (stream->decoder, stream->output);
|
|
158 if (show_local)
|
|
159 ms_filter_add_link(stream->encoder,stream->output);
|
|
160
|
|
161 /* create the synchronisation source */
|
|
162 stream->timer = ms_timer_new();
|
|
163 ms_sync_attach (stream->timer, stream->source);
|
|
164 ms_sync_attach (stream->timer, stream->rtprecv);
|
|
165
|
|
166 /* and start */
|
|
167 ms_video_source_start(MS_VIDEO_SOURCE(stream->source));
|
|
168 ms_start (stream->timer);
|
|
169 stream->show_local=show_local;
|
|
170 return stream;
|
|
171 }
|
|
172
|
|
173
|
|
174
|
|
175 void
|
|
176 video_stream_stop (VideoStream * stream)
|
|
177 {
|
|
178
|
|
179 ms_stop (stream->timer);
|
|
180 ms_video_source_stop (MS_VIDEO_SOURCE(stream->source));
|
|
181 ms_sync_detach (stream->timer, stream->source);
|
|
182 ms_sync_detach (stream->timer, stream->rtprecv);
|
|
183
|
|
184 ms_filter_remove_links(stream->source,stream->encoder);
|
|
185 ms_filter_remove_links (stream->encoder,
|
|
186 stream->rtpsend);
|
|
187
|
|
188 ms_filter_remove_links (stream->rtprecv,
|
|
189 stream->decoder);
|
|
190 ms_filter_remove_links (stream->decoder,
|
|
191 stream->output);
|
|
192 if (stream->show_local) {
|
|
193 ms_filter_remove_links (stream->encoder,
|
|
194 stream->output);
|
|
195 }
|
|
196 video_stream_free (stream);
|
|
197 }
|
|
198
|
|
199
|
|
200 void video_stream_set_rtcp_information(VideoStream *st, const char *cname){
|
|
201 if (st->send_session!=NULL){
|
|
202 rtp_session_set_source_description(st->send_session,cname,NULL,NULL,NULL,NULL,"linphone-" LINPHONE_VERSION,
|
|
203 "This is free software (GPL) !");
|
|
204 }
|
|
205 }
|
|
206
|
|
207
|
|
208
|
|
209 VideoStream * video_preview_start(const gchar *source, const gchar *device){
|
|
210 VideoStream *stream = g_new0 (VideoStream, 1);
|
|
211 gchar *format;
|
|
212 gint width = VIDEO_SIZE_CIF_W;
|
|
213 gint height = VIDEO_SIZE_CIF_H;
|
|
214
|
|
215 /* creates the filters */
|
|
216 stream->source = ms_filter_new_with_name(source);
|
|
217 if (stream->source == NULL){
|
|
218 g_error("videostream.c: failed to create video source %s.", source);
|
|
219 return NULL;
|
|
220 }
|
|
221 #ifdef USE_SDL
|
|
222 stream->output=ms_sdl_out_new();
|
|
223 #else
|
|
224 stream->output = ms_video_output_new ();
|
|
225 #endif
|
|
226 /* configure the filters */
|
|
227 ms_video_source_set_device(MS_VIDEO_SOURCE(stream->source), device);
|
|
228 ms_video_source_set_size(MS_VIDEO_SOURCE(stream->source), width, height);
|
|
229 ms_video_source_set_frame_rate(MS_VIDEO_SOURCE(stream->source), 8, 1);
|
|
230 format = ms_video_source_get_format(MS_VIDEO_SOURCE(stream->source));
|
|
231
|
|
232 #ifdef USE_SDL
|
|
233 ms_sdl_out_set_format(MS_SDL_OUT(stream->output),format);
|
|
234 #else
|
|
235 ms_video_output_set_format(MS_VIDEO_OUTPUT(stream->output),format);
|
|
236 ms_video_output_set_size (MS_VIDEO_OUTPUT (stream->output), width, height);
|
|
237 ms_video_output_set_title(MS_VIDEO_OUTPUT(stream->output),"Linphone Video");
|
|
238 #endif
|
|
239 /* and then connect all */
|
|
240 ms_filter_add_link (stream->source, stream->output);
|
|
241 /* create the synchronisation source */
|
|
242 stream->timer = ms_timer_new();
|
|
243 ms_sync_attach (stream->timer, stream->source);
|
|
244
|
|
245 /* and start */
|
|
246 ms_video_source_start(MS_VIDEO_SOURCE(stream->source));
|
|
247 ms_start (stream->timer);
|
|
248
|
|
249 return stream;
|
|
250 }
|
|
251
|
|
252 void video_preview_stop(VideoStream *stream){
|
|
253 ms_stop (stream->timer);
|
|
254 ms_video_source_stop (MS_VIDEO_SOURCE(stream->source));
|
|
255 ms_sync_detach (stream->timer, stream->source);
|
|
256 ms_filter_remove_links(stream->source,stream->output);
|
|
257 video_stream_free(stream);
|
|
258 }
|