1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

refine examples of srs-librtmp, add srs_print_rtmp_packet. 2.0.28.

This commit is contained in:
winlin 2014-11-21 13:48:57 +08:00
parent a058eeeb20
commit d9474d7600
8 changed files with 284 additions and 101 deletions

View file

@ -936,6 +936,161 @@ int srs_parse_timestamp(
return ret;
}
char srs_get_codec_id(char* data, int size)
{
if (size < 1) {
return 0;
}
char codec_id = data[0];
codec_id = codec_id & 0x0F;
return codec_id;
}
const char* srs_code_id2string(char codec_id)
{
static const char* h263 = "H.263";
static const char* screen = "Screen";
static const char* vp6 = "VP6";
static const char* vp6_alpha = "VP6Alpha";
static const char* screen2 = "Screen2";
static const char* h264 = "H.264";
static const char* unknown = "Unknown";
switch (codec_id) {
case 2: return h263;
case 3: return screen;
case 4: return vp6;
case 5: return vp6_alpha;
case 6: return screen2;
case 7: return h264;
default: return unknown;
}
return unknown;
}
char srs_get_avc_packet_type(char* data, int size)
{
if (size < 2) {
return -1;
}
if (!SrsFlvCodec::video_is_h264(data, size)) {
return -1;
}
u_int8_t avc_packet_type = data[1];
if (avc_packet_type > 2) {
return -1;
}
return avc_packet_type;
}
const char* srs_avc_packet2string(char avc_packet_type)
{
static const char* sps_pps = "SpsPps";
static const char* nalu = "Nalu";
static const char* sps_pps_end = "SpsPpsEnd";
static const char* unknown = "Unknown";
switch (avc_packet_type) {
case 0: return sps_pps;
case 1: return nalu;
case 2: return sps_pps_end;
default: return unknown;
}
return unknown;
}
char srs_get_frame_type(char* data, int size)
{
if (size < 1) {
return -1;
}
if (!SrsFlvCodec::video_is_h264(data, size)) {
return -1;
}
u_int8_t frame_type = data[0];
frame_type = (frame_type >> 4) & 0x0f;
if (frame_type < 1 || frame_type > 5) {
return -1;
}
return frame_type;
}
const char* srs_frame_type2string(char frame_type)
{
static const char* keyframe = "I";
static const char* interframe = "P/B";
static const char* disposable_interframe = "DI";
static const char* generated_keyframe = "GI";
static const char* video_infoframe = "VI";
static const char* unknown = "Unknown";
switch (frame_type) {
case 1: return keyframe;
case 2: return interframe;
case 3: return disposable_interframe;
case 4: return generated_keyframe;
case 5: return video_infoframe;
default: return unknown;
}
return unknown;
}
int srs_print_rtmp_packet(char type, u_int32_t timestamp, char* data, int size)
{
int ret = ERROR_SUCCESS;
u_int32_t pts;
if (srs_parse_timestamp(timestamp, type, data, size, &pts) != 0) {
return ret;
}
if (type == SRS_RTMP_TYPE_VIDEO) {
srs_lib_trace("Video packet type=%s, dts=%d, pts=%d, size=%d, %s(%s,%s)",
srs_type2string(type), timestamp, pts, size,
srs_code_id2string(srs_get_codec_id(data, size)),
srs_avc_packet2string(srs_get_avc_packet_type(data, size)),
srs_frame_type2string(srs_get_frame_type(data, size))
);
} else if (type == SRS_RTMP_TYPE_AUDIO) {
srs_lib_trace("Audio packet type=%s, dts=%d, pts=%d, size=%d",
srs_type2string(type), timestamp, pts, size);
} else if (type == SRS_RTMP_TYPE_SCRIPT) {
srs_lib_verbose("Data packet type=%s, time=%d, size=%d",
srs_type2string(type), timestamp, size);
int nparsed = 0;
while (nparsed < size) {
int nb_parsed_this = 0;
srs_amf0_t amf0 = srs_amf0_parse(data + nparsed, size - nparsed, &nb_parsed_this);
if (amf0 == NULL) {
break;
}
nparsed += nb_parsed_this;
char* amf0_str = NULL;
srs_raw_trace("%s", srs_amf0_human_print(amf0, &amf0_str, NULL));
srs_amf0_free_bytes(amf0_str);
}
} else {
srs_lib_trace("Unknown packet type=%s, dts=%d, pts=%d, size=%d",
srs_type2string(type), timestamp, pts, size);
}
return ret;
}
const char* srs_format_time()
{
struct timeval tv;
@ -1171,7 +1326,9 @@ srs_amf0_t srs_amf0_parse(char* data, int size, int* nparsed)
return amf0;
}
*nparsed = stream.pos();
if (nparsed) {
*nparsed = stream.pos();
}
amf0 = (srs_amf0_t)any;
return amf0;
@ -1445,8 +1602,6 @@ int srs_audio_write_raw_frame(srs_rtmp_t rtmp,
char sound_format, char sound_rate, char sound_size, char sound_type,
char aac_packet_type, char* frame, int frame_size, u_int32_t timestamp
) {
int ret = ERROR_SUCCESS;
Context* context = (Context*)rtmp;
srs_assert(context);