ประเภทอุปกรณ์กล้องจะใช้งานโดยใช้ลักษณะ 2 อย่าง ได้แก่
PushAvStreamTransport
ซึ่งจัดการการรับส่งสตรีมเสียงและวิดีโอโดยใช้โปรโตคอลแบบพุช และ
WebRtcLiveView
ซึ่งช่วยให้ควบคุมไลฟ์สดและฟีเจอร์ TalkBack ได้
โปรดตรวจสอบการรองรับแอตทริบิวต์และคำสั่งของอุปกรณ์เสมอก่อนที่จะใช้ฟีเจอร์หรือพยายามอัปเดตแอตทริบิวต์ ดูข้อมูลเพิ่มเติมได้ที่ควบคุมอุปกรณ์ใน Android
| ประเภทอุปกรณ์ API สำหรับ Home | ลักษณะ | แอปตัวอย่าง Kotlin | กรณีการใช้งาน |
|---|---|---|---|
กล้อง
อุปกรณ์ที่ถ่ายภาพนิ่งหรือวิดีโอ กล้องอาจมีฟีเจอร์สตรีมแบบสดที่เข้าถึงได้ การพูดโต้ตอบแบบ 2 ทาง หรือเหตุการณ์ที่ตรวจพบ |
ลักษณะที่ต้องระบุ google PushAvStreamTransport google WebRtcLiveView |
กล้อง |
เริ่มไลฟ์สด
หากต้องการเริ่มไลฟ์สด ให้ส่งสตริง Session Description Protocol (SDP)
ไปยัง
WebRtcLiveView
ของลักษณะ
startLiveView() ซึ่งจะแสดงผล
WebRtcLiveViewTrait.StartLiveViewCommand.Response
ที่มีค่า 3 ค่า ได้แก่
- SDP สำหรับเซสชัน
- ระยะเวลาเซสชันเป็นวินาที
- รหัสเซสชัน ซึ่งอาจใช้เพื่อขยายหรือสิ้นสุดเซสชัน
suspend fun getWebRtcLiveViewTrait(cameraDevice: HomeDevice) { return cameraDevice.type(GoogleCameraDevice).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)
ขยายเวลาไลฟ์สด
ไลฟ์สดมีระยะเวลาที่กำหนดไว้ล่วงหน้าซึ่งจะหมดอายุหลังจากนั้น หากต้องการขยาย
ระยะเวลาของสตรีมที่ใช้งานอยู่ ให้ส่งคำขอขยายเวลาโดยใช้เมธอด
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 }
เริ่มและหยุด TalkBack
หากต้องการเริ่ม Talkback ให้เรียกใช้เมธอด startTalkback() ของลักษณะ WebRtcLiveView
หากต้องการหยุด ให้ใช้
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) } }
เปิดและปิดใช้ความสามารถในการบันทึก
หากต้องการเปิดใช้ความสามารถในการบันทึกของกล้อง ให้ส่ง
TransportStatusEnum.Active
ไปยังเมธอด
setTransportStatus()
ของลักษณะ
PushAvStreamTransport หากต้องการปิดใช้ความสามารถในการบันทึก ให้ส่งTransportStatusEnum.Inactive
ในตัวอย่างต่อไปนี้ เราจะรวมการเรียกเหล่านี้ไว้ในการเรียกครั้งเดียวที่ใช้
Booleanเพื่อสลับความสามารถในการบันทึก
// Start or stop recording for all connections. suspend fun setCameraRecording(trait: PushAvStreamTransport, isOn: Boolean) { if(isOn) { trait.setTransportStatus(TransportStatusEnum.Active) } else { trait.setTransportStatus(TransportStatusEnum.Inactive) } }
การเปิดหรือปิดใช้ความสามารถในการบันทึกของกล้องจะเหมือนกับการเปิดหรือปิดวิดีโอของกล้อง เมื่อวิดีโอของกล้องเปิดอยู่ กล้องจะบันทึก (เพื่อวัตถุประสงค์ของเหตุการณ์และคลิปที่เกี่ยวข้อง)
เมื่อปิดใช้ความสามารถในการบันทึก (วิดีโอจากกล้องปิดอยู่)
- กล้องอาจยังคงแสดงเป็นออนไลน์ตาม
connectivityStateของ ประเภทอุปกรณ์ - เข้าถึงไลฟ์สดไม่ได้ และกล้องไม่ตรวจพบเหตุการณ์ในระบบคลาวด์
ตรวจสอบว่าได้เปิดใช้ความสามารถในการบันทึกแล้ว
หากต้องการดูว่าเปิดใช้ความสามารถในการบันทึกของกล้องหรือไม่ ให้ตรวจสอบว่ามีการเชื่อมต่อที่ใช้งานอยู่หรือไม่ ตัวอย่างต่อไปนี้กำหนดฟังก์ชัน 2 รายการเพื่อดำเนินการนี้
// Get the on/off state suspend fun onOffState(pushAvStreamTransport: PushAvStreamTransport) { return pushAvStreamTransport .currentConnections?.any { it.transportStatus == TransportStatusEnum.Active } ?: false } // Check if the camera's recording capability is enabled fun PushAvStreamTransport.recordModeActive(): Boolean { return currentConnections?.any { it.transportStatus == TransportStatusEnum.Active } ?: false }
อีกวิธีในการตรวจสอบคือการใช้ฟังก์ชัน findTransport() กับเพรดิเคต
// Fetch the current connections suspend fun queryRecordModeState(trait: PushAvStreamTransport) { return trait.findTransport().let { it.transportConfigurations.any { it.transportStatus == TransportStatusEnum.Active } }
การตั้งค่าเสียง
คุณควบคุมการตั้งค่าเสียงของกล้องต่างๆ ได้ผ่าน Home API
เปิดหรือปิดไมโครโฟน
หากต้องการเปิดหรือปิดไมโครโฟนของอุปกรณ์ ให้อัปเดตแอตทริบิวต์
microphoneMuted
ของลักษณะ CameraAvStreamManagement โดยใช้ฟังก์ชัน Kotlin setMicrophoneMuted ในตัว ดังนี้
// Turn the device's microphone on or off suspend fun turnOffMicrophone(disableMicrophone: Boolean, trait: CameraAvStreamManagement) { trait.update { setMicrophoneMuted(disableMicrophone) } }
เปิดหรือปิดการบันทึกเสียง
หากต้องการเปิดหรือปิดการบันทึกเสียงสำหรับอุปกรณ์ ให้อัปเดตแอตทริบิวต์
recordingMicrophoneMuted
ของลักษณะCameraAvStreamManagementโดยใช้ฟังก์ชัน Kotlin setRecordingMicrophoneMuted ในตัว ดังนี้
// Turn audio recording on or off for the device suspend fun turnOffAudioRecording(disableAudioRecording: Boolean, trait: CameraAvStreamManagement) { trait.update { setRecordingMicrophoneMuted(disableAudioRecording) } }
ปรับระดับเสียงของลำโพง
หากต้องการปรับระดับเสียงของลำโพงสำหรับอุปกรณ์ ให้อัปเดตแอตทริบิวต์
speakerVolumeLevel
ของลักษณะ CameraAvStreamManagement โดยใช้ฟังก์ชัน Kotlin setSpeakerVolumeLevel ในตัว ดังนี้
// Adjust the camera speaker volume suspend fun adjustSpeakerVolume(volume: Int, trait: CameraAvStreamManagement) { trait.update { setSpeakerVolumeLevel(volume.toUbyte()) } }
การตั้งค่าอื่นๆ
การตั้งค่ากล้องอื่นๆ อีกมากมายสามารถควบคุมได้ผ่าน Home API
เปลี่ยนการวางแนวรูปภาพ
คุณหมุนการวางแนวของรูปภาพ (วิดีโอ) จากกล้องได้ วิดีโอจะหมุนได้ 180 องศาเท่านั้น
หากต้องการเปลี่ยนการวางแนวรูปภาพของกล้อง ให้อัปเดตแอตทริบิวต์
imageRotation
ของลักษณะ CameraAvStreamManagement โดยใช้ฟังก์ชัน setImageRotation Kotlin ในตัว ดังนี้
// Change the camera's image orientation val isRotated = false cameraAvStreamManagement.update { setImageRotation(if (isRotated) 180.toUShort() else 0.toUShort()) }
เปิดหรือปิดฟีเจอร์ภาพกลางคืน
หากต้องการเปิดหรือปิดการมองเห็นตอนกลางคืนสำหรับกล้อง ให้ใช้ TriStateAutoEnum
เพื่ออัปเดตแอตทริบิวต์
nightVision
ของลักษณะ CameraAvStreamManagement โดยใช้ฟังก์ชัน setNightVision Kotlin ในตัว
// Turn night vision on cameraAvStreamManagement.update { setNightVision(CameraAvStreamManagementTrait.TriStateAutoEnum.On) } // Turn night vision off CameraAvStreamManagement.update { setNightVision(CameraAvStreamManagementTrait.TriStateAutoEnum.Off) }
เปลี่ยนความสว่างของ LED แสดงสถานะ
หากต้องการเปลี่ยนความสว่างของ LED สถานะ ให้ใช้
ThreeLevelAutoEnum
เพื่ออัปเดตแอตทริบิวต์
statusLightBrightness
ของลักษณะ CameraAvStreamManagement โดยใช้ฟังก์ชัน setStatusLightBrightness Kotlin ในตัว
// Set the LED brightness to high cameraAvStreamManagement.update { setStatusLightBrightness(CameraAvStreamManagementTrait.ThreeLevelAutoEnum.High) } // Set the LED brightness to low cameraAvStreamManagement.update { setStatusLightBrightness(CameraAvStreamManagementTrait.ThreeLevelAutoEnum.Low) }
เปลี่ยนวิวพอร์ตของกล้อง
ช่องมองภาพของกล้องจะเหมือนกับฟีเจอร์ซูมและครอบตัดที่อธิบายไว้ในบทความการสนับสนุนซูมและปรับปรุงวิดีโอของกล้อง Nest
กำหนดวิวพอร์ตใน ViewportStruct ที่มีค่า 4 ค่า ซึ่งใช้เป็นพิกัดของวิวพอร์ต โดยพิกัดจะได้รับการกําหนดดังนี้
(x1,y1) -- (x2,y1) | | (x1,y2) -- (x2,y2)
การกำหนดค่าสำหรับ ViewportStruct ขึ้นอยู่กับ UI ของแอปและการติดตั้งใช้งานกล้อง
ในระดับพื้นฐานมากที่สุด หากต้องการตั้งค่า Viewport ของวิดีโอจากกล้อง ให้อัปเดตแอตทริบิวต์
viewport
ของลักษณะ CameraAvStreamManagement ด้วย ViewportStruct โดยใช้ฟังก์ชัน setViewport Kotlin ในตัว ดังนี้
cameraAvStreamManagement .update { setViewport( CameraAvStreamManagementTrait.ViewportStruct( x1 = horizontalRange.rangeStart.roundToInt().toUShort(), x2 = horizontalRange.rangeEnd.roundToInt().toUShort(), y1 = verticalRange.rangeStart.roundToInt().toUShort(), y2 = verticalRange.rangeEnd.roundToInt().toUShort(), ) ) }
ปรับความไวในการปลุกอุปกรณ์
ความไวในการปลุกของอุปกรณ์จะใช้เพื่อประหยัดแบตเตอรี่โดยการลด ช่วงที่อุปกรณ์ตรวจหากิจกรรมได้ และเพิ่มเวลาในการปลุก หลังจากตรวจพบกิจกรรมนั้น
ใน Home API คุณตั้งค่านี้ได้โดยใช้พร็อพเพอร์ตี้ motionSensitivity ของ
triggerOptions ใน transportOptions ของอุปกรณ์ ตัวเลือกเหล่านี้กำหนดไว้
ในPushAvStreamTransportลักษณะของอุปกรณ์แต่ละเครื่อง
คุณตั้งค่าความไวต่อการปลุกได้เฉพาะค่าต่อไปนี้
- 1 = ต่ำ
- 5 = ปานกลาง
- 10 = สูง
กระบวนการอัปเดตคือการค้นหาการกำหนดค่าการรับส่งข้อมูลสำหรับสตรีมการบันทึกที่ใช้งานอยู่โดยใช้คำสั่ง
findTransport
จากนั้นแก้ไขการกำหนดค่าด้วยค่าความไวใหม่โดยใช้คำสั่ง
modifyPushTransport
// Create a struct with the new wake-up sensitivity val toUpdate = TransportOptionsStruct( triggerOptions = TransportTriggerOptionsStruct( motionSensitivity = OptionalValue.present(wakeUpSensitivity.toUByte()) ) ) // Get the configurations for active connections val connections = pushAvStreamTransport.findTransport().transportConfigurations // Update all recording streams with the new transport options. for (connection in connections) { if (connection.transportOptions.getOrNull()?.streamUsage == StreamUsageEnum.Recording) { trait.modifyPushTransport( connectionId = connection.connectionId, transportOptions = toUpdate, ) } }
ปรับระยะเวลาสูงสุดของเหตุการณ์
ความยาวสูงสุดของเหตุการณ์คือระยะเวลาที่กล้องจะบันทึกคลิปสำหรับเหตุการณ์ คุณกำหนดค่านี้ได้ต่ออุปกรณ์ผ่าน Home API ให้มีความยาวเท่ากับที่กำหนดผ่าน GHA โดยมีช่วงเวลาเป็นวินาที
- 10 วินาที
- 15 วินาที
- 30 วินาที
- 60 วินาที (1 นาที)
- 120 วินาที (2 นาที)
- 180 วินาที (3 นาที)
ใน Home API คุณตั้งค่านี้ได้โดยใช้พร็อพเพอร์ตี้ motionTimeControl ของ
triggerOptions ใน transportOptions ของอุปกรณ์ ตัวเลือกเหล่านี้กำหนดไว้
ในPushAvStreamTransportลักษณะของอุปกรณ์แต่ละเครื่อง
กระบวนการอัปเดตคือการค้นหาการกำหนดค่าการรับส่งสำหรับสตรีมการบันทึกที่ใช้งานอยู่โดยใช้คำสั่ง
findTransport
จากนั้นแก้ไขการกำหนดค่าด้วยค่าความยาวของเหตุการณ์ใหม่โดยใช้คำสั่ง
modifyPushTransport
// Create a struct with the new max event length // where maxDuration is the length in seconds val toUpdate = TransportOptionsStruct( triggerOptions = TransportTriggerOptionsStruct( motionTimeControl = OptionalValue.present( TransportMotionTriggerTimeControlStruct(maxDuration = it.toUInt()) ) ) ) // Get the configurations for active connections val connections = pushAvStreamTransport.findTransport().transportConfigurations // Update all recording streams with the new transport options. for (connection in connections) { if (connection.transportOptions.getOrNull()?.streamUsage == StreamUsageEnum.Recording) { trait.modifyPushTransport( connectionId = connection.connectionId, transportOptions = toUpdate, ) } }