From b62f634d45f6c5a4a71415b36adb91a9d54df45c Mon Sep 17 00:00:00 2001 From: XUXIAOBO Date: Wed, 25 Aug 2021 08:49:06 +0000 Subject: [PATCH 1/6] =?UTF-8?q?=E8=B0=83=E8=AF=95=20Signed-off-by:=20XUXIA?= =?UTF-8?q?OBO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../source/videocapture/include/video_capture_sf_es_avc_impl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/engine/gstreamer/plugins/source/videocapture/include/video_capture_sf_es_avc_impl.h b/services/engine/gstreamer/plugins/source/videocapture/include/video_capture_sf_es_avc_impl.h index 951b8726d..ab1bddaa0 100644 --- a/services/engine/gstreamer/plugins/source/videocapture/include/video_capture_sf_es_avc_impl.h +++ b/services/engine/gstreamer/plugins/source/videocapture/include/video_capture_sf_es_avc_impl.h @@ -39,7 +39,7 @@ private: char *codecData_ = nullptr; int32_t codecDataSize_ = 0; uint32_t nalSize_ = 0; - bool transStreamFormat = false; + bool transStreamFormat = true; }; } // namespace Media } // namespace OHOS -- Gitee From 92013c114e38a3d1f78f125da233be569c5337bf Mon Sep 17 00:00:00 2001 From: XUXIAOBO Date: Wed, 25 Aug 2021 09:25:06 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=E6=89=93=E5=BC=80videosource=E7=BC=96?= =?UTF-8?q?=E8=AF=91=20Signed-off-by:=20XUXIAOBO=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/engine/gstreamer/plugins/BUILD.gn | 1 + 1 file changed, 1 insertion(+) diff --git a/services/engine/gstreamer/plugins/BUILD.gn b/services/engine/gstreamer/plugins/BUILD.gn index 7b36b2f34..6ca84b63b 100644 --- a/services/engine/gstreamer/plugins/BUILD.gn +++ b/services/engine/gstreamer/plugins/BUILD.gn @@ -17,5 +17,6 @@ group("media_engine_gst_plugins") { deps = [ "source/audiocapture:gst_audio_capture_src", "sink/audiosink:gst_audio_server_sink", + "source/videocapture:gst_surface_video_src", ] } \ No newline at end of file -- Gitee From 67326a5cd7d1c05765f327a7a07231ab9dbb2248 Mon Sep 17 00:00:00 2001 From: XUXIAOBO Date: Wed, 25 Aug 2021 11:26:17 +0000 Subject: [PATCH 3/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=9F=B3=E9=87=8Fbug=20S?= =?UTF-8?q?igned-off-by:=20XUXIAOBO=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/engine/gstreamer/player/gst_player_ctrl.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/engine/gstreamer/player/gst_player_ctrl.cpp b/services/engine/gstreamer/player/gst_player_ctrl.cpp index 9ef714acd..00fe3be9b 100644 --- a/services/engine/gstreamer/player/gst_player_ctrl.cpp +++ b/services/engine/gstreamer/player/gst_player_ctrl.cpp @@ -287,7 +287,8 @@ void GstPlayerCtrl::SetVolume(float leftVolume, float rightVolume) std::unique_lock lock(mutex_); AudioStandard::AudioSystemManager *audioManager = AudioStandard::AudioSystemManager::GetInstance(); CHECK_AND_RETURN_LOG(audioManager != nullptr, "audioManager is nullptr"); - int32_t ret = audioManager->SetVolume(AudioStandard::AudioSystemManager::AudioVolumeType::STREAM_MUSIC, leftVolume); + int32_t volume = static_cast(VOLUME_TO_SYSTEM_VOLUME * leftVolume); + int32_t ret = audioManager->SetVolume(AudioStandard::AudioSystemManager::AudioVolumeType::STREAM_MUSIC, volume); CHECK_AND_RETURN_LOG(ret == AudioStandard::SUCCESS, "set volume fail"); OnVolumeChange(); } -- Gitee From af4e5fe7d67f2aab59f1c6052ad1dc313130305a Mon Sep 17 00:00:00 2001 From: XUXIAOBO Date: Wed, 25 Aug 2021 11:27:56 +0000 Subject: [PATCH 4/6] =?UTF-8?q?=E5=85=B3=E9=97=AD=E6=89=93=E6=A1=A9?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=BC=96=E8=AF=91=20Signed-off-by:=20XUXIAOB?= =?UTF-8?q?O=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/engine/gstreamer/plugins/source/videocapture/BUILD.gn | 1 - 1 file changed, 1 deletion(-) diff --git a/services/engine/gstreamer/plugins/source/videocapture/BUILD.gn b/services/engine/gstreamer/plugins/source/videocapture/BUILD.gn index ba07ff5ba..bfc1e0402 100644 --- a/services/engine/gstreamer/plugins/source/videocapture/BUILD.gn +++ b/services/engine/gstreamer/plugins/source/videocapture/BUILD.gn @@ -57,7 +57,6 @@ ohos_shared_library("gst_surface_video_src") { "src/gst_surface_video_src.cpp", "src/video_capture_factory.cpp", "src/video_capture_sf_impl.cpp", - "src/video_capture_stub_impl.cpp", "src/video_capture_sf_es_avc_impl.cpp", ] -- Gitee From 0f2102b7ad2b0c4151902a373e6e77ae7452d006 Mon Sep 17 00:00:00 2001 From: XUXIAOBO Date: Wed, 25 Aug 2021 11:28:31 +0000 Subject: [PATCH 5/6] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20serv?= =?UTF-8?q?ices/engine/gstreamer/plugins/source/videocapture/include/video?= =?UTF-8?q?=5Fcapture=5Fstub=5Fimpl.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../include/video_capture_stub_impl.h | 74 ------------------- 1 file changed, 74 deletions(-) delete mode 100644 services/engine/gstreamer/plugins/source/videocapture/include/video_capture_stub_impl.h diff --git a/services/engine/gstreamer/plugins/source/videocapture/include/video_capture_stub_impl.h b/services/engine/gstreamer/plugins/source/videocapture/include/video_capture_stub_impl.h deleted file mode 100644 index b8d362d10..000000000 --- a/services/engine/gstreamer/plugins/source/videocapture/include/video_capture_stub_impl.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (C) 2021 Huawei Device Co., Ltd. - * 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef VIDEO_CAPTURE_STUB_IMPL_H -#define VIDEO_CAPTURE_STUB_IMPL_H - -#include -#include "video_capture.h" - -namespace OHOS { -namespace Media { -class VideoCaptureStubImpl : public VideoCapture { -public: - VideoCaptureStubImpl(); - virtual ~VideoCaptureStubImpl(); - - int32_t Prepare() override; - int32_t Start() override; - int32_t Pause() override; - int32_t Resume() override; - int32_t Stop() override; - int32_t SetSurfaceWidth(uint32_t width) override; - int32_t SetSurfaceHeight(uint32_t height) override; - sptr GetSurface() override; - std::shared_ptr GetCodecBuffer() override; - std::shared_ptr GetFrameBuffer() override; - -private: - void SetSurfaceUserData(); - void ProcessSysProperty(); - std::shared_ptr GetFirstBuffer(); - const uint8_t *FindNextNal(const uint8_t *start, const uint8_t *end, uint32_t &nalLen); - void GetCodecData(const uint8_t *data, int32_t len, std::vector &sps, std::vector &pps, - uint32_t &nalSize); - class ConsumerListenerProxy : public IBufferConsumerListener { - public: - ConsumerListenerProxy(VideoCaptureStubImpl &owner) : owner_(owner) {} - ~ConsumerListenerProxy() = default; - void OnBufferAvailable() override; - private: - VideoCaptureStubImpl &owner_; - }; - void OnBufferAvailable(); - - uint32_t surfaceWidth_; - uint32_t surfaceHeight_; - std::unique_ptr testFile_; - int32_t frameSequence_; - char *codecData_; - uint32_t codecDataSize_; - uint32_t pts_; - sptr dataConSurface_; - sptr producerSurface_; - uint32_t frameWidth_; - uint32_t frameHeight_; - std::string filePath_; - const int32_t *frameLenArray_; - uint32_t nalSize_; -}; -} // namespace Media -} // namespace OHOS -#endif // VIDEO_CAPTURE_STUB_IMPL_H -- Gitee From 820a3f2c2103c34c2a9b5881e852a09457abb66a Mon Sep 17 00:00:00 2001 From: XUXIAOBO Date: Wed, 25 Aug 2021 11:28:41 +0000 Subject: [PATCH 6/6] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20serv?= =?UTF-8?q?ices/engine/gstreamer/plugins/source/videocapture/src/video=5Fc?= =?UTF-8?q?apture=5Fstub=5Fimpl.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/video_capture_stub_impl.cpp | 464 ------------------ 1 file changed, 464 deletions(-) delete mode 100644 services/engine/gstreamer/plugins/source/videocapture/src/video_capture_stub_impl.cpp diff --git a/services/engine/gstreamer/plugins/source/videocapture/src/video_capture_stub_impl.cpp b/services/engine/gstreamer/plugins/source/videocapture/src/video_capture_stub_impl.cpp deleted file mode 100644 index 238f64976..000000000 --- a/services/engine/gstreamer/plugins/source/videocapture/src/video_capture_stub_impl.cpp +++ /dev/null @@ -1,464 +0,0 @@ -/* - * Copyright (C) 2021 Huawei Device Co., Ltd. - * 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "video_capture_stub_impl.h" -#include -#include "param_wrapper.h" -#include "media_log.h" -#include "media_errors.h" -#include "scope_guard.h" -#include "securec.h" - -namespace { - constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "VideoCaptureStubImpl"}; - constexpr int32_t DEFAULT_FRAME_DURATION = 33425000; - constexpr uint32_t DEFAULT_SURFACE_WIDTH = 1920; - constexpr uint32_t DEFAULT_SURFACE_HEIGHT = 1080; - const std::string DEFAULT_FILE_PATH = "/data/h264_1920_1080.es"; - constexpr int32_t DEFAULT_SURFACE_QUEUE_SIZE = 3; - constexpr int32_t DEFAULT_SURFACE_SIZE = 1024 * 1024; -} - -namespace OHOS { -namespace Media { -const std::map SURFACE_MAP = { - { 1920, 1080 }, - { 1280, 720 }, - { 720, 480 }, -}; - -// frame offset of video frame in h264.es -const int32_t HIGH_VIDEO_FRAME_SIZE[226] = - { 31197, 785, 4084, 3532, 3468, 3449, 964, 3345, 3725, 3374, 3626, 1098, 3330, 3305, 3283, 6903, - 1388, 3594, 3791, 3671, 3678, 1350, 3466, 3475, 3591, 3551, 1092, 3318, 3207, 3340, 6454, 1420, 3506, - 3737, 3769, 3875, 1166, 3604, 3808, 3678, 3613, 1163, 3419, 4055, 3839, 6840, 1391, 3742, 4267, 4333, - 4253, 1280, 4139, 4480, 3799, 3854, 1026, 3388, 3292, 3562, 32673, 624, 4205, 3659, 3854, 3813, 835, - 3453, 3558, 3889, 3800, 986, 3364, 3790, 3379, 6625, 963, 3294, 3186, 3667, 3344, 1240, 3093, 3589, - 2611, 3353, 818, 3170, 3264, 3360, 4881, 1510, 2426, 2742, 2484, 2965, 764, 2250, 2159, 2130, 1529, 345, - 972, 662, 630, 35704, 1112, 2724, 2896, 3070, 3070, 1424, 2746, 3448, 3111, 3900, 1277, 2937, 3008, - 2708, 6146, 1463, 4032, 3275, 3035, 3222, 1125, 3337, 2851, 2632, 2664, 1626, 2309, 2547, 2582, 5632, - 1274, 2526, 2681, 3137, 2754, 1251, 2526, 3006, 2521, 2886, 814, 2414, 1951, 2356, 4652, 1421, 2469, - 2771, 2624, 3054, 796, 2463, 2205, 2783, 2803, 521, 4925, 6872, 8551, 31033, 9787, 10254, 13513, 14272, - 15447, 16037, 12133, 17105, 18514, 18871, 19697, 12643, 20666, 21506, 27645, 20742, 18800, 24027, 26226, - 26769, 28484, 11077, 26956, 28647, 30788, 33112, 9286, 29835, 32406, 35572, 30705, 2307, 26235, 28120, - 28891, 31952, 1072, 32607, 32968, 28632, 29051, 2010, 24916, 24573, 29684, 25905, 1089, 22359, 22607, - 21882, 24131, 1193, 22290, 24737, 26631, 28613, 1488, 24672, 24947 }; - -const int32_t MEDIUM_VIDEO_FRAME_SIZE[185] = - { 22103, 16744, 13748, 382, 9401, 13781, 9113, 435, 12823, 8412, 12119, 7238, 306, 12701, 8220, 6960, - 12702, 10172, 270, 7705, 10033, 9878, 7798, 1342, 41629, 15084, 8142, 580, 6127, 15182, 9513, 887, - 6110, 17744, 6294, 19037, 8607, 619, 7364, 18685, 11962, 1045, 7779, 19412, 7723, 18739, 9719, 541, - 7403, 17992, 12088, 707, 7347, 17487, 7522, 17811, 10219, 466, 8300, 17740, 13157, 532, 9212, 16816, - 8511, 14516, 8134, 539, 5909, 13708, 8014, 962, 5523, 15699, 5901, 16471, 9072, 554, 7728, 12270, - 47278, 20035, 5555, 4769, 22270, 7743, 1596, 5426, 20404, 9258, 753, 5792, 23463, 5836, 22047, 7143, - 740, 5207, 20719, 8349, 708, 4717, 19710, 4223, 19145, 5435, 625, 4556, 18769, 7153, 1020, 4168, - 17812, 4103, 18842, 5908, 680, 4841, 17715, 8151, 677, 4729, 16668, 4822, 21720, 7800, 2387, 4916, - 20115, 8407, 682, 5056, 19555, 5056, 21030, 6721, 600, 4910, 15224, 4256, 24242, 8168, 1992, 6759, - 20484, 6797, 1233, 5343, 20065, 10362, 631, 7088, 23135, 10767, 6673, 733, 20229, 8049, 5457, 462, - 24554, 10294, 5752, 4203, 722, 12639, 13072, 7066, 8082, 644, 16680, 11435, 7087, 7500, 13629, 8425, - 662, 4856, 10455, 8992, 766, 3450, 8923, 8041, 3603 }; - -const int32_t LOW_VIDEO_FRAME_SIZE[159] = - { 11963, 8677, 6697, 203, 4613, 8207, 4301, 7544, 4668, 139, 3383, 6434, 4685, 127, 3132, 7275, 3236, - 6232, 4464, 113, 3988, 4877, 3920, 510, 22070, 7263, 3637, 198, 2808, 7721, 4485, 261, 2815, 8954, - 2956, 9993, 4237, 209, 3563, 9617, 5869, 298, 3710, 9959, 3830, 9431, 4822, 151, 3699, 8976, 5808, - 235, 3679, 8821, 3691, 8684, 5162, 167, 4281, 8567, 8900, 5603, 157, 4697, 7274, 5552, 343, 3200, - 6821, 2505, 7542, 3238, 162, 2836, 7833, 4802, 270, 3412, 7253, 4400, 25342, 10450, 3096, 2121, 321, - 10916, 3913, 2447, 2377, 10279, 3460, 283, 2507, 11240, 4692, 192, 2325, 9849, 2036, 9591, 2603, 267, - 2025, 9519, 3120, 237, 1804, 9095, 1856, 8460, 2487, 319, 1774, 9010, 3358, 223, 2010, 9124, 1917, - 8491, 2851, 280, 2265, 9890, 3883, 433, 2235, 8336, 2028, 777, 11778, 4398, 2342, 2361, 10531, 2973, - 209, 2042, 7375, 1318, 11809, 3893, 1808, 1870, 9397, 2915, 318, 2231, 9576, 4471, 213, 3023, 10621, - 2734, 9527, 3700, 200, 2381, 4096 }; - -VideoCaptureStubImpl::VideoCaptureStubImpl() - : surfaceWidth_(DEFAULT_SURFACE_WIDTH), - surfaceHeight_(DEFAULT_SURFACE_HEIGHT), - testFile_(nullptr), - frameSequence_(0), - codecData_(nullptr), - codecDataSize_(0), - pts_(0), - dataConSurface_(nullptr), - frameWidth_(DEFAULT_SURFACE_WIDTH), - frameHeight_(DEFAULT_SURFACE_HEIGHT), - filePath_(DEFAULT_FILE_PATH), - frameLenArray_(HIGH_VIDEO_FRAME_SIZE), - nalSize_(0) -{ -} - -VideoCaptureStubImpl::~VideoCaptureStubImpl() -{ - Stop(); -} - -int32_t VideoCaptureStubImpl::Prepare() -{ - auto iter = SURFACE_MAP.find(surfaceWidth_); - CHECK_AND_RETURN_RET_LOG(iter != SURFACE_MAP.end(), MSERR_INVALID_VAL, "illegal surface width"); - CHECK_AND_RETURN_RET_LOG(surfaceHeight_ == iter->second, MSERR_INVALID_VAL, "illegal surface height"); - - ProcessSysProperty(); - - testFile_ = std::make_unique(); - CHECK_AND_RETURN_RET_LOG(testFile_ != nullptr, MSERR_NO_MEMORY, "no memory"); - testFile_->open(filePath_, std::ios::in | std::ios::binary); - CHECK_AND_RETURN_RET_LOG(testFile_->is_open(), MSERR_UNKNOWN, "open local file fail"); - - sptr consumerSurface = Surface::CreateSurfaceAsConsumer(); - CHECK_AND_RETURN_RET_LOG(consumerSurface != nullptr, MSERR_UNKNOWN, "create surface fail"); - - sptr listenerProxy = new (std::nothrow) ConsumerListenerProxy(*this); - CHECK_AND_RETURN_RET_LOG(listenerProxy != nullptr, MSERR_UNKNOWN, "create consumer listener fail"); - - if (consumerSurface->RegisterConsumerListener(listenerProxy) != SURFACE_ERROR_OK) { - MEDIA_LOGW("register consumer listener fail"); - } - - sptr producer = consumerSurface->GetProducer(); - CHECK_AND_RETURN_RET_LOG(producer != nullptr, MSERR_UNKNOWN, "get producer fail"); - sptr producerSurface = Surface::CreateSurfaceAsProducer(producer); - CHECK_AND_RETURN_RET_LOG(producerSurface != nullptr, MSERR_UNKNOWN, "get producer fail"); - - dataConSurface_ = consumerSurface; - producerSurface_ = producerSurface; - - SetSurfaceUserData(); - return MSERR_OK; -} - -int32_t VideoCaptureStubImpl::Start() -{ - MEDIA_LOGD("Start"); - return MSERR_OK; -} - -int32_t VideoCaptureStubImpl::Pause() -{ - MEDIA_LOGD("Pause"); - return MSERR_OK; -} - -int32_t VideoCaptureStubImpl::Resume() -{ - MEDIA_LOGD("Resume"); - return MSERR_OK; -} - -int32_t VideoCaptureStubImpl::Stop() -{ - MEDIA_LOGD("Stop"); - - if ((testFile_ != nullptr) && (testFile_->is_open())) { - testFile_->close(); - } - testFile_ = nullptr; - if (codecData_ != nullptr) { - free(codecData_); - codecData_ = nullptr; - } - - if (dataConSurface_ != nullptr) { - if (dataConSurface_->UnregisterConsumerListener() != SURFACE_ERROR_OK) { - MEDIA_LOGW("deregister consumer listener fail"); - } - } - - return MSERR_OK; -} - -int32_t VideoCaptureStubImpl::SetSurfaceWidth(uint32_t width) -{ - surfaceWidth_ = width; - return MSERR_OK; -} - -int32_t VideoCaptureStubImpl::SetSurfaceHeight(uint32_t height) -{ - surfaceHeight_ = height; - return MSERR_OK; -} - -sptr VideoCaptureStubImpl::GetSurface() -{ - CHECK_AND_RETURN_RET_LOG(producerSurface_ != nullptr, nullptr, "surface not created"); - return producerSurface_; -} - -void VideoCaptureStubImpl::ConsumerListenerProxy::OnBufferAvailable() -{ - return owner_.OnBufferAvailable(); -} - -void VideoCaptureStubImpl::OnBufferAvailable() -{ - return; -} - -std::shared_ptr VideoCaptureStubImpl::GetCodecBuffer() -{ - CHECK_AND_RETURN_RET_LOG(testFile_ != nullptr, nullptr, "ifstream is nullptr"); - char *buffer = (char *) malloc(sizeof(char) * (*frameLenArray_) + 1); - CHECK_AND_RETURN_RET_LOG(buffer != nullptr, nullptr, "no memory"); - - ON_SCOPE_EXIT(0) { free(buffer); }; - - testFile_->read(buffer, *frameLenArray_); - std::vector sps; - std::vector pps; - GetCodecData(reinterpret_cast(buffer), *frameLenArray_, sps, pps, nalSize_); - CHECK_AND_RETURN_RET_LOG(nalSize_ > 0 && sps.size() > 0 && pps.size() > 0, nullptr, "illegal codec buffer"); - - // 11 is the length of AVCDecoderConfigurationRecord field except sps and pps - uint32_t codecBufferSize = sps.size() + pps.size() + 11; - GstBuffer *codec = gst_buffer_new_allocate(nullptr, codecBufferSize, nullptr); - CHECK_AND_RETURN_RET_LOG(codec != nullptr, nullptr, "no memory"); - GstMapInfo map; - CHECK_AND_RETURN_RET_LOG(gst_buffer_map(codec, &map, GST_MAP_READ) == TRUE, nullptr, "gst_buffer_map fail"); - - ON_SCOPE_EXIT(1) { gst_buffer_unmap(codec, &map); }; - ON_SCOPE_EXIT(2) { gst_buffer_unref(codec); }; - - uint32_t offset = 0; - map.data[offset++] = 0x01; // configurationVersion - map.data[offset++] = sps[1]; // AVCProfileIndication - map.data[offset++] = sps[2]; // profile_compatibility - map.data[offset++] = sps[3]; // AVCLevelIndication - map.data[offset++] = 0xff; // lengthSizeMinusOne - - map.data[offset++] = 0xe0 | 0x01; // numOfSequenceParameterSets - map.data[offset++] = (sps.size() >> 8) & 0xff; // sequenceParameterSetLength high 8 bits - map.data[offset++] = sps.size() & 0xff; // sequenceParameterSetLength low 8 bits - // sequenceParameterSetNALUnit - CHECK_AND_RETURN_RET_LOG(memcpy_s(map.data + offset, codecBufferSize - offset, &sps[0], sps.size()) == EOK, - nullptr, "memcpy_s fail"); - offset += sps.size(); - - map.data[offset++] = 0x01; // numOfPictureParameterSets - map.data[offset++] = (pps.size() >> 8) & 0xff; // pictureParameterSetLength high 8 bits - map.data[offset++] = pps.size() & 0xff; // pictureParameterSetLength low 8 bits - // pictureParameterSetNALUnit - CHECK_AND_RETURN_RET_LOG(memcpy_s(map.data + offset, codecBufferSize - offset, &pps[0], pps.size()) == EOK, - nullptr, "memcpy_s fail"); - - std::shared_ptr codecBuffer = std::make_shared(); - CHECK_AND_RETURN_RET_LOG(codecBuffer != nullptr, nullptr, "no memory"); - codecBuffer->width = frameWidth_; - codecBuffer->height = frameHeight_; - codecBuffer->segmentStart = 0; - codecBuffer->gstCodecBuffer = codec; - codecData_ = buffer; - codecDataSize_ = nalSize_ + sps.size() + nalSize_ + pps.size(); - - CANCEL_SCOPE_EXIT_GUARD(0); - CANCEL_SCOPE_EXIT_GUARD(2); - - return codecBuffer; -} - -std::shared_ptr VideoCaptureStubImpl::GetFrameBuffer() -{ - CHECK_AND_RETURN_RET_LOG(testFile_ != nullptr, nullptr, "ifstream is nullptr"); - g_usleep(30000); - if (frameSequence_ == 0 && codecData_ == nullptr) { - (void)GetCodecBuffer(); - } - - if (frameSequence_ == 0) { - return GetFirstBuffer(); - } - - char *buffer = (char *)malloc(sizeof(char) * (*frameLenArray_) + 1); - CHECK_AND_RETURN_RET_LOG(buffer != nullptr, nullptr, "no memory"); - - ON_SCOPE_EXIT(0) { - free(buffer); - buffer = nullptr; - }; - - testFile_->read(buffer, *frameLenArray_); - - uint32_t frameSize = (*frameLenArray_) - nalSize_; - // 0x00000001, 0x000001 - if (nalSize_ == 4) { - buffer[0] = (char)((frameSize >> 24) & 0xff); - buffer[1] = (char)((frameSize >> 16) & 0xff); - buffer[2] = (char)((frameSize >> 8) & 0xff); - buffer[3] = (char)(frameSize & 0xff); - } else { - buffer[0] = (char)((frameSize >> 16) & 0xff); - buffer[1] = (char)((frameSize >> 8) & 0xff); - buffer[2] = (char)(frameSize & 0xff); - } - - GstBuffer *gstBuffer = gst_buffer_new_allocate(nullptr, *frameLenArray_, nullptr); - CHECK_AND_RETURN_RET_LOG(gstBuffer != nullptr, nullptr, "no memory"); - - ON_SCOPE_EXIT(1) { gst_buffer_unref(gstBuffer); }; - - gsize size = gst_buffer_fill(gstBuffer, 0, buffer, *frameLenArray_); - CHECK_AND_RETURN_RET_LOG(size == static_cast(*frameLenArray_), nullptr, - "unkonwn error during gst_buffer_fill"); - - std::shared_ptr frameBuffer = std::make_shared(); - frameBuffer->keyFrameFlag = 0; - frameBuffer->timeStamp = pts_; - pts_ += DEFAULT_FRAME_DURATION; - frameBuffer->gstBuffer = gstBuffer; - frameBuffer->duration = DEFAULT_FRAME_DURATION; - frameBuffer->size = *frameLenArray_; - frameLenArray_++; - - CANCEL_SCOPE_EXIT_GUARD(1); - return frameBuffer; -} - -void VideoCaptureStubImpl::SetSurfaceUserData() -{ - SurfaceError ret = dataConSurface_->SetUserData("surface_width", std::to_string(surfaceWidth_)); - if (ret != SURFACE_ERROR_OK) { - MEDIA_LOGW("set surface width fail"); - } - ret = dataConSurface_->SetUserData("surface_height", std::to_string(surfaceHeight_)); - if (ret != SURFACE_ERROR_OK) { - MEDIA_LOGW("set surface height fail"); - } - ret = dataConSurface_->SetQueueSize(DEFAULT_SURFACE_QUEUE_SIZE); - if (ret != SURFACE_ERROR_OK) { - MEDIA_LOGW("set queue size fail"); - } - ret = dataConSurface_->SetUserData("surface_size", std::to_string(DEFAULT_SURFACE_SIZE)); - if (ret != SURFACE_ERROR_OK) { - MEDIA_LOGW("set surface size fail"); - } -} - -void VideoCaptureStubImpl::ProcessSysProperty() -{ - std::string videoQuality; - int32_t res = OHOS::system::GetStringParameter("sys.media.videosrc.stub.quality", videoQuality, ""); - if (res != 0 || videoQuality.empty()) { - videoQuality = "high"; - } - - if (videoQuality == "high") { - frameWidth_ = 1920; - frameHeight_ = 1080; - frameLenArray_ = HIGH_VIDEO_FRAME_SIZE; - filePath_ = "/data/h264_1920_1080.es"; - } else if (videoQuality == "medium") { - frameWidth_ = 1280; - frameHeight_ = 720; - frameLenArray_ = MEDIUM_VIDEO_FRAME_SIZE; - filePath_ = "/data/h264_1280_720.es"; - } else if (videoQuality == "low") { - frameWidth_ = 720; - frameHeight_ = 480; - frameLenArray_ = LOW_VIDEO_FRAME_SIZE; - filePath_ = "/data/h264_720_480.es"; - } -} - -std::shared_ptr VideoCaptureStubImpl::GetFirstBuffer() -{ - CHECK_AND_RETURN_RET_LOG(codecData_ != nullptr, nullptr, "no valid codec data"); - - ON_SCOPE_EXIT(0) { - free(codecData_); - codecData_ = nullptr; - }; - - int32_t bufferSize = *frameLenArray_ - codecDataSize_; - GstBuffer *gstBuffer = gst_buffer_new_allocate(nullptr, bufferSize, nullptr); - CHECK_AND_RETURN_RET_LOG(gstBuffer != nullptr, nullptr, "no memory"); - - ON_SCOPE_EXIT(1) { gst_buffer_unref(gstBuffer); }; - - // 0x00000001, 0x000001 - uint32_t frameSize = bufferSize - nalSize_; - if (nalSize_ == 4) { - codecData_[codecDataSize_] = (char)((frameSize >> 24) & 0xff); - codecData_[codecDataSize_ + 1] = (char)((frameSize >> 16) & 0xff); - codecData_[codecDataSize_ + 2] = (char)((frameSize >> 8) & 0xff); - codecData_[codecDataSize_ + 3] = (char)(frameSize & 0xff); - } else { - codecData_[codecDataSize_] = (char)((frameSize >> 16) & 0xff); - codecData_[codecDataSize_ + 1] = (char)((frameSize >> 8) & 0xff); - codecData_[codecDataSize_ + 2] = (char)(frameSize & 0xff); - } - - gsize size = gst_buffer_fill(gstBuffer, 0, codecData_ + codecDataSize_, bufferSize); - CHECK_AND_RETURN_RET_LOG(size == (gsize)bufferSize, nullptr, "unkonwn error during gst_buffer_fill"); - - std::shared_ptr frameBuffer = std::make_shared(); - CHECK_AND_RETURN_RET_LOG(frameBuffer != nullptr, nullptr, "no memory"); - const uint32_t firstFrameDuration = 40000000; - frameBuffer->keyFrameFlag = 0; - frameBuffer->timeStamp = pts_; - pts_ += firstFrameDuration; - frameBuffer->gstBuffer = gstBuffer; - frameBuffer->duration = firstFrameDuration; - frameBuffer->size = bufferSize; - frameSequence_++; - frameLenArray_++; - - CANCEL_SCOPE_EXIT_GUARD(1); - return frameBuffer; -} - -const uint8_t *VideoCaptureStubImpl::FindNextNal(const uint8_t *start, const uint8_t *end, uint32_t &nalSize) -{ - CHECK_AND_RETURN_RET(start != nullptr && end != nullptr, nullptr); - while (start <= end - 3) { - // 0x000001 Nal - if (start[0] == 0x00 && start[1] == 0x00 && start[2] == 0x01) { - nalSize = 3; - return start; - } - // 0x00000001 Nal - if (start[0] == 0x00 && start[1] == 0x00 && start[2] == 0x00 && start[3] == 0x01) { - nalSize = 4; - return start; - } - start++; - } - return end; -} - -void VideoCaptureStubImpl::GetCodecData(const uint8_t *data, int32_t len, - std::vector &sps, std::vector &pps, uint32_t &nalSize) -{ - CHECK_AND_RETURN(data != nullptr); - const uint8_t *end = data + len; - const uint8_t *pBegin = data; - const uint8_t *pEnd = nullptr; - while (pBegin < end) { - pBegin = FindNextNal(pBegin, end, nalSize); - if (pBegin == end) { - break; - } - pBegin += nalSize; - pEnd = FindNextNal(pBegin, end, nalSize); - if (((*pBegin) & 0x1F) == 0x07) { // sps - sps.assign(pBegin, pBegin + static_cast(pEnd - pBegin)); - } - if (((*pBegin) & 0x1F) == 0x08) { // pps - pps.assign(pBegin, pBegin + static_cast(pEnd - pBegin)); - } - pBegin = pEnd; - } -} -} // namespace Media -} // namespace OHOS \ No newline at end of file -- Gitee