Hướng dẫn về thiết bị máy ảnh cho Android

Loại thiết bị Camera được triển khai bằng 2 đặc điểm: PushAvStreamTransport, xử lý việc truyền luồng âm thanh và video bằng các giao thức dựa trên tính năng truyền dữ liệu, và WebRtcLiveView, cung cấp khả năng kiểm soát luồng phát trực tiếp và tính năng phản hồi bằng giọng nói. Loại thiết bị Chuông cửa (đối với những cách triển khai có chức năng camera) cũng sử dụng các đặc điểm này.

Loại thiết bị Home API Đặc điểm Ứng dụng mẫu Swift Trường hợp sử dụng

Máy ảnh

GoogleCameraDeviceType

home.matter.6006.types.0158

Thiết bị chụp ảnh tĩnh hoặc quay video. Camera có thể có tính năng phát trực tiếp, nói chuyện hai chiều hoặc phát hiện sự kiện.

Đặc điểm bắt buộc
     google PushAvStreamTransportTrait
     google WebRtcLiveViewTrait

Camera

Chuông cửa

GoogleDoorbellDeviceType

home.matter.6006.types.0113

Một thiết bị được kích hoạt bằng nút bên ngoài cửa, tạo ra tín hiệu âm thanh và/hoặc hình ảnh, dùng để yêu cầu sự chú ý của một người ở phía bên kia cửa. Chuông cửa có thể có tính năng phát trực tiếp, đàm thoại hai chiều hoặc phát hiện sự kiện.

Đặc điểm bắt buộc
     google PushAvStreamTransportTrait
     google WebRtcLiveViewTrait

Chuông cửa

Bắt đầu phát trực tiếp

Để bắt đầu phát trực tiếp, hãy gửi chuỗi Giao thức mô tả phiên (SDP) đến phương thức startLiveView() của đặc điểm WebRtcLiveView. Phương thức này sẽ trả về một WebRtcLiveViewTrait.StartLiveViewCommand.Response chứa 3 giá trị:

  • SDP cho phiên.
  • Thời lượng phiên tính bằng giây.
  • Mã phiên, có thể dùng để kéo dài hoặc kết thúc phiên.
suspend fun getWebRtcLiveViewTrait(cameraDevice, cameraDeviceType) {
 return cameraDevice.type(cameraDeviceType).trait(WebRtcLiveView).first {
    it?.metadata?.sourceConnectivity?.connectivityState == ConnectivityState.ONLINE
  }

}

// Start the live view
suspend fun startCameraStream(trait: WebRtcLiveView,offerSdp: String) {
  val response = trait.startLiveView(offerSdp)
  // Response contains three fields (see below)
  return response
}
  ...

// This is used to manage the WebRTC connection
val peerConnection: RTCPeerConnection = ...

   ...

val startResponse = startCameraStream(sdp)
val answerSdp = startResponse?.answerSdp
val sessionDuration = startResponse?.liveSessionDurationSeconds
val mediaSessionId = startResponse?.mediaSessionId

peerConnection.setRemoteDescription(SessionDescription.Type.ANSWER,
                                    answerSdp)

Kéo dài thời gian phát trực tiếp

Sự kiện phát trực tiếp có thời lượng đặt sẵn và sẽ hết hạn sau thời lượng đó. Để kéo dài thời lượng của một luồng đang hoạt động, hãy đưa ra yêu cầu gia hạn bằng phương thức WebRtcLiveView.extendLiveView():

// Assuming camera stream has just been started
suspend fun scheduleExtension(trait: WebRtcLiveView, mediaSessionId: String, liveSessionDurationSeconds: UShort ) {
  delay(liveSessionDurationSeconds - BUFFER_SECONDS * 1000)
  val response = trait.extendLiveView(mediaSessionId)
  // returns how long the session will be live for
  return response.liveSessionDurationSeconds
}

Bật và tắt tính năng ghi lại

Để bật tính năng ghi hình của camera, hãy truyền TransportStatusEnum.Active đến phương thức setTransportStatus() của đặc điểm PushAvStreamTransport. Để tắt tính năng ghi, hãy truyền TransportStatusEnum.Inactive. Trong ví dụ sau, chúng ta sẽ gói các lệnh gọi này trong một lệnh gọi duy nhất sử dụng Boolean để bật/tắt khả năng ghi:

// Start or stop recording for all connections.
suspend fun setCameraRecording(isOn: Boolean) {
  val pushAvStreamTransport = getPushAvStreamTransport
  if(isOn) {
    pushAvStreamTransport.setTransportStatus(TransportStatusEnum.Active)
  } else {
    pushAvStreamTransport.setTransportStatus(TransportStatusEnum.Inactive)
  }
}

Kiểm tra xem bạn có bật tính năng ghi hình hay không

Để xác định xem camera có được bật tính năng ghi hình hay không, hãy kiểm tra xem có kết nối nào đang hoạt động hay không. Ví dụ sau đây xác định 2 hàm để thực hiện việc này:

// Get the on/off state
suspend fun onOffState(cameraDevice: HomeDevice, cameraDeviceType) {
  // Query the device for pushAvStreamTransport
  val pushAvTrait = getPushAvStreamTransport()
  return pushAvTrait.recordModeActive()
}

// Check to see if the camera's recording capability is enabled
fun PushAvStreamTransport.recordModeActive(): Boolean {
  return currentConnections?.any { it.transportStatus == TransportStatusEnum.Active } ?: false
}

Một cách khác để kiểm tra là sử dụng hàm findTransport() với một vị từ:

// Fetch the current connections
suspend fun queryRecordModeState(cameraDevice: HomeDevice, cameraDeviceType) {
  val pushAvStreamTransport = getPushAvStreamTransport()
  return pushAvStreamTransport.findTransport().let {
      it.transportConfigurations.any { it.transportStatus == TransportStatusEnum.Active
    }
}

Bắt đầu và dừng TalkBack

Để bắt đầu talkback, hãy gọi phương thức startTalkback() của đặc điểm WebRtcLiveView. Để dừng, hãy sử dụng stopTalkback().

// Make sure camera stream is on
suspend fun setTalkback(isOn: Boolean, trait: WebRtcLiveView, mediaSessionId: String) {
  if(isOn) {
    trait.startTalkback(mediaSessionId)
  } else {
    trait.stopTalkback(mediaSessionId)
  }
}