Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a1845300 authored by Pavlin Radoslavov's avatar Pavlin Radoslavov
Browse files

Added Start/End Session steps when A2DP codec is changed

If the A2DP codec is changed for the active device, then
the current session is restarted by calling End/Start Session.

Also:
 * Updated btav_a2dp_codec_config_t string representation to
   include the codec priority.
 * Fixed A2dpCodecConfig::setCodecPriority() and setDefaultCodecPriority()
   so the codec priority for the current codec_config_ is updated.
 * Fixed A2dpCodecs::setCodecUserConfig() so the restart_input flag
   is set as appropriate.
 * Use btav_a2dp_codec_config_t::ToString() as appropritate to print
   the codec config instead of explicitly printing each field.

Bug: 74988739
Test: Manual: Connect two headsets, change codecs, switch active device.
Change-Id: I6652168f3c48e5b431e00aa8f554929afbdcdbcf
Merged-In: I6652168f3c48e5b431e00aa8f554929afbdcdbcf
(cherry picked from commit 42c88d1a55ac98b46c757f18c8cb518fd442e649)
parent b5b3c361
Loading
Loading
Loading
Loading
+30 −16
Original line number Diff line number Diff line
@@ -438,6 +438,32 @@ class BtifAvSource {
    return true;
  }

  /**
   * Update source codec configuration for a peer.
   *
   * @param peer_address the address of the peer to update
   * @param codec_preferences the updated codec preferences
   */
  void UpdateCodecConfig(
      const RawAddress& peer_address,
      const std::vector<btav_a2dp_codec_config_t>& codec_preferences) {
    // Restart the session if the codec for the active peer is updated
    bool restart_session =
        ((active_peer_ == peer_address) && !active_peer_.IsEmpty());
    if (restart_session) {
      btif_a2dp_source_end_session(active_peer_);
    }

    for (auto cp : codec_preferences) {
      BTIF_TRACE_DEBUG("%s: codec_preference=%s", __func__,
                       cp.ToString().c_str());
      btif_a2dp_source_encoder_user_config_update_req(peer_address, cp);
    }
    if (restart_session) {
      btif_a2dp_source_start_session(active_peer_);
    }
  }

  const std::map<RawAddress, BtifAvPeer*>& Peers() const { return peers_; }

  void RegisterAllBtaHandles();
@@ -2637,22 +2663,10 @@ static bt_status_t codec_config_src(
    return BT_STATUS_NOT_READY;
  }

  for (auto cp : codec_preferences) {
    BTIF_TRACE_DEBUG(
        "%s: codec_type=%d codec_priority=%d "
        "sample_rate=0x%x bits_per_sample=0x%x "
        "channel_mode=0x%x codec_specific_1=%d "
        "codec_specific_2=%d codec_specific_3=%d "
        "codec_specific_4=%d",
        __func__, cp.codec_type, cp.codec_priority, cp.sample_rate,
        cp.bits_per_sample, cp.channel_mode, cp.codec_specific_1,
        cp.codec_specific_2, cp.codec_specific_3, cp.codec_specific_4);
    do_in_jni_thread(
        FROM_HERE, base::Bind(&btif_a2dp_source_encoder_user_config_update_req,
                              peer_address, cp));
  }

  return BT_STATUS_SUCCESS;
  return do_in_jni_thread(
      FROM_HERE, base::Bind(&BtifAvSource::UpdateCodecConfig,
                            base::Unretained(&btif_av_source), peer_address,
                            codec_preferences));
}

static void cleanup_src(void) {
+3 −1
Original line number Diff line number Diff line
@@ -212,7 +212,9 @@ typedef struct {
                     (channel_mode & BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO),
                     "STEREO");

    return "codec: " + codec_name_str + " sample_rate: " + sample_rate_str +
    return "codec: " + codec_name_str +
           " priority: " + std::to_string(codec_priority) +
           " sample_rate: " + sample_rate_str +
           " bits_per_sample: " + bits_per_sample_str +
           " channel_mode: " + channel_mode_str +
           " codec_specific_1: " + std::to_string(codec_specific_1) +
+6 −16
Original line number Diff line number Diff line
@@ -89,6 +89,7 @@ void A2dpCodecConfig::setCodecPriority(
  } else {
    codec_priority_ = codec_priority;
  }
  codec_config_.codec_priority = codec_priority_;
}

void A2dpCodecConfig::setDefaultCodecPriority() {
@@ -99,6 +100,7 @@ void A2dpCodecConfig::setDefaultCodecPriority() {
    uint32_t priority = 1000 * (codec_index_ + 1) + 1;
    codec_priority_ = static_cast<btav_a2dp_codec_priority_t>(priority);
  }
  codec_config_.codec_priority = codec_priority_;
}

A2dpCodecConfig* A2dpCodecConfig::createCodec(
@@ -677,22 +679,8 @@ bool A2dpCodecs::setCodecUserConfig(
  *p_restart_output = false;
  *p_config_updated = false;

  LOG_DEBUG(
      LOG_TAG,
      "%s: Configuring: codec_type=%d codec_priority=%d "
      "sample_rate=0x%x bits_per_sample=0x%x "
      "channel_mode=0x%x codec_specific_1=%" PRIi64
      " "
      "codec_specific_2=%" PRIi64
      " "
      "codec_specific_3=%" PRIi64
      " "
      "codec_specific_4=%" PRIi64,
      __func__, codec_user_config.codec_type, codec_user_config.codec_priority,
      codec_user_config.sample_rate, codec_user_config.bits_per_sample,
      codec_user_config.channel_mode, codec_user_config.codec_specific_1,
      codec_user_config.codec_specific_2, codec_user_config.codec_specific_3,
      codec_user_config.codec_specific_4);
  LOG_DEBUG(LOG_TAG, "%s: Configuring: %s", __func__,
            codec_user_config.ToString().c_str());

  if (codec_user_config.codec_type < BTAV_A2DP_CODEC_INDEX_MAX) {
    auto iter = indexed_codecs_.find(codec_user_config.codec_type);
@@ -727,6 +715,7 @@ bool A2dpCodecs::setCodecUserConfig(
    // Check if there was no previous codec
    if (last_codec_config == nullptr) {
      current_codec_config_ = a2dp_codec_config;
      *p_restart_input = true;
      *p_restart_output = true;
      break;
    }
@@ -762,6 +751,7 @@ bool A2dpCodecs::setCodecUserConfig(
      // connection to select a new codec.
      current_codec_config_ = a2dp_codec_config;
      last_codec_config->setDefaultCodecPriority();
      *p_restart_input = true;
      *p_restart_output = true;
    }
  } while (false);