Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 3 | * Copyright 2016 The Android Open Source Project |
| 4 | * Copyright 2009-2012 Broadcom Corporation |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | ******************************************************************************/ |
| 19 | |
| 20 | #define LOG_TAG "bt_btif_a2dp_source" |
Philip Cuadra | c02b5d4 | 2017-06-09 14:36:06 -0700 | [diff] [blame] | 21 | #define ATRACE_TAG ATRACE_TAG_AUDIO |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 22 | |
Jack He | f2af1c4 | 2016-12-13 01:59:12 -0800 | [diff] [blame] | 23 | #include <base/logging.h> |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 24 | #include <base/run_loop.h> |
Jakub Pawlowski | d3988fd | 2017-07-14 21:08:47 -0700 | [diff] [blame] | 25 | #ifndef OS_GENERIC |
Philip Cuadra | c02b5d4 | 2017-06-09 14:36:06 -0700 | [diff] [blame] | 26 | #include <cutils/trace.h> |
Jakub Pawlowski | d3988fd | 2017-07-14 21:08:47 -0700 | [diff] [blame] | 27 | #endif |
Jakub Pawlowski | 3fa7cce | 2016-10-13 16:49:07 -0700 | [diff] [blame] | 28 | #include <limits.h> |
Jakub Pawlowski | 5baa875 | 2016-10-18 18:45:21 -0700 | [diff] [blame] | 29 | #include <string.h> |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 30 | #include <algorithm> |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 31 | |
Pavlin Radoslavov | 304ceeb | 2017-04-05 16:18:26 -0700 | [diff] [blame] | 32 | #include "audio_a2dp_hw/include/audio_a2dp_hw.h" |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 33 | #include "bt_common.h" |
| 34 | #include "bta_av_ci.h" |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 35 | #include "btif_a2dp.h" |
| 36 | #include "btif_a2dp_control.h" |
| 37 | #include "btif_a2dp_source.h" |
| 38 | #include "btif_av.h" |
| 39 | #include "btif_av_co.h" |
| 40 | #include "btif_util.h" |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 41 | #include "osi/include/fixed_queue.h" |
| 42 | #include "osi/include/log.h" |
| 43 | #include "osi/include/metrics.h" |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 44 | #include "osi/include/osi.h" |
| 45 | #include "osi/include/thread.h" |
| 46 | #include "osi/include/time.h" |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 47 | #include "uipc.h" |
| 48 | |
Jack He | abc34b4 | 2018-02-07 17:33:16 -0800 | [diff] [blame] | 49 | #include <condition_variable> |
| 50 | #include <mutex> |
| 51 | |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 52 | using system_bt_osi::BluetoothMetricsLogger; |
| 53 | using system_bt_osi::A2dpSessionMetrics; |
| 54 | |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 55 | /** |
| 56 | * The typical runlevel of the tx queue size is ~1 buffer |
| 57 | * but due to link flow control or thread preemption in lower |
| 58 | * layers we might need to temporarily buffer up data. |
| 59 | */ |
| 60 | #define MAX_OUTPUT_A2DP_FRAME_QUEUE_SZ (MAX_PCM_FRAME_NUM_PER_TICK * 2) |
| 61 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 62 | class SchedulingStats { |
| 63 | public: |
| 64 | SchedulingStats() { Reset(); } |
| 65 | void Reset() { |
| 66 | total_updates = 0; |
| 67 | last_update_us = 0; |
| 68 | overdue_scheduling_count = 0; |
| 69 | total_overdue_scheduling_delta_us = 0; |
| 70 | max_overdue_scheduling_delta_us = 0; |
| 71 | premature_scheduling_count = 0; |
| 72 | total_premature_scheduling_delta_us = 0; |
| 73 | max_premature_scheduling_delta_us = 0; |
| 74 | exact_scheduling_count = 0; |
| 75 | total_scheduling_time_us = 0; |
| 76 | } |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 77 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 78 | // Counter for total updates |
| 79 | size_t total_updates; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 80 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 81 | // Last update timestamp (in us) |
| 82 | uint64_t last_update_us; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 83 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 84 | // Counter for overdue scheduling |
| 85 | size_t overdue_scheduling_count; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 86 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 87 | // Accumulated overdue scheduling deviations (in us) |
| 88 | uint64_t total_overdue_scheduling_delta_us; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 89 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 90 | // Max. overdue scheduling delta time (in us) |
| 91 | uint64_t max_overdue_scheduling_delta_us; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 92 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 93 | // Counter for premature scheduling |
| 94 | size_t premature_scheduling_count; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 95 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 96 | // Accumulated premature scheduling deviations (in us) |
| 97 | uint64_t total_premature_scheduling_delta_us; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 98 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 99 | // Max. premature scheduling delta time (in us) |
| 100 | uint64_t max_premature_scheduling_delta_us; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 101 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 102 | // Counter for exact scheduling |
| 103 | size_t exact_scheduling_count; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 104 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 105 | // Accumulated and counted scheduling time (in us) |
| 106 | uint64_t total_scheduling_time_us; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 107 | }; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 108 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 109 | class BtifMediaStats { |
| 110 | public: |
| 111 | BtifMediaStats() { Reset(); } |
| 112 | void Reset() { |
| 113 | session_start_us = 0; |
| 114 | session_end_us = 0; |
| 115 | tx_queue_enqueue_stats.Reset(); |
| 116 | tx_queue_dequeue_stats.Reset(); |
| 117 | tx_queue_total_frames = 0; |
| 118 | tx_queue_max_frames_per_packet = 0; |
| 119 | tx_queue_total_queueing_time_us = 0; |
| 120 | tx_queue_max_queueing_time_us = 0; |
| 121 | tx_queue_total_readbuf_calls = 0; |
| 122 | tx_queue_last_readbuf_us = 0; |
| 123 | tx_queue_total_flushed_messages = 0; |
| 124 | tx_queue_last_flushed_us = 0; |
| 125 | tx_queue_total_dropped_messages = 0; |
| 126 | tx_queue_max_dropped_messages = 0; |
| 127 | tx_queue_dropouts = 0; |
| 128 | tx_queue_last_dropouts_us = 0; |
| 129 | media_read_total_underflow_bytes = 0; |
| 130 | media_read_total_underflow_count = 0; |
| 131 | media_read_last_underflow_us = 0; |
| 132 | } |
| 133 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 134 | uint64_t session_start_us; |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 135 | uint64_t session_end_us; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 136 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 137 | SchedulingStats tx_queue_enqueue_stats; |
| 138 | SchedulingStats tx_queue_dequeue_stats; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 139 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 140 | size_t tx_queue_total_frames; |
| 141 | size_t tx_queue_max_frames_per_packet; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 142 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 143 | uint64_t tx_queue_total_queueing_time_us; |
| 144 | uint64_t tx_queue_max_queueing_time_us; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 145 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 146 | size_t tx_queue_total_readbuf_calls; |
| 147 | uint64_t tx_queue_last_readbuf_us; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 148 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 149 | size_t tx_queue_total_flushed_messages; |
| 150 | uint64_t tx_queue_last_flushed_us; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 151 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 152 | size_t tx_queue_total_dropped_messages; |
| 153 | size_t tx_queue_max_dropped_messages; |
| 154 | size_t tx_queue_dropouts; |
| 155 | uint64_t tx_queue_last_dropouts_us; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 156 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 157 | size_t media_read_total_underflow_bytes; |
| 158 | size_t media_read_total_underflow_count; |
| 159 | uint64_t media_read_last_underflow_us; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 160 | }; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 161 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 162 | class BtWorkerThread { |
| 163 | public: |
| 164 | BtWorkerThread(const std::string& thread_name) |
| 165 | : thread_name_(thread_name), |
| 166 | message_loop_(nullptr), |
| 167 | run_loop_(nullptr), |
Jack He | abc34b4 | 2018-02-07 17:33:16 -0800 | [diff] [blame] | 168 | message_loop_thread_(nullptr), |
| 169 | started_(false) {} |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 170 | |
| 171 | void StartUp() { |
| 172 | if (message_loop_thread_ != nullptr) { |
| 173 | return; // Already started up |
| 174 | } |
Jack He | abc34b4 | 2018-02-07 17:33:16 -0800 | [diff] [blame] | 175 | message_loop_thread_ = thread_new(thread_name_.c_str()); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 176 | CHECK(message_loop_thread_ != nullptr); |
Jack He | abc34b4 | 2018-02-07 17:33:16 -0800 | [diff] [blame] | 177 | started_ = false; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 178 | thread_post(message_loop_thread_, &BtWorkerThread::RunThread, this); |
Jack He | abc34b4 | 2018-02-07 17:33:16 -0800 | [diff] [blame] | 179 | { |
| 180 | // Block until run_loop_ is allocated and ready to run |
| 181 | std::unique_lock<std::mutex> start_lock(start_up_mutex_); |
| 182 | while (!started_) { |
| 183 | start_up_cv_.wait(start_lock); |
| 184 | } |
| 185 | } |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Jack He | abc34b4 | 2018-02-07 17:33:16 -0800 | [diff] [blame] | 188 | bool DoInThread(const tracked_objects::Location& from_here, |
| 189 | const base::Closure& task) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 190 | if ((message_loop_ == nullptr) || !message_loop_->task_runner().get()) { |
| 191 | LOG_ERROR( |
| 192 | LOG_TAG, |
| 193 | "%s: Dropping message for thread %s: message loop is not initialized", |
| 194 | __func__, thread_name_.c_str()); |
Jack He | abc34b4 | 2018-02-07 17:33:16 -0800 | [diff] [blame] | 195 | return false; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 196 | } |
| 197 | if (!message_loop_->task_runner()->PostTask(from_here, task)) { |
| 198 | LOG_ERROR(LOG_TAG, |
| 199 | "%s: Posting task to message loop for thread %s failed", |
| 200 | __func__, thread_name_.c_str()); |
Jack He | abc34b4 | 2018-02-07 17:33:16 -0800 | [diff] [blame] | 201 | return false; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 202 | } |
Jack He | abc34b4 | 2018-02-07 17:33:16 -0800 | [diff] [blame] | 203 | return true; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void ShutDown() { |
| 207 | if ((run_loop_ != nullptr) && (message_loop_ != nullptr)) { |
| 208 | message_loop_->task_runner()->PostTask(FROM_HERE, |
| 209 | run_loop_->QuitClosure()); |
| 210 | } |
| 211 | thread_free(message_loop_thread_); |
| 212 | message_loop_thread_ = nullptr; |
| 213 | } |
| 214 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 215 | private: |
| 216 | static void RunThread(void* context) { |
Jack He | abc34b4 | 2018-02-07 17:33:16 -0800 | [diff] [blame] | 217 | auto wt = static_cast<BtWorkerThread*>(context); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 218 | wt->Run(); |
| 219 | } |
| 220 | |
| 221 | void Run() { |
Jack He | abc34b4 | 2018-02-07 17:33:16 -0800 | [diff] [blame] | 222 | LOG_INFO(LOG_TAG, "%s: message loop for thread %s started", __func__, |
| 223 | thread_name_.c_str()); |
| 224 | message_loop_ = new base::MessageLoop(); |
| 225 | run_loop_ = new base::RunLoop(); |
| 226 | { |
| 227 | std::unique_lock<std::mutex> start_lock(start_up_mutex_); |
| 228 | started_ = true; |
| 229 | start_up_cv_.notify_all(); |
| 230 | } |
| 231 | // Blocking util ShutDown() is called |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 232 | run_loop_->Run(); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 233 | delete message_loop_; |
| 234 | message_loop_ = nullptr; |
| 235 | delete run_loop_; |
| 236 | run_loop_ = nullptr; |
Jack He | abc34b4 | 2018-02-07 17:33:16 -0800 | [diff] [blame] | 237 | LOG_INFO(LOG_TAG, "%s: message loop for thread %s finished", __func__, |
| 238 | thread_name_.c_str()); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | std::string thread_name_; |
| 242 | base::MessageLoop* message_loop_; |
| 243 | base::RunLoop* run_loop_; |
| 244 | thread_t* message_loop_thread_; |
Jack He | abc34b4 | 2018-02-07 17:33:16 -0800 | [diff] [blame] | 245 | // For start-up |
| 246 | bool started_; |
| 247 | std::mutex start_up_mutex_; |
| 248 | std::condition_variable start_up_cv_; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | class BtifA2dpSource { |
| 252 | public: |
| 253 | enum RunState { |
| 254 | kStateOff, |
| 255 | kStateStartingUp, |
| 256 | kStateRunning, |
| 257 | kStateShuttingDown |
| 258 | }; |
| 259 | |
| 260 | BtifA2dpSource() |
| 261 | : tx_audio_queue(nullptr), |
| 262 | tx_flush(false), |
| 263 | media_alarm(nullptr), |
| 264 | encoder_interface(nullptr), |
| 265 | encoder_interval_ms(0), |
| 266 | state_(kStateOff) {} |
| 267 | |
| 268 | void Reset() { |
| 269 | fixed_queue_free(tx_audio_queue, nullptr); |
| 270 | tx_audio_queue = nullptr; |
| 271 | tx_flush = false; |
| 272 | alarm_free(media_alarm); |
| 273 | media_alarm = nullptr; |
| 274 | encoder_interface = nullptr; |
| 275 | encoder_interval_ms = 0; |
| 276 | stats.Reset(); |
| 277 | accumulated_stats.Reset(); |
| 278 | state_ = kStateOff; |
| 279 | } |
| 280 | |
| 281 | BtifA2dpSource::RunState State() const { return state_; } |
| 282 | void SetState(BtifA2dpSource::RunState state) { state_ = state; } |
| 283 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 284 | fixed_queue_t* tx_audio_queue; |
| 285 | bool tx_flush; /* Discards any outgoing data when true */ |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 286 | alarm_t* media_alarm; |
| 287 | const tA2DP_ENCODER_INTERFACE* encoder_interface; |
| 288 | period_ms_t encoder_interval_ms; /* Local copy of the encoder interval */ |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 289 | BtifMediaStats stats; |
| 290 | BtifMediaStats accumulated_stats; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 291 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 292 | private: |
| 293 | BtifA2dpSource::RunState state_; |
| 294 | }; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 295 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 296 | static BtWorkerThread btif_a2dp_source_thread("btif_a2dp_source_thread"); |
| 297 | static BtifA2dpSource btif_a2dp_source_cb; |
| 298 | |
| 299 | static void btif_a2dp_source_startup_delayed(void); |
| 300 | static void btif_a2dp_source_shutdown_delayed(void); |
Pavlin Radoslavov | 1394c19 | 2016-10-02 18:34:46 -0700 | [diff] [blame] | 301 | static void btif_a2dp_source_audio_tx_start_event(void); |
| 302 | static void btif_a2dp_source_audio_tx_stop_event(void); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 303 | static void btif_a2dp_source_audio_tx_flush_event(void); |
| 304 | static void btif_a2dp_source_encoder_init_event( |
| 305 | const tA2DP_ENCODER_INIT_PEER_PARAMS& peer_params, |
| 306 | const RawAddress& peer_address); |
| 307 | static void btif_a2dp_source_encoder_user_config_update_event( |
| 308 | const RawAddress& peer_address, |
| 309 | const btav_a2dp_codec_config_t& codec_user_config); |
| 310 | static void btif_a2dp_source_audio_feeding_update_event( |
| 311 | const btav_a2dp_codec_config_t& codec_audio_config); |
Pavlin Radoslavov | 1394c19 | 2016-10-02 18:34:46 -0700 | [diff] [blame] | 312 | static bool btif_a2dp_source_audio_tx_flush_req(void); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 313 | static void btif_a2dp_source_alarm_cb(void* context); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 314 | static void btif_a2dp_source_audio_handle_timer(void); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 315 | static uint32_t btif_a2dp_source_read_callback(uint8_t* p_buf, uint32_t len); |
| 316 | static bool btif_a2dp_source_enqueue_callback(BT_HDR* p_buf, size_t frames_n); |
| 317 | static void log_tstamps_us(const char* comment, uint64_t timestamp_us); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 318 | static void update_scheduling_stats(SchedulingStats* stats, uint64_t now_us, |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 319 | uint64_t expected_delta); |
| 320 | static void btm_read_rssi_cb(void* data); |
Pavlin Radoslavov | 6ab749f | 2017-08-31 22:06:11 -0700 | [diff] [blame] | 321 | static void btm_read_failed_contact_counter_cb(void* data); |
Pavlin Radoslavov | b8568ae | 2017-09-01 16:09:27 -0700 | [diff] [blame] | 322 | static void btm_read_automatic_flush_timeout_cb(void* data); |
Pavlin Radoslavov | c7bf47c | 2017-09-01 16:40:59 -0700 | [diff] [blame] | 323 | static void btm_read_tx_power_cb(void* data); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 324 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 325 | void btif_a2dp_source_accumulate_scheduling_stats(SchedulingStats* src, |
| 326 | SchedulingStats* dst) { |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 327 | dst->total_updates += src->total_updates; |
| 328 | dst->last_update_us = src->last_update_us; |
| 329 | dst->overdue_scheduling_count += src->overdue_scheduling_count; |
| 330 | dst->total_overdue_scheduling_delta_us += |
| 331 | src->total_overdue_scheduling_delta_us; |
| 332 | dst->max_overdue_scheduling_delta_us = |
| 333 | std::max(dst->max_overdue_scheduling_delta_us, |
| 334 | src->max_overdue_scheduling_delta_us); |
| 335 | dst->premature_scheduling_count += src->premature_scheduling_count; |
| 336 | dst->total_premature_scheduling_delta_us += |
| 337 | src->total_premature_scheduling_delta_us; |
| 338 | dst->max_premature_scheduling_delta_us = |
| 339 | std::max(dst->max_premature_scheduling_delta_us, |
| 340 | src->max_premature_scheduling_delta_us); |
| 341 | dst->exact_scheduling_count += src->exact_scheduling_count; |
| 342 | dst->total_scheduling_time_us += src->total_scheduling_time_us; |
| 343 | } |
| 344 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 345 | void btif_a2dp_source_accumulate_stats(BtifMediaStats* src, |
| 346 | BtifMediaStats* dst) { |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 347 | dst->tx_queue_total_frames += src->tx_queue_total_frames; |
| 348 | dst->tx_queue_max_frames_per_packet = std::max( |
| 349 | dst->tx_queue_max_frames_per_packet, src->tx_queue_max_frames_per_packet); |
| 350 | dst->tx_queue_total_queueing_time_us += src->tx_queue_total_queueing_time_us; |
| 351 | dst->tx_queue_max_queueing_time_us = std::max( |
| 352 | dst->tx_queue_max_queueing_time_us, src->tx_queue_max_queueing_time_us); |
| 353 | dst->tx_queue_total_readbuf_calls += src->tx_queue_total_readbuf_calls; |
| 354 | dst->tx_queue_last_readbuf_us = src->tx_queue_last_readbuf_us; |
| 355 | dst->tx_queue_total_flushed_messages += src->tx_queue_total_flushed_messages; |
| 356 | dst->tx_queue_last_flushed_us = src->tx_queue_last_flushed_us; |
| 357 | dst->tx_queue_total_dropped_messages += src->tx_queue_total_dropped_messages; |
| 358 | dst->tx_queue_max_dropped_messages = std::max( |
| 359 | dst->tx_queue_max_dropped_messages, src->tx_queue_max_dropped_messages); |
| 360 | dst->tx_queue_dropouts += src->tx_queue_dropouts; |
| 361 | dst->tx_queue_last_dropouts_us = src->tx_queue_last_dropouts_us; |
| 362 | dst->media_read_total_underflow_bytes += |
| 363 | src->media_read_total_underflow_bytes; |
| 364 | dst->media_read_total_underflow_count += |
| 365 | src->media_read_total_underflow_count; |
| 366 | dst->media_read_last_underflow_us = src->media_read_last_underflow_us; |
| 367 | btif_a2dp_source_accumulate_scheduling_stats(&src->tx_queue_enqueue_stats, |
| 368 | &dst->tx_queue_enqueue_stats); |
| 369 | btif_a2dp_source_accumulate_scheduling_stats(&src->tx_queue_dequeue_stats, |
| 370 | &dst->tx_queue_dequeue_stats); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 371 | src->Reset(); |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 374 | bool btif_a2dp_source_startup(void) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 375 | if (btif_a2dp_source_cb.State() != BtifA2dpSource::kStateOff) { |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 376 | APPL_TRACE_ERROR("%s: A2DP Source media task already running", __func__); |
| 377 | return false; |
| 378 | } |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 379 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 380 | btif_a2dp_source_cb.Reset(); |
| 381 | btif_a2dp_source_cb.SetState(BtifA2dpSource::kStateStartingUp); |
| 382 | btif_a2dp_source_cb.tx_audio_queue = fixed_queue_new(SIZE_MAX); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 383 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 384 | APPL_TRACE_EVENT("## A2DP SOURCE START MEDIA THREAD ##"); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 385 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 386 | /* Start A2DP Source media task */ |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 387 | btif_a2dp_source_thread.StartUp(); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 388 | |
| 389 | APPL_TRACE_EVENT("## A2DP SOURCE MEDIA THREAD STARTED ##"); |
| 390 | |
| 391 | /* Schedule the rest of the startup operations */ |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 392 | btif_a2dp_source_thread.DoInThread( |
| 393 | FROM_HERE, base::Bind(&btif_a2dp_source_startup_delayed)); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 394 | |
| 395 | return true; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 398 | static void btif_a2dp_source_startup_delayed(void) { |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 399 | raise_priority_a2dp(TASK_HIGH_MEDIA); |
| 400 | btif_a2dp_control_init(); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 401 | btif_a2dp_source_cb.SetState(BtifA2dpSource::kStateRunning); |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 402 | BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart( |
| 403 | system_bt_osi::CONNECTION_TECHNOLOGY_TYPE_BREDR, 0); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 404 | } |
| 405 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 406 | void btif_a2dp_source_shutdown(void) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 407 | if ((btif_a2dp_source_cb.State() == BtifA2dpSource::kStateOff) || |
| 408 | (btif_a2dp_source_cb.State() == BtifA2dpSource::kStateShuttingDown)) { |
Sanket Agarwal | f436d2f | 2016-11-02 11:56:44 -0700 | [diff] [blame] | 409 | return; |
| 410 | } |
| 411 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 412 | /* Make sure no channels are restarted while shutting down */ |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 413 | btif_a2dp_source_cb.SetState(BtifA2dpSource::kStateShuttingDown); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 414 | |
| 415 | APPL_TRACE_EVENT("## A2DP SOURCE STOP MEDIA THREAD ##"); |
| 416 | |
| 417 | // Stop the timer |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 418 | alarm_free(btif_a2dp_source_cb.media_alarm); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 419 | btif_a2dp_source_cb.media_alarm = nullptr; |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 420 | |
| 421 | // Exit the thread |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 422 | btif_a2dp_source_thread.DoInThread( |
| 423 | FROM_HERE, base::Bind(&btif_a2dp_source_shutdown_delayed)); |
| 424 | btif_a2dp_source_thread.ShutDown(); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 427 | static void btif_a2dp_source_shutdown_delayed(void) { |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 428 | btif_a2dp_control_cleanup(); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 429 | fixed_queue_free(btif_a2dp_source_cb.tx_audio_queue, nullptr); |
| 430 | btif_a2dp_source_cb.tx_audio_queue = nullptr; |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 431 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 432 | btif_a2dp_source_cb.SetState(BtifA2dpSource::kStateOff); |
Jack He | a3f831c | 2017-01-17 15:41:30 -0800 | [diff] [blame] | 433 | BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd( |
| 434 | system_bt_osi::DISCONNECT_REASON_UNKNOWN, 0); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 437 | bool btif_a2dp_source_media_task_is_running(void) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 438 | return (btif_a2dp_source_cb.State() == BtifA2dpSource::kStateRunning); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 439 | } |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 440 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 441 | bool btif_a2dp_source_media_task_is_shutting_down(void) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 442 | return (btif_a2dp_source_cb.State() == BtifA2dpSource::kStateShuttingDown); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 443 | } |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 444 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 445 | bool btif_a2dp_source_is_streaming(void) { |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 446 | return alarm_is_scheduled(btif_a2dp_source_cb.media_alarm); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 449 | void btif_a2dp_source_setup_codec(const RawAddress& peer_address) { |
| 450 | APPL_TRACE_EVENT("## A2DP SOURCE SETUP CODEC (peer %s) ##", |
| 451 | peer_address.ToString().c_str()); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 452 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 453 | // Check to make sure the platform has 8 bits/byte since |
| 454 | // we're using that in frame size calculations now. |
| 455 | CHECK(CHAR_BIT == 8); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 456 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 457 | tA2DP_ENCODER_INIT_PEER_PARAMS peer_params; |
| 458 | bta_av_co_get_peer_params(peer_address, &peer_params); |
| 459 | btif_a2dp_source_thread.DoInThread( |
| 460 | FROM_HERE, base::Bind(&btif_a2dp_source_encoder_init_event, peer_params, |
| 461 | peer_address)); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 464 | void btif_a2dp_source_start_audio_req(void) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 465 | btif_a2dp_source_thread.DoInThread( |
| 466 | FROM_HERE, base::Bind(&btif_a2dp_source_audio_tx_start_event)); |
| 467 | btif_a2dp_source_cb.stats.Reset(); |
Jack He | ee5c257 | 2017-01-20 11:17:44 -0800 | [diff] [blame] | 468 | // Assign session_start_us to 1 when time_get_os_boottime_us() is 0 to |
| 469 | // indicate btif_a2dp_source_start_audio_req() has been called |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 470 | btif_a2dp_source_cb.stats.session_start_us = time_get_os_boottime_us(); |
Jack He | ee5c257 | 2017-01-20 11:17:44 -0800 | [diff] [blame] | 471 | if (btif_a2dp_source_cb.stats.session_start_us == 0) { |
| 472 | btif_a2dp_source_cb.stats.session_start_us = 1; |
| 473 | } |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 474 | btif_a2dp_source_cb.stats.session_end_us = 0; |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | void btif_a2dp_source_stop_audio_req(void) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 478 | btif_a2dp_source_thread.DoInThread( |
| 479 | FROM_HERE, base::Bind(&btif_a2dp_source_audio_tx_stop_event)); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 480 | |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 481 | btif_a2dp_source_cb.stats.session_end_us = time_get_os_boottime_us(); |
| 482 | btif_a2dp_source_update_metrics(); |
| 483 | btif_a2dp_source_accumulate_stats(&btif_a2dp_source_cb.stats, |
| 484 | &btif_a2dp_source_cb.accumulated_stats); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 487 | static void btif_a2dp_source_encoder_init_event( |
| 488 | const tA2DP_ENCODER_INIT_PEER_PARAMS& peer_params, |
| 489 | const RawAddress& peer_address) { |
| 490 | APPL_TRACE_DEBUG("%s: peer_address=%s", __func__, |
| 491 | peer_address.ToString().c_str()); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 492 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 493 | if (!bta_av_co_set_active_peer(peer_address)) { |
| 494 | APPL_TRACE_ERROR("%s: Cannot stream audio: cannot set active peer to %s", |
| 495 | __func__, peer_address.ToString().c_str()); |
| 496 | return; |
| 497 | } |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 498 | btif_a2dp_source_cb.encoder_interface = bta_av_co_get_encoder_interface(); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 499 | if (btif_a2dp_source_cb.encoder_interface == nullptr) { |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 500 | APPL_TRACE_ERROR("%s: Cannot stream audio: no source encoder interface", |
| 501 | __func__); |
| 502 | return; |
| 503 | } |
| 504 | |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 505 | A2dpCodecConfig* a2dp_codec_config = bta_av_get_a2dp_current_codec(); |
| 506 | if (a2dp_codec_config == nullptr) { |
| 507 | APPL_TRACE_ERROR("%s: Cannot stream audio: current codec is not set", |
| 508 | __func__); |
| 509 | return; |
| 510 | } |
| 511 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 512 | btif_a2dp_source_cb.encoder_interface->encoder_init( |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 513 | &peer_params, a2dp_codec_config, btif_a2dp_source_read_callback, |
| 514 | btif_a2dp_source_enqueue_callback); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 515 | |
| 516 | // Save a local copy of the encoder_interval_ms |
| 517 | btif_a2dp_source_cb.encoder_interval_ms = |
| 518 | btif_a2dp_source_cb.encoder_interface->get_encoder_interval_ms(); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 521 | void btif_a2dp_source_encoder_user_config_update_req( |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 522 | const RawAddress& peer_address, |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 523 | const btav_a2dp_codec_config_t& codec_user_config) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 524 | btif_a2dp_source_thread.DoInThread( |
| 525 | FROM_HERE, base::Bind(&btif_a2dp_source_encoder_user_config_update_event, |
| 526 | peer_address, codec_user_config)); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 529 | static void btif_a2dp_source_encoder_user_config_update_event( |
| 530 | const RawAddress& peer_address, |
| 531 | const btav_a2dp_codec_config_t& codec_user_config) { |
| 532 | APPL_TRACE_DEBUG("%s: peer_address=%s", __func__, |
| 533 | peer_address.ToString().c_str()); |
| 534 | if (!bta_av_co_set_codec_user_config(peer_address, codec_user_config)) { |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 535 | APPL_TRACE_ERROR("%s: cannot update codec user configuration", __func__); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | void btif_a2dp_source_feeding_update_req( |
| 540 | const btav_a2dp_codec_config_t& codec_audio_config) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 541 | btif_a2dp_source_thread.DoInThread( |
| 542 | FROM_HERE, base::Bind(&btif_a2dp_source_audio_feeding_update_event, |
| 543 | codec_audio_config)); |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 544 | } |
| 545 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 546 | static void btif_a2dp_source_audio_feeding_update_event( |
| 547 | const btav_a2dp_codec_config_t& codec_audio_config) { |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 548 | APPL_TRACE_DEBUG("%s", __func__); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 549 | if (!bta_av_co_set_codec_audio_config(codec_audio_config)) { |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 550 | APPL_TRACE_ERROR("%s: cannot update codec audio feeding parameters", |
| 551 | __func__); |
| 552 | } |
| 553 | } |
| 554 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 555 | void btif_a2dp_source_on_idle(void) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 556 | if (btif_a2dp_source_cb.State() == BtifA2dpSource::kStateOff) return; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 557 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 558 | /* Make sure media task is stopped */ |
| 559 | btif_a2dp_source_stop_audio_req(); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 562 | void btif_a2dp_source_on_stopped(tBTA_AV_SUSPEND* p_av_suspend) { |
| 563 | APPL_TRACE_EVENT("## ON A2DP SOURCE STOPPED ##"); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 564 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 565 | if (btif_a2dp_source_cb.State() == BtifA2dpSource::kStateOff) return; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 566 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 567 | /* allow using this api for other than suspend */ |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 568 | if (p_av_suspend != nullptr) { |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 569 | if (p_av_suspend->status != BTA_AV_SUCCESS) { |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 570 | APPL_TRACE_EVENT("AV STOP FAILED (%d)", p_av_suspend->status); |
| 571 | if (p_av_suspend->initiator) { |
| 572 | APPL_TRACE_WARNING("%s: A2DP stop request failed: status = %d", |
| 573 | __func__, p_av_suspend->status); |
| 574 | btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE); |
| 575 | } |
| 576 | return; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 577 | } |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 578 | } |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 579 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 580 | /* ensure tx frames are immediately suspended */ |
| 581 | btif_a2dp_source_cb.tx_flush = true; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 582 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 583 | /* request to stop media task */ |
| 584 | btif_a2dp_source_audio_tx_flush_req(); |
| 585 | btif_a2dp_source_stop_audio_req(); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 586 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 587 | /* once stream is fully stopped we will ack back */ |
| 588 | } |
| 589 | |
| 590 | void btif_a2dp_source_on_suspended(tBTA_AV_SUSPEND* p_av_suspend) { |
| 591 | APPL_TRACE_EVENT("## ON A2DP SOURCE SUSPENDED ##"); |
| 592 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 593 | if (btif_a2dp_source_cb.State() == BtifA2dpSource::kStateOff) return; |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 594 | |
| 595 | /* check for status failures */ |
| 596 | if (p_av_suspend->status != BTA_AV_SUCCESS) { |
| 597 | if (p_av_suspend->initiator) { |
| 598 | APPL_TRACE_WARNING("%s: A2DP suspend request failed: status = %d", |
| 599 | __func__, p_av_suspend->status); |
| 600 | btif_a2dp_command_ack(A2DP_CTRL_ACK_FAILURE); |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | /* once stream is fully stopped we will ack back */ |
| 605 | |
| 606 | /* ensure tx frames are immediately flushed */ |
| 607 | btif_a2dp_source_cb.tx_flush = true; |
| 608 | |
| 609 | /* stop timer tick */ |
| 610 | btif_a2dp_source_stop_audio_req(); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /* when true media task discards any tx frames */ |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 614 | void btif_a2dp_source_set_tx_flush(bool enable) { |
| 615 | APPL_TRACE_EVENT("## DROP TX %d ##", enable); |
| 616 | btif_a2dp_source_cb.tx_flush = enable; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 619 | static void btif_a2dp_source_audio_tx_start_event(void) { |
| 620 | APPL_TRACE_DEBUG( |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 621 | "%s media_alarm is %srunning, streaming %s", __func__, |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 622 | alarm_is_scheduled(btif_a2dp_source_cb.media_alarm) ? "" : "not ", |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 623 | btif_a2dp_source_is_streaming() ? "true" : "false"); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 624 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 625 | /* Reset the media feeding state */ |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 626 | CHECK(btif_a2dp_source_cb.encoder_interface != nullptr); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 627 | btif_a2dp_source_cb.encoder_interface->feeding_reset(); |
| 628 | |
| 629 | APPL_TRACE_EVENT( |
| 630 | "starting timer %dms", |
| 631 | btif_a2dp_source_cb.encoder_interface->get_encoder_interval_ms()); |
| 632 | |
| 633 | alarm_free(btif_a2dp_source_cb.media_alarm); |
| 634 | btif_a2dp_source_cb.media_alarm = |
| 635 | alarm_new_periodic("btif.a2dp_source_media_alarm"); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 636 | if (btif_a2dp_source_cb.media_alarm == nullptr) { |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 637 | LOG_ERROR(LOG_TAG, "%s unable to allocate media alarm", __func__); |
| 638 | return; |
| 639 | } |
| 640 | |
| 641 | alarm_set(btif_a2dp_source_cb.media_alarm, |
| 642 | btif_a2dp_source_cb.encoder_interface->get_encoder_interval_ms(), |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 643 | btif_a2dp_source_alarm_cb, nullptr); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | static void btif_a2dp_source_audio_tx_stop_event(void) { |
| 647 | APPL_TRACE_DEBUG( |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 648 | "%s media_alarm is %srunning, streaming %s", __func__, |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 649 | alarm_is_scheduled(btif_a2dp_source_cb.media_alarm) ? "" : "not ", |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 650 | btif_a2dp_source_is_streaming() ? "true" : "false"); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 651 | |
Pavlin Radoslavov | 5ce0116 | 2016-12-05 13:02:26 -0800 | [diff] [blame] | 652 | const bool send_ack = btif_a2dp_source_is_streaming(); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 653 | |
| 654 | /* Stop the timer first */ |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 655 | alarm_free(btif_a2dp_source_cb.media_alarm); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 656 | btif_a2dp_source_cb.media_alarm = nullptr; |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 657 | |
Hansong Zhang | f88552c | 2018-02-01 18:02:53 -0800 | [diff] [blame^] | 658 | UIPC_Close(UIPC_CH_ID_AV_AUDIO, UIPC_USER_A2DP); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 659 | |
| 660 | /* |
| 661 | * Try to send acknowldegment once the media stream is |
| 662 | * stopped. This will make sure that the A2DP HAL layer is |
| 663 | * un-blocked on wait for acknowledgment for the sent command. |
| 664 | * This resolves a corner cases AVDTP SUSPEND collision |
| 665 | * when the DUT and the remote device issue SUSPEND simultaneously |
| 666 | * and due to the processing of the SUSPEND request from the remote, |
| 667 | * the media path is torn down. If the A2DP HAL happens to wait |
| 668 | * for ACK for the initiated SUSPEND, it would never receive it casuing |
| 669 | * a block/wait. Due to this acknowledgement, the A2DP HAL is guranteed |
| 670 | * to get the ACK for any pending command in such cases. |
| 671 | */ |
| 672 | |
| 673 | if (send_ack) btif_a2dp_command_ack(A2DP_CTRL_ACK_SUCCESS); |
| 674 | |
| 675 | /* audio engine stopped, reset tx suspended flag */ |
| 676 | btif_a2dp_source_cb.tx_flush = false; |
| 677 | |
| 678 | /* Reset the media feeding state */ |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 679 | if (btif_a2dp_source_cb.encoder_interface != nullptr) |
Pavlin Radoslavov | 1394c19 | 2016-10-02 18:34:46 -0700 | [diff] [blame] | 680 | btif_a2dp_source_cb.encoder_interface->feeding_reset(); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 683 | static void btif_a2dp_source_alarm_cb(UNUSED_ATTR void* context) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 684 | btif_a2dp_source_thread.DoInThread( |
| 685 | FROM_HERE, base::Bind(&btif_a2dp_source_audio_handle_timer)); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 688 | static void btif_a2dp_source_audio_handle_timer(void) { |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 689 | uint64_t timestamp_us = time_get_os_boottime_us(); |
| 690 | log_tstamps_us("A2DP Source tx timer", timestamp_us); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 691 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 692 | if (alarm_is_scheduled(btif_a2dp_source_cb.media_alarm)) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 693 | CHECK(btif_a2dp_source_cb.encoder_interface != nullptr); |
Philip Cuadra | c02b5d4 | 2017-06-09 14:36:06 -0700 | [diff] [blame] | 694 | size_t transmit_queue_length = |
| 695 | fixed_queue_length(btif_a2dp_source_cb.tx_audio_queue); |
Jakub Pawlowski | d3988fd | 2017-07-14 21:08:47 -0700 | [diff] [blame] | 696 | #ifndef OS_GENERIC |
Philip Cuadra | c02b5d4 | 2017-06-09 14:36:06 -0700 | [diff] [blame] | 697 | ATRACE_INT("btif TX queue", transmit_queue_length); |
Jakub Pawlowski | d3988fd | 2017-07-14 21:08:47 -0700 | [diff] [blame] | 698 | #endif |
fen wang | dfc02a9 | 2017-03-15 14:52:52 +0900 | [diff] [blame] | 699 | if (btif_a2dp_source_cb.encoder_interface->set_transmit_queue_length != |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 700 | nullptr) { |
fen wang | dfc02a9 | 2017-03-15 14:52:52 +0900 | [diff] [blame] | 701 | btif_a2dp_source_cb.encoder_interface->set_transmit_queue_length( |
| 702 | transmit_queue_length); |
| 703 | } |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 704 | btif_a2dp_source_cb.encoder_interface->send_frames(timestamp_us); |
| 705 | bta_av_ci_src_data_ready(BTA_AV_CHNL_AUDIO); |
Jack He | ee5c257 | 2017-01-20 11:17:44 -0800 | [diff] [blame] | 706 | update_scheduling_stats(&btif_a2dp_source_cb.stats.tx_queue_enqueue_stats, |
| 707 | timestamp_us, |
| 708 | btif_a2dp_source_cb.encoder_interval_ms * 1000); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 709 | } else { |
| 710 | APPL_TRACE_ERROR("ERROR Media task Scheduled after Suspend"); |
| 711 | } |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 714 | static uint32_t btif_a2dp_source_read_callback(uint8_t* p_buf, uint32_t len) { |
| 715 | uint16_t event; |
| 716 | uint32_t bytes_read = UIPC_Read(UIPC_CH_ID_AV_AUDIO, &event, p_buf, len); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 717 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 718 | if (bytes_read < len) { |
| 719 | LOG_WARN(LOG_TAG, "%s: UNDERFLOW: ONLY READ %d BYTES OUT OF %d", __func__, |
| 720 | bytes_read, len); |
| 721 | btif_a2dp_source_cb.stats.media_read_total_underflow_bytes += |
| 722 | (len - bytes_read); |
| 723 | btif_a2dp_source_cb.stats.media_read_total_underflow_count++; |
| 724 | btif_a2dp_source_cb.stats.media_read_last_underflow_us = |
| 725 | time_get_os_boottime_us(); |
| 726 | } |
| 727 | |
| 728 | return bytes_read; |
| 729 | } |
| 730 | |
| 731 | static bool btif_a2dp_source_enqueue_callback(BT_HDR* p_buf, size_t frames_n) { |
| 732 | uint64_t now_us = time_get_os_boottime_us(); |
| 733 | |
| 734 | /* Check if timer was stopped (media task stopped) */ |
| 735 | if (!alarm_is_scheduled(btif_a2dp_source_cb.media_alarm)) { |
| 736 | osi_free(p_buf); |
| 737 | return false; |
| 738 | } |
| 739 | |
| 740 | /* Check if the transmission queue has been flushed */ |
| 741 | if (btif_a2dp_source_cb.tx_flush) { |
| 742 | LOG_VERBOSE(LOG_TAG, "%s: tx suspended, discarded frame", __func__); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 743 | |
| 744 | btif_a2dp_source_cb.stats.tx_queue_total_flushed_messages += |
Pavlin Radoslavov | 1394c19 | 2016-10-02 18:34:46 -0700 | [diff] [blame] | 745 | fixed_queue_length(btif_a2dp_source_cb.tx_audio_queue); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 746 | btif_a2dp_source_cb.stats.tx_queue_last_flushed_us = now_us; |
Pavlin Radoslavov | 1394c19 | 2016-10-02 18:34:46 -0700 | [diff] [blame] | 747 | fixed_queue_flush(btif_a2dp_source_cb.tx_audio_queue, osi_free); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 748 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 749 | osi_free(p_buf); |
| 750 | return false; |
| 751 | } |
| 752 | |
| 753 | // Check for TX queue overflow |
| 754 | // TODO: Using frames_n here is probably wrong: should be "+ 1" instead. |
| 755 | if (fixed_queue_length(btif_a2dp_source_cb.tx_audio_queue) + frames_n > |
| 756 | MAX_OUTPUT_A2DP_FRAME_QUEUE_SZ) { |
| 757 | LOG_WARN(LOG_TAG, "%s: TX queue buffer size now=%u adding=%u max=%d", |
| 758 | __func__, |
| 759 | (uint32_t)fixed_queue_length(btif_a2dp_source_cb.tx_audio_queue), |
| 760 | (uint32_t)frames_n, MAX_OUTPUT_A2DP_FRAME_QUEUE_SZ); |
| 761 | // Keep track of drop-outs |
| 762 | btif_a2dp_source_cb.stats.tx_queue_dropouts++; |
| 763 | btif_a2dp_source_cb.stats.tx_queue_last_dropouts_us = now_us; |
| 764 | |
| 765 | // Flush all queued buffers |
| 766 | size_t drop_n = fixed_queue_length(btif_a2dp_source_cb.tx_audio_queue); |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 767 | btif_a2dp_source_cb.stats.tx_queue_max_dropped_messages = std::max( |
| 768 | drop_n, btif_a2dp_source_cb.stats.tx_queue_max_dropped_messages); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 769 | while (fixed_queue_length(btif_a2dp_source_cb.tx_audio_queue)) { |
| 770 | btif_a2dp_source_cb.stats.tx_queue_total_dropped_messages++; |
| 771 | osi_free(fixed_queue_try_dequeue(btif_a2dp_source_cb.tx_audio_queue)); |
| 772 | } |
| 773 | |
Pavlin Radoslavov | b8568ae | 2017-09-01 16:09:27 -0700 | [diff] [blame] | 774 | // Request additional debug info if we had to flush buffers |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 775 | RawAddress peer_bda = btif_av_source_active_peer(); |
Pavlin Radoslavov | 6ab749f | 2017-08-31 22:06:11 -0700 | [diff] [blame] | 776 | tBTM_STATUS status = BTM_ReadRSSI(peer_bda, btm_read_rssi_cb); |
| 777 | if (status != BTM_CMD_STARTED) { |
| 778 | LOG_WARN(LOG_TAG, "%s: Cannot read RSSI: status %d", __func__, status); |
| 779 | } |
| 780 | status = BTM_ReadFailedContactCounter(peer_bda, |
| 781 | btm_read_failed_contact_counter_cb); |
| 782 | if (status != BTM_CMD_STARTED) { |
| 783 | LOG_WARN(LOG_TAG, "%s: Cannot read Failed Contact Counter: status %d", |
| 784 | __func__, status); |
| 785 | } |
Pavlin Radoslavov | b8568ae | 2017-09-01 16:09:27 -0700 | [diff] [blame] | 786 | status = BTM_ReadAutomaticFlushTimeout(peer_bda, |
| 787 | btm_read_automatic_flush_timeout_cb); |
| 788 | if (status != BTM_CMD_STARTED) { |
| 789 | LOG_WARN(LOG_TAG, "%s: Cannot read Automatic Flush Timeout: status %d", |
| 790 | __func__, status); |
| 791 | } |
Pavlin Radoslavov | c7bf47c | 2017-09-01 16:40:59 -0700 | [diff] [blame] | 792 | status = |
| 793 | BTM_ReadTxPower(peer_bda, BT_TRANSPORT_BR_EDR, btm_read_tx_power_cb); |
| 794 | if (status != BTM_CMD_STARTED) { |
| 795 | LOG_WARN(LOG_TAG, "%s: Cannot read Tx Power: status %d", __func__, |
| 796 | status); |
| 797 | } |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | /* Update the statistics */ |
| 801 | btif_a2dp_source_cb.stats.tx_queue_total_frames += frames_n; |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 802 | btif_a2dp_source_cb.stats.tx_queue_max_frames_per_packet = std::max( |
| 803 | frames_n, btif_a2dp_source_cb.stats.tx_queue_max_frames_per_packet); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 804 | CHECK(btif_a2dp_source_cb.encoder_interface != nullptr); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 805 | |
| 806 | fixed_queue_enqueue(btif_a2dp_source_cb.tx_audio_queue, p_buf); |
| 807 | |
| 808 | return true; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 809 | } |
| 810 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 811 | static void btif_a2dp_source_audio_tx_flush_event(void) { |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 812 | /* Flush all enqueued audio buffers (encoded) */ |
| 813 | APPL_TRACE_DEBUG("%s", __func__); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 814 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 815 | if (btif_a2dp_source_cb.encoder_interface != nullptr) |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 816 | btif_a2dp_source_cb.encoder_interface->feeding_flush(); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 817 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 818 | btif_a2dp_source_cb.stats.tx_queue_total_flushed_messages += |
| 819 | fixed_queue_length(btif_a2dp_source_cb.tx_audio_queue); |
| 820 | btif_a2dp_source_cb.stats.tx_queue_last_flushed_us = |
| 821 | time_get_os_boottime_us(); |
| 822 | fixed_queue_flush(btif_a2dp_source_cb.tx_audio_queue, osi_free); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 823 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 824 | UIPC_Ioctl(UIPC_CH_ID_AV_AUDIO, UIPC_REQ_RX_FLUSH, nullptr); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 827 | static bool btif_a2dp_source_audio_tx_flush_req(void) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 828 | btif_a2dp_source_thread.DoInThread( |
| 829 | FROM_HERE, base::Bind(&btif_a2dp_source_audio_tx_flush_event)); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 830 | return true; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 833 | BT_HDR* btif_a2dp_source_audio_readbuf(void) { |
| 834 | uint64_t now_us = time_get_os_boottime_us(); |
| 835 | BT_HDR* p_buf = |
| 836 | (BT_HDR*)fixed_queue_try_dequeue(btif_a2dp_source_cb.tx_audio_queue); |
| 837 | |
| 838 | btif_a2dp_source_cb.stats.tx_queue_total_readbuf_calls++; |
| 839 | btif_a2dp_source_cb.stats.tx_queue_last_readbuf_us = now_us; |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 840 | if (p_buf != nullptr) { |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 841 | // Update the statistics |
| 842 | update_scheduling_stats(&btif_a2dp_source_cb.stats.tx_queue_dequeue_stats, |
| 843 | now_us, |
| 844 | btif_a2dp_source_cb.encoder_interval_ms * 1000); |
| 845 | } |
| 846 | |
| 847 | return p_buf; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 848 | } |
| 849 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 850 | static void log_tstamps_us(const char* comment, uint64_t timestamp_us) { |
| 851 | static uint64_t prev_us = 0; |
| 852 | APPL_TRACE_DEBUG("[%s] ts %08llu, diff : %08llu, queue sz %d", comment, |
| 853 | timestamp_us, timestamp_us - prev_us, |
| 854 | fixed_queue_length(btif_a2dp_source_cb.tx_audio_queue)); |
| 855 | prev_us = timestamp_us; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 856 | } |
| 857 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 858 | static void update_scheduling_stats(SchedulingStats* stats, uint64_t now_us, |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 859 | uint64_t expected_delta) { |
| 860 | uint64_t last_us = stats->last_update_us; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 861 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 862 | stats->total_updates++; |
| 863 | stats->last_update_us = now_us; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 864 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 865 | if (last_us == 0) return; // First update: expected delta doesn't apply |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 866 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 867 | uint64_t deadline_us = last_us + expected_delta; |
| 868 | if (deadline_us < now_us) { |
| 869 | // Overdue scheduling |
| 870 | uint64_t delta_us = now_us - deadline_us; |
| 871 | // Ignore extreme outliers |
| 872 | if (delta_us < 10 * expected_delta) { |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 873 | stats->max_overdue_scheduling_delta_us = |
| 874 | std::max(delta_us, stats->max_overdue_scheduling_delta_us); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 875 | stats->total_overdue_scheduling_delta_us += delta_us; |
| 876 | stats->overdue_scheduling_count++; |
| 877 | stats->total_scheduling_time_us += now_us - last_us; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 878 | } |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 879 | } else if (deadline_us > now_us) { |
| 880 | // Premature scheduling |
| 881 | uint64_t delta_us = deadline_us - now_us; |
| 882 | // Ignore extreme outliers |
| 883 | if (delta_us < 10 * expected_delta) { |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 884 | stats->max_premature_scheduling_delta_us = |
| 885 | std::max(delta_us, stats->max_premature_scheduling_delta_us); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 886 | stats->total_premature_scheduling_delta_us += delta_us; |
| 887 | stats->premature_scheduling_count++; |
| 888 | stats->total_scheduling_time_us += now_us - last_us; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 889 | } |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 890 | } else { |
| 891 | // On-time scheduling |
| 892 | stats->exact_scheduling_count++; |
| 893 | stats->total_scheduling_time_us += now_us - last_us; |
| 894 | } |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 897 | void btif_a2dp_source_debug_dump(int fd) { |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 898 | btif_a2dp_source_accumulate_stats(&btif_a2dp_source_cb.stats, |
| 899 | &btif_a2dp_source_cb.accumulated_stats); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 900 | uint64_t now_us = time_get_os_boottime_us(); |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 901 | BtifMediaStats* accumulated_stats = &btif_a2dp_source_cb.accumulated_stats; |
| 902 | SchedulingStats* enqueue_stats = &accumulated_stats->tx_queue_enqueue_stats; |
| 903 | SchedulingStats* dequeue_stats = &accumulated_stats->tx_queue_dequeue_stats; |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 904 | size_t ave_size; |
| 905 | uint64_t ave_time_us; |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 906 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 907 | dprintf(fd, "\nA2DP State:\n"); |
| 908 | dprintf(fd, " TxQueue:\n"); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 909 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 910 | dprintf(fd, |
| 911 | " Counts (enqueue/dequeue/readbuf) : %zu / " |
| 912 | "%zu / %zu\n", |
| 913 | enqueue_stats->total_updates, dequeue_stats->total_updates, |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 914 | accumulated_stats->tx_queue_total_readbuf_calls); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 915 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 916 | dprintf( |
| 917 | fd, |
| 918 | " Last update time ago in ms (enqueue/dequeue/readbuf) : %llu / %llu " |
| 919 | "/ %llu\n", |
| 920 | (enqueue_stats->last_update_us > 0) |
| 921 | ? (unsigned long long)(now_us - enqueue_stats->last_update_us) / 1000 |
| 922 | : 0, |
| 923 | (dequeue_stats->last_update_us > 0) |
| 924 | ? (unsigned long long)(now_us - dequeue_stats->last_update_us) / 1000 |
| 925 | : 0, |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 926 | (accumulated_stats->tx_queue_last_readbuf_us > 0) |
| 927 | ? (unsigned long long)(now_us - |
| 928 | accumulated_stats->tx_queue_last_readbuf_us) / |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 929 | 1000 |
| 930 | : 0); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 931 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 932 | ave_size = 0; |
| 933 | if (enqueue_stats->total_updates != 0) |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 934 | ave_size = |
| 935 | accumulated_stats->tx_queue_total_frames / enqueue_stats->total_updates; |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 936 | dprintf(fd, |
| 937 | " Frames per packet (total/max/ave) : %zu / " |
| 938 | "%zu / %zu\n", |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 939 | accumulated_stats->tx_queue_total_frames, |
| 940 | accumulated_stats->tx_queue_max_frames_per_packet, ave_size); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 941 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 942 | dprintf(fd, |
| 943 | " Counts (flushed/dropped/dropouts) : %zu / " |
| 944 | "%zu / %zu\n", |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 945 | accumulated_stats->tx_queue_total_flushed_messages, |
| 946 | accumulated_stats->tx_queue_total_dropped_messages, |
| 947 | accumulated_stats->tx_queue_dropouts); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 948 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 949 | dprintf(fd, |
| 950 | " Counts (max dropped) : %zu\n", |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 951 | accumulated_stats->tx_queue_max_dropped_messages); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 952 | |
| 953 | dprintf( |
| 954 | fd, |
| 955 | " Last update time ago in ms (flushed/dropped) : %llu / " |
| 956 | "%llu\n", |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 957 | (accumulated_stats->tx_queue_last_flushed_us > 0) |
| 958 | ? (unsigned long long)(now_us - |
| 959 | accumulated_stats->tx_queue_last_flushed_us) / |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 960 | 1000 |
| 961 | : 0, |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 962 | (accumulated_stats->tx_queue_last_dropouts_us > 0) |
| 963 | ? (unsigned long long)(now_us - |
| 964 | accumulated_stats->tx_queue_last_dropouts_us) / |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 965 | 1000 |
| 966 | : 0); |
| 967 | |
| 968 | dprintf(fd, |
| 969 | " Counts (underflow) : %zu\n", |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 970 | accumulated_stats->media_read_total_underflow_count); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 971 | |
| 972 | dprintf(fd, |
| 973 | " Bytes (underflow) : %zu\n", |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 974 | accumulated_stats->media_read_total_underflow_bytes); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 975 | |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 976 | dprintf(fd, |
| 977 | " Last update time ago in ms (underflow) : %llu\n", |
| 978 | (accumulated_stats->media_read_last_underflow_us > 0) |
| 979 | ? (unsigned long long)(now_us - |
| 980 | accumulated_stats |
| 981 | ->media_read_last_underflow_us) / |
| 982 | 1000 |
| 983 | : 0); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 984 | |
| 985 | // |
| 986 | // TxQueue enqueue stats |
| 987 | // |
| 988 | dprintf( |
| 989 | fd, |
| 990 | " Enqueue deviation counts (overdue/premature) : %zu / %zu\n", |
| 991 | enqueue_stats->overdue_scheduling_count, |
| 992 | enqueue_stats->premature_scheduling_count); |
| 993 | |
| 994 | ave_time_us = 0; |
| 995 | if (enqueue_stats->overdue_scheduling_count != 0) { |
| 996 | ave_time_us = enqueue_stats->total_overdue_scheduling_delta_us / |
| 997 | enqueue_stats->overdue_scheduling_count; |
| 998 | } |
| 999 | dprintf( |
| 1000 | fd, |
| 1001 | " Enqueue overdue scheduling time in ms (total/max/ave) : %llu / %llu " |
| 1002 | "/ %llu\n", |
| 1003 | (unsigned long long)enqueue_stats->total_overdue_scheduling_delta_us / |
| 1004 | 1000, |
| 1005 | (unsigned long long)enqueue_stats->max_overdue_scheduling_delta_us / 1000, |
| 1006 | (unsigned long long)ave_time_us / 1000); |
| 1007 | |
| 1008 | ave_time_us = 0; |
| 1009 | if (enqueue_stats->premature_scheduling_count != 0) { |
| 1010 | ave_time_us = enqueue_stats->total_premature_scheduling_delta_us / |
| 1011 | enqueue_stats->premature_scheduling_count; |
| 1012 | } |
| 1013 | dprintf( |
| 1014 | fd, |
| 1015 | " Enqueue premature scheduling time in ms (total/max/ave) : %llu / %llu " |
| 1016 | "/ %llu\n", |
| 1017 | (unsigned long long)enqueue_stats->total_premature_scheduling_delta_us / |
| 1018 | 1000, |
| 1019 | (unsigned long long)enqueue_stats->max_premature_scheduling_delta_us / |
| 1020 | 1000, |
| 1021 | (unsigned long long)ave_time_us / 1000); |
| 1022 | |
| 1023 | // |
| 1024 | // TxQueue dequeue stats |
| 1025 | // |
| 1026 | dprintf( |
| 1027 | fd, |
| 1028 | " Dequeue deviation counts (overdue/premature) : %zu / %zu\n", |
| 1029 | dequeue_stats->overdue_scheduling_count, |
| 1030 | dequeue_stats->premature_scheduling_count); |
| 1031 | |
| 1032 | ave_time_us = 0; |
| 1033 | if (dequeue_stats->overdue_scheduling_count != 0) { |
| 1034 | ave_time_us = dequeue_stats->total_overdue_scheduling_delta_us / |
| 1035 | dequeue_stats->overdue_scheduling_count; |
| 1036 | } |
| 1037 | dprintf( |
| 1038 | fd, |
| 1039 | " Dequeue overdue scheduling time in ms (total/max/ave) : %llu / %llu " |
| 1040 | "/ %llu\n", |
| 1041 | (unsigned long long)dequeue_stats->total_overdue_scheduling_delta_us / |
| 1042 | 1000, |
| 1043 | (unsigned long long)dequeue_stats->max_overdue_scheduling_delta_us / 1000, |
| 1044 | (unsigned long long)ave_time_us / 1000); |
| 1045 | |
| 1046 | ave_time_us = 0; |
| 1047 | if (dequeue_stats->premature_scheduling_count != 0) { |
| 1048 | ave_time_us = dequeue_stats->total_premature_scheduling_delta_us / |
| 1049 | dequeue_stats->premature_scheduling_count; |
| 1050 | } |
| 1051 | dprintf( |
| 1052 | fd, |
| 1053 | " Dequeue premature scheduling time in ms (total/max/ave) : %llu / %llu " |
| 1054 | "/ %llu\n", |
| 1055 | (unsigned long long)dequeue_stats->total_premature_scheduling_delta_us / |
| 1056 | 1000, |
| 1057 | (unsigned long long)dequeue_stats->max_premature_scheduling_delta_us / |
| 1058 | 1000, |
| 1059 | (unsigned long long)ave_time_us / 1000); |
Pavlin Radoslavov | 08406e9 | 2016-09-23 16:36:47 -0700 | [diff] [blame] | 1060 | } |
Pavlin Radoslavov | 1394c19 | 2016-10-02 18:34:46 -0700 | [diff] [blame] | 1061 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 1062 | void btif_a2dp_source_update_metrics(void) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1063 | const BtifMediaStats& stats = btif_a2dp_source_cb.stats; |
| 1064 | const SchedulingStats& enqueue_stats = stats.tx_queue_enqueue_stats; |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 1065 | A2dpSessionMetrics metrics; |
Jack He | ee5c257 | 2017-01-20 11:17:44 -0800 | [diff] [blame] | 1066 | // session_start_us is 0 when btif_a2dp_source_start_audio_req() is not called |
| 1067 | // mark the metric duration as invalid (-1) in this case |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1068 | if (stats.session_start_us != 0) { |
| 1069 | int64_t session_end_us = stats.session_end_us == 0 |
Jack He | ee5c257 | 2017-01-20 11:17:44 -0800 | [diff] [blame] | 1070 | ? time_get_os_boottime_us() |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1071 | : stats.session_end_us; |
Jack He | ee5c257 | 2017-01-20 11:17:44 -0800 | [diff] [blame] | 1072 | metrics.audio_duration_ms = |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1073 | (session_end_us - stats.session_start_us) / 1000; |
Jack He | ee5c257 | 2017-01-20 11:17:44 -0800 | [diff] [blame] | 1074 | } |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 1075 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1076 | if (enqueue_stats.total_updates > 1) { |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 1077 | metrics.media_timer_min_ms = |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 1078 | btif_a2dp_source_cb.encoder_interval_ms - |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1079 | (enqueue_stats.max_premature_scheduling_delta_us / 1000); |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 1080 | metrics.media_timer_max_ms = |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 1081 | btif_a2dp_source_cb.encoder_interval_ms + |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1082 | (enqueue_stats.max_overdue_scheduling_delta_us / 1000); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 1083 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1084 | metrics.total_scheduling_count = enqueue_stats.overdue_scheduling_count + |
| 1085 | enqueue_stats.premature_scheduling_count + |
| 1086 | enqueue_stats.exact_scheduling_count; |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 1087 | if (metrics.total_scheduling_count > 0) { |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1088 | metrics.media_timer_avg_ms = enqueue_stats.total_scheduling_time_us / |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 1089 | (1000 * metrics.total_scheduling_count); |
Pavlin Radoslavov | 1394c19 | 2016-10-02 18:34:46 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1092 | metrics.buffer_overruns_max_count = stats.tx_queue_max_dropped_messages; |
| 1093 | metrics.buffer_overruns_total = stats.tx_queue_total_dropped_messages; |
| 1094 | metrics.buffer_underruns_count = stats.media_read_total_underflow_count; |
Jack He | ee5c257 | 2017-01-20 11:17:44 -0800 | [diff] [blame] | 1095 | metrics.buffer_underruns_average = 0; |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 1096 | if (metrics.buffer_underruns_count > 0) { |
| 1097 | metrics.buffer_underruns_average = |
Pavlin Radoslavov | d752229 | 2017-11-24 19:12:11 -0800 | [diff] [blame] | 1098 | stats.media_read_total_underflow_bytes / |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 1099 | metrics.buffer_underruns_count; |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 1100 | } |
| 1101 | } |
Jack He | f317562 | 2016-12-08 19:29:00 -0800 | [diff] [blame] | 1102 | BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics); |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | static void btm_read_rssi_cb(void* data) { |
Andre Eisenbach | ce8e858 | 2016-11-03 15:05:22 -0700 | [diff] [blame] | 1106 | if (data == nullptr) { |
Pavlin Radoslavov | b8568ae | 2017-09-01 16:09:27 -0700 | [diff] [blame] | 1107 | LOG_ERROR(LOG_TAG, "%s Read RSSI request timed out", __func__); |
Andre Eisenbach | ce8e858 | 2016-11-03 15:05:22 -0700 | [diff] [blame] | 1108 | return; |
| 1109 | } |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 1110 | |
Pavlin Radoslavov | c7bf47c | 2017-09-01 16:40:59 -0700 | [diff] [blame] | 1111 | tBTM_RSSI_RESULT* result = (tBTM_RSSI_RESULT*)data; |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 1112 | if (result->status != BTM_SUCCESS) { |
| 1113 | LOG_ERROR(LOG_TAG, "%s unable to read remote RSSI (status %d)", __func__, |
| 1114 | result->status); |
| 1115 | return; |
| 1116 | } |
| 1117 | |
Pavlin Radoslavov | 397e5a5 | 2016-10-14 16:13:54 -0700 | [diff] [blame] | 1118 | LOG_WARN(LOG_TAG, "%s device: %s, rssi: %d", __func__, |
Jakub Pawlowski | b707f44 | 2017-07-03 15:39:36 -0700 | [diff] [blame] | 1119 | result->rem_bda.ToString().c_str(), result->rssi); |
Pavlin Radoslavov | 1394c19 | 2016-10-02 18:34:46 -0700 | [diff] [blame] | 1120 | } |
Pavlin Radoslavov | 6ab749f | 2017-08-31 22:06:11 -0700 | [diff] [blame] | 1121 | |
| 1122 | static void btm_read_failed_contact_counter_cb(void* data) { |
| 1123 | if (data == nullptr) { |
Pavlin Radoslavov | b8568ae | 2017-09-01 16:09:27 -0700 | [diff] [blame] | 1124 | LOG_ERROR(LOG_TAG, "%s Read Failed Contact Counter request timed out", |
| 1125 | __func__); |
Pavlin Radoslavov | 6ab749f | 2017-08-31 22:06:11 -0700 | [diff] [blame] | 1126 | return; |
| 1127 | } |
| 1128 | |
| 1129 | tBTM_FAILED_CONTACT_COUNTER_RESULT* result = |
| 1130 | (tBTM_FAILED_CONTACT_COUNTER_RESULT*)data; |
| 1131 | if (result->status != BTM_SUCCESS) { |
Pavlin Radoslavov | b8568ae | 2017-09-01 16:09:27 -0700 | [diff] [blame] | 1132 | LOG_ERROR(LOG_TAG, "%s unable to read Failed Contact Counter (status %d)", |
Pavlin Radoslavov | 6ab749f | 2017-08-31 22:06:11 -0700 | [diff] [blame] | 1133 | __func__, result->status); |
| 1134 | return; |
| 1135 | } |
| 1136 | |
| 1137 | LOG_WARN(LOG_TAG, "%s device: %s, Failed Contact Counter: %u", __func__, |
| 1138 | result->rem_bda.ToString().c_str(), result->failed_contact_counter); |
| 1139 | } |
Pavlin Radoslavov | b8568ae | 2017-09-01 16:09:27 -0700 | [diff] [blame] | 1140 | |
| 1141 | static void btm_read_automatic_flush_timeout_cb(void* data) { |
| 1142 | if (data == nullptr) { |
| 1143 | LOG_ERROR(LOG_TAG, "%s Read Automatic Flush Timeout request timed out", |
| 1144 | __func__); |
| 1145 | return; |
| 1146 | } |
| 1147 | |
| 1148 | tBTM_AUTOMATIC_FLUSH_TIMEOUT_RESULT* result = |
| 1149 | (tBTM_AUTOMATIC_FLUSH_TIMEOUT_RESULT*)data; |
| 1150 | if (result->status != BTM_SUCCESS) { |
| 1151 | LOG_ERROR(LOG_TAG, "%s unable to read Automatic Flush Timeout (status %d)", |
| 1152 | __func__, result->status); |
| 1153 | return; |
| 1154 | } |
| 1155 | |
| 1156 | LOG_WARN(LOG_TAG, "%s device: %s, Automatic Flush Timeout: %u", __func__, |
| 1157 | result->rem_bda.ToString().c_str(), result->automatic_flush_timeout); |
| 1158 | } |
Pavlin Radoslavov | c7bf47c | 2017-09-01 16:40:59 -0700 | [diff] [blame] | 1159 | |
| 1160 | static void btm_read_tx_power_cb(void* data) { |
| 1161 | if (data == nullptr) { |
| 1162 | LOG_ERROR(LOG_TAG, "%s Read Tx Power request timed out", __func__); |
| 1163 | return; |
| 1164 | } |
| 1165 | |
| 1166 | tBTM_TX_POWER_RESULT* result = (tBTM_TX_POWER_RESULT*)data; |
| 1167 | if (result->status != BTM_SUCCESS) { |
| 1168 | LOG_ERROR(LOG_TAG, "%s unable to read Tx Power (status %d)", __func__, |
| 1169 | result->status); |
| 1170 | return; |
| 1171 | } |
| 1172 | |
| 1173 | LOG_WARN(LOG_TAG, "%s device: %s, Tx Power: %d", __func__, |
| 1174 | result->rem_bda.ToString().c_str(), result->tx_power); |
| 1175 | } |