• 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 <fstream>
17 #include <functional>
18 #include <getopt.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include "codec_factory.h"
24 #include "common/sharing_log.h"
25 #include "frame.h"
26 #include "media_frame_pipeline.h"
27 
28 using namespace OHOS::Sharing;
29 
30 char *gInFile = nullptr;
31 char *gOutFile = nullptr;
32 int gType = -1; // 0 for decode, 1 for encode
33 
ShowUsage(char * exe)34 void ShowUsage(char *exe)
35 {
36     SHARING_LOGD("usage:\n%{public}s -t <type> -i <in file> -o <out file>", exe);
37     SHARING_LOGD("\t-t 0: decode G.711, 1: encode G.711, 2:decode AAC-ADTS");
38     SHARING_LOGD("\t-i in file");
39     SHARING_LOGD("\t-o out file");
40 }
41 
ParseParam(int argc,char * argv[])42 int ParseParam(int argc, char *argv[])
43 {
44     int ret;
45 
46     while ((ret = getopt(argc, argv, ":t:i:o:")) != -1) {
47         switch (ret) {
48             case ('t'):
49                 gType = atoi(optarg);
50                 break;
51             case ('i'):
52                 gInFile = optarg;
53                 break;
54             case ('o'):
55                 gOutFile = optarg;
56                 break;
57             case ':':
58                 SHARING_LOGD("option [-%c] requires an argument.", static_cast<char>(optopt));
59                 break;
60             case '?':
61                 SHARING_LOGD("unknown option: %c.", static_cast<char>(optopt));
62                 break;
63             default:
64                 break;
65         }
66     }
67 
68     if ((gType > 2 || gType < 0) || gInFile == nullptr || gOutFile == nullptr) {
69         SHARING_LOGD("param error");
70         ShowUsage(argv[0]);
71         return -1;
72     }
73 
74     return 0;
75 }
76 
77 class RawDataReceiver : public FrameDestination {
78 public:
RawDataReceiver(std::fstream & fd)79     RawDataReceiver(std::fstream &fd) : fd_(fd) {}
80     ~RawDataReceiver() = default;
OnFrame(const Frame::Ptr & frame)81     void OnFrame(const Frame::Ptr &frame) override
82     {
83         fd_.write((char *)frame->Data(), frame->Size());
84         SHARING_LOGD("write data(%{public}p) len(%{public}d)", frame->Data(), frame->Size());
85     }
86 
87 private:
88     std::fstream &fd_;
89 };
90 
DecodeG711(char * data,int length,std::fstream & fd)91 void DecodeG711(char *data, int length, std::fstream &fd)
92 {
93     std::shared_ptr<AudioDecoder> decoder = CodecFactory::CreateAudioDecoder(CODEC_G711A);
94     if (!decoder) {
95         return;
96     }
97     AudioTrack audioTrack;
98     decoder->Init(audioTrack);
99 
100     auto rawReceiver = std::make_shared<RawDataReceiver>(fd);
101 
102     decoder->AddAudioDestination(rawReceiver);
103     auto g711Frame = FrameImpl::Create();
104     g711Frame->codecId_ = CODEC_G711A;
105     char *p = data;
106     for (int i = 0; i < length / 160; i++) {
107         SHARING_LOGD("for i(%{public}d)", i);
108         g711Frame->Clear();
109         g711Frame->Assign(p, 160);
110         decoder->OnFrame(g711Frame);
111         p += 160;
112     }
113     SHARING_LOGD("decodeG711 line(%{public}d).", __LINE__);
114 }
115 
DecodeAAC(char * data,int length,std::fstream & fd)116 void DecodeAAC(char *data, int length, std::fstream &fd)
117 {
118     std::shared_ptr<AudioDecoder> decoder = CodecFactory::CreateAudioDecoder(OHOS::Sharing::CODEC_AAC);
119     if (!decoder) {
120         return;
121     }
122     AudioTrack audioTrack;
123     decoder->Init(audioTrack);
124 
125     auto rawReceiver = std::make_shared<RawDataReceiver>(fd);
126 
127     decoder->AddAudioDestination(rawReceiver);
128     auto aacFrame = FrameImpl::Create();
129     aacFrame->codecId_ = CODEC_AAC;
130     char *p = data;
131     for (int i = 0; i < length / 2048; i++) {
132         SHARING_LOGD("for i(%{public}d)", i);
133         aacFrame->Clear();
134         aacFrame->Assign(p, 2048);
135         decoder->OnFrame(aacFrame);
136         p += 2048;
137     }
138     SHARING_LOGD("decodeAAC line(%{public}d).", __LINE__);
139 }
140 
EncodeG711(char * data,int length,std::fstream & fd)141 void EncodeG711(char *data, int length, std::fstream &fd)
142 {
143     std::shared_ptr<AudioEncoder> encoder = CodecFactory::CreateAudioEncoder(CODEC_G711A);
144     if (!encoder) {
145         return;
146     }
147     encoder->Init();
148 
149     auto rawReceiver = std::make_shared<RawDataReceiver>(fd);
150 
151     encoder->AddAudioDestination(rawReceiver);
152 
153     auto pcmFrame = FrameImpl::Create();
154     pcmFrame->codecId_ = CODEC_PCM;
155     char *p = data;
156     for (int i = 0; i < length / 320; i++) {
157         SHARING_LOGD("for i(%{public}d)", i);
158         pcmFrame->Clear();
159         pcmFrame->Assign(p, 320);
160         encoder->OnFrame(pcmFrame);
161         p += 320;
162     }
163     SHARING_LOGD("decodeG711 line(%{public}d).", __LINE__);
164 }
165 
main(int argc,char * argv[])166 int main(int argc, char *argv[])
167 {
168     SHARING_LOGD("hello codec_demo.");
169     if (ParseParam(argc, argv) != 0) {
170         return -1;
171     }
172 
173     std::fstream infile(gInFile, std::ios::in | std::ios_base::binary);
174     if (!infile.is_open()) {
175         SHARING_LOGD("failed to open file");
176         return -1;
177     }
178 
179     std::fstream outfile(gOutFile, std::ios::out | std::ios_base::binary);
180     if (!outfile.is_open()) {
181         SHARING_LOGD("failed to open file");
182         return -1;
183     }
184 
185     infile.seekg(0, std::ios::end);
186     int size = infile.tellg();
187     infile.seekg(0, std::ios::beg);
188 
189     char *content = new char[size];
190     infile.read(content, size);
191     SHARING_LOGD("size %{public}d.", size);
192     infile.close();
193     if (gType == 0) {
194         DecodeG711(content, size, outfile);
195     } else if (gType == 1) {
196         EncodeG711(content, size, outfile);
197     } else if (gType == 2) {
198         DecodeAAC(content, size, outfile);
199     }
200 
201     outfile.close();
202     delete[] content;
203 }
204