• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "ActsTestMediaUtils.h"
17 
18 using namespace std;
19 
20 /* *
21  * get current dir
22  * @return  string current file path of the test suits
23  */
GetCurDir()24 string GetCurDir()
25 {
26     string filePath = "";
27     char *buffer;
28     if ((buffer = getcwd(NULL, 0)) == NULL) {
29         perror("get file path error");
30     } else {
31         printf("Current Dir: %s\r\n", buffer);
32         filePath = buffer;
33         free(buffer);
34     }
35     return filePath + "/";
36 }
37 
38 /* *
39  * check file exist
40  * @param filename filename
41  * @return  check result
42  */
FileCheck(const char * filename)43 int32_t FileCheck(const char *filename)
44 {
45     fstream fileTmp;
46     fileTmp.open(filename);
47     if (!fileTmp) {
48         cout << "file is not exist!" << endl;
49         return RET_ERR;
50     } else {
51         cout << "file is exist!" << endl;
52         fileTmp.close();
53         return RET_OK;
54     }
55 }
56 
57 /* *
58  * Save Capture
59  * @return
60  */
SampleSaveCapture(string testPath,const char * p,uint32_t size)61 int32_t SampleSaveCapture(string testPath, const char *p, uint32_t size)
62 {
63     cout << "Start saving picture" << endl;
64     string filePath = "";
65     struct timeval tv;
66     gettimeofday(&tv, NULL);
67     struct tm *ltm = localtime(&tv.tv_sec);
68     if (ltm != nullptr) {
69         ostringstream ss("Capture_");
70         ss << "Capture" << ltm->tm_hour << "_" << ltm->tm_min << "_" << ltm->tm_sec << ".jpg";
71         filePath = testPath + ss.str();
72         ofstream pic(testPath + ss.str(), ofstream::out | ofstream::trunc);
73         cout << "write " << size << " bytes" << endl;
74         pic.write(p, size);
75         cout << "Saving picture end" << endl;
76     }
77     const char *filename = filePath.data();
78     int32_t ret = FileCheck(filename);
79     return ret;
80 }
81 
82 /* *
83  * get recorder fd
84  * @return fd
85  */
SampleGetRecordFd(string & recordFilePath)86 int32_t SampleGetRecordFd(string &recordFilePath)
87 {
88     struct timeval tv = {};
89     gettimeofday(&tv, nullptr);
90     struct tm *ltm = localtime(&tv.tv_sec);
91     int32_t fd = FdNull;
92     if (ltm != nullptr) {
93         ostringstream ss("Recorder_");
94         ss << "Record" << ltm->tm_hour << "_" << ltm->tm_min << "_" << ltm->tm_sec << ".mp4";
95         recordFilePath = recordFilePath + ss.str();
96         fd = open(recordFilePath.c_str(), O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
97         cout << "Open " << recordFilePath << endl;
98         if (fd == FdNull) {
99             cout << "Open recorder file failed. err=" << strerror(errno) << endl;
100         }
101     }
102     return fd;
103 }