• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_factory.h"
29 
30 #define BUF_SIZE 2 * 1024
31 
32 using namespace OHOS::Sharing;
33 
StartRtpServer(const std::function<void (const char * buf,size_t len)> & cb)34 void StartRtpServer(const std::function<void(const char *buf, size_t len)> &cb)
35 {
36     int listenfd, opt = 1;
37     (void)opt;
38     struct sockaddr_in serveraddr;
39 
40     listenfd = socket(AF_INET, SOCK_DGRAM, 0);
41     if (listenfd < 0) {
42         perror("Create socket fail.");
43         return;
44     }
45 
46     memset((void *)&serveraddr, 0, sizeof(struct sockaddr_in));
47     serveraddr.sin_family = AF_INET;
48     serveraddr.sin_port = htons(12345);
49     serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
50 
51     if (bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(struct sockaddr_in)) < 0) {
52         perror("bind error.");
53         return;
54     }
55 
56     int byte = 0;
57     char buf[BUF_SIZE] = {0};
58     socklen_t len = sizeof(struct sockaddr_in);
59     struct sockaddr_in clientaddr;
60 
61     if (listenfd <= 0) {
62         perror("socket fd value err");
63         return;
64     }
65 
66     while (1) {
67         memset(buf, 0, BUF_SIZE);
68         byte = recvfrom(listenfd, buf, BUF_SIZE, 0, (struct sockaddr *)&clientaddr, &len);
69         if (byte == 0) {
70             printf("sockfd:%d read over\n", listenfd);
71             break;
72         }
73         if (byte < 0) {
74             perror("read failed");
75             break;
76         }
77         cb(buf, byte);
78     }
79     close(listenfd);
80 }
81 
main(int argc,char ** argv)82 int main(int argc, char **argv)
83 {
84     const char *h264file = "/test.h264";
85     const char *aacfile = "/test.aac";
86     FILE *fh264 = fopen(h264file, "wb");
87     FILE *faac = fopen(aacfile, "wb");
88 
89     auto unPack = RtpFactory::CreateRtpUnpack();
90     unPack->SetOnRtpUnpack([=](uint32_t ssrc, const Frame::Ptr &frame) {
91         printf("SetOnRtpUnpack\n");
92         if (frame->GetTrackType() == TRACK_VIDEO) {
93             printf("h264 data: len: %d=====================================\n", frame->Size());
94             auto data = frame->Data();
95             auto bytes = frame->Size();
96             for (size_t i = 0; i < bytes; i++) {
97                 printf("%02X ", data[i]);
98                 if ((i + 1) % 16 == 0) {
99                     printf("\n");
100                 }
101             }
102             printf("\n");
103             fwrite(frame->Data(), frame->Size(), 1, fh264);
104         } else if (frame->GetTrackType() == TRACK_AUDIO) {
105             printf("aac data: len: %d=====================================\n", frame->Size());
106             auto data = frame->Data();
107             auto bytes = frame->Size();
108             for (size_t i = 0; i < bytes; i++) {
109                 printf("%02X ", data[i]);
110                 if ((i + 1) % 16 == 0) {
111                     printf("\n");
112                 }
113             }
114             printf("\n");
115             fwrite(frame->Data(), frame->Size(), 1, faac);
116         }
117     });
118 
119     StartRtpServer([unPack](const char *buf, size_t len) { unPack->ParseRtp(buf, len); });
120     return 0;
121 }