• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2024 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 "writer.h"
17 
18 #include "dp_log.h"
19 
20 namespace OHOS {
21 namespace CameraStandard {
22 namespace DeferredProcessing {
~Writer()23 Writer::~Writer()
24 {
25     outputMuxer_ = nullptr;
26 }
27 
Create(int32_t outputFd,const std::map<Media::Plugins::MediaType,std::shared_ptr<Track>> & trackMap)28 MediaManagerError Writer::Create(int32_t outputFd,
29     const std::map<Media::Plugins::MediaType, std::shared_ptr<Track>>& trackMap)
30 {
31     DP_CHECK_ERROR_RETURN_RET_LOG(outputFd == INVALID_FD, ERROR_FAIL, "outputFd is invalid: %{public}d.", outputFd);
32     DP_CHECK_ERROR_RETURN_RET_LOG(trackMap.empty(), ERROR_FAIL, "Finvalid track map.");
33 
34     outputFileFd_ = outputFd;
35     outputMuxer_ = std::make_shared<Muxer>();
36     DP_DEBUG_LOG("outputFd: %{public}d, track size: %{public}d", outputFileFd_, static_cast<int32_t>(trackMap.size()));
37     auto ret = outputMuxer_->Create(outputFileFd_, Plugins::OutputFormat::MPEG_4);
38     DP_CHECK_ERROR_RETURN_RET_LOG(ret != OK, ERROR_FAIL, "Create muxer failed.");
39 
40     ret = outputMuxer_->AddTracks(trackMap);
41     DP_CHECK_ERROR_RETURN_RET_LOG(ret != OK, ERROR_FAIL, "Add track failed.");
42 
43     return ret;
44 }
45 
Write(Media::Plugins::MediaType type,const std::shared_ptr<AVBuffer> & sample)46 MediaManagerError Writer::Write(Media::Plugins::MediaType type, const std::shared_ptr<AVBuffer>& sample)
47 {
48     DP_DEBUG_LOG("pts: %{public}" PRId64 ", flag: %{public}d", sample->pts_, sample->flag_);
49     if (sample->memory_ != nullptr) {
50         DP_DEBUG_LOG("sample size: %{public}d", sample->memory_->GetSize());
51     }
52 
53     DP_CHECK_RETURN_RET_LOG(type == Media::Plugins::MediaType::VIDEO && sample->pts_ < lastPause_, OK,
54         "MediaType: %{public}d, drop feame pts: %{public}" PRId64, type, sample->pts_);
55 
56     auto ret = outputMuxer_->WriteStream(type, sample);
57     DP_CHECK_RETURN_RET_LOG(ret != OK, ERROR_FAIL,
58         "Write sample failed, type: %{public}d", static_cast<int32_t>(type));
59     return OK;
60 }
61 
Start()62 MediaManagerError Writer::Start()
63 {
64     DP_DEBUG_LOG("entered.");
65     DP_CHECK_RETURN_RET(started_, OK);
66     DP_CHECK_ERROR_RETURN_RET_LOG(outputMuxer_ == nullptr, ERROR_FAIL, "Failed to start, muxer is nullptr.");
67 
68     auto ret = outputMuxer_->Start();
69     DP_CHECK_ERROR_RETURN_RET_LOG(ret != OK, ERROR_FAIL, "Start failed, ret: %{public}d", ret);
70 
71     started_ = true;
72     return OK;
73 }
74 
Stop()75 MediaManagerError Writer::Stop()
76 {
77     DP_DEBUG_LOG("entered.");
78     auto ret = outputMuxer_->Stop();
79     DP_CHECK_ERROR_RETURN_RET_LOG(ret != OK, ERROR_FAIL, "Stop failed, ret: %{public}d", ret);
80 
81     started_ = false;
82     return OK;
83 }
84 
AddMediaInfo(const std::shared_ptr<MediaInfo> & mediaInfo)85 MediaManagerError Writer::AddMediaInfo(const std::shared_ptr<MediaInfo>& mediaInfo)
86 {
87     DP_DEBUG_LOG("entered.");
88     auto ret = outputMuxer_->AddMediaInfo(mediaInfo);
89     DP_CHECK_ERROR_RETURN_RET_LOG(ret != OK, ERROR_FAIL, "Add media info failed.");
90     return OK;
91 }
92 
AddUserMeta(const std::shared_ptr<Meta> & userMeta)93 MediaManagerError Writer::AddUserMeta(const std::shared_ptr<Meta>& userMeta)
94 {
95     DP_DEBUG_LOG("entered.");
96     auto ret = outputMuxer_->AddUserMeta(userMeta);
97     DP_CHECK_ERROR_RETURN_RET_LOG(ret != OK, ERROR_FAIL, "Add user meta info failed.");
98     return OK;
99 }
100 } // namespace DeferredProcessing
101 } // namespace CameraStandard
102 } // namespace OHOS
103