1 /* 2 * Copyright (C) 2021-2022 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 A2DP_CODEC_THREAD_H 17 #define A2DP_CODEC_THREAD_H 18 19 #include <cstddef> 20 #include <cstdint> 21 #include <memory> 22 #include <string> 23 24 #include "a2dp_codec/include/a2dp_codec_wrapper.h" 25 #include "a2dp_codec/include/a2dp_codec_config.h" 26 #include "a2dp_codec/include/a2dp_codec_constant.h" 27 #include "a2dp_profile_peer.h" 28 #include "base_def.h" 29 #include "dispatcher.h" 30 #include "message.h" 31 32 namespace OHOS { 33 namespace bluetooth { 34 using utility::Dispatcher; 35 using utility::Message; 36 37 constexpr int A2DP_AUDIO_RECONFIGURE = 1; 38 constexpr int A2DP_PCM_ENCODED = 2; 39 constexpr int A2DP_FRAME_DECODED = 3; 40 constexpr int A2DP_FRAME_READY = 4; 41 constexpr int A2DP_PCM_PUSH = 5; 42 43 class A2dpCodecThread { 44 public: 45 /** 46 * @brief A constructor used to create an <b>A2dpCodecThread</b> instance. 47 * 48 * @param[in] The name of the thread 49 * @since 6.0 50 */ 51 explicit A2dpCodecThread(const std::string &name); 52 53 /** 54 * @brief Get a A2dpCodecThread object. 55 * 56 * @param name A2dpCodecThread name. 57 * @since 6.0 58 */ 59 static A2dpCodecThread *GetInstance(); 60 61 /** 62 * @brief Start a A2dpCodecThread object. 63 * 64 * @since 6.0 65 */ 66 void StartA2dpCodecThread(); 67 68 /** 69 * @brief Stop a A2dpCodecThread object. 70 * 71 * @since 6.0 72 */ 73 void StopA2dpCodecThread(); 74 75 /** 76 * @brief A destructor used to create an <b>A2dpCodecThread</b> instance. 77 * 78 * @since 6.0 79 */ 80 ~A2dpCodecThread(); 81 82 /** 83 * @brief Get A2dpCodecThread's name. 84 * @return A2dpCodecThread's name. 85 * @since 6.0 86 */ Name()87 std::string &Name() 88 { 89 return name_; 90 }; 91 92 /** 93 * @brief Get the A2dpCodecThread's Dispatcher object 94 * @return Dispatcher pointer. 95 * @since 6.0 96 */ GetDispatcher()97 Dispatcher *GetDispatcher() 98 { 99 return dispatcher_.get(); 100 }; 101 102 /** 103 * @brief PostMessage to A2dpCodecThread's dispatcher. 104 * @return Success PostTask to dispatcher return true, else return false. 105 * @since 6.0 106 */ 107 bool PostMessage(const utility::Message msg, const A2dpEncoderInitPeerParams &peerParams, A2dpCodecConfig *config, 108 A2dpDecoderObserver *decObserver) const; 109 110 /** 111 * @brief process the message of codec thread. 112 * @return Success PostTask to dispatcher return true, else return false. 113 * @since 6.0 114 */ 115 void ProcessMessage(utility::Message msg, const A2dpEncoderInitPeerParams &peerParams, A2dpCodecConfig *config, 116 A2dpDecoderObserver *decObserver); 117 /** 118 * @brief Start the timer 119 * @since 6.0 120 */ 121 void StartTimer() const; 122 123 /** 124 * @brief Stop the timer 125 * @since 6.0 126 */ 127 void StopTimer() const; 128 129 /** 130 * @brief Get the init status 131 * @since 6.0 132 */ 133 bool GetInitStatus() const; 134 135 /** 136 * @brief Get the information of the current rendered position. 137 * @param[out] sendDataSize is the data size that has been sent 138 * @param[out] timeStamp is the current time stamp 139 * @since 6.0 140 */ 141 void GetRenderPosition(uint64_t &sendDataSize, uint32_t &timeStamp) const; 142 143 private: 144 /** 145 * @brief Source side encode 146 * 147 * @since 6.0 148 */ 149 void SourceEncode(const A2dpEncoderInitPeerParams &peerParams, const A2dpCodecConfig &config); 150 151 /** 152 * @brief Source side encode 153 * 154 * @since 6.0 155 */ 156 void SinkDecode(const A2dpCodecConfig &config, A2dpDecoderObserver &observer); 157 158 /** 159 * @brief Timer to push pcm data 160 * 161 * @since 6.0 162 */ 163 void SignalingTimeoutCallback() const; 164 165 std::string name_ {}; 166 std::unique_ptr<Dispatcher> dispatcher_ {}; 167 std::unique_ptr<A2dpEncoder> encoder_ = nullptr; 168 std::unique_ptr<A2dpDecoder> decoder_ = nullptr; 169 std::unique_ptr<utility::Timer> signalingTimer_ = nullptr; 170 static A2dpCodecThread *g_instance; 171 bool threadInit = false; 172 bool isSbc_ = false; 173 }; 174 } // namespace bluetooth 175 } // namespace OHOS 176 177 #endif // A2DP_CODEC_THREAD_H