Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions guides/HKSV3Protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ The second way has a catch, and it cost me a few days: **the timestamps have to

How fast you hand the buffered stretch over is yours to choose. At twice the stream's speed the viewer sees a fast-forward. At fifty times it sees nothing but a view that starts at once.

**In HAP-NodeJS** every running tier keeps its video back to the latest keyframe. A view opens from that keyframe, stamped past everything already sent, at up to fifty times the stream's speed, and a plugin is never asked to make a keyframe.
**In HAP-NodeJS** every running tier keeps its video back to the latest keyframe. A view opens from that keyframe, stamped past everything already sent, at up to fifty times the stream's speed, and a plugin is never asked to make a keyframe. A recording's lead-up is the one exception: no view opens from it, and a view waits for the first live keyframe instead.

#### When A Packet Is Lost

Expand Down Expand Up @@ -636,6 +636,9 @@ That time is **not required** to be the moment the video was taken. A recording
- A hub that timed out asked once more, 5 seconds after its close, while Motion Detected was still raised. After a second timeout it didn't ask again, and where motion had already cleared it didn't ask a second time at all.
- A recording that starts late carries no video from before the motion. The hub judged it as usual, and its clip showed in the Home app's timeline and played.

**In HAP-NodeJS** a camera that can't keep its stream open states the recording mode `'onDemand'`. The library asks for its stream at the hub's open, and the recording
opens at the stream's first decodable keyframe. It carries the lead-up the plugin's stream brings, and nothing from before the open where the stream brings none.

One recording was refused outright. Its samples had been nudged by a tick to keep their decode times apart, which left some composition offsets below zero, and more than one hub closed it at once with the reason UNEXPECTED_FAILURE. The same picture and sound, cut cleanly by duration, was taken. I didn't work out which detail the hubs objected to. Give every sample its true time.

#### The Verdicts
Expand Down Expand Up @@ -715,7 +718,7 @@ Each of these is answered 201.
- A segment may be the very bytes the camera served the hub: a `prft`, a `moof` and an `mdat`. Segments that open with `styp` and `sidx` boxes are accepted as well.
- **A camera can upload while the hub is still pulling.** Send the playlists and the initialization segment at the Start, each segment as it's produced, and the closing playlist after the Stop. There's no need to hold a clip until it's whole.

**In HAP-NodeJS** a clip's media playlist states one target duration, in its opening form and its closing form: the longest segment the camera's recording can cut, which is its prebuffer plus ten seconds, 15 at the default.
**In HAP-NodeJS** a clip's media playlist states one target duration, in its opening form and its closing form: the longest segment the camera's recording can cut, which is its prebuffer plus ten seconds: 15 at the default prebuffer, and 10 for a camera that records on demand.

The playlists are in appendix A.

Expand Down
65 changes: 38 additions & 27 deletions guides/UpgradingCameraPlugins.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/accessories/ModernCamera_example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* removing `persist` lets the camera be added afresh.
*
* It leaves out what a camera adds only where it needs it: talkback, a doorbell, a second sensor, motion zones, shared sound, a tier marked `keepRunning`,
* and `strict`.
* recording on demand and `strict`.
*/
import type { CameraSnapshotRequest, TierStream } from '../index.js'

Expand Down
13 changes: 9 additions & 4 deletions src/lib/Accessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ import { HAPConnectionEvent } from './util/eventedhttp.js'
import { HapStatusError } from './util/hapStatusError.js'
import { once } from './util/once.js'
import { callDelegate } from './util/promise-utils.js'
import { extractHAPStatusFromError, formatOutgoingCharacteristicValue } from './util/request-util.js'
import {
CHARACTERISTIC_TIMEOUT_AFTER_WARNING,
CHARACTERISTIC_TIMEOUT_WARNING,
extractHAPStatusFromError,
formatOutgoingCharacteristicValue,
} from './util/request-util.js'
import * as uuid from './util/uuid.js'
import { toShortForm } from './util/uuid.js'

Expand Down Expand Up @@ -441,9 +446,9 @@ function loggableValue(characteristic: Characteristic, value: unknown): unknown
*/
export class Accessory extends EventEmitter {
// Timeout in milliseconds until a characteristic warning is issued
private static readonly TIMEOUT_WARNING = 3000
private static readonly TIMEOUT_WARNING = CHARACTERISTIC_TIMEOUT_WARNING
// Timeout in milliseconds after `TIMEOUT_WARNING` until the operation on the characteristic is considered timed out.
private static readonly TIMEOUT_AFTER_WARNING = 6000
private static readonly TIMEOUT_AFTER_WARNING = CHARACTERISTIC_TIMEOUT_AFTER_WARNING

// NOTICE: when adding/changing properties, remember to possibly adjust the serialize/deserialize functions
aid: Nullable<number> = null // assigned by us in assignIDs() or by a Bridge
Expand Down Expand Up @@ -1909,7 +1914,7 @@ export class Accessory extends EventEmitter {
let timeout: NodeJS.Timeout | undefined = setTimeout(() => {
warnMissing(true)

// after a total of 10s we no longer wait for a request to appear and just return status code timeout
// after a total of TIMEOUT_WARNING plus TIMEOUT_AFTER_WARNING we no longer wait for a request to appear and just return status code timeout
timeout = setTimeout(() => {
timeout = undefined

Expand Down
22 changes: 22 additions & 0 deletions src/lib/Advertiser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,28 @@ describe('CiaoAdvertiser destroy', () => {
expect(destroyService).toHaveBeenCalledTimes(1)
expect(shutdown).toHaveBeenCalledTimes(1)
})

it('should release the shared responder when the destroy of its service rejects, and reject its own destroy with that failure', async () => {
const advertiser = new CiaoAdvertiser(accessoryInfo('AA:BB:CC:DD:EE:04', 'Destroy Test Four'))
const service = (advertiser as any).advertisedService
const failure = new Error('The service could not send its goodbye.')
const destroyService = vi.spyOn(service, 'destroy').mockRejectedValue(failure)
const shutdown = vi.spyOn((advertiser as any).responder, 'shutdown')

try {
await expect(advertiser.destroy()).rejects.toBe(failure)

expect(shutdown).toHaveBeenCalledTimes(1)

await expect(advertiser.destroy()).rejects.toBe(failure)

expect(destroyService).toHaveBeenCalledTimes(1)
expect(shutdown).toHaveBeenCalledTimes(1)
} finally {
destroyService.mockRestore()
await service.destroy()
}
})
})

describe('BonjourHAPAdvertiser', () => {
Expand Down
10 changes: 7 additions & 3 deletions src/lib/Advertiser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,13 @@ export class CiaoAdvertiser extends EventEmitter implements Advertiser {
private async destroyService(): Promise<void> {
// The responder is shared with every other advertiser created with the same options, and its shutdown()
// only does something for the last one of them. So the service must send its goodbye on its own first.
await this.advertisedService.destroy()
await this.responder.shutdown()
this.removeAllListeners()
// A goodbye that fails still releases this advertiser's hold on the responder, and the failure still rejects the destroy.
try {
await this.advertisedService.destroy()
} finally {
await this.responder.shutdown()
this.removeAllListeners()
}
}

static createTxt(accessoryInfo: AccessoryInfo, setupHash: string): ServiceTxt {
Expand Down
38 changes: 38 additions & 0 deletions src/lib/Characteristic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3227,6 +3227,44 @@ describe('transient characteristics', () => {
await expect(characteristic.handleGetRequest(connection)).resolves.toBe('')
})

// An unpaired writer is one a server in insecure mode lets through: alive until its close, and never authenticated.
it.each([
{
before: (): void => undefined,
held: ['AQEB', '', 1],
writer: 'that is alive',
},
{
before: (writer: HAPConnection): void => {
writer.emit(HAPConnectionEvent.CLOSED)
},
held: ['', '', 0],
writer: 'that closed before its write',
},
])('holds a write\'s answer for an unpaired writer $writer as for a paired one, for its read alone and until its close', async ({ before, held }) => {
const characteristic = createTransientCharacteristic()
const unpaired = buildEmitterConnection('IPv6', { paired: false })
const other = buildEmitterConnection()
characteristic.onSet(() => 'AQEB')

before(unpaired)
await expect(characteristic.handleSetRequest('AQEC', unpaired)).resolves.toBe('AQEB')

const kept = [
await characteristic.handleGetRequest(unpaired),
await characteristic.handleGetRequest(other),
unpaired.listenerCount(HAPConnectionEvent.CLOSED),
]

unpaired.emit(HAPConnectionEvent.CLOSED)

expect([
kept,
await characteristic.handleGetRequest(unpaired),
unpaired.listenerCount(HAPConnectionEvent.CLOSED),
]).toEqual([held, '', 0])
})

it('serves a held answer ahead of an onGet handler, which the writer\'s read does not call and another connection\'s read does', async () => {
const characteristic = createTransientCharacteristic()
const other = buildEmitterConnection()
Expand Down
6 changes: 3 additions & 3 deletions src/lib/Characteristic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2991,9 +2991,9 @@ export class Characteristic extends EventEmitter {
return
}

// A connection that no longer reads as authenticated is closing or closed, and a closed one would never release an answer, because its close
// listeners have run and been removed: it holds nothing.
if (!connection.isAuthenticated()) {
// A connection that is not alive is closing or closed, and a closed one would never release an answer, because its close listeners have run and been
// removed: it holds nothing.
if (!connection.isAlive()) {
this.releaseTransientAnswers(connection)
return
}
Expand Down
Loading
Loading