• 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 #ifndef GST_APPSRC_WRAP_H
17 #define GST_APPSRC_WRAP_H
18 
19 #include <gst/gst.h>
20 #include <queue>
21 #include "task_queue.h"
22 #include "media_data_source.h"
23 #include "gst/app/gstappsrc.h"
24 #include "nocopyable.h"
25 
26 namespace OHOS {
27 namespace Media {
28 struct AppsrcMemWrap {
29     std::shared_ptr<AVSharedMemory> mem;
30     // position of mem in file
31     uint64_t pos;
32     // size of mem
33     int32_t size;
34     // offset of mem
35     int32_t offset;
36 };
37 
38 struct AppsrcBufferWrap {
39     GstBuffer *buffer = nullptr;
40     int32_t offset = 0;
41     int32_t size = 0;
42 };
43 
44 class GstAppsrcWrap : public NoCopyable {
45 public:
46     using AppsrcErrorNotifier = std::function<void(int32_t)>;
47     static std::shared_ptr<GstAppsrcWrap> Create(const std::shared_ptr<IMediaDataSource> &dataSrc);
48     GstAppsrcWrap(const std::shared_ptr<IMediaDataSource> &dataSrc, const int64_t size);
49     ~GstAppsrcWrap();
50     int32_t SetAppsrc(GstElement *appSrc);
51     int32_t SetErrorCallback(AppsrcErrorNotifier notifier);
52     bool IsLiveMode() const;
53     int32_t Init();
54     int32_t Prepare();
55     void Stop();
56 
57 private:
58     void SetCallBackForAppSrc();
59     void ClearAppsrc();
60     static void NeedData(const GstElement *appSrc, uint32_t size, gpointer self);
61     static gboolean SeekData(const GstElement *appSrc, uint64_t seekPos, gpointer self);
62     void NeedDataInner(uint32_t size);
63     gboolean SeekDataInner(uint64_t pos);
64     void SeekAndFreeBuffers(uint64_t pos);
65     int32_t ReadAndGetMem();
66     void AnalyzeSize(int32_t size);
67     int32_t GetAndPushMem();
68     void OnError(int32_t errorCode);
69     void PushData(const GstBuffer *buffer) const;
70     void PushEos();
71     void FillTask();
72     void EmptyTask();
73     void EosAndCheckSize(int32_t size);
74     bool CopyToGstBuffer(const GstMapInfo &info);
75     std::shared_ptr<IMediaDataSource> dataSrc_ = nullptr;
76     const int64_t size_;
77     uint64_t curPos_ = 0;
78     std::mutex mutex_;
79     std::condition_variable emptyCond_;
80     std::condition_variable fillCond_;
81     GstElement *appSrc_ = nullptr;
82     TaskQueue fillTaskQue_;
83     TaskQueue emptyTaskQue_;
84     GstAppStreamType streamType_ = GST_APP_STREAM_TYPE_STREAM;
85     AppsrcErrorNotifier notifier_;
86     std::vector<gulong> callbackIds_;
87     std::queue<std::shared_ptr<AppsrcMemWrap>> emptyBuffers_;
88     std::queue<std::shared_ptr<AppsrcMemWrap>> filledBuffers_;
89     bool atEos_ = false;
90     bool needData_ = false;
91     int32_t needDataSize_ = 0;
92     bool isExit_ = true;
93     int32_t filledBufferSize_ = 0;
94     int32_t bufferSize_;
95     int32_t buffersNum_;
96     std::shared_ptr<AppsrcBufferWrap> bufferWrap_;
97 };
98 } // namespace Media
99 } // namespace OHOS
100 #endif /* GST_APPSRC_WRAP_H */
101