• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_VIDEO_PLAY_CONTROLLER_H
17 #define OHOS_SHARING_VIDEO_PLAY_CONTROLLER_H
18 
19 #include <thread>
20 #include "buffer_dispatcher.h"
21 #include "codec/include/video_sink_decoder.h"
22 #include "common/event_comm.h"
23 #include "common/sharing_log.h"
24 
25 namespace OHOS {
26 namespace Sharing {
27 
28 class MediaController;
29 
30 class VideoPlayController : public VideoSinkDecoderListener,
31                             public IBufferReceiverListener,
32                             public std::enable_shared_from_this<VideoPlayController> {
33 public:
34     explicit VideoPlayController(uint32_t mediaChannelId);
35     ~VideoPlayController();
36 
SetKeyMode(bool mode)37     void SetKeyMode(bool mode)
38     {
39         SHARING_LOGD("trace.");
40         isKeyMode_ = mode;
41         if (bufferReceiver_ && isVideoRunning_) {
42             bufferReceiver_->EnableKeyMode(mode);
43         }
44     }
45 
SetKeyRedirect(bool keyRedirect)46     void SetKeyRedirect(bool keyRedirect)
47     {
48         SHARING_LOGD("trace.");
49         if (bufferReceiver_) {
50             bufferReceiver_->EnableKeyRedirect(keyRedirect);
51         }
52     }
53 
SetMediaController(std::shared_ptr<MediaController> mediaController)54     void SetMediaController(std::shared_ptr<MediaController> mediaController)
55     {
56         SHARING_LOGD("trace.");
57         mediaController_ = mediaController;
58     }
59 
60 public:
61     void Release();
62     void Stop(BufferDispatcher::Ptr &dispatcher);
63 
64     bool Init(VideoTrack &videoTrack);
65     bool Start(BufferDispatcher::Ptr &dispatcher);
66     bool SetSurface(sptr<Surface> surface, bool keyFrame = false);
67 
68     // impl IBufferReceiverListener
69     void OnAccelerationDoneNotify() override;
70     void OnKeyModeNotify(bool enable) override;
71 
72 protected:
73     void OnError(int32_t errorCode) final;
74     void OnVideoDataDecoded(DataBuffer::Ptr decodedData) final;
75 
76 private:
77     void StopVideoThread();
78     void VideoPlayThread();
79     void StartVideoThread();
80     void ProcessVideoData(const char *data, int32_t size);
81     int32_t RenderInCopyMode(const DataBuffer::Ptr decodedData);
82 
83 private:
84     bool firstFrame_ = true;
85     bool enableSurface_ = false;
86     bool forceSWDecoder_ = false;
87     bool isSurfaceNoCopy_ = false;
88     uint32_t mediachannelId_ = 0;
89     sptr<Surface> surface_ = nullptr;
90 
91     std::atomic_bool isKeyMode_ = false;
92     std::atomic_bool isVideoRunning_ = false;
93     std::weak_ptr<MediaController> mediaController_;
94     std::shared_ptr<std::thread> videoPlayThread_ = nullptr;
95     std::shared_ptr<BufferReceiver> bufferReceiver_ = nullptr;
96     std::shared_ptr<VideoSinkDecoder> videoSinkDecoder_ = nullptr;
97 
98     VideoTrack videoTrack_;
99 };
100 
101 } // namespace Sharing
102 } // namespace OHOS
103 #endif