• 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 #include <string>
17 #include "gtest/gtest.h"
18 #include "AVMuxerDemo.h"
19 
20 using namespace std;
21 using namespace testing::ext;
22 using namespace OHOS;
23 using namespace OHOS::MediaAVCodec;
24 
25 
26 namespace {
27     constexpr int64_t BIT_RATE = 32000;
28     constexpr int32_t CODEC_CONFIG = 100;
29     constexpr int32_t AUDIO_CHANNELS = 1;
30     constexpr int32_t SAMPLE_RATE = 48000;
31     constexpr int32_t SAMPLE_PER_FRAME = 480;
32     constexpr int32_t AAC_PROFILE = 0;
33 
34     class FFmpegAVMuxerDemo : public testing::Test {
35     public:
36         static void SetUpTestCase();
37         static void TearDownTestCase();
38         void SetUp() override;
39         void TearDown() override;
40     };
41 
SetUpTestCase()42     void FFmpegAVMuxerDemo::SetUpTestCase() {}
TearDownTestCase()43     void FFmpegAVMuxerDemo::TearDownTestCase() {}
SetUp()44     void FFmpegAVMuxerDemo::SetUp() {}
TearDown()45     void FFmpegAVMuxerDemo::TearDown() {}
46 
47 
Create(AVMuxerDemo * muxerDemo)48     void Create(AVMuxerDemo* muxerDemo)
49     {
50         OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
51         int32_t fd = muxerDemo->getFdByMode(format);
52         muxerDemo->FFmpegCreate(fd);
53     }
54 
SetLocation(AVMuxerDemo * muxerDemo)55     Status SetLocation(AVMuxerDemo* muxerDemo)
56     {
57         float latitude = 0;
58         float longitude = 0;
59 
60         return muxerDemo->FFmpegSetLocation(latitude, longitude);
61     }
62 
SetRotation(AVMuxerDemo * muxerDemo)63     Status SetRotation(AVMuxerDemo* muxerDemo)
64     {
65         int32_t rotation = 0;
66 
67         return muxerDemo->FFmpegSetRotation(rotation);
68     }
69 
SetParameter(AVMuxerDemo * muxerDemo)70     Status SetParameter(AVMuxerDemo* muxerDemo)
71     {
72         Format generalFormat;
73         generalFormat.PutStringValue(OH_AV_KEY_TITLE, "aaa");
74 
75         return muxerDemo->FFmpegSetParameter(generalFormat);
76     }
77 
AddTrack(AVMuxerDemo * muxerDemo,int32_t & trackIndex)78     Status AddTrack(AVMuxerDemo* muxerDemo, int32_t& trackIndex)
79     {
80         Format trackFormat;
81         uint8_t a[100];
82 
83         trackFormat.PutStringValue(OH_AV_KEY_MIME, OH_AV_MIME_AUDIO_AAC);
84         trackFormat.PutLongValue(OH_AV_KEY_BIT_RATE, BIT_RATE);
85         trackFormat.PutBuffer(OH_AV_KEY_CODEC_CONFIG, a, CODEC_CONFIG);
86         trackFormat.PutIntValue(OH_AV_KEY_AUDIO_SAMPLE_FORMAT, A_SAMPLE_FMT_S16);
87         trackFormat.PutIntValue(OH_AV_KEY_AUDIO_CHANNELS, AUDIO_CHANNELS);
88         trackFormat.PutIntValue(OH_AV_KEY_AUDIO_SAMPLE_RATE, SAMPLE_RATE);
89         trackFormat.PutLongValue(OH_AV_KEY_AUDIO_CHANNEL_MASK, A_CH_MASK_STEREO);
90         trackFormat.PutIntValue(OH_AV_KEY_AUDIO_SAMPLE_PER_FRAME, SAMPLE_PER_FRAME);
91         trackFormat.PutIntValue(OH_AV_KEY_AUDIO_AAC_PROFILE, AAC_PROFILE);
92 
93         return muxerDemo->FFmpegAddTrack(trackIndex, trackFormat);
94     }
95 
Start(AVMuxerDemo * muxerDemo)96     Status Start(AVMuxerDemo* muxerDemo)
97     {
98         return muxerDemo->FFmpegStart();
99     }
100 
WriteSampleBuffer(AVMuxerDemo * muxerDemo,uint32_t trackIndex)101     Status WriteSampleBuffer(AVMuxerDemo* muxerDemo, uint32_t trackIndex)
102     {
103         uint8_t data[100];
104 
105         AVCodecBufferInfo info;
106         constexpr uint32_t INFO_SIZE = 100;
107         info.size = INFO_SIZE;
108         info.pts = 0;
109         info.offset = 0;
110         info.flags = 0;
111 
112         return muxerDemo->FFmpegWriteSampleBuffer(trackIndex, data, info);
113     }
114 
Stop(AVMuxerDemo * muxerDemo)115     Status Stop(AVMuxerDemo* muxerDemo)
116     {
117         return muxerDemo->FFmpegStop();
118     }
119 
Destroy(AVMuxerDemo * muxerDemo)120     Status Destroy(AVMuxerDemo* muxerDemo)
121     {
122         return muxerDemo->FFmpegDestroy();
123     }
124 }
125 
126 /**
127  * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_INTERFACE_DEPEND_CHECK_001
128  * @tc.name      : Create -> SetLocation -> SetRotation -> SetParameter -> AddTrack -> Start ->
129  * WriteSampleBuffer -> Stop -> Destroy
130  * @tc.desc      : interface depend check
131  */
132 HWTEST_F(FFmpegAVMuxerDemo, SUB_MULTIMEDIA_MEDIA_MUXER_INTERFACE_DEPEND_CHECK_001, TestSize.Level2)
133 {
134     AVMuxerDemo* muxerDemo = new AVMuxerDemo();
135     Create(muxerDemo);
136 
137     Status ret;
138     int32_t trackId;
139 
140     ret = SetLocation(muxerDemo);
141     ASSERT_EQ(CSERR_OK, ret);
142 
143     ret = SetRotation(muxerDemo);
144     ASSERT_EQ(CSERR_OK, ret);
145 
146     ret = AddTrack(muxerDemo, trackId);
147     ASSERT_EQ(CSERR_OK, ret);
148     ASSERT_EQ(0, trackId);
149 
150     ret = Start(muxerDemo);
151     ASSERT_EQ(CSERR_OK, ret);
152 
153     ret = WriteSampleBuffer(muxerDemo, trackId);
154     ASSERT_EQ(CSERR_OK, ret);
155 
156     ret = Stop(muxerDemo);
157     ASSERT_EQ(CSERR_OK, ret);
158 
159     ret = Destroy(muxerDemo);
160     ASSERT_EQ(CSERR_OK, ret);
161 
162     delete muxerDemo;
163 }