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