1f6603c60Sopenharmony_ci/*
2f6603c60Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License.
5f6603c60Sopenharmony_ci * You may obtain a copy of the License at
6f6603c60Sopenharmony_ci *
7f6603c60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f6603c60Sopenharmony_ci *
9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and
13f6603c60Sopenharmony_ci * limitations under the License.
14f6603c60Sopenharmony_ci */
15f6603c60Sopenharmony_ci
16f6603c60Sopenharmony_ci#include "ActsTestMediaUtils.h"
17f6603c60Sopenharmony_ci
18f6603c60Sopenharmony_ciusing namespace std;
19f6603c60Sopenharmony_ci
20f6603c60Sopenharmony_ci/* *
21f6603c60Sopenharmony_ci * get current dir
22f6603c60Sopenharmony_ci * @return  string current file path of the test suits
23f6603c60Sopenharmony_ci */
24f6603c60Sopenharmony_cistring GetCurDir()
25f6603c60Sopenharmony_ci{
26f6603c60Sopenharmony_ci    string filePath = "";
27f6603c60Sopenharmony_ci    char *buffer;
28f6603c60Sopenharmony_ci    if ((buffer = getcwd(NULL, 0)) == NULL) {
29f6603c60Sopenharmony_ci        perror("get file path error");
30f6603c60Sopenharmony_ci    } else {
31f6603c60Sopenharmony_ci        printf("Current Dir: %s\r\n", buffer);
32f6603c60Sopenharmony_ci        filePath = buffer;
33f6603c60Sopenharmony_ci        free(buffer);
34f6603c60Sopenharmony_ci    }
35f6603c60Sopenharmony_ci    return filePath + "/";
36f6603c60Sopenharmony_ci}
37f6603c60Sopenharmony_ci
38f6603c60Sopenharmony_ci/* *
39f6603c60Sopenharmony_ci * check file exist
40f6603c60Sopenharmony_ci * @param filename filename
41f6603c60Sopenharmony_ci * @return  check result
42f6603c60Sopenharmony_ci */
43f6603c60Sopenharmony_ciint32_t FileCheck(const char *filename)
44f6603c60Sopenharmony_ci{
45f6603c60Sopenharmony_ci    fstream fileTmp;
46f6603c60Sopenharmony_ci    fileTmp.open(filename);
47f6603c60Sopenharmony_ci    if (!fileTmp) {
48f6603c60Sopenharmony_ci        cout << "file is not exist!" << endl;
49f6603c60Sopenharmony_ci        return RET_ERR;
50f6603c60Sopenharmony_ci    } else {
51f6603c60Sopenharmony_ci        cout << "file is exist!" << endl;
52f6603c60Sopenharmony_ci        fileTmp.close();
53f6603c60Sopenharmony_ci        return RET_OK;
54f6603c60Sopenharmony_ci    }
55f6603c60Sopenharmony_ci}
56f6603c60Sopenharmony_ci
57f6603c60Sopenharmony_ci/* *
58f6603c60Sopenharmony_ci * Save Capture
59f6603c60Sopenharmony_ci * @return
60f6603c60Sopenharmony_ci */
61f6603c60Sopenharmony_ciint32_t SampleSaveCapture(string testPath, const char *p, uint32_t size)
62f6603c60Sopenharmony_ci{
63f6603c60Sopenharmony_ci    cout << "Start saving picture" << endl;
64f6603c60Sopenharmony_ci    string filePath = "";
65f6603c60Sopenharmony_ci    struct timeval tv;
66f6603c60Sopenharmony_ci    gettimeofday(&tv, NULL);
67f6603c60Sopenharmony_ci    struct tm *ltm = localtime(&tv.tv_sec);
68f6603c60Sopenharmony_ci    if (ltm != nullptr) {
69f6603c60Sopenharmony_ci        ostringstream ss("Capture_");
70f6603c60Sopenharmony_ci        ss << "Capture" << ltm->tm_hour << "_" << ltm->tm_min << "_" << ltm->tm_sec << ".jpg";
71f6603c60Sopenharmony_ci        filePath = testPath + ss.str();
72f6603c60Sopenharmony_ci        ofstream pic(testPath + ss.str(), ofstream::out | ofstream::trunc);
73f6603c60Sopenharmony_ci        cout << "write " << size << " bytes" << endl;
74f6603c60Sopenharmony_ci        pic.write(p, size);
75f6603c60Sopenharmony_ci        cout << "Saving picture end" << endl;
76f6603c60Sopenharmony_ci    }
77f6603c60Sopenharmony_ci    const char *filename = filePath.data();
78f6603c60Sopenharmony_ci    int32_t ret = FileCheck(filename);
79f6603c60Sopenharmony_ci    return ret;
80f6603c60Sopenharmony_ci}
81f6603c60Sopenharmony_ci
82f6603c60Sopenharmony_ci/* *
83f6603c60Sopenharmony_ci * get recorder fd
84f6603c60Sopenharmony_ci * @return fd
85f6603c60Sopenharmony_ci */
86f6603c60Sopenharmony_ciint32_t SampleGetRecordFd(string &recordFilePath)
87f6603c60Sopenharmony_ci{
88f6603c60Sopenharmony_ci    struct timeval tv = {};
89f6603c60Sopenharmony_ci    gettimeofday(&tv, nullptr);
90f6603c60Sopenharmony_ci    struct tm *ltm = localtime(&tv.tv_sec);
91f6603c60Sopenharmony_ci    int32_t fd = FdNull;
92f6603c60Sopenharmony_ci    if (ltm != nullptr) {
93f6603c60Sopenharmony_ci        ostringstream ss("Recorder_");
94f6603c60Sopenharmony_ci        ss << "Record" << ltm->tm_hour << "_" << ltm->tm_min << "_" << ltm->tm_sec << ".mp4";
95f6603c60Sopenharmony_ci        recordFilePath = recordFilePath + ss.str();
96f6603c60Sopenharmony_ci        fd = open(recordFilePath.c_str(), O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
97f6603c60Sopenharmony_ci        cout << "Open " << recordFilePath << endl;
98f6603c60Sopenharmony_ci        if (fd == FdNull) {
99f6603c60Sopenharmony_ci            cout << "Open recorder file failed. err=" << strerror(errno) << endl;
100f6603c60Sopenharmony_ci        }
101f6603c60Sopenharmony_ci    }
102f6603c60Sopenharmony_ci    return fd;
103f6603c60Sopenharmony_ci}