1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 OHOS_SHARING_BASE_CONSUMER_H
17 #define OHOS_SHARING_BASE_CONSUMER_H
18 
19 #include <functional>
20 #include <memory>
21 #include <cstdint>
22 #include <string>
23 #include "buffer_dispatcher.h"
24 #include "common/event_channel.h"
25 #include "common/identifier.h"
26 #include "common/object.h"
27 #include "event/handle_event_base.h"
28 #include "mediachannel/media_channel_def.h"
29 #include "utils/data_buffer.h"
30 
31 namespace OHOS {
32 namespace Sharing {
33 
34 class DataBuffer;
35 
36 class IConsumerListener {
37 public:
38     using Ptr = std::shared_ptr<IConsumerListener>;
39 
40     virtual ~IConsumerListener() = default;
41 
42     virtual void OnConsumerNotify(ProsumerStatusMsg::Ptr &statusMsg) = 0;
43     virtual BufferDispatcher::Ptr GetDispatcher() = 0;
44 };
45 
46 class BaseConsumer : public Object,
47                      public HandleEventBase,
48                      public IdentifierInfo {
49 public:
50     using Ptr = std::shared_ptr<BaseConsumer>;
51 
52     BaseConsumer();
53     virtual ~BaseConsumer();
54 
55     virtual void SetSinkAgentId(uint32_t agentId);
56     virtual void UpdateOperation(ProsumerStatusMsg::Ptr &statusMsg) = 0;
57     void SetConsumerListener(std::weak_ptr<IConsumerListener> listener);
58 
59     virtual bool IsPasued();
60     virtual bool IsRunning();
61     virtual bool IsCapture();
62 
63     virtual int32_t Release() = 0;
64     virtual uint32_t GetSinkAgentId();
65 
66     virtual const AudioTrack &GetAudioTrack() const;
67     virtual const VideoTrack &GetVideoTrack() const;
68 
69 protected:
70     void Notify(ProsumerStatusMsg::Ptr &statusMsg);
71     void NotifyPrivateEvent(EventMsg::Ptr eventMsg);
72 
73 protected:
74     bool isInit_ = false;
75     bool isPaused_ = false;
76     bool isRunning_ = false;
77     uint32_t sinkAgentId_ = INVALID_ID;
78     std::weak_ptr<IConsumerListener> listener_;
79 
80     AudioTrack audioTrack_;
81     VideoTrack videoTrack_;
82 };
83 
84 } // namespace Sharing
85 } // namespace OHOS
86 #endif