# Pre-requisite

해당 페이지에서는 Omnitalk SDK를 사용하기 위해서 공통적으로 필요한 내용에 대해서 설명합니다.&#x20;

## 1. SDK 객체 초기화

[콘솔](https://omnitalk.io/console/service/service-id)에서 발급받은 Service Id와 Service Key로 SDK를 초기화 합니다. 해당 정보는 노출되지 않도록 주의하여야 합니다. 초기화된 SDK 객체는 이후 모든 메서드 호출에 사용됩니다.&#x20;

Omnitalk SDK는 싱글톤 패턴으로 제공됩니다. 아래 방법으로 SDK 초기화 및 객체를 얻을 수 있습니다.

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

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

val sdk = Omnitalk.getInstance()
```

## 2. 이벤트 리스너 등록

sdk.setOnEventListener 를 호출하여 이벤트 리스너를 등록 합니다. onEvent와 onClose 함수를 override하여 이벤트 메세지를 수신할 수 있습니다. 이벤트 메세지 내용은 [Event Message](/commons/event-message.md)를 참조 바랍니다.

이벤트 메세지는 Omnitalk SDK에서 제공하는 타입으로 캐스팅하여 사용할 수 있습니다. 다음은 onEvent 이벤트 처리 예시입니다.&#x20;

```kotlin
sdk.setOnEventListener(object : OmniEventListener 
    override fun onEvent(eventName: OmniEvent, message: Any) {
        when(eventName) {
            OmniEvent.LEAVE_EVENT -> {
                val leaveMsg = message as EventLeave
                ...
            }
            ...
        }
    }
    override fun onClose() {
        ...
    }
})
```

## OLD. 방송 개시

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

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

publish 성공시 화면에 영상이 렌더링되기 때문에 예제에서는 `CoroutineScope(Dispatchers.Main)` 에서 코드를 실행 합니다.

```kotlin
import org.webrtc.SurfaceViewRenderer

CoroutineScope(Dispatchers.Main).launch {
    try {
        val localView = findViewById<SurfaceViewRenderer>(R.id.localVideo)
        sdk.publish(localView=localView)
    } catch (err: Exception) {
        ...
    }
}
```

## OLD. 방송 중인 참여자 조회 및 시청

subscribe는 roomType `VIDEO_ROOM` 또는 `WEBINAR` 에서만 필요하며, 방에 참여하면 방송 중인 송출자의 목록을 조회하고 시청할 수 있습니다. 다른 송출자의 session은 송출자 목록을 조회하면 알 수 있습니다.

subscribe의 경우 송출자의 영상을 출력할 SurfaceViewRenderer 객체가 필요합니다. 해당 객체를 생성하기 위해서 org.webrtc 패키지를 import 해야 합니다. ( Omnitalk SDK 패키지에 포함된 모듈 )

subscribe 성공시 화면에 영상이 렌더링되기 때문에 예제에서는 `CoroutineScope(Dispatchers.Main)` 에서 코드를 실행 합니다. 만약 RecyclerView를 사용하려는 경우에는 `onBindViewHolder` 에서 `sdk.bind` 를 호출하여 영상을 바인딩 해주어야 합니다. 자세한 예시는 [Demo](https://github.com/omnistory-labs/omnitalk.android.sdk/tree/demo)를 참고 바랍니다.

```kotlin
import org.webrtc.SurfaceViewRenderer

CoroutineScope(Dispatchers.Main).launch {
    try {
        val remoteView = findViewById<SurfaceViewRenderer>(R.id.remoteView)
        sdk.subscribe(publisherSession="PUBLISHER_SESSION", remoteView=remoteView)
    } catch (err: Exception) {
        ...
    }
} 
```


---

# 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/developers-guide/pre-requisite.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.
