Skip to content

Voice Intercom And Forwarding

Flow of implementing voice intercom/forwarding

Interface Overview

interface Name Functional Description
NET_SDK_StartVoiceCom Start voice intercom.
NET_SDK_StartVoiceCom_MR Turn on voice forwarding.
NET_SDK_VoiceComSendData Voice forwarding for real-time data delivery.
NET_SDK_InitAudioEncoder Initializing the audio encoder.
NET_SDK_EncodeAudioFrame Audio Encoding.
NET_SDK_ReleaseAudioEncoder Free up audio encoding resources.
NET_SDK_StopVoiceCom End voice intercom or voice forwarding.
Note

Detailed parameters of the interface can be found in the interface definition section

Process Description

---
title: Voice Intercom And Forwarding
---

flowchart TD
    A(Device SDK Initialization<br><strong>NET_SDK_Init</strong>)
    B(User Registers Device<br><strong>NET_SDK_Login</strong> or<br><strong>NET_SDK_LoginEx</strong>)
    C("Start Voice Intercom<br><strong>NET_SDK_StartVoiceCom</strong>")
    D("Start Voice Forwarding<br><strong>NET_SDK_StartVoiceCom_MR</strong>")

    subgraph S1 [Audio Data Encoding]
        C1(Initialize Audio Encoder<br><strong>NET_SDK_InitAudioEncoder</strong>)
        C2(Audio Encoding<br><strong>NET_SDK_EncodeAudioFrame</strong>)
        C3(Release Audio Encoding Resources<br><strong>NET_SDK_ReleaseAudioEncoder</strong>)
        C1 --> C2 --> C3
    end

    E("Send Audio Data<br><strong>NET_SDK_VoiceComSendData</strong>")
    style S1 fill:#e7b13387,stroke:#f66,stroke-width:2px,stroke-dasharray: 5
    X("Stop voice intercom or voice forwarding <br><strong>NET_SDK_StopVoiceCom</strong>")
    Y(Log out of device <br><strong>NET_SDK_Logout</strong>)
    Z(Release SDK resources <br><strong>NET_SDK_Cleanup</strong>)

    A --> B --> C ----> X --> Y --> Z
    B ---> D --> S1 --> E ---> X
  • The voice intercom function enables the sending and receiving of audio between the PC and the device. After successfully registering the device call NET_SDK_StartVoiceCom interface is completed, while in the interface the user can set the callback function to get the data sent by the current device or collected by the PC (after the callback encoding or PCM data as needed).

  • First call NET_SDK_StartVoiceCom_MR interface initiates voice forwarding with the device (at this point the connection to the device is established and waiting for data to be sent).

  • Prepare the data to be sent (which needs to be encoded), the encoding process is shown in the purple box in the figure above. The data source can be captured from the PC sound card or read from a file, but it needs to be compressed by the compression algorithm provided by our company.

  • After the encoding operation, we can get a fixed size and encoded data each time by calling NET_SDK_VoiceComSendData interface sends this data to the device.

  • When all forwarding operations are complete, call NET_SDK_StopVoiceCom interface ends the voice forwarding connection to the device.

Note

The audio data format received by the device is: 8000HZ sample rate, 16bit, mono.

Sample Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
#include "stdafx.h"
#include "DVR_NET_SDK.h"

using namespace std;

void VoiceCom()
{
    // Initial
    NET_SDK_Init();
    // Device information
    const std::string device_ip = "10.80.1.177";
    const DWORD decice_port = 6036;
    const std::string username = "admin";
    const std::string password = "123456";
    //  Login
    NET_SDK_DEVICEINFO device_info = {0};
    int userid = NET_SDK_Login(const_cast<char *>(device_ip.c_str()), decice_port, const_cast<char *>(username.c_str()), const_cast<char *>(password.c_str()), &device_info);

    if (userid > 0)
    {
        cout << "Login successfully: " << userid << endl;
    }
    else
    {
        cout << "Failed to login: " << userid << endl;
        return;
    }

    // Start voice communication
    LONG lChannel = 0;
    LONG vchandle = NET_SDK_StartVoiceCom(userid, FALSE, NULL, NULL, lChannel);

    if (vchandle == -1)
    {
        DWORD err1 = NET_SDK_GetLastError();
        cout << "Failed to start voice communication! error code: " << err1 << endl;
    }
    else
    {
        cout << "Start voice communication successfully!" << endl;
    }

    // Stop voice communication
    NET_SDK_StopVoiceCom(vchandle);
    // Logout
    NET_SDK_Logout(userid);
    // Clean up the data
    NET_SDK_Cleanup();
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <string>
#include "stdafx.h"
#include "DVR_NET_SDK.h"

using namespace std;

void VoiceForward()
{
    // Initial
    NET_SDK_Init();
    // Device information
    const std::string device_ip = "10.80.1.177";
    const DWORD decice_port = 6036;
    const std::string username = "admin";
    const std::string password = "123456";
    //  Login
    NET_SDK_DEVICEINFO device_info = {0};
    int userid = NET_SDK_Login(const_cast<char *>(device_ip.c_str()), decice_port, const_cast<char *>(username.c_str()), const_cast<char *>(password.c_str()), &device_info);

    if (userid > 0)
    {
        cout << "Login successfully: " << userid << endl;
    }
    else
    {
        cout << "Failed to login: " << userid << endl;
        return;
    }

    std::string fname = "./voice.pcm";

    LONG lChannel = -1;
    if (reinterpret_cast<CButton*>(GetDlgItem(IDC_CHECK_TALK_TO_CHANNEL))->GetCheck())
    {
        lChannel = m_comChannel.GetCurSel();
    }

    POINTERHANDLE handle = NET_SDK_StartVoiceCom_MR(m_userID, TRUE, nullptr, this, lChannel);
    if (handle == -1) {
        return;
    }

    FILE* hStreamFile = fopen(fname.c_str(), "rb+");
    if (NULL == hStreamFile)
    {
        return;
    }


    const int SAMPLE_RATE = 8000;   /* sample rate */
    const int CHANNELS = 1;         /* number of channels (i.e. mono, stereo...) */
    const int BITS_PER_SAMPLE = 16; /* Number of bits per sample of mono data */
    const size_t BUFFER_SIZE = 3200;  
    char buffer[BUFFER_SIZE];  
    size_t bytesRead;
    int sleepTime = 0;
    while ((bytesRead = fread(buffer, sizeof(char), BUFFER_SIZE, hStreamFile)) > 0) {
        NET_SDK_VoiceComSendData(handle, buffer, bytesRead);
        sleepTime = bytesRead *1000/ (SAMPLE_RATE* BITS_PER_SAMPLE* CHANNELS /8);
        Sleep(sleepTime);
    }

    fclose(hStreamFile);

    // Stop voice forwarding
    NET_SDK_StopVoiceCom(handle);
    // Logout
    NET_SDK_Logout(userid);
    // Clean up the data
    NET_SDK_Cleanup();
}
Error Code