-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from IoTeaTime/feature/54-kan-127-navigation-s…
…etting feat : 앱 구조 설계
- Loading branch information
Showing
7 changed files
with
373 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
app/src/main/java/com/example/mhnfe/data/signaling/model/Event.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.example.mhnfe.data.signaling.model | ||
|
||
import android.util.Base64 | ||
import android.util.Log | ||
import com.google.gson.JsonParser | ||
import org.webrtc.IceCandidate | ||
import java.nio.charset.StandardCharsets | ||
|
||
/** | ||
* A class representing the Event object. All response messages are asynchronously delivered | ||
* to the recipient as events (for example, an SDP offer or SDP answer delivery). | ||
* | ||
* @see <a href="https://docs.aws.amazon.com/kinesisvideostreams-webrtc-dg/latest/devguide/kvswebrtc-websocket-apis-7.html">Event</a> | ||
*/ | ||
class Event( | ||
val senderClientId: String, | ||
val messageType: String, | ||
val messagePayload: String, | ||
var statusCode: String? = null, | ||
var body: String? = null | ||
) { | ||
companion object { | ||
private const val TAG = "Event" | ||
|
||
/** | ||
* Attempts to convert an [ICE_CANDIDATE] [Event] into an [IceCandidate]. | ||
* | ||
* @param event the ICE_CANDIDATE event to convert. | ||
* @return an [IceCandidate] from the [Event]. `null` if the IceCandidate wasn't | ||
* able to be constructed. | ||
*/ | ||
fun parseIceCandidate(event: Event?): IceCandidate? { | ||
if (event == null || event.messageType.equals("ICE_CANDIDATE", ignoreCase = true).not()) { | ||
Log.e(TAG, "$event is not an ICE_CANDIDATE type!") | ||
return null | ||
} | ||
|
||
val decode = Base64.decode(event.messagePayload, Base64.DEFAULT) | ||
val candidateString = String(decode, StandardCharsets.UTF_8) | ||
|
||
if (candidateString == "null") { | ||
Log.w(TAG, "Received null IceCandidate!") | ||
return null | ||
} | ||
|
||
val jsonObject = JsonParser.parseString(candidateString).asJsonObject | ||
|
||
val sdpMid = jsonObject["sdpMid"]?.asString?.removeSurrounding("\"") ?: "" | ||
|
||
val sdpMLineIndex = jsonObject["sdpMLineIndex"]?.asInt ?: -1 | ||
|
||
// Ice Candidate needs one of these two to be present | ||
if (sdpMid.isEmpty() && sdpMLineIndex == -1) { | ||
return null | ||
} | ||
|
||
val candidate = jsonObject["candidate"]?.asString?.removeSurrounding("\"") ?: "" | ||
|
||
return IceCandidate(sdpMid, if (sdpMLineIndex == -1) 0 else sdpMLineIndex, candidate) | ||
} | ||
|
||
fun parseSdpEvent(answerEvent: Event): String { | ||
val message = String(Base64.decode(answerEvent.messagePayload.toByteArray(), Base64.DEFAULT)) | ||
val jsonObject = JsonParser.parseString(message).asJsonObject | ||
val type = jsonObject["type"].asString | ||
|
||
if (!type.equals("answer", ignoreCase = true)) { | ||
Log.e(TAG, "Error in answer message") | ||
} | ||
|
||
val sdp = jsonObject["sdp"].asString | ||
Log.d(TAG, "SDP answer received from master: $sdp") | ||
return sdp | ||
} | ||
|
||
fun parseOfferEvent(offerEvent: Event): String { | ||
val decodedPayload = String(Base64.decode(offerEvent.messagePayload, Base64.DEFAULT)) | ||
|
||
return JsonParser.parseString(decodedPayload) | ||
.takeIf { it.isJsonObject } | ||
?.asJsonObject | ||
?.get("sdp") | ||
?.asString ?: "" | ||
} | ||
} | ||
|
||
override fun toString(): String { | ||
return "Event(senderClientId='$senderClientId', messageType='$messageType', messagePayload='$messagePayload', statusCode='$statusCode', body='$body')" | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
app/src/main/java/com/example/mhnfe/data/signaling/model/Message.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.example.mhnfe.data.signaling.model | ||
|
||
import android.util.Base64 | ||
import org.webrtc.SessionDescription | ||
|
||
data class Message( | ||
var action: String?, | ||
var recipientClientId: String?, | ||
var senderClientId: String?, | ||
var messagePayload: String? | ||
) { | ||
constructor() : this(null, null, null, null) | ||
|
||
// 두 번째 생성자는 팩토리 메서드로 변경 | ||
companion object { | ||
fun createMessage( | ||
action: String, | ||
recipientClientId: String, | ||
senderClientId: String, | ||
messagePayload: String | ||
) = Message(action, recipientClientId, senderClientId, messagePayload) | ||
|
||
fun createAnswerMessage( | ||
sessionDescription: SessionDescription, | ||
master: Boolean, | ||
recipientClientId: String? | ||
): Message { | ||
val description = sessionDescription.description | ||
val answerPayload = "{\"type\":\"answer\",\"sdp\":\"${description.replace("\r\n", "\\r\\n")}\"}" | ||
val encodedString = Base64.encodeToString(answerPayload.toByteArray(), Base64.URL_SAFE or Base64.NO_WRAP) | ||
|
||
// SenderClientId should always be "" for master creating answer case | ||
return createMessage("SDP_ANSWER", recipientClientId ?: "", "", encodedString) | ||
} | ||
|
||
fun createOfferMessage( | ||
sessionDescription: SessionDescription, | ||
clientId: String | ||
): Message { | ||
val description = sessionDescription.description | ||
val offerPayload = "{\"type\":\"offer\",\"sdp\":\"${description.replace("\r\n", "\\r\\n")}\"}" | ||
val encodedString = Base64.encodeToString(offerPayload.toByteArray(), Base64.URL_SAFE or Base64.NO_WRAP) | ||
|
||
return createMessage("SDP_OFFER", "", clientId, encodedString) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.