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 Plugins::OutputFormat format = Plugins::OutputFormat::MPEG_4; 51 int32_t fd = muxerDemo->InnerGetFdByMode(format); 52 muxerDemo->InnerCreate(fd, format); 53 } 54 SetRotation(AVMuxerDemo * muxerDemo)55 int32_t SetRotation(AVMuxerDemo* muxerDemo) 56 { 57 int32_t rotation = 0; 58 return muxerDemo->InnerSetRotation(rotation); 59 } 60 AddTrack(AVMuxerDemo * muxerDemo)61 int32_t AddTrack(AVMuxerDemo* muxerDemo) 62 { 63 std::shared_ptr<Meta> trackFormat = std::make_shared<Meta>(); 64 std::vector<uint8_t> a(CODEC_CONFIG); 65 trackFormat->Set<Tag::MIME_TYPE>(Plugins::MimeType::AUDIO_AAC); 66 trackFormat->Set<Tag::MEDIA_BITRATE>(BIT_RATE); 67 trackFormat->Set<Tag::MEDIA_CODEC_CONFIG>(a); 68 trackFormat->Set<Tag::AUDIO_CHANNEL_COUNT>(AUDIO_CHANNELS); 69 trackFormat->Set<Tag::AUDIO_SAMPLE_RATE>(SAMPLE_RATE); 70 trackFormat->Set<Tag::AUDIO_SAMPLE_PER_FRAME>(SAMPLE_PER_FRAME); 71 return muxerDemo->InnerAddTrack(trackFormat); 72 } 73 Start(AVMuxerDemo * muxerDemo)74 int32_t Start(AVMuxerDemo* muxerDemo) 75 { 76 return muxerDemo->InnerStart(); 77 } 78 WriteSampleBuffer(AVMuxerDemo * muxerDemo,uint32_t trackIndex)79 int32_t WriteSampleBuffer(AVMuxerDemo* muxerDemo, uint32_t trackIndex) 80 { 81 uint8_t data[100]; 82 83 AVCodecBufferInfo info; 84 constexpr uint32_t INFO_SIZE = 100; 85 info.size = INFO_SIZE; 86 info.pts = 0; 87 info.offset = 0; 88 info.flags = 0; 89 90 return muxerDemo->InnerWriteSampleBuffer(trackIndex, data, info); 91 } 92 Stop(AVMuxerDemo * muxerDemo)93 int32_t Stop(AVMuxerDemo* muxerDemo) 94 { 95 return muxerDemo->InnerStop(); 96 } 97 Destroy(AVMuxerDemo * muxerDemo)98 int32_t Destroy(AVMuxerDemo* muxerDemo) 99 { 100 return muxerDemo->InnerDestroy(); 101 } 102 } 103 104 /** 105 * @tc.number : SUB_MULTIMEDIA_MEDIA_MUXER_INTERFACE_DEPEND_CHECK_001 106 * @tc.name : Create -> SetLocation -> SetRotation -> SetParameter -> AddTrack -> Start -> 107 * WriteSampleBuffer -> Stop -> Destroy 108 * @tc.desc : interface depend check 109 */ 110 HWTEST_F(InnerAVMuxerDemo, SUB_MULTIMEDIA_MEDIA_MUXER_INTERFACE_DEPEND_CHECK_001, TestSize.Level2) 111 { 112 AVMuxerDemo* muxerDemo = new AVMuxerDemo(); 113 Create(muxerDemo); 114 115 int32_t ret; 116 int32_t trackId; 117 118 ret = SetLocation(muxerDemo); 119 ASSERT_EQ(AV_ERR_OK, ret); 120 121 ret = SetRotation(muxerDemo); 122 ASSERT_EQ(AV_ERR_OK, ret); 123 124 trackId = AddTrack(muxerDemo); 125 ASSERT_EQ(0, trackId); 126 127 ret = Start(muxerDemo); 128 ASSERT_EQ(AV_ERR_OK, ret); 129 130 ret = WriteSampleBuffer(muxerDemo, trackId); 131 ASSERT_EQ(AV_ERR_OK, ret); 132 133 ret = Stop(muxerDemo); 134 ASSERT_EQ(AV_ERR_OK, ret); 135 136 ret = Destroy(muxerDemo); 137 ASSERT_EQ(AV_ERR_OK, ret); 138 139 delete muxerDemo; 140 }