# Quick Start

Omnitalk SDK를 어떻게 활용할 수 있는지에 대한 간단한 예시입니다. 자세한 사용법은 [Android API Reference](https://docs.omnitalk.io/android/api-reference)를 참조 바랍니다.

기본적으로 모든 API는 suspend fun 으로 작성 되었으며, 요청이 실패한 경우 에러를 throw 합니다. 예시와 데모에서는 CoroutineScope를 사용합니다.

데모 앱 소스코드: <https://github.com/omnistory-labs/omnitalk.android.sdk/tree/demo>

## 1:1 영상 통화

### 1. 옴니톡 객체 생성

[콘솔](https://omnitalk.io/console/service/service-id)에서 발급받은 Service ID와 Service KEY로 Omnitalk 객체를 생성합니다.

```kotlin
import io.omnitalk.sdk.Omnitalk

Omnitalk.sdkInit(
    serviceId = "YOUR_SERVICE_ID",
    serviceKey = "YOUR_SERVICE_KEY",
    applicationContext = applicationContext
)

val sdk = Omnitalk.getInstance()
```

### 2. 세션 생성

인수로 전달한 userId로 세션을 생성하게 됩니다. userId는 Optional이며, null일 경우 서버에서 임의의 userId를 생성합니다.

```kotlin
val createSessionResult = sdk.createSession(userId = "alice")
```

### 3. 발신

1:1 영상 통화를 구현하기 위한 발신 기능은 [offerCall()](https://docs.omnitalk.io/api-reference#offercall) API를 이용합니다. 자신과 상대방의 영상을 화면에 재생하기 위해서 파라미터에 영상을 출력할 `SurfaceViewRenderer` 객체를 전달합니다. 해당 객체를 생성하기 위해서 WebRTC 모듈을 import 해야 합니다. ( Omnitalk SDK 설치에 포함된 패키지 )

offerCall 호출이 성공하면 caller에게는 `RINGBACK_EVENT`가, callee에게는 `RINGING_EVENT`가 전달됩니다.

```kotlin
import org.webrtc.SurfaceViewRenderer
import io.omnitalk.sdk.types.PublicTypes

val callee = "test@omnistory.net"
val localView = findViewById<SurfaceViewRenderer>(R.id.localView)
val remoteView = findViewById<SurfaceViewRenderer>(R.id.remoteView)

sdk.offerCall(
    callType = PublicTypes.CALL_TYPE.videocall,
    callee = callee,
    record = true,
    localView = localView,
    remoteView = remoteView
)
```

### 4. 수신

callee측에서는 `RINGING_EVENT`를 받고 통화를 수락하거나 거절할 수 있습니다. 통화 수락은 [answerCall()](https://docs.omnitalk.io/api-reference#answercall) API를 이용합니다. 자신과 상대방의 영상을 재생하기 위해서 파라미터에 영상을 출력할 `SurfaceViewRenderer` 객체를 전달합니다. 해당 객체를 생성하기 위해서 WebRTC 모듈을 import 해야 합니다. ( Omnitalk SDK 설치에 포함된 패키지 )

answerCall이 정상적으로 수행되면 caller, callee 양측은 `CONNECTED_EVENT`를 받습니다. 수신 거절은 [leave()](https://docs.omnitalk.io/api-reference#leave) API를 이용하시면 됩니다.

```kotlin
override fun onEvent(eventName: OmniEvent, message: Any) {
    when(eventName) {
        OmniEvent.RINGING_EVENT -> {
            sdk.answerCall(localView = localView, remoteView = remoteView)
        }
        OmniEvent.CONNECTED_EVENT -> {
            ...
        }            
    }
}
```

## 영상 회의

### 1. 옴니톡 객체 생성

[콘솔](https://omnitalk.io/console/service/service-id)에서 발급받은 Service ID와 Service KEY로 Omnitalk 객체를 생성합니다.

```kotlin
import io.omnitalk.sdk.Omnitalk

Omnitalk.sdkInit(
    serviceId = "YOUR_SERVICE_ID",
    serviceKey = "YOUR_SERVICE_KEY",
    applicationContext = applicationContext
)

val sdk = Omnitalk.getInstance()
```

### 2. 세션 생성

인수로 전달한 userId로 세션을 생성하게 됩니다. userId는 Optional이며, null일 경우 서버에서 임의의 userId를 생성합니다.

```kotlin
val createSessionResult = sdk.createSession(userId = "alice")
```

### 3. 룸 생성

영상 회의를 위한 룸을 생성합니다. roomType을 제외한 나머지 파라미터는 Optional 입니다.

```kotlin
import io.omnitalk.sdk.types.PublicTypes

val createRoomResult = sdk.createRoom(
    roomType = PublicTypes.VIDEOROOM_TYPE.videoroom,
    subject = "subject",
    secret = "secret",
    startDate = null,
    endDate = null
)
```

### 4. 룸 참여

룸에 참여하게 되면 음성과 채팅 메시지를 주고 받을 수 있는 상태가 됩니다. roomId을 제외한 나머지 파라미터는 Optional 입니다.

```kotlin
val roomId = createRoomResult.roomId
sdk.joinRoom(roomId = roomId, secret = "secret", userName = "userName")
```

### 5. 방송 시작

로컬의 영상을 송출합니다. publish API는 roomType `VIDEO_ROOM` 또는 `WEBINAR` 에서만 필요한 기능입니다.

영상을 재생하기 위해서 파라미터에 영상을 출력할 `SurfaceViewRenderer` 객체를 전달합니다. 해당 객체를 생성하기 위해서 WebRTC 모듈을 import 해야 합니다. ( Omnitalk SDK 설치에 포함된 패키지 )

```kotlin
import org.webrtc.SurfaceViewRenderer

val localView = findViewById<SurfaceViewRenderer>(R.id.localView)
sdk.publish(localView = localView)
```

### 6. 방송 구독

구독하고자는 방송의 session을 인수로 전달하면 해당 방송을 구독할 수 있습니다. 상대방 영상을 재생하기 위해서 파라미터에 영상을 출력할 `SurfaceViewRenderer` 객체를 전달합니다. 해당 객체를 생성하기 위해서 WebRTC 모듈을 import 해야 합니다. ( Omnitalk SDK 설치에 포함된 패키지 )

```kotlin
import org.webrtc.SurfaceViewRenderer

val remoteView = findViewById<SurfaceViewRenderer>(R.id.remoteView)
sdk.subscribe(publisherSession = publisherSession, remoteView = remoteView)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.omnitalk.io/android/quick-start.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
