1 /*
2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <arpa/inet.h>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <iostream>
20 #include <netdb.h>
21 #include <netinet/in.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/socket.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include "frame/frame.h"
28 #include "rtp_def.h"
29 #include "rtp_factory.h"
30
31 #define BUF_SIZE 2 * 1024
32
33 using namespace OHOS::Sharing;
34
StartRtpServer(int port,const std::function<void (const char * buf,size_t len)> & cb)35 void StartRtpServer(int port, const std::function<void(const char *buf, size_t len)> &cb)
36 {
37 int listenfd, opt = 1;
38 (void)opt;
39 struct sockaddr_in serveraddr;
40
41 listenfd = socket(AF_INET, SOCK_DGRAM, 0);
42 if (listenfd < 0) {
43 perror("Create socket fail.");
44 return;
45 }
46
47 memset((void *)&serveraddr, 0, sizeof(struct sockaddr_in));
48 serveraddr.sin_family = AF_INET;
49 serveraddr.sin_port = htons(port);
50 serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
51
52 if (bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(struct sockaddr_in)) < 0) {
53 perror("bind error.");
54 return;
55 }
56
57 int byte = 0;
58 char buf[BUF_SIZE] = {0};
59 socklen_t len = sizeof(struct sockaddr_in);
60 struct sockaddr_in clientaddr;
61
62 if (listenfd <= 0) {
63 perror("socket fd value err");
64 return;
65 }
66
67 while (1) {
68 memset(buf, 0, BUF_SIZE);
69 byte = recvfrom(listenfd, buf, BUF_SIZE, 0, (struct sockaddr *)&clientaddr, &len);
70 if (byte == 0) {
71 printf("sockfd:%d read over\n", listenfd);
72 break;
73 }
74 if (byte < 0) {
75 perror("read failed");
76 break;
77 }
78 printf("client IP:%s, port:%d, datalen:%d, info:%s\n", inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port,
79 byte, buf);
80 cb(buf, byte);
81 }
82 close(listenfd);
83 }
84
main(int argc,char ** argv)85 int main(int argc, char **argv)
86 {
87 const char *h264file = "/test.h264";
88 const char *aacfile = "/test.aac";
89 const char *aacrtp = "/aac.rtp";
90 FILE *fh264 = fopen(h264file, "wb");
91 FILE *faac = fopen(aacfile, "wb");
92 FILE *faacrtp = fopen(aacrtp, "wb");
93
94 auto extra = std::make_shared<AACExtra>();
95 extra->aacConfig_ = "1210";
96 auto aacPack = RtpFactory::CreateRtpPack(4568712, 1400, 44100, 97, RtpPayloadStream::MPEG4_GENERIC);
97
98 aacPack->SetOnRtpPack([=](const RtpPacket::Ptr &rtp) {
99 printf("rtp packed seq: %d, timestamp: %d, size: %d\n", rtp->GetSeq(), rtp->GetStamp(), rtp->Size());
100 fwrite(rtp->Data(), rtp->Size(), 1, faacrtp);
101 });
102
103 auto aacunPack = RtpFactory::CreateRtpUnpack(RtpPlaylodParam{97, 44100, RtpPayloadStream::MPEG4_GENERIC, extra});
104 aacunPack->SetOnRtpUnpack([=](uint32_t ssrc, const Frame::Ptr &frame) {
105 printf("SetOnRtpUnpack\n");
106 if (frame->GetTrackType() == TRACK_AUDIO) {
107 printf("aac data: len: %d dts: %d", frame->Size(), frame->Dts());
108 auto data = frame->Data();
109 auto bytes = frame->Size();
110 for (size_t i = 0; i < bytes; i++) {
111 printf("%02X ", data[i]);
112 if ((i + 1) % 16 == 0) {
113 printf("\n");
114 }
115 }
116 printf("\n");
117 fwrite(frame->Data(), frame->Size(), 1, faac);
118 aacPack->InputFrame(frame);
119 }
120 });
121
122 StartRtpServer(1234, [aacunPack](const char *buf, size_t len) { aacunPack->ParseRtp(buf, len); });
123
124 auto h264unPack = RtpFactory::CreateRtpUnpack(RtpPlaylodParam{96, 90000, RtpPayloadStream::H264});
125 h264unPack->SetOnRtpUnpack([=](uint32_t ssrc, const Frame::Ptr &frame) {
126 if (frame->GetTrackType() == TRACK_VIDEO) {
127 printf("h264 data: len: %d dts: %d", frame->Size(), frame->Dts());
128 auto data = frame->Data();
129 auto bytes = frame->Size();
130 for (size_t i = 0; i < bytes; i++) {
131 printf("%02X ", data[i]);
132 if ((i + 1) % 16 == 0) {
133 printf("\n");
134 }
135 }
136 printf("\n");
137 fwrite(frame->Data(), frame->Size(), 1, fh264);
138 }
139 });
140
141 StartRtpServer(1236, [h264unPack](const char *buf, size_t len) { h264unPack->ParseRtp(buf, len); });
142
143 return 0;
144 }