• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "ptsandindexconversion_sample.h"
16 #include <cstddef>
17 #include <cstdint>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <sys/stat.h>
21 #include <iostream>
22 
23 using namespace OHOS;
24 using namespace OHOS::Media;
25 using namespace std;
26 
27 const size_t INDEX_MAX = 32;
28 const size_t EXPECT_SIZE = 64;
29 const size_t TRACKINDEX_MAX = 32;
30 const size_t RELATIVE_PRESENTATION_TIMEUS_MAX = 64;
31 
~PtsAndIndexConversion()32 PtsAndIndexConversion::~PtsAndIndexConversion()
33 {
34     if (fd_ > 0) {
35         close(fd_);
36     }
37     fd_ = -1;
38 
39     index_ = 0;
40     trackIndex_ = 0;
41     relativePresentationTimeUs_ = 0;
42 }
43 
Init(const uint8_t * data,size_t size)44 bool PtsAndIndexConversion::Init(const uint8_t *data, size_t size)
45 {
46     if (size < EXPECT_SIZE) {
47         return false;
48     }
49     fd_ = open(filePath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
50     if (fd_ < 0) {
51         return false;
52     }
53     int len = write(fd_, data, size);
54     if (len <= 0) {
55         close(fd_);
56         fd_ = -1;
57         return false;
58     }
59     std::string uri = "fd://" + std::to_string(fd_) + "?offset=0&size=" + std::to_string(size);
60     timeandindexConversions_ = std::make_shared<TimeAndIndexConversion>();
61     std::shared_ptr<MediaSource> mediaSource = std::make_shared<MediaSource>(uri);
62     Status ret = timeandindexConversions_->SetDataSource(mediaSource);
63     close(fd_);
64     fd_ = -1;
65     if (ret == Status::OK) {
66         index_ = data[size - INDEX_MAX];
67         trackIndex_ = data[size - TRACKINDEX_MAX];
68         relativePresentationTimeUs_ = data[size - RELATIVE_PRESENTATION_TIMEUS_MAX];
69         return true;
70     }
71     timeandindexConversions_ = nullptr;
72     return false;
73 }
74 
RunNormalTimeAndIndexConversions()75 void PtsAndIndexConversion::RunNormalTimeAndIndexConversions()
76 {
77     timeandindexConversions_->GetIndexByRelativePresentationTimeUs(trackIndex_, relativePresentationTimeUs_, index_);
78     timeandindexConversions_->GetRelativePresentationTimeUsByIndex(trackIndex_, index_, relativePresentationTimeUs_);
79     timeandindexConversions_->GetFirstVideoTrackIndex(trackIndex_);
80 }
81