1 /*
2 * Copyright (c) 2023-2023 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<TrackType,const std::shared_ptr<Track>> & trackMap)28 MediaManagerError Writer::Create(int32_t outputFd, const std::map<TrackType, const std::shared_ptr<Track>>& trackMap)
29 {
30 DP_CHECK_ERROR_RETURN_RET_LOG(outputFd == INVALID_FD, ERROR_FAIL, "outputFd is invalid: %{public}d.", outputFd);
31 DP_CHECK_ERROR_RETURN_RET_LOG(trackMap.empty(), ERROR_FAIL, "finvalid track map.");
32
33 outputFileFd_ = outputFd;
34 outputMuxer_ = std::make_shared<Muxer>();
35 DP_DEBUG_LOG("outputFd: %{public}d, track size: %{public}d",
36 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(TrackType type,const std::shared_ptr<AVBuffer> & sample)46 MediaManagerError Writer::Write(TrackType type, const std::shared_ptr<AVBuffer>& sample)
47 {
48 DP_DEBUG_LOG("pts: %{public}lld, flag: %{public}d", static_cast<long long>(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(sample->pts_ < lastPause_ && type == TrackType::AV_KEY_VIDEO_TYPE,
54 OK, "drop feame pts: %{public}lld", static_cast<long long>(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 } // namespace DeferredProcessing
93 } // namespace CameraStandard
94 } // namespace OHOS
95