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 InnerAVMuxerDemo : 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 InnerAVMuxerDemo::SetUpTestCase() {} TearDownTestCase()43 void InnerAVMuxerDemo::TearDownTestCase() {} SetUp()44 void InnerAVMuxerDemo::SetUp() {} TearDown()45 void InnerAVMuxerDemo::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->InnerCreate(fd, format); 53 } 54 SetLocation(AVMuxerDemo * muxerDemo)55 int32_t SetLocation(AVMuxerDemo* muxerDemo) 56 { 57 float latitude = 0; 58 float longitude = 0; 59 60 return muxerDemo->InnerSetLocation(latitude, longitude); 61 } 62 SetRotation(AVMuxerDemo * muxerDemo)63 int32_t SetRotation(AVMuxerDemo* muxerDemo) 64 { 65 int32_t rotation = 0; 66 67 return muxerDemo->InnerSetRotation(rotation); 68 } 69 AddTrack(AVMuxerDemo * muxerDemo)70 int32_t AddTrack(AVMuxerDemo* muxerDemo) 71 { 72 Format trackFormat; 73 uint8_t a[100]; 74 trackFormat.PutStringValue(OH_AV_KEY_MIME, OH_AV_MIME_AUDIO_AAC); 75 trackFormat.PutLongValue(OH_AV_KEY_BIT_RATE, BIT_RATE); 76 trackFormat.PutBuffer(OH_AV_KEY_CODEC_CONFIG, a, CODEC_CONFIG); 77 trackFormat.PutIntValue(OH_AV_KEY_AUDIO_SAMPLE_FORMAT, A_SAMPLE_FMT_S16); 78 trackFormat.PutIntValue(OH_AV_KEY_AUDIO_CHANNELS, AUDIO_CHANNELS); 79 trackFormat.PutIntValue(OH_AV_KEY_AUDIO_SAMPLE_RATE, SAMPLE_RATE); 80 trackFormat.PutLongValue(OH_AV_KEY_AUDIO_CHANNEL_MASK, A_CH_MASK_STEREO); 81 trackFormat.PutIntValue(OH_AV_KEY_AUDIO_SAMPLE_PER_FRAME, SAMPLE_PER_FRAME); 82 trackFormat.PutIntValue(OH_AV_KEY_AUDIO_AAC_PROFILE, AAC_PROFILE); 83 84 return muxerDemo->InnerAddTrack(trackFormat); 85 } 86 Start(AVMuxerDemo * muxerDemo)87 int32_t Start(AVMuxerDemo* muxerDemo) 88 { 89 return muxerDemo->InnerStart(); 90 } 91 WriteSampleBuffer(AVMuxerDemo * muxerDemo,uint32_t trackIndex)92 int32_t WriteSampleBuffer(AVMuxerDemo* muxerDemo, uint32_t trackIndex) 93 { 94 uint8_t data[100]; 95 96 AVCodecBufferInfo info; 97 constexpr uint32_t INFO_SIZE = 100; 98 info.size = INFO_SIZE; 99 info.pts = 0; 100 info.offset = 0; 101 info.flags = 0; 102 103 return muxerDemo->InnerWriteSampleBuffer(trackIndex, data, info); 104 } 105 Stop(AVMuxerDemo * muxerDemo)106 int32_t Stop(AVMuxerDemo* muxerDemo) 107 { 108 return muxerDemo->InnerStop(); 109 } 110 Destroy(AVMuxerDemo * muxerDemo)111 int32_t Destroy(AVMuxerDemo* muxerDemo) 112 { 113 return muxerDemo->InnerDestroy(); 114 } 115 } 116 117 /** 118 * @tc.number : SUB_MULTIMEDIA_MEDIA_MUXER_INTERFACE_DEPEND_CHECK_001 119 * @tc.name : Create -> SetLocation -> SetRotation -> SetParameter -> AddTrack -> Start -> 120 * WriteSampleBuffer -> Stop -> Destroy 121 * @tc.desc : interface depend check 122 */ 123 HWTEST_F(InnerAVMuxerDemo, SUB_MULTIMEDIA_MEDIA_MUXER_INTERFACE_DEPEND_CHECK_001, TestSize.Level2) 124 { 125 AVMuxerDemo* muxerDemo = new AVMuxerDemo(); 126 Create(muxerDemo); 127 128 int32_t ret; 129 int32_t trackId; 130 131 ret = SetLocation(muxerDemo); 132 ASSERT_EQ(AV_ERR_OK, ret); 133 134 ret = SetRotation(muxerDemo); 135 ASSERT_EQ(AV_ERR_OK, ret); 136 137 trackId = AddTrack(muxerDemo); 138 ASSERT_EQ(0, trackId); 139 140 ret = Start(muxerDemo); 141 ASSERT_EQ(AV_ERR_OK, ret); 142 143 ret = WriteSampleBuffer(muxerDemo, trackId); 144 ASSERT_EQ(AV_ERR_OK, ret); 145 146 ret = Stop(muxerDemo); 147 ASSERT_EQ(AV_ERR_OK, ret); 148 149 ret = Destroy(muxerDemo); 150 ASSERT_EQ(AV_ERR_OK, ret); 151 152 delete muxerDemo; 153 }