Skip to content

Commit

Permalink
taco: Fix Tapo 2-way audio (#1460)
Browse files Browse the repository at this point in the history
* tapo: update tapo intercom types

* tapo: update http-auth-fetch calls

* tapo: write extra crlf to 2way audio stream

* tapo: bump version
  • Loading branch information
xdissent committed May 4, 2024
1 parent 1abda3b commit 5ce1a2b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion plugins/tapo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@scrypted/tapo",
"version": "0.0.11",
"version": "0.0.12",
"description": "Tapo Camera Plugin for Scrypted",
"scripts": {
"scrypted-setup-project": "scrypted-setup-project",
Expand Down
4 changes: 2 additions & 2 deletions plugins/tapo/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SettingsMixinDeviceBase } from '@scrypted/common/src/settings-mixin';
import sdk, { DeviceProvider, DeviceState, FFmpegInput, Intercom, MediaObject, MixinProvider, ScryptedDeviceBase, ScryptedDeviceType, ScryptedInterface, ScryptedMimeTypes, Setting, Settings, SettingValue, VideoCamera } from '@scrypted/sdk';
import sdk, { DeviceProvider, FFmpegInput, Intercom, MediaObject, MixinProvider, ScryptedDeviceBase, ScryptedDeviceType, ScryptedInterface, ScryptedMimeTypes, Setting, Settings, SettingValue, VideoCamera, WritableDeviceState } from '@scrypted/sdk';
import { StorageSettings } from '@scrypted/sdk/storage-settings';
import { startRtpForwarderProcess } from '../../webrtc/src/rtp-forwarders';
import { MpegTSWriter, StreamTypePCMATapo } from './mpegts-writer';
Expand Down Expand Up @@ -97,7 +97,7 @@ class TapoIntercom extends ScryptedDeviceBase implements MixinProvider {
]
}

async getMixin(mixinDevice: any, mixinDeviceInterfaces: ScryptedInterface[], mixinDeviceState: DeviceState): Promise<any> {
async getMixin(mixinDevice: any, mixinDeviceInterfaces: ScryptedInterface[], mixinDeviceState: WritableDeviceState): Promise<any> {
return new TapoIntercomMixin({
mixinProviderNativeId: this.nativeId,
group: 'Tapo Two Way Audio',
Expand Down
15 changes: 8 additions & 7 deletions plugins/tapo/src/tapo-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { readLine } from '@scrypted/common/src/read-stream';
import { parseHeaders, readBody, readMessage, writeMessage } from '@scrypted/common/src/rtsp-server';
import crypto from 'crypto';
import { Duplex, PassThrough, Writable } from 'stream';
import { BufferParser, StreamParser } from '../../../server/src/http-fetch-helpers';
import { digestAuthHeader } from './digest-auth';

export function getTapoAdminPassword(cloudPassword: string, useSHA256: boolean) {
Expand Down Expand Up @@ -32,17 +31,17 @@ export class TapoAPI {
credential: undefined,
url: url,
ignoreStatusCode: true,
}, {
method: 'POST',
headers: {
'Content-Type': 'multipart/mixed; boundary=--client-stream-boundary--',
},
}, BufferParser);
responseType: 'buffer',
});

if (response.statusCode !== 401)
throw new Error('Expected 401 status code for two way audio init')

const wwwAuthenticate = response.headers['www-authenticate'];
const wwwAuthenticate = response.headers.get('www-authenticate') ?? '';
const useSHA256 = wwwAuthenticate.includes('encrypt_type="3"');

const password = getTapoAdminPassword(options.cloudPassword, useSHA256);
Expand All @@ -52,16 +51,16 @@ export class TapoAPI {
const response2 = await authHttpFetch({
credential: undefined,
url: url,
}, {
method: 'POST',
headers: {
'Authorization': auth,
'Content-Type': 'multipart/mixed; boundary=--client-stream-boundary--',
},
}, StreamParser)
responseType: 'readable',
})

const tapo = new TapoAPI();
tapo.keyExchange = response2.headers['key-exchange'] as string;
tapo.keyExchange = response2.headers.get('key-exchange') ?? '';
tapo.stream = response2.body.socket;
tapo.stream.on('close', () => console.error('stream closed'));
// this.stream.on('data', data => console.log('data', data));
Expand Down Expand Up @@ -129,6 +128,7 @@ export class TapoAPI {
'X-If-Encrypt': '0',
'X-Session-Id': this.backchannelSessionId,
});
this.stream.write('\r\n');
});

this.stream.on('close', () => pt.destroy());
Expand All @@ -150,6 +150,7 @@ export class TapoAPI {
writeMessage(this.stream, undefined, Buffer.from(JSON.stringify(request)), {
'Content-Type': 'application/json',
});
this.stream.write('\r\n');

return deferred.promise;
}
Expand Down

0 comments on commit 5ce1a2b

Please sign in to comment.