1 /*
2  * Copyright (c) 2020-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 #ifndef RECORDER_SINK_H
17 #define RECORDER_SINK_H
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <memory>
22 #include <pthread.h>
23 #include <queue>
24 #include <semaphore.h>
25 #include <time.h>
26 
27 #include "media_errors.h"
28 #include "media_info.h"
29 #include "format.h"
30 #include "format_type.h"
31 #include "recorder.h"
32 
33 namespace OHOS {
34 namespace Media {
35 using namespace std;
36 
37 struct MessageData {
38     int32_t event;
39     int32_t extra;
40     bool isInfo;
41 };
42 
43 class RecorderSink {
44 public:
45 
46     RecorderSink();
47     virtual ~RecorderSink();
48 
49     int32_t AddTrackSource(const TrackSource &trackSource, int32_t &trackId);
50     int32_t WriteData(int32_t trackId, FormatFrame &frameData) const;
51     int32_t SetOutputFormat(OutputFormat format);
52     int32_t SetOutputPath(const string &path);
53     int32_t SetOutputFile(int32_t fd);
54     int32_t SetNextOutputFile(int32_t fd);
55     int32_t SetMaxDuration(int64_t duration);
56     int32_t SetMaxFileSize(int64_t size);
57     int32_t SetOrientationHint(int degrees);
58     int32_t SetLocation(int latitude, int longitude);
59     int32_t SetRecorderCallback(const std::shared_ptr<RecorderCallback> &callback);
60     int32_t Prepare();
61     int32_t Start();
62     int32_t Stop(bool block);
63     int32_t Reset();
64     int32_t Release();
65     int32_t SetFileSplitDuration(ManualSplitType type, int64_t timestamp, uint32_t duration);
66     int32_t SetParameter(int32_t trackId, const Format &format);
67 
68     int32_t SendCallbackInfo(int32_t type, int32_t extra);
69     int32_t SendCallbackError(int32_t errorType, int32_t errorCode);
70     bool threadRunning = false;
71     queue<MessageData> messageQueue;
72     sem_t sem;
73     pthread_t threadId = 0;
74 
75 private:
76     int32_t CheckPrepared();
77     int32_t CheckStarted() const;
78     void CloseFd();
79     void *formatMuxerHandle_;
80     bool prepared_;
81     bool started_;
82     OutputFormat outputFormat_;
83     int32_t outputFd_;
84     int32_t outputNextFd_;
85     std::string path_;
86     int64_t maxFileSize_;
87     int64_t maxDuration_;
88     std::shared_ptr<RecorderCallback> recCallBack_;
89     std::shared_ptr<FormatCallback> sinkCallback_;
90 };
91 }  // namespace Media
92 }  // namespace OHOS
93 
94 #endif  // RECORDER_SINK_H
95