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