123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- /*************************************************************************
- * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *************************************************************************/
- #include <gtest/gtest.h>
- #include <chrono>
- #include <memory>
- #include <string>
- #include <thread>
- #include "cnrt.h"
- #include "cnstream_source.hpp"
- #include "data_handler_file.hpp"
- #include "data_handler_mem.hpp"
- #include "data_source.hpp"
- #include "util/video_decoder.hpp"
- #include "test_base.hpp"
- namespace cnstream {
- static constexpr const char *gname = "source";
- static constexpr const char *gmp4_path = "../../modules/unitest/source/data/img.mp4";
- static constexpr const char *gh264_path = "../../modules/unitest/source/data/raw.h264";
- static constexpr const char *gimage_path = "../../data/images/%d.jpg";
- class PrepareEnvFile {
- public:
- // device = 0 mlu, device = 1 cpu
- explicit PrepareEnvFile(int device, bool img = false) {
- std::string mp4_path = GetExePath() + gmp4_path;
- std::string image_path = GetExePath() + gimage_path;
- src = new DataSource(gname);
- if (img == false) {
- auto handler = FileHandler::Create(src, "0", mp4_path, 30, false);
- file_handler = std::dynamic_pointer_cast<FileHandler>(handler);
- } else {
- auto handler = FileHandler::Create(src, "0", image_path, 30, false);
- file_handler = std::dynamic_pointer_cast<FileHandler>(handler);
- }
- if (device == 0) {
- ModuleParamSet param;
- param["output_type"] = "mlu";
- param["interval"] = "1";
- param["decoder_type"] = "mlu";
- param["device_id"] = "0";
- param["reuse_cndec_buf"] = "false";
- src->Open(param);
- file_handler->impl_->SetDecodeParam(src->GetSourceParam());
- mlu_decoder = std::make_shared<MluDecoder>("fake_id", file_handler->impl_);
- } else {
- ModuleParamSet param;
- param["output_type"] = "cpu";
- param["interval"] = "1";
- param["decoder_type"] = "cpu";
- src->Open(param);
- file_handler->impl_->SetDecodeParam(src->GetSourceParam());
- ffmpeg_cpu_decoder = std::make_shared<FFmpegCpuDecoder>("fake_id", file_handler->impl_);
- }
- info.codec_id = AV_CODEC_ID_H264;
- }
- ~PrepareEnvFile() {
- if (src) delete src;
- }
- DataSource *src = nullptr;
- std::shared_ptr<FileHandler> file_handler;
- std::shared_ptr<MluDecoder> mlu_decoder;
- std::shared_ptr<FFmpegCpuDecoder> ffmpeg_cpu_decoder;
- VideoInfo info;
- ExtraDecoderInfo extra;
- VideoEsPacket pkt;
- }; // PrepareEnvFile
- class PrepareEnvMem {
- public:
- PrepareEnvMem() {
- std::string h264_path = GetExePath() + gh264_path;
- src = new DataSource(gname);
- auto handler = ESMemHandler::Create(src, "0");
- mem_handler = std::dynamic_pointer_cast<ESMemHandler>(handler);
- ModuleParamSet param;
- param["output_type"] = "mlu";
- param["interval"] = "1";
- param["decoder_type"] = "mlu";
- param["device_id"] = "0";
- param["reuse_cndec_buf"] = "false";
- src->Open(param);
- mem_handler->impl_->SetDecodeParam(src->GetSourceParam());
- mlu_decoder = std::make_shared<MluDecoder>("fake_id", mem_handler->impl_);
- info.codec_id = AV_CODEC_ID_H264;
- }
- ~PrepareEnvMem() {
- if (src) delete src;
- }
- DataSource *src = nullptr;
- std::shared_ptr<ESMemHandler> mem_handler;
- std::shared_ptr<MluDecoder> mlu_decoder;
- VideoInfo info;
- ExtraDecoderInfo extra;
- VideoEsPacket pkt;
- }; // PrepareEnvMem
- TEST(SourceMluDecoder, CreateDestroyJpeg) {
- PrepareEnvFile env(0, true);
- // mjpeg
- env.info.codec_id = AV_CODEC_ID_MJPEG;
- EXPECT_TRUE(env.mlu_decoder->Create(&env.info, &env.extra));
- env.mlu_decoder->Destroy();
- }
- // Cpu FFmpeg Decoder
- TEST(SourceCpuFFmpegDecoder, CreateDestroy) {
- int device_type = 1;
- PrepareEnvFile env(device_type);
- // h264
- EXPECT_TRUE(env.ffmpeg_cpu_decoder->Create(&env.info, &env.extra));
- env.ffmpeg_cpu_decoder->Destroy();
- // h265
- env.info.codec_id = AV_CODEC_ID_HEVC;
- EXPECT_TRUE(env.ffmpeg_cpu_decoder->Create(&env.info, &env.extra));
- env.ffmpeg_cpu_decoder->Destroy();
- // mjpeg
- PrepareEnvFile env_jpeg(device_type, true);
- env_jpeg.info.codec_id = AV_CODEC_ID_MJPEG;
- EXPECT_TRUE(env_jpeg.ffmpeg_cpu_decoder->Create(&env_jpeg.info, &env_jpeg.extra));
- env_jpeg.ffmpeg_cpu_decoder->Destroy();
- // invalid
- env_jpeg.info.codec_id = AV_CODEC_ID_NONE;
- EXPECT_FALSE(env_jpeg.ffmpeg_cpu_decoder->Create(&env_jpeg.info, &env_jpeg.extra));
- env_jpeg.ffmpeg_cpu_decoder->Destroy();
- }
- TEST(SourceCpuFFmpegDecoder, Process) {
- PrepareEnvFile env(1);
- EXPECT_TRUE(env.ffmpeg_cpu_decoder->Create(&env.info, &env.extra));
- EXPECT_FALSE(env.ffmpeg_cpu_decoder->Process(&env.pkt)); // EOS
- env.ffmpeg_cpu_decoder->Destroy();
- }
- // Mlu Mem Decoder
- TEST(SourceMluDecoder, CreateDestroy) {
- PrepareEnvMem env;
- // h264
- env.info.codec_id = AV_CODEC_ID_H264;
- EXPECT_TRUE(env.mlu_decoder->Create(&env.info, &env.extra));
- env.mlu_decoder->Destroy();
- // destroy twice, the function will return directly.
- env.mlu_decoder->Destroy();
- env.info.codec_id = AV_CODEC_ID_HEVC;
- EXPECT_TRUE(env.mlu_decoder->Create(&env.info, &env.extra));
- env.mlu_decoder->Destroy();
- }
- } // namespace cnstream
|