77#include " aom-av1.h"
88#include " rtp-payload.h"
99#include " rtp-profile.h"
10- #include " mpeg-ps.h"
11- #include " mpeg-ts.h"
10+ #include " rtsp-muxer.h"
1211#include < stdio.h>
1312#include < stdlib.h>
1413#include < string.h>
1514#include < assert.h>
1615#include < time.h>
1716
18- // #define RTP_VIDEO_WITH_PS
17+ #define RTP_VIDEO_WITH_PS
1918
2019#define RTP_LOST_PERCENT 5
2120
@@ -37,35 +36,21 @@ struct mov_rtp_test_stream_t
3736 int av;
3837 int object;
3938 int track;
40- int psi ;
39+ int mid ;
4140 int64_t dts;
42-
43- void * encoder;
44- void * decoder;
4541};
4642
4743struct mov_rtp_test_t
4844{
4945 struct mov_rtp_test_stream_t a, v;
5046
51- struct ps_muxer_t * psenc ;
52- struct ps_demuxer_t * psdec ;
47+ struct rtsp_muxer_t * muxer ;
48+ int rtp_ps_pid ;
5349};
5450
5551static unsigned char packet[8 * 1024 * 1024 ];
5652
57- static void * rtp_alloc (void * /* param*/ , int bytes)
58- {
59- static uint8_t buffer[2 * 1024 * 1024 + 4 ] = { 0 , 0 , 0 , 1 , };
60- assert (bytes <= sizeof (buffer) - 4 );
61- return buffer + 4 ;
62- }
63-
64- static void rtp_free (void * /* param*/ , void * /* packet*/ )
65- {
66- }
67-
68- static int rtp_encode_packet (void * param, const void *packet, int bytes, uint32_t timestamp, int /* flags*/ )
53+ static int rtp_encode_packet (void * param, int pid, const void * data, int bytes, uint32_t timestamp, int flags)
6954{
7055 struct mov_rtp_test_stream_t * ctx = (struct mov_rtp_test_stream_t *)param;
7156
@@ -76,41 +61,16 @@ static int rtp_encode_packet(void* param, const void *packet, int bytes, uint32_
7661 // return 0;
7762 // }
7863
79- int r = rtp_payload_decode_input (ctx->decoder , packet, bytes);
80- return r >= 0 ? 0 : r;
81- }
82-
83- static int rtp_decode_packet (void * param, const void *packet, int bytes, uint32_t timestamp, int flags)
84- {
85- struct mov_rtp_test_stream_t * ctx = (struct mov_rtp_test_stream_t *)param;
86- printf (" RTP Decode: [%s] timestamp: %u, bytes: %d\n " , ctx->av ? " V" : " A" , (unsigned int )timestamp, bytes);
87-
88- #if defined(RTP_VIDEO_WITH_PS)
89- if (ctx == &ctx->ctx ->v )
90- {
91- size_t r = ps_demuxer_input (ctx->ctx ->psdec , (const uint8_t *)packet, bytes);
92- assert (r == bytes);
93- return r;
94- }
95- #endif
96-
97- return 0 ;
98- }
99-
100- static int rtp_payload_codec_create (struct mov_rtp_test_stream_t * ctx, int payload, const char * encoding, uint16_t seq, uint32_t ssrc)
101- {
102- struct rtp_payload_t handler1;
103- handler1.alloc = rtp_alloc;
104- handler1.free = rtp_free;
105- handler1.packet = rtp_decode_packet;
106- ctx->decoder = rtp_payload_decode_create (payload, encoding, &handler1, ctx);
107-
108- struct rtp_payload_t handler2;
109- handler2.alloc = rtp_alloc;
110- handler2.free = rtp_free;
111- handler2.packet = rtp_encode_packet;
112- ctx->encoder = rtp_payload_encode_create (payload, encoding, seq, ssrc, &handler2, ctx);
113-
64+ // int r = rtp_payload_decode_input(ctx->decoder, packet, bytes);
65+ // return r >= 0 ? 0 : r;
66+
67+ uint8_t len[2 ];
68+ static FILE * fp = fopen (" 2.mp4.rtp" , " wb" );
69+ len[0 ] = bytes >> 8 ;
70+ len[1 ] = bytes;
71+ fwrite (len, 1 , 2 , fp);
72+ fwrite (data, 1 , bytes, fp);
73+ fflush (fp);
11474 return 0 ;
11575}
11676
@@ -154,21 +114,15 @@ static void onread(void* param, uint32_t track, const void* buffer, size_t bytes
154114 printf (" [V] pts: %s, dts: %s, diff: %03d/%03d, %d%s\n " , ftimestamp (pts, s_pts), ftimestamp (dts, s_dts), (int )(pts - v_pts), (int )(dts - v_dts), (int )n, flags ? " [I]" : " " );
155115 v_pts = pts;
156116 v_dts = dts;
157- #if defined(RTP_VIDEO_WITH_PS)
158- if (MOV_OBJECT_H264 == ctx->v .object || MOV_OBJECT_HEVC == ctx->v .object )
159- {
160- ctx->v .dts = dts;
161- ps_muxer_input (ctx->psenc , ctx->v .psi , (MOV_AV_FLAG_KEYFREAME &flags)?0x0001 :0 , pts, dts, s_packet, n);
162- return ;
163- }
164- #endif
165- assert (0 == rtp_payload_encode_input (ctx->v .encoder , s_packet, n, (unsigned int )dts));
117+ assert (0 == rtsp_muxer_input (ctx->muxer , ctx->v .mid , pts, dts, s_packet, n, (MOV_AV_FLAG_KEYFREAME & flags) ? 0x0001 : 0 ));
166118 }
167119 else if (ctx->a .track == track)
168120 {
169121 if (MOV_OBJECT_AAC == ctx->a .object )
170122 {
171123 n = mpeg4_aac_adts_save (&s_aac, bytes, s_packet, sizeof (s_packet));
124+ memcpy (s_packet + n, buffer, bytes);
125+ n += bytes;
172126 }
173127 else if (MOV_OBJECT_OPUS == ctx->a .object )
174128 {
@@ -182,7 +136,7 @@ static void onread(void* param, uint32_t track, const void* buffer, size_t bytes
182136 printf (" [A] pts: %s, dts: %s, diff: %03d/%03d, %d\n " , ftimestamp (pts, s_pts), ftimestamp (dts, s_dts), (int )(pts - a_pts), (int )(dts - a_dts), (int )n);
183137 a_pts = pts;
184138 a_dts = dts;
185- assert (0 == rtp_payload_encode_input (ctx->a . encoder , s_packet, n, ( unsigned int )dts ));
139+ assert (0 == rtsp_muxer_input (ctx->muxer , ctx-> a . mid , pts, dts, s_packet, n, 0 ));
186140 }
187141 else
188142 {
@@ -200,35 +154,40 @@ static void mov_video_info(void* param, uint32_t track, uint8_t object, int /*wi
200154 if (MOV_OBJECT_H264 == object)
201155 {
202156#if defined(RTP_VIDEO_WITH_PS)
203- ctx-> v . psi = ps_muxer_add_stream ( ctx->psenc , PSI_STREAM_H264 , NULL , 0 ) ;
204- #else
205- assert ( 0 == rtp_payload_codec_create (& ctx->v , 96 , " H264" , 0 , 0 ) );
157+ int pid = ctx->rtp_ps_pid ;
158+ #else
159+ int pid = rtsp_muxer_add_payload ( ctx->muxer , " RTP/AVP " , 90000 , 96 , " H264" , 0 , 0 , 0 , extra, bytes );
206160#endif
207- assert (bytes == mpeg4_avc_decoder_configuration_record_load ((const uint8_t *)extra, bytes, &s_avc));
161+ ctx->v .mid = rtsp_muxer_add_media (ctx->muxer , pid, RTP_PAYLOAD_H264 , extra, bytes);
162+ assert (bytes >= mpeg4_avc_decoder_configuration_record_load ((const uint8_t *)extra, bytes, &s_avc));
208163
209164 }
210165 else if (MOV_OBJECT_HEVC == object)
211166 {
212167#if defined(RTP_VIDEO_WITH_PS)
213- ctx-> v . psi = ps_muxer_add_stream ( ctx->psenc , PSI_STREAM_H265 , NULL , 0 ) ;
214- #else
215- assert ( 0 == rtp_payload_codec_create (& ctx->v , 96 , " H265" , 0 , 0 ) );
168+ int pid = ctx->rtp_ps_pid ;
169+ #else
170+ int pid = rtsp_muxer_add_payload ( ctx->muxer , " RTP/AVP " , 90000 , 96 , " H265" , 0 , 0 , 0 , extra, bytes );
216171#endif
172+ ctx->v .mid = rtsp_muxer_add_media (ctx->muxer , pid, RTP_PAYLOAD_H265 , extra, bytes);
217173 assert (bytes == mpeg4_hevc_decoder_configuration_record_load ((const uint8_t *)extra, bytes, &s_hevc));
218174 }
219175 else if (MOV_OBJECT_AV1 == object)
220176 {
221- assert (0 == rtp_payload_codec_create (&ctx->v , 96 , " AV1" , 0 , 0 ));
177+ int pid = rtsp_muxer_add_payload (ctx->muxer , " RTP/AVP" , 90000 , RTP_PAYLOAD_AV1X , " AV1X" , 0 , 0 , 0 , extra, bytes);
178+ ctx->v .mid = rtsp_muxer_add_media (ctx->muxer , pid, RTP_PAYLOAD_AV1X , extra, bytes);
222179 assert (bytes == aom_av1_codec_configuration_record_load ((const uint8_t *)extra, bytes, &s_av1));
223180 }
224181 else if (MOV_OBJECT_VP9 == object)
225182 {
226- assert (0 == rtp_payload_codec_create (&ctx->v , 96 , " VP9" , 0 , 0 ));
183+ int pid = rtsp_muxer_add_payload (ctx->muxer , " RTP/AVP" , 90000 , 96 , " VP9" , 0 , 0 , 0 , extra, bytes);
184+ ctx->v .mid = rtsp_muxer_add_media (ctx->muxer , pid, RTP_PAYLOAD_VP9 , extra, bytes);
227185 assert (bytes == webm_vpx_codec_configuration_record_load ((const uint8_t *)extra, bytes, &s_vpx));
228186 }
229187 else if (MOV_OBJECT_VP8 == object)
230188 {
231- assert (0 == rtp_payload_codec_create (&ctx->v , 96 , " VP8" , 0 , 0 ));
189+ int pid = rtsp_muxer_add_payload (ctx->muxer , " RTP/AVP" , 90000 , 96 , " VP8" , 0 , 0 , 0 , extra, bytes);
190+ ctx->v .mid = rtsp_muxer_add_media (ctx->muxer , pid, RTP_PAYLOAD_VP8 , extra, bytes);
232191 assert (bytes == webm_vpx_codec_configuration_record_load ((const uint8_t *)extra, bytes, &s_vpx));
233192 }
234193 else
@@ -237,7 +196,7 @@ static void mov_video_info(void* param, uint32_t track, uint8_t object, int /*wi
237196 }
238197}
239198
240- static void mov_audio_info (void * param, uint32_t track, uint8_t object, int /* channel_count*/ , int /* bit_per_sample*/ , int /* sample_rate*/ , const void * extra, size_t bytes)
199+ static void mov_audio_info (void * param, uint32_t track, uint8_t object, int /* channel_count*/ , int /* bit_per_sample*/ , int sample_rate, const void * extra, size_t bytes)
241200{
242201 struct mov_rtp_test_t * ctx = (struct mov_rtp_test_t *)param;
243202 ctx->a .track = track;
@@ -246,58 +205,35 @@ static void mov_audio_info(void* param, uint32_t track, uint8_t object, int /*ch
246205
247206 if (MOV_OBJECT_AAC == object)
248207 {
249- assert (0 == rtp_payload_codec_create (&ctx->a , 97 , " MP4A-LATM" , 0 , 0 ));
208+ #if defined(RTP_VIDEO_WITH_PS)
209+ int pid = ctx->rtp_ps_pid ;
210+ #else
211+ int pid = rtsp_muxer_add_payload (ctx->muxer , " RTP/AVP" , sample_rate, 97 , " MP4A-LATM" , 0 , 0 , 0 , extra, bytes);
212+ #endif
213+ ctx->a .mid = rtsp_muxer_add_media (ctx->muxer , pid, RTP_PAYLOAD_LATM , extra, bytes);
250214 assert (bytes == mpeg4_aac_audio_specific_config_load ((const uint8_t *)extra, bytes, &s_aac));
251215 }
252216 else if (MOV_OBJECT_OPUS == object)
253217 {
254- assert (0 == rtp_payload_codec_create (&ctx->a , 97 , " OPUS" , 0 , 0 ));
218+ int pid = rtsp_muxer_add_payload (ctx->muxer , " RTP/AVP" , sample_rate, 97 , " OPUS" , 0 , 0 , 0 , extra, bytes);
219+ ctx->a .mid = rtsp_muxer_add_media (ctx->muxer , pid, RTP_PAYLOAD_OPUS , extra, bytes);
255220 }
256221 else
257222 {
258223 assert (0 );
259224 }
260225}
261226
262- static void * ps_alloc (void * /* param*/ , size_t bytes)
263- {
264- static char s_buffer[2 * 1024 * 1024 ];
265- assert (bytes <= sizeof (s_buffer));
266- return s_buffer;
267- }
268-
269- static void ps_free (void * /* param*/ , void * /* packet*/ )
270- {
271- return ;
272- }
273-
274- static int ps_write (void * param, int stream, void * packet, size_t bytes)
275- {
276- struct mov_rtp_test_t * ctx = (struct mov_rtp_test_t *)param;
277- return rtp_payload_encode_input (ctx->v .encoder , packet, bytes, (unsigned int )ctx->v .dts );
278- }
279-
280- static int ps_onpacket (void * ps, int stream, int codecid, int flags, int64_t pts, int64_t dts, const void * data, size_t bytes)
281- {
282- printf (" PS Decode [V] pts: %08lu, dts: %08lu, bytes: %u, %s\n " , (unsigned long )pts, (unsigned long )dts, (unsigned int )bytes, flags ? " [I]" : " " );
283- return 0 ;
284- }
285-
286227void mov_rtp_test (const char * mp4)
287228{
288229 struct mov_rtp_test_t ctx;
289230 memset (&ctx, 0 , sizeof (ctx));
290231 ctx.a .ctx = &ctx;
291232 ctx.v .ctx = &ctx;
292-
233+ ctx.muxer = rtsp_muxer_create (rtp_encode_packet, &ctx);
234+
293235#if defined(RTP_VIDEO_WITH_PS)
294- struct ps_muxer_func_t handler;
295- handler.alloc = ps_alloc;
296- handler.write = ps_write;
297- handler.free = ps_free;
298- ctx.psenc = ps_muxer_create (&handler, &ctx);
299- ctx.psdec = ps_demuxer_create (ps_onpacket, &ctx);
300- assert (0 == rtp_payload_codec_create (&ctx.v , 96 , " MP2P" , 0 , 0 ));
236+ ctx.rtp_ps_pid = rtsp_muxer_add_payload (ctx.muxer , " RTP/AVP" , 90000 , 96 , " MP2P" , 0 , 0 , 0 , NULL , 0 );
301237#endif
302238
303239 FILE * fp = fopen (mp4, " rb" );
@@ -312,19 +248,7 @@ void mov_rtp_test(const char* mp4)
312248 {
313249 }
314250
315- if (ctx.a .decoder )
316- rtp_payload_decode_destroy (ctx.a .decoder );
317- if (ctx.a .encoder )
318- rtp_payload_encode_destroy (ctx.a .encoder );
319- if (ctx.v .decoder )
320- rtp_payload_decode_destroy (ctx.v .decoder );
321- if (ctx.v .encoder )
322- rtp_payload_encode_destroy (ctx.v .encoder );
323-
324- #if defined(RTP_VIDEO_WITH_PS)
325- ps_demuxer_destroy (ctx.psdec );
326- ps_muxer_destroy (ctx.psenc );
327- #endif
251+ rtsp_muxer_destroy (ctx.muxer );
328252 mov_reader_destroy (mov);
329253 fclose (fp);
330254}
0 commit comments