1# Copyright 2021-2024 Google LLC 2# 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# https://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# Imports 17# ----------------------------------------------------------------------------- 18import asyncio 19import logging 20import sys 21import os 22import secrets 23import websockets 24import json 25 26from bumble.core import AdvertisingData 27from bumble.device import Device, AdvertisingParameters, AdvertisingEventProperties 28from bumble.hci import ( 29 CodecID, 30 CodingFormat, 31 OwnAddressType, 32) 33from bumble.profiles.bap import ( 34 UnicastServerAdvertisingData, 35 CodecSpecificCapabilities, 36 ContextType, 37 AudioLocation, 38 SupportedSamplingFrequency, 39 SupportedFrameDuration, 40 PacRecord, 41 PublishedAudioCapabilitiesService, 42 AudioStreamControlService, 43) 44from bumble.profiles.cap import CommonAudioServiceService 45from bumble.profiles.csip import CoordinatedSetIdentificationService, SirkType 46from bumble.profiles.vcp import VolumeControlService 47 48from bumble.transport import open_transport_or_link 49 50from typing import Optional 51 52 53def dumps_volume_state(volume_setting: int, muted: int, change_counter: int) -> str: 54 return json.dumps( 55 { 56 'volume_setting': volume_setting, 57 'muted': muted, 58 'change_counter': change_counter, 59 } 60 ) 61 62 63# ----------------------------------------------------------------------------- 64async def main() -> None: 65 if len(sys.argv) < 3: 66 print('Usage: run_vcp_renderer.py <config-file>' '<transport-spec-for-device>') 67 return 68 69 print('<<< connecting to HCI...') 70 async with await open_transport_or_link(sys.argv[2]) as hci_transport: 71 print('<<< connected') 72 73 device = Device.from_config_file_with_hci( 74 sys.argv[1], hci_transport.source, hci_transport.sink 75 ) 76 77 await device.power_on() 78 79 # Add "placeholder" services to enable Android LEA features. 80 csis = CoordinatedSetIdentificationService( 81 set_identity_resolving_key=secrets.token_bytes(16), 82 set_identity_resolving_key_type=SirkType.PLAINTEXT, 83 ) 84 device.add_service(CommonAudioServiceService(csis)) 85 device.add_service( 86 PublishedAudioCapabilitiesService( 87 supported_source_context=ContextType.PROHIBITED, 88 available_source_context=ContextType.PROHIBITED, 89 supported_sink_context=ContextType.MEDIA, 90 available_sink_context=ContextType.MEDIA, 91 sink_audio_locations=( 92 AudioLocation.FRONT_LEFT | AudioLocation.FRONT_RIGHT 93 ), 94 sink_pac=[ 95 # Codec Capability Setting 48_4 96 PacRecord( 97 coding_format=CodingFormat(CodecID.LC3), 98 codec_specific_capabilities=CodecSpecificCapabilities( 99 supported_sampling_frequencies=( 100 SupportedSamplingFrequency.FREQ_48000 101 ), 102 supported_frame_durations=( 103 SupportedFrameDuration.DURATION_10000_US_SUPPORTED 104 ), 105 supported_audio_channel_count=[1], 106 min_octets_per_codec_frame=120, 107 max_octets_per_codec_frame=120, 108 supported_max_codec_frames_per_sdu=1, 109 ), 110 ), 111 ], 112 ) 113 ) 114 device.add_service(AudioStreamControlService(device, sink_ase_id=[1, 2])) 115 116 vcs = VolumeControlService() 117 device.add_service(vcs) 118 119 ws: Optional[websockets.WebSocketServerProtocol] = None 120 121 def on_volume_state(volume_setting: int, muted: int, change_counter: int): 122 if ws: 123 asyncio.create_task( 124 ws.send(dumps_volume_state(volume_setting, muted, change_counter)) 125 ) 126 127 vcs.on('volume_state', on_volume_state) 128 129 advertising_data = ( 130 bytes( 131 AdvertisingData( 132 [ 133 ( 134 AdvertisingData.COMPLETE_LOCAL_NAME, 135 bytes('Bumble LE Audio', 'utf-8'), 136 ), 137 ( 138 AdvertisingData.FLAGS, 139 bytes( 140 [ 141 AdvertisingData.LE_GENERAL_DISCOVERABLE_MODE_FLAG 142 | AdvertisingData.BR_EDR_HOST_FLAG 143 | AdvertisingData.BR_EDR_CONTROLLER_FLAG 144 ] 145 ), 146 ), 147 ( 148 AdvertisingData.INCOMPLETE_LIST_OF_16_BIT_SERVICE_CLASS_UUIDS, 149 bytes(PublishedAudioCapabilitiesService.UUID), 150 ), 151 ] 152 ) 153 ) 154 + csis.get_advertising_data() 155 + bytes(UnicastServerAdvertisingData()) 156 ) 157 158 await device.create_advertising_set( 159 advertising_parameters=AdvertisingParameters( 160 advertising_event_properties=AdvertisingEventProperties(), 161 own_address_type=OwnAddressType.PUBLIC, 162 ), 163 advertising_data=advertising_data, 164 ) 165 166 async def serve(websocket: websockets.WebSocketServerProtocol, _path): 167 nonlocal ws 168 await websocket.send( 169 dumps_volume_state(vcs.volume_setting, vcs.muted, vcs.change_counter) 170 ) 171 ws = websocket 172 async for message in websocket: 173 volume_state = json.loads(message) 174 vcs.volume_state_bytes = bytes( 175 [ 176 volume_state['volume_setting'], 177 volume_state['muted'], 178 volume_state['change_counter'], 179 ] 180 ) 181 await device.notify_subscribers( 182 vcs.volume_state, vcs.volume_state_bytes 183 ) 184 ws = None 185 186 await websockets.serve(serve, 'localhost', 8989) 187 188 await hci_transport.source.terminated 189 190 191# ----------------------------------------------------------------------------- 192logging.basicConfig(level=os.environ.get('BUMBLE_LOGLEVEL', 'DEBUG').upper()) 193asyncio.run(main()) 194