Skip to content

Commit

Permalink
音声トラック毎に音量調整できるかの検証UIを足す
Browse files Browse the repository at this point in the history
  • Loading branch information
zztkm committed Aug 22, 2024
1 parent aacfa27 commit 1a4c464
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,26 @@ class SoraAudioChannel(
disconnect()
}

// ローカル (自分の) ストリームが追加されたときに呼ばれる
override fun onAddLocalStream(mediaChannel: SoraMediaChannel, ms: MediaStream) {
SoraLogger.d(TAG, "[audio_channel] @onAddLocalStream")
SoraLogger.d(TAG, "[audio_channel] @onAddLocalStream: connectionId=${mediaChannel.connectionId}")
if (ms.audioTracks.size > 0) {
localAudioTrack = ms.audioTracks[0]
}
}

// リモート (自分以外の) ストリームが追加されたときに呼ばれる
override fun onAddRemoteStream(mediaChannel: SoraMediaChannel, ms: MediaStream) {
// TODO: 相手が誰なのかを知る方法はあるか?
SoraLogger.d(TAG, "[audio_channel] @onAddRemoteStream")
if (ms.audioTracks.size > 0) {
remoteAudioTrack = ms.audioTracks[0]
// https://chromium.googlesource.com/external/webrtc/+/HEAD/sdk/android/api/org/webrtc/AudioTrack.java
// setVolume は 0 から 10 までの値を取る
remoteAudioTrack?.setVolume(0.0)
}
}

override fun onAttendeesCountUpdated(mediaChannel: SoraMediaChannel, attendees: ChannelAttendeesCount) {
SoraLogger.d(TAG, "[audio_channel] @onAttendeesCountUpdated")
handler.post { listener?.onAttendeesCountUpdated(this@SoraAudioChannel, attendees) }
Expand All @@ -72,6 +85,7 @@ class SoraAudioChannel(

private var mediaChannel: SoraMediaChannel? = null
private var localAudioTrack: AudioTrack? = null
private var remoteAudioTrack: AudioTrack? = null

private var closed = false

Expand Down Expand Up @@ -118,6 +132,7 @@ class SoraAudioChannel(
handler.post {
listener?.onClose(this@SoraAudioChannel)
localAudioTrack = null
remoteAudioTrack = null
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,10 @@ class SoraVideoChannel(
localAudioTrack?.setEnabled(!mute)
}

fun setAudioVolume(msid: String, volume: Double) {
remoteRenderersSlot?.setVolume(msid, volume)
}

fun disconnect() {
stopCapturer()
mediaChannel?.disconnect()
Expand Down Expand Up @@ -437,4 +441,12 @@ class SoraVideoChannel(
egl = null
listener = null
}

fun listMsids(): List<String> {
return remoteRenderersSlot?.listMsids() ?: emptyList()
}

fun getMsidByIndex(index: Int): String? {
return remoteRenderersSlot?.listMsids()?.get(index)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,12 @@ class VideoChatRoomActivity : AppCompatActivity() {

override fun onAddRemoteRenderer(channel: SoraVideoChannel, renderer: SurfaceViewRenderer) {
ui?.addRenderer(renderer)
ui?.setMsids(channel.listMsids())
}

override fun onRemoveRemoteRenderer(channel: SoraVideoChannel, renderer: SurfaceViewRenderer) {
ui?.removeRenderer(renderer)
ui?.setMsids(channel.listMsids())
}

override fun onAttendeesCountUpdated(channel: SoraVideoChannel, attendees: ChannelAttendeesCount) {
Expand Down Expand Up @@ -391,6 +393,25 @@ class VideoChatRoomActivity : AppCompatActivity() {
muted = !muted
channel?.mute(muted)
}

internal fun toggleMutedTest() {
if (muted) {
ui?.showMuteButtonTest()
// 現在選択されている msid (connectionID を内部的には MediaStream id として扱っている) の音量を 3 にする
// 3 でも変化を感じられたので
ui?.getSelectedMsid()?.let {
channel?.setAudioVolume(it, 3.0)
}
} else {
ui?.showUnmuteButtonTest()
// 現在選択されている msid の音量を 0.0 にする
ui?.getSelectedMsid()?.let {
channel?.setAudioVolume(it, 0.5)
}
}
muted = !muted
channel?.mute(muted)
}
}

class VideoChatRoomActivityUI(
Expand All @@ -415,8 +436,11 @@ class VideoChatRoomActivityUI(
height = SoraScreenUtil.size(activity).y - dp2px(20 * 2 + 100)
)
binding.toggleMuteButton.setOnClickListener { activity.toggleMuted() }
// (仮)setVolume で音量を切り替えるためのボタン
binding.toggleMuteButtonTest.setOnClickListener { activity.toggleMutedTest() }
binding.switchCameraButton.setOnClickListener { activity.switchCamera() }
binding.closeButton.setOnClickListener { activity.close() }
binding.msidSelection.name.text = "音量調整する connectionID"
}

internal fun changeState(colorCode: String) {
Expand Down Expand Up @@ -452,6 +476,30 @@ class VideoChatRoomActivityUI(
)
}

internal fun showUnmuteButtonTest() {
binding.toggleMuteButtonTest.setImageDrawable(
resources.getDrawable(R.drawable.ic_mic_white_48dp, null)
)
}

internal fun showMuteButtonTest() {
binding.toggleMuteButtonTest.setImageDrawable(
resources.getDrawable(R.drawable.ic_mic_off_black_48dp, null)
)
}

/**
* 音量調整対象の connectionID 一覧を更新する
*/
internal fun setMsids(msids: List<String>) {
SoraLogger.d("kensaku", "setMsids: $msids")
binding.msidSelection.spinner.setItems(msids)
}

internal fun getSelectedMsid(): String {
return binding.msidSelection.spinner.getItems<String>()[binding.msidSelection.spinner.selectedIndex]
}

private fun dp2px(d: Int): Int = (density * d).toInt()

private fun rendererLayoutParams(): RelativeLayout.LayoutParams {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jp.shiguredo.sora.sample.ui.util

import android.content.Context
import jp.shiguredo.sora.sdk.util.SoraLogger
import org.webrtc.AudioTrack
import org.webrtc.EglBase
import org.webrtc.MediaStream
import org.webrtc.SurfaceViewRenderer
Expand All @@ -24,6 +25,8 @@ class SoraRemoteRendererSlot(

val workingRenderers = HashMap<String, SurfaceViewRenderer>()
val workingTracks = HashMap<String, VideoTrack>()
// ここに Volume 持たせるのはこの class の目的外だと思うので、仮置きとしておく
val workingVolumeTracks = HashMap<String, AudioTrack>()

fun onAddRemoteStream(ms: MediaStream) {
SoraLogger.d(TAG, "onAddRemoteStream:${ms.id}")
Expand All @@ -36,6 +39,10 @@ class SoraRemoteRendererSlot(
listener?.onAddRenderer(renderer)

val track = ms.videoTracks[0]
if (ms.audioTracks.size > 0) {
val audioTrack = ms.audioTracks[0]
workingVolumeTracks[ms.id] = audioTrack
}
workingRenderers.put(ms.id, renderer)
workingTracks.put(ms.id, track)

Expand All @@ -58,6 +65,7 @@ class SoraRemoteRendererSlot(
}
workingRenderers.remove(msid)
workingTracks.remove(msid)
workingVolumeTracks.remove(msid)
}
}

Expand All @@ -67,6 +75,16 @@ class SoraRemoteRendererSlot(
return renderer
}

fun setVolume(msid: String, volume: Double) {
if (workingVolumeTracks.containsKey(msid)) {
workingVolumeTracks[msid]?.setVolume(volume)
}
}

fun listMsids(): List<String> {
return workingVolumeTracks.keys.toList()
}

fun dispose() {
SoraLogger.d(TAG, "dispose")
workingRenderers.values.forEach {
Expand All @@ -75,6 +93,7 @@ class SoraRemoteRendererSlot(
}
workingRenderers.clear()
workingTracks.clear()
workingVolumeTracks.clear()
listener = null
}
}
17 changes: 17 additions & 0 deletions samples/src/main/res/layout/activity_video_chat_room.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
android:gravity="center"
android:text="[Channel]" />

<include
android:id="@+id/msidSelection"
layout="@layout/signaling_selection"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<RelativeLayout
android:id="@+id/rendererContainer"
android:layout_width="wrap_content"
Expand Down Expand Up @@ -55,6 +61,17 @@
android:layout_alignParentEnd="true"
android:layout_centerVertical="true">

<ImageButton
android:id="@+id/toggleMuteButtonTest"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginEnd="10dp"
android:layout_weight="1"
android:background="@drawable/enabled_button_background"
android:padding="6dp"
android:scaleType="fitCenter"
app:srcCompat="@drawable/ic_mic_off_black_48dp" />

<ImageButton
android:id="@+id/toggleMuteButton"
android:layout_width="50dp"
Expand Down

0 comments on commit 1a4c464

Please sign in to comment.