• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device 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 
17 #include <string>
18 #include "securec.h"
19 #include "gtest/gtest.h"
20 #include "audio_info.h"
21 #include "native_avcodec_base.h"
22 #include "native_avformat.h"
23 
24 using namespace std;
25 using namespace testing::ext;
26 
27 namespace {
28 class ActsCodecFormatNdkTest : public testing::Test {
29 public:
30     static void SetUpTestCase();
31     static void TearDownTestCase();
32     void SetUp() override;
33     void TearDown() override;
34 };
35 
SetUpTestCase()36 void ActsCodecFormatNdkTest::SetUpTestCase() {}
TearDownTestCase()37 void ActsCodecFormatNdkTest::TearDownTestCase() {}
SetUp()38 void ActsCodecFormatNdkTest::SetUp() {}
TearDown()39 void ActsCodecFormatNdkTest::TearDown() {}
40 
CheckDecDesc(map<string,int> InDesc,OH_AVFormat * OutDesc)41 bool CheckDecDesc(map<string, int> InDesc, OH_AVFormat* OutDesc)
42 {
43     int32_t out ;
44     for (const auto& t: InDesc) {
45         bool res = OH_AVFormat_GetIntValue(OutDesc, t.first.c_str(), &out);
46         cout << "key: " << t.first << "; out: " << out <<endl;
47         if (!res) {
48             cout << "OH_AVFormat_GetIntValue Fail. key:" << t.first << endl;
49             return false;
50         }
51         if (out != t.second) {
52             cout << "OH_AVFormat_GetIntValue error. key: " << t.first
53             << "; expect: "<< t.second
54             << ", actual: "<< out << endl;
55             return false;
56         }
57         out = 0;
58     }
59     return true;
60 }
61 
SetFormat(struct OH_AVFormat * format,map<string,int> mediaDescription)62 bool SetFormat(struct OH_AVFormat *format, map<string, int> mediaDescription)
63 {
64     const char *key;
65     for (const auto& t: mediaDescription) {
66         key = t.first.c_str();
67         if (not OH_AVFormat_SetIntValue(format, key, t.second)) {
68             cout << "OH_AV_FormatPutIntValue Fail. format key: " << t.first
69             << ", value: "<< t.second << endl;
70             return false;
71         }
72     }
73     return true;
74 }
75 }
76 
77 /**
78  * @tc.number    : SUB_MULTIMEDIA_MEDIA_CODEC_FORMAT_0100
79  * @tc.name      : test codec format set key
80  * @tc.desc      : Basic function test
81  */
82 HWTEST_F(ActsCodecFormatNdkTest, SUB_MULTIMEDIA_MEDIA_CODEC_FORMAT_0100, TestSize.Level1)
83 {
84     OH_AVFormat *codecFormatIn = OH_AVFormat_Create();
85     ASSERT_NE(nullptr, codecFormatIn);
86     OH_AVFormat *codecFormatOut = OH_AVFormat_Create();
87     ASSERT_NE(nullptr, codecFormatOut);
88     map<string, int> CodecParam = {
89         {OH_ED_KEY_TIME_STAMP, 200}, // set timestamp 200ms
90         {OH_ED_KEY_EOS, 1}, // set end of stream
91         {OH_MD_KEY_TRACK_TYPE, 1}, // set track type video
92         {OH_MD_KEY_DURATION, 200}, // set key duration 200ms
93         {OH_MD_KEY_BITRATE, 48000}, // set key bitrate 48000
94         {OH_MD_KEY_MAX_INPUT_SIZE, 2000}, // set key max input size 2000
95         {OH_MD_KEY_VIDEO_ENCODE_BITRATE_MODE, 0}, // set videoencoder bitrate mode CBR
96         {OH_MD_KEY_PROFILE, 1}, // set encode profile AVC_PROFILE_BASELINE
97         {OH_MD_KEY_I_FRAME_INTERVAL, 1}, // set key i frame 1ms
98         {OH_MD_KEY_ROTATION, 90}, // set key rotation 0 degree
99     };
100     ASSERT_EQ(true, SetFormat(codecFormatIn, CodecParam));
101     OH_AVFormat_Copy(codecFormatOut, codecFormatIn);
102     ASSERT_EQ(true, CheckDecDesc(CodecParam, codecFormatOut));
103     ASSERT_NE(nullptr, OH_AVFormat_DumpInfo(codecFormatOut));
104     cout << OH_AVFormat_DumpInfo(codecFormatIn) << endl;
105     OH_AVFormat_Destroy(codecFormatIn);
106     codecFormatIn = nullptr;
107     OH_AVFormat_Destroy(codecFormatOut);
108     codecFormatIn = nullptr;
109 }
110 
111 /**
112  * @tc.number    : SUB_MULTIMEDIA_MEDIA_CODEC_FORMAT_0200
113  * @tc.name      : test codec format set and get value
114  * @tc.desc      : Basic function test
115  */
116 HWTEST_F(ActsCodecFormatNdkTest, SUB_MULTIMEDIA_MEDIA_CODEC_FORMAT_0200, TestSize.Level1)
117 {
118     const char *intKey = "int value key";
119     const char *longKey = "long value key";
120     const char *floatKey = "float value key";
121     const char *doubleKey = "double value key";
122     const char *stringKey = "string value key";
123     const char *stringValue = "string_value";
124     int32_t intValue = 1;
125     int64_t longValue = 1;
126     float floatValue = 1.0;
127     double doubleValue = 1.0;
128 
129     OH_AVFormat *codecFormatIn = OH_AVFormat_Create();
130     ASSERT_NE(nullptr, codecFormatIn);
131     OH_AVFormat *codecFormatOut = OH_AVFormat_Create();
132     ASSERT_NE(nullptr, codecFormatOut);
133     OH_AVFormat_SetIntValue(codecFormatIn, intKey, intValue);
134     OH_AVFormat_SetLongValue(codecFormatIn, longKey, longValue);
135     OH_AVFormat_SetFloatValue(codecFormatIn, floatKey, floatValue);
136     OH_AVFormat_SetDoubleValue(codecFormatIn, doubleKey, doubleValue);
137     OH_AVFormat_SetStringValue(codecFormatIn, stringKey, stringValue);
138 
139     OH_AVFormat_Copy(codecFormatOut, codecFormatIn);
140     int32_t intValueOut;
141     OH_AVFormat_GetIntValue(codecFormatOut, intKey, &intValueOut);
142     ASSERT_EQ(intValueOut, intValue);
143     int64_t longValueOut;
144     OH_AVFormat_GetLongValue(codecFormatOut, longKey, &longValueOut);
145     ASSERT_EQ(longValueOut, longValue);
146     float floatValueOut;
147     OH_AVFormat_GetFloatValue(codecFormatOut, floatKey, &floatValueOut);
148     ASSERT_EQ(floatValueOut, floatValue);
149     double doubleValueOut;
150     OH_AVFormat_GetDoubleValue(codecFormatOut, doubleKey, &doubleValueOut);
151     ASSERT_EQ(doubleValueOut, doubleValue);
152     const char *stringValueOut;
153     OH_AVFormat_GetStringValue(codecFormatOut, stringKey, &stringValueOut);
154     ASSERT_EQ(*stringValueOut, *stringValue);
155 
156     OH_AVFormat_Destroy(codecFormatIn);
157     codecFormatIn = nullptr;
158     OH_AVFormat_Destroy(codecFormatOut);
159     codecFormatOut = nullptr;
160 }
161 
162 /**
163  * @tc.number    : SUB_MULTIMEDIA_MEDIA_CODEC_FORMAT_0300
164  * @tc.name      : test codec format set and get buffer
165  * @tc.desc      : Basic function test
166  */
167 HWTEST_F(ActsCodecFormatNdkTest, SUB_MULTIMEDIA_MEDIA_CODEC_FORMAT_0300, TestSize.Level1)
168 {
169     const char *bufferKey = "buffer value key";
170     OH_AVFormat *codecFormatIn = OH_AVFormat_Create();
171     ASSERT_NE(nullptr, codecFormatIn);
172     OH_AVFormat *codecFormatOut = OH_AVFormat_Create();
173     ASSERT_NE(nullptr, codecFormatOut);
174     int32_t buffernum = 10;
175     size_t sizeIn = buffernum * sizeof(uint8_t);
176     uint8_t *buffer = reinterpret_cast<uint8_t *>(malloc(sizeIn));
177     (void)memset_s(buffer, sizeIn, 1, sizeIn);
178     OH_AVFormat_SetBuffer(codecFormatIn, bufferKey, buffer, sizeIn);
179 
180     OH_AVFormat_Copy(codecFormatOut, codecFormatIn);
181     uint8_t *addrout;
182     size_t sizeOut;
183     OH_AVFormat_GetBuffer(codecFormatOut, bufferKey, &addrout, &sizeOut);
184     ASSERT_EQ(sizeIn, sizeOut);
185     for (int32_t i = 0; i < buffernum; i++) {
186         ASSERT_EQ(buffer[i], addrout[i]);
187     }
188 
189     OH_AVFormat_Destroy(codecFormatIn);
190     codecFormatIn = nullptr;
191     OH_AVFormat_Destroy(codecFormatOut);
192     codecFormatOut = nullptr;
193 }