Compare commits

..

4 Commits

12152 changed files with 419446 additions and 2333370 deletions
-69
View File
@@ -1,69 +0,0 @@
# Formatting rules for rtphone's own C++ sources (src/engine, src/libs/ice, test).
# Third-party code under src/libs/ is excluded via .clang-format-ignore.
---
Language: Cpp
BasedOnStyle: LLVM
Standard: c++20
# Indentation: 4 columns, spaces only.
IndentWidth: 4
TabWidth: 4
UseTab: Never
AccessModifierOffset: -4
NamespaceIndentation: None
IndentCaseLabels: false
ColumnLimit: 120
# Allman braces, as used throughout the codebase.
BreakBeforeBraces: Allman
PointerAlignment: Left
ReferenceAlignment: Left
DerivePointerAlignment: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortBlocksOnASingleLine: Empty
# Include order matters here (platform headers, windows.h, vendored libs),
# so never reorder them.
SortIncludes: Never
IndentPPDirectives: None
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 2
FixNamespaceComments: true
SpaceAfterTemplateKeyword: false
AlwaysBreakTemplateDeclarations: Yes
AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments
# Objective-C++ (src/libs/ice/ICEIosSupport.mm) — same rules as C++.
---
Language: ObjC
BasedOnStyle: LLVM
IndentWidth: 4
TabWidth: 4
UseTab: Never
AccessModifierOffset: -4
NamespaceIndentation: None
IndentCaseLabels: false
ColumnLimit: 120
BreakBeforeBraces: Allman
PointerAlignment: Left
ReferenceAlignment: Left
DerivePointerAlignment: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortBlocksOnASingleLine: Empty
SortIncludes: Never
IndentPPDirectives: None
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 2
FixNamespaceComments: true
AlignConsecutiveDeclarations: AcrossEmptyLinesAndComments
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
-2
View File
@@ -1,2 +0,0 @@
src/libs/**
!src/libs/ice/ICE*
-89
View File
@@ -1,89 +0,0 @@
# Build directories
build/
build_*/
out/
bin/
lib/
Release/
Debug/
x64/
x86/
# CMake generated files
CMakeCache.txt
CMakeFiles/
cmake_install.cmake
Makefile
*.cmake
!CMakeLists.txt
compile_commands.json
CTestTestfile.cmake
_deps/
# QtCreator
*.user
*.user.*
*.qmlproject.user
*.creator.user
.qmake.stash
# Visual Studio
*.suo
*.user
*.userosscache
*.sln.docstates
.vs/
*.vcxproj.user
*.ncb
*.sdf
*.opensdf
*.VC.db
*.VC.VC.opendb
ipch/
# Compiled object files
*.o
*.obj
*.ko
*.elf
# Precompiled headers
*.gch
*.pch
# Compiled libraries
# *.lib
# *.a
*.la
*.lo
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
# IDE caches
.cache/
.idea/
.vscode/
*.swp
*.swo
*~
# Downloaded archives
*.tar.gz
*.tar.bz2
*.tar.xz
*.zip
# OS specific
.DS_Store
Thumbs.db
# Logs and temp files
*.log
*.tmp
*.temp
+8
View File
@@ -0,0 +1,8 @@
build_exe:
script:
- mkdir -p build
- cd build
- git clone https://gitlab.com/dmytro.bogovych/libraries.git
- cmake -D LIB_PLATFORM=libraries ../src
- cmake --build .
-366
View File
@@ -1,366 +0,0 @@
# RTPhone Platform - Agent Guide
## Project Overview
RTPhone is a comprehensive real-time communication (RTC) platform developed by VoIP Objects (Sevana) that provides a complete software stack for building VoIP/SIP-based communication applications. It delivers production-ready voice communication capabilities with extensive codec support and cross-platform compatibility.
The project produces a static library (`librtphone.a`) that can be integrated into larger telephony and communication systems. It provides a JSON-based command interface for easy integration and control.
## Technology Stack
- **Language**: C++20
- **Build System**: CMake 3.20+
- **License**: Mozilla Public License Version 2.0 (see LICENSE_MPL.txt)
- **Primary Platforms**: Linux (x64, ARM/Raspberry Pi), Windows (32/64-bit), macOS, Android, iOS
## Project Structure
```
/home/anand/works/sevana/platform/rtphone/
├── src/ # Main source code
│ ├── CMakeLists.txt # Main CMake configuration
│ ├── engine/ # Core engine modules
│ │ ├── agent/ # JSON-based command interface
│ │ ├── audio/ # Cross-platform audio I/O handling
│ │ ├── endpoint/ # SIP user agent implementation
│ │ ├── helper/ # Utility functions (networking, logging, threading)
│ │ ├── media/ # Audio codec management and processing
│ │ └── engine_config.h # Compile-time configuration
│ └── libs/ # Third-party libraries
│ ├── resiprocate/ # SIP stack (git submodule)
│ ├── libsrtp/ # SRTP library (git submodule)
│ ├── libraries/ # Prebuilt platform libraries (git submodule)
│ ├── ice/ # ICE (Interactive Connectivity Establishment)
│ ├── jrtplib/ # RTP library
│ ├── opus/ # Opus codec
│ ├── webrtc/ # WebRTC components
│ ├── libg729/ # G.729 codec
│ ├── libgsm/ # GSM codec
│ ├── gsmhr/ # GSM HR codec
│ ├── g722/ # G.722 codec
│ ├── speexdsp/ # Speex DSP
│ ├── libevs/ # EVS codec (optional)
│ ├── opencore-amr/ # AMR codec (optional)
│ ├── oboe/ # Android low-latency audio
│ └── fmt/ # Format library
├── build_linux.py # Linux build script
├── build_android.py # Android build script
├── build_android.sh # Android build script (shell)
├── run_ci.sh # CI build script
└── README.txt # Human-readable README
```
## Build Instructions
### Prerequisites
- CMake 3.20+
- C++20 compatible compiler (GCC, Clang, MSVC)
- Python 3 (for build scripts)
- Ninja (recommended, used by build scripts)
- OpenSSL 1.1+ development libraries
- Platform-specific audio libraries
### Linux Build
```bash
python3 build_linux.py
```
This creates a `build_linux/` directory and outputs `librtphone.a`.
### Android Build
```bash
# Ensure ANDROID_NDK_HOME environment variable is set
export ANDROID_NDK_HOME=/path/to/android-ndk
python3 build_android.py
```
Or use the shell script:
```bash
./build_android.sh
```
This creates a `build_android/` directory with ARM64 libraries by default.
### Manual CMake Build
```bash
mkdir build && cd build
cmake ../src -G Ninja
cmake --build . -j$(nproc)
```
### Build Options
The following CMake options are available (defined in `src/CMakeLists.txt`):
- `USE_AMR_CODEC` (ON/OFF): Include AMR-NB/AMR-WB codec support. Default: ON
- `USE_EVS_CODEC` (ON/OFF): Include EVS codec support. Default: ON
- `USE_MUSL` (ON/OFF): Build with MUSL library. Default: OFF
## Module Architecture
### 1. Agent Module (`src/engine/agent/`)
Provides a JSON-based command interface for controlling the engine.
**Key Classes:**
- `AgentImpl`: Main agent implementation, processes JSON commands
- `Agent_AudioManager`: Manages audio devices and streams
**Interface Pattern:**
Commands are sent as JSON strings and responses are returned as JSON.
### 2. Endpoint Module (`src/engine/endpoint/`)
SIP user agent implementation based on reSIProcate.
**Key Classes:**
- `UserAgent` (in EP_Engine.h): Main SIP stack wrapper, handles registration, sessions, presence
- `Account` (EP_Account): SIP account management
- `Session` (EP_Session): Call/session management
- `EP_AudioProvider`: Audio data provider for sessions
- `EP_DataProvider`: Generic data provider interface
**Key Concepts:**
- Implements resiprocate handlers: `ClientRegistrationHandler`, `InviteSessionHandler`, etc.
- Supports multiple transport types: UDP, TCP, TLS
- ICE integration for NAT traversal
### 3. Media Module (`src/engine/media/`)
Audio codec management, RTP/RTCP handling, and media processing.
**Key Classes:**
- `MT::AudioCodec`: Codec management
- `MT::AudioStream`: Audio streaming
- `MT::AudioReceiver`: RTP packet receiving
- `MT::CodecList`: Codec negotiation
- `MT::SrtpHelper`: SRTP encryption
- `MT::Dtmf`: DTMF tone handling
- `MT::AmrCodec`: AMR codec wrapper
- `MT::EvsCodec`: EVS codec wrapper
### 4. Audio Module (`src/engine/audio/`)
Cross-platform audio I/O abstraction.
**Key Classes:**
- `Audio::Interface`: Audio interface abstraction
- `Audio::DevicePair`: Input/output device pair
- `Audio::Resampler`: Audio resampling
- `Audio::Mixer`: Multi-channel audio mixing
- `Audio::CoreAudio`: macOS/iOS implementation
- `Audio::DirectSound`: Windows DirectSound implementation
- `Audio::AndroidOboe`: Android Oboe implementation
- `Audio::Null`: Null/no-op implementation for testing
### 5. Helper Module (`src/engine/helper/`)
Utility classes and platform abstractions.
**Key Classes:**
- `HL::Types`: Type definitions and BiMap template
- `HL::Sync`: Threading primitives (Mutex, Event, etc.)
- `HL::ByteBuffer`: Binary data buffer
- `HL::VariantMap`: Key-value configuration storage
- `HL::Rtp`: RTP packet utilities
- `HL::IuUP`: Iu User Plane protocol (3G)
- `HL::NetworkSocket`: Network socket abstraction
- `HL::ThreadPool`: Thread pool implementation
## Code Style Guidelines
### Naming Conventions
1. **Classes**: PascalCase with module prefix
- `AgentImpl`, `UserAgent`, `MT::AudioCodec`, `HL::Sync`
2. **Files**: Prefix indicates module
- `Agent_*.cpp/h` - Agent module
- `EP_*.cpp/h` - Endpoint module
- `MT_*.cpp/h` - Media module
- `Audio_*.cpp/h` - Audio module
- `HL_*.cpp/h` - Helper module
3. **Member Variables**: Hungarian notation with `m` prefix
- `mAgentMutex`, `mSessionMap`, `mShutdown`
4. **Type Aliases**: `P` prefix for smart pointers
- `PAccount`, `PSession`, `PVariantMap`
### File Header Template
All source files must include the MPL license header:
```cpp
/* Copyright(C) 2007-YYYY VoIP objects (voipobjects.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
```
### Include Order
1. Corresponding header file (for .cpp files)
2. Project headers (using `"..."`)
3. Third-party library headers
4. Standard library headers
5. System headers
### Platform Abstraction
Use preprocessor defines for platform-specific code:
- `TARGET_WIN` - Windows
- `TARGET_LINUX` - Linux
- `TARGET_OSX` - macOS
- `TARGET_ANDROID` - Android
- `TARGET_MUSL` - MUSL libc
## Configuration
### Compile-Time Configuration (`src/engine/engine_config.h`)
Key configuration constants:
```cpp
// Audio settings
#define AUDIO_SAMPLE_WIDTH 16
#define AUDIO_CHANNELS 1
#define AUDIO_SAMPLERATE 48000
#define AUDIO_RESAMPLER_QUALITY 1
// SIP settings
#define UA_REGISTRATION_TIME 3600
#define UA_MEDIA_PORT_START 20000
#define UA_MEDIA_PORT_FINISH 30000
// Codec payload types
#define MT_AMRNB_PAYLOADTYPE 112
#define MT_AMRWB_PAYLOADTYPE 96
#define MT_EVS_PAYLOADTYPE 127
#define MT_OPUS_CODEC_PT 106
```
### Runtime Configuration
Configuration is passed via `VariantMap` objects to the UserAgent:
```cpp
enum
{
CONFIG_IPV4 = 0, // Use IP4
CONFIG_IPV6, // Use IP6
CONFIG_USERNAME, // Username
CONFIG_DOMAIN, // Domain
CONFIG_PASSWORD, // Password
CONFIG_STUNSERVER_NAME, // STUN server hostname
CONFIG_STUNSERVER_PORT, // STUN server port
CONFIG_TRANSPORT, // 0=all, 1=UDP, 2=TCP, 3=TLS
// ... see EP_Engine.h for full list
};
```
## Dependencies
### Git Submodules
The project uses Git submodules for some dependencies:
- `src/libs/resiprocate` - SIP stack (sevana branch)
- `src/libs/libsrtp` - SRTP library
- `src/libs/libraries` - Prebuilt platform libraries
To initialize:
```bash
git submodule update --init --recursive
```
### Third-Party Libraries
Prebuilt libraries are provided in `src/libs/libraries/`:
- OpenSSL 1.1 (crypto, SSL)
- Opus codec
- Opencore AMR (NB/WB)
- Boost (headers)
- PortAudio
- libevent2
## Testing
There is no dedicated test suite in the main project. Testing is typically done through:
1. Integration with the final application
2. The `run_ci.sh` script for build verification
3. Unit tests in individual library submodules (e.g., oboe)
### CI Build
```bash
./run_ci.sh
```
This configures with CMake and builds with make (2 parallel jobs).
## Security Considerations
1. **OpenSSL Integration**: Uses OpenSSL 1.1+ for TLS and certificate handling
2. **SRTP Support**: Media encryption via libsrtp
3. **Certificate Management**: Root certificates can be added at runtime via `addRootCert()`
4. **AMR/EVS Patents**: The project does NOT include patent licenses for AMR and EVS codecs. Users must acquire these independently.
## Integration Guide
### Basic Usage Pattern
1. Create `AgentImpl` instance
2. Send JSON commands via `command()` method
3. Poll for events via `waitForData()` and `read()`
4. Process JSON event responses
### Example Commands
- `config` - Configure the engine
- `start` - Start the engine
- `createAccount` - Create SIP account
- `startAccount` - Register account
- `createSession` - Create call session
- `startSession` - Make/accept call
- `waitForEvent` - Poll for events
## Development Workflow
1. **Making Changes**: Edit files in appropriate `src/engine/<module>/` directory
2. **Building**: Use `build_linux.py` for quick Linux builds
3. **Testing**: Integrate with test application or use existing CI
4. **Commits**: Follow existing commit message style (visible in git log)
## Common Tasks
### Adding a New Codec
1. Create `MT_<Codec>Codec.cpp/h` in `src/engine/media/`
2. Derive from `MT::Codec` base class
3. Register in `MT_CodecList.cpp`
4. Add to `src/CMakeLists.txt` if external library needed
### Adding Platform Audio Support
1. Create `Audio_<Platform>.cpp/h` in `src/engine/audio/`
2. Implement `Audio::Interface` methods
3. Add platform detection in `src/CMakeLists.txt`
4. Include in build conditionally
### Modifying Build Configuration
- Edit `src/CMakeLists.txt` for main build changes
- Edit `src/libs/libraries/platform_libs.cmake` for platform library paths
- Edit `src/engine/engine_config.h` for compile-time constants
## Notes
- The codebase uses C++20 features - ensure compiler compatibility
- Thread safety: Use `std::recursive_mutex` (e.g., `mAgentMutex`) for thread-safe access
- Memory management: Uses smart pointers (`std::shared_ptr`) extensively
- The JSON library is embedded in `src/libs/json/`
-79
View File
@@ -1,79 +0,0 @@
# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
A more detailed agent reference (style guide, JSON command list, integration notes, full module class listings) is in `AGENTS.md`. Read that for deeper context; this file only captures what is needed to be productive quickly.
## What this repo produces
A single C++20 static library, `librtphone.a`, that implements a SIP/RTP softphone stack (codecs, media transport, SIP user agent, cross-platform audio I/O). It is consumed by other applications via a JSON command interface (`AgentImpl`). There is no executable target in `src/` — only the library.
## Build and run
The Python scripts wipe and recreate their build directory each invocation, so they are clean configure+build runs, not incremental.
```bash
python3 build_linux.py # wipes build_linux/, configures with Ninja, outputs build_linux/librtphone.a
python3 build_android.py # needs ANDROID_NDK_HOME and VCPKG_ROOT; arm64-v8a, API 24
python3 build_windows.py # needs VCPKG_ROOT (defaults to C:\tools\vcpkg); VS 2022 x64
./run_ci.sh # Make-based CI build into ./build (used by Jenkins; pings Telegram on failure)
```
For incremental work, configure once and rebuild manually instead of re-running the wrapper:
```bash
mkdir -p build && cd build
cmake ../src -G Ninja -D OPUS_X86_MAY_HAVE_SSE4_1=ON
cmake --build . -j$(nproc)
```
CMake options (in `src/CMakeLists.txt`): `USE_AMR_CODEC` (default ON, force-OFF on Android), `USE_EVS_CODEC` (default ON), `USE_MUSL` (default OFF).
## Tests
There is no unit test suite for the library itself. The only first-party test is `test/rtp_decode/` — a standalone CMake project that links the library and exercises RTP decoding from a capture file. Build and run it via:
```bash
mkdir -p test/rtp_decode/build && cd test/rtp_decode/build
cmake .. && cmake --build . -j$(nproc)
./rtp_decode <args>
```
Note: `test/rtp_decode/CMakeLists.txt` does `add_subdirectory(../../src ...)`, so it rebuilds the whole library inside its own build tree. Don't expect to share artifacts with `build_linux/`.
## Submodules and external paths
The `src/libs/` directory mixes vendored sources with three git submodules: `resiprocate` (SIP stack, sevana branch), `libsrtp`, and `libraries` (prebuilt platform binaries — OpenSSL 1.1, Boost headers, etc.). After clone:
```bash
git submodule update --init --recursive
```
## Architecture: how a call flows through the modules
The five modules under `src/engine/` form a vertical stack. Understanding the flow matters more than the file list (which is in `AGENTS.md`):
1. **`agent/`** — Public API surface. Hosts apps create one `AgentImpl`, push JSON command strings into `command()`, and pull JSON event strings out via `waitForData()`/`read()`. This is the only thing external code should touch.
2. **`endpoint/`** — Wraps reSIProcate. `UserAgent` (in `EP_Engine.h`) owns the SIP transports, registrations, and session lifecycle; `EP_Account` and `EP_Session` are the SIP-side objects the agent manipulates. `EP_AudioProvider`/`EP_DataProvider` bridge SIP sessions to media streams.
3. **`media/`** (`MT::` namespace) — Codec registry (`MT_CodecList`), per-call audio pipeline (`MT_AudioStream`, `MT_AudioReceiver`), RTP send (`MT_NativeRtpSender`), SRTP (`MT_SrtpHelper`), DTMF (`MT_Dtmf`). Codec wrappers like `MT_AmrCodec`/`MT_EvsCodec` adapt vendored libraries in `src/libs/` to the `MT::Codec` base.
4. **`audio/`** (`Audio::` namespace) — Cross-platform device I/O. `Audio::Interface` is the abstraction; concrete implementations are picked at compile time: `Audio_DirectSound` (Windows), `Audio_CoreAudio` (macOS/iOS), `Audio_AndroidOboe` (Android, via `libs/oboe`), `Audio_Null` (testing). Also hosts mixing, resampling, WAV I/O, AEC integration.
5. **`helper/`** (`HL::` namespace) — Cross-cutting primitives used by every other module: `HL_Sync` (mutex/event), `HL_NetworkSocket`, `HL_Log`, `HL_VariantMap` (used as the runtime config bag passed to `UserAgent`), `HL_Rtp`, `HL_IuUP` (3G Iu-UP framing), `HL_ThreadPool`, `HL_ByteBuffer`.
ICE/STUN lives separately under `src/libs/ice/` (it is compiled directly into the library, not as a subproject) and is wired into the media path for NAT traversal.
Compile-time tunables — sample rate (48 kHz), buffer sizes, RTP/codec payload types, media port range — are all in `src/engine/engine_config.h`. Runtime configuration flows through `HL::VariantMap` keyed by the `CONFIG_*` enum in `EP_Engine.h`.
Per-stream call-quality metrics (RTT, jitter per RFC 3550 §A.8, packet-loss timeline, RFC 2833 DTMF events, network MOS) are collected in `MT::Statistics` / `MT::JitterStatistics` in `src/engine/media/MT_Statistics.{h,cpp}` and surfaced through the agent's JSON event stream.
## Conventions worth knowing before editing
- **File prefixes encode the module**: `Agent_*`, `EP_*`, `MT_*`, `Audio_*`, `HL_*`. Match this when adding files.
- **Members use `m` prefix** (`mAgentMutex`, `mSessionMap`); smart-pointer typedefs use `P` prefix (`PSession`, `PVariantMap`).
- **Platform code is gated by `TARGET_WIN` / `TARGET_LINUX` / `TARGET_OSX` / `TARGET_ANDROID` / `TARGET_MUSL`**, set in `src/CMakeLists.txt`. Don't sniff `_WIN32`/`__linux__` directly.
- **Every source file carries the MPL 2.0 header** (see top of any existing `.cpp`). New files need the same block.
- **Thread safety is via `std::recursive_mutex`** (e.g. `mAgentMutex` guards the agent's public surface). Memory uses `std::shared_ptr` extensively — prefer the existing `P*` typedefs over raw pointers.
- The codebase recently migrated to **C++20 and `std::chrono`**; avoid reintroducing older idioms or hand-rolled time math.
## Patent caveat
AMR-NB/AMR-WB and EVS sources are included but no patent licenses are bundled. If a change touches `MT_AmrCodec`/`MT_EvsCodec` or their build flags, keep in mind users are expected to license these codecs themselves — don't enable them by default in contexts where that hasn't been arranged.
-373
View File
@@ -1,373 +0,0 @@
Mozilla Public License Version 2.0
==================================
1. Definitions
--------------
1.1. "Contributor"
means each individual or legal entity that creates, contributes to
the creation of, or owns Covered Software.
1.2. "Contributor Version"
means the combination of the Contributions of others (if any) used
by a Contributor and that particular Contributor's Contribution.
1.3. "Contribution"
means Covered Software of a particular Contributor.
1.4. "Covered Software"
means Source Code Form to which the initial Contributor has attached
the notice in Exhibit A, the Executable Form of such Source Code
Form, and Modifications of such Source Code Form, in each case
including portions thereof.
1.5. "Incompatible With Secondary Licenses"
means
(a) that the initial Contributor has attached the notice described
in Exhibit B to the Covered Software; or
(b) that the Covered Software was made available under the terms of
version 1.1 or earlier of the License, but not also under the
terms of a Secondary License.
1.6. "Executable Form"
means any form of the work other than Source Code Form.
1.7. "Larger Work"
means a work that combines Covered Software with other material, in
a separate file or files, that is not Covered Software.
1.8. "License"
means this document.
1.9. "Licensable"
means having the right to grant, to the maximum extent possible,
whether at the time of the initial grant or subsequently, any and
all of the rights conveyed by this License.
1.10. "Modifications"
means any of the following:
(a) any file in Source Code Form that results from an addition to,
deletion from, or modification of the contents of Covered
Software; or
(b) any new file in Source Code Form that contains any Covered
Software.
1.11. "Patent Claims" of a Contributor
means any patent claim(s), including without limitation, method,
process, and apparatus claims, in any patent Licensable by such
Contributor that would be infringed, but for the grant of the
License, by the making, using, selling, offering for sale, having
made, import, or transfer of either its Contributions or its
Contributor Version.
1.12. "Secondary License"
means either the GNU General Public License, Version 2.0, the GNU
Lesser General Public License, Version 2.1, the GNU Affero General
Public License, Version 3.0, or any later versions of those
licenses.
1.13. "Source Code Form"
means the form of the work preferred for making modifications.
1.14. "You" (or "Your")
means an individual or a legal entity exercising rights under this
License. For legal entities, "You" includes any entity that
controls, is controlled by, or is under common control with You. For
purposes of this definition, "control" means (a) the power, direct
or indirect, to cause the direction or management of such entity,
whether by contract or otherwise, or (b) ownership of more than
fifty percent (50%) of the outstanding shares or beneficial
ownership of such entity.
2. License Grants and Conditions
--------------------------------
2.1. Grants
Each Contributor hereby grants You a world-wide, royalty-free,
non-exclusive license:
(a) under intellectual property rights (other than patent or trademark)
Licensable by such Contributor to use, reproduce, make available,
modify, display, perform, distribute, and otherwise exploit its
Contributions, either on an unmodified basis, with Modifications, or
as part of a Larger Work; and
(b) under Patent Claims of such Contributor to make, use, sell, offer
for sale, have made, import, and otherwise transfer either its
Contributions or its Contributor Version.
2.2. Effective Date
The licenses granted in Section 2.1 with respect to any Contribution
become effective for each Contribution on the date the Contributor first
distributes such Contribution.
2.3. Limitations on Grant Scope
The licenses granted in this Section 2 are the only rights granted under
this License. No additional rights or licenses will be implied from the
distribution or licensing of Covered Software under this License.
Notwithstanding Section 2.1(b) above, no patent license is granted by a
Contributor:
(a) for any code that a Contributor has removed from Covered Software;
or
(b) for infringements caused by: (i) Your and any other third party's
modifications of Covered Software, or (ii) the combination of its
Contributions with other software (except as part of its Contributor
Version); or
(c) under Patent Claims infringed by Covered Software in the absence of
its Contributions.
This License does not grant any rights in the trademarks, service marks,
or logos of any Contributor (except as may be necessary to comply with
the notice requirements in Section 3.4).
2.4. Subsequent Licenses
No Contributor makes additional grants as a result of Your choice to
distribute the Covered Software under a subsequent version of this
License (see Section 10.2) or under the terms of a Secondary License (if
permitted under the terms of Section 3.3).
2.5. Representation
Each Contributor represents that the Contributor believes its
Contributions are its original creation(s) or it has sufficient rights
to grant the rights to its Contributions conveyed by this License.
2.6. Fair Use
This License is not intended to limit any rights You have under
applicable copyright doctrines of fair use, fair dealing, or other
equivalents.
2.7. Conditions
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
in Section 2.1.
3. Responsibilities
-------------------
3.1. Distribution of Source Form
All distribution of Covered Software in Source Code Form, including any
Modifications that You create or to which You contribute, must be under
the terms of this License. You must inform recipients that the Source
Code Form of the Covered Software is governed by the terms of this
License, and how they can obtain a copy of this License. You may not
attempt to alter or restrict the recipients' rights in the Source Code
Form.
3.2. Distribution of Executable Form
If You distribute Covered Software in Executable Form then:
(a) such Covered Software must also be made available in Source Code
Form, as described in Section 3.1, and You must inform recipients of
the Executable Form how they can obtain a copy of such Source Code
Form by reasonable means in a timely manner, at a charge no more
than the cost of distribution to the recipient; and
(b) You may distribute such Executable Form under the terms of this
License, or sublicense it under different terms, provided that the
license for the Executable Form does not attempt to limit or alter
the recipients' rights in the Source Code Form under this License.
3.3. Distribution of a Larger Work
You may create and distribute a Larger Work under terms of Your choice,
provided that You also comply with the requirements of this License for
the Covered Software. If the Larger Work is a combination of Covered
Software with a work governed by one or more Secondary Licenses, and the
Covered Software is not Incompatible With Secondary Licenses, this
License permits You to additionally distribute such Covered Software
under the terms of such Secondary License(s), so that the recipient of
the Larger Work may, at their option, further distribute the Covered
Software under the terms of either this License or such Secondary
License(s).
3.4. Notices
You may not remove or alter the substance of any license notices
(including copyright notices, patent notices, disclaimers of warranty,
or limitations of liability) contained within the Source Code Form of
the Covered Software, except that You may alter any license notices to
the extent required to remedy known factual inaccuracies.
3.5. Application of Additional Terms
You may choose to offer, and to charge a fee for, warranty, support,
indemnity or liability obligations to one or more recipients of Covered
Software. However, You may do so only on Your own behalf, and not on
behalf of any Contributor. You must make it absolutely clear that any
such warranty, support, indemnity, or liability obligation is offered by
You alone, and You hereby agree to indemnify every Contributor for any
liability incurred by such Contributor as a result of warranty, support,
indemnity or liability terms You offer. You may include additional
disclaimers of warranty and limitations of liability specific to any
jurisdiction.
4. Inability to Comply Due to Statute or Regulation
---------------------------------------------------
If it is impossible for You to comply with any of the terms of this
License with respect to some or all of the Covered Software due to
statute, judicial order, or regulation then You must: (a) comply with
the terms of this License to the maximum extent possible; and (b)
describe the limitations and the code they affect. Such description must
be placed in a text file included with all distributions of the Covered
Software under this License. Except to the extent prohibited by statute
or regulation, such description must be sufficiently detailed for a
recipient of ordinary skill to be able to understand it.
5. Termination
--------------
5.1. The rights granted under this License will terminate automatically
if You fail to comply with any of its terms. However, if You become
compliant, then the rights granted under this License from a particular
Contributor are reinstated (a) provisionally, unless and until such
Contributor explicitly and finally terminates Your grants, and (b) on an
ongoing basis, if such Contributor fails to notify You of the
non-compliance by some reasonable means prior to 60 days after You have
come back into compliance. Moreover, Your grants from a particular
Contributor are reinstated on an ongoing basis if such Contributor
notifies You of the non-compliance by some reasonable means, this is the
first time You have received notice of non-compliance with this License
from such Contributor, and You become compliant prior to 30 days after
Your receipt of the notice.
5.2. If You initiate litigation against any entity by asserting a patent
infringement claim (excluding declaratory judgment actions,
counter-claims, and cross-claims) alleging that a Contributor Version
directly or indirectly infringes any patent, then the rights granted to
You by any and all Contributors for the Covered Software under Section
2.1 of this License shall terminate.
5.3. In the event of termination under Sections 5.1 or 5.2 above, all
end user license agreements (excluding distributors and resellers) which
have been validly granted by You or Your distributors under this License
prior to termination shall survive termination.
************************************************************************
* *
* 6. Disclaimer of Warranty *
* ------------------------- *
* *
* Covered Software is provided under this License on an "as is" *
* basis, without warranty of any kind, either expressed, implied, or *
* statutory, including, without limitation, warranties that the *
* Covered Software is free of defects, merchantable, fit for a *
* particular purpose or non-infringing. The entire risk as to the *
* quality and performance of the Covered Software is with You. *
* Should any Covered Software prove defective in any respect, You *
* (not any Contributor) assume the cost of any necessary servicing, *
* repair, or correction. This disclaimer of warranty constitutes an *
* essential part of this License. No use of any Covered Software is *
* authorized under this License except under this disclaimer. *
* *
************************************************************************
************************************************************************
* *
* 7. Limitation of Liability *
* -------------------------- *
* *
* Under no circumstances and under no legal theory, whether tort *
* (including negligence), contract, or otherwise, shall any *
* Contributor, or anyone who distributes Covered Software as *
* permitted above, be liable to You for any direct, indirect, *
* special, incidental, or consequential damages of any character *
* including, without limitation, damages for lost profits, loss of *
* goodwill, work stoppage, computer failure or malfunction, or any *
* and all other commercial damages or losses, even if such party *
* shall have been informed of the possibility of such damages. This *
* limitation of liability shall not apply to liability for death or *
* personal injury resulting from such party's negligence to the *
* extent applicable law prohibits such limitation. Some *
* jurisdictions do not allow the exclusion or limitation of *
* incidental or consequential damages, so this exclusion and *
* limitation may not apply to You. *
* *
************************************************************************
8. Litigation
-------------
Any litigation relating to this License may be brought only in the
courts of a jurisdiction where the defendant maintains its principal
place of business and such litigation shall be governed by laws of that
jurisdiction, without reference to its conflict-of-law provisions.
Nothing in this Section shall prevent a party's ability to bring
cross-claims or counter-claims.
9. Miscellaneous
----------------
This License represents the complete agreement concerning the subject
matter hereof. If any provision of this License is held to be
unenforceable, such provision shall be reformed only to the extent
necessary to make it enforceable. Any law or regulation which provides
that the language of a contract shall be construed against the drafter
shall not be used to construe this License against a Contributor.
10. Versions of the License
---------------------------
10.1. New Versions
Mozilla Foundation is the license steward. Except as provided in Section
10.3, no one other than the license steward has the right to modify or
publish new versions of this License. Each version will be given a
distinguishing version number.
10.2. Effect of New Versions
You may distribute the Covered Software under the terms of the version
of the License under which You originally received the Covered Software,
or under the terms of any subsequent version published by the license
steward.
10.3. Modified Versions
If you create software not governed by this License, and you want to
create a new license for such software, you may create and use a
modified version of this License if you rename the license and remove
any references to the name of the license steward (except to note that
such modified license differs from this License).
10.4. Distributing Source Code Form that is Incompatible With Secondary
Licenses
If You choose to distribute Source Code Form that is Incompatible With
Secondary Licenses under the terms of this version of the License, the
notice described in Exhibit B of this License must be attached.
Exhibit A - Source Code Form License Notice
-------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
If it is not possible or desirable to put the notice in a particular
file, then You may include the notice in a location (such as a LICENSE
file in a relevant directory) where a recipient would be likely to look
for such a notice.
You may add additional accurate notices of copyright ownership.
Exhibit B - "Incompatible With Secondary Licenses" Notice
---------------------------------------------------------
This Source Code Form is "Incompatible With Secondary Licenses", as
defined by the Mozilla Public License, v. 2.0.
-141
View File
@@ -1,141 +0,0 @@
# RTPhone Platform
RTPhone is a comprehensive real-time communication (RTC) platform that provides a complete software stack for building VoIP/SIP-based communication applications. Developed by VoIP Objects (Sevana), RTPhone delivers production-ready voice communication capabilities with extensive codec support and cross-platform compatibility.
## Overview
RTPhone serves as a static library (`librtphone.a`) that can be integrated into larger telephony and communication systems. It provides a JSON-based command interface for easy integration and control, making it suitable for building softphones, PBX systems, WebRTC gateways, and carrier-grade voice solutions.
## Key Features
### Audio Codec Support
RTPhone supports an extensive range of audio codecs:
**Standard Codecs:**
- G.711 (A-law/¼-law)
- G.722 (16kHz wideband)
- G.729
- GSM (Full Rate, Half Rate, Enhanced Full Rate)
- iLBC (20ms/30ms)
- ISAC (16kHz/32kHz)
**Advanced Codecs:**
- AMR-NB/AMR-WB (Adaptive Multi-Rate Narrowband/Wideband) - please be aware - there is no patents for AMR codecs usage included ! You should acquire them on your own.
- EVS (Enhanced Voice Services) - 3GPP's latest codec. Again - please be aware - there is no patents for EVS codec usage included ! You should acquire them on your own.
- Opus - Modern low-latency codec
- Speex (with acoustic echo cancellation)
**Codec Features:**
- Bandwidth-efficient and octet-aligned modes
- IuUP (Iu User Plane) protocol support for 3G networks
- Dynamic codec switching
- Packet loss concealment (PLC)
- Comfort noise generation (CNG)
### Network & Protocol Support
**SIP Features:**
- Full SIP 2.0 implementation via reSIProcate
- Multiple transport protocols (UDP, TCP, TLS)
- Registration, authentication, and session management
- SIP MESSAGE, presence, and REFER support
**Media Transport:**
- RTP/RTCP for media streaming
- SRTP for secure media
- ICE for NAT traversal with STUN/TURN support
- WebRTC integration components
- IPv4 and IPv6 support
### Cross-Platform Audio Support
- DirectSound/WMME (Windows)
- Core Audio (macOS/iOS)
- ALSA/PulseAudio (Linux)
- Oboe (Android) for low-latency audio
- PortAudio fallback support
### Audio Quality Features
- 48kHz sample rate support
- Acoustic Echo Cancellation (AEC)
- Audio resampling and format conversion
- Multi-channel audio mixing
- Perceptual Voice Quality Assessment (PVQA)
## Architecture
The platform is organized into several core modules:
- **Engine/Agent**: JSON-based command interface
- **Engine/Endpoint**: SIP user agent implementation
- **Engine/Media**: Audio codec management and processing
- **Engine/Audio**: Cross-platform audio I/O handling
- **Engine/Helper**: Utility functions (networking, logging, threading)
## Supported Platforms
- Linux (x64, ARM/Raspberry Pi)
- Windows (32/64-bit)
- macOS
- Android (with Oboe integration)
- iOS
## Building
RTPhone uses a CMake-based build system with cross-compilation support:
### Linux
```bash
python3 build_linux.py
```
### Android
```bash
python3 build_android.py
# or
./build_android.sh
```
### Dependencies
- CMake 3.10+
- OpenSSL 1.1+
- Boost libraries
- Platform-specific audio libraries
## Recent Updates
Recent development has focused on:
- AMR codec parsing and decoding improvements
- Octet-aligned mode fixes for AMR-WB
- RTP SSRC handling enhancements
- Build system optimizations
- Code modernization to C++20
## Use Cases
RTPhone is good for building:
- VoIP softphones and mobile applications
- PBX and telephony server systems
- RTP proxies
- Carrier-grade voice communication platforms
- 3GPP/IMS-compliant systems
## Security
RTPhone includes comprehensive security features:
- OpenSSL 1.1 integration for encryption
- TLS transport layer security
- SRTP media encryption
- Certificate management support
## Integration
The platform provides a developer-friendly interface with:
- Event-driven architecture
- Comprehensive logging system
- Modern C++20 codebase
For detailed integration instructions and API documentation, please refer to the source code and header files in the `/src/engine/` directory.
## License
Our source code is licensed under the MPL license. Naturally, any third-party components we use are subject to their respective licenses.
-45
View File
@@ -1,45 +0,0 @@
#!/usr/bin/python3
from pathlib import Path
import os
import multiprocessing
import shutil
# Temporary build directory
DIR_BUILD = 'build_android'
# Android NDK home directory
NDK_HOME = os.environ['ANDROID_NDK_HOME']
VCPKG_ROOT = os.environ['VCPKG_ROOT']
# CMake toolchain file
TOOLCHAIN_FILE = f'{NDK_HOME}/build/cmake/android.toolchain.cmake'
TOOLCHAIN_FILE_VCPKG=f'{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake'
# This directory
DIR_THIS = Path(__file__).parent.resolve()
# Path to app
DIR_SOURCE = (DIR_THIS / '../src').resolve()
def make_build() -> Path:
if Path(DIR_BUILD).exists():
shutil.rmtree(DIR_BUILD)
os.mkdir(DIR_BUILD)
os.chdir(DIR_BUILD)
cmd = f'cmake -DCMAKE_TOOLCHAIN_FILE={TOOLCHAIN_FILE} -DCMAKE_TOOLCHAIN_FILE={TOOLCHAIN_FILE_VCPKG} '
cmd += f'-DANDROID_NDK=$NDK_HOME '
cmd += f'-DANDROID_PLATFORM=24 '
cmd += f'-DCMAKE_BUILD=Release '
cmd += f'-DANDROID_ABI="arm64-v8a" '
cmd += '../src'
retcode = os.system(cmd)
if retcode != 0:
raise RuntimeError('Problem when configuring the project')
cmd = f'cmake --build . -j {multiprocessing.cpu_count()}'
retcode = os.system(cmd)
if retcode != 0:
raise RuntimeError('Problem when building the project')
if __name__ == '__main__':
make_build()
-38
View File
@@ -1,38 +0,0 @@
#!/usr/bin/env python3
from pathlib import Path
import os
import multiprocessing
import shutil
# Temporary build directory
DIR_BUILD = 'build_linux'
# This directory
DIR_THIS = Path(__file__).parent.resolve()
# Path to app
DIR_SOURCE = (DIR_THIS / '../src').resolve()
def make_build() -> Path:
if Path(DIR_BUILD).exists():
shutil.rmtree(DIR_BUILD)
os.mkdir(DIR_BUILD)
os.chdir(DIR_BUILD)
# OPUS_X86_MAY_HAVE_SSE4_1 is for clang builds
cmd = f'cmake ../src -G Ninja -D OPUS_X86_MAY_HAVE_SSE4_1=ON'
retcode = os.system(cmd)
if retcode != 0:
raise RuntimeError('Problem when configuring the project')
cmd = f'cmake --build . -j {multiprocessing.cpu_count()}'
retcode = os.system(cmd)
if retcode != 0:
raise RuntimeError('Problem when building the project')
os.chdir('..')
return Path(DIR_BUILD) / 'librtphone.a'
if __name__ == '__main__':
p = make_build()
print (f'Built: {p}')
-55
View File
@@ -1,55 +0,0 @@
#!/usr/bin/env python3
# This script is just to check if the library is buildable
from pathlib import Path
import os
import multiprocessing
import shutil
# Temporary build directory
DIR_BUILD = 'build_windows'
# This directory
DIR_THIS = Path(__file__).parent.resolve()
# Path to app
DIR_SOURCE = (DIR_THIS / '../src').resolve()
CMAKE_GENERATOR = '-G "Visual Studio 17 2022" -A x64'
# Not used yet
CMAKE_BUILD_TYPE = 'Debug'
def make_build() -> Path:
if Path(DIR_BUILD).exists():
shutil.rmtree(DIR_BUILD)
os.mkdir(DIR_BUILD)
os.chdir(DIR_BUILD)
if os.environ['VCPKG_ROOT']:
vcpkg_root = os.environ['VCPKG_ROOT']
else:
vcpkg_root = 'C:\\tools\\vcpkg'
if not Path(vcpkg_root).exists():
print(f'Failed to find vcpkg (OpenSSL libraries needed)')
exit(1)
cmd = f'cmake ../src {CMAKE_GENERATOR} -D CMAKE_TOOLCHAIN_FILE="{vcpkg_root}\\scripts\\buildsystems\\vcpkg.cmake"'
print(cmd)
retcode = os.system(cmd)
if retcode != 0:
raise RuntimeError('Problem when configuring the project')
cmd = f'cmake --build . -j {multiprocessing.cpu_count()}'
retcode = os.system(cmd)
if retcode != 0:
raise RuntimeError('Problem when building the project')
os.chdir('..')
return Path(DIR_BUILD) / 'Debug' / 'rtphone.lib'
if __name__ == '__main__':
p = make_build()
print (f'Built: {p}')
-123
View File
@@ -1,123 +0,0 @@
#!/usr/bin/env python3
"""
Count C/C++ files and lines in the project.
Works on both Linux and Windows.
"""
import os
import sys
from pathlib import Path
# C/C++ file extensions
CPP_EXTENSIONS = {'.c', '.cc', '.cpp', '.cxx', '.h', '.hh', '.hpp', '.hxx'}
def is_cpp_file(filepath: Path) -> bool:
"""Check if file has a C/C++ extension."""
return filepath.suffix.lower() in CPP_EXTENSIONS
def count_lines_in_file(filepath: Path) -> int:
"""Count lines in a single file."""
try:
with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
return sum(1 for _ in f)
except (IOError, OSError):
return 0
def collect_cpp_files(root_dir: Path) -> list[Path]:
"""Collect all C/C++ files recursively from root directory."""
cpp_files = []
for path in root_dir.rglob('*'):
if path.is_file() and is_cpp_file(path):
cpp_files.append(path)
return cpp_files
def get_directory_stats(root_dir: Path, cpp_files: list[Path]) -> dict[str, dict]:
"""Get statistics grouped by immediate subdirectories."""
stats = {}
# Initialize stats for immediate subdirectories
for item in root_dir.iterdir():
if item.is_dir():
stats[item.name] = {'files': 0, 'lines': 0}
# Also count files directly in root
stats['.'] = {'files': 0, 'lines': 0}
for filepath in cpp_files:
try:
# Get relative path from root
rel_path = filepath.relative_to(root_dir)
# Determine which immediate subdirectory this file belongs to
if len(rel_path.parts) == 1:
# File is directly in root
dir_key = '.'
else:
# File is in a subdirectory
dir_key = rel_path.parts[0]
if dir_key in stats:
line_count = count_lines_in_file(filepath)
stats[dir_key]['files'] += 1
stats[dir_key]['lines'] += line_count
except (ValueError, IndexError):
continue
return stats
def main():
# Use current directory as root, or accept a path argument
if len(sys.argv) > 1:
root_dir = Path(sys.argv[1]).resolve()
else:
root_dir = Path.cwd()
if not root_dir.exists():
print(f"Error: Directory '{root_dir}' does not exist.")
sys.exit(1)
print(f"Scanning C/C++ files in: {root_dir}")
print("=" * 60)
# Collect all C/C++ files
cpp_files = collect_cpp_files(root_dir)
if not cpp_files:
print("No C/C++ files found.")
sys.exit(0)
# Get directory statistics
dir_stats = get_directory_stats(root_dir, cpp_files)
# Print per-directory statistics
print("\nPer-directory C/C++ statistics:")
print("-" * 60)
print(f"{'Directory':<30} {'Files':>10} {'Lines':>15}")
print("-" * 60)
total_files = 0
total_lines = 0
# Sort directories: root first, then alphabetically
sorted_dirs = sorted(dir_stats.keys(), key=lambda x: (x != '.', x.lower()))
for dir_name in sorted_dirs:
stat = dir_stats[dir_name]
if stat['files'] > 0:
display_name = '(root)' if dir_name == '.' else dir_name
print(f"{display_name:<30} {stat['files']:>10} {stat['lines']:>15,}")
total_files += stat['files']
total_lines += stat['lines']
print("-" * 60)
print(f"{'TOTAL':<30} {total_files:>10} {total_lines:>15,}")
print("=" * 60)
if __name__ == '__main__':
main()
-23
View File
@@ -1,23 +0,0 @@
#!/bin/bash
SEND_MSG="/var/ci/conformance_ci/send_telegram_message.py"
mkdir -p build
cd build
# Configure
cmake ../src
if [ $? -ne 0 ]; then
/usr/bin/python3 $SEND_MSG "rtphone cmake failed. $BUILD_URL"
exit 1
fi
# Build
make -j2
if [ $? -ne 0 ]; then
/usr/bin/python3 $SEND_MSG "rtphone build failed. $BUILD_URL"
exit 1
fi
/usr/bin/python3 $SEND_MSG "rtphone builds ok. $BUILD_URL"
+192 -344
View File
@@ -1,381 +1,233 @@
cmake_minimum_required(VERSION 3.20)
project(rtphone) project(rtphone)
# Rely on C++ 20 cmake_minimum_required(VERSION 3.0)
set (CMAKE_CXX_STANDARD 20)
macro(configure_msvc_runtime)
if(MSVC)
# Default to statically-linked runtime.
if("${MSVC_RUNTIME}" STREQUAL "")
set(MSVC_RUNTIME "static")
endif()
# Set compiler options.
set(variables
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_MINSIZEREL
CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_RELWITHDEBINFO
)
if(${MSVC_RUNTIME} STREQUAL "static")
message(STATUS
"rtphone: MSVC -> forcing use of statically-linked runtime."
)
foreach(variable ${variables})
if(${variable} MATCHES "/MD")
string(REGEX REPLACE "/MD" "/MT" ${variable} "${${variable}}")
endif()
endforeach()
else()
message(STATUS
"rtphone: MSVC -> forcing use of dynamically-linked runtime."
)
foreach(variable ${variables})
if(${variable} MATCHES "/MT")
string(REGEX REPLACE "/MT" "/MD" ${variable} "${${variable}}")
endif()
endforeach()
endif()
foreach(variable ${variables})
string(REGEX REPLACE "/Z[iI7]" ""
${variable}
"${${variable}}")
set(${variable} "${${variable}} /Zi /Oy-")
endforeach()
endif()
endmacro()
# Rely on C++ 11
set (CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_STANDARD_REQUIRED ON) set (CMAKE_CXX_STANDARD_REQUIRED ON)
set (L libs) set (rtphone_libs libs)
set (E engine) set (rtphone_engine engine)
option (USE_AMR_CODEC "Use AMR codec. Requires libraries." ON) set (USE_AMR_CODEC OFF CACHE BOOL "Use AMR codec. Requires libraries.")
option (USE_EVS_CODEC "Use EVS codec." ON) set (USE_EVS_CODEC OFF CACHE BOOL "Use EVS codec." )
option (USE_MUSL "Build with MUSL library" OFF) set (USE_OPUS_CODEC OFF CACHE BOOL "Use Opus codec." )
set (USE_PVQA_LIB OFF CACHE BOOL "Build with Sevana PVQA library" )
set (USE_AQUA_LIB OFF CACHE BOOL "Build with Sevana AQuA library" )
set (USE_MUSL OFF CACHE BOOL "Build with MUSL library" )
# PIC code by default # PIC code by default
set (CMAKE_POSITION_INDEPENDENT_CODE ON) set (CMAKE_POSITION_INDEPENDENT_CODE ON)
set (RUNTIME_CPU_CAPABILITY_DETECTION ON)
set (CMAKE_WARN_DEPRECATED OFF)
find_package(OpenSSL REQUIRED) if (NOT DEFINED LIB_PLATFORM)
set (OPENSSL_SSL OpenSSL::SSL) set (LIB_PLATFORM ${CMAKE_CURRENT_SOURCE_DIR}/../../libraries)
set (OPENSSL_CRYPTO OpenSSL::Crypto) endif()
message("Libraries: ${LIB_PLATFORM}")
set (OPENSSL_INCLUDE ${LIB_PLATFORM}/openssl/1.0/include)
message ("Using OpenSSL include files from ${OPENSSL_INCLUDE}") message ("Using OpenSSL include files from ${OPENSSL_INCLUDE}")
message ("Using OpenSSL libs: ${OPENSSL_SSL} and ${OPENSSL_CRYPTO}")
# include_directories(${OPENSSL_INCLUDE})
# Used defines for our project
set (DEFINES -DUSE_OPENSSL)
# Libraries for our project
set (LIBS_STATIC "")
set (LIBS_DYNAMIC "")
# Try to prefer static libraries anyway
set (CMAKE_FIND_LIBRARY_SUFFIXES .a .so .dylib)
# Windows-specific definitions
if (CMAKE_SYSTEM MATCHES "Windows*") if (CMAKE_SYSTEM MATCHES "Windows*")
set (DEFINES ${DEFINES} -DTARGET_WIN -D_SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS -D_UNICODE -D_CRT_SECURE_NO_WARNINGS -DNOMINMAX) add_definitions (-DTARGET_WIN -D_SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS -D_UNICODE -D_CRT_SECURE_NO_WARNINGS)
set (TARGET_WIN ON)
endif() endif()
# Linux-specific definitions
if (CMAKE_SYSTEM MATCHES "Linux*") if (CMAKE_SYSTEM MATCHES "Linux*")
set (DEFINES ${DEFINES} -DTARGET_LINUX -DHAVE_NETINET_IN_H) add_definitions (-DTARGET_LINUX)
set (TARGET_LINUX ON)
set (LIBS_STATIC ${LIBS_STATIC} dl)
endif() endif()
# macOS-specific definitions
if (CMAKE_SYSTEM MATCHES "Darwin*") if (CMAKE_SYSTEM MATCHES "Darwin*")
set (DEFINES ${DEFINES} -DTARGET_OSX) add_definitions (-DTARGET_OSX)
set (TARGET_OSX ON)
set (LIBS_STATIC ${LIBS_STATIC} dl)
endif() endif()
if (CMAKE_SYSTEM MATCHES "Android") if (CMAKE_SYSTEM MATCHES "Android")
# Disable opencore AMR codecs - there is no ready libraries in vcpkg right now
set (USE_AMR_CODEC OFF)
message("Adding the Oboe library") message("Adding the Oboe library")
set (OBOE_DIR libs/oboe) set (OBOE_DIR libs/oboe)
add_subdirectory (${OBOE_DIR} build_oboe) add_subdirectory (${OBOE_DIR} ./oboe)
include_directories (${OBOE_DIR}/include) include_directories (${OBOE_DIR}/include)
set (DEFINES ${DEFINES} -DTARGET_ANDROID -DHAVE_NETINET_IN_H)
set (TARGET_ANDROID ON)
set (LIBS_STATIC ${LIBS} oboe)
endif() endif()
if (USE_MUSL) if (USE_MUSL)
set (DEFINES ${DEFINES} -DTARGET_MUSL) add_definitions(-DTARGET_MUSL)
set (TARGET_MUSL ON) endif()
if (USE_AQUA_LIB)
message("Use AQuA library")
add_definitions( -DUSE_AQUA_LIBRARY )
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/libs/pvqa/include)
endif()
if (USE_PVQA_LIBRARY)
message("Use PVQA libraries")
add_definitions( -DUSE_PVQA_LIBRARY )
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/libs/pvqa/include
${CMAKE_CURRENT_SOURCE_DIR}/libs/pvqa++/include)
endif() endif()
set (RTPHONE_SOURCES set (RTPHONE_SOURCES
${E}/engine_config.h ${rtphone_engine}/media/MT_Statistics.cpp
${E}/media/MT_Statistics.cpp ${rtphone_engine}/media/MT_WebRtc.cpp
${E}/media/MT_WebRtc.cpp ${rtphone_engine}/media/MT_Stream.cpp
${E}/media/MT_Stream.cpp ${rtphone_engine}/media/MT_SrtpHelper.cpp
${E}/media/MT_SrtpHelper.cpp ${rtphone_engine}/media/MT_SingleAudioStream.cpp
${E}/media/MT_SingleAudioStream.cpp ${rtphone_engine}/media/MT_NativeRtpSender.cpp
${E}/media/MT_NativeRtpSender.cpp ${rtphone_engine}/media/MT_Dtmf.cpp
${E}/media/MT_Dtmf.cpp ${rtphone_engine}/media/MT_CodecList.cpp
${E}/media/MT_CodecList.cpp ${rtphone_engine}/media/MT_Codec.cpp
${E}/media/MT_Codec.cpp ${rtphone_engine}/media/MT_Box.cpp
${E}/media/MT_Box.cpp ${rtphone_engine}/media/MT_AudioStream.cpp
${E}/media/MT_AudioStream.cpp ${rtphone_engine}/media/MT_AudioReceiver.cpp
${E}/media/MT_AudioReceiver.cpp ${rtphone_engine}/media/MT_AudioCodec.cpp
${E}/media/MT_AudioCodec.cpp ${rtphone_engine}/media/MT_CngHelper.cpp
${E}/media/MT_CngHelper.cpp ${rtphone_engine}/agent/Agent_Impl.cpp
${E}/agent/Agent_Impl.cpp ${rtphone_engine}/agent/Agent_AudioManager.cpp
${E}/agent/Agent_Impl.h ${rtphone_engine}/endpoint/EP_Account.cpp
${E}/agent/Agent_AudioManager.cpp ${rtphone_engine}/endpoint/EP_AudioProvider.cpp
${E}/agent/Agent_AudioManager.h ${rtphone_engine}/endpoint/EP_DataProvider.cpp
${E}/endpoint/EP_Account.cpp ${rtphone_engine}/endpoint/EP_Engine.cpp
${E}/endpoint/EP_Account.h ${rtphone_engine}/endpoint/EP_NetworkQueue.cpp
${E}/endpoint/EP_AudioProvider.cpp ${rtphone_engine}/endpoint/EP_Observer.cpp
${E}/endpoint/EP_AudioProvider.h ${rtphone_engine}/endpoint/EP_Session.cpp
${E}/endpoint/EP_DataProvider.cpp
${E}/endpoint/EP_DataProvider.h
${E}/endpoint/EP_Engine.cpp
${E}/endpoint/EP_Engine.h
${E}/endpoint/EP_NetworkQueue.cpp
${E}/endpoint/EP_NetworkQueue.h
${E}/endpoint/EP_Observer.cpp
${E}/endpoint/EP_Observer.h
${E}/endpoint/EP_Session.cpp
${E}/endpoint/EP_Session.h
${E}/media/MT_Statistics.h ${rtphone_engine}/media/MT_Statistics.h
${E}/media/MT_WebRtc.h ${rtphone_engine}/media/MT_WebRtc.h
${E}/media/MT_Stream.h ${rtphone_engine}/media/MT_Stream.h
${E}/media/MT_SrtpHelper.h ${rtphone_engine}/media/MT_SrtpHelper.h
${E}/media/MT_SingleAudioStream.h ${rtphone_engine}/media/MT_SingleAudioStream.h
${E}/media/MT_NativeRtpSender.h ${rtphone_engine}/media/MT_NativeRtpSender.h
${E}/media/MT_Dtmf.h ${rtphone_engine}/media/MT_Dtmf.h
${E}/media/MT_CodecList.h ${rtphone_engine}/media/MT_CodecList.h
${E}/media/MT_Codec.h ${rtphone_engine}/media/MT_Codec.h
${E}/media/MT_Box.h ${rtphone_engine}/media/MT_Box.h
${E}/media/MT_AudioStream.h ${rtphone_engine}/media/MT_AudioStream.h
${E}/media/MT_AudioReceiver.h ${rtphone_engine}/media/MT_AudioReceiver.h
${E}/media/MT_AudioCodec.h ${rtphone_engine}/media/MT_AudioCodec.h
${E}/media/MT_CngHelper.h
${E}/media/MT_Statistics.cpp ${rtphone_engine}/media/MT_CngHelper.h
${E}/media/MT_WebRtc.cpp ${rtphone_engine}/agent/Agent_Impl.h
${E}/media/MT_Stream.cpp ${rtphone_engine}/agent/Agent_AudioManager.h
${E}/media/MT_SrtpHelper.cpp ${rtphone_engine}/endpoint/EP_Account.h
${E}/media/MT_SingleAudioStream.cpp ${rtphone_engine}/endpoint/EP_AudioProvider.h
${E}/media/MT_NativeRtpSender.cpp ${rtphone_engine}/endpoint/EP_DataProvider.h
${E}/media/MT_Dtmf.cpp ${rtphone_engine}/endpoint/EP_Engine.h
${E}/media/MT_CodecList.cpp ${rtphone_engine}/endpoint/EP_NetworkQueue.h
${E}/media/MT_Codec.cpp ${rtphone_engine}/endpoint/EP_Observer.h
${E}/media/MT_Box.cpp ${rtphone_engine}/endpoint/EP_Session.h
${E}/media/MT_AudioStream.cpp
${E}/media/MT_AudioReceiver.cpp
${E}/media/MT_AudioCodec.cpp
${E}/media/MT_CngHelper.cpp
${E}/media/MT_AmrCodec.cpp
${E}/media/MT_EvsCodec.cpp
${E}/media/MT_Statistics.h
${E}/media/MT_WebRtc.h
${E}/media/MT_Stream.h
${E}/media/MT_SrtpHelper.h
${E}/media/MT_SingleAudioStream.h
${E}/media/MT_NativeRtpSender.h
${E}/media/MT_Dtmf.h
${E}/media/MT_CodecList.h
${E}/media/MT_Codec.h
${E}/media/MT_Box.h
${E}/media/MT_AudioStream.h
${E}/media/MT_AudioReceiver.h
${E}/media/MT_AudioCodec.h
${E}/media/MT_CngHelper.h
${E}/media/MT_AmrCodec.h
${E}/media/MT_EvsCodec.h
${E}/helper/HL_AsyncCommand.cpp
${E}/helper/HL_AsyncCommand.h
${E}/helper/HL_Base64.h
${E}/helper/HL_ByteBuffer.h
${E}/helper/HL_Calculator.cpp
${E}/helper/HL_Calculator.h
${E}/helper/HL_CrashRpt.cpp
${E}/helper/HL_CrashRpt.h
${E}/helper/HL_CsvReader.cpp
${E}/helper/HL_CsvReader.h
${E}/helper/HL_Epoll.cpp
${E}/helper/HL_Epoll.h
${E}/helper/HL_Exception.h
${E}/helper/HL_File.cpp
${E}/helper/HL_File.h
${E}/helper/HL_HepSupport.cpp
${E}/helper/HL_HepSupport.h
${E}/helper/HL_InternetAddress.h
${E}/helper/HL_IuUP.cpp
${E}/helper/HL_IuUP.h
${E}/helper/HL_Log.cpp
${E}/helper/HL_Log.h
${E}/helper/HL_NetworkFrame.cpp
${E}/helper/HL_NetworkFrame.h
${E}/helper/HL_NetworkSocket.cpp
${E}/helper/HL_NetworkSocket.h
${E}/helper/HL_Optional.hpp
${E}/helper/HL_OsVersion.cpp
${E}/helper/HL_OsVersion.h
${E}/helper/HL_Pointer.cpp
${E}/helper/HL_Pointer.h
${E}/helper/HL_Process.cpp
${E}/helper/HL_Process.h
${E}/helper/HL_Rtp.cpp
${E}/helper/HL_Rtp.h
${E}/helper/HL_Singletone.cpp
${E}/helper/HL_Singletone.h
${E}/helper/HL_SocketHeap.cpp
${E}/helper/HL_SocketHeap.h
${E}/helper/HL_Statistics.cpp
${E}/helper/HL_Statistics.h
${E}/helper/HL_StreamState.h
${E}/helper/HL_String.cpp
${E}/helper/HL_String.h
${E}/helper/HL_Sync.cpp
${E}/helper/HL_Sync.h
${E}/helper/HL_ThreadPool.cpp
${E}/helper/HL_ThreadPool.h
${E}/helper/HL_Time.cpp
${E}/helper/HL_Time.h
${E}/helper/HL_Types.h
${E}/helper/HL_Types.cpp
${E}/helper/HL_Usb.cpp
${E}/helper/HL_Usb.h
${E}/helper/HL_Uuid.cpp
${E}/helper/HL_Uuid.h
${E}/helper/HL_VariantMap.cpp
${E}/helper/HL_VariantMap.h
${E}/helper/HL_Xcap.cpp
${E}/helper/HL_Xcap.h
${E}/audio/Audio_Resampler.cpp
${E}/audio/Audio_Resampler.h
${E}/audio/Audio_Quality.cpp
${E}/audio/Audio_Quality.h
${E}/audio/Audio_Mixer.cpp
${E}/audio/Audio_Mixer.h
${E}/audio/Audio_Interface.cpp
${E}/audio/Audio_Interface.h
${E}/audio/Audio_Helper.cpp
${E}/audio/Audio_Helper.h
${E}/audio/Audio_DataWindow.cpp
${E}/audio/Audio_DataWindow.h
${E}/audio/Audio_DevicePair.cpp
${E}/audio/Audio_DevicePair.h
${E}/audio/Audio_Player.cpp
${E}/audio/Audio_Player.h
${E}/audio/Audio_Null.cpp
${E}/audio/Audio_Null.h
${E}/audio/Audio_CoreAudio.cpp
${E}/audio/Audio_CoreAudio.h
${E}/audio/Audio_DirectSound.cpp
${E}/audio/Audio_DirectSound.h
${E}/audio/Audio_AndroidOboe.cpp
${E}/audio/Audio_AndroidOboe.h
${E}/audio/Audio_WavFile.cpp
${E}/audio/Audio_WavFile.h
${L}/ice/hmac_sha1_impl.cpp
${L}/ice/hmac_sha1_impl.h
${L}/ice/ICEAction.h
${L}/ice/ICEAddress.cpp
${L}/ice/ICEAddress.h
${L}/ice/ICEAuthTransaction.cpp
${L}/ice/ICEAuthTransaction.h
${L}/ice/ICEBinding.cpp
${L}/ice/ICEBinding.h
${L}/ice/ICEBox.cpp
${L}/ice/ICEBox.h
${L}/ice/ICEBoxImpl.cpp
${L}/ice/ICEBoxImpl.h
${L}/ice/ICEByteBuffer.cpp
${L}/ice/ICEByteBuffer.h
${L}/ice/ICECandidate.cpp
${L}/ice/ICECandidate.h
${L}/ice/ICECandidatePair.cpp
${L}/ice/ICECandidatePair.h
${L}/ice/ICECheckList.cpp
${L}/ice/ICECheckList.h
${L}/ice/ICECRC32.cpp
${L}/ice/ICECRC32.h
${L}/ice/ICEError.cpp
${L}/ice/ICEError.h
${L}/ice/ICEEvent.h
${L}/ice/ICELog.cpp
${L}/ice/ICELog.h
${L}/ice/ICEMD5.cpp
${L}/ice/ICEMD5.h
${L}/ice/ICENetworkHelper.cpp
${L}/ice/ICENetworkHelper.h
${L}/ice/ICEPacketTimer.cpp
${L}/ice/ICEPacketTimer.h
${L}/ice/ICEPlatform.cpp
${L}/ice/ICEPlatform.h
${L}/ice/ICERelaying.cpp
${L}/ice/ICERelaying.h
${L}/ice/ICESession.cpp
${L}/ice/ICESession.h
${L}/ice/ICESHA1.cpp
${L}/ice/ICESHA1.h
${L}/ice/ICESocket.h
${L}/ice/ICEStream.cpp
${L}/ice/ICEStream.h
${L}/ice/ICEStunAttributes.cpp
${L}/ice/ICEStunAttributes.h
${L}/ice/ICEStunConfig.cpp
${L}/ice/ICEStunConfig.h
${L}/ice/ICEStunMessage.cpp
${L}/ice/ICEStunMessage.h
${L}/ice/ICEStunTransaction.cpp
${L}/ice/ICEStunTransaction.h
${L}/ice/ICESync.cpp
${L}/ice/ICESync.h
${L}/ice/ICETime.cpp
${L}/ice/ICETime.h
${L}/ice/ICETransactionList.cpp
${L}/ice/ICETransactionList.h
${L}/ice/ICETypes.h
${L}/ice/md5_impl.cpp
${L}/ice/md5_impl.h
) )
if (USE_AMR_CODEC)
add_definitions(-DUSE_AMR_CODEC)
set(RTPHONE_SOURCES ${RTPHONE_SOURCES} ${rtphone_engine}/media/MT_AmrCodec.cpp ${rtphone_engine}/media/MT_AmrCodec.h)
endif()
if (USE_EVS_CODEC)
add_definitions(-DUSE_EVS_CODEC)
set(RTPHONE_SOURCES ${RTPHONE_SOURCES} ${rtphone_engine}/media/MT_EvsCodec.cpp ${rtphone_engine}/media/MT_EvsCodec.h)
endif()
if (USE_OPUS_CODEC)
add_definitions(-DUSE_OPUS_CODEC)
endif()
add_library (rtphone STATIC ${RTPHONE_SOURCES}) add_library (rtphone STATIC ${RTPHONE_SOURCES})
add_subdirectory(${L}/resiprocate) add_subdirectory(${rtphone_libs}/resiprocate)
add_subdirectory(${L}/jrtplib/src) add_subdirectory(${rtphone_libs}/ice)
add_subdirectory(${L}/libg729) add_subdirectory(${rtphone_libs}/jrtplib/src)
add_subdirectory(${rtphone_libs}/libg729)
if (USE_EVS_CODEC) if (USE_EVS_CODEC)
add_subdirectory(${L}/libevs) add_subdirectory(${rtphone_libs}/libevs)
endif() endif()
add_subdirectory(${L}/libgsm) add_subdirectory(${rtphone_libs}/libgsm)
add_subdirectory(${L}/gsmhr) add_subdirectory(${rtphone_libs}/gsmhr)
add_subdirectory(${L}/g722) add_subdirectory(${rtphone_libs}/g722)
add_subdirectory(${L}/speexdsp) add_subdirectory(${rtphone_libs}/speexdsp)
add_subdirectory(${L}/libsrtp) add_subdirectory(${rtphone_libs}/srtp)
add_subdirectory(${L}/webrtc) add_subdirectory(${rtphone_libs}/webrtc)
add_subdirectory(${L}/opus) add_subdirectory(${rtphone_engine}/helper)
add_subdirectory(${rtphone_engine}/audio)
add_subdirectory(${rtphone_engine}/media)
# Suppose the subproject defines target "mylib" set (LIBS ice_stack jrtplib g729_codec gsm_codec
if(MSVC) gsmhr_codec g722_codec srtp resiprocate helper_lib audio_lib webrtc speexdsp
# target_compile_options(opus PRIVATE /O2 /DNDEBUG) uuid)
# Optional: enable whole program optimization on MSVC
# target_compile_options(mylib PRIVATE /GL) if (CMAKE_SYSTEM MATCHES "Win*")
# target_link_options(mylib PRIVATE /LTCG) set (LIBS ${LIBS} )
else() else ()
target_compile_options(opus PRIVATE -O3 -DNDEBUG) set (LIBS ${LIBS} dl uuid)
endif ()
if (CMAKE_SYSTEM MATCHES "Android")
set (LIBS ${LIBS} oboe)
endif() endif()
set (LIBS_STATIC ${LIBS_STATIC} jrtplib g729_codec gsm_codec opus
gsmhr_codec g722_codec srtp3 resiprocate webrtc speexdsp)
if (USE_AMR_CODEC) if (USE_AMR_CODEC)
message("Media: AMR NB and WB codecs will be included") set (LIBS ${LIBS})
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/libs/opencore-amr build_opencore) endif (USE_AMR_CODEC)
set (OPENCORE_AMRWB opencore-amrwb)
set (OPENCORE_AMRNB opencore-amrnb)
set (DEFINES ${DEFINES} -DUSE_AMR_CODEC) target_link_libraries(rtphone
set (LIBS_STATIC ${LIBS_STATIC} ${OPENCORE_AMRNB} ${OPENCORE_AMRWB}) ice_stack jrtplib g729_codec gsm_codec
endif() gsmhr_codec g722_codec srtp resiprocate
helper_lib
audio_lib
webrtc
speexdsp
uuid
${OPENSSL_SSL}
${OPENSSL_CRYPTO}
${LIBS} )
if (USE_EVS_CODEC)
message("Media: EVS codec will be included.")
set (DEFINES ${DEFINES} -DUSE_EVS_CODEC)
set (LIBS_STATIC ${LIBS_STATIC} evs_codec)
endif()
target_compile_definitions(rtphone PUBLIC ${DEFINES} )
if (TARGET_LINUX)
# PRIVATE, not PUBLIC: rtphone is a STATIC library, so these link options are
# never used to build rtphone itself and must not propagate to consumers.
# As PUBLIC they leaked into every consumer's LINK_FLAGS as an adjacent
# "-Wl,-Bstatic -Wl,-Bdynamic" pair (the wrapped libraries land in a separate
# LINK_LIBRARIES section, so nothing is actually wrapped). The trailing
# -Bdynamic forced the linker back into dynamic-search mode, which broke
# fully-static consumers (e.g. vq-core built with SERVER_STATIC_LINKING=ON:
# "attempted static link of dynamic object libz.so").
target_link_options(rtphone PRIVATE -Wl,-Bstatic)
target_compile_options(rtphone PUBLIC -Wno-deprecated -Wno-deprecated-declarations)
endif()
target_link_libraries(rtphone PUBLIC ${LIBS_STATIC})
if (TARGET_LINUX)
target_link_options(rtphone PRIVATE -Wl,-Bdynamic)
endif()
target_include_directories(rtphone target_include_directories(rtphone
PUBLIC PUBLIC
@@ -383,19 +235,15 @@ target_include_directories(rtphone
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/engine> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/engine>
${CMAKE_CURRENT_SOURCE_DIR}/libs ${CMAKE_CURRENT_SOURCE_DIR}/libs
${LIB_PLATFORM}/opus/include ${LIB_PLATFORM}/opus/include
${E}/helper
${E}/audio
${E}/media
${L}
${L}/ice
PRIVATE PRIVATE
${L}/libevs/lib_com ${CMAKE_CURRENT_SOURCE_DIR}/libs/
${L}/libevs/lib_enc ${CMAKE_CURRENT_SOURCE_DIR}/libs/libevs/lib_com
${L}/libevs/lib_dec ${CMAKE_CURRENT_SOURCE_DIR}/libs/libevs/lib_enc
${L}/speex/include ${CMAKE_CURRENT_SOURCE_DIR}/libs/libevs/lib_dec
${L}/libs/json ${CMAKE_CURRENT_SOURCE_DIR}/libs/speex/include
${CMAKE_CURRENT_SOURCE_DIR}/libs/opus/include/
${CMAKE_CURRENT_SOURCE_DIR}/libs/json
) )
find_package(OpenSSL REQUIRED) # For MSVC static builds
target_link_libraries(rtphone PUBLIC OpenSSL::SSL) configure_msvc_runtime()
target_link_libraries(rtphone PUBLIC OpenSSL::Crypto)
+120 -133
View File
@@ -1,206 +1,193 @@
/* Copyright(C) 2007-2023 VoIP objects (voipobjects.com) /* Copyright(C) 2007-2017 VoIP objects (voipobjects.com)
* This Source Code Form is subject to the terms of the Mozilla Public * This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "Agent_AudioManager.h" #include "Agent_AudioManager.h"
#include "../engine/audio/Audio_WavFile.h" #include "../engine/audio/Audio_WavFile.h"
#include "../engine/helper/HL_String.h"
#include "../engine/audio/Audio_Null.h" #include "../engine/audio/Audio_Null.h"
#include "HL_String.h"
#if defined(TARGET_ANDROID) #if defined(TARGET_ANDROID)
#include "../engine/audio/Audio_Android.h" # include "../engine/audio/Audio_Android.h"
#endif #endif
#define LOG_SUBSYSTEM "audio" #define LOG_SUBSYSTEM "AudioManager"
AudioManager::AudioManager() : mTerminal(nullptr), mAudioMonitoring(nullptr) // ---------------- AudioManager -------------
//static AudioManager GAudioManager;
AudioManager::AudioManager()
:mTerminal(nullptr)
{ {
mPlayer.setDelegate(this); mPlayer.setDelegate(this);
} }
AudioManager::~AudioManager() AudioManager::~AudioManager()
{ {
// stop(); //stop();
}
AudioManager& AudioManager::instance()
{
static std::shared_ptr<AudioManager> GAudioManager;
if (!GAudioManager)
GAudioManager = std::make_shared<AudioManager>();
return *GAudioManager;
} }
void AudioManager::setTerminal(MT::Terminal* terminal) void AudioManager::setTerminal(MT::Terminal* terminal)
{ {
mTerminal = terminal; mTerminal = terminal;
} }
MT::Terminal* AudioManager::terminal() MT::Terminal* AudioManager::terminal()
{ {
return mTerminal; return mTerminal;
}
void AudioManager::setAudioMonitoring(Audio::DataConnection* monitoring)
{
mAudioMonitoring = monitoring;
}
Audio::DataConnection* AudioManager::audioMonitoring()
{
return mAudioMonitoring;
} }
#define LOCK_MANAGER std::unique_lock<std::mutex> l(mGuard) #define LOCK_MANAGER std::unique_lock<std::mutex> l(mGuard)
void AudioManager::start(int usageId) void AudioManager::start(int usageId)
{ {
assert(mTerminal); assert(mTerminal);
LOCK_MANAGER; LOCK_MANAGER;
ICELogInfo(<< "Start main audio with usage id " << usageId); ICELogInfo(<< "Start main audio with usage id " << usageId);
if (mUsage.obtain(usageId) > 1) if (mUsage.obtain(usageId) > 1)
return; return;
// Maybe it is time to initialize global audio support if (Audio::OsEngine::instance())
if (Audio::OsEngine::instance()) Audio::OsEngine::instance()->open();
Audio::OsEngine::instance()->open();
if (!mAudioInput || !mAudioOutput) if (!mAudioInput || !mAudioOutput)
{ {
// Disable AEC for now - because PVQA conflicts with speex AEC. // Disable AEC for now - because PVQA conflicts with speex AEC.
std::shared_ptr<Audio::Enumerator> enumerator(Audio::Enumerator::make(usageId == atNull)); std::shared_ptr<Audio::Enumerator> enumerator(Audio::Enumerator::make(usageId == atNull));
if (!mTerminal->audio()) if (!mTerminal->audio())
{ mTerminal->setAudio(std::make_shared<Audio::DevicePair>(false, true));
auto audio = std::make_shared<Audio::DevicePair>();
audio->setAgc(true);
audio->setAec(false);
audio->setMonitoring(mAudioMonitoring);
mTerminal->setAudio(audio); if (!mAudioInput)
} {
enumerator->open(Audio::myMicrophone);
int inputIndex = enumerator->indexOfDefaultDevice();
if (!mAudioInput) // Construct and set to terminal's audio pair input device
{ if (usageId != atNull)
enumerator->open(Audio::myMicrophone); mAudioInput = Audio::PInputDevice(Audio::InputDevice::make(enumerator->idAt(inputIndex)));
int inputIndex = enumerator->indexOfDefaultDevice(); else
mAudioInput = Audio::PInputDevice(new Audio::NullInputDevice());
// Construct default platform input device mTerminal->audio()->setInput(mAudioInput);
if (usageId != atNull) }
mAudioInput = Audio::PInputDevice(Audio::InputDevice::make(enumerator->idAt(inputIndex)));
else
mAudioInput = Audio::PInputDevice(new Audio::NullInputDevice());
}
// Bind input to the terminal's device pair regardless of whether it was
// just constructed or externally injected via setAudioInput().
mTerminal->audio()->setInput(mAudioInput);
if (!mAudioOutput) if (!mAudioOutput)
{ {
Audio::Enumerator* enumerator = Audio::Enumerator::make(usageId == atNull); Audio::Enumerator *enumerator = Audio::Enumerator::make(usageId == atNull);
enumerator->open(Audio::mySpeaker); enumerator->open(Audio::mySpeaker);
int outputIndex = enumerator->indexOfDefaultDevice(); int outputIndex = enumerator->indexOfDefaultDevice();
// Construct default platform output device // Construct and set terminal's audio pair output device
if (usageId != atNull) if (usageId != atNull)
{ {
if (outputIndex >= enumerator->count()) if (outputIndex >= enumerator->count())
outputIndex = 0; outputIndex = 0;
mAudioOutput = Audio::POutputDevice(Audio::OutputDevice::make(enumerator->idAt(outputIndex))); mAudioOutput = Audio::POutputDevice(
} Audio::OutputDevice::make(enumerator->idAt(outputIndex)));
else }
mAudioOutput = Audio::POutputDevice(new Audio::NullOutputDevice()); else
} mAudioOutput = Audio::POutputDevice(new Audio::NullOutputDevice());
mTerminal->audio()->setOutput(mAudioOutput);
}
// Open audio mTerminal->audio()->setOutput(mAudioOutput);
if (mAudioInput) }
mAudioInput->open(); }
if (mAudioOutput)
mAudioOutput->open(); // Open audio
if (mAudioInput)
mAudioInput->open();
if (mAudioOutput)
mAudioOutput->open();
} }
void AudioManager::close() void AudioManager::close()
{ {
mUsage.clear(); mUsage.clear();
if (mAudioInput) if (mAudioInput)
{ {
mAudioInput->close(); mAudioInput->close();
mAudioInput.reset(); mAudioInput.reset();
} }
if (mAudioOutput) if (mAudioOutput)
{ {
mAudioOutput->close(); mAudioOutput->close();
mAudioOutput.reset(); mAudioOutput.reset();
} }
mPlayer.setOutput(Audio::POutputDevice()); mPlayer.setOutput(Audio::POutputDevice());
} }
void AudioManager::stop(int usageId) void AudioManager::stop(int usageId)
{ {
LOCK_MANAGER; LOCK_MANAGER;
ICELogInfo(<< "Stop main audio with usage id " << usageId); ICELogInfo( << "Stop main audio with usage id " << usageId);
if (mTerminal) if (mTerminal)
{ {
if (mTerminal->audio()) if (mTerminal->audio())
mTerminal->audio()->player().release(usageId); mTerminal->audio()->player().release(usageId);
} }
if (!mUsage.release(usageId)) if (!mUsage.release(usageId))
{ {
close(); close();
// Reset device pair on terminal side // Reset device pair on terminal side
mTerminal->setAudio(Audio::PDevicePair()); mTerminal->setAudio(Audio::PDevicePair());
if (Audio::OsEngine::instance()) if (Audio::OsEngine::instance())
Audio::OsEngine::instance()->close(); Audio::OsEngine::instance()->close();
} }
}
void AudioManager::setAudioInput(Audio::PInputDevice input)
{
LOCK_MANAGER;
mAudioInput = std::move(input);
}
void AudioManager::setAudioOutput(Audio::POutputDevice output)
{
LOCK_MANAGER;
mAudioOutput = std::move(output);
} }
void AudioManager::startPlayFile(int usageId, const std::string& path, AudioTarget target, LoopMode lm, int timelimit) void AudioManager::startPlayFile(int usageId, const std::string& path, AudioTarget target, LoopMode lm, int timelimit)
{ {
// Check if file exists // Check if file exists
Audio::PWavFileReader r = std::make_shared<Audio::WavFileReader>(); Audio::PWavFileReader r = std::make_shared<Audio::WavFileReader>();
#ifdef TARGET_WIN #ifdef TARGET_WIN
r->open(strx::makeTstring(path)); r->open(StringHelper::makeTstring(path));
#else #else
r->open(path); r->open(path);
#endif #endif
if (!r->isOpened()) if (!r->isOpened())
{ {
ICELogError(<< "Cannot open file to play"); ICELogError(<< "Cannot open file to play");
return; return;
} }
// Delegate processing to existing audio device pair manager // Delegate processing to existing audio device pair manager
mTerminal->audio()->player().add(usageId, r, lm == lmLoopAudio, timelimit); mTerminal->audio()->player().add(usageId, r, lm == lmLoopAudio, timelimit);
start(usageId); start(usageId);
} }
void AudioManager::stopPlayFile(int usageId) void AudioManager::stopPlayFile(int usageId)
{ {
stop(usageId); stop(usageId);
mPlayer.release(usageId); mPlayer.release(usageId);
} }
void AudioManager::onFilePlayed(Audio::Player::PlaylistItem& item) {} void AudioManager::onFilePlayed(Audio::Player::PlaylistItem& item)
{
}
void AudioManager::process() void AudioManager::process()
{ {
mPlayer.releasePlayed(); mPlayer.releasePlayed();
std::vector<int> ids; std::vector<int> ids;
mTerminal->audio()->player().retrieveUsageIds(ids); mTerminal->audio()->player().retrieveUsageIds(ids);
for (int id : ids) for (unsigned i=0; i<ids.size(); i++)
stop(id); stop(ids[i]);
} }
+52 -59
View File
@@ -8,87 +8,80 @@
#include "../engine/audio/Audio_Interface.h" #include "../engine/audio/Audio_Interface.h"
#include "../engine/audio/Audio_Player.h" #include "../engine/audio/Audio_Player.h"
#include "../engine/endpoint/EP_Engine.h"
#include "../engine/media/MT_Box.h" #include "../engine/media/MT_Box.h"
#include "../engine/helper/HL_Log.h"
#include "../engine/helper/HL_Sync.h"
enum enum
{ {
AudioPrefix_Ring = 1, AudioPrefix_Ring = 1,
AudioPrefix_Zero, AudioPrefix_Zero,
AudioPrefix_One, AudioPrefix_One,
AudioPrefix_Two, AudioPrefix_Two,
AudioPrefix_Three, AudioPrefix_Three,
AudioPrefix_Four, AudioPrefix_Four,
AudioPrefix_Five, AudioPrefix_Five,
AudioPrefix_Six, AudioPrefix_Six,
AudioPrefix_Seven, AudioPrefix_Seven,
AudioPrefix_Eight, AudioPrefix_Eight,
AudioPrefix_Nine, AudioPrefix_Nine,
AudioPrefix_Asterisk, AudioPrefix_Asterisk,
AudioPrefix_Diez AudioPrefix_Diez
}; };
#define AudioSessionCoeff 64 #define AudioSessionCoeff 64
class AudioManager : public Audio::Player::EndOfAudioDelegate class AudioManager: public Audio::Player::EndOfAudioDelegate
{ {
public: public:
AudioManager(); AudioManager();
virtual ~AudioManager(); virtual ~AudioManager();
// static AudioManager& instance(); static AudioManager& instance();
// Enforces to close audio devices. Used to shutdown AudioManager on exit from application // Enforces to close audio devices. Used to shutdown AudioManager on exit from application
void close(); void close();
// Terminal and settings must be available for AudioManager // Terminal and settings must be available for AudioManager
void setTerminal(MT::Terminal* terminal); void setTerminal(MT::Terminal* terminal);
MT::Terminal* terminal(); MT::Terminal* terminal();
void setAudioMonitoring(Audio::DataConnection* monitoring); // Start/stop methods relies on usage counter; only first start and last stop opens/closes devices actually
Audio::DataConnection* audioMonitoring(); void start(int usageId);
void stop(int usageId);
// Start/stop methods relies on usage counter; only first start and last stop opens/closes devices actually enum AudioTarget
void start(int usageId); {
void stop(int usageId); atNull,
atReceiver,
atRinger
};
// Inject a custom input device. Must be called before start(): when set, enum LoopMode
// start() skips construction of the default platform microphone. Pass an {
// empty pointer to clear the override. lmLoopAudio,
void setAudioInput(Audio::PInputDevice input); lmNoloop
void setAudioOutput(Audio::POutputDevice output); };
enum AudioTarget void startPlayFile(int usageId, const std::string& path, AudioTarget target, LoopMode lm, int timelimit = 0);
{ void stopPlayFile(int usageId);
atNull,
atReceiver,
atRinger
};
enum LoopMode void onFilePlayed(Audio::Player::PlaylistItem& item);
{
lmLoopAudio,
lmNoloop
};
void startPlayFile(int usageId, const std::string& path, AudioTarget target, LoopMode lm, int timelimit = 0); // Must be called from main loop to release used audio devices
void stopPlayFile(int usageId); void process();
void onFilePlayed(Audio::Player::PlaylistItem& item);
// Must be called from main loop to release used audio devices
void process();
protected: protected:
Audio::PInputDevice mAudioInput; Audio::PInputDevice mAudioInput;
Audio::POutputDevice mAudioOutput; Audio::POutputDevice mAudioOutput;
Audio::Player mPlayer; Audio::Player mPlayer;
MT::Terminal* mTerminal; MT::Terminal* mTerminal;
Audio::DataConnection* mAudioMonitoring;
std::map<int, int> UsageMap; std::map<int, int> UsageMap;
UsageCounter mUsage; UsageCounter mUsage;
std::mutex mGuard; std::mutex mGuard;
}; };
#endif #endif
+324 -218
View File
@@ -3,12 +3,17 @@
#include "helper/HL_String.h" #include "helper/HL_String.h"
#include "helper/HL_StreamState.h" #include "helper/HL_StreamState.h"
#include "helper/HL_VariantMap.h" #include "helper/HL_VariantMap.h"
// #include "helper/HL_CsvReader.h" #include "helper/HL_CsvReader.h"
// #include "helper/HL_Base64.h" #include "helper/HL_Base64.h"
#include "media/MT_CodecList.h" #include <fstream>
#include "audio/Audio_Null.h"
// #include <fstream>
#if defined(USE_PVQA_LIBRARY)
# include "pvqa++.h"
#endif
#if defined(USE_AQUA_LIBRARY)
# include "aqua++.h"
#endif
const std::string Status_Ok = "ok"; const std::string Status_Ok = "ok";
const std::string Status_SessionNotFound = "session not found"; const std::string Status_SessionNotFound = "session not found";
@@ -17,11 +22,11 @@ const std::string Status_FailedToOpenFile = "failed to open file";
const std::string Status_NoActiveProvider = "no active provider"; const std::string Status_NoActiveProvider = "no active provider";
const std::string Status_NoMediaAction = "no valid media action"; const std::string Status_NoMediaAction = "no valid media action";
const std::string Status_NoCommand = "no valid command"; const std::string Status_NoCommand = "no valid command";
const std::string Status_NoAudioManager = "no audio manager";
#define LOG_SUBSYSTEM "agent" #define LOG_SUBSYSTEM "Agent"
AgentImpl::AgentImpl() : mShutdown(false), mEventListChangeCondVar() AgentImpl::AgentImpl()
:mShutdown(false), mEventListChangeCondVar()
{ {
#if defined(TARGET_ANDROID) || defined(TARGET_WIN) #if defined(TARGET_ANDROID) || defined(TARGET_WIN)
ice::GLogger.useDebugWindow(true); ice::GLogger.useDebugWindow(true);
@@ -34,24 +39,6 @@ AgentImpl::~AgentImpl()
stopAgentAndThread(); stopAgentAndThread();
} }
// Get access to internal audio manager. Value can be nullptr.
const std::shared_ptr<AudioManager>& AgentImpl::audioManager() const
{
return mAudioManager;
}
void AgentImpl::setAudioMonitoring(Audio::DataConnection* monitoring)
{
mAudioMonitoring = monitoring;
if (mAudioManager)
mAudioManager->setAudioMonitoring(monitoring);
}
Audio::DataConnection* AgentImpl::monitoring() const
{
return mAudioMonitoring;
}
void AgentImpl::run() void AgentImpl::run()
{ {
while (!mShutdown) while (!mShutdown)
@@ -77,71 +64,82 @@ std::string AgentImpl::command(const std::string& command)
return ""; return "";
std::string cmd = d["command"].asString(); std::string cmd = d["command"].asString();
if (cmd != "wait_for_event" && cmd != "agent_add_root_cert") if (cmd != "wait_for_event")
{ {
ICELogInfo(<< command); ICELogInfo(<< command);
} }
if (cmd == "config") if (cmd == "config")
processConfig(d, answer); processConfig(d, answer);
else if (cmd == "start") else
if (cmd == "start")
processStart(d, answer); processStart(d, answer);
else if (cmd == "stop") else
if (cmd == "stop")
processStop(d, answer); processStop(d, answer);
else if (cmd == "account_create") else
if (cmd == "account_create")
processCreateAccount(d, answer); processCreateAccount(d, answer);
else if (cmd == "account_start") else
if (cmd == "account_start")
processStartAccount(d, answer); processStartAccount(d, answer);
else if (cmd == "account_setuserinfo") else
if (cmd == "account_setuserinfo")
processSetUserInfoToAccount(d, answer); processSetUserInfoToAccount(d, answer);
else if (cmd == "session_create") else
{ if (cmd == "session_create")
// For Bugsnag test
// int* v = nullptr;
// *v = 0;
processCreateSession(d, answer); processCreateSession(d, answer);
} else
else if (cmd == "session_start") if (cmd == "session_start")
processStartSession(d, answer); processStartSession(d, answer);
else if (cmd == "session_stop") else
if (cmd == "session_stop")
processStopSession(d, answer); processStopSession(d, answer);
else if (cmd == "session_accept") else
if (cmd == "session_accept")
processAcceptSession(d, answer); processAcceptSession(d, answer);
else if (cmd == "session_destroy") else
if (cmd == "session_destroy")
processDestroySession(d, answer); processDestroySession(d, answer);
else if (cmd == "session_use_stream") else
if (cmd == "session_use_stream")
processUseStreamForSession(d, answer); processUseStreamForSession(d, answer);
else if (cmd == "wait_for_event") else
if (cmd == "wait_for_event")
processWaitForEvent(d, answer); processWaitForEvent(d, answer);
else if (cmd == "session_get_media_stats") else
if (cmd == "session_get_media_stats")
processGetMediaStats(d, answer); processGetMediaStats(d, answer);
else if (cmd == "agent_network_changed") else
if (cmd == "agent_network_changed")
processNetworkChanged(d, answer); processNetworkChanged(d, answer);
else if (cmd == "agent_add_root_cert") else
if (cmd == "agent_add_root_cert")
processAddRootCert(d, answer); processAddRootCert(d, answer);
else if (cmd == "detach_log") else
if (cmd == "detach_log")
{ {
GLogger.closeFile(); GLogger.closeFile();
answer["status"] = Status_Ok; answer["status"] = Status_Ok;
} }
else if (cmd == "attach_log") else
if (cmd == "attach_log")
{ {
GLogger.openFile(); GLogger.openFile();
answer["status"] = Status_Ok; answer["status"] = Status_Ok;
} }
else if (cmd == "log_message") else
if (cmd == "log_message")
processLogMessage(d, answer); processLogMessage(d, answer);
else else
{ {
answer["status"] = Status_NoCommand; answer["status"] = Status_NoCommand;
} }
} }
catch (std::exception& e) catch(std::exception& e)
{ {
answer["status"] = e.what(); answer["status"] = e.what();
} }
std::string result = answer.toStyledString(); return answer.toStyledString();
return result;
} }
bool AgentImpl::waitForData(int /*milliseconds*/) bool AgentImpl::waitForData(int /*milliseconds*/)
@@ -154,37 +152,49 @@ std::string AgentImpl::read()
return ""; return "";
} }
void AgentImpl::processConfig(JsonCpp::Value& d, JsonCpp::Value& answer) void AgentImpl::processConfig(JsonCpp::Value &d, JsonCpp::Value &answer)
{ {
std::unique_lock<std::recursive_mutex> l(mAgentMutex); std::unique_lock<std::recursive_mutex> l(mAgentMutex);
std::string transport = d["transport"].asString(); #if !defined(TARGET_ANDROID) && defined(USE_PVQA_LIBRARY)
config()[CONFIG_TRANSPORT] = // It works for desktop OSes only
(transport == "any") // Because Android requires special initializing procedure (valid JNI environment context)
? TransportType_Any std::string pvqaLicense = d["pvqa-license"].asString(), pvqaConfig = d["pvqa-config"].asString();
: (transport == "udp" ? TransportType_Udp : (transport == "tcp" ? TransportType_Tcp : TransportType_Tls)); if (!pvqaLicense.empty() && !pvqaConfig.empty())
sevana::pvqa::initialize(pvqaLicense, pvqaConfig);
#endif
#if !defined(TARGET_ANDROID) && defined(USE_AQUA_LIBRARY)
std::string aquaLicense = d["aqua-license"].asString();
if (!aquaLicense.empty())
sevana::aqua::initialize(aquaLicense.c_str(), aquaLicense.size());
#endif
std::string transport = d["transport"].asString();
config()[CONFIG_TRANSPORT] = (transport == "any") ? TransportType_Any : (transport == "udp" ? TransportType_Udp : (transport == "tcp" ? TransportType_Tcp : TransportType_Tls));
config()[CONFIG_IPV4] = d["ipv4"].asBool(); config()[CONFIG_IPV4] = d["ipv4"].asBool();
config()[CONFIG_IPV6] = d["ipv6"].asBool(); config()[CONFIG_IPV6] = d["ipv6"].asBool();
if (transport == "tls")
config()[CONFIG_SIPS] = true;
// Log file // Log file
std::string logfile = d["logfile"].asString(); std::string logfile = d["logfile"].asString();
ice::Logger& logger = ice::GLogger; ice::Logger& logger = ice::GLogger;
logger.useFile(logfile.empty() ? nullptr : logfile.c_str()); logger.useFile(logfile.empty() ? nullptr : logfile.c_str());
config()[CONFIG_MULTIPLEXING] = true; config()[CONFIG_MULTIPLEXING] = true;
config()[CONFIG_DISPLAYNAME] = "Voip quality tester"; config()[CONFIG_DISPLAYNAME] = "Voip quality tester";
config()[CONFIG_USERAGENT] = d["useragent"].asString();
mUseNativeAudio = d["nativeaudio"].asBool(); mUseNativeAudio = d["nativeaudio"].asBool();
config()[CONFIG_OWN_DNS] = d["dns_servers"].asString(); config()[CONFIG_OWN_DNS] = d["dns_servers"].asString();
config()[CONFIG_SIPS] = d["secure"].asBool() || transport == "tls"; config()[CONFIG_SIPS] = d["secure"].asBool();
config()[CONFIG_STUNSERVER_IP] = d["stun_server"].asString();
answer["status"] = Status_Ok; answer["status"] = Status_Ok;
} }
void AgentImpl::processStart(JsonCpp::Value& request, JsonCpp::Value& answer) void AgentImpl::processStart(JsonCpp::Value& request, JsonCpp::Value &answer)
{ {
std::unique_lock<std::recursive_mutex> l(mAgentMutex); std::unique_lock<std::recursive_mutex> l(mAgentMutex);
if (mThread) if (mThread)
@@ -200,25 +210,28 @@ void AgentImpl::processStart(JsonCpp::Value& request, JsonCpp::Value& answer)
SocketHeap::instance().start(); SocketHeap::instance().start();
// Initialize terminal // Initialize terminal
auto settings = MT::CodecList::Settings::getClientSettings(); MT::CodecList::Settings settings;
mTerminal = std::make_shared<MT::Terminal>(settings); mTerminal = std::make_shared<MT::Terminal>(settings);
// Enable/disable codecs // Enable/disable codecs
PVariantMap priorityConfig = std::make_shared<VariantMap>(); PVariantMap priorityConfig = std::make_shared<VariantMap>();
MT::CodecList& cl = mTerminal->codeclist(); MT::CodecList& cl = mTerminal->codeclist();
for (int i = 0; i < cl.count(); i++) for (int i=0; i<cl.count(); i++)
priorityConfig->at(i) = i; priorityConfig->at(i) = i;
// Disable dynamic payload codec types - commented for now
/*if (cl.codecAt(i).payloadType() < 96)
priorityConfig->at(i) = i;
else
priorityConfig->at(i) = -1;*/
config()[CONFIG_CODEC_PRIORITY] = priorityConfig; config()[CONFIG_CODEC_PRIORITY] = priorityConfig;
// Enable audio // Enable audio
mAudioManager = std::make_shared<AudioManager>(); mAudioManager = std::make_shared<AudioManager>();
mAudioManager->setTerminal(mTerminal.get()); mAudioManager->setTerminal(mTerminal.get());
if (mAudioMonitoring)
mAudioManager->setAudioMonitoring(mAudioMonitoring);
// Do not start audio manager here. Start right before call. // Do not start here. Start right before call.
// Initialize endpoint // Initialize endpoint
start(); start();
@@ -235,19 +248,17 @@ void AgentImpl::processStop(JsonCpp::Value& /*request*/, JsonCpp::Value& answer)
answer["status"] = Status_Ok; answer["status"] = Status_Ok;
} }
void AgentImpl::processCreateAccount(JsonCpp::Value& d, JsonCpp::Value& answer) void AgentImpl::processCreateAccount(JsonCpp::Value &d, JsonCpp::Value& answer)
{ {
std::unique_lock<std::recursive_mutex> l(mAgentMutex); std::unique_lock<std::recursive_mutex> l(mAgentMutex);
PVariantMap c = std::make_shared<VariantMap>(); PVariantMap c = std::make_shared<VariantMap>();
(*c)[CONFIG_USERNAME] = d["username"].asString(); (*c)[CONFIG_USERNAME] = d["username"].asString();
(*c)[CONFIG_PASSWORD] = d["password"].asString(); (*c)[CONFIG_PASSWORD] = d["password"].asString();
(*c)[CONFIG_DOMAIN] = d["domain"].asString(); (*c)[CONFIG_DOMAIN] = d["domain"].asString();
if (d.isMember("domain_port"))
(*c)[CONFIG_DOMAINPORT] = d["domain_port"].asInt();
(*c)[CONFIG_EXTERNALIP] = d["use_external_ip"].asBool(); (*c)[CONFIG_EXTERNALIP] = d["use_external_ip"].asBool();
auto nameAndPort = strx::parseHost(d["stun_server"].asString(), 3478); auto nameAndPort = StringHelper::parseHost(d["stun_server"].asString(), 3478);
(*c)[CONFIG_STUNSERVER_NAME] = nameAndPort.first; (*c)[CONFIG_STUNSERVER_NAME] = nameAndPort.first;
(*c)[CONFIG_STUNSERVER_PORT] = nameAndPort.second; (*c)[CONFIG_STUNSERVER_PORT] = nameAndPort.second;
@@ -261,7 +272,7 @@ void AgentImpl::processStartAccount(JsonCpp::Value& request, JsonCpp::Value& ans
{ {
std::unique_lock<std::recursive_mutex> l(mAgentMutex); std::unique_lock<std::recursive_mutex> l(mAgentMutex);
// Locate account in map // Locate account in map
auto accountIter = mAccountMap.find(request["account_id"].asInt()); auto accountIter = mAccountMap.find(request["account_id"].asInt());
if (accountIter != mAccountMap.end()) if (accountIter != mAccountMap.end())
{ {
accountIter->second->start(); accountIter->second->start();
@@ -271,17 +282,17 @@ void AgentImpl::processStartAccount(JsonCpp::Value& request, JsonCpp::Value& ans
answer["status"] = Status_AccountNotFound; answer["status"] = Status_AccountNotFound;
} }
void AgentImpl::processSetUserInfoToAccount(JsonCpp::Value& request, JsonCpp::Value& answer) void AgentImpl::processSetUserInfoToAccount(JsonCpp::Value &request, JsonCpp::Value &answer)
{ {
std::unique_lock<std::recursive_mutex> l(mAgentMutex); std::unique_lock<std::recursive_mutex> l(mAgentMutex);
// Locate account in map // Locate account in map
auto accountIter = mAccountMap.find(request["account_id"].asInt()); auto accountIter = mAccountMap.find(request["account_id"].asInt());
if (accountIter != mAccountMap.end()) if (accountIter != mAccountMap.end())
{ {
Account::UserInfo info; Account::UserInfo info;
JsonCpp::Value& arg = request["userinfo"]; JsonCpp::Value& arg = request["userinfo"];
std::vector<std::string> keys = arg.getMemberNames(); std::vector<std::string> keys = arg.getMemberNames();
for (const std::string& k : keys) for (const std::string& k: keys)
info[k] = arg[k].asString(); info[k] = arg[k].asString();
accountIter->second->setUserInfo(info); accountIter->second->setUserInfo(info);
@@ -291,10 +302,10 @@ void AgentImpl::processSetUserInfoToAccount(JsonCpp::Value& request, JsonCpp::Va
answer["status"] = Status_AccountNotFound; answer["status"] = Status_AccountNotFound;
} }
void AgentImpl::processCreateSession(JsonCpp::Value& request, JsonCpp::Value& answer) void AgentImpl::processCreateSession(JsonCpp::Value &request, JsonCpp::Value &answer)
{ {
std::unique_lock<std::recursive_mutex> l(mAgentMutex); std::unique_lock<std::recursive_mutex> l(mAgentMutex);
auto accountIter = mAccountMap.find(request["account_id"].asInt()); auto accountIter = mAccountMap.find(request["account_id"].asInt());
if (accountIter != mAccountMap.end()) if (accountIter != mAccountMap.end())
{ {
PSession session = createSession(accountIter->second); PSession session = createSession(accountIter->second);
@@ -315,27 +326,21 @@ void AgentImpl::processStartSession(JsonCpp::Value& request, JsonCpp::Value& ans
{ {
// Agent was not started // Agent was not started
ICELogError(<< "No audio manager installed."); ICELogError(<< "No audio manager installed.");
answer["status"] = Status_NoAudioManager; answer["status"] = "Audio manager not started. Most probably agent is not started.";
return; return;
} }
if (request["use_null_mic"].asBool())
mAudioManager->setAudioInput(std::make_shared<Audio::NullInputDevice>());
if (request["use_null_spk"].asBool())
mAudioManager->setAudioOutput(std::make_shared<Audio::NullOutputDevice>());
mAudioManager->start(mUseNativeAudio ? AudioManager::atReceiver : AudioManager::atNull); mAudioManager->start(mUseNativeAudio ? AudioManager::atReceiver : AudioManager::atNull);
auto sessionIter = mSessionMap.find(request["session_id"].asInt()); auto sessionIter = mSessionMap.find(request["session_id"].asInt());
if (sessionIter != mSessionMap.end()) if (sessionIter != mSessionMap.end())
{ {
// Ensure audio provider is here // Ensure audio provider is here
PSession session = sessionIter->second; PSession session = sessionIter->second;
PDataProvider audioProvider = std::make_shared<AudioProvider>(*this, *mTerminal); PDataProvider audioProvider = std::make_shared<AudioProvider>(*this, *mTerminal);
audioProvider->setState(audioProvider->state() | static_cast<int>(StreamState::Grabbing) | audioProvider->setState(audioProvider->state() | static_cast<int>(StreamState::Grabbing) | static_cast<int>(StreamState::Playing));
static_cast<int>(StreamState::Playing));
/*#if defined(USE_AQUA_LIBRARY) #if defined(USE_AQUA_LIBRARY)
std::string path_faults = request["path_faults"].asString(); std::string path_faults = request["path_faults"].asString();
sevana::aqua::config config = { sevana::aqua::config config = {
@@ -358,10 +363,9 @@ void AgentImpl::processStartSession(JsonCpp::Value& request, JsonCpp::Value& ans
{ "specp", "32"} { "specp", "32"}
}; };
// std::string config = "-avlp on -smtnrm on -decor off -mprio off -npnt auto -voip off -enorm off -g711 on // std::string config = "-avlp on -smtnrm on -decor off -mprio off -npnt auto -voip off -enorm off -g711 on -spfrcor off -grad off -tmc on -miter 1 -trim a 10 -output json";
-spfrcor off -grad off -tmc on -miter 1 -trim a 10 -output json"; /*if (temp_path.size())
// if (temp_path.size()) config += " -fau " + temp_path; */
// config += " -fau " + temp_path;
auto qc = std::make_shared<sevana::aqua>(); auto qc = std::make_shared<sevana::aqua>();
if (!qc->is_open()) if (!qc->is_open())
@@ -374,15 +378,14 @@ void AgentImpl::processStartSession(JsonCpp::Value& request, JsonCpp::Value& ans
mAquaMap[sessionIter->first] = qc; mAquaMap[sessionIter->first] = qc;
dynamic_cast<AudioProvider*>(audioProvider.get())->configureMediaObserver(this, (void*)qc.get()); dynamic_cast<AudioProvider*>(audioProvider.get())->configureMediaObserver(this, (void*)qc.get());
#endif #endif
*/
// TODO: support SRTP via StreamState::Srtp option in audio provider state // TODO: support SRTP via StreamState::Srtp option in audio provider state
// Get user headers // Get user headers
Session::UserHeaders info; Session::UserHeaders info;
JsonCpp::Value& arg = request["userinfo"]; JsonCpp::Value& arg = request["userinfo"];
std::vector<std::string> keys = arg.getMemberNames(); std::vector<std::string> keys = arg.getMemberNames();
for (const std::string& k : keys) for (const std::string& k: keys)
info[k] = arg[k].asString(); info[k] = arg[k].asString();
session->setUserHeaders(info); session->setUserHeaders(info);
@@ -400,7 +403,7 @@ void AgentImpl::processStopSession(JsonCpp::Value& request, JsonCpp::Value& answ
{ {
std::unique_lock<std::recursive_mutex> l(mAgentMutex); std::unique_lock<std::recursive_mutex> l(mAgentMutex);
auto sessionIter = mSessionMap.find(request["session_id"].asInt()); auto sessionIter = mSessionMap.find(request["session_id"].asInt());
if (sessionIter != mSessionMap.end()) if (sessionIter != mSessionMap.end())
{ {
PSession session = sessionIter->second; PSession session = sessionIter->second;
@@ -414,72 +417,58 @@ void AgentImpl::processStopSession(JsonCpp::Value& request, JsonCpp::Value& answ
void AgentImpl::processAcceptSession(JsonCpp::Value& request, JsonCpp::Value& answer) void AgentImpl::processAcceptSession(JsonCpp::Value& request, JsonCpp::Value& answer)
{ {
std::unique_lock<std::recursive_mutex> l(mAgentMutex); std::unique_lock<std::recursive_mutex> l(mAgentMutex);
auto sessionIter = mSessionMap.find(request["session_id"].asInt()); auto sessionIter = mSessionMap.find(request["session_id"].asInt());
if (sessionIter != mSessionMap.end()) if (sessionIter != mSessionMap.end())
{ {
if (!mAudioManager) // Ensure audio manager is here
{ mAudioManager->start(mUseNativeAudio ? AudioManager::atReceiver : AudioManager::atNull);
ICELogError(<< "No audio manager installed.");
answer["status"] = Status_NoAudioManager;
}
else
{
// Ensure audio manager is here
if (request["use_null_mic"].asBool())
mAudioManager->setAudioInput(std::make_shared<Audio::NullInputDevice>());
if (request["use_null_spk"].asBool())
mAudioManager->setAudioOutput(std::make_shared<Audio::NullOutputDevice>());
mAudioManager->start(mUseNativeAudio ? AudioManager::atReceiver : AudioManager::atNull);
// Accept session on SIP level // Accept session on SIP level
PSession session = sessionIter->second; PSession session = sessionIter->second;
// Get user headers // Get user headers
Session::UserHeaders info; Session::UserHeaders info;
JsonCpp::Value& arg = request["userinfo"]; JsonCpp::Value& arg = request["userinfo"];
std::vector<std::string> keys = arg.getMemberNames(); std::vector<std::string> keys = arg.getMemberNames();
for (const std::string& k : keys) for (const std::string& k: keys)
info[k] = arg[k].asString(); info[k] = arg[k].asString();
session->setUserHeaders(info); session->setUserHeaders(info);
// Accept finally // Accept finally
session->accept(); session->accept();
answer["status"] = Status_Ok; answer["status"] = Status_Ok;
}
} }
else else
answer["status"] = Status_SessionNotFound; answer["status"] = Status_SessionNotFound;
} }
void AgentImpl::processDestroySession(JsonCpp::Value& request, JsonCpp::Value& answer) void AgentImpl::processDestroySession(JsonCpp::Value& request, JsonCpp::Value& answer)
{ {
std::unique_lock<std::recursive_mutex> l(mAgentMutex); std::unique_lock<std::recursive_mutex> l(mAgentMutex);
int sessionId = request["session_id"].asInt(); int sessionId = request["session_id"].asInt();
auto sessionIter = mSessionMap.find(sessionId); auto sessionIter = mSessionMap.find(sessionId);
if (sessionIter != mSessionMap.end()) if (sessionIter != mSessionMap.end())
mSessionMap.erase(sessionIter); mSessionMap.erase(sessionIter);
// #if defined(USE_AQUA_LIBRARY) #if defined(USE_AQUA_LIBRARY)
// closeAqua(sessionId); closeAqua(sessionId);
// #endif #endif
answer["status"] = Status_Ok; answer["status"] = Status_Ok;
} }
void AgentImpl::processWaitForEvent(JsonCpp::Value& request, JsonCpp::Value& answer) void AgentImpl::processWaitForEvent(JsonCpp::Value &request, JsonCpp::Value &answer)
{ {
// Deliberately does NOT take mAgentMutex: events are produced by the worker std::unique_lock<std::recursive_mutex> l(mAgentMutex);
// thread inside process(), which needs mAgentMutex. Holding it here would
// stall all SIP/media processing for the whole timeout and guarantee that
// the awaited event can never arrive during the wait.
int timeout = 0; //int x = 0;
if (request.isMember("timeout")) //int y = 1/x;
timeout = request["timeout"].asInt();
int timeout = request["timeout"].asInt();
std::unique_lock<std::mutex> eventLock(mEventListMutex); std::unique_lock<std::mutex> eventLock(mEventListMutex);
mEventListChangeCondVar.wait_for(eventLock, chrono::milliseconds(timeout), if (mEventList.empty())
[this]() { return !mEventList.empty(); }); mEventListChangeCondVar.wait_for(eventLock, chrono::milliseconds(timeout));
if (!mEventList.empty()) if (!mEventList.empty())
{ {
@@ -489,23 +478,70 @@ void AgentImpl::processWaitForEvent(JsonCpp::Value& request, JsonCpp::Value& ans
answer["status"] = Status_Ok; answer["status"] = Status_Ok;
} }
#if defined(USE_PVQA_LIBRARY)
static JsonCpp::Value CsvReportToJson(const std::string& report)
{
JsonCpp::Value detectorValues;
std::istringstream iss(report);
CsvReader reader(iss);
std::vector<std::string> cells;
if (reader.readLine(cells))
{
JsonCpp::Value detectorNames;
for (size_t nameIndex = 0; nameIndex < cells.size(); nameIndex++)
detectorNames[static_cast<int>(nameIndex)] = StringHelper::trim(cells[nameIndex]);
// Put first line name of columns
detectorValues[0] = detectorNames;
int rowIndex = 1;
while (reader.readLine(cells))
{
// Skip last column for now
JsonCpp::Value row;
for (size_t valueIndex = 0; valueIndex < cells.size(); valueIndex++)
{
bool isFloat = true;
float v = StringHelper::toFloat(cells[valueIndex], 0.0, &isFloat);
if (isFloat)
row[static_cast<int>(valueIndex)] = static_cast<double>(v);
else
row[static_cast<int>(valueIndex)] = cells[valueIndex];
}
detectorValues[rowIndex++] = row;
}
}
return detectorValues;
}
#endif
void AgentImpl::processGetMediaStats(JsonCpp::Value& request, JsonCpp::Value& answer) void AgentImpl::processGetMediaStats(JsonCpp::Value& request, JsonCpp::Value& answer)
{ {
std::unique_lock<std::recursive_mutex> l(mAgentMutex); std::unique_lock<std::recursive_mutex> l(mAgentMutex);
int sessionId = request["session_id"].asInt(); int sessionId = request["session_id"].asInt();
SessionMap::iterator sessionIter = mSessionMap.find(sessionId); SessionMap::iterator sessionIter = mSessionMap.find(sessionId);
if (sessionIter != mSessionMap.end()) if (sessionIter != mSessionMap.end())
{ {
PSession session = sessionIter->second; PSession session = sessionIter->second;
VariantMap result; VariantMap result;
session->getSessionInfo(Session::InfoOptions::Detailed, result); bool includePvqa = request["include_pvqa"].asBool();
#if defined(USE_AQUA_LIBRARY)
bool includeAqua = request["include_aqua"].asBool();
std::string aquaReference = request["aqua_reference_audio"].asString();
#endif
session->getSessionInfo(includePvqa ? Session::InfoOptions::Detailed : Session::InfoOptions::Standard,
result);
if (result.exists(SessionInfo_AudioCodec)) if (result.exists(SessionInfo_AudioCodec))
answer["codec"] = result[SessionInfo_AudioCodec].asStdString(); answer["codec"] = result[SessionInfo_AudioCodec].asStdString();
if (result.exists(SessionInfo_NetworkMos)) if (result.exists(SessionInfo_NetworkMos))
answer["network_mos"] = result[SessionInfo_NetworkMos].asFloat(); answer["network_mos"] = result[SessionInfo_NetworkMos].asFloat();
if (result.exists(SessionInfo_LostRtp)) #if defined(USE_PVQA_LIBRARY)
if (result.exists(SessionInfo_PvqaMos))
answer["pvqa_mos"] = result[SessionInfo_PvqaMos].asFloat();
if (result.exists(SessionInfo_PvqaReport))
answer["pvqa_report"] = CsvReportToJson(result[SessionInfo_PvqaReport].asStdString());
#endif
if (result.exists(SessionInfo_PacketLoss))
answer["rtp_lost"] = result[SessionInfo_LostRtp].asInt(); answer["rtp_lost"] = result[SessionInfo_LostRtp].asInt();
if (result.exists(SessionInfo_DroppedRtp)) if (result.exists(SessionInfo_DroppedRtp))
answer["rtp_dropped"] = result[SessionInfo_DroppedRtp].asInt(); answer["rtp_dropped"] = result[SessionInfo_DroppedRtp].asInt();
@@ -514,9 +550,6 @@ void AgentImpl::processGetMediaStats(JsonCpp::Value& request, JsonCpp::Value& an
answer["rtp_sent"] = result[SessionInfo_SentRtp].asInt(); answer["rtp_sent"] = result[SessionInfo_SentRtp].asInt();
if (result.exists(SessionInfo_ReceivedRtp)) if (result.exists(SessionInfo_ReceivedRtp))
answer["rtp_received"] = result[SessionInfo_ReceivedRtp].asInt(); answer["rtp_received"] = result[SessionInfo_ReceivedRtp].asInt();
// Raw byte total; consumers divide by rtp_received for the average packet size.
if (result.exists(SessionInfo_ReceivedRtpTraffic))
answer["rtp_received_bytes"] = result[SessionInfo_ReceivedRtpTraffic].asInt();
if (result.exists(SessionInfo_Duration)) if (result.exists(SessionInfo_Duration))
answer["duration"] = result[SessionInfo_Duration].asInt(); answer["duration"] = result[SessionInfo_Duration].asInt();
if (result.exists(SessionInfo_Jitter)) if (result.exists(SessionInfo_Jitter))
@@ -525,13 +558,79 @@ void AgentImpl::processGetMediaStats(JsonCpp::Value& request, JsonCpp::Value& an
answer["rtt"] = result[SessionInfo_Rtt].asFloat(); answer["rtt"] = result[SessionInfo_Rtt].asFloat();
if (result.exists(SessionInfo_BitrateSwitchCounter)) if (result.exists(SessionInfo_BitrateSwitchCounter))
answer["bitrate_switch_counter"] = result[SessionInfo_BitrateSwitchCounter].asInt(); answer["bitrate_switch_counter"] = result[SessionInfo_BitrateSwitchCounter].asInt();
if (result.exists(SessionInfo_CngCounter))
answer["cng_counter"] = result[SessionInfo_CngCounter].asInt();
if (result.exists(SessionInfo_SSRC)) if (result.exists(SessionInfo_SSRC))
answer["rtp_ssrc"] = result[SessionInfo_SSRC].asInt(); answer["rtp_ssrc"] = result[SessionInfo_SSRC].asInt();
if (result.exists(SessionInfo_RemotePeer)) if (result.exists(SessionInfo_RemotePeer))
answer["rtp_remotepeer"] = result[SessionInfo_RemotePeer].asStdString(); answer["rtp_remotepeer"] = result[SessionInfo_RemotePeer].asStdString();
#if defined(USE_AQUA_LIBRARY)
if (includeAqua)
{
answer["incoming_audio"] = mAquaIncoming.hexstring();
answer["incoming_audio_samplerate"] = AUDIO_SAMPLERATE;
answer["incoming_audio_channels"] = AUDIO_CHANNELS;
ICELogInfo(<< "Running AQuA analyzer.");
ByteBuffer referenceAudio;
// Read AQuA reference audio from file if available
if (aquaReference.empty())
{
ICELogCritical(<< "AQuA reference audio file is not set, skipping analyzing.");
}
else {
auto sa = mAquaMap[sessionIter->first];
if (sa) {
Audio::WavFileReader reader;
reader.open(StringHelper::makeTstring(aquaReference));
if (reader.isOpened()) {
char buffer[1024];
int wasRead = 0;
do {
wasRead = reader.read(buffer, 1024);
if (wasRead > 0)
referenceAudio.appendBuffer(buffer, wasRead);
} while (wasRead == 1024);
}
else {
ICELogCritical(<< "Failed to read AQuA reference audio, error code: " << reader.lastError());
}
sevana::aqua::audio_buffer test(mAquaIncoming.data(), mAquaIncoming.size()),
reference(referenceAudio.data(), referenceAudio.size());
test.mRate = AUDIO_SAMPLERATE;
reference.mRate = AUDIO_SAMPLERATE;
test.mChannels = AUDIO_CHANNELS;
reference.mChannels = AUDIO_CHANNELS;
ICELogInfo(
<< "Comparing test audio " << mAquaIncoming.size() << " bytes with reference audio " << referenceAudio.size() << " bytes.");
auto r = sa->compare(reference, test);
if (r.mErrorCode) {
ICELogInfo(
<< "Error code: " << r.mErrorCode << ", msg: " << r.mErrorMessage);
} else {
ICELogInfo(<< "MOS: " << r.mMos << ", faults: " << r.mFaultsText);
}
answer["aqua_mos"] = r.mMos;
answer["aqua_report"] = r.mFaultsText;
/*std::string aqua_audio_text;
if (Base64::Encode(std::string(reinterpret_cast<const char*>(mAquaIncoming.data()), mAquaIncoming.size()), &aqua_audio_text))
{
answer["aqua_audio"] = aqua_audio_text;
}*/
if (r.mErrorCode) {
answer["aqua_error_code"] = r.mErrorCode;
answer["aqua_error_message"] = r.mErrorMessage;
}
closeAqua(sessionIter->first);
}
}
// Remove test audio
mAquaIncoming.clear(); mAquaOutgoing.clear();
}
#endif
answer["status"] = Status_Ok; answer["status"] = Status_Ok;
} }
else else
@@ -546,32 +645,32 @@ void AgentImpl::processNetworkChanged(JsonCpp::Value& /*request*/, JsonCpp::Valu
const std::string BeginCertificate = "-----BEGIN CERTIFICATE-----"; const std::string BeginCertificate = "-----BEGIN CERTIFICATE-----";
const std::string EndCertificate = "-----END CERTIFICATE-----"; const std::string EndCertificate = "-----END CERTIFICATE-----";
void AgentImpl::processAddRootCert(JsonCpp::Value& request, JsonCpp::Value& answer) void AgentImpl::processAddRootCert(JsonCpp::Value& request, JsonCpp::Value& answer)
{ {
std::unique_lock<std::recursive_mutex> l(mAgentMutex); std::unique_lock<std::recursive_mutex> l(mAgentMutex);
std::string pem = request["cert"].asString(); std::string pem = request["cert"].asString();
std::string::size_type pb = 0, pe = 0; std::string::size_type pb = 0, pe = 0;
while (pb != std::string::npos && pe != std::string::npos)
for(pb = pem.find(BeginCertificate, pb), pe = pem.find(EndCertificate, pe);
pb != std::string::npos && pe != std::string::npos;
pb = pem.find(BeginCertificate, pb + BeginCertificate.size()), pe = pem.find(EndCertificate, pe + EndCertificate.size()))
{ {
pb = pem.find(BeginCertificate, pb); // Get single certificate
pe = pem.find(EndCertificate, pe); std::string cert = pem.substr(pb, pe + EndCertificate.size());
//int size = cert.size();
addRootCert(ByteBuffer(cert.c_str(), cert.size()));
if (pb != std::string::npos && pe != std::string::npos && pe > pb) // Delete processed part
{ pem.erase(0, pe + EndCertificate.size());
std::string cert = pem.substr(pb, pe - pb + EndCertificate.size());
addRootCert(ByteBuffer(cert.c_str(), cert.size()));
pb = ++pe;
}
} }
answer["status"] = Status_Ok; answer["status"] = Status_Ok;
} }
void AgentImpl::processLogMessage(JsonCpp::Value& request, JsonCpp::Value& answer) void AgentImpl::processLogMessage(JsonCpp::Value &request, JsonCpp::Value &answer)
{ {
int level = request["level"].asInt(); int level = request["level"].asInt();
std::string message = request["message"].asString(); std::string message = request["message"].asString();
ICELog(static_cast<ice::LogLevel>(level), "App", << message); ICELog(static_cast<ice::LogLevel>(level), "App", << message);
@@ -588,8 +687,7 @@ void AgentImpl::stopAgentAndThread()
stop(); stop();
} }
catch (...) catch (...)
{ {}
}
// Stop worker thread // Stop worker thread
if (mThread) if (mThread)
@@ -623,19 +721,19 @@ void AgentImpl::stopAgentAndThread()
void AgentImpl::processUseStreamForSession(JsonCpp::Value& request, JsonCpp::Value& answer) void AgentImpl::processUseStreamForSession(JsonCpp::Value& request, JsonCpp::Value& answer)
{ {
std::unique_lock<std::recursive_mutex> l(mAgentMutex); std::unique_lock<std::recursive_mutex> l(mAgentMutex);
SessionMap::iterator sessionIter = mSessionMap.find(request["session_id"].asInt()); SessionMap::iterator sessionIter = mSessionMap.find(request["session_id"].asInt());
if (sessionIter != mSessionMap.end()) if (sessionIter != mSessionMap.end())
{ {
// Extract ptr to session // Extract ptr to session
PSession session = sessionIter->second; PSession session = sessionIter->second;
// Parse command // Parse command
std::string actionText = request["media_action"].asString(), std::string actionText = request["media_action"].asString(),
directionText = request["media_direction"].asString(); directionText = request["media_direction"].asString();
MT::Stream::MediaDirection direction = MT::Stream::MediaDirection direction = directionText == "incoming" ? MT::Stream::MediaDirection::Incoming
directionText == "incoming" ? MT::Stream::MediaDirection::Incoming : MT::Stream::MediaDirection::Outgoing; : MT::Stream::MediaDirection::Outgoing;
std::string path = request["path"].asString(); std::string path = request["path"].asString();
// Try to open file // Try to open file
AudioProvider* prov = session->findProviderForActiveAudio(); AudioProvider* prov = session->findProviderForActiveAudio();
@@ -652,7 +750,7 @@ void AgentImpl::processUseStreamForSession(JsonCpp::Value& request, JsonCpp::Val
else else
{ {
Audio::PWavFileReader reader = std::make_shared<Audio::WavFileReader>(); Audio::PWavFileReader reader = std::make_shared<Audio::WavFileReader>();
if (!reader->open(strx::makeTstring(path))) if (!reader->open(StringHelper::makeTstring(path)))
answer["status"] = Status_FailedToOpenFile; answer["status"] = Status_FailedToOpenFile;
else else
{ {
@@ -661,64 +759,72 @@ void AgentImpl::processUseStreamForSession(JsonCpp::Value& request, JsonCpp::Val
} }
} }
} }
else if (actionText == "write") else
{ if (actionText == "write")
if (path.empty())
{ {
// Turn off recording from the stream if (path.empty())
prov->writeFile(Audio::PWavFileWriter(), direction);
answer["status"] = Status_Ok;
}
else
{
Audio::PWavFileWriter writer = std::make_shared<Audio::WavFileWriter>();
if (!writer->open(strx::makeTstring(path), AUDIO_SAMPLERATE, AUDIO_CHANNELS))
answer["status"] = Status_FailedToOpenFile;
else
{ {
prov->writeFile(writer, direction); // Turn off recording from the stream
prov->writeFile(Audio::PWavFileWriter(), direction);
answer["status"] = Status_Ok; answer["status"] = Status_Ok;
} }
else
{
Audio::PWavFileWriter writer = std::make_shared<Audio::WavFileWriter>();
if (!writer->open(StringHelper::makeTstring(path), AUDIO_SAMPLERATE, AUDIO_CHANNELS))
answer["status"] = Status_FailedToOpenFile;
else
{
prov->writeFile(writer, direction);
answer["status"] = Status_Ok;
}
}
} }
} else
else if (actionText == "mirror") if (actionText == "mirror")
{ {
prov->setupMirror(request["enable"].asBool()); prov->setupMirror(request["enable"].asBool());
answer["status"] = Status_Ok; answer["status"] = Status_Ok;
} }
else else
answer["status"] = Status_NoCommand; answer["status"] = Status_AccountNotFound;
} }
else else
answer["status"] = Status_NoMediaAction; answer["status"] = Status_NoMediaAction;
} }
} }
void AgentImpl::onMedia(const void* data, int length, MT::Stream::MediaDirection direction, void* context, #if defined(USE_AQUA_LIBRARY)
void* userTag) void AgentImpl::onMedia(const void* data, int length, MT::Stream::MediaDirection direction, void* context, void* userTag)
{ {
/*switch (direction) /* if (mIncomingAudioDump && direction == MT::Stream::MediaDirection::Incoming)
mIncomingAudioDump->write(data, length);
if (mOutgoingAudioDump && direction == MT::Stream::MediaDirection::Outgoing)
mOutgoingAudioDump->write(data, length);*/
// User tag points to accumulator object which includes
// auto* sa = reinterpret_cast<sevana::aqua*>(userTag);
switch (direction)
{ {
case MT::Stream::MediaDirection::Incoming: mAquaIncoming.appendBuffer(data, length); break; case MT::Stream::MediaDirection::Incoming: mAquaIncoming.appendBuffer(data, length); break;
case MT::Stream::MediaDirection::Outgoing: mAquaOutgoing.appendBuffer(data, length); break; case MT::Stream::MediaDirection::Outgoing: mAquaOutgoing.appendBuffer(data, length); break;
}*/ }
} }
#endif
// Called on new incoming session; providers shoukld // Called on new incoming session; providers shoukld
#define EVENT_WITH_NAME(X) \ #define EVENT_WITH_NAME(X) JsonCpp::Value v; v["event_name"] = X;
JsonCpp::Value v; \
v["event_name"] = X;
PDataProvider AgentImpl::onProviderNeeded(const std::string& name) PDataProvider AgentImpl::onProviderNeeded(const std::string& name)
{ {
assert(mTerminal);
EVENT_WITH_NAME("provider_needed"); EVENT_WITH_NAME("provider_needed");
v["provider_name"] = name; v["provider_name"] = name;
addEvent(v); addEvent(v);
return std::make_shared<AudioProvider>(*this, *mTerminal); return PDataProvider(new AudioProvider(*this, *mTerminal));
} }
// Called on new session offer // Called on new session offer
@@ -740,8 +846,7 @@ void AgentImpl::onSessionTerminated(PSession s, int responsecode, int reason)
if (mOutgoingAudioDump) if (mOutgoingAudioDump)
mOutgoingAudioDump->close(); mOutgoingAudioDump->close();
*/ */
if (mAudioManager) mAudioManager->stop(mUseNativeAudio ? AudioManager::atReceiver : AudioManager::atNull);
mAudioManager->stop(mUseNativeAudio ? AudioManager::atReceiver : AudioManager::atNull);
// Gather statistics before // Gather statistics before
EVENT_WITH_NAME("session_terminated"); EVENT_WITH_NAME("session_terminated");
v["session_id"] = s->id(); v["session_id"] = s->id();
@@ -846,7 +951,8 @@ void AgentImpl::onCheckFinished(PSession s, const char* description)
} }
// Called when log message must be recorded // Called when log message must be recorded
void AgentImpl::onLog(const char* /*msg*/) {} void AgentImpl::onLog(const char* /*msg*/)
{}
// Called when problem with SIP connection(s) detected // Called when problem with SIP connection(s) detected
void AgentImpl::onSipConnectionFailed() void AgentImpl::onSipConnectionFailed()
@@ -863,12 +969,12 @@ void AgentImpl::addEvent(const JsonCpp::Value& v)
} }
#if defined(USE_AQUA_LIBRARY) #if defined(USE_AQUA_LIBRARY)
/*void AgentImpl::closeAqua(int sessionId) void AgentImpl::closeAqua(int sessionId)
{ {
auto aquaIter = mAquaMap.find(sessionId); auto aquaIter = mAquaMap.find(sessionId);
if (aquaIter != mAquaMap.end()) { if (aquaIter != mAquaMap.end()) {
aquaIter->second->close(); aquaIter->second->close();
mAquaMap.erase(aquaIter); mAquaMap.erase(aquaIter);
} }
}*/ }
#endif #endif
+93 -83
View File
@@ -13,118 +13,128 @@
#include "Agent_AudioManager.h" #include "Agent_AudioManager.h"
#include <mutex> #include <mutex>
#include <condition_variable> #include <condition_variable>
#include <atomic> #if defined(USE_AQUA_LIBRARY)
# include "aqua++.h"
#endif
#if defined(USE_PVQA_LIBRARY)
# include "pvqa++.h"
#endif
class AgentImpl : public UserAgent, public MT::Stream::MediaObserver class AgentImpl: public UserAgent
#if defined(USE_AQUA_LIBRARY)
, public MT::Stream::MediaObserver
#endif
{ {
protected: protected:
std::recursive_mutex mAgentMutex; std::recursive_mutex mAgentMutex;
std::mutex mEventListMutex; std::mutex mEventListMutex;
std::condition_variable mEventListChangeCondVar; std::condition_variable mEventListChangeCondVar;
std::vector<JsonCpp::Value> mEventList; std::vector<JsonCpp::Value> mEventList;
bool mUseNativeAudio = false; bool mUseNativeAudio = false;
typedef std::map<int, PAccount> AccountMap; typedef std::map<int, PAccount> AccountMap;
AccountMap mAccountMap; AccountMap mAccountMap;
typedef std::map<int, PSession> SessionMap; typedef std::map<int, PSession> SessionMap;
SessionMap mSessionMap; SessionMap mSessionMap;
#if defined(USE_AQUA_LIBRARY)
// Keys are the same as used in mSessionMap
typedef std::map<int, std::shared_ptr<sevana::aqua>> AquaMap;
AquaMap mAquaMap;
ByteBuffer mAquaIncoming, mAquaOutgoing;
void closeAqua(int sessionId);
#endif
std::shared_ptr<std::thread> mThread; std::shared_ptr<std::thread> mThread;
std::atomic<bool> mShutdown; volatile bool mShutdown;
std::shared_ptr<MT::Terminal> mTerminal; std::shared_ptr<MT::Terminal> mTerminal;
std::shared_ptr<AudioManager> mAudioManager; std::shared_ptr<AudioManager> mAudioManager;
Audio::DataConnection* mAudioMonitoring = nullptr; //Audio::PWavFileWriter mIncomingAudioDump, mOutgoingAudioDump;
void run(); void run();
void addEvent(const JsonCpp::Value& v); void addEvent(const JsonCpp::Value& v);
void processConfig(JsonCpp::Value& request, JsonCpp::Value& answer); void processConfig(JsonCpp::Value& request, JsonCpp::Value& answer);
void processStart(JsonCpp::Value& request, JsonCpp::Value& answer); void processStart(JsonCpp::Value& request, JsonCpp::Value& answer);
void processStop(JsonCpp::Value& request, JsonCpp::Value& answer); void processStop(JsonCpp::Value& request, JsonCpp::Value& answer);
void processCreateAccount(JsonCpp::Value& request, JsonCpp::Value& answer); void processCreateAccount(JsonCpp::Value& request, JsonCpp::Value& answer);
void processStartAccount(JsonCpp::Value& request, JsonCpp::Value& answer); void processStartAccount(JsonCpp::Value& request, JsonCpp::Value& answer);
void processSetUserInfoToAccount(JsonCpp::Value& request, JsonCpp::Value& answer); void processSetUserInfoToAccount(JsonCpp::Value& request, JsonCpp::Value& answer);
void processCreateSession(JsonCpp::Value& request, JsonCpp::Value& answer); void processCreateSession(JsonCpp::Value& request, JsonCpp::Value& answer);
void processStartSession(JsonCpp::Value& request, JsonCpp::Value& answer); void processStartSession(JsonCpp::Value& request, JsonCpp::Value& answer);
void processStopSession(JsonCpp::Value& request, JsonCpp::Value& answer); void processStopSession(JsonCpp::Value& request, JsonCpp::Value& answer);
void processAcceptSession(JsonCpp::Value& request, JsonCpp::Value& answer); void processAcceptSession(JsonCpp::Value& request, JsonCpp::Value& answer);
void processDestroySession(JsonCpp::Value& request, JsonCpp::Value& answer); void processDestroySession(JsonCpp::Value& request, JsonCpp::Value& answer);
void processWaitForEvent(JsonCpp::Value& request, JsonCpp::Value& answer); void processWaitForEvent(JsonCpp::Value& request, JsonCpp::Value& answer);
void processGetMediaStats(JsonCpp::Value& request, JsonCpp::Value& answer); void processGetMediaStats(JsonCpp::Value& request, JsonCpp::Value& answer);
void processUseStreamForSession(JsonCpp::Value& request, JsonCpp::Value& answer); void processUseStreamForSession(JsonCpp::Value& request, JsonCpp::Value& answer);
void processNetworkChanged(JsonCpp::Value& request, JsonCpp::Value& answer); void processNetworkChanged(JsonCpp::Value& request, JsonCpp::Value& answer);
void processAddRootCert(JsonCpp::Value& request, JsonCpp::Value& answer); void processAddRootCert(JsonCpp::Value& request, JsonCpp::Value& answer);
void processLogMessage(JsonCpp::Value& request, JsonCpp::Value& answer); void processLogMessage(JsonCpp::Value& request, JsonCpp::Value& answer);
void stopAgentAndThread(); void stopAgentAndThread();
public: public:
AgentImpl(); AgentImpl();
~AgentImpl(); ~AgentImpl();
std::string command(const std::string& command); std::string command(const std::string& command);
bool waitForData(int milliseconds); bool waitForData(int milliseconds);
std::string read(); std::string read();
// Get access to internal audio manager. Value can be nullptr. // UserAgent overrides
const std::shared_ptr<AudioManager>& audioManager() const; // Called on new incoming session; providers shoukld
PDataProvider onProviderNeeded(const std::string& name) override;
void setAudioMonitoring(Audio::DataConnection* monitoring); // Called on new session offer
Audio::DataConnection* monitoring() const; void onNewSession(PSession s) override;
// UserAgent overrides // Called when session is terminated
// Called on new incoming session; providers shoukld void onSessionTerminated(PSession s, int responsecode, int reason) override;
PDataProvider onProviderNeeded(const std::string& name) override;
// Called on new session offer // Called when session is established ok i.e. after all ICE signalling is finished
void onNewSession(PSession s) override; // Conntype is type of establish event - EV_SIP or EV_ICE
void onSessionEstablished(PSession s, int conntype, const RtpPair<InternetAddress>& p) override;
// Called when session is terminated void onSessionProvisional(PSession s, int code) override;
void onSessionTerminated(PSession s, int responsecode, int reason) override;
// Called when session is established ok i.e. after all ICE signalling is finished // Called when user agent started
// Conntype is type of establish event - EV_SIP or EV_ICE void onStart(int errorcode) override;
void onSessionEstablished(PSession s, int conntype, const RtpPair<InternetAddress>& p) override;
void onSessionProvisional(PSession s, int code) override; // Called when user agent stopped
void onStop() override;
// Called when user agent started // Called when account registered
void onStart(int errorcode) override; void onAccountStart(PAccount account) override;
// Called when user agent stopped // Called when account removed or failed (non zero error code)
void onStop() override; void onAccountStop(PAccount account, int error) override;
// Called when account registered // Called when connectivity checks failed.
void onAccountStart(PAccount account) override; void onConnectivityFailed(PSession s) override;
// Called when account removed or failed (non zero error code) // Called when new candidate is gathered
void onAccountStop(PAccount account, int error) override; void onCandidateGathered(PSession s, const char* address) override;
// Called when connectivity checks failed. // Called when network change detected
void onConnectivityFailed(PSession s) override; void onNetworkChange(PSession s) override;
// Called when new candidate is gathered // Called when all candidates are gathered
void onCandidateGathered(PSession s, const char* address) override; void onGathered(PSession s) override;
// Called when network change detected // Called when new connectivity check is finished
void onNetworkChange(PSession s) override; void onCheckFinished(PSession s, const char* description) override;
// Called when all candidates are gathered // Called when log message must be recorded
void onGathered(PSession s) override; void onLog(const char* msg) override;
// Called when new connectivity check is finished // Called when problem with SIP connection(s) detected
void onCheckFinished(PSession s, const char* description) override; void onSipConnectionFailed() override;
// Called when log message must be recorded #if defined(USE_AQUA_LIBRARY)
void onLog(const char* msg) override; // Called on incoming & outgoing audio for voice sessions
void onMedia(const void* data, int length, MT::Stream::MediaDirection direction, void* context, void* userTag) override;
// Called when problem with SIP connection(s) detected #endif
void onSipConnectionFailed() override;
// Called on incoming & outgoing audio for voice sessions
void onMedia(const void* data, int length, MT::Stream::MediaDirection direction, void* context,
void* userTag) override;
}; };
#endif #endif
+16 -5
View File
@@ -1,16 +1,27 @@
#include "Agent_Interface.h" #include "Agent_Interface.h"
#include "Agent_Impl.h" #include "Agent_Impl.h"
Agent::Agent() : mContext(new AgentImpl()); Agent::Agent()
:mContext(new AgentImpl());
{ {
} }
Agent::~Agent() {} Agent::~Agent()
{
void Agent::write(const std::string& command) {} }
void Agent::write(const std::string& command)
{
}
bool Agent::waitForData(int milliseconds) bool Agent::waitForData(int milliseconds)
{ {
return false; return false;
} }
std::string Agent::read() {} std::string Agent::read()
{
}
+6 -6
View File
@@ -6,14 +6,14 @@
class Agent class Agent
{ {
protected: protected:
void* mContext; void* mContext;
public: public:
Agent(); Agent();
~Agent(); ~Agent();
void write(const std::string& command); void write(const std::string& command);
bool waitForData(int milliseconds); bool waitForData(int milliseconds);
std::string read(); std::string read();
}; };
#endif #endif
+378 -373
View File
@@ -14,580 +14,585 @@ using namespace Audio;
// -------------------- AndroidEnumerator ----------------------------- // -------------------- AndroidEnumerator -----------------------------
AndroidEnumerator::AndroidEnumerator() {} AndroidEnumerator::AndroidEnumerator()
{}
AndroidEnumerator::~AndroidEnumerator() {} AndroidEnumerator::~AndroidEnumerator()
{}
int AndroidEnumerator::indexOfDefaultDevice() int AndroidEnumerator::indexOfDefaultDevice()
{ {
return 0; return 0;
} }
int AndroidEnumerator::count() int AndroidEnumerator::count()
{ {
return 1; return 1;
} }
int AndroidEnumerator::idAt(int index) int AndroidEnumerator::idAt(int index)
{ {
return 0; return 0;
} }
std::string AndroidEnumerator::nameAt(int index) std::string AndroidEnumerator::nameAt(int index)
{ {
return "Audio"; return "Audio";
} }
void AndroidEnumerator::open(int direction) {} void AndroidEnumerator::open(int direction)
{}
void AndroidEnumerator::close() {} void AndroidEnumerator::close()
{}
// ----------------------- // -----------------------
OpenSLEngine::OpenSLEngine() {} OpenSLEngine::OpenSLEngine()
{}
OpenSLEngine::~OpenSLEngine() {} OpenSLEngine::~OpenSLEngine()
{}
void OpenSLEngine::open() void OpenSLEngine::open()
{ {
std::unique_lock<std::mutex> l(mMutex); std::unique_lock<std::mutex> l(mMutex);
if (++mUsageCounter == 1) if (++mUsageCounter == 1)
internalOpen(); internalOpen();
} }
void OpenSLEngine::close() void OpenSLEngine::close()
{ {
std::unique_lock<std::mutex> l(mMutex); std::unique_lock<std::mutex> l(mMutex);
if (mUsageCounter == 0) if (mUsageCounter == 0)
return; return;
if (--mUsageCounter == 0) if (--mUsageCounter == 0)
internalClose(); internalClose();
} }
#define CHECK_OPENSLES_ERROR \ #define CHECK_OPENSLES_ERROR if (resultCode != SL_RESULT_SUCCESS) throw Exception(ERR_OPENSLES, (int)resultCode)
if (resultCode != SL_RESULT_SUCCESS) \
throw Exception(ERR_OPENSLES, (int)resultCode)
void OpenSLEngine::internalOpen() void OpenSLEngine::internalOpen()
{ {
SLresult resultCode; SLresult resultCode;
// Instantiate OpenSL ES engine object // Instantiate OpenSL ES engine object
resultCode = slCreateEngine(&mEngineObject, 0, nullptr, 0, nullptr, nullptr); resultCode = slCreateEngine(&mEngineObject, 0, nullptr, 0, nullptr, nullptr);
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
// Bring it online (realize) // Bring it online (realize)
resultCode = (*mEngineObject)->Realize(mEngineObject, SL_BOOLEAN_FALSE); resultCode = (*mEngineObject)->Realize(mEngineObject, SL_BOOLEAN_FALSE);
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
// Get interface finally // Get interface finally
resultCode = (*mEngineObject)->GetInterface(mEngineObject, SL_IID_ENGINE, &mEngineInterface); resultCode = (*mEngineObject)->GetInterface(mEngineObject, SL_IID_ENGINE, &mEngineInterface);
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
ICELogInfo(<< "OpenSL engine object created."); ICELogInfo(<< "OpenSL engine object created.");
} }
void OpenSLEngine::internalClose() void OpenSLEngine::internalClose()
{ {
if (mEngineObject != nullptr) if (mEngineObject != nullptr)
{ {
ICELogInfo(<< "Destroy OpenSL engine object."); ICELogInfo(<< "Destroy OpenSL engine object.");
(*mEngineObject)->Destroy(mEngineObject); (*mEngineObject)->Destroy(mEngineObject);
mEngineObject = nullptr; mEngineObject = nullptr;
mEngineInterface = nullptr; mEngineInterface = nullptr;
} }
} }
SLEngineItf OpenSLEngine::getNativeEngine() const SLEngineItf OpenSLEngine::getNativeEngine() const
{ {
return mEngineInterface; return mEngineInterface;
} }
static OpenSLEngine OpenSLEngineInstance; static OpenSLEngine OpenSLEngineInstance;
OpenSLEngine& OpenSLEngine::instance() OpenSLEngine& OpenSLEngine::instance()
{ {
return OpenSLEngineInstance; return OpenSLEngineInstance;
} }
// --------------- Input implementation ---------------- // --------------- Input implementation ----------------
AndroidInputDevice::AndroidInputDevice(int devId) {} AndroidInputDevice::AndroidInputDevice(int devId)
{}
AndroidInputDevice::~AndroidInputDevice() {} AndroidInputDevice::~AndroidInputDevice()
{}
static int RateToProbe[12][2] = { static int RateToProbe[12][2] = {
{SL_SAMPLINGRATE_16, 16000}, {SL_SAMPLINGRATE_8, 8000}, {SL_SAMPLINGRATE_32, 32000}, { SL_SAMPLINGRATE_16, 16000 },
{SL_SAMPLINGRATE_44_1, 44100}, {SL_SAMPLINGRATE_11_025, 10025}, {SL_SAMPLINGRATE_22_05, 22050}, { SL_SAMPLINGRATE_8, 8000 },
{SL_SAMPLINGRATE_24, 24000}, {SL_SAMPLINGRATE_48, 48000}, {SL_SAMPLINGRATE_64, 64000}, { SL_SAMPLINGRATE_32, 32000 },
{SL_SAMPLINGRATE_88_2, 88200}, {SL_SAMPLINGRATE_96, 96000}, {SL_SAMPLINGRATE_192, 192000}}; { SL_SAMPLINGRATE_44_1, 44100 },
{ SL_SAMPLINGRATE_11_025, 10025 },
{ SL_SAMPLINGRATE_22_05, 22050 },
{ SL_SAMPLINGRATE_24, 24000 },
{ SL_SAMPLINGRATE_48, 48000 },
{ SL_SAMPLINGRATE_64, 64000 },
{ SL_SAMPLINGRATE_88_2, 88200 },
{ SL_SAMPLINGRATE_96, 96000 },
{ SL_SAMPLINGRATE_192, 192000} };
bool AndroidInputDevice::open() bool AndroidInputDevice::open()
{ {
if (active()) if (active())
return true; return true;
OpenSLEngine::instance().open(); OpenSLEngine::instance().open();
// Probe few sampling rates // Probe few sampling rates
bool opened = false; bool opened = false;
for (int rateIndex = 0; rateIndex < 12 && !opened; rateIndex++) for (int rateIndex = 0; rateIndex < 12 && !opened; rateIndex++)
{
try
{ {
try internalOpen(RateToProbe[rateIndex][0], RateToProbe[rateIndex][1]);
{ mDeviceRate = RateToProbe[rateIndex][1];
internalOpen(RateToProbe[rateIndex][0], RateToProbe[rateIndex][1]); ICELogInfo(<< "Input Opened with rate " << mDeviceRate << " and rate index " << rateIndex);
mDeviceRate = RateToProbe[rateIndex][1]; opened = mDeviceRate != 0;
ICELogInfo(<< "Input Opened with rate " << mDeviceRate << " and rate index " << rateIndex); if (!opened)
opened = mDeviceRate != 0; internalClose();
if (!opened)
internalClose();
}
catch (...)
{
opened = false;
internalClose();
}
} }
mActive = opened; catch(...)
{
opened = false;
internalClose();
}
}
mActive = opened;
return opened; return opened;
} }
void AndroidInputDevice::close() void AndroidInputDevice::close()
{ {
// There is no check for active() value because close() can be called to cleanup after bad open() call. // There is no check for active() value because close() can be called to cleanup after bad open() call.
internalClose(); internalClose();
OpenSLEngine::instance().close(); OpenSLEngine::instance().close();
mActive = false; mActive = false;
} }
Format AndroidInputDevice::getFormat() Format AndroidInputDevice::getFormat()
{ {
return Format(mDeviceRate, 1); return Format(mDeviceRate, 1);
} }
bool AndroidInputDevice::active() const bool AndroidInputDevice::active() const
{ {
return mActive; return mActive;
} }
bool AndroidInputDevice::fakeMode() bool AndroidInputDevice::fakeMode()
{ {
return false; return false;
} }
void AndroidInputDevice::setFakeMode(bool fakemode) {} void AndroidInputDevice::setFakeMode(bool fakemode)
{}
int AndroidInputDevice::readBuffer(void* buffer) int AndroidInputDevice::readBuffer(void* buffer)
{ {
std::unique_lock<std::mutex> l(mMutex); std::unique_lock<std::mutex> l(mMutex);
while (mSdkRateCache.filled() < AUDIO_MIC_BUFFER_SIZE) while (mSdkRateCache.filled() < AUDIO_MIC_BUFFER_SIZE)
{ {
mDataCondVar.wait(l); mDataCondVar.wait(l);
} }
return mSdkRateCache.read(buffer, AUDIO_MIC_BUFFER_SIZE); return mSdkRateCache.read(buffer, AUDIO_MIC_BUFFER_SIZE);
} }
#define CHECK_SL_INTERFACE(INTF, ERR) \ #define CHECK_SL_INTERFACE(INTF, ERR) {if (!INTF) throw Exception(ERR_OPENSLES, ERR); if (!(*INTF)) throw Exception(ERR_OPENSLES, ERR);}
{ \
if (!INTF) \
throw Exception(ERR_OPENSLES, ERR); \
if (!(*INTF)) \
throw Exception(ERR_OPENSLES, ERR); \
}
void AndroidInputDevice::internalOpen(int rateCode, int rate) void AndroidInputDevice::internalOpen(int rateCode, int rate)
{ {
SLresult resultCode = 0; SLresult resultCode = 0;
SLuint32 nrOfChannels = 1; SLuint32 nrOfChannels = 1;
// Prepare audio source // Prepare audio source
SLDataLocator_IODevice devDescription = {SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT, SLDataLocator_IODevice devDescription = { SL_DATALOCATOR_IODEVICE, SL_IODEVICE_AUDIOINPUT, SL_DEFAULTDEVICEID_AUDIOINPUT, NULL};
SL_DEFAULTDEVICEID_AUDIOINPUT, NULL}; SLDataSource audioSource = { &devDescription, NULL };
SLDataSource audioSource = {&devDescription, NULL};
// Source flags // Source flags
SLuint32 speakersFlags = SLuint32 speakersFlags = nrOfChannels > 1 ? (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT) : SL_SPEAKER_FRONT_CENTER;
nrOfChannels > 1 ? (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT) : SL_SPEAKER_FRONT_CENTER;
// Buffer queue // Buffer queue
SLDataLocator_AndroidSimpleBufferQueue queueDescription = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2}; SLDataLocator_AndroidSimpleBufferQueue queueDescription = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2 };
// Audio format // Audio format
SLDataFormat_PCM formatDescription = {SL_DATAFORMAT_PCM, nrOfChannels, SLDataFormat_PCM formatDescription = { SL_DATAFORMAT_PCM, nrOfChannels, (SLuint32)rateCode, SL_PCMSAMPLEFORMAT_FIXED_16,
(SLuint32)rateCode, SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16, (SLuint32)speakersFlags, SL_BYTEORDER_LITTLEENDIAN };
SL_PCMSAMPLEFORMAT_FIXED_16, (SLuint32)speakersFlags,
SL_BYTEORDER_LITTLEENDIAN};
SLDataSink audioSink = {&queueDescription, &formatDescription}; SLDataSink audioSink = { &queueDescription, &formatDescription };
// Create recorder // Create recorder
// Do not forget about RECORD_AUDIO permission // Do not forget about RECORD_AUDIO permission
const SLInterfaceID interfacesList[2] = {SL_IID_ANDROIDSIMPLEBUFFERQUEUE, SL_IID_ANDROIDCONFIGURATION}; const SLInterfaceID interfacesList[2] = { SL_IID_ANDROIDSIMPLEBUFFERQUEUE, SL_IID_ANDROIDCONFIGURATION };
const SLboolean interfacesRequirements[2] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE}; const SLboolean interfacesRequirements[2] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
// Get access to OpenSL engine // Get access to OpenSL engine
SLEngineItf engine_interface = OpenSLEngine::instance().getNativeEngine(); SLEngineItf engine_interface = OpenSLEngine::instance().getNativeEngine();
CHECK_SL_INTERFACE(engine_interface, -1); CHECK_SL_INTERFACE(engine_interface, -1);
resultCode = (*engine_interface) resultCode = (*engine_interface)->CreateAudioRecorder(
->CreateAudioRecorder(OpenSLEngine::instance().getNativeEngine(), &mRecorderObject, &audioSource, OpenSLEngine::instance().getNativeEngine(),
&audioSink, 2, interfacesList, interfacesRequirements); &mRecorderObject, &audioSource, &audioSink, 2, interfacesList, interfacesRequirements);
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
CHECK_SL_INTERFACE(mRecorderObject, -2); CHECK_SL_INTERFACE(mRecorderObject, -2);
// Obtain stream type // Obtain stream type
resultCode = (*mRecorderObject)->GetInterface(mRecorderObject, SL_IID_ANDROIDCONFIGURATION, &mAndroidCfg); resultCode = (*mRecorderObject)->GetInterface(mRecorderObject, SL_IID_ANDROIDCONFIGURATION, &mAndroidCfg);
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
// Now audio recorder goes to real world // Now audio recorder goes to real world
resultCode = (*mRecorderObject)->Realize(mRecorderObject, SL_BOOLEAN_FALSE); resultCode = (*mRecorderObject)->Realize(mRecorderObject, SL_BOOLEAN_FALSE);
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
// Get recorder interface // Get recorder interface
resultCode = (*mRecorderObject)->GetInterface(mRecorderObject, SL_IID_RECORD, &mRecorderInterface); resultCode = (*mRecorderObject)->GetInterface(mRecorderObject, SL_IID_RECORD, &mRecorderInterface);
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
CHECK_SL_INTERFACE(mRecorderInterface, -3); CHECK_SL_INTERFACE(mRecorderInterface, -3);
// Now buffer queue interface... // Now buffer queue interface...
resultCode = resultCode = (*mRecorderObject)->GetInterface(mRecorderObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &mRecorderBufferInterface);
(*mRecorderObject)->GetInterface(mRecorderObject, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &mRecorderBufferInterface); CHECK_OPENSLES_ERROR;
CHECK_OPENSLES_ERROR; CHECK_SL_INTERFACE(mRecorderBufferInterface, -4);
CHECK_SL_INTERFACE(mRecorderBufferInterface, -4);
// Resampler is needed to provide SDK's rate // Resampler is needed to provide SDK's rate
mResampler = std::make_shared<Resampler>(); mResampler = std::make_shared<Resampler>();
mResampler->start(nrOfChannels, rate, AUDIO_SAMPLERATE); mResampler->start(nrOfChannels, rate, AUDIO_SAMPLERATE);
// Allocate recorder buffer size // Allocate recorder buffer size
mBufferSize = (AUDIO_MIC_BUFFER_LENGTH / 10) * (rate / 100) * 2; mBufferSize = (AUDIO_MIC_BUFFER_LENGTH / 10) * (rate / 100) * 2;
mRecorderBuffer.setCapacity(mBufferSize * AUDIO_MIC_BUFFER_COUNT); mRecorderBuffer.setCapacity(mBufferSize * AUDIO_MIC_BUFFER_COUNT);
mRecorderBufferIndex = 0; mRecorderBufferIndex = 0;
// Setup data consuming callback // Setup data consuming callback
resultCode = (*mRecorderBufferInterface)->RegisterCallback(mRecorderBufferInterface, DeviceCallback, (void*)this); resultCode = (*mRecorderBufferInterface)->RegisterCallback(mRecorderBufferInterface, DeviceCallback, (void*)this);
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
// Setup buffers // Setup buffers
for (int i = 0; i < AUDIO_MIC_BUFFER_COUNT; i++) for (int i=0; i<AUDIO_MIC_BUFFER_COUNT; i++)
(*mRecorderBufferInterface) (*mRecorderBufferInterface)->Enqueue(mRecorderBufferInterface, mRecorderBuffer.data() + i * mBufferSize, mBufferSize);
->Enqueue(mRecorderBufferInterface, mRecorderBuffer.data() + i * mBufferSize, mBufferSize);
// Start finally // Start finally
resultCode = (*mRecorderInterface)->SetRecordState(mRecorderInterface, SL_RECORDSTATE_RECORDING); resultCode = (*mRecorderInterface)->SetRecordState(mRecorderInterface, SL_RECORDSTATE_RECORDING);
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
} }
void AndroidInputDevice::internalClose() void AndroidInputDevice::internalClose()
{ {
if (!mRecorderObject) if (!mRecorderObject)
return; return;
if (*mRecorderObject) if (*mRecorderObject)
{
if (active())
{ {
if (active()) // Stop recording
{ (*mRecorderInterface)->SetRecordState(mRecorderInterface, SL_RECORDSTATE_STOPPED);
// Stop recording
(*mRecorderInterface)->SetRecordState(mRecorderInterface, SL_RECORDSTATE_STOPPED);
// Wait until recording will not stop really // Wait until recording will not stop really
SLuint32 state = SL_RECORDSTATE_STOPPED; SLuint32 state = SL_RECORDSTATE_STOPPED;
do do
{ {
(*mRecorderInterface)->GetRecordState(mRecorderInterface, &state); (*mRecorderInterface)->GetRecordState(mRecorderInterface, &state);
SyncHelper::delay(1); SyncHelper::delay(1);
} while (state == SL_RECORDSTATE_RECORDING); }
} while (state == SL_RECORDSTATE_RECORDING);
(*mRecorderObject)->Destroy(mRecorderObject);
} }
(*mRecorderObject)->Destroy(mRecorderObject);
}
mRecorderObject = nullptr; mRecorderObject = nullptr;
mRecorderInterface = nullptr; mRecorderInterface = nullptr;
mRecorderBufferInterface = nullptr; mRecorderBufferInterface = nullptr;
mAndroidCfg = nullptr; mAndroidCfg = nullptr;
} }
void AndroidInputDevice::handleCallback(SLAndroidSimpleBufferQueueItf bq) void AndroidInputDevice::handleCallback(SLAndroidSimpleBufferQueueItf bq)
{ {
std::unique_lock<std::mutex> l(mMutex); std::unique_lock<std::mutex> l(mMutex);
// Send data to AudioPair // Send data to AudioPair
if (mConnection)
mConnection->onMicData(getFormat(), mRecorderBuffer.data() + mRecorderBufferIndex * mBufferSize, mBufferSize);
/*
// Send audio to cache with native sample rate
mDeviceRateCache.add(mRecorderBuffer.data() + mRecorderBufferIndex * mBufferSize, mBufferSize);
// Check if there is enough data (10 ms) to send
int tenMsSize = (int)Format(mDeviceRate, 1).sizeFromTime(10);
while (mDeviceRateCache.filled() >= tenMsSize)
{
char* resampled = (char*)alloca(Format().sizeFromTime(10));
int processed = 0;
int outlen = mResampler->processBuffer(mDeviceRateCache.data(), tenMsSize, processed, resampled, Format().sizeFromTime(10));
if (outlen > 0)
mSdkRateCache.add(resampled, (int)Format().sizeFromTime(10));
mDeviceRateCache.erase(tenMsSize);
}
// Tell about data
while (mSdkRateCache.filled() >= AUDIO_MIC_BUFFER_SIZE)
{
if (mConnection) if (mConnection)
mConnection->onMicData(getFormat(), mRecorderBuffer.data() + mRecorderBufferIndex * mBufferSize, mBufferSize); mConnection->onMicData(Format(), mSdkRateCache.data(), AUDIO_MIC_BUFFER_SIZE);
/* mSdkRateCache.erase(AUDIO_MIC_BUFFER_SIZE);
// Send audio to cache with native sample rate }
mDeviceRateCache.add(mRecorderBuffer.data() + mRecorderBufferIndex * mBufferSize, mBufferSize); */
// Re-enqueue used buffer
// Check if there is enough data (10 ms) to send (*mRecorderBufferInterface)->Enqueue(mRecorderBufferInterface, mRecorderBuffer.data() + mRecorderBufferIndex * mBufferSize, mBufferSize);
int tenMsSize = (int)Format(mDeviceRate, 1).sizeFromTime(10); mRecorderBufferIndex++;
while (mDeviceRateCache.filled() >= tenMsSize) mRecorderBufferIndex %= AUDIO_MIC_BUFFER_COUNT;
{
char* resampled = (char*)alloca(Format().sizeFromTime(10));
int processed = 0;
int outlen = mResampler->processBuffer(mDeviceRateCache.data(), tenMsSize, processed, resampled,
Format().sizeFromTime(10)); if (outlen > 0) mSdkRateCache.add(resampled, (int)Format().sizeFromTime(10));
mDeviceRateCache.erase(tenMsSize);
}
// Tell about data
while (mSdkRateCache.filled() >= AUDIO_MIC_BUFFER_SIZE)
{
if (mConnection)
mConnection->onMicData(Format(), mSdkRateCache.data(), AUDIO_MIC_BUFFER_SIZE);
mSdkRateCache.erase(AUDIO_MIC_BUFFER_SIZE);
}
*/
// Re-enqueue used buffer
(*mRecorderBufferInterface)
->Enqueue(mRecorderBufferInterface, mRecorderBuffer.data() + mRecorderBufferIndex * mBufferSize, mBufferSize);
mRecorderBufferIndex++;
mRecorderBufferIndex %= AUDIO_MIC_BUFFER_COUNT;
} }
void AndroidInputDevice::DeviceCallback(SLAndroidSimpleBufferQueueItf bq, void* context) void AndroidInputDevice::DeviceCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
{ {
try try
{ {
if (context) if (context)
reinterpret_cast<AndroidInputDevice*>(context)->handleCallback(bq); reinterpret_cast<AndroidInputDevice*>(context)->handleCallback(bq);
} }
catch (...) catch(...)
{ {}
}
} }
// ------------ AndroidOutputDevice ----------------- // ------------ AndroidOutputDevice -----------------
AndroidOutputDevice::AndroidOutputDevice(int devId) AndroidOutputDevice::AndroidOutputDevice(int devId)
{ {
ICELogDebug(<< "Creating AndroidOutputDevice. This is: " << strx::toHex(this)); ICELogDebug(<< "Creating AndroidOutputDevice. This is: " << StringHelper::toHex(this));
} }
AndroidOutputDevice::~AndroidOutputDevice() AndroidOutputDevice::~AndroidOutputDevice()
{ {
ICELogDebug(<< "Deleting AndroidOutputDevice."); ICELogDebug(<< "Deleting AndroidOutputDevice.");
close(); close();
} }
bool AndroidOutputDevice::open() bool AndroidOutputDevice::open()
{ {
std::unique_lock<std::mutex> l(mMutex); std::unique_lock<std::mutex> l(mMutex);
bool opened = false; bool opened = false;
for (int rateIndex = 0; rateIndex < 12 && !opened; rateIndex++) for (int rateIndex = 0; rateIndex < 12 && !opened; rateIndex++)
{
try
{ {
try internalOpen(RateToProbe[rateIndex][0], RateToProbe[rateIndex][1], true);
{ opened = true;
internalOpen(RateToProbe[rateIndex][0], RateToProbe[rateIndex][1], true); mDeviceRate = RateToProbe[rateIndex][1];
opened = true; ICELogCritical(<< "Output opened with rate " << mDeviceRate << " and index " << rateIndex);
mDeviceRate = RateToProbe[rateIndex][1];
ICELogCritical(<< "Output opened with rate " << mDeviceRate << " and index " << rateIndex);
}
catch (...)
{
opened = false;
}
} }
if (opened) catch(...)
ICELogInfo(<< "Speaker opened on rate " << mDeviceRate); {
opened = false;
}
}
if (opened)
ICELogInfo(<< "Speaker opened on rate " << mDeviceRate);
return opened; return opened;
} }
void AndroidOutputDevice::close() void AndroidOutputDevice::close()
{ {
std::unique_lock<std::mutex> l(mMutex); std::unique_lock<std::mutex> l(mMutex);
internalClose(); internalClose();
} }
Format AndroidOutputDevice::getFormat() Format AndroidOutputDevice::getFormat()
{ {
return Format(mDeviceRate, 1); return Format(mDeviceRate, 1);
} }
bool AndroidOutputDevice::fakeMode() bool AndroidOutputDevice::fakeMode()
{ {
return false; return false;
} }
void AndroidOutputDevice::setFakeMode(bool fakemode) {} void AndroidOutputDevice::setFakeMode(bool fakemode)
{
}
void AndroidOutputDevice::internalOpen(int rateId, int rate, bool voice) void AndroidOutputDevice::internalOpen(int rateId, int rate, bool voice)
{ {
mInShutdown = false; mInShutdown = false;
SLresult resultCode; SLresult resultCode;
SLuint32 channels = 1; SLuint32 channels = 1;
// Configure audio source // Configure audio source
SLDataLocator_AndroidSimpleBufferQueue queue_desc = {SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2}; SLDataLocator_AndroidSimpleBufferQueue queue_desc = { SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, 2 };
const SLInterfaceID interfacesList[] = {SL_IID_VOLUME}; const SLInterfaceID interfacesList[] = { SL_IID_VOLUME };
const SLboolean interfaceRequirements[] = {SL_BOOLEAN_FALSE}; const SLboolean interfaceRequirements[] = { SL_BOOLEAN_FALSE };
resultCode = (*OpenSLEngine::instance().getNativeEngine()) resultCode = (*OpenSLEngine::instance().getNativeEngine())->CreateOutputMix(
->CreateOutputMix(OpenSLEngine::instance().getNativeEngine(), &mMixer, 1, interfacesList, OpenSLEngine::instance().getNativeEngine(), &mMixer, 1, interfacesList,
interfaceRequirements); interfaceRequirements);
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
// Bring mixer online // Bring mixer online
resultCode = (*mMixer)->Realize(mMixer, SL_BOOLEAN_FALSE); resultCode = (*mMixer)->Realize(mMixer, SL_BOOLEAN_FALSE);
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
// Prepare mixer configuration // Prepare mixer configuration
SLuint32 speakers = channels > 1 ? (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT) : SL_SPEAKER_FRONT_CENTER; SLuint32 speakers =
channels > 1 ? (SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT) : SL_SPEAKER_FRONT_CENTER;
// Describe audio format // Describe audio format
SLDataFormat_PCM pcm_format = { SLDataFormat_PCM pcm_format = {SL_DATAFORMAT_PCM, channels, (SLuint32) rateId,
SL_DATAFORMAT_PCM, channels, (SLuint32)rateId, SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16, SL_PCMSAMPLEFORMAT_FIXED_16,
SL_PCMSAMPLEFORMAT_FIXED_16, speakers, SL_BYTEORDER_LITTLEENDIAN}; speakers, SL_BYTEORDER_LITTLEENDIAN};
// Describe audio source - buffers + audio format // Describe audio source - buffers + audio format
SLDataSource audio_source = {&queue_desc, &pcm_format}; SLDataSource audio_source = { &queue_desc, &pcm_format };
// Describe audio sink // Describe audio sink
SLDataLocator_OutputMix mixer_desc = {SL_DATALOCATOR_OUTPUTMIX, mMixer}; SLDataLocator_OutputMix mixer_desc = { SL_DATALOCATOR_OUTPUTMIX, mMixer };
SLDataSink audio_sink = {&mixer_desc, NULL}; SLDataSink audio_sink = { &mixer_desc, NULL };
// Create player instance // Create player instance
const SLInterfaceID playerInterfaces[] = {SL_IID_ANDROIDSIMPLEBUFFERQUEUE, SL_IID_VOLUME, const SLInterfaceID playerInterfaces[] = { SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
SL_IID_ANDROIDCONFIGURATION}; SL_IID_VOLUME,
const SLboolean playerInterfacesReqs[] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE}; SL_IID_ANDROIDCONFIGURATION };
const SLboolean playerInterfacesReqs[] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE };
resultCode = (*OpenSLEngine::instance().getNativeEngine()) resultCode = (*OpenSLEngine::instance().getNativeEngine())->CreateAudioPlayer(
->CreateAudioPlayer(OpenSLEngine::instance().getNativeEngine(), &mPlayer, &audio_source, OpenSLEngine::instance().getNativeEngine(), &mPlayer,
&audio_sink, 3, playerInterfaces, playerInterfacesReqs); &audio_source, &audio_sink, 3, playerInterfaces, playerInterfacesReqs);
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
// Get android config interface // Get android config interface
resultCode = (*mPlayer)->GetInterface(mPlayer, SL_IID_ANDROIDCONFIGURATION, &mAndroidConfig); resultCode = (*mPlayer)->GetInterface(mPlayer, SL_IID_ANDROIDCONFIGURATION, &mAndroidConfig);
if (resultCode == SL_RESULT_SUCCESS) if (resultCode == SL_RESULT_SUCCESS)
{ {
SLint32 streamType = voice ? SL_ANDROID_STREAM_VOICE : SL_ANDROID_STREAM_MEDIA; SLint32 streamType = voice ? SL_ANDROID_STREAM_VOICE : SL_ANDROID_STREAM_MEDIA;
resultCode = (*mAndroidConfig) resultCode = (*mAndroidConfig)->SetConfiguration(mAndroidConfig, SL_ANDROID_KEY_STREAM_TYPE,
->SetConfiguration(mAndroidConfig, SL_ANDROID_KEY_STREAM_TYPE, &streamType, sizeof(SLint32)); &streamType, sizeof(SLint32));
if (resultCode != SL_RESULT_SUCCESS) if (resultCode != SL_RESULT_SUCCESS)
ICELogCritical(<< "Failed to set audio destination with error " << (unsigned)resultCode); ICELogCritical(<< "Failed to set audio destination with error " << (unsigned)resultCode);
} }
else else
ICELogCritical(<< "Failed to obtain android cfg audio interface with error " << (unsigned)resultCode); ICELogCritical(<< "Failed to obtain android cfg audio interface with error " << (unsigned)resultCode);
// Bring player online // Bring player online
resultCode = (*mPlayer)->Realize(mPlayer, SL_BOOLEAN_FALSE); resultCode = (*mPlayer)->Realize(mPlayer, SL_BOOLEAN_FALSE);
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
// Obtain player control // Obtain player control
resultCode = (*mPlayer)->GetInterface(mPlayer, SL_IID_PLAY, &mPlayerControl); resultCode = (*mPlayer)->GetInterface(mPlayer, SL_IID_PLAY, &mPlayerControl);
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
// Get the buffer queue interface // Get the buffer queue interface
resultCode = (*mPlayer)->GetInterface(mPlayer, SL_IID_ANDROIDSIMPLEBUFFERQUEUE, &mBufferQueue); resultCode = (*mPlayer)->GetInterface(mPlayer, SL_IID_ANDROIDSIMPLEBUFFERQUEUE,
CHECK_OPENSLES_ERROR; &mBufferQueue);
CHECK_OPENSLES_ERROR;
// Setup callback // Setup callback
resultCode = (*mBufferQueue)->RegisterCallback(mBufferQueue, DeviceCallback, this); resultCode = (*mBufferQueue)->RegisterCallback(mBufferQueue, DeviceCallback, this);
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
// Enqueue buffers // Enqueue buffers
mBufferSize = (int)Format(rate, channels).sizeFromTime(AUDIO_SPK_BUFFER_LENGTH); mBufferSize = (int)Format(rate, channels).sizeFromTime(AUDIO_SPK_BUFFER_LENGTH);
mPlayBuffer.setCapacity(AUDIO_SPK_BUFFER_COUNT * mBufferSize); mPlayBuffer.setCapacity(AUDIO_SPK_BUFFER_COUNT * mBufferSize);
mBufferIndex = 0; mBufferIndex = 0;
for (int i = 0; i < AUDIO_SPK_BUFFER_COUNT; i++) for (int i = 0; i < AUDIO_SPK_BUFFER_COUNT; i++)
(*mBufferQueue)->Enqueue(mBufferQueue, mPlayBuffer.data() + i * mBufferSize, (SLuint32)mBufferSize); (*mBufferQueue)->Enqueue(mBufferQueue, mPlayBuffer.data() + i * mBufferSize,
(SLuint32)mBufferSize);
// Set the player's state to playing // Set the player's state to playing
resultCode = (*mPlayerControl)->SetPlayState(mPlayerControl, SL_PLAYSTATE_PLAYING); resultCode = (*mPlayerControl)->SetPlayState(mPlayerControl, SL_PLAYSTATE_PLAYING);
CHECK_OPENSLES_ERROR; CHECK_OPENSLES_ERROR;
ICELogInfo(<< "Android audio output is opened and playing."); ICELogInfo(<< "Android audio output is opened and playing.");
} }
void AndroidOutputDevice::internalClose() void AndroidOutputDevice::internalClose()
{ {
if (mPlayer) if (mPlayer)
{
if (*mPlayer)
{ {
if (*mPlayer) mInShutdown = true;
{ ICELogInfo(<< "Stop player");
mInShutdown = true; if (mPlayerControl) {
ICELogInfo(<< "Stop player"); if (*mPlayerControl) {
if (mPlayerControl) SLuint32 state = SL_PLAYSTATE_PLAYING;
{ (*mPlayerControl)->SetPlayState(mPlayerControl, SL_PLAYSTATE_STOPPED);
if (*mPlayerControl)
{
SLuint32 state = SL_PLAYSTATE_PLAYING;
(*mPlayerControl)->SetPlayState(mPlayerControl, SL_PLAYSTATE_STOPPED);
while (state != SL_PLAYSTATE_STOPPED) while (state != SL_PLAYSTATE_STOPPED) {
{ (*mPlayerControl)->GetPlayState(mPlayerControl, &state);
(*mPlayerControl)->GetPlayState(mPlayerControl, &state); SyncHelper::delay(1);
SyncHelper::delay(1); }
}
}
}
// Clear buffer queue
ICELogInfo(<< "Clear player buffer queue");
(*mBufferQueue)->Clear(mBufferQueue);
ICELogInfo(<< "Destroy player object");
// Destroy player object
(*mPlayer)->Destroy(mPlayer);
ICELogInfo(<< "Android audio output closed.");
mPlayer = nullptr;
mPlayerControl = nullptr;
mBufferQueue = nullptr;
mEffect = nullptr;
mAndroidConfig = nullptr;
} }
} }
if (mMixer) // Clear buffer queue
{ ICELogInfo(<< "Clear player buffer queue");
if (*mMixer) (*mBufferQueue)->Clear(mBufferQueue);
(*mMixer)->Destroy(mMixer);
mMixer = nullptr; ICELogInfo(<< "Destroy player object");
// Destroy player object
(*mPlayer)->Destroy(mPlayer);
ICELogInfo(<< "Android audio output closed.");
mPlayer = nullptr;
mPlayerControl = nullptr;
mBufferQueue = nullptr;
mEffect = nullptr;
mAndroidConfig = nullptr;
} }
}
if (mMixer)
{
if (*mMixer)
(*mMixer)->Destroy(mMixer);
mMixer = nullptr;
}
} }
void AndroidOutputDevice::handleCallback(SLAndroidSimpleBufferQueueItf bq) void AndroidOutputDevice::handleCallback(SLAndroidSimpleBufferQueueItf bq)
{ {
if (mInShutdown) if (mInShutdown)
return; return;
/*{ /*{
char silence[mBufferSize]; memset(silence, 0, mBufferSize); char silence[mBufferSize]; memset(silence, 0, mBufferSize);
(*mBufferQueue)->Enqueue(mBufferQueue, silence, mBufferSize); (*mBufferQueue)->Enqueue(mBufferQueue, silence, mBufferSize);
return; return;
}*/ }*/
// Ask producer about data // Ask producer about data
char* buffer = mPlayBuffer.mutableData() + mBufferIndex * mBufferSize; char* buffer = mPlayBuffer.mutableData() + mBufferIndex * mBufferSize;
if (mConnection) if (mConnection)
{ {
Format f = getFormat(); Format f = getFormat();
if (f.mRate != 0) if (f.mRate != 0)
mConnection->onSpkData(f, buffer, mBufferSize); mConnection->onSpkData(f, buffer, mBufferSize);
} }
(*mBufferQueue)->Enqueue(mBufferQueue, buffer, (SLuint32)mBufferSize); (*mBufferQueue)->Enqueue(mBufferQueue, buffer, (SLuint32)mBufferSize);
mBufferIndex++; mBufferIndex++;
mBufferIndex %= AUDIO_SPK_BUFFER_COUNT; mBufferIndex %= AUDIO_SPK_BUFFER_COUNT;
} }
void AndroidOutputDevice::DeviceCallback(SLAndroidSimpleBufferQueueItf bq, void* context) void AndroidOutputDevice::DeviceCallback(SLAndroidSimpleBufferQueueItf bq, void* context)
{ {
if (!context) if (!context)
return; return;
try try
{ {
reinterpret_cast<AndroidOutputDevice*>(context)->handleCallback(bq); reinterpret_cast<AndroidOutputDevice*>(context)->handleCallback(bq);
} }
catch (...) catch(...)
{ {}
}
} }
#endif // TARGET_ANDROID #endif // TARGET_ANDROID
+93 -92
View File
@@ -25,120 +25,121 @@
namespace Audio namespace Audio
{ {
class AndroidEnumerator : public Enumerator class AndroidEnumerator: public Enumerator
{ {
public: public:
AndroidEnumerator(); AndroidEnumerator();
~AndroidEnumerator(); ~AndroidEnumerator();
void open(int direction); void open(int direction);
void close(); void close();
int count(); int count();
std::string nameAt(int index); std::string nameAt(int index);
int idAt(int index); int idAt(int index);
int indexOfDefaultDevice(); int indexOfDefaultDevice();
protected: protected:
}; };
class AndroidInputDevice : public InputDevice class AndroidInputDevice: public InputDevice
{ {
public: public:
AndroidInputDevice(int devId); AndroidInputDevice(int devId);
~AndroidInputDevice(); ~AndroidInputDevice();
bool open(); bool open();
void close(); void close();
Format getFormat(); Format getFormat();
bool fakeMode(); bool fakeMode();
void setFakeMode(bool fakemode); void setFakeMode(bool fakemode);
int readBuffer(void* buffer); int readBuffer(void* buffer);
bool active() const; bool active() const;
protected: protected:
bool mActive = false; bool mActive = false;
SLObjectItf mRecorderObject = nullptr; SLObjectItf mRecorderObject = nullptr;
SLRecordItf mRecorderInterface = nullptr; SLRecordItf mRecorderInterface = nullptr;
SLAndroidSimpleBufferQueueItf mRecorderBufferInterface = nullptr; SLAndroidSimpleBufferQueueItf mRecorderBufferInterface = nullptr;
SLAndroidConfigurationItf mAndroidCfg = nullptr; SLAndroidConfigurationItf mAndroidCfg = nullptr;
PResampler mResampler; PResampler mResampler;
DataWindow mDeviceRateCache, mSdkRateCache; DataWindow mDeviceRateCache, mSdkRateCache;
int mDeviceRate; // Actual rate of opened recorder int mDeviceRate; // Actual rate of opened recorder
int mBufferSize; // Size of buffer used for recording (at native sample rate) int mBufferSize; // Size of buffer used for recording (at native sample rate)
DataWindow mRecorderBuffer; DataWindow mRecorderBuffer;
std::condition_variable mDataCondVar; std::condition_variable mDataCondVar;
int mRecorderBufferIndex; int mRecorderBufferIndex;
std::mutex mMutex; std::mutex mMutex;
void internalOpen(int rateCode, int rate); void internalOpen(int rateCode, int rate);
void internalClose(); void internalClose();
void handleCallback(SLAndroidSimpleBufferQueueItf bq); void handleCallback(SLAndroidSimpleBufferQueueItf bq);
static void DeviceCallback(SLAndroidSimpleBufferQueueItf bq, void* context); static void DeviceCallback(SLAndroidSimpleBufferQueueItf bq, void* context);
}; };
class AndroidOutputDevice : public OutputDevice class AndroidOutputDevice: public OutputDevice
{ {
public: public:
AndroidOutputDevice(int devId); AndroidOutputDevice(int devId);
~AndroidOutputDevice(); ~AndroidOutputDevice();
bool open(); bool open();
void close(); void close();
Format getFormat(); Format getFormat();
bool fakeMode(); bool fakeMode();
void setFakeMode(bool fakemode); void setFakeMode(bool fakemode);
protected: protected:
std::mutex mMutex; std::mutex mMutex;
int mDeviceRate = 0; int mDeviceRate = 0;
SLObjectItf mMixer = nullptr; SLObjectItf mMixer = nullptr;
SLObjectItf mPlayer = nullptr; SLObjectItf mPlayer = nullptr;
SLPlayItf mPlayerControl = nullptr; SLPlayItf mPlayerControl = nullptr;
SLAndroidSimpleBufferQueueItf mBufferQueue = nullptr; SLAndroidSimpleBufferQueueItf mBufferQueue = nullptr;
SLAndroidConfigurationItf mAndroidConfig = nullptr; SLAndroidConfigurationItf mAndroidConfig = nullptr;
SLEffectSendItf mEffect = nullptr; SLEffectSendItf mEffect = nullptr;
DataWindow mPlayBuffer; DataWindow mPlayBuffer;
int mBufferIndex = 0, mBufferSize = 0; int mBufferIndex = 0, mBufferSize = 0;
bool mInShutdown = false; bool mInShutdown = false;
void internalOpen(int rateId, int rate, bool voice); void internalOpen(int rateId, int rate, bool voice);
void internalClose(); void internalClose();
void handleCallback(SLAndroidSimpleBufferQueueItf bq); void handleCallback(SLAndroidSimpleBufferQueueItf bq);
static void DeviceCallback(SLAndroidSimpleBufferQueueItf bq, void* context); static void DeviceCallback(SLAndroidSimpleBufferQueueItf bq, void* context);
};
class OpenSLEngine : public OsEngine };
{
public:
OpenSLEngine();
~OpenSLEngine();
// open() / close() methods are based on usage counting. class OpenSLEngine: public OsEngine
// It means every close() call must be matched by corresponding open() call. {
// True audio engine close will happen only on last close() call. public:
void open() override; OpenSLEngine();
void close() override; ~OpenSLEngine();
SLEngineItf getNativeEngine() const; // open() / close() methods are based on usage counting.
// It means every close() call must be matched by corresponding open() call.
// True audio engine close will happen only on last close() call.
void open() override;
void close() override;
static OpenSLEngine& instance(); SLEngineItf getNativeEngine() const;
protected: static OpenSLEngine& instance();
std::mutex mMutex;
int mUsageCounter = 0;
SLObjectItf mEngineObject = nullptr;
SLEngineItf mEngineInterface = nullptr;
void internalOpen(); protected:
void internalClose(); std::mutex mMutex;
}; int mUsageCounter = 0;
} // namespace Audio SLObjectItf mEngineObject = nullptr;
SLEngineItf mEngineInterface = nullptr;
void internalOpen();
void internalClose();
};
}
#endif // TARGET_ANDROID #endif // TARGET_ANDROID
+131 -182
View File
@@ -4,293 +4,242 @@
#include <mutex> #include <mutex>
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <algorithm>
#include "../helper/HL_String.h" #include "../helper/HL_String.h"
#include "../helper/HL_Time.h" #include "../helper/HL_Time.h"
#ifdef TARGET_ANDROID #ifdef TARGET_ANDROID
#define LOG_SUBSYSTEM "audio" #define LOG_SUBSYSTEM "Audio"
using namespace Audio; using namespace Audio;
// -------------------- AndroidEnumerator ----------------------------- // -------------------- AndroidEnumerator -----------------------------
AndroidEnumerator::AndroidEnumerator() {} AndroidEnumerator::AndroidEnumerator()
{}
AndroidEnumerator::~AndroidEnumerator() {} AndroidEnumerator::~AndroidEnumerator()
{}
int AndroidEnumerator::indexOfDefaultDevice() int AndroidEnumerator::indexOfDefaultDevice()
{ {
return 0; return 0;
} }
int AndroidEnumerator::count() int AndroidEnumerator::count()
{ {
return 1; return 1;
} }
int AndroidEnumerator::idAt(int index) int AndroidEnumerator::idAt(int index)
{ {
return 0; return 0;
} }
std::string AndroidEnumerator::nameAt(int index) std::string AndroidEnumerator::nameAt(int index)
{ {
return "Audio"; return "Audio";
} }
void AndroidEnumerator::open(int direction) {} void AndroidEnumerator::open(int direction)
{}
void AndroidEnumerator::close() {} void AndroidEnumerator::close()
{}
// --------------- Input implementation ---------------- // --------------- Input implementation ----------------
AndroidInputDevice::AndroidInputDevice(int devId) {} AndroidInputDevice::AndroidInputDevice(int devId)
{}
AndroidInputDevice::~AndroidInputDevice() AndroidInputDevice::~AndroidInputDevice()
{ {
close(); close();
} }
bool AndroidInputDevice::open() bool AndroidInputDevice::open()
{ {
if (active()) if (active())
return true; return true;
oboe::AudioStreamBuilder builder; oboe::AudioStreamBuilder builder;
builder.setDirection(oboe::Direction::Input); builder.setDirection(oboe::Direction::Input);
builder.setPerformanceMode(oboe::PerformanceMode::LowLatency); builder.setPerformanceMode(oboe::PerformanceMode::LowLatency);
builder.setSharingMode(oboe::SharingMode::Exclusive); builder.setSharingMode(oboe::SharingMode::Exclusive);
builder.setFormat(oboe::AudioFormat::I16); builder.setFormat(oboe::AudioFormat::I16);
builder.setChannelCount(oboe::ChannelCount::Mono); builder.setChannelCount(oboe::ChannelCount::Mono);
builder.setCallback(this); builder.setCallback(this);
oboe::Result rescode = builder.openStream(&mRecordingStream); oboe::Result rescode = builder.openStream(&mRecordingStream);
if (rescode != oboe::Result::OK) if (rescode != oboe::Result::OK)
return false; return false;
mDeviceRate = mRecordingStream->getSampleRate(); mDeviceRate = mRecordingStream->getSampleRate();
ICELogInfo(<< "Input Opened with rate " << mDeviceRate); ICELogInfo(<< "Input Opened with rate " << mDeviceRate);
mActive = true; mActive = true;
rescode = mRecordingStream->requestStart(); rescode = mRecordingStream->requestStart();
if (rescode != oboe::Result::OK) if (rescode != oboe::Result::OK)
{ {
close(); close();
mActive = false; mActive = false;
} }
return mActive; return mActive;
} }
void AndroidInputDevice::close() void AndroidInputDevice::close()
{ {
// There is no check for active() value because close() can be called to cleanup after bad open() call. // There is no check for active() value because close() can be called to cleanup after bad open() call.
if (mRecordingStream != nullptr) if (mRecordingStream != nullptr)
{ {
mRecordingStream->close(); mRecordingStream->close();
delete mRecordingStream; delete mRecordingStream; mRecordingStream = nullptr;
mRecordingStream = nullptr; }
} mActive = false;
mActive = false;
} }
oboe::DataCallbackResult AndroidInputDevice::onAudioReady(oboe::AudioStream* audioStream, void* audioData, oboe::DataCallbackResult
int32_t numFrames) AndroidInputDevice::onAudioReady(oboe::AudioStream *audioStream, void *audioData, int32_t numFrames)
{ {
std::unique_lock<std::mutex> l(mMutex); std::unique_lock<std::mutex> l(mMutex);
// Send data to AudioPair // Send data to AudioPair
if (mConnection) if (mConnection)
mConnection->onMicData(getFormat(), audioData, numFrames); mConnection->onMicData(getFormat(), audioData, numFrames);
return oboe::DataCallbackResult::Continue; return oboe::DataCallbackResult::Continue;
} }
Format AndroidInputDevice::getFormat() Format AndroidInputDevice::getFormat()
{ {
return Format(mDeviceRate, 1); return Format(mDeviceRate, 1);
} }
bool AndroidInputDevice::active() const bool AndroidInputDevice::active() const
{ {
return mActive; return mActive;
} }
bool AndroidInputDevice::fakeMode() bool AndroidInputDevice::fakeMode()
{ {
return false; return false;
} }
void AndroidInputDevice::setFakeMode(bool fakemode) {} void AndroidInputDevice::setFakeMode(bool fakemode)
{}
int AndroidInputDevice::readBuffer(void* buffer) int AndroidInputDevice::readBuffer(void* buffer)
{ {
throw std::runtime_error("AndroidInputDevice::readBuffer() is not implemented."); throw std::runtime_error("AndroidInputDevice::readBuffer() is not implemented.");
} }
// ------------ AndroidOutputDevice ----------------- // ------------ AndroidOutputDevice -----------------
AndroidOutputDevice::AndroidOutputDevice(int devId) AndroidOutputDevice::AndroidOutputDevice(int devId)
{ {
ICELogDebug(<< "Creating AndroidOutputDevice. This is: " << strx::toHex(this)); ICELogDebug(<< "Creating AndroidOutputDevice. This is: " << StringHelper::toHex(this));
} }
AndroidOutputDevice::~AndroidOutputDevice() AndroidOutputDevice::~AndroidOutputDevice()
{ {
ICELogDebug(<< "Deleting AndroidOutputDevice."); ICELogDebug(<< "Deleting AndroidOutputDevice.");
// Mark shutdown before closing so a disconnect callback racing with teardown close();
// does not resurrect the stream via onErrorAfterClose()'s restart.
mInShutdown = true;
close();
} }
bool AndroidOutputDevice::open() bool AndroidOutputDevice::open()
{ {
std::unique_lock<std::mutex> l(mMutex); std::unique_lock<std::mutex> l(mMutex);
if (mActive) if (mActive)
return true; return true;
mInShutdown = false; mRequestedFrames = 0;
mRequestedFrames = 0; mStartTime = 0.0;
mStartTime = 0.0; mEndTime = 0.0;
mEndTime = 0.0;
oboe::AudioStreamBuilder builder; oboe::AudioStreamBuilder builder;
builder.setDirection(oboe::Direction::Output); builder.setDirection(oboe::Direction::Output);
builder.setPerformanceMode(oboe::PerformanceMode::LowLatency); builder.setPerformanceMode(oboe::PerformanceMode::LowLatency);
builder.setSharingMode(oboe::SharingMode::Exclusive); builder.setSharingMode(oboe::SharingMode::Exclusive);
builder.setFormat(oboe::AudioFormat::I16); builder.setFormat(oboe::AudioFormat::I16);
builder.setChannelCount(oboe::ChannelCount::Mono); builder.setChannelCount(oboe::ChannelCount::Mono);
// Route through the platform voice-call path: correct device selection/volume // builder.setDataCallback(this);
// and platform voice tuning for a softphone. builder.setCallback(this);
builder.setUsage(oboe::Usage::VoiceCommunication); //builder.setErrorCallback(this)
builder.setContentType(oboe::ContentType::Speech);
// builder.setDataCallback(this);
builder.setCallback(this);
// builder.setErrorCallback(this)
oboe::Result rescode = builder.openStream(&mPlayingStream); oboe::Result rescode = builder.openStream(&mPlayingStream);
if (rescode != oboe::Result::OK) if (rescode != oboe::Result::OK)
return false; return false;
mDeviceRate = mPlayingStream->getSampleRate(); mDeviceRate = mPlayingStream->getSampleRate();
ICELogInfo(<< "Output opened with rate " << mDeviceRate); ICELogInfo(<< "Input Opened with rate " << mDeviceRate);
mActive = true; mActive = true;
rescode = mPlayingStream->requestStart(); rescode = mPlayingStream->requestStart();
if (rescode != oboe::Result::OK) if (rescode != oboe::Result::OK)
{ {
close(); close();
mActive = false; mActive = false;
return mActive; }
} return mActive;
// Latch the burst size and start from a two-burst buffer. onAudioReady() grows
// this on XRuns (up to a cap) so we keep low latency when the device can sustain
// it and trade a little latency for glitch-free playback when it can't.
mBurstFrames = mPlayingStream->getFramesPerBurst();
mXRunLast = 0;
mHeartbeatLast = 0.0f;
if (mBurstFrames > 0)
mPlayingStream->setBufferSizeInFrames(mBurstFrames * 2);
return mActive;
} }
void AndroidOutputDevice::close() void AndroidOutputDevice::close()
{ {
std::unique_lock<std::mutex> l(mMutex); std::unique_lock<std::mutex> l(mMutex);
if (!mActive) if (!mActive)
return; return;
if (mPlayingStream != nullptr) if (mPlayingStream != nullptr)
{ {
mPlayingStream->close(); mPlayingStream->close();
delete mPlayingStream; delete mPlayingStream; mPlayingStream = nullptr;
mPlayingStream = nullptr; }
} mEndTime = now_ms();
mEndTime = now_ms(); mActive = false;
mActive = false;
ICELogInfo(<< "For time " << mEndTime - mStartTime << " ms was requested " ICELogInfo(<< "For time " << mEndTime - mStartTime << " ms was requested "
<< float(mRequestedFrames) / getFormat().mRate * 1000 << " ms"); << float(mRequestedFrames) / getFormat().mRate * 1000 << " ms");
} }
Format AndroidOutputDevice::getFormat() Format AndroidOutputDevice::getFormat()
{ {
return {mDeviceRate, 1}; return {mDeviceRate, 1};
} }
bool AndroidOutputDevice::fakeMode() bool AndroidOutputDevice::fakeMode()
{ {
return false; return false;
} }
void AndroidOutputDevice::setFakeMode(bool /*fakemode*/) {} void AndroidOutputDevice::setFakeMode(bool /*fakemode*/)
oboe::DataCallbackResult AndroidOutputDevice::onAudioReady(oboe::AudioStream* audioStream, void* audioData,
int32_t numFrames)
{ {
if (mInShutdown)
return oboe::DataCallbackResult::Stop;
if (mStartTime == 0.0)
mStartTime = now_ms();
// Ask producer about data
memset(audioData, 0, numFrames * 2);
if (mConnection)
{
Format f = getFormat();
if (f.mRate != 0)
mConnection->onSpkData(f, audioData, numFrames * 2);
}
mRequestedFrames += numFrames;
// Adaptive buffer sizing: on new XRuns (device-side underruns, i.e. we missed a
// callback deadline), grow the buffer one burst at a time up to a cap. Both calls
// are documented as safe from within the data callback.
auto xrun = audioStream->getXRunCount();
if (xrun && xrun.value() > mXRunLast)
{
mXRunLast = xrun.value();
if (mBurstFrames > 0)
{
int32_t cap = mBurstFrames * 8;
int32_t cur = audioStream->getBufferSizeInFrames();
int32_t next = std::min(cur + mBurstFrames, cap);
if (next > cur)
audioStream->setBufferSizeInFrames(next);
}
}
// Device-side heartbeat (Step 0): surfaces XRuns/buffer growth so device glitches
// can be told apart from network/jitter impairments.
float t = now_ms();
if (mHeartbeatLast == 0.0f)
mHeartbeatLast = t;
else if (t - mHeartbeatLast >= 5000.0f)
{
ICELogInfo(<< "[spk-heartbeat] xruns=" << mXRunLast << " bufFrames=" << audioStream->getBufferSizeInFrames()
<< " burst=" << mBurstFrames << " rate=" << mDeviceRate);
mHeartbeatLast = t;
}
return oboe::DataCallbackResult::Continue;
} }
// Disconnect recovery: on a route change (headset/BT plug/unplug) AAudio tears the oboe::DataCallbackResult AndroidOutputDevice::onAudioReady(oboe::AudioStream *audioStream, void *audioData, int32_t numFrames)
// stream down and calls this on its own thread after the stream is closed. Rebuild
// on the new default route so audio doesn't silently die mid-call.
// See https://github.com/google/oboe/blob/master/docs/notes/disconnect.md
void AndroidOutputDevice::onErrorAfterClose(oboe::AudioStream* stream, oboe::Result result)
{ {
if (result == oboe::Result::ErrorDisconnected && !mInShutdown) if (mInShutdown)
{ return oboe::DataCallbackResult::Stop;
ICELogInfo(<< "Output stream disconnected; restarting on the new route");
// close() and open() each take mMutex internally; this callback holds none. if (mStartTime == 0.0)
close(); mStartTime = now_ms();
open();
} // Ask producer about data
memset(audioData, 0, numFrames * 2);
if (mConnection)
{
Format f = getFormat();
if (f.mRate != 0)
mConnection->onSpkData(f, audioData, numFrames * 2);
}
mRequestedFrames += numFrames;
return oboe::DataCallbackResult::Continue;
}
// TODO - special case https://github.com/google/oboe/blob/master/docs/notes/disconnect.md
void AndroidOutputDevice::onErrorAfterClose(oboe::AudioStream *stream, oboe::Result result) {
if (result == oboe::Result::ErrorDisconnected) {
// LOGI("Restarting AudioStream after disconnect");
// soundEngine.restart(); // please check oboe samples for soundEngine.restart(); call
}
} }
#endif // TARGET_ANDROID #endif // TARGET_ANDROID
+63 -67
View File
@@ -25,88 +25,84 @@
namespace Audio namespace Audio
{ {
class AndroidEnumerator : public Enumerator class AndroidEnumerator: public Enumerator
{ {
public: public:
AndroidEnumerator(); AndroidEnumerator();
~AndroidEnumerator(); ~AndroidEnumerator();
void open(int direction); void open(int direction);
void close(); void close();
int count(); int count();
std::string nameAt(int index); std::string nameAt(int index);
int idAt(int index); int idAt(int index);
int indexOfDefaultDevice(); int indexOfDefaultDevice();
protected: protected:
}; };
class AndroidInputDevice : public InputDevice, public oboe::AudioStreamCallback class AndroidInputDevice: public InputDevice, public oboe::AudioStreamCallback
{ {
public: public:
AndroidInputDevice(int devId); AndroidInputDevice(int devId);
~AndroidInputDevice(); ~AndroidInputDevice();
bool open(); bool open();
void close(); void close();
Format getFormat(); Format getFormat();
bool fakeMode(); bool fakeMode();
void setFakeMode(bool fakemode); void setFakeMode(bool fakemode);
int readBuffer(void* buffer); int readBuffer(void* buffer);
bool active() const; bool active() const;
oboe::DataCallbackResult onAudioReady(oboe::AudioStream* audioStream, void* audioData, int32_t numFrames); oboe::DataCallbackResult
onAudioReady(oboe::AudioStream *audioStream, void *audioData, int32_t numFrames);
protected: protected:
bool mActive = false; bool mActive = false;
oboe::AudioStream* mRecordingStream = nullptr; oboe::AudioStream* mRecordingStream = nullptr;
PResampler mResampler; PResampler mResampler;
DataWindow mDeviceRateCache, mSdkRateCache; DataWindow mDeviceRateCache, mSdkRateCache;
int mDeviceRate; // Actual rate of opened recorder int mDeviceRate; // Actual rate of opened recorder
int mBufferSize; // Size of buffer used for recording (at native sample rate) int mBufferSize; // Size of buffer used for recording (at native sample rate)
DataWindow mRecorderBuffer; DataWindow mRecorderBuffer;
std::condition_variable mDataCondVar; std::condition_variable mDataCondVar;
int mRecorderBufferIndex; int mRecorderBufferIndex;
std::mutex mMutex; std::mutex mMutex;
}; };
class AndroidOutputDevice : public OutputDevice, public oboe::AudioStreamCallback class AndroidOutputDevice: public OutputDevice, public oboe::AudioStreamCallback
{ {
public: public:
AndroidOutputDevice(int devId); AndroidOutputDevice(int devId);
~AndroidOutputDevice(); ~AndroidOutputDevice();
bool open(); bool open();
void close(); void close();
Format getFormat(); Format getFormat();
bool fakeMode(); bool fakeMode();
void setFakeMode(bool fakemode); void setFakeMode(bool fakemode);
oboe::DataCallbackResult onAudioReady(oboe::AudioStream* audioStream, void* audioData, int32_t numFrames); oboe::DataCallbackResult onAudioReady(oboe::AudioStream *audioStream, void *audioData, int32_t numFrames);
void onErrorAfterClose(oboe::AudioStream* stream, oboe::Result result); void onErrorAfterClose(oboe::AudioStream *stream, oboe::Result result);
protected: protected:
std::mutex mMutex; std::mutex mMutex;
int mDeviceRate = 0; int mDeviceRate = 0;
oboe::AudioStream* mPlayingStream = nullptr; oboe::AudioStream* mPlayingStream = nullptr;
DataWindow mPlayBuffer; DataWindow mPlayBuffer;
int mBufferIndex = 0, mBufferSize = 0; int mBufferIndex = 0, mBufferSize = 0;
bool mInShutdown = false; bool mInShutdown = false;
bool mActive = false; bool mActive = false;
// Adaptive buffer sizing + device-side diagnostics (Step 0 / #5). // Statistics
int32_t mBurstFrames = 0; // frames per burst, latched at open() float mRequestedFrames = 0.0, mStartTime = 0.0, mEndTime = 0.0;
int32_t mXRunLast = 0; // last observed cumulative XRun count };
float mHeartbeatLast = 0.0f; // now_ms() of last device heartbeat log }
// Statistics
float mRequestedFrames = 0.0, mStartTime = 0.0, mEndTime = 0.0;
};
} // namespace Audio
#endif // TARGET_ANDROID #endif // TARGET_ANDROID
File diff suppressed because it is too large Load Diff
+103 -96
View File
@@ -25,71 +25,69 @@
namespace Audio namespace Audio
{ {
class AudioException : public Exception class AudioException: public Exception
{ {
public: public:
AudioException(int code, OSStatus subcode) : Exception(code, int(subcode)) {} AudioException(int code, OSStatus subcode)
:Exception(code, int(subcode))
{}
}; };
// #ifndef AudioDeviceID //#ifndef AudioDeviceID
// # define AudioDeviceID unsigned //# define AudioDeviceID unsigned
// #endif //#endif
class MacEnumerator : public Enumerator class MacEnumerator: public Enumerator
{ {
public: public:
MacEnumerator(); MacEnumerator();
~MacEnumerator(); ~MacEnumerator();
void open(int direction); void open(int direction);
void close(); void close();
int count(); int count();
std::tstring nameAt(int index); std::tstring nameAt(int index);
int idAt(int index); int idAt(int index);
int indexOfDefaultDevice(); int indexOfDefaultDevice();
protected: protected:
struct DeviceInfo struct DeviceInfo
{ {
AudioDeviceID mId; AudioDeviceID mId;
std::string mName; std::string mName;
bool mCanChangeOutputVolume; bool mCanChangeOutputVolume;
bool mCanChangeInputVolume; bool mCanChangeInputVolume;
int mInputCount, mOutputCount; int mInputCount, mOutputCount;
int mDefaultRate; int mDefaultRate;
DeviceInfo() DeviceInfo(): mId(0), mCanChangeOutputVolume(false), mCanChangeInputVolume(false), mInputCount(0), mOutputCount(0), mDefaultRate(16000) {}
: mId(0), mCanChangeOutputVolume(false), mCanChangeInputVolume(false), mInputCount(0), mOutputCount(0), };
mDefaultRate(16000) std::vector<DeviceInfo> mDeviceList;
{ unsigned mDefaultInput, mDefaultOutput;
} int mDirection;
}; void getInfo(DeviceInfo& di);
std::vector<DeviceInfo> mDeviceList;
unsigned mDefaultInput, mDefaultOutput;
int mDirection;
void getInfo(DeviceInfo& di);
}; };
class CoreAudioUnit class CoreAudioUnit
{ {
public: public:
CoreAudioUnit(); CoreAudioUnit();
~CoreAudioUnit(); ~CoreAudioUnit();
void open(bool voice); void open(bool voice);
void close(); void close();
AudioStreamBasicDescription getFormat(int scope, int bus); AudioStreamBasicDescription getFormat(int scope, int bus);
void setFormat(AudioStreamBasicDescription& format, int scope, int bus); void setFormat(AudioStreamBasicDescription& format, int scope, int bus);
bool getEnabled(int scope, int bus); bool getEnabled(int scope, int bus);
void setEnabled(bool enabled, int scope, int bus); void setEnabled(bool enabled, int scope, int bus);
void makeCurrent(AudioDeviceID deviceId, int scope, int bus); void makeCurrent(AudioDeviceID deviceId, int scope, int bus);
void setCallback(AURenderCallbackStruct cb, int callbackType, int scope, int bus); void setCallback(AURenderCallbackStruct cb, int callbackType, int scope, int bus);
void setBufferFrameSizeInMilliseconds(int ms); void setBufferFrameSizeInMilliseconds(int ms);
int getBufferFrameSize(); int getBufferFrameSize();
void initialize(); void initialize();
AudioUnit getHandle(); AudioUnit getHandle();
protected: protected:
AudioUnit mUnit; AudioUnit mUnit;
}; };
class MacDevice class MacDevice
@@ -98,92 +96,101 @@ public:
MacDevice(int devId); MacDevice(int devId);
~MacDevice(); ~MacDevice();
bool open(); bool open();
void close(); void close();
void setRender(bool render); void setRender(bool render);
void setCapture(bool capture); void setCapture(bool capture);
int getId(); int getId();
Format getFormat(); Format getFormat();
DataConnection* connection(); DataConnection* connection();
void setConnection(DataConnection* c); void setConnection(DataConnection* c);
void provideAudioToSpeaker(int channels, void* buffer, int length); void provideAudioToSpeaker(int channels, void* buffer, int length);
void obtainAudioFromMic(int channels, const void* buffer, int length); void obtainAudioFromMic(int channels, const void* buffer, int length);
protected: protected:
AudioDeviceID mDeviceId; AudioDeviceID mDeviceId;
bool mCapture, mRender; bool mCapture, mRender;
bool mActive; bool mActive;
int mUsageCount; int mUsageCount;
Mutex mGuard; Mutex mGuard;
CoreAudioUnit mAudioUnit; CoreAudioUnit mAudioUnit;
AudioComponent mComponent; AudioComponent mComponent;
AudioStreamBasicDescription mCaptureInputFormat, mCaptureOutputFormat, mRenderInputFormat, mRenderOutputFormat, AudioStreamBasicDescription mCaptureInputFormat, mCaptureOutputFormat, mRenderInputFormat, mRenderOutputFormat, mStreamFormat;
mStreamFormat;
AudioBufferList* mInputBufferList; AudioBufferList* mInputBufferList;
DataConnection* mConnection; DataConnection* mConnection;
SpeexResampler mCaptureResampler, mRenderResampler; SpeexResampler mCaptureResampler, mRenderResampler;
ByteBuffer mTail; ByteBuffer mTail;
DataWindow mInputBuffer, mOutputBuffer; DataWindow mInputBuffer, mOutputBuffer;
bool createUnit(bool voice); bool createUnit(bool voice);
void destroyUnit(); void destroyUnit();
void startStream(); void startStream();
void stopStream(); void stopStream();
void setupStreamFormat(); void setupStreamFormat();
bool createResampleUnit(AudioStreamBasicDescription format); bool createResampleUnit(AudioStreamBasicDescription format);
static OSStatus outputCallback(void* inRefCon, AudioUnitRenderActionFlags* ioActionFlags, static OSStatus outputCallback( void *inRefCon,
const AudioTimeStamp* inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioUnitRenderActionFlags *ioActionFlags,
AudioBufferList* ioData); const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData );
static OSStatus inputCallback(void* inRefCon, AudioUnitRenderActionFlags* ioActionFlags, static OSStatus inputCallback(void *inRefCon,
const AudioTimeStamp* inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioUnitRenderActionFlags *ioActionFlags,
AudioBufferList* ioData); const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData);
#ifdef TARGET_IOS #ifdef TARGET_IOS
static void propListener(void* inClientData, AudioSessionPropertyID inID, UInt32 inDataSize, const void* inData); static void propListener(void *inClientData,
static void interruptionListener(void* inClientData, UInt32 inInterruption); AudioSessionPropertyID inID,
UInt32 inDataSize,
const void * inData);
static void interruptionListener(void *inClientData, UInt32 inInterruption);
#endif #endif
}; };
typedef std::shared_ptr<MacDevice> PMacDevice; typedef std::shared_ptr<MacDevice> PMacDevice;
class MacInputDevice : public InputDevice class MacInputDevice: public InputDevice
{ {
public: public:
MacInputDevice(int devId); MacInputDevice(int devId);
~MacInputDevice(); ~MacInputDevice();
bool open(); bool open();
void close(); void close();
Format getFormat(); Format getFormat();
bool fakeMode(); bool fakeMode();
void setFakeMode(bool fakemode); void setFakeMode(bool fakemode);
int readBuffer(void* buffer); int readBuffer(void* buffer);
protected: protected:
PMacDevice mDevice; PMacDevice mDevice;
}; };
class MacOutputDevice : public OutputDevice class MacOutputDevice: public OutputDevice
{ {
public: public:
MacOutputDevice(int devId); MacOutputDevice(int devId);
~MacOutputDevice(); ~MacOutputDevice();
bool open(); bool open();
void close(); void close();
Format getFormat(); Format getFormat();
bool fakeMode(); bool fakeMode();
void setFakeMode(bool fakemode); void setFakeMode(bool fakemode);
protected: protected:
PMacDevice mDevice; PMacDevice mDevice;
}; };
} // namespace Audio }
#endif // TARGET_OSX #endif // TARGET_OSX
+103 -134
View File
@@ -1,4 +1,4 @@
/* Copyright(C) 2007-2026 VoIP objects (voipobjects.com) /* Copyright(C) 2007-2018 VoIP objects (voipobjects.com)
* This Source Code Form is subject to the terms of the Mozilla Public * This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
@@ -8,204 +8,173 @@
using namespace Audio; using namespace Audio;
DataWindow::DataWindow() {} DataWindow::DataWindow()
{
mFilled = 0;
mData = NULL;
mCapacity = 0;
}
DataWindow::~DataWindow() DataWindow::~DataWindow()
{ {
if (mData) if (mData)
{ free(mData);
free(mData);
mData = nullptr;
}
} }
void DataWindow::setCapacity(size_t capacity) void DataWindow::setCapacity(int capacity)
{ {
Lock l(mMutex); Lock l(mMutex);
int tail = capacity - mCapacity;
// The window only ever grows; a smaller request keeps the current buffer. mData = (char*)realloc(mData, capacity);
if (capacity <= mCapacity) if (tail > 0)
return; memset(mData + mCapacity, 0, tail);
mCapacity = capacity;
size_t tail = capacity - mCapacity;
char* buffer = mData;
mData = (char*)realloc(mData, capacity);
if (!mData)
{
// Realloc failed
mData = buffer;
throw std::bad_alloc();
}
if (tail > 0)
memset(mData + mCapacity, 0, tail);
mCapacity = capacity;
} }
void DataWindow::addZero(size_t length) void DataWindow::addZero(int length)
{ {
Lock l(mMutex); Lock l(mMutex);
if (length > mCapacity) if (length > mCapacity)
length = mCapacity; length = mCapacity;
int avail = mCapacity - mFilled; int avail = mCapacity - mFilled;
if (avail < length) if (avail < length)
{ {
memmove(mData, mData + length - avail, mFilled - (length - avail)); memmove(mData, mData + length - avail, mFilled - (length - avail));
mFilled -= length - avail; mFilled -= length - avail;
} }
memset(mData + mFilled, 0, length); memset(mData + mFilled, 0, length);
mFilled += length; mFilled += length;
} }
void DataWindow::add(const void* data, size_t length) void DataWindow::add(const void* data, int length)
{ {
Lock l(mMutex); Lock l(mMutex);
if (length > mCapacity) if (length > mCapacity)
{ {
// Use latest bytes from data buffer in this case. // Use latest bytes from data buffer in this case.
data = (char*)data + length - mCapacity; data = (char*)data + length - mCapacity;
length = mCapacity; length = mCapacity;
} }
// Check how much free space we have // Check how much free space we have
int avail = mCapacity - mFilled; int avail = mCapacity - mFilled;
if (avail < length) if (avail < length)
{ {
// Find the portion of data to move & save // Find the portion of data to move & save
int delta = length - avail; int delta = length - avail;
// Move the data // Move the data
if (mFilled - delta > 0) if (mFilled - delta > 0)
memmove(mData, mData + delta, mFilled - delta); memmove(mData, mData + delta, mFilled - delta);
mFilled -= delta; mFilled -= delta;
} }
memcpy(mData + mFilled, data, length); memcpy(mData + mFilled, data, length);
mFilled += length; mFilled += length;
} }
void DataWindow::add(short sample) void DataWindow::add(short sample)
{ {
add(&sample, sizeof sample); add(&sample, sizeof sample);
} }
void DataWindow::erase(size_t length) void DataWindow::erase(int length)
{ {
Lock l(mMutex); Lock l(mMutex);
if (length > mFilled) if (length > mFilled)
length = mFilled; length = mFilled;
if (length != mFilled) if (length != mFilled)
memmove(mData, mData + length, mFilled - length); memmove(mData, mData + length, mFilled - length);
mFilled -= length; mFilled -= length;
} }
const char* DataWindow::data() const const char* DataWindow::data() const
{ {
return mData; return mData;
} }
char* DataWindow::mutableData() char* DataWindow::mutableData()
{ {
return mData; return mData;
} }
void DataWindow::clear() void DataWindow::clear()
{ {
Lock l(mMutex); Lock l(mMutex);
mFilled = 0; mFilled = 0;
} }
short DataWindow::shortAt(size_t index) const short DataWindow::shortAt(int index) const
{ {
Lock l(mMutex); Lock l(mMutex);
assert(index < mFilled / 2); assert(index < mFilled / 2);
return ((short*)mData)[index]; return ((short*)mData)[index];
} }
void DataWindow::setShortAt(short value, size_t index) void DataWindow::setShortAt(short value, int index)
{ {
Lock l(mMutex); Lock l(mMutex);
assert(index < mFilled / 2); assert(index < mFilled / 2);
((short*)mData)[index] = value; ((short*)mData)[index] = value;
} }
size_t DataWindow::read(void* buffer, size_t length) int DataWindow::read(void* buffer, int length)
{ {
Lock l(mMutex); Lock l(mMutex);
if (length > mFilled) if (length > mFilled)
length = mFilled; length = mFilled;
if (length) if (length)
{ {
if (buffer) if (buffer)
memcpy(buffer, mData, length); memcpy(buffer, mData, length);
if (length < mFilled) if (length < mFilled)
memmove(mData, mData + length, mFilled - length); memmove(mData, mData+length, mFilled - length);
mFilled -= length; mFilled -= length;
} }
return length; return length;
} }
size_t DataWindow::filled() const int DataWindow::filled() const
{ {
Lock l(mMutex); Lock l(mMutex);
return mFilled; return mFilled;
} }
void DataWindow::setFilled(size_t filled) void DataWindow::setFilled(int filled)
{ {
Lock l(mMutex); Lock l(mMutex);
if (filled > mCapacity) mFilled = filled;
throw std::bad_alloc();
mFilled = filled;
} }
size_t DataWindow::capacity() const int DataWindow::capacity() const
{ {
Lock l(mMutex); Lock l(mMutex);
return mCapacity; return mCapacity;
} }
void DataWindow::zero(size_t length) void DataWindow::zero(int length)
{ {
Lock l(mMutex); Lock l(mMutex);
assert(length <= mCapacity); assert(length <= mCapacity);
mFilled = length; mFilled = length;
memset(mData, 0, mFilled); memset(mData, 0, mFilled);
}
size_t DataWindow::moveTo(DataWindow& dst, size_t size)
{
Lock l(mMutex);
size_t avail = std::min(size, (size_t)filled());
if (avail != 0)
{
dst.add(mData, avail);
erase(avail);
}
return avail;
}
std::chrono::milliseconds DataWindow::getTimeLength(const Audio::Format& fmt) const
{
Lock l(mMutex);
return std::chrono::milliseconds(mFilled / sizeof(short) / fmt.channels() / (fmt.rate() / 1000));
} }
void DataWindow::makeStereoFromMono(DataWindow& dst, DataWindow& src) void DataWindow::makeStereoFromMono(DataWindow& dst, DataWindow& src)
{ {
Lock lockDst(dst.mMutex), lockSrc(src.mMutex); Lock lockDst(dst.mMutex), lockSrc(src.mMutex);
dst.setCapacity(src.filled() * 2); dst.setCapacity(src.filled()*2);
short* input = (short*)src.mutableData(); short* input = (short*)src.mutableData();
short* output = (short*)dst.mutableData(); short* output = (short*)dst.mutableData();
for (int i = 0; i < src.filled() / 2; i++) for (int i=0; i<src.filled()/2; i++)
output[i * 2] = output[i * 2 + 1] = input[i]; output[i*2] = output[i*2+1] = input[i];
dst.mFilled = src.filled() * 2; dst.mFilled = src.filled() * 2;
} }
+25 -29
View File
@@ -8,44 +8,40 @@
#include "../helper/HL_ByteBuffer.h" #include "../helper/HL_ByteBuffer.h"
#include "../helper/HL_Sync.h" #include "../helper/HL_Sync.h"
#include "Audio_Interface.h"
namespace Audio namespace Audio
{ {
class DataWindow class DataWindow
{ {
public: public:
DataWindow(); DataWindow();
~DataWindow(); ~DataWindow();
void setCapacity(size_t capacity); void setCapacity(int capacity);
size_t capacity() const; int capacity() const;
void addZero(size_t length); void addZero(int length);
void add(const void* data, size_t length); void add(const void* data, int length);
void add(short sample); void add(short sample);
size_t read(void* buffer, size_t length); int read(void* buffer, int length);
void erase(size_t length); void erase(int length = -1);
const char* data() const; const char* data() const;
char* mutableData(); char* mutableData();
size_t filled() const; int filled() const;
void setFilled(size_t filled); void setFilled(int filled);
void clear(); void clear();
short shortAt(size_t index) const; short shortAt(int index) const;
void setShortAt(short value, size_t index); void setShortAt(short value, int index);
void zero(size_t length); void zero(int length);
size_t moveTo(DataWindow& dst, size_t size /* in bytes*/);
std::chrono::milliseconds getTimeLength(const Format& fmt) const; static void makeStereoFromMono(DataWindow& dst, DataWindow& src);
static void makeStereoFromMono(DataWindow& dst, DataWindow& src); protected:
protected:
mutable Mutex mMutex; mutable Mutex mMutex;
char* mData = nullptr; char* mData;
size_t mFilled = 0; int mFilled;
size_t mCapacity = 0; int mCapacity;
}; };
} // namespace Audio }
#endif #endif
+139 -185
View File
@@ -1,286 +1,240 @@
/* Copyright(C) 2007-2026 VoIPobjects (voipobjects.com) /* Copyright(C) 2007-2017 VoIPobjects (voipobjects.com)
* This Source Code Form is subject to the terms of the Mozilla Public * This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#define NOMINMAX
#include "Audio_DevicePair.h" #include "Audio_DevicePair.h"
#include <algorithm> #include <algorithm>
#include <assert.h> #include <assert.h>
#define LOG_SUBSYSTEM "audio" #define LOG_SUBSYSTEM "Audio"
using namespace Audio; using namespace Audio;
// --- DevicePair --- // --- DevicePair ---
DevicePair::DevicePair() DevicePair::DevicePair(bool aec, bool agc)
: mConfig(nullptr), mDelegate(nullptr), mAec(false), mAgc(false), :mConfig(NULL), mDelegate(NULL), mAec(aec), mAgc(agc), mAecFilter(AUDIO_MIC_BUFFER_LENGTH*10, AUDIO_MIC_BUFFER_LENGTH, AUDIO_SAMPLERATE), mAgcFilter(AUDIO_CHANNELS)
mAecFilter(AUDIO_MIC_BUFFER_LENGTH * 10, AUDIO_MIC_BUFFER_LENGTH, AUDIO_SAMPLERATE), mAgcFilter(AUDIO_CHANNELS),
mMonitoring(nullptr)
{ {
mInputBuffer.setCapacity(AUDIO_MIC_BUFFER_SIZE * (AUDIO_MIC_BUFFER_COUNT + 1)); mInputBuffer.setCapacity(AUDIO_MIC_BUFFER_SIZE * (AUDIO_MIC_BUFFER_COUNT + 1));
mOutputBuffer.setCapacity(AUDIO_SPK_BUFFER_SIZE * (AUDIO_SPK_BUFFER_COUNT + 1)); mOutputBuffer.setCapacity(AUDIO_SPK_BUFFER_SIZE * (AUDIO_SPK_BUFFER_COUNT + 1));
mInputResampingData.setCapacity(AUDIO_MIC_BUFFER_SIZE * (AUDIO_MIC_BUFFER_COUNT + 1)); mInputResampingData.setCapacity(AUDIO_MIC_BUFFER_SIZE * (AUDIO_MIC_BUFFER_COUNT + 1));
mOutput10msBuffer.setCapacity((int)Format().sizeFromTime(AUDIO_SPK_BUFFER_LENGTH)); mOutput10msBuffer.setCapacity((int)Format().sizeFromTime(AUDIO_SPK_BUFFER_LENGTH));
mOutputNativeData.setCapacity((int)Format().sizeFromTime(AUDIO_SPK_BUFFER_LENGTH * AUDIO_SPK_BUFFER_COUNT * 24)); mOutputNativeData.setCapacity((int)Format().sizeFromTime(AUDIO_SPK_BUFFER_LENGTH * AUDIO_SPK_BUFFER_COUNT * 24));
} }
DevicePair::~DevicePair() DevicePair::~DevicePair()
{ {
if (mInput) if (mInput)
{ {
if (mInput->connection() == this) if (mInput->connection() == this)
mInput->setConnection(nullptr); mInput->setConnection(NULL);
mInput.reset(); mInput.reset();
} }
if (mOutput) if (mOutput)
{ {
if (mOutput->connection() == this) if (mOutput->connection() == this)
mOutput->setConnection(nullptr); mOutput->setConnection(NULL);
mOutput.reset(); mOutput.reset();
} }
} }
DevicePair& DevicePair::setAec(bool aec)
{
mAec = aec;
return *this;
}
bool DevicePair::aec()
{
return mAec;
}
DevicePair& DevicePair::setAgc(bool agc)
{
mAgc = agc;
return *this;
}
bool DevicePair::agc()
{
return mAgc;
}
VariantMap* DevicePair::config() VariantMap* DevicePair::config()
{ {
return mConfig; return mConfig;
} }
DevicePair& DevicePair::setConfig(VariantMap* config) void DevicePair::setConfig(VariantMap* config)
{ {
mConfig = config; mConfig = config;
return *this;
} }
PInputDevice DevicePair::input() PInputDevice DevicePair::input()
{ {
return mInput; return mInput;
} }
DevicePair& DevicePair::setInput(PInputDevice input) void DevicePair::setInput(PInputDevice input)
{ {
if (mInput == input) if (mInput == input)
return *this; return;
mInput = input; mInput = input;
mInput->setConnection(this); mInput->setConnection(this);
if (mDelegate) if (mDelegate)
mDelegate->deviceChanged(this); mDelegate->deviceChanged(this);
return *this;
} }
POutputDevice DevicePair::output() POutputDevice DevicePair::output()
{ {
return mOutput; return mOutput;
} }
DevicePair& DevicePair::setOutput(POutputDevice output) void DevicePair::setOutput(POutputDevice output)
{ {
if (output == mOutput) if (output == mOutput)
return *this; return;
mOutput = output;
mOutput = output; mOutput->setConnection(this);
mOutput->setConnection(this); if (mDelegate)
if (mDelegate) mDelegate->deviceChanged(this);
mDelegate->deviceChanged(this);
return *this;
} }
bool DevicePair::start() bool DevicePair::start()
{ {
bool result = false; bool result = false;
if (mInput) if (mInput)
result = mInput->open(); result = mInput->open();
if (mOutput && result) if (mOutput && result)
result &= mOutput->open(); result &= mOutput->open();
return result;
return result;
} }
void DevicePair::stop() void DevicePair::stop()
{ {
if (mInput) if (mInput)
mInput->close(); mInput->close();
if (mOutput) if (mOutput)
mOutput->close(); mOutput->close();
} }
DevicePair& DevicePair::setDelegate(Delegate* dc) void DevicePair::setDelegate(Delegate* dc)
{ {
mDelegate = dc; mDelegate = dc;
return *this;
} }
DevicePair::Delegate* DevicePair::delegate() DevicePair::Delegate* DevicePair::delegate()
{ {
return mDelegate; return mDelegate;
}
DevicePair& DevicePair::setMonitoring(DataConnection* monitoring)
{
mMonitoring = monitoring;
return *this;
}
DataConnection* DevicePair::monitoring()
{
return mMonitoring;
} }
Player& DevicePair::player() Player& DevicePair::player()
{ {
return mPlayer; return mPlayer;
} }
void DevicePair::onMicData(const Format& f, const void* buffer, int length) void DevicePair::onMicData(const Format& f, const void* buffer, int length)
{ {
#ifdef DUMP_NATIVEINPUT #ifdef DUMP_NATIVEINPUT
if (!mNativeInputDump) if (!mNativeInputDump)
{ {
mNativeInputDump = std::make_shared<WavFileWriter>(); mNativeInputDump = std::make_shared<WavFileWriter>();
mNativeInputDump->open("nativeinput.wav", f.mRate, f.mChannels); mNativeInputDump->open("nativeinput.wav", f.mRate, f.mChannels);
} }
if (mNativeInputDump) if (mNativeInputDump)
mNativeInputDump->write(buffer, length); mNativeInputDump->write(buffer, length);
#endif #endif
// send the data to internal queue - it can hold data which were not processed by resampler in last call // send the data to internal queue - it can hold data which were not processed by resampler in last call
mInputResampingData.add(buffer, length); mInputResampingData.add(buffer, length);
// split processing by blocks // split processing by blocks
int blocks = mInputResampingData.filled() / (int)f.sizeFromTime(AUDIO_MIC_BUFFER_LENGTH); int blocks = mInputResampingData.filled() / (int)f.sizeFromTime(AUDIO_MIC_BUFFER_LENGTH);
for (int blockIndex = 0; blockIndex < blocks; blockIndex++) for (int blockIndex = 0; blockIndex < blocks; blockIndex++)
{ {
size_t wasProcessed = 0;
size_t wasProduced = mMicResampler.resample(f.mRate, // Source rate size_t wasProcessed = 0;
mInputResampingData.data(), // Source data
(int)f.sizeFromTime(AUDIO_MIC_BUFFER_LENGTH), // Source size
wasProcessed,
AUDIO_SAMPLERATE, // Dest rate
mInputBuffer.mutableData() + mInputBuffer.filled(),
mInputBuffer.capacity() - mInputBuffer.filled());
mInputBuffer.setFilled(mInputBuffer.filled() + wasProduced); size_t wasProduced = mMicResampler.resample(f.mRate, // Source rate
mInputResampingData.erase((int)f.sizeFromTime(AUDIO_MIC_BUFFER_LENGTH)); mInputResampingData.data(), // Source data
processMicData(Format(), mInputBuffer.mutableData(), (int)Format().sizeFromTime(AUDIO_MIC_BUFFER_LENGTH)); (int)f.sizeFromTime(AUDIO_MIC_BUFFER_LENGTH), // Source size
wasProcessed,
AUDIO_SAMPLERATE, // Dest rate
mInputBuffer.mutableData() + mInputBuffer.filled(),
mInputBuffer.capacity() - mInputBuffer.filled());
mInputBuffer.setFilled(mInputBuffer.filled() + wasProduced);
mInputResampingData.erase((int)f.sizeFromTime(AUDIO_MIC_BUFFER_LENGTH));
processMicData(Format(), mInputBuffer.mutableData(), (int)Format().sizeFromTime(AUDIO_MIC_BUFFER_LENGTH));
mInputBuffer.erase((int)Format().sizeFromTime(AUDIO_MIC_BUFFER_LENGTH));
}
mInputBuffer.erase((int)Format().sizeFromTime(AUDIO_MIC_BUFFER_LENGTH));
}
} }
void DevicePair::onSpkData(const Format& f, void* buffer, int length) void DevicePair::onSpkData(const Format& f, void* buffer, int length)
{ {
// ICELogMedia(<< "Audio::DevicePair::onSpkData() begin"); //ICELogMedia(<< "Audio::DevicePair::onSpkData() begin");
#ifdef DUMP_NATIVEOUTPUT #ifdef DUMP_NATIVEOUTPUT
if (!mNativeOutputDump) if (!mNativeOutputDump)
{ {
mNativeOutputDump = std::make_shared<WavFileWriter>(); mNativeOutputDump = std::make_shared<WavFileWriter>();
mNativeOutputDump->open("nativeoutput.wav", f.mRate, f.mChannels); mNativeOutputDump->open("nativeoutput.wav", f.mRate, f.mChannels);
} }
#endif #endif
#ifdef CONSOLE_LOGGING #ifdef CONSOLE_LOGGING
printf("Speaker requests %d\n", length); printf("Speaker requests %d\n", length);
#endif #endif
Format nativeFormat = mOutput->getFormat(); Format nativeFormat = mOutput->getFormat();
// See how much bytes are needed yet - mOutputNativeData can contain some data already // See how much bytes are needed yet - mOutputNativeData can contain some data already
int required = length - mOutputNativeData.filled(); int required = length - mOutputNativeData.filled();
if (required > 0) if (required > 0)
{
// Find how much blocks must be received from RTP/decoder side
int nativeBufferSize = (int)nativeFormat.sizeFromTime(AUDIO_SPK_BUFFER_LENGTH);
int blocks = required / nativeBufferSize;
if (required % nativeBufferSize)
blocks++;
// Now request data from terminal or whetever delegate is
for (int blockIndex = 0; blockIndex < blocks; blockIndex++)
{ {
// Find how much blocks must be received from RTP/decoder side memset(mOutput10msBuffer.mutableData(), 0, (size_t)mOutput10msBuffer.capacity());
int nativeBufferSize = (int)nativeFormat.sizeFromTime(AUDIO_SPK_BUFFER_LENGTH);
int blocks = required / nativeBufferSize;
if (required % nativeBufferSize)
blocks++;
// Now request data from terminal or whetever delegate is if (mDelegate)
for (int blockIndex = 0; blockIndex < blocks; blockIndex++) mDelegate->onSpkData(Format(), mOutput10msBuffer.mutableData(), mOutput10msBuffer.capacity());
{
memset(mOutput10msBuffer.mutableData(), 0, (size_t)mOutput10msBuffer.capacity());
// Ask audio data on main AUDIO_SAMPLERATE frequency // Replace received data with custom file or data playing
if (mDelegate) mPlayer.onSpkData(Format(), mOutput10msBuffer.mutableData(), mOutput10msBuffer.capacity());
mDelegate->onSpkData(Format(), mOutput10msBuffer.mutableData(), mOutput10msBuffer.capacity());
// Replace received data with custom file or data playing // Save it to process with AEC
mPlayer.onSpkData(Format(), mOutput10msBuffer.mutableData(), mOutput10msBuffer.capacity()); if (mAec)
mAecSpkBuffer.add(mOutput10msBuffer.data(), mOutput10msBuffer.capacity());
// Save it to process with AEC // Resample these 10 milliseconds it to native format
if (mAec) size_t wasProcessed = 0;
mAecSpkBuffer.add(mOutput10msBuffer.data(), mOutput10msBuffer.capacity()); size_t wasProduced = mSpkResampler.resample(nativeFormat.mRate, mOutput10msBuffer.data(), mOutput10msBuffer.capacity(), wasProcessed, f.mRate,
mOutputNativeData.mutableData() + mOutputNativeData.filled(), mOutputNativeData.capacity() - mOutputNativeData.filled());
// Resample these 10 milliseconds it to native format mOutputNativeData.setFilled(mOutputNativeData.filled() + wasProduced);
size_t wasProcessed = 0;
size_t wasProduced = mSpkResampler.resample(Format().mRate, mOutput10msBuffer.data(),
mOutput10msBuffer.capacity(), wasProcessed, f.mRate,
mOutputNativeData.mutableData() + mOutputNativeData.filled(),
mOutputNativeData.capacity() - mOutputNativeData.filled());
mOutputNativeData.setFilled(mOutputNativeData.filled() + wasProduced);
#ifdef CONSOLE_LOGGING #ifdef CONSOLE_LOGGING
printf("Resampled %d to %d\n", wasProcessed, wasProduced); printf("Resampled %d to %d\n", wasProcessed, wasProduced);
#endif #endif
}
} }
}
// assert(mOutputNativeData.filled() >= length); // assert(mOutputNativeData.filled() >= length);
#ifdef DUMP_NATIVEOUTPUT #ifdef DUMP_NATIVEOUTPUT
if (mNativeOutputDump) if (mNativeOutputDump)
mNativeOutputDump->write(mOutputNativeData.data(), length); mNativeOutputDump->write(mOutputNativeData.data(), length);
#endif #endif
mOutputNativeData.read(buffer, length); mOutputNativeData.read(buffer, length);
// Send data to monitoring if needed #define AEC_FRAME_SIZE (AUDIO_CHANNELS * (AUDIO_SAMPLERATE / 1000) * AEC_FRAME_TIME * sizeof(short))
if (mMonitoring)
mMonitoring->onSpkData(f, buffer, length);
#define AEC_FRAME_SIZE (AUDIO_CHANNELS * (AUDIO_SAMPLERATE / 1000) * AEC_FRAME_TIME * sizeof(short)) // AEC filter wants frames.
if (mAec)
// AEC filter wants frames. {
if (mAec) int nrOfFrames = mAecSpkBuffer.filled() / AEC_FRAME_SIZE;
{ for (int frameIndex=0; frameIndex < nrOfFrames; frameIndex++)
int nrOfFrames = mAecSpkBuffer.filled() / AEC_FRAME_SIZE; mAecFilter.toSpeaker(mAecSpkBuffer.mutableData() + AEC_FRAME_SIZE * frameIndex);
for (int frameIndex = 0; frameIndex < nrOfFrames; frameIndex++) mAecSpkBuffer.erase(nrOfFrames * AEC_FRAME_SIZE);
mAecFilter.toSpeaker(mAecSpkBuffer.mutableData() + AEC_FRAME_SIZE * frameIndex); }
mAecSpkBuffer.erase(nrOfFrames * AEC_FRAME_SIZE); //ICELogMedia(<< "Audio::DevicePair::onSpkData() end")
}
} }
void DevicePair::processMicData(const Format& f, void* buffer, int length) void DevicePair::processMicData(const Format& f, void* buffer, int length)
{ {
if (mAgc) if (mAgc)
mAgcFilter.process(buffer, length); mAgcFilter.process(buffer, length);
if (mAec) if (mAec)
mAecFilter.fromMic(buffer); mAecFilter.fromMic(buffer);
if (mDelegate) if (mDelegate)
mDelegate->onMicData(f, buffer, length); mDelegate->onMicData(f, buffer, length);
} }
+36 -40
View File
@@ -11,62 +11,58 @@
#include "Audio_Resampler.h" #include "Audio_Resampler.h"
#include "Audio_DataWindow.h" #include "Audio_DataWindow.h"
// #define DUMP_NATIVEOUTPUT //#define DUMP_NATIVEOUTPUT
// #define DUMP_NATIVEINPUT //#define DUMP_NATIVEINPUT
namespace Audio namespace Audio
{ {
class DevicePair : protected DataConnection class DevicePair: protected DataConnection
{ {
public: public:
class Delegate : public DataConnection class Delegate: public DataConnection
{ {
public: public:
virtual void deviceChanged(DevicePair* dpair) = 0; virtual void deviceChanged(DevicePair* dpair) = 0;
}; };
DevicePair(); DevicePair(bool aec = true, bool agc = true);
virtual ~DevicePair(); virtual ~DevicePair();
DevicePair& setAec(bool aec); void setAec(bool aec);
bool aec(); bool aec();
DevicePair& setAgc(bool agc); void setAgc(bool agc);
bool agc(); bool agc();
VariantMap* config(); VariantMap* config();
DevicePair& setConfig(VariantMap* config); void setConfig(VariantMap* config);
PInputDevice input(); PInputDevice input();
DevicePair& setInput(PInputDevice input); void setInput(PInputDevice input);
POutputDevice output(); POutputDevice output();
DevicePair& setOutput(POutputDevice output); void setOutput(POutputDevice output);
bool start(); bool start();
void stop(); void stop();
DevicePair& setDelegate(Delegate* dc); void setDelegate(Delegate* dc);
Delegate* delegate(); Delegate* delegate();
DevicePair& setMonitoring(DataConnection* monitoring); Player& player();
DataConnection* monitoring();
Player& player(); protected:
VariantMap* mConfig;
protected: PInputDevice mInput;
VariantMap* mConfig; POutputDevice mOutput;
PInputDevice mInput; Delegate* mDelegate;
POutputDevice mOutput; bool mAec;
Delegate* mDelegate; bool mAgc;
bool mAec; AgcFilter mAgcFilter;
bool mAgc; AecFilter mAecFilter;
AgcFilter mAgcFilter; Player mPlayer;
AecFilter mAecFilter;
Player mPlayer;
UniversalResampler mMicResampler, mSpkResampler; UniversalResampler mMicResampler, mSpkResampler;
DataWindow mInputBuffer, mOutputBuffer, mAecSpkBuffer, mInputResampingData, mOutputNativeData, mOutput10msBuffer; DataWindow mInputBuffer, mOutputBuffer, mAecSpkBuffer, mInputResampingData, mOutputNativeData, mOutput10msBuffer;
DataConnection* mMonitoring;
#ifdef DUMP_NATIVEOUTPUT #ifdef DUMP_NATIVEOUTPUT
std::shared_ptr<WavFileWriter> mNativeOutputDump; std::shared_ptr<WavFileWriter> mNativeOutputDump;
@@ -77,9 +73,9 @@ protected:
void onMicData(const Format& f, const void* buffer, int length); void onMicData(const Format& f, const void* buffer, int length);
void onSpkData(const Format& f, void* buffer, int length); void onSpkData(const Format& f, void* buffer, int length);
void processMicData(const Format& f, void* buffer, int length); void processMicData(const Format& f, void* buffer, int length);
}; };
typedef std::shared_ptr<DevicePair> PDevicePair; typedef std::shared_ptr<DevicePair> PDevicePair;
} // namespace Audio }
#endif #endif
+155 -138
View File
@@ -19,10 +19,10 @@
#define DRVM_MAPPER_CONSOLEVOICECOM_GET (0x2000 + 23) #define DRVM_MAPPER_CONSOLEVOICECOM_GET (0x2000 + 23)
#define DRVM_MAPPER_PREFERRED_GET (0x2000 + 21) #define DRVM_MAPPER_PREFERRED_GET (0x2000 + 21)
#define DRV_QUERYFUNCTIONINSTANCEID (DRV_RESERVED + 17) #define DRV_QUERYFUNCTIONINSTANCEID (DRV_RESERVED + 17)
#define DRV_QUERYFUNCTIONINSTANCEIDSIZE (DRV_RESERVED + 18) #define DRV_QUERYFUNCTIONINSTANCEIDSIZE (DRV_RESERVED + 18)
#define LOG_SUBSYSTEM "audio" #define LOG_SUBSYSTEM "DirectSound"
using namespace Audio; using namespace Audio;
@@ -39,29 +39,32 @@ public:
{ {
HINSTANCE mInstance; HINSTANCE mInstance;
HRESULT(WINAPI* DirectSoundCreate8)(LPGUID, LPDIRECTSOUND8*, LPUNKNOWN); HRESULT (WINAPI *DirectSoundCreate8)(LPGUID, LPDIRECTSOUND8 *, LPUNKNOWN);
HRESULT(WINAPI* DirectSoundEnumerateW)(LPDSENUMCALLBACKW, LPVOID); HRESULT (WINAPI *DirectSoundEnumerateW)(LPDSENUMCALLBACKW, LPVOID);
HRESULT(WINAPI* DirectSoundEnumerateA)(LPDSENUMCALLBACKA, LPVOID); HRESULT (WINAPI *DirectSoundEnumerateA)(LPDSENUMCALLBACKA, LPVOID);
HRESULT(WINAPI* DirectSoundCaptureCreate8)(LPGUID, LPDIRECTSOUNDCAPTURE8*, LPUNKNOWN); HRESULT (WINAPI *DirectSoundCaptureCreate8)(LPGUID, LPDIRECTSOUNDCAPTURE8* , LPUNKNOWN);
HRESULT(WINAPI* DirectSoundCaptureEnumerateW)(LPDSENUMCALLBACKW, LPVOID); HRESULT (WINAPI *DirectSoundCaptureEnumerateW)(LPDSENUMCALLBACKW, LPVOID);
HRESULT(WINAPI* DirectSoundCaptureEnumerateA)(LPDSENUMCALLBACKA, LPVOID); HRESULT (WINAPI *DirectSoundCaptureEnumerateA)(LPDSENUMCALLBACKA, LPVOID);
HRESULT(WINAPI* GetDeviceID)(LPCGUID src, LPGUID dst); HRESULT (WINAPI *GetDeviceID)(LPCGUID src, LPGUID dst);
} mRoutines; } mRoutines;
protected: protected:
LPDIRECTSOUND mDirectSound; LPDIRECTSOUND mDirectSound;
Mutex mGuard; Mutex mGuard;
unsigned int mRefCount; unsigned int mRefCount;
}; };
DSoundInit gDSoundInit; DSoundInit gDSoundInit;
DSoundInit::DSoundInit() : mRefCount(0) {} DSoundInit::DSoundInit()
:mRefCount(0)
{
}
DSoundInit::~DSoundInit() DSoundInit::~DSoundInit()
{ {
// Unload(); //Unload();
} }
void DSoundInit::load() void DSoundInit::load()
@@ -74,21 +77,16 @@ void DSoundInit::load()
hr = ::CoInitialize(NULL); hr = ::CoInitialize(NULL);
// load the DirectSound DLL //load the DirectSound DLL
mRoutines.mInstance = ::LoadLibraryW(L"dsound.dll"); mRoutines.mInstance = ::LoadLibraryW(L"dsound.dll");
if (!mRoutines.mInstance) if (!mRoutines.mInstance)
throw std::logic_error("Cannot load dsound.dll"); throw std::logic_error("Cannot load dsound.dll");
mRoutines.DirectSoundCaptureCreate8 = mRoutines.DirectSoundCaptureCreate8 = (HRESULT (WINAPI *)(LPGUID, LPDIRECTSOUNDCAPTURE8 *, LPUNKNOWN))::GetProcAddress(mRoutines.mInstance, "DirectSoundCaptureCreate8");
(HRESULT(WINAPI*)(LPGUID, LPDIRECTSOUNDCAPTURE8*, LPUNKNOWN))::GetProcAddress(mRoutines.mInstance, mRoutines.DirectSoundCaptureEnumerateW = (HRESULT (WINAPI *)(LPDSENUMCALLBACKW, LPVOID))::GetProcAddress(mRoutines.mInstance, "DirectSoundCaptureEnumerateW");
"DirectSoundCaptureCreate8"); mRoutines.DirectSoundCreate8 = (HRESULT (WINAPI *)(LPGUID, LPDIRECTSOUND8 *, LPUNKNOWN))::GetProcAddress(mRoutines.mInstance, "DirectSoundCreate8");
mRoutines.DirectSoundCaptureEnumerateW = (HRESULT(WINAPI*)(LPDSENUMCALLBACKW, LPVOID))::GetProcAddress( mRoutines.DirectSoundEnumerateW = (HRESULT (WINAPI *)(LPDSENUMCALLBACKW, LPVOID))::GetProcAddress(mRoutines.mInstance, "DirectSoundEnumerateW");
mRoutines.mInstance, "DirectSoundCaptureEnumerateW"); mRoutines.GetDeviceID = (HRESULT (WINAPI*) (LPCGUID, LPGUID)) GetProcAddress(mRoutines.mInstance, "GetDeviceID");
mRoutines.DirectSoundCreate8 = (HRESULT(WINAPI*)(LPGUID, LPDIRECTSOUND8*, LPUNKNOWN))::GetProcAddress(
mRoutines.mInstance, "DirectSoundCreate8");
mRoutines.DirectSoundEnumerateW =
(HRESULT(WINAPI*)(LPDSENUMCALLBACKW, LPVOID))::GetProcAddress(mRoutines.mInstance, "DirectSoundEnumerateW");
mRoutines.GetDeviceID = (HRESULT(WINAPI*)(LPCGUID, LPGUID))GetProcAddress(mRoutines.mInstance, "GetDeviceID");
} }
} }
@@ -108,7 +106,10 @@ void DSoundInit::unload()
} }
// --------------- VistaEnumerator --------------------- // --------------- VistaEnumerator ---------------------
VistaEnumerator::VistaEnumerator() : mCollection(NULL), mDefaultDevice(NULL), mEnumerator(NULL), mDirection(eCapture) {} VistaEnumerator::VistaEnumerator()
:mCollection(NULL), mDefaultDevice(NULL), mEnumerator(NULL), mDirection(eCapture)
{
}
VistaEnumerator::~VistaEnumerator() VistaEnumerator::~VistaEnumerator()
{ {
@@ -118,12 +119,14 @@ VistaEnumerator::~VistaEnumerator()
void VistaEnumerator::open(int direction) void VistaEnumerator::open(int direction)
{ {
const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator); const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator); const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
mDirection = (direction == myMicrophone) ? eCapture : eRender; mDirection = (direction == myMicrophone) ? eCapture : eRender;
HRESULT hr = HRESULT hr = CoCreateInstance(
CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&mEnumerator); CLSID_MMDeviceEnumerator, NULL,
CLSCTX_ALL, IID_IMMDeviceEnumerator,
(void**)&mEnumerator);
if (!mEnumerator) if (!mEnumerator)
return; return;
@@ -149,7 +152,7 @@ void VistaEnumerator::close()
if (mDefaultDevice) if (mDefaultDevice)
{ {
// mDefaultDevice->Release(); //mDefaultDevice->Release();
mDefaultDevice = NULL; mDefaultDevice = NULL;
} }
@@ -159,7 +162,7 @@ void VistaEnumerator::close()
mEnumerator = NULL; mEnumerator = NULL;
} }
} }
catch (...) catch(...)
{ {
} }
} }
@@ -172,9 +175,9 @@ IMMDevice* VistaEnumerator::mapIndexToInterface(int index)
if (index == -1) if (index == -1)
return mDefaultDevice; return mDefaultDevice;
size_t idSize = 0; size_t idSize = 0;
MMRESULT mmres = 0; MMRESULT mmres = 0;
WCHAR* id = NULL; WCHAR* id = NULL;
if (mDirection == eCapture) if (mDirection == eCapture)
{ {
mmres = waveInMessage((HWAVEIN)index, DRV_QUERYFUNCTIONINSTANCEIDSIZE, (DWORD_PTR)&idSize, NULL); mmres = waveInMessage((HWAVEIN)index, DRV_QUERYFUNCTIONINSTANCEIDSIZE, (DWORD_PTR)&idSize, NULL);
@@ -182,7 +185,7 @@ IMMDevice* VistaEnumerator::mapIndexToInterface(int index)
if (mmres != MMSYSERR_NOERROR) if (mmres != MMSYSERR_NOERROR)
return NULL; return NULL;
id = (WCHAR*)_alloca(idSize * sizeof(WCHAR)); id = (WCHAR*)_alloca(idSize*sizeof(WCHAR));
mmres = waveInMessage((HWAVEIN)index, DRV_QUERYFUNCTIONINSTANCEID, (DWORD_PTR)id, idSize); mmres = waveInMessage((HWAVEIN)index, DRV_QUERYFUNCTIONINSTANCEID, (DWORD_PTR)id, idSize);
} }
else else
@@ -192,7 +195,7 @@ IMMDevice* VistaEnumerator::mapIndexToInterface(int index)
if (mmres != MMSYSERR_NOERROR) if (mmres != MMSYSERR_NOERROR)
return NULL; return NULL;
id = (WCHAR*)_alloca(idSize * sizeof(WCHAR)); id = (WCHAR*)_alloca(idSize*sizeof(WCHAR));
mmres = waveOutMessage((HWAVEOUT)index, DRV_QUERYFUNCTIONINSTANCEID, (DWORD_PTR)id, idSize); mmres = waveOutMessage((HWAVEOUT)index, DRV_QUERYFUNCTIONINSTANCEID, (DWORD_PTR)id, idSize);
} }
@@ -210,7 +213,7 @@ void VistaEnumerator::enumerate()
mNameList.clear(); mNameList.clear();
int res = (int)count(); int res = (int)count();
for (int i = 0; i < res; i++) for (int i=0; i<res; i++)
{ {
IMMDevice* dev = mapIndexToInterface(i); IMMDevice* dev = mapIndexToInterface(i);
if (dev) if (dev)
@@ -255,23 +258,26 @@ int VistaEnumerator::indexOfDefaultDevice()
if (mDirection == mySpeaker) if (mDirection == mySpeaker)
{ {
if (waveOutMessage((HWAVEOUT)WAVE_MAPPER, DRVM_MAPPER_CONSOLEVOICECOM_GET, (DWORD_PTR)&devID, if (waveOutMessage((HWAVEOUT)WAVE_MAPPER, DRVM_MAPPER_CONSOLEVOICECOM_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status) != MMSYSERR_NOERROR)
(DWORD_PTR)&status) != MMSYSERR_NOERROR)
waveOutMessage((HWAVEOUT)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status); waveOutMessage((HWAVEOUT)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status);
} }
else else
{ {
if (waveInMessage((HWAVEIN)WAVE_MAPPER, DRVM_MAPPER_CONSOLEVOICECOM_GET, (DWORD_PTR)&devID, if (waveInMessage((HWAVEIN)WAVE_MAPPER, DRVM_MAPPER_CONSOLEVOICECOM_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status) != MMSYSERR_NOERROR)
(DWORD_PTR)&status) != MMSYSERR_NOERROR)
waveInMessage((HWAVEIN)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status); waveInMessage((HWAVEIN)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status);
} }
return devID; return devID;
} }
// -------------- XpEnumerator --------------- // -------------- XpEnumerator ---------------
XpEnumerator::XpEnumerator() : mDirection(-1) {} XpEnumerator::XpEnumerator()
:mDirection(-1)
{
}
XpEnumerator::~XpEnumerator() {} XpEnumerator::~XpEnumerator()
{
}
void XpEnumerator::open(int direction) void XpEnumerator::open(int direction)
{ {
@@ -279,7 +285,7 @@ void XpEnumerator::open(int direction)
if (direction == myMicrophone) if (direction == myMicrophone)
{ {
int count = waveInGetNumDevs(); int count = waveInGetNumDevs();
for (int i = 0; i < count; i++) for (int i=0; i<count; i++)
{ {
WAVEINCAPSW caps; WAVEINCAPSW caps;
if (waveInGetDevCapsW(i, &caps, sizeof caps) == MMSYSERR_NOERROR) if (waveInGetDevCapsW(i, &caps, sizeof caps) == MMSYSERR_NOERROR)
@@ -291,7 +297,7 @@ void XpEnumerator::open(int direction)
else else
{ {
int count = waveOutGetNumDevs(); int count = waveOutGetNumDevs();
for (int i = 0; i < count; i++) for (int i=0; i<count; i++)
{ {
WAVEOUTCAPSW caps; WAVEOUTCAPSW caps;
if (waveOutGetDevCapsW(i, &caps, sizeof caps) == MMSYSERR_NOERROR) if (waveOutGetDevCapsW(i, &caps, sizeof caps) == MMSYSERR_NOERROR)
@@ -302,9 +308,11 @@ void XpEnumerator::open(int direction)
} }
} }
void XpEnumerator::close() {} void XpEnumerator::close()
{
}
int XpEnumerator::count() int XpEnumerator::count()
{ {
return mNameList.size(); return mNameList.size();
} }
@@ -325,14 +333,12 @@ int XpEnumerator::indexOfDefaultDevice()
if (mDirection == mySpeaker) if (mDirection == mySpeaker)
{ {
if (waveOutMessage((HWAVEOUT)WAVE_MAPPER, DRVM_MAPPER_CONSOLEVOICECOM_GET, (DWORD_PTR)&devID, if (waveOutMessage((HWAVEOUT)WAVE_MAPPER, DRVM_MAPPER_CONSOLEVOICECOM_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status) != MMSYSERR_NOERROR)
(DWORD_PTR)&status) != MMSYSERR_NOERROR)
waveOutMessage((HWAVEOUT)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status); waveOutMessage((HWAVEOUT)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status);
} }
else else
{ {
if (waveInMessage((HWAVEIN)WAVE_MAPPER, DRVM_MAPPER_CONSOLEVOICECOM_GET, (DWORD_PTR)&devID, if (waveInMessage((HWAVEIN)WAVE_MAPPER, DRVM_MAPPER_CONSOLEVOICECOM_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status) != MMSYSERR_NOERROR)
(DWORD_PTR)&status) != MMSYSERR_NOERROR)
waveInMessage((HWAVEIN)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status); waveInMessage((HWAVEIN)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&devID, (DWORD_PTR)&status);
} }
return devID; return devID;
@@ -340,12 +346,11 @@ int XpEnumerator::indexOfDefaultDevice()
// -------- DSoundInputDevice --------------- // -------- DSoundInputDevice ---------------
DSoundInputDevice::DSoundInputDevice(GUID deviceId) DSoundInputDevice::DSoundInputDevice(GUID deviceId)
: mSimulate(false), mBufferIndex(0), mGUID(deviceId), mThreadHandle(0), mDenoiser(AUDIO_SAMPLERATE), :mSimulate(false), mBufferIndex(0), mGUID(deviceId), mThreadHandle(0), mDenoiser(AUDIO_SAMPLERATE), mEnableDenoiser(true),
mEnableDenoiser(true), mNullAudio(AUDIO_MIC_BUFFER_LENGTH, AUDIO_MIC_BUFFER_COUNT) mNullAudio(AUDIO_MIC_BUFFER_LENGTH, AUDIO_MIC_BUFFER_COUNT)
#ifdef AUDIO_DUMPINPUT #ifdef AUDIO_DUMPINPUT
, ,mDump(AUDIO_SAMPLERATE)
mDump(AUDIO_SAMPLERATE) #endif
#endif
{ {
gDSoundInit.load(); gDSoundInit.load();
@@ -354,7 +359,7 @@ DSoundInputDevice::DSoundInputDevice(GUID deviceId)
mDevice = NULL; mDevice = NULL;
mBuffer = NULL; mBuffer = NULL;
mShutdownSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); mShutdownSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL);
for (unsigned i = 0; i < AUDIO_MIC_BUFFER_COUNT; i++) for (unsigned i=0; i<AUDIO_MIC_BUFFER_COUNT; i++)
{ {
mEventArray[i].dwOffset = (i + 1) * AUDIO_MIC_BUFFER_SIZE - 1; mEventArray[i].dwOffset = (i + 1) * AUDIO_MIC_BUFFER_SIZE - 1;
mEventSignals[i] = mEventArray[i].hEventNotify = ::CreateEvent(NULL, TRUE, FALSE, NULL); mEventSignals[i] = mEventArray[i].hEventNotify = ::CreateEvent(NULL, TRUE, FALSE, NULL);
@@ -366,7 +371,7 @@ DSoundInputDevice::~DSoundInputDevice()
{ {
close(); close();
::CloseHandle(mShutdownSignal); ::CloseHandle(mShutdownSignal);
for (int i = 0; i < AUDIO_MIC_BUFFER_COUNT; i++) for (int i=0; i<AUDIO_MIC_BUFFER_COUNT; i++)
::CloseHandle(mEventArray[i].hEventNotify); ::CloseHandle(mEventArray[i].hEventNotify);
gDSoundInit.unload(); gDSoundInit.unload();
@@ -384,12 +389,13 @@ bool DSoundInputDevice::isSimulate() const
void DSoundInputDevice::openDevice() void DSoundInputDevice::openDevice()
{ {
ICELogInfo(<< "Open DirectSound audio input.")::CoInitialize(NULL); ICELogInfo(<< "Open DirectSound audio input.")
::CoInitialize(NULL);
Lock l(mGuard); Lock l(mGuard);
// Ensure if GUID is not null // Ensure if GUID is not null
if (IsEqualGUID(mGUID, GUID_NULL)) if (IsEqualGUID(mGUID, GUID_NULL))
{ {
setSimulate(true); setSimulate( true );
return; return;
} }
@@ -398,28 +404,24 @@ void DSoundInputDevice::openDevice()
mDump.open(L"audioinput.wav"); mDump.open(L"audioinput.wav");
#endif #endif
mNextBuffer = 0; mNextBuffer = 0; mDevice = NULL; IUnknown* unk = NULL; mBuffer = NULL;
mDevice = NULL;
IUnknown* unk = NULL;
mBuffer = NULL;
DSoundHelper::checkComResult(gDSoundInit.mRoutines.DirectSoundCaptureCreate8(&mGUID, &mDevice, NULL)); DSoundHelper::checkComResult(gDSoundInit.mRoutines.DirectSoundCaptureCreate8(&mGUID, &mDevice, NULL));
WAVEFORMATEX wfx; WAVEFORMATEX wfx;
memset(&wfx, 0, sizeof(wfx)); memset(&wfx, 0, sizeof(wfx));
// wfx.cbSize = sizeof(wfx); //wfx.cbSize = sizeof(wfx);
wfx.nChannels = AUDIO_CHANNELS; wfx.nChannels = AUDIO_CHANNELS;
wfx.nSamplesPerSec = AUDIO_SAMPLERATE; wfx.nSamplesPerSec = AUDIO_SAMPLERATE;
wfx.wBitsPerSample = 16; wfx.wBitsPerSample = 16;
wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8; wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;;
;
wfx.nAvgBytesPerSec = AUDIO_SAMPLERATE * 2 * AUDIO_CHANNELS; wfx.nAvgBytesPerSec = AUDIO_SAMPLERATE * 2 * AUDIO_CHANNELS;
wfx.wFormatTag = WAVE_FORMAT_PCM; wfx.wFormatTag = WAVE_FORMAT_PCM;
DSCBUFFERDESC dsbd; DSCBUFFERDESC dsbd;
ZeroMemory(&dsbd, sizeof(dsbd)); ZeroMemory(&dsbd, sizeof(dsbd));
dsbd.dwSize = sizeof(DSCBUFFERDESC); dsbd.dwSize = sizeof(DSCBUFFERDESC);
dsbd.dwFlags = 0; // DSBCAPS_CTRLPOSITIONNOTIFY; dsbd.dwFlags = 0;//DSBCAPS_CTRLPOSITIONNOTIFY;
dsbd.dwBufferBytes = AUDIO_MIC_BUFFER_COUNT * AUDIO_MIC_BUFFER_SIZE; dsbd.dwBufferBytes = AUDIO_MIC_BUFFER_COUNT * AUDIO_MIC_BUFFER_SIZE;
dsbd.lpwfxFormat = &wfx; dsbd.lpwfxFormat = &wfx;
dsbd.dwFXCount = 0; dsbd.dwFXCount = 0;
@@ -432,7 +434,7 @@ void DSoundInputDevice::openDevice()
DSoundHelper::checkComResult(mNotifications->SetNotificationPositions(AUDIO_MIC_BUFFER_COUNT, mEventArray)); DSoundHelper::checkComResult(mNotifications->SetNotificationPositions(AUDIO_MIC_BUFFER_COUNT, mEventArray));
DSoundHelper::checkComResult(mBuffer->Start(DSCBSTART_LOOPING)); DSoundHelper::checkComResult(mBuffer->Start(DSCBSTART_LOOPING));
dscb->Release(); dscb->Release();
setSimulate(false); setSimulate( false );
} }
bool DSoundInputDevice::open() bool DSoundInputDevice::open()
@@ -450,7 +452,7 @@ bool DSoundInputDevice::open()
void DSoundInputDevice::closeDevice() void DSoundInputDevice::closeDevice()
{ {
ICELogInfo(<< "Close DirectSound audio input"); ICELogInfo(<<"Close DirectSound audio input");
Lock l(mGuard); Lock l(mGuard);
#ifdef AUDIO_DUMPINPUT #ifdef AUDIO_DUMPINPUT
@@ -503,7 +505,7 @@ bool DSoundInputDevice::tryReadBuffer(void* buffer)
// Ensure device exists // Ensure device exists
if (!mDevice) if (!mDevice)
{ {
setSimulate(true); setSimulate( true );
return false; return false;
} }
@@ -522,10 +524,9 @@ bool DSoundInputDevice::tryReadBuffer(void* buffer)
try try
{ {
if (::WaitForSingleObject(mEventArray[mNextBuffer].hEventNotify, if (::WaitForSingleObject(mEventArray[mNextBuffer].hEventNotify, AUDIO_MIC_BUFFER_COUNT * AUDIO_MIC_BUFFER_LENGTH * 4) != WAIT_OBJECT_0)
AUDIO_MIC_BUFFER_COUNT * AUDIO_MIC_BUFFER_LENGTH * 4) != WAIT_OBJECT_0)
{ {
setSimulate(true); setSimulate( true );
return false; return false;
} }
@@ -533,7 +534,7 @@ bool DSoundInputDevice::tryReadBuffer(void* buffer)
if (::WaitForMultipleObjects(AUDIO_MIC_BUFFER_COUNT, mEventSignals, TRUE, 0) != WAIT_TIMEOUT) if (::WaitForMultipleObjects(AUDIO_MIC_BUFFER_COUNT, mEventSignals, TRUE, 0) != WAIT_TIMEOUT)
{ {
// Possible overflow. Consider current buffer resulting. Reset ALL events. // Possible overflow. Consider current buffer resulting. Reset ALL events.
for (int i = 0; i < AUDIO_MIC_BUFFER_COUNT; i++) for (int i = 0; i<AUDIO_MIC_BUFFER_COUNT; i++)
ResetEvent(mEventArray[i].hEventNotify); ResetEvent(mEventArray[i].hEventNotify);
} }
else else
@@ -542,12 +543,11 @@ bool DSoundInputDevice::tryReadBuffer(void* buffer)
// Find the buffer start offset // Find the buffer start offset
mReadOffset = mNextBuffer * AUDIO_MIC_BUFFER_SIZE; mReadOffset = mNextBuffer * AUDIO_MIC_BUFFER_SIZE;
// increase the buffer's index //increase the buffer's index
if (++mNextBuffer == AUDIO_MIC_BUFFER_COUNT) if (++mNextBuffer == AUDIO_MIC_BUFFER_COUNT)
mNextBuffer = 0; mNextBuffer = 0;
LPVOID ptr1 = NULL, ptr2 = NULL; LPVOID ptr1 = NULL, ptr2 = NULL; DWORD len1 = 0, len2 = 0;
DWORD len1 = 0, len2 = 0;
DSoundHelper::checkComResult(mBuffer->Lock(mReadOffset, AUDIO_MIC_BUFFER_SIZE, &ptr1, &len1, &ptr2, &len2, 0)); DSoundHelper::checkComResult(mBuffer->Lock(mReadOffset, AUDIO_MIC_BUFFER_SIZE, &ptr1, &len1, &ptr2, &len2, 0));
// Copy&Enqueue captured data to mQueue // Copy&Enqueue captured data to mQueue
@@ -574,9 +574,9 @@ bool DSoundInputDevice::tryReadBuffer(void* buffer)
return true; return true;
} }
catch (...) catch(...)
{ {
setSimulate(true); setSimulate( true );
} }
return false; return false;
} }
@@ -585,8 +585,9 @@ void DSoundInputDevice::setSimulate(bool s)
{ {
if (!mSimulate && s) if (!mSimulate && s)
mNullAudio.start(); mNullAudio.start();
else if (mSimulate && !s) else
mNullAudio.stop(); if (mSimulate && !s)
mNullAudio.stop();
mSimulate = s; mSimulate = s;
} }
@@ -598,7 +599,7 @@ Format DSoundInputDevice::getFormat()
int DSoundInputDevice::readBuffer(void* buffer) int DSoundInputDevice::readBuffer(void* buffer)
{ {
// Lock lock(mGuard); //Lock lock(mGuard);
if (mRefCount <= 0 || isSimulate()) if (mRefCount <= 0 || isSimulate())
return 0; return 0;
@@ -641,8 +642,10 @@ void DSoundInputDevice::threadProc(void* arg)
DSoundOutputDevice::DSoundOutputDevice(GUID deviceId) DSoundOutputDevice::DSoundOutputDevice(GUID deviceId)
: mDevice(NULL), mPrimaryBuffer(NULL), mBuffer(NULL), mWriteOffset(0), mPlayedSamples(0), mTotalPlayed(0), mTail(0), :mDevice(NULL), mPrimaryBuffer(NULL), mBuffer(NULL),
mThreadHandle(0), mSimulate(false), mGUID(deviceId), mNullAudio(AUDIO_SPK_BUFFER_LENGTH, AUDIO_SPK_BUFFER_COUNT) mWriteOffset(0), mPlayedSamples(0), mTotalPlayed(0), mTail(0),
mThreadHandle(0), mSimulate(false), mGUID(deviceId),
mNullAudio(AUDIO_SPK_BUFFER_LENGTH, AUDIO_SPK_BUFFER_COUNT)
{ {
gDSoundInit.load(); gDSoundInit.load();
mShutdownSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); mShutdownSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL);
@@ -697,7 +700,7 @@ void DSoundOutputDevice::openDevice()
ICELogInfo(<< "Open DirectSound audio output"); ICELogInfo(<< "Open DirectSound audio output");
if (IsEqualGUID(mGUID, GUID_NULL)) if (IsEqualGUID(mGUID, GUID_NULL))
{ {
setSimulate(true); setSimulate( true );
return; return;
} }
@@ -726,10 +729,10 @@ void DSoundOutputDevice::openDevice()
dsbd.dwSize = sizeof(DSBUFFERDESC); dsbd.dwSize = sizeof(DSBUFFERDESC);
dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER; dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
dsbd.dwBufferBytes = 0; dsbd.dwBufferBytes = 0;
dsbd.lpwfxFormat = NULL; //&wfx; dsbd.lpwfxFormat = NULL;//&wfx;
dsbd.guid3DAlgorithm = DS3DALG_DEFAULT; dsbd.guid3DAlgorithm = DS3DALG_DEFAULT;
DSoundHelper::checkComResult(mDevice->CreateSoundBuffer(&dsbd, &mPrimaryBuffer, NULL)); DSoundHelper::checkComResult(mDevice->CreateSoundBuffer(&dsbd, &mPrimaryBuffer, NULL ));
DSBCAPS caps; DSBCAPS caps;
caps.dwSize = sizeof(caps); caps.dwSize = sizeof(caps);
caps.dwFlags = 0; caps.dwFlags = 0;
@@ -748,16 +751,14 @@ void DSoundOutputDevice::openDevice()
DSoundHelper::checkComResult(mDevice->CreateSoundBuffer(&dsbd, &mBuffer, NULL)); DSoundHelper::checkComResult(mDevice->CreateSoundBuffer(&dsbd, &mBuffer, NULL));
// Fill the buffer with silence // Fill the buffer with silence
LPVOID ptr1 = NULL, ptr2 = NULL; LPVOID ptr1 = NULL, ptr2 = NULL; DWORD len1 = 0, len2 = 0;
DWORD len1 = 0, len2 = 0; DSoundHelper::checkComResult(mBuffer->Lock(0, AUDIO_SPK_BUFFER_SIZE * AUDIO_SPK_BUFFER_COUNT, &ptr1, &len1, &ptr2, &len2, 0));
DSoundHelper::checkComResult(
mBuffer->Lock(0, AUDIO_SPK_BUFFER_SIZE * AUDIO_SPK_BUFFER_COUNT, &ptr1, &len1, &ptr2, &len2, 0));
if (len1 && ptr1) if (len1 && ptr1)
memset(ptr1, 0, len1); memset(ptr1, 0, len1);
if (len2 && ptr2) if (len2 && ptr2)
memset(ptr2, 0, len2); memset(ptr2, 0, len2);
DSoundHelper::checkComResult(mBuffer->Unlock(ptr1, len1, ptr2, len2)); DSoundHelper::checkComResult(mBuffer->Unlock(ptr1, len1, ptr2, len2));
DSoundHelper::checkComResult(mBuffer->Play(0, 0, DSBPLAY_LOOPING)); DSoundHelper::checkComResult(mBuffer->Play(0,0,DSBPLAY_LOOPING));
mBuffer->GetCurrentPosition(NULL, &mWriteCursor); mBuffer->GetCurrentPosition(NULL, &mWriteCursor);
} }
@@ -803,9 +804,8 @@ bool DSoundOutputDevice::getMediaFrame()
if (mConnection) if (mConnection)
mConnection->onSpkData(getFormat(), mMediaFrame, sizeof mMediaFrame); mConnection->onSpkData(getFormat(), mMediaFrame, sizeof mMediaFrame);
} }
catch (...) catch(...)
{ {}
}
return true; return true;
} }
@@ -833,8 +833,7 @@ bool DSoundOutputDevice::process()
offset %= mBufferSize; offset %= mBufferSize;
// See what we can write // See what we can write
LPVOID ptr1 = NULL, ptr2 = NULL; LPVOID ptr1 = NULL, ptr2 = NULL; DWORD len1 = 0, len2 = 0;
DWORD len1 = 0, len2 = 0;
DSoundHelper::checkComResult(mBuffer->Lock(offset, AUDIO_SPK_BUFFER_SIZE, &ptr1, &len1, &ptr2, &len2, 0)); DSoundHelper::checkComResult(mBuffer->Lock(offset, AUDIO_SPK_BUFFER_SIZE, &ptr1, &len1, &ptr2, &len2, 0));
assert(ptr2 == NULL); assert(ptr2 == NULL);
@@ -859,10 +858,10 @@ void DSoundOutputDevice::threadProc(void* arg)
DSoundOutputDevice* impl = (DSoundOutputDevice*)arg; DSoundOutputDevice* impl = (DSoundOutputDevice*)arg;
impl->openDevice(); impl->openDevice();
DWORD waitResult = 0; DWORD waitResult = 0;
HANDLE waitArray[2] = {impl->mBufferSignal, impl->mShutdownSignal}; HANDLE waitArray[2] = {impl->mBufferSignal, impl->mShutdownSignal};
unsigned exitCount = 0; unsigned exitCount = 0;
bool exitSignal = false; bool exitSignal = false;
while (true) while (true)
{ {
// Poll for shutdown signal // Poll for shutdown signal
@@ -885,12 +884,12 @@ void DSoundOutputDevice::threadProc(void* arg)
impl->restoreBuffer(); impl->restoreBuffer();
impl->process(); impl->process();
} }
catch (const Exception& e) catch(const Exception& e)
{ {
ICELogError(<< "DirectSound output failed with code = " << e.code() << ", subcode = " << e.subcode()); ICELogError(<< "DirectSound output failed with code = " << e.code() << ", subcode = " << e.subcode());
impl->setSimulate(true); impl->setSimulate(true);
} }
catch (...) catch(...)
{ {
ICELogError(<< "DirectSound output failed due to unexpected exception."); ICELogError(<< "DirectSound output failed due to unexpected exception.");
impl->setSimulate(true); impl->setSimulate(true);
@@ -926,61 +925,62 @@ bool DSoundOutputDevice::closing()
return false; return false;
} }
typedef WINUSERAPI HRESULT(WINAPI* LPFNDLLGETCLASSOBJECT)(const CLSID&, const IID&, void**); typedef WINUSERAPI HRESULT (WINAPI *LPFNDLLGETCLASSOBJECT) (const CLSID &, const IID &, void **);
HRESULT DirectSoundPrivateCreate(OUT LPKSPROPERTYSET* ppKsPropertySet) HRESULT DirectSoundPrivateCreate (OUT LPKSPROPERTYSET * ppKsPropertySet)
{ {
HMODULE hLibDsound = NULL; HMODULE hLibDsound = NULL;
LPFNDLLGETCLASSOBJECT pfnDllGetClassObject = NULL; LPFNDLLGETCLASSOBJECT pfnDllGetClassObject = NULL;
LPCLASSFACTORY pClassFactory = NULL; LPCLASSFACTORY pClassFactory = NULL;
LPKSPROPERTYSET pKsPropertySet = NULL; LPKSPROPERTYSET pKsPropertySet = NULL;
HRESULT hr = DS_OK; HRESULT hr = DS_OK;
// Load dsound.dll // Load dsound.dll
hLibDsound = LoadLibrary(TEXT("dsound.dll")); hLibDsound = LoadLibrary(TEXT("dsound.dll"));
if (!hLibDsound) if(!hLibDsound)
{ {
hr = DSERR_GENERIC; hr = DSERR_GENERIC;
} }
// Find DllGetClassObject // Find DllGetClassObject
if (SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
pfnDllGetClassObject = (LPFNDLLGETCLASSOBJECT)GetProcAddress(hLibDsound, "DllGetClassObject"); pfnDllGetClassObject =
(LPFNDLLGETCLASSOBJECT)GetProcAddress ( hLibDsound, "DllGetClassObject" );
if (!pfnDllGetClassObject) if(!pfnDllGetClassObject)
{ {
hr = DSERR_GENERIC; hr = DSERR_GENERIC;
} }
} }
// Create a class factory object // Create a class factory object
if (SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
hr = pfnDllGetClassObject(CLSID_DirectSoundPrivate, IID_IClassFactory, (LPVOID*)&pClassFactory); hr = pfnDllGetClassObject (CLSID_DirectSoundPrivate, IID_IClassFactory, (LPVOID *)&pClassFactory );
} }
// Create the DirectSoundPrivate object and query for an IKsPropertySet // Create the DirectSoundPrivate object and query for an IKsPropertySet
// interface // interface
if (SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
hr = pClassFactory->CreateInstance(NULL, IID_IKsPropertySet, (LPVOID*)&pKsPropertySet); hr = pClassFactory->CreateInstance ( NULL, IID_IKsPropertySet, (LPVOID *)&pKsPropertySet );
} }
// Release the class factory // Release the class factory
if (pClassFactory) if(pClassFactory)
{ {
pClassFactory->Release(); pClassFactory->Release();
} }
// Handle final success or failure // Handle final success or failure
if (SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
*ppKsPropertySet = pKsPropertySet; *ppKsPropertySet = pKsPropertySet;
} }
else if (pKsPropertySet) else if(pKsPropertySet)
{ {
pKsPropertySet->Release(); pKsPropertySet->Release();
} }
@@ -990,43 +990,54 @@ HRESULT DirectSoundPrivateCreate(OUT LPKSPROPERTYSET* ppKsPropertySet
return hr; return hr;
} }
BOOL GetInfoFromDSoundGUID(GUID i_sGUID, int& dwWaveID) BOOL GetInfoFromDSoundGUID( GUID i_sGUID, int &dwWaveID)
{ {
LPKSPROPERTYSET pKsPropertySet = NULL; LPKSPROPERTYSET pKsPropertySet = NULL;
HRESULT hr; HRESULT hr;
BOOL retval = FALSE; BOOL retval = FALSE;
PDSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA psDirectSoundDeviceDescription = NULL; PDSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA psDirectSoundDeviceDescription = NULL;
DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA sDirectSoundDeviceDescription; DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA sDirectSoundDeviceDescription;
memset(&sDirectSoundDeviceDescription, 0, sizeof(sDirectSoundDeviceDescription)); memset(&sDirectSoundDeviceDescription,0,sizeof(sDirectSoundDeviceDescription));
hr = DirectSoundPrivateCreate(&pKsPropertySet); hr = DirectSoundPrivateCreate( &pKsPropertySet );
if (SUCCEEDED(hr)) if(SUCCEEDED(hr))
{ {
ULONG ulBytesReturned = 0; ULONG ulBytesReturned = 0;
sDirectSoundDeviceDescription.DeviceId = i_sGUID; sDirectSoundDeviceDescription.DeviceId = i_sGUID;
// On the first call the final size is unknown so pass the size of the struct in order to receive // On the first call the final size is unknown so pass the size of the struct in order to receive
// "Type" and "DataFlow" values, ulBytesReturned will be populated with bytes required for struct+strings. // "Type" and "DataFlow" values, ulBytesReturned will be populated with bytes required for struct+strings.
hr = pKsPropertySet->Get(DSPROPSETID_DirectSoundDevice, DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION, NULL, 0, hr = pKsPropertySet->Get(DSPROPSETID_DirectSoundDevice,
&sDirectSoundDeviceDescription, sizeof(sDirectSoundDeviceDescription), DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION,
&ulBytesReturned); NULL,
0,
&sDirectSoundDeviceDescription,
sizeof(sDirectSoundDeviceDescription),
&ulBytesReturned
);
if (ulBytesReturned) if (ulBytesReturned)
{ {
// On the first call it notifies us of the required amount of memory in order to receive the strings. // On the first call it notifies us of the required amount of memory in order to receive the strings.
// Allocate the required memory, the strings will be pointed to the memory space directly after the struct. // Allocate the required memory, the strings will be pointed to the memory space directly after the struct.
psDirectSoundDeviceDescription = (PDSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA) new BYTE[ulBytesReturned]; psDirectSoundDeviceDescription = (PDSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION_DATA)new BYTE[ulBytesReturned];
*psDirectSoundDeviceDescription = sDirectSoundDeviceDescription; *psDirectSoundDeviceDescription = sDirectSoundDeviceDescription;
hr = pKsPropertySet->Get(DSPROPSETID_DirectSoundDevice, DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION, NULL, 0, hr = pKsPropertySet->Get(DSPROPSETID_DirectSoundDevice,
psDirectSoundDeviceDescription, ulBytesReturned, &ulBytesReturned); DSPROPERTY_DIRECTSOUNDDEVICE_DESCRIPTION,
NULL,
0,
psDirectSoundDeviceDescription,
ulBytesReturned,
&ulBytesReturned
);
dwWaveID = psDirectSoundDeviceDescription->WaveDeviceId; dwWaveID = psDirectSoundDeviceDescription->WaveDeviceId;
/*Description = psDirectSoundDeviceDescription->Description; /*Description = psDirectSoundDeviceDescription->Description;
Module = psDirectSoundDeviceDescription->Module; Module = psDirectSoundDeviceDescription->Module;
Interface = psDirectSoundDeviceDescription->Interface;*/ Interface = psDirectSoundDeviceDescription->Interface;*/
delete[] psDirectSoundDeviceDescription; delete [] psDirectSoundDeviceDescription;
retval = TRUE; retval = TRUE;
} }
@@ -1038,14 +1049,20 @@ BOOL GetInfoFromDSoundGUID(GUID i_sGUID, int& dwWaveID)
struct EnumResult struct EnumResult
{ {
int mDeviceId; int mDeviceId;
GUID mGuid; GUID mGuid;
}; };
BOOL CALLBACK DSEnumCallback(LPGUID lpGuid, LPCTSTR lpcstrDescription, LPCTSTR lpcstrModule, LPVOID lpContext) BOOL CALLBACK DSEnumCallback(
LPGUID lpGuid,
LPCTSTR lpcstrDescription,
LPCTSTR lpcstrModule,
LPVOID lpContext
)
{ {
if (lpGuid) if (lpGuid)
{ {
int devId = -1; int devId = -1;
GetInfoFromDSoundGUID(*lpGuid, devId); GetInfoFromDSoundGUID(*lpGuid, devId);
EnumResult* er = (EnumResult*)lpContext; EnumResult* er = (EnumResult*)lpContext;
+77 -77
View File
@@ -1,4 +1,4 @@
/* Copyright(C) 2007-2025 VoIP objects (voipobjects.com) /* Copyright(C) 2007-2017 VoIP objects (voipobjects.com)
* This Source Code Form is subject to the terms of the Mozilla Public * This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
@@ -6,7 +6,7 @@
#ifndef __AUDIO_DSOUND_H #ifndef __AUDIO_DSOUND_H
#define __AUDIO_DSOUND_H #define __AUDIO_DSOUND_H
#include "../engine_config.h" #include "../config.h"
#include <winsock2.h> #include <winsock2.h>
#include <windows.h> #include <windows.h>
@@ -22,7 +22,7 @@
#include <EndpointVolume.h> #include <EndpointVolume.h>
#include <MMDeviceAPI.h> #include <MMDeviceAPI.h>
#if defined(_MSC_VER) #if defined(_MSC_VER)
#include <Functiondiscoverykeys_devpkey.h> # include <Functiondiscoverykeys_devpkey.h>
#endif #endif
#include <vector> #include <vector>
#include <string> #include <string>
@@ -31,48 +31,48 @@
namespace Audio namespace Audio
{ {
class VistaEnumerator : public Enumerator class VistaEnumerator: public Enumerator
{ {
public: public:
VistaEnumerator(); VistaEnumerator();
~VistaEnumerator(); ~VistaEnumerator();
void open(int direction); void open(int direction);
void close(); void close();
int count(); int count();
std::tstring nameAt(int index); std::tstring nameAt(int index);
int idAt(int index); int idAt(int index);
int indexOfDefaultDevice(); int indexOfDefaultDevice();
protected: protected:
IMMDeviceCollection* mCollection; IMMDeviceCollection* mCollection;
IMMDevice* mDefaultDevice; IMMDevice* mDefaultDevice;
IMMDeviceEnumerator* mEnumerator; IMMDeviceEnumerator* mEnumerator;
EDataFlow mDirection; EDataFlow mDirection;
std::vector<std::wstring> mNameList; std::vector<std::wstring> mNameList;
void enumerate(); void enumerate();
IMMDevice* mapIndexToInterface(int index); IMMDevice* mapIndexToInterface(int index);
}; };
class XpEnumerator : public Enumerator class XpEnumerator: public Enumerator
{ {
public: public:
XpEnumerator(); XpEnumerator();
~XpEnumerator(); ~XpEnumerator();
void open(int direction); void open(int direction);
void close(); void close();
int count(); int count();
std::tstring nameAt(int index); std::tstring nameAt(int index);
int idAt(int index); int idAt(int index);
int indexOfDefaultDevice(); int indexOfDefaultDevice();
protected: protected:
std::vector<std::wstring> mNameList; std::vector<std::wstring> mNameList;
int mDirection; int mDirection;
}; };
class DSoundHelper class DSoundHelper
@@ -83,32 +83,32 @@ public:
}; };
#if !defined(_MSC_VER) #if !defined(_MSC_VER)
typedef struct IDirectSoundNotify8* LPDIRECTSOUNDNOTIFY8; typedef struct IDirectSoundNotify8 *LPDIRECTSOUNDNOTIFY8;
#endif #endif
class DSoundInputDevice : public InputDevice class DSoundInputDevice: public InputDevice
{ {
public: public:
DSoundInputDevice(GUID deviceId); DSoundInputDevice(GUID deviceId);
~DSoundInputDevice(); ~DSoundInputDevice();
void enableDenoiser(bool enable); void enableDenoiser(bool enable);
bool open(); bool open();
void close(); void close();
bool isSimulate() const; bool isSimulate() const;
void setSimulate(bool s); void setSimulate(bool s);
int readBuffer(void* buffer); int readBuffer(void* buffer);
Format getFormat(); Format getFormat();
protected: protected:
Mutex mGuard; /// Mutex to protect this instance. Mutex mGuard; /// Mutex to protect this instance.
LPDIRECTSOUNDCAPTURE8 mDevice; LPDIRECTSOUNDCAPTURE8 mDevice;
LPDIRECTSOUNDCAPTUREBUFFER8 mBuffer; LPDIRECTSOUNDCAPTUREBUFFER8 mBuffer;
LPDIRECTSOUNDNOTIFY8 mNotifications; LPDIRECTSOUNDNOTIFY8 mNotifications;
DSBPOSITIONNOTIFY mEventArray[AUDIO_MIC_BUFFER_COUNT]; DSBPOSITIONNOTIFY mEventArray[AUDIO_MIC_BUFFER_COUNT];
HANDLE mEventSignals[AUDIO_MIC_BUFFER_COUNT]; // Helper array to make WaitForMultipleObjects in loop HANDLE mEventSignals[AUDIO_MIC_BUFFER_COUNT]; // Helper array to make WaitForMultipleObjects in loop
int mBufferIndex; int mBufferIndex;
int mNextBuffer; int mNextBuffer;
@@ -116,7 +116,7 @@ protected:
HANDLE mThreadHandle; HANDLE mThreadHandle;
HANDLE mShutdownSignal; HANDLE mShutdownSignal;
volatile bool mSimulate; /// Marks if simulate mode is active. volatile bool mSimulate; /// Marks if simulate mode is active.
int mRefCount; int mRefCount;
ByteBuffer mQueue; ByteBuffer mQueue;
unsigned mReadOffset; unsigned mReadOffset;
@@ -129,59 +129,59 @@ protected:
WavFileWriter mDump; WavFileWriter mDump;
#endif #endif
bool tryReadBuffer(void* buffer); bool tryReadBuffer(void* buffer);
void openDevice(); void openDevice();
void closeDevice(); void closeDevice();
static void threadProc(void* arg); static void threadProc(void* arg);
}; };
class DSoundOutputDevice : public OutputDevice class DSoundOutputDevice: public OutputDevice
{ {
public: public:
DSoundOutputDevice(GUID deviceId); DSoundOutputDevice(GUID deviceId);
~DSoundOutputDevice(); ~DSoundOutputDevice();
bool open(); bool open();
void close(); void close();
unsigned playedTime() const; unsigned playedTime() const;
bool isSimulate() const; bool isSimulate() const;
void setSimulate(bool s); void setSimulate(bool s);
bool closing(); bool closing();
Format getFormat(); Format getFormat();
protected: protected:
Mutex mGuard; /// Mutex to protect this instance Mutex mGuard; /// Mutex to protect this instance
int mDeviceID; int mDeviceID;
LPDIRECTSOUND8 mDevice; LPDIRECTSOUND8 mDevice;
LPDIRECTSOUNDBUFFER mPrimaryBuffer; LPDIRECTSOUNDBUFFER mPrimaryBuffer;
LPDIRECTSOUNDBUFFER mBuffer; LPDIRECTSOUNDBUFFER mBuffer;
GUID mGUID; GUID mGUID;
unsigned mWriteOffset; unsigned mWriteOffset;
unsigned mPlayedSamples; unsigned mPlayedSamples;
unsigned mSentBytes; unsigned mSentBytes;
DWORD mPlayCursor; // Measured in bytes DWORD mPlayCursor; // Measured in bytes
unsigned mBufferSize; unsigned mBufferSize;
unsigned mTotalPlayed; // Measured in bytes unsigned mTotalPlayed; // Measured in bytes
unsigned mTail; // Measured in bytes unsigned mTail; // Measured in bytes
HANDLE mShutdownSignal; HANDLE mShutdownSignal;
HANDLE mBufferSignal; HANDLE mBufferSignal;
HANDLE mThreadHandle; HANDLE mThreadHandle;
bool mSimulate; bool mSimulate;
StubTimer mNullAudio; StubTimer mNullAudio;
DWORD mWriteCursor; DWORD mWriteCursor;
char mMediaFrame[AUDIO_SPK_BUFFER_SIZE]; char mMediaFrame[AUDIO_SPK_BUFFER_SIZE];
unsigned mRefCount; unsigned mRefCount;
void openDevice(); void openDevice();
void closeDevice(); void closeDevice();
void restoreBuffer(); void restoreBuffer();
bool process(); bool process();
bool getMediaFrame(); bool getMediaFrame();
static void threadProc(void* arg); static void threadProc(void* arg);
}; };
} // namespace Audio }
#endif #endif
+71 -67
View File
@@ -4,7 +4,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifdef TARGET_WIN #ifdef TARGET_WIN
#include <WinSock2.h> # include <WinSock2.h>
#endif #endif
#include <assert.h> #include <assert.h>
@@ -17,92 +17,96 @@ using namespace Audio;
TimeSource::TimeSource(int quantTime, int nrOfQuants) TimeSource::TimeSource(int quantTime, int nrOfQuants)
{ {
#ifdef TARGET_WIN #ifdef TARGET_WIN
mCounter.QuadPart = 0; mCounter.QuadPart = 0;
#endif #endif
#if defined(TARGET_OSX) || defined(TARGET_IOS) #if defined(TARGET_OSX) || defined(TARGET_IOS)
mach_timebase_info(&mTimebase); mach_timebase_info(&mTimebase);
mRatio = ((double)mTimebase.numer / (double)mTimebase.denom) / 1000000; mRatio = ((double)mTimebase.numer / (double)mTimebase.denom) / 1000000;
#endif #endif
mQuantTime = quantTime; mQuantTime = quantTime;
mDepthTime = quantTime * nrOfQuants; mDepthTime = quantTime * nrOfQuants;
mTailTime = 0; mTailTime = 0;
}
TimeSource::~TimeSource()
{
} }
void TimeSource::start() void TimeSource::start()
{ {
#ifdef TARGET_WIN #ifdef TARGET_WIN
if (!QueryPerformanceFrequency(&mFreq)) if (!QueryPerformanceFrequency(&mFreq))
throw Exception(ERR_QPC, GetLastError()); throw Exception(ERR_QPC, GetLastError());
if (!QueryPerformanceCounter(&mCounter)) if (!QueryPerformanceCounter(&mCounter))
throw Exception(ERR_QPC, GetLastError()); throw Exception(ERR_QPC, GetLastError());
#endif #endif
} }
void TimeSource::stop() {} void TimeSource::stop()
{
}
unsigned TimeSource::time() unsigned TimeSource::time()
{ {
#ifdef TARGET_WIN #ifdef TARGET_WIN
LARGE_INTEGER c; LARGE_INTEGER c;
if (!QueryPerformanceCounter(&c)) if (!QueryPerformanceCounter(&c))
throw Exception(ERR_QPC, GetLastError()); throw Exception(ERR_QPC, GetLastError());
// find the f //find the f
double f = (double)mFreq.QuadPart / 1000.0; double f = (double)mFreq.QuadPart / 1000.0;
// find the difference //find the difference
unsigned __int64 diff = c.QuadPart - mCounter.QuadPart; unsigned __int64 diff = c.QuadPart - mCounter.QuadPart;
mCounter.QuadPart = c.QuadPart; mCounter.QuadPart = c.QuadPart;
diff = (unsigned __int64)((double)diff / f + 0.5); // get ms diff = (unsigned __int64)((double)diff / f + 0.5); //get ms
diff += mTailTime; diff += mTailTime;
if (diff > mDepthTime) if (diff > mDepthTime)
{ {
mTailTime = 0; mTailTime = 0;
return mDepthTime; return mDepthTime;
} }
else else
{ {
mTailTime = (unsigned)(diff % (unsigned __int64)mQuantTime); mTailTime = (unsigned )(diff % (unsigned __int64)mQuantTime);
unsigned int t = (unsigned)(diff / (unsigned __int64)mQuantTime); unsigned int t = (unsigned )(diff / (unsigned __int64)mQuantTime);
return t * mQuantTime; return t * mQuantTime;
} }
#endif #endif
#if defined(TARGET_OSX) || defined(TARGET_IOS) #if defined(TARGET_OSX) || defined(TARGET_IOS)
uint64_t t = mach_absolute_time(); uint64_t t = mach_absolute_time();
uint64_t c = uint64_t((double)t * mRatio + 0.5); uint64_t c = uint64_t((double)t * mRatio + 0.5);
uint64_t diff = c - this->mTime + mTailTime; uint64_t diff = c - this->mTime + mTailTime;
mTime = c; mTime = c;
if (diff > mDepthTime) if (diff > mDepthTime)
{ {
mTailTime = 0; mTailTime = 0;
return mDepthTime; return mDepthTime;
} }
else else
{ {
mTailTime = diff % mQuantTime; mTailTime = diff % mQuantTime;
uint64_t t = diff / mQuantTime; uint64_t t = diff / mQuantTime;
return t * mQuantTime; return t * mQuantTime;
} }
#endif #endif
#if defined(TARGET_LINUX) #if defined(TARGET_LINUX)
assert(0); assert(0);
#endif #endif
#if defined(TARGET_ANDROID) #if defined(TARGET_ANDROID)
assert(0); assert(0);
#endif #endif
return 0;
} }
// --- StubTimer --- // --- StubTimer ---
StubTimer::StubTimer(int bufferTime, int bufferCount) StubTimer::StubTimer(int bufferTime, int bufferCount)
: mBufferTime(bufferTime), mBufferCount(bufferCount), mTimeSource(bufferTime, bufferCount), mActive(false), :mBufferTime(bufferTime), mBufferCount(bufferCount), mTimeSource(bufferTime, bufferCount), mActive(false)
mCurrentTime(0)
{ {
#ifdef TARGET_WIN #ifdef TARGET_WIN
mStubSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); mStubSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL);
@@ -112,38 +116,38 @@ StubTimer::StubTimer(int bufferTime, int bufferCount)
StubTimer::~StubTimer() StubTimer::~StubTimer()
{ {
#ifdef TARGET_WIN #ifdef TARGET_WIN
::CloseHandle(mStubSignal); ::CloseHandle(mStubSignal);
#endif #endif
} }
void StubTimer::start() void StubTimer::start()
{ {
mTimeSource.start(); mTimeSource.start();
mCurrentTime = mTimeSource.time(); mCurrentTime = mTimeSource.time();
mActive = true; mActive = true;
} }
void StubTimer::stop() void StubTimer::stop()
{ {
mTimeSource.stop(); mTimeSource.stop();
mActive = false; mActive = false;
} }
void StubTimer::waitForBuffer() void StubTimer::waitForBuffer()
{ {
if (!mActive) if (!mActive)
start(); start();
unsigned t = mTimeSource.time(); unsigned t = mTimeSource.time();
while (!t) while (!t)
{ {
#ifdef TARGET_WIN #ifdef TARGET_WIN
::WaitForSingleObject(mStubSignal, mBufferTime); ::WaitForSingleObject(mStubSignal, mBufferTime);
#endif #endif
#if defined(TARGET_OSX) || defined(TARGET_IOS) #if defined(TARGET_OSX) || defined(TARGET_IOS)
usleep(100); usleep(100);
#endif #endif
t = mTimeSource.time(); t = mTimeSource.time();
} }
} }
+32 -32
View File
@@ -10,15 +10,15 @@
#include <EndpointVolume.h> #include <EndpointVolume.h>
#include <MMDeviceAPI.h> #include <MMDeviceAPI.h>
#if defined(_MSC_VER) #if defined(_MSC_VER)
#include <Functiondiscoverykeys_devpkey.h> # include <Functiondiscoverykeys_devpkey.h>
#endif #endif
#endif #endif
#if defined(TARGET_OSX) || defined(TARGET_IOS) #if defined(TARGET_OSX) || defined(TARGET_IOS)
#include <AudioUnit/AudioUnit.h> # include <AudioUnit/AudioUnit.h>
#include <AudioToolbox/AudioConverter.h> # include <AudioToolbox/AudioConverter.h>
#include <AudioToolbox/AudioServices.h> # include <AudioToolbox/AudioServices.h>
#include <mach/mach_time.h> # include <mach/mach_time.h>
#endif #endif
#include <vector> #include <vector>
@@ -27,52 +27,52 @@
namespace Audio namespace Audio
{ {
class TimeSource class TimeSource
{ {
protected: protected:
#ifdef TARGET_WIN #ifdef TARGET_WIN
LARGE_INTEGER mCounter; /// Current value from QPC. LARGE_INTEGER mCounter; /// Current value from QPC.
LARGE_INTEGER mFreq; /// Current frequency from QPC. LARGE_INTEGER mFreq; /// Current frequency from QPC.
#endif #endif
#if defined(TARGET_OSX) || defined(TARGET_IOS) #if defined(TARGET_OSX) || defined(TARGET_IOS)
uint64_t mTime; uint64_t mTime;
struct mach_timebase_info mTimebase; struct mach_timebase_info mTimebase;
double mRatio; double mRatio;
#endif #endif
unsigned mQuantTime; /// Used time quants length in milliseconds. unsigned mQuantTime; /// Used time quants length in milliseconds.
unsigned mDepthTime; /// Number of available time quants. unsigned mDepthTime; /// Number of available time quants.
unsigned mTailTime; /// Not-accounted milliseconds. unsigned mTailTime; /// Not-accounted milliseconds.
public: public:
TimeSource(int quantTime, int nrOfQuants); TimeSource(int quantTime, int nrOfQuants);
~TimeSource() = default; ~TimeSource();
void start(); void start();
void stop(); void stop();
unsigned time(); unsigned time();
}; };
class StubTimer class StubTimer
{ {
public: public:
StubTimer(int bufferTime, int bufferCount); StubTimer(int bufferTime, int bufferCount);
~StubTimer(); ~StubTimer();
void start(); void start();
void stop(); void stop();
void waitForBuffer(); void waitForBuffer();
protected: protected:
unsigned mBufferTime; unsigned mBufferTime;
unsigned mBufferCount; unsigned mBufferCount;
unsigned mCurrentTime; unsigned mCurrentTime;
TimeSource mTimeSource; TimeSource mTimeSource;
#ifdef TARGET_WIN #ifdef TARGET_WIN
HANDLE mStubSignal; HANDLE mStubSignal;
#endif #endif
bool mActive; bool mActive;
}; };
} // namespace Audio }
#endif #endif
+38 -20
View File
@@ -7,16 +7,16 @@
#include "../helper/HL_OsVersion.h" #include "../helper/HL_OsVersion.h"
#if !defined(USE_NULL_AUDIO) #if !defined(USE_NULL_AUDIO)
#ifdef TARGET_WIN # ifdef TARGET_WIN
#include "Audio_Wmme.h" # include "Audio_Wmme.h"
#include "Audio_DirectSound.h" # include "Audio_DirectSound.h"
#endif # endif
#ifdef TARGET_OSX # ifdef TARGET_OSX
#include "Audio_CoreAudio.h" # include "Audio_CoreAudio.h"
#endif # endif
#ifdef TARGET_ANDROID # ifdef TARGET_ANDROID
#include "Audio_Android.h" # include "Audio_Android.h"
#endif # endif
#endif #endif
#include "Audio_Helper.h" #include "Audio_Helper.h"
@@ -24,9 +24,14 @@
using namespace Audio; using namespace Audio;
Device::Device() : mConnection(nullptr) {} Device::Device()
:mConnection(nullptr)
{
}
Device::~Device() {} Device::~Device()
{
}
void Device::setConnection(DataConnection* connection) void Device::setConnection(DataConnection* connection)
@@ -39,9 +44,13 @@ DataConnection* Device::connection()
return mConnection; return mConnection;
} }
InputDevice::InputDevice() {} InputDevice::InputDevice()
{
}
InputDevice::~InputDevice() {} InputDevice::~InputDevice()
{
}
InputDevice* InputDevice::make(int devId) InputDevice* InputDevice::make(int devId)
{ {
@@ -62,9 +71,13 @@ InputDevice* InputDevice::make(int devId)
return nullptr; return nullptr;
} }
OutputDevice::OutputDevice() {} OutputDevice::OutputDevice()
{
}
OutputDevice::~OutputDevice() {} OutputDevice::~OutputDevice()
{
}
OutputDevice* OutputDevice::make(int devId) OutputDevice* OutputDevice::make(int devId)
{ {
@@ -72,7 +85,7 @@ OutputDevice* OutputDevice::make(int devId)
return new NullOutputDevice(); return new NullOutputDevice();
#else #else
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
// return new WmmeOutputDevice(index); //return new WmmeOutputDevice(index);
return new DSoundOutputDevice(DSoundHelper::deviceId2Guid(devId, false)); return new DSoundOutputDevice(DSoundHelper::deviceId2Guid(devId, false));
#endif #endif
#ifdef TARGET_OSX #ifdef TARGET_OSX
@@ -87,9 +100,13 @@ OutputDevice* OutputDevice::make(int devId)
// --- Enumerator --- // --- Enumerator ---
Enumerator::Enumerator() {} Enumerator::Enumerator()
{
}
Enumerator::~Enumerator() {} Enumerator::~Enumerator()
{
}
int Enumerator::nameToIndex(const std::tstring& name) int Enumerator::nameToIndex(const std::tstring& name)
{ {
@@ -102,6 +119,7 @@ int Enumerator::nameToIndex(const std::tstring& name)
Enumerator* Enumerator::make(bool useNull) Enumerator* Enumerator::make(bool useNull)
{ {
if (useNull) if (useNull)
return new NullEnumerator(); return new NullEnumerator();
#ifndef USE_NULL_AUDIO #ifndef USE_NULL_AUDIO
@@ -129,7 +147,7 @@ OsEngine* OsEngine::instance()
#ifdef TARGET_ANDROID #ifdef TARGET_ANDROID
return nullptr; // As we use Oboe library for now return nullptr; // As we use Oboe library for now
// return &OpenSLEngine::instance(); //return &OpenSLEngine::instance();
#endif #endif
return nullptr; return nullptr;
+68 -66
View File
@@ -7,7 +7,7 @@
#define __AUDIO_INTERFACE_H #define __AUDIO_INTERFACE_H
#include <string> #include <string>
#include "../engine_config.h" #include "../config.h"
#include "../helper/HL_Types.h" #include "../helper/HL_Types.h"
#include "../helper/HL_VariantMap.h" #include "../helper/HL_VariantMap.h"
#include "../helper/HL_Pointer.h" #include "../helper/HL_Pointer.h"
@@ -16,118 +16,120 @@
namespace Audio namespace Audio
{ {
enum enum
{ {
myMicrophone = 1, myMicrophone = 1,
mySpeaker = 2 mySpeaker = 2
}; };
struct Format struct Format
{ {
int mRate; int mRate;
int mChannels; int mChannels;
Format() : mRate(AUDIO_SAMPLERATE), mChannels(AUDIO_CHANNELS) {} Format()
:mRate(AUDIO_SAMPLERATE), mChannels(AUDIO_CHANNELS)
{}
Format(int rate, int channels) : mRate(rate), mChannels(channels) {} Format(int rate, int channels)
:mRate(rate), mChannels(channels)
{}
size_t samplesFromSize(size_t length) const { return length / 2 / mChannels; } size_t samplesFromSize(size_t length) const
{
return length / 2 / mChannels;
}
// Returns milliseconds // Returns milliseconds
float timeFromSize(size_t length) const { return float(samplesFromSize(length) / (mRate / 1000.0)); } float timeFromSize(size_t length) const
{
return float(samplesFromSize(length) / (mRate / 1000.0));
}
float sizeFromTime(size_t milliseconds) const { return float((milliseconds * mRate) / 500.0 * mChannels); } float sizeFromTime(size_t milliseconds) const
{
size_t sizeFromTime(std::chrono::milliseconds ms) const { return sizeFromTime(ms.count()); } return float((milliseconds * mRate) / 500.0 * mChannels);
}
std::string toString() std::string toString()
{ {
char buffer[64]; char buffer[64];
std::snprintf(buffer, sizeof(buffer), "%dHz %dch", mRate, mChannels); sprintf(buffer, "%dHz %dch", mRate, mChannels);
return std::string(buffer); return std::string(buffer);
} }
};
bool operator==(const Format& rhs) const { return mRate == rhs.mRate && mChannels == rhs.mChannels; } class DataConnection
{
bool operator!=(const Format& rhs) const { return mRate != rhs.mRate || mChannels != rhs.mChannels; } public:
int rate() const { return mRate; }
int channels() const { return mChannels; }
};
class DataConnection
{
public:
virtual void onMicData(const Format& format, const void* buffer, int length) = 0; virtual void onMicData(const Format& format, const void* buffer, int length) = 0;
virtual void onSpkData(const Format& format, void* buffer, int length) = 0; virtual void onSpkData(const Format& format, void* buffer, int length) = 0;
}; };
class Device class Device
{ {
public: public:
Device(); Device();
virtual ~Device(); virtual ~Device();
void setConnection(DataConnection* connection); void setConnection(DataConnection* connection);
DataConnection* connection(); DataConnection* connection();
virtual bool open() = 0; virtual bool open() = 0;
virtual void close() = 0; virtual void close() = 0;
virtual Format getFormat() = 0; virtual Format getFormat() = 0;
protected:
protected:
DataConnection* mConnection; DataConnection* mConnection;
}; };
class InputDevice : public Device class InputDevice: public Device
{ {
public: public:
InputDevice(); InputDevice();
virtual ~InputDevice(); virtual ~InputDevice();
static InputDevice* make(int devId); static InputDevice* make(int devId);
}; };
typedef std::shared_ptr<InputDevice> PInputDevice; typedef std::shared_ptr<InputDevice> PInputDevice;
class OutputDevice : public Device class OutputDevice: public Device
{ {
public: public:
OutputDevice(); OutputDevice();
virtual ~OutputDevice(); virtual ~OutputDevice();
static OutputDevice* make(int devId); static OutputDevice* make(int devId);
}; };
typedef std::shared_ptr<OutputDevice> POutputDevice; typedef std::shared_ptr<OutputDevice> POutputDevice;
class Enumerator class Enumerator
{ {
public: public:
Enumerator(); Enumerator();
virtual ~Enumerator(); virtual ~Enumerator();
int nameToIndex(const std::tstring& name); int nameToIndex(const std::tstring& name);
virtual void open(int direction) = 0; virtual void open(int direction) = 0;
virtual void close() = 0; virtual void close() = 0;
virtual int count() = 0; virtual int count() = 0;
virtual std::tstring nameAt(int index) = 0; virtual std::tstring nameAt(int index) = 0;
virtual int idAt(int index) = 0; virtual int idAt(int index) = 0;
virtual int indexOfDefaultDevice() = 0; virtual int indexOfDefaultDevice() = 0;
static Enumerator* make(bool useNull = false); static Enumerator* make(bool useNull = false);
}; };
class OsEngine class OsEngine
{ {
public: public:
virtual void open() = 0; virtual void open() = 0;
virtual void close() = 0; virtual void close() = 0;
static OsEngine* instance(); static OsEngine* instance();
};
}; };
}; // namespace Audio
#endif #endif
+212 -225
View File
@@ -3,7 +3,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "../engine_config.h" #include "../config.h"
#include "../helper/HL_Exception.h" #include "../helper/HL_Exception.h"
#include "../helper/HL_Log.h" #include "../helper/HL_Log.h"
@@ -12,339 +12,326 @@
#include "Audio_Mixer.h" #include "Audio_Mixer.h"
#define LOG_SUBSYSTEM "audio" #define LOG_SUBSYSTEM "Mixer"
using namespace Audio; using namespace Audio;
Mixer::Stream::Stream() Mixer::Stream::Stream()
{ {
mResampler8.start(AUDIO_CHANNELS, 8000, AUDIO_SAMPLERATE); mResampler8.start(AUDIO_CHANNELS, 8000, AUDIO_SAMPLERATE);
mResampler16.start(AUDIO_CHANNELS, 16000, AUDIO_SAMPLERATE); mResampler16.start(AUDIO_CHANNELS, 16000, AUDIO_SAMPLERATE);
mResampler32.start(AUDIO_CHANNELS, 32000, AUDIO_SAMPLERATE); mResampler32.start(AUDIO_CHANNELS, 32000, AUDIO_SAMPLERATE);
mResampler48.start(AUDIO_CHANNELS, 48000, AUDIO_SAMPLERATE); mResampler48.start(AUDIO_CHANNELS, 48000, AUDIO_SAMPLERATE);
mActive = false; mActive = false;
mContext = nullptr; mContext = nullptr;
mSSRC = 0; mSSRC = 0;
mFadeOutCounter = 0; mFadeOutCounter = 0;
mData.setCapacity(AUDIO_SPK_BUFFER_SIZE * AUDIO_SPK_BUFFER_COUNT); mData.setCapacity(AUDIO_SPK_BUFFER_SIZE * AUDIO_SPK_BUFFER_COUNT);
} }
Mixer::Stream::~Stream() {} Mixer::Stream::~Stream()
{
}
void Mixer::Stream::setSsrc(unsigned ssrc) void Mixer::Stream::setSsrc(unsigned ssrc)
{ {
mSSRC = ssrc; mSSRC = ssrc;
} }
unsigned Mixer::Stream::ssrc() unsigned Mixer::Stream::ssrc()
{ {
return mSSRC; return mSSRC;
} }
void Mixer::Stream::setContext(void* context) void Mixer::Stream::setContext(void* context)
{ {
mContext = context; mContext = context;
} }
void* Mixer::Stream::context() void* Mixer::Stream::context()
{ {
return mContext; return mContext;
} }
DataWindow& Mixer::Stream::data() DataWindow& Mixer::Stream::data()
{ {
return mData; return mData;
} }
bool Mixer::Stream::active() bool Mixer::Stream::active()
{ {
return mActive; return mActive;
} }
void Mixer::Stream::setActive(bool active) void Mixer::Stream::setActive(bool active)
{ {
mActive = active; mActive = active;
} }
void Mixer::Stream::addPcm(int rate, const void* input, int length) void Mixer::Stream::addPcm(int rate, const void* input, int length)
{ {
// Resample to internal sample rate // Resample to internal sample rate
size_t outputSize = size_t(0.5 + length * ((float)AUDIO_SAMPLERATE / rate)); size_t outputSize = size_t(0.5 + length * ((float)AUDIO_SAMPLERATE / rate));
if (mTempBuffer.size() < outputSize) if (mTempBuffer.size() < outputSize)
mTempBuffer.resize(outputSize); mTempBuffer.resize(outputSize);
Resampler* resampler = (rate == 8000) Resampler* resampler = (rate == 8000) ? &mResampler8 : ((rate == 16000) ? &mResampler16 : ((rate == 32000) ? &mResampler32 : &mResampler48));
? &mResampler8 size_t inputProcessed = 0;
: ((rate == 16000) ? &mResampler16 : ((rate == 32000) ? &mResampler32 : &mResampler48)); resampler->processBuffer(input, length, inputProcessed, mTempBuffer.mutableData(), outputSize);
size_t inputProcessed = 0; // inputProcessed result value is ignored here - rate will be 8/16/32/48k, inputProcessed is equal to length
resampler->processBuffer(input, length, inputProcessed, mTempBuffer.mutableData(), outputSize);
// inputProcessed result value is ignored here - rate will be 8/16/32/48k, inputProcessed is equal to length
// Queue data // Queue data
mData.add(mTempBuffer.data(), outputSize); mData.add(mTempBuffer.data(), outputSize);
} }
Mixer::Mixer() Mixer::Mixer()
{ {
mActiveCounter = 0; mActiveCounter = 0;
mOutput.setCapacity(32768); mOutput.setCapacity(32768);
} }
Mixer::~Mixer() {} Mixer::~Mixer()
{
}
void Mixer::unregisterChannel(void* channel) void Mixer::unregisterChannel(void* channel)
{ {
Lock l(mMutex); for (int i=0; i<AUDIO_MIX_CHANNEL_COUNT; i++)
for (int i = 0; i < AUDIO_MIX_CHANNEL_COUNT; i++) {
Stream& c = mChannelList[i];
if (c.active() && c.context() == channel)
{ {
Stream& c = mChannelList[i]; c.setActive(false); // stream is not active anymore
if (c.active() && c.context() == channel) c.data().clear(); // clear data
{ mActiveCounter--;
c.setActive(false); // stream is not active anymore
c.data().clear(); // clear data
mActiveCounter--;
}
} }
}
} }
void Mixer::clear(void* context, unsigned ssrc) void Mixer::clear(void* context, unsigned ssrc)
{ {
Lock l(mMutex); for (int i=0; i<AUDIO_MIX_CHANNEL_COUNT; i++)
for (int i = 0; i < AUDIO_MIX_CHANNEL_COUNT; i++) {
Stream& c = mChannelList[i];
if (c.active() && c.context() == context && c.ssrc() == ssrc)
{ {
Stream& c = mChannelList[i]; c.setActive(false);
if (c.active() && c.context() == context && c.ssrc() == ssrc) c.data().clear();
{ mActiveCounter--;
c.setActive(false);
c.data().clear();
mActiveCounter--;
}
} }
}
} }
Mixer::Stream* Mixer::allocateChannel(void* context, unsigned ssrc) Mixer::Stream* Mixer::allocateChannel(void* context, unsigned ssrc)
{ {
// Allocate new channel // Allocate new channel
Lock l(mMutex); Lock l(mMutex);
Stream* channel; Stream* channel;
for (int i = 0; i < AUDIO_MIX_CHANNEL_COUNT; i++) for (int i=0; i<AUDIO_MIX_CHANNEL_COUNT;i++)
{
channel = &mChannelList[i];
if (!channel->active())
{ {
channel = &mChannelList[i]; channel->setSsrc(ssrc);
if (!channel->active()) channel->setContext(context);
{ channel->data().clear();
channel->setSsrc(ssrc); mActiveCounter++;
channel->setContext(context); channel->setActive(true);
channel->data().clear(); return channel;
mActiveCounter++;
channel->setActive(true);
return channel;
}
} }
return NULL; }
return NULL;
} }
void Mixer::addPcm(void* context, unsigned ssrc, const void* inputData, int inputLength, int inputRate, bool fadeOut) void Mixer::addPcm(void* context, unsigned ssrc,
const void* inputData, int inputLength,
int inputRate, bool fadeOut)
{ {
assert(inputRate == 8000 || inputRate == 16000 || inputRate == 32000); assert(inputRate == 8000 || inputRate == 16000 || inputRate == 32000);
Lock l(mMutex); int i;
int i;
// Locate a channel // Locate a channel
Stream* channel = NULL; Stream* channel = NULL;
for (i = 0; i < AUDIO_MIX_CHANNEL_COUNT && !channel; i++) for (i=0; i<AUDIO_MIX_CHANNEL_COUNT && !channel; i++)
{ {
Stream& c = mChannelList[i]; Stream& c = mChannelList[i];
if (c.active() && c.context() == context && c.ssrc() == ssrc) if (c.active() && c.context() == context && c.ssrc() == ssrc)
channel = &c; channel = &c;
} }
if (!channel)
{
channel = allocateChannel(context, ssrc);
if (!channel) if (!channel)
{ throw Exception(ERR_MIXER_OVERFLOW);
channel = allocateChannel(context, ssrc); }
if (!channel)
throw Exception(ERR_MIXER_OVERFLOW);
}
channel->addPcm(inputRate, inputData, inputLength); channel->addPcm(inputRate, inputData, inputLength);
} }
void Mixer::addPcm(void* context, unsigned ssrc, Audio::DataWindow& w, int rate, bool fadeOut) void Mixer::addPcm(void* context, unsigned ssrc, Audio::DataWindow& w, int rate, bool fadeOut)
{ {
assert(rate == 8000 || rate == 16000 || rate == 32000 || rate == 48000); assert(rate == 8000 || rate == 16000 || rate == 32000 || rate == 48000);
Lock l(mMutex); int i;
int i;
// Locate a channel // Locate a channel
Stream* channel = NULL; Stream* channel = NULL;
for (i = 0; i < AUDIO_MIX_CHANNEL_COUNT && !channel; i++) for (i=0; i<AUDIO_MIX_CHANNEL_COUNT && !channel; i++)
{ {
Stream& c = mChannelList[i]; Stream& c = mChannelList[i];
if (c.active() && c.context() == context && c.ssrc() == ssrc) if (c.active() && c.context() == context && c.ssrc() == ssrc)
channel = &c; channel = &c;
} }
if (!channel)
{
channel = allocateChannel(context, ssrc);
if (!channel) if (!channel)
{ throw Exception(ERR_MIXER_OVERFLOW);
channel = allocateChannel(context, ssrc); }
if (!channel)
throw Exception(ERR_MIXER_OVERFLOW);
}
channel->addPcm(rate, w.data(), w.filled()); channel->addPcm(rate, w.data(), w.filled());
// ICELogSpecial(<<"Mixer stream " << int(this) << " has " << w.filled() << " bytes"); //ICELogSpecial(<<"Mixer stream " << int(this) << " has " << w.filled() << " bytes");
} }
void Mixer::mix() void Mixer::mix()
{ {
Lock l(mMutex); // Current sample
int sample = 0;
// Current sample // Counter of processed active channels
int sample = 0; int processed = 0;
// Counter of processed active channels // Samples & sources counters
int processed = 0; unsigned sampleCounter = 0, sourceCounter;
// Samples & sources counters short outputBuffer[512];
unsigned sampleCounter = 0, sourceCounter; unsigned outputCounter = 0;
short outputBuffer[512]; // Build active channel map
unsigned outputCounter = 0; Stream* channelList[AUDIO_MIX_CHANNEL_COUNT];
int activeCounter = 0;
for (int i=0; i<AUDIO_MIX_CHANNEL_COUNT; i++)
if (mChannelList[i].active())
channelList[activeCounter++] = &mChannelList[i];
// Build active channel map // No active channels - nothing to mix - exit
Stream* channelList[AUDIO_MIX_CHANNEL_COUNT]; if (!activeCounter)
int activeCounter = 0; {
for (int i = 0; i < AUDIO_MIX_CHANNEL_COUNT; i++) // ICELogDebug(<< "No active channel");
if (mChannelList[i].active()) return;
channelList[activeCounter++] = &mChannelList[i]; }
// No active channels - nothing to mix - exit // Optimized versions for 1& 2 active channels
if (!activeCounter) if (activeCounter == 1)
{
// Copy much samples as we have
Stream& audio = *channelList[0];
// Copy the decoded data
mOutput.add(audio.data().data(), audio.data().filled());
// Erase copied audio samples
audio.data().erase(audio.data().filled());
//ICELogSpecial(<<"Length of mixer stream " << audio.data().filled());
}
else
if (activeCounter == 2)
{
Stream& audio1 = *channelList[0];
Stream& audio2 = *channelList[1];
int filled1 = audio1.data().filled() / 2, filled2 = audio2.data().filled() / 2;
int available = filled1 > filled2 ? filled1 : filled2;
// Find how much samples can be mixed
int filled = mOutput.filled() / 2;
int maxsize = mOutput.capacity() / 2;
if (maxsize - filled < available)
available = maxsize - filled;
short sample = 0;
for (int i=0; i<available; i++)
{
short sample1 = filled1 > i ? audio1.data().shortAt(i) : 0;
short sample2 = filled2 > i ? audio2.data().shortAt(i) : 0;
sample = (abs(sample1) > abs(sample2)) ? sample1 : sample2;
mOutput.add(sample);
}
audio1.data().erase(available*2);
audio2.data().erase(available*2);
}
else
{
do
{ {
// ICELogDebug(<< "No active channel"); sample = 0;
return; sourceCounter = 0;
} processed = 0;
for (int i=0; i<activeCounter; i++)
{
Stream& audio = *channelList[i];
processed++;
// Optimized versions for 1& 2 active channels if (audio.data().filled() > (int)sampleCounter * 2)
if (activeCounter == 1)
{
// Copy much samples as we have
Stream& audio = *channelList[0];
// Copy the decoded data
mOutput.add(audio.data().data(), audio.data().filled());
// Erase copied audio samples
audio.data().erase(audio.data().filled());
// ICELogSpecial(<<"Length of mixer stream " << audio.data().filled());
}
else if (activeCounter == 2)
{
Stream& audio1 = *channelList[0];
Stream& audio2 = *channelList[1];
int filled1 = audio1.data().filled() / 2, filled2 = audio2.data().filled() / 2;
int available = filled1 > filled2 ? filled1 : filled2;
// Find how much samples can be mixed
int filled = mOutput.filled() / 2;
int maxsize = mOutput.capacity() / 2;
if (maxsize - filled < available)
available = maxsize - filled;
short sample = 0;
for (int i = 0; i < available; i++)
{ {
short sample1 = filled1 > i ? audio1.data().shortAt(i) : 0; short currentSample = audio.data().shortAt(sampleCounter);
short sample2 = filled2 > i ? audio2.data().shortAt(i) : 0; if (abs(currentSample) > abs(sample))
sample = (abs(sample1) > abs(sample2)) ? sample1 : sample2; sample = currentSample;
sourceCounter++;
mOutput.add(sample);
} }
audio1.data().erase(available * 2); }
audio2.data().erase(available * 2);
if (sourceCounter)
{
outputBuffer[outputCounter++] = (short)sample;
sampleCounter++;
}
// Check if time to flash output buffer
if ((!sourceCounter || outputCounter == 512) && outputCounter)
{
mOutput.add(outputBuffer, outputCounter * 2);
outputCounter = 0;
}
} }
else while (sourceCounter);
processed = 0;
for (int i=0; i<activeCounter; i++)
{ {
do Stream& audio = *channelList[i];
{ audio.data().erase(sampleCounter*2);
sample = 0;
sourceCounter = 0;
processed = 0;
for (int i = 0; i < activeCounter; i++)
{
Stream& audio = *channelList[i];
processed++;
if (audio.data().filled() > (int)sampleCounter * 2)
{
short currentSample = audio.data().shortAt(sampleCounter);
if (abs(currentSample) > abs(sample))
sample = currentSample;
sourceCounter++;
}
}
if (sourceCounter)
{
outputBuffer[outputCounter++] = (short)sample;
sampleCounter++;
}
// Check if time to flash output buffer
if ((!sourceCounter || outputCounter == 512) && outputCounter)
{
mOutput.add(outputBuffer, outputCounter * 2);
outputCounter = 0;
}
} while (sourceCounter);
processed = 0;
for (int i = 0; i < activeCounter; i++)
{
Stream& audio = *channelList[i];
audio.data().erase(sampleCounter * 2);
}
} }
}
} }
int Mixer::getPcm(void* outputData, int outputLength) int Mixer::getPcm(void* outputData, int outputLength)
{ {
Lock l(mMutex); if (mOutput.filled() < outputLength)
mix();
if (mOutput.filled() < outputLength) //ICELogSpecial(<<"Mixer has " << mOutput.filled() << " available bytes");
mix(); memset(outputData, 0, outputLength);
return mOutput.read(outputData, outputLength);
// ICELogSpecial(<<"Mixer has " << mOutput.filled() << " available bytes");
memset(outputData, 0, outputLength);
return mOutput.read(outputData, outputLength);
} }
int Mixer::mixAndGetPcm(Audio::DataWindow& output) int Mixer::mixAndGetPcm(Audio::DataWindow& output)
{ {
Lock l(mMutex); // Mix
mix();
// Mix // Set output space
mix(); output.setCapacity(mOutput.filled());
size_t avail = mOutput.filled(); // Read mixed data to output
if (!avail) return mOutput.read(output.mutableData(), output.capacity());
{
output.setFilled(0);
return 0;
}
// Make sure output has enough space (setCapacity only ever grows the window)
if (output.capacity() < avail)
output.setCapacity(avail);
// Read mixed data to output and publish the real byte count
size_t got = mOutput.read(output.mutableData(), avail);
output.setFilled(got);
return static_cast<int>(got);
} }
int Mixer::available() int Mixer::available()
{ {
return mOutput.filled(); return mOutput.filled();
} }
+34 -31
View File
@@ -6,7 +6,7 @@
#ifndef _RX_MIXER_H #ifndef _RX_MIXER_H
#define _RX_MIXER_H #define _RX_MIXER_H
#include "../engine_config.h" #include "../config.h"
#include "../helper/HL_ByteBuffer.h" #include "../helper/HL_ByteBuffer.h"
#include "../helper/HL_Sync.h" #include "../helper/HL_Sync.h"
#include "Audio_Resampler.h" #include "Audio_Resampler.h"
@@ -16,43 +16,46 @@
namespace Audio namespace Audio
{ {
class Mixer class Mixer
{ {
protected: protected:
class Stream class Stream
{ {
protected: protected:
DataWindow mData; DataWindow mData;
Resampler mResampler8, mResampler16, mResampler32, mResampler48; Resampler mResampler8,
bool mActive; mResampler16,
void* mContext; mResampler32,
unsigned mSSRC; mResampler48;
unsigned mFadeOutCounter; bool mActive;
ByteBuffer mTempBuffer; void* mContext;
unsigned mSSRC;
unsigned mFadeOutCounter;
ByteBuffer mTempBuffer;
public: public:
Stream(); Stream();
~Stream(); ~Stream();
void setSsrc(unsigned ssrc); void setSsrc(unsigned ssrc);
unsigned ssrc(); unsigned ssrc();
void setContext(void* context); void setContext(void* context);
void* context(); void* context();
DataWindow& data(); DataWindow& data();
bool active(); bool active();
void setActive(bool active); void setActive(bool active);
void addPcm(int rate, const void* input, int length); void addPcm(int rate, const void* input, int length);
}; };
Stream mChannelList[AUDIO_MIX_CHANNEL_COUNT]; Stream mChannelList[AUDIO_MIX_CHANNEL_COUNT];
Mutex mMutex; Mutex mMutex;
DataWindow mOutput; DataWindow mOutput;
std::atomic_int mActiveCounter; std::atomic_int mActiveCounter;
void mix(); void mix();
Stream* allocateChannel(void* context, unsigned ssrc); Stream* allocateChannel(void* context, unsigned ssrc);
public: public:
Mixer(); Mixer();
~Mixer(); ~Mixer();
@@ -62,8 +65,8 @@ public:
void addPcm(void* context, unsigned ssrc, Audio::DataWindow& w, int rate, bool fadeOut); void addPcm(void* context, unsigned ssrc, Audio::DataWindow& w, int rate, bool fadeOut);
int getPcm(void* outputData, int outputLength); int getPcm(void* outputData, int outputLength);
int mixAndGetPcm(Audio::DataWindow& output); int mixAndGetPcm(Audio::DataWindow& output);
int available(); int available();
}; };
} // namespace Audio } //end of namespace
#endif #endif
+85 -94
View File
@@ -1,187 +1,178 @@
#include "Audio_Null.h" #include "Audio_Null.h"
#include "helper/HL_Log.h" #include "helper/HL_Log.h"
#include <assert.h> #include <assert.h>
#include <chrono> #define LOG_SUBSYSTEM "NULL audio"
#define LOG_SUBSYSTEM "audio"
using namespace Audio; using namespace Audio;
using namespace std::chrono_literals;
NullTimer::NullTimer(std::chrono::milliseconds interval, Delegate* delegate, const char* name) NullTimer::NullTimer(int interval, Delegate *delegate, const char* name)
: mShutdown(false), mDelegate(delegate), mInterval(interval), mThreadName(name) :mShutdown(false), mDelegate(delegate), mInterval(interval), mThreadName(name)
{ {
start(); start();
} }
NullTimer::~NullTimer() NullTimer::~NullTimer()
{ {
stop(); stop();
} }
void NullTimer::start() void NullTimer::start()
{ {
mShutdown = false; mShutdown = false;
mWorkerThread = std::thread(&NullTimer::run, this); mWorkerThread = std::thread(&NullTimer::run, this);
} }
void NullTimer::stop() void NullTimer::stop()
{ {
mShutdown = true; mShutdown = true;
if (mWorkerThread.joinable()) if (mWorkerThread.joinable())
mWorkerThread.join(); mWorkerThread.join();
} }
void NullTimer::run() void NullTimer::run()
{ {
mTail = 0us; mTail = 0;
while (!mShutdown) while (!mShutdown)
{
// Get current timestamp
std::chrono::system_clock::time_point timestamp = std::chrono::system_clock::now();
while (mTail >= mInterval * 1000)
{ {
// Get current timestamp if (mDelegate)
std::chrono::system_clock::time_point timestamp = std::chrono::system_clock::now(); mDelegate->onTimerSignal(*this);
mTail -= mInterval * 1000;
while (mTail >= mInterval)
{
if (mDelegate)
mDelegate->onTimerSignal(*this);
mTail -= mInterval;
}
// Sleep for mInterval - mTail milliseconds
std::this_thread::sleep_for(mInterval - mTail);
mTail =
mTail + std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now() - timestamp);
} }
// Sleep for mInterval - mTail milliseconds
std::this_thread::sleep_for(std::chrono::microseconds(mInterval * 1000 - mTail));
mTail += (int)std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now() - timestamp).count();
}
} }
// --------------------- NullInputDevice ------------------------- // --------------------- NullInputDevice -------------------------
NullInputDevice::NullInputDevice() : mBuffer(nullptr) {} NullInputDevice::NullInputDevice()
:mBuffer(nullptr)
{
}
NullInputDevice::~NullInputDevice() NullInputDevice::~NullInputDevice()
{ {
internalClose(); close();
} }
bool NullInputDevice::open() bool NullInputDevice::open()
{ {
ICELogInfo(<< "Starting NullInputDevice for " << AUDIO_MIC_BUFFER_LENGTH << "ms buffers"); mBuffer = malloc(AUDIO_MIC_BUFFER_SIZE);
mBuffer = malloc(AUDIO_MIC_BUFFER_SIZE); memset(mBuffer, 0, AUDIO_MIC_BUFFER_SIZE);
memset(mBuffer, 0, AUDIO_MIC_BUFFER_SIZE); mTimeCounter = 0; mDataCounter = 0;
mTimeCounter = 0; // Creation of timer starts it also. So first onTimerSignal can come even before open() returns.
mDataCounter = 0; mTimer = std::make_shared<NullTimer>(AUDIO_MIC_BUFFER_LENGTH, this, "NullMicrophoneThread");
return true;
// Creation of timer starts it also. So first onTimerSignal can come even before open() returns.
mTimer = std::make_shared<NullTimer>(std::chrono::milliseconds(AUDIO_MIC_BUFFER_LENGTH), this, "null_mic");
return true;
}
void NullInputDevice::internalClose()
{
ICELogInfo(<< "Stopping NullInputDevice");
mTimer.reset();
if (mBuffer)
{
free(mBuffer);
mBuffer = nullptr;
}
ICELogInfo(<< "Pseudocaptured " << mTimeCounter << " milliseconds , " << mDataCounter << " bytes.");
} }
void NullInputDevice::close() void NullInputDevice::close()
{ {
internalClose(); mTimer.reset();
if (mBuffer)
{
free(mBuffer);
mBuffer = nullptr;
}
ICELogInfo(<<"Pseudocaptured " << mTimeCounter << " milliseconds , " << mDataCounter << " bytes.");
} }
Format NullInputDevice::getFormat() Format NullInputDevice::getFormat()
{ {
assert(Format().sizeFromTime(AUDIO_MIC_BUFFER_LENGTH) == AUDIO_MIC_BUFFER_SIZE); assert (Format().sizeFromTime(AUDIO_MIC_BUFFER_LENGTH) == AUDIO_MIC_BUFFER_SIZE);
return Format();
return {}; // Return library-define default format
} }
void NullInputDevice::onTimerSignal(NullTimer& timer) void NullInputDevice::onTimerSignal(NullTimer& timer)
{ {
mTimeCounter += AUDIO_MIC_BUFFER_LENGTH; mTimeCounter += AUDIO_MIC_BUFFER_LENGTH;
mDataCounter += AUDIO_MIC_BUFFER_SIZE; mDataCounter += AUDIO_MIC_BUFFER_SIZE;
if (mConnection) if (mConnection)
mConnection->onMicData(getFormat(), mBuffer, AUDIO_MIC_BUFFER_SIZE); mConnection->onMicData(getFormat(), mBuffer, AUDIO_MIC_BUFFER_SIZE);
} }
// --------------------- NullOutputDevice -------------------------- // --------------------- NullOutputDevice --------------------------
NullOutputDevice::NullOutputDevice() : mBuffer(nullptr) {} NullOutputDevice::NullOutputDevice()
:mBuffer(nullptr)
{
}
NullOutputDevice::~NullOutputDevice() NullOutputDevice::~NullOutputDevice()
{ {
internalClose(); close();
} }
bool NullOutputDevice::open() bool NullOutputDevice::open()
{ {
mTimeCounter = 0; mTimeCounter = 0; mDataCounter = 0;
mDataCounter = 0; mBuffer = malloc(AUDIO_SPK_BUFFER_SIZE);
mBuffer = malloc(AUDIO_SPK_BUFFER_SIZE); // Creation of timer starts it also. So first onSpkData() can come before open() returns even.
// Creation of timer starts it also. So first onSpkData() can come before open() returns even. mTimer = std::make_shared<NullTimer>(AUDIO_SPK_BUFFER_LENGTH, this, "NullSpeakerThread");
mTimer = std::make_shared<NullTimer>(std::chrono::milliseconds(AUDIO_SPK_BUFFER_LENGTH), this, "null_spk"); return true;
return true;
}
void NullOutputDevice::internalClose()
{
mTimer.reset();
free(mBuffer);
mBuffer = nullptr;
ICELogInfo(<< "Pseudoplayed " << mTimeCounter << " milliseconds, " << mDataCounter << " bytes.");
} }
void NullOutputDevice::close() void NullOutputDevice::close()
{ {
internalClose(); mTimer.reset();
free(mBuffer); mBuffer = nullptr;
ICELogInfo(<< "Pseudoplayed " << mTimeCounter << " milliseconds, " << mDataCounter << " bytes.");
} }
Format NullOutputDevice::getFormat() Format NullOutputDevice::getFormat()
{ {
assert(Format().sizeFromTime(AUDIO_SPK_BUFFER_LENGTH) == AUDIO_SPK_BUFFER_SIZE); assert (Format().sizeFromTime(AUDIO_SPK_BUFFER_LENGTH) == AUDIO_SPK_BUFFER_SIZE);
return Format(); return Format();
} }
void NullOutputDevice::onTimerSignal(NullTimer& timer) void NullOutputDevice::onTimerSignal(NullTimer &timer)
{ {
mTimeCounter += AUDIO_SPK_BUFFER_LENGTH; mTimeCounter += AUDIO_SPK_BUFFER_LENGTH;
mDataCounter += AUDIO_SPK_BUFFER_SIZE; mDataCounter += AUDIO_SPK_BUFFER_SIZE;
if (mConnection) if (mConnection)
mConnection->onSpkData(getFormat(), mBuffer, AUDIO_SPK_BUFFER_SIZE); mConnection->onSpkData(getFormat(), mBuffer, AUDIO_SPK_BUFFER_SIZE);
} }
// ---------------------- NullEnumerator -------------------------- // ---------------------- NullEnumerator --------------------------
NullEnumerator::NullEnumerator() {} NullEnumerator::NullEnumerator()
{}
NullEnumerator::~NullEnumerator() {} NullEnumerator::~NullEnumerator()
{}
void NullEnumerator::open(int direction) {} void NullEnumerator::open(int direction)
{}
void NullEnumerator::close() {} void NullEnumerator::close()
{}
int NullEnumerator::count() int NullEnumerator::count()
{ {
return 1; return 1;
} }
std::tstring NullEnumerator::nameAt(int index) std::tstring NullEnumerator::nameAt(int index)
{ {
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
return L"null"; return L"null";
#else #else
return "null"; return "null";
#endif #endif
} }
int NullEnumerator::idAt(int index) int NullEnumerator::idAt(int index)
{ {
return 0; return 0;
} }
int NullEnumerator::indexOfDefaultDevice() int NullEnumerator::indexOfDefaultDevice()
{ {
return 0; return 0;
} }
+47 -54
View File
@@ -2,92 +2,85 @@
#define __AUDIO_NULL_H #define __AUDIO_NULL_H
#include <thread> #include <thread>
#include <chrono>
#include "Audio_Interface.h" #include "Audio_Interface.h"
namespace Audio namespace Audio
{ {
class NullTimer class NullTimer
{ {
public: public:
class Delegate class Delegate
{ {
public: public:
virtual void onTimerSignal(NullTimer& timer) = 0; virtual void onTimerSignal(NullTimer& timer) = 0;
}; };
protected: protected:
std::thread mWorkerThread; std::thread mWorkerThread;
std::atomic_bool mShutdown = {false}; volatile bool mShutdown;
Delegate* mDelegate = nullptr; Delegate* mDelegate;
std::chrono::milliseconds mInterval; // Interval - wanted number of milliseconds int mInterval, // Interval - wanted number of milliseconds
std::chrono::microseconds mTail; // Number of milliseconds that can be sent immediately to sink mTail; // Number of milliseconds that can be sent immediately to sink
std::string mThreadName; std::string mThreadName;
void start(); void start();
void stop(); void stop();
void run(); void run();
public:
public:
/* Interval is in milliseconds. */ /* Interval is in milliseconds. */
NullTimer(std::chrono::milliseconds interval, Delegate* delegate, const char* name = nullptr); NullTimer(int interval, Delegate* delegate, const char* name = nullptr);
~NullTimer(); ~NullTimer();
}; };
class NullInputDevice : public InputDevice, public NullTimer::Delegate class NullInputDevice: public InputDevice, public NullTimer::Delegate
{ {
protected: protected:
void* mBuffer = nullptr; void* mBuffer = nullptr;
std::shared_ptr<NullTimer> mTimer; std::shared_ptr<NullTimer> mTimer;
int64_t mTimeCounter = 0, mDataCounter = 0; int64_t mTimeCounter = 0, mDataCounter = 0;
void internalClose(); public:
public:
NullInputDevice(); NullInputDevice();
virtual ~NullInputDevice(); virtual ~NullInputDevice();
bool open() override; bool open() override;
void close() override; void close() override;
Format getFormat() override; Format getFormat() override;
void onTimerSignal(NullTimer& timer) override; void onTimerSignal(NullTimer& timer) override;
}; };
class NullOutputDevice : public OutputDevice, public NullTimer::Delegate class NullOutputDevice: public OutputDevice, public NullTimer::Delegate
{ {
protected: protected:
std::shared_ptr<NullTimer> mTimer; std::shared_ptr<NullTimer> mTimer;
void* mBuffer = nullptr; void* mBuffer = nullptr;
int64_t mDataCounter = 0, mTimeCounter = 0; int64_t mDataCounter = 0, mTimeCounter = 0;
public:
void internalClose();
public:
NullOutputDevice(); NullOutputDevice();
virtual ~NullOutputDevice(); virtual ~NullOutputDevice();
bool open() override; bool open() override;
void close() override; void close() override;
Format getFormat() override; Format getFormat() override;
void onTimerSignal(NullTimer& timer) override; void onTimerSignal(NullTimer& timer) override;
}; };
class NullEnumerator : public Enumerator class NullEnumerator: public Enumerator
{ {
public: public:
NullEnumerator(); NullEnumerator();
~NullEnumerator(); ~NullEnumerator();
void open(int direction) override; void open(int direction) override;
void close() override; void close() override;
int count() override; int count() override;
std::tstring nameAt(int index) override; std::tstring nameAt(int index) override;
int idAt(int index) override; int idAt(int index) override;
int indexOfDefaultDevice() override; int indexOfDefaultDevice() override;
};
} // namespace Audio };
}
#endif #endif
+91 -86
View File
@@ -7,160 +7,165 @@
#include "../helper/HL_Log.h" #include "../helper/HL_Log.h"
#define LOG_SUBSYSTEM "audio" #define LOG_SUBSYSTEM "Player"
using namespace Audio; using namespace Audio;
// -------------- Player ----------- // -------------- Player -----------
Player::Player() : mDelegate(nullptr), mPlayedTime(0) {} Player::Player()
:mDelegate(nullptr), mPlayedTime(0)
{
}
Player::~Player() {} Player::~Player()
{
}
void Player::setDelegate(EndOfAudioDelegate* d) void Player::setDelegate(EndOfAudioDelegate* d)
{ {
mDelegate = d; mDelegate = d;
} }
Player::EndOfAudioDelegate* Player::getDelegate() const Player::EndOfAudioDelegate* Player::getDelegate() const
{ {
return mDelegate; return mDelegate;
} }
void Player::setOutput(POutputDevice output) void Player::setOutput(POutputDevice output)
{ {
mOutput = output; mOutput = output;
if (mOutput) if (mOutput)
mOutput->setConnection(this); mOutput->setConnection(this);
} }
POutputDevice Player::getOutput() const POutputDevice Player::getOutput() const
{ {
return mOutput; return mOutput;
} }
void Player::onMicData(const Format& f, const void* buffer, int length) void Player::onMicData(const Format& f, const void* buffer, int length)
{ {
// Do nothing here - this data sink is not used in player // Do nothing here - this data sink is not used in player
} }
#define BYTES_PER_MILLISECOND (AUDIO_SAMPLERATE / 1000 * 2 * AUDIO_CHANNELS) #define BYTES_PER_MILLISECOND (AUDIO_SAMPLERATE / 1000 * 2 * AUDIO_CHANNELS)
void Player::onSpkData(const Format& f, void* buffer, int length) void Player::onSpkData(const Format& f, void* buffer, int length)
{ {
Lock l(mGuard); Lock l(mGuard);
// Fill buffer by zero if player owns dedicated device // Fill buffer by zero if player owns dedicated device
if (mOutput) if (mOutput)
memset(buffer, 0, length); memset(buffer, 0, length);
// See if there is item in playlist // See if there is item in playlist
int produced = 0; int produced = 0;
while (mPlaylist.size() && produced < length) while (mPlaylist.size() && produced < length)
{
PlaylistItem& item = mPlaylist.front();
// Check for timelength
if (item.mTimelength > 0 && item.mTimelength < mPlayedTime)
{ {
PlaylistItem& item = mPlaylist.front(); onFilePlayed();
// Check for timelength continue;
if (item.mTimelength > 0 && item.mTimelength < mPlayedTime) }
{
onFilePlayed();
continue;
}
int wasread = item.mFile->read((char*)buffer + produced, length - produced); int wasread = item.mFile->read((char*)buffer+produced, length-produced);
mPlayedTime += float(wasread) / BYTES_PER_MILLISECOND;
produced += wasread;
if (wasread < length-produced)
{
if (item.mLoop)
{
item.mFile->rewind();
wasread = item.mFile->read((char*)buffer+produced, (length - produced));
mPlayedTime += float(wasread) / BYTES_PER_MILLISECOND; mPlayedTime += float(wasread) / BYTES_PER_MILLISECOND;
produced += wasread; produced += wasread;
if (wasread < length - produced) }
{ else
if (item.mLoop) onFilePlayed();
{
item.mFile->rewind();
wasread = item.mFile->read((char*)buffer + produced, (length - produced));
mPlayedTime += float(wasread) / BYTES_PER_MILLISECOND;
produced += wasread;
}
else
onFilePlayed();
}
} }
}
} }
void Player::onFilePlayed() void Player::onFilePlayed()
{ {
// Save usage id to release later from main loop // Save usage id to release later from main loop
mFinishedUsages.push_back(mPlaylist.front().mUsageId); mFinishedUsages.push_back(mPlaylist.front().mUsageId);
// Send event // Send event
if (mDelegate) if (mDelegate)
mDelegate->onFilePlayed(mPlaylist.front()); mDelegate->onFilePlayed(mPlaylist.front());
// Remove played item & reset played time // Remove played item & reset played time
mPlaylist.pop_front(); mPlaylist.pop_front();
mPlayedTime = 0; mPlayedTime = 0;
} }
void Player::obtain(int usage) void Player::obtain(int usage)
{ {
Lock l(mGuard); Lock l(mGuard);
auto usageIter = mUsage.find(usage); auto usageIter = mUsage.find(usage);
if (usageIter == mUsage.end()) if (usageIter == mUsage.end())
mUsage[usage] = 1; mUsage[usage] = 1;
else else
usageIter->second = usageIter->second + 1; usageIter->second = usageIter->second + 1;
if (mUsage.size() == 1 && mOutput) if (mUsage.size() == 1 && mOutput)
mOutput->open(); mOutput->open();
} }
void Player::release(int usage) void Player::release(int usage)
{ {
Lock l(mGuard); Lock l(mGuard);
UsageMap::iterator usageIter = mUsage.find(usage); UsageMap::iterator usageIter = mUsage.find(usage);
if (usageIter == mUsage.end()) if (usageIter == mUsage.end())
return; return;
usageIter->second = usageIter->second - 1; usageIter->second = usageIter->second - 1;
if (!usageIter->second) if (!usageIter->second)
mUsage.erase(usageIter); mUsage.erase(usageIter);
for (unsigned i = 0; i < mPlaylist.size(); i++) for (unsigned i=0; i<mPlaylist.size(); i++)
if (mPlaylist[i].mUsageId == usage) if (mPlaylist[i].mUsageId == usage)
mPlaylist.erase(mPlaylist.begin() + i); mPlaylist.erase(mPlaylist.begin() + i);
if (mUsage.empty() && mOutput) if (mUsage.empty() && mOutput)
mOutput->close(); mOutput->close();
} }
int Player::releasePlayed() int Player::releasePlayed()
{ {
Lock l(mGuard); Lock l(mGuard);
int result = mFinishedUsages.size(); int result = mFinishedUsages.size();
while (!mFinishedUsages.empty()) while (!mFinishedUsages.empty())
{ {
release(mFinishedUsages.front()); release(mFinishedUsages.front());
mFinishedUsages.erase(mFinishedUsages.begin()); mFinishedUsages.erase(mFinishedUsages.begin());
} }
return result; return result;
} }
void Player::add(int usageId, PWavFileReader file, bool loop, int timelength) void Player::add(int usageId, PWavFileReader file, bool loop, int timelength)
{ {
Lock l(mGuard); Lock l(mGuard);
PlaylistItem item; PlaylistItem item;
item.mFile = file; item.mFile = file;
item.mLoop = loop; item.mLoop = loop;
item.mTimelength = timelength; item.mTimelength = timelength;
item.mUsageId = usageId; item.mUsageId = usageId;
mPlaylist.push_back(item); mPlaylist.push_back(item);
obtain(usageId); obtain(usageId);
} }
void Player::clear() void Player::clear()
{ {
Lock l(mGuard); Lock l(mGuard);
while (mPlaylist.size()) while (mPlaylist.size())
onFilePlayed(); onFilePlayed();
} }
void Player::retrieveUsageIds(std::vector<int>& ids) void Player::retrieveUsageIds(std::vector<int>& ids)
{ {
ids.assign(mFinishedUsages.begin(), mFinishedUsages.end()); ids.assign(mFinishedUsages.begin(), mFinishedUsages.end());
mFinishedUsages.clear(); mFinishedUsages.clear();
} }
+32 -33
View File
@@ -16,57 +16,56 @@
namespace Audio namespace Audio
{ {
class Player : public DataConnection class Player: public DataConnection
{ {
friend class DevicePair; friend class DevicePair;
public:
public:
struct PlaylistItem struct PlaylistItem
{ {
PWavFileReader mFile; PWavFileReader mFile;
bool mLoop; bool mLoop;
int mTimelength; int mTimelength;
int mUsageId; int mUsageId;
}; };
typedef std::deque<PlaylistItem> Playlist; typedef std::deque<PlaylistItem> Playlist;
class EndOfAudioDelegate class EndOfAudioDelegate
{ {
public: public:
virtual void onFilePlayed(PlaylistItem& item) = 0; virtual void onFilePlayed(PlaylistItem& item) = 0;
}; };
protected: protected:
typedef std::map<int, int> UsageMap; typedef std::map<int, int> UsageMap;
Audio::POutputDevice mOutput; Audio::POutputDevice mOutput;
UsageMap mUsage; // References map UsageMap mUsage; // References map
std::vector<int> mFinishedUsages; // Finished plays std::vector<int> mFinishedUsages; // Finished plays
Mutex mGuard; Mutex mGuard;
Playlist mPlaylist; Playlist mPlaylist;
float mPlayedTime; float mPlayedTime;
EndOfAudioDelegate* mDelegate; EndOfAudioDelegate* mDelegate;
void onMicData(const Format& f, const void* buffer, int length); void onMicData(const Format& f, const void* buffer, int length);
void onSpkData(const Format& f, void* buffer, int length); void onSpkData(const Format& f, void* buffer, int length);
void onFilePlayed(); void onFilePlayed();
void obtain(int usageId); void obtain(int usageId);
public: public:
Player(); Player();
~Player(); ~Player();
void setDelegate(EndOfAudioDelegate* d); void setDelegate(EndOfAudioDelegate* d);
EndOfAudioDelegate* getDelegate() const; EndOfAudioDelegate* getDelegate() const;
void setOutput(POutputDevice output); void setOutput(POutputDevice output);
POutputDevice getOutput() const; POutputDevice getOutput() const;
void add(int usageId, PWavFileReader file, bool loop, int timelength); void add(int usageId, PWavFileReader file, bool loop, int timelength);
void release(int usageId); void release(int usageId);
void clear(); void clear();
int releasePlayed(); int releasePlayed();
void retrieveUsageIds(std::vector<int>& ids); void retrieveUsageIds(std::vector<int>& ids);
}; };
} // namespace Audio }
#endif #endif
+128 -127
View File
@@ -3,232 +3,233 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "../engine_config.h" #include "../config.h"
#include "Audio_Quality.h" #include "Audio_Quality.h"
#include "../helper/HL_Exception.h" #include "../helper/HL_Exception.h"
#include "../helper/HL_Types.h" #include "../helper/HL_Types.h"
#include "speex/speex_preprocess.h" #include "speex/speex_preprocess.h"
#ifdef WIN32 #ifdef WIN32
#include <malloc.h> # include <malloc.h>
#endif #endif
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
using namespace Audio; using namespace Audio;
#ifndef SHRT_MAX #define SHRT_MAX 32767 /* maximum (signed) short value */
#define SHRT_MAX 32767 /* maximum (signed) short value */
#endif
AgcFilter::AgcFilter(int channels) AgcFilter::AgcFilter(int channels)
{ {
static const float DefaultLevel = 0.8f; static const float DefaultLevel = 0.8f;
for (int i = 0; i < channels; i++) for (int i=0; i<channels; i++)
{ {
Channel c; Channel c;
float level = DefaultLevel; float level = DefaultLevel;
c.mSampleMax = 1; c.mSampleMax = 1;
c.mCounter = 0; c.mCounter = 0;
c.mIgain = 65536; c.mIgain = 65536;
if (level > 1.0f) if (level > 1.0f)
level = 1.0f; level = 1.0f;
else if (level < 0.5f) else
level = 0.5f; if (level < 0.5f)
level = 0.5f;
c.mIpeak = (int)(SHRT_MAX * level * 65536); c.mIpeak = (int)(SHRT_MAX * level * 65536);
c.mSilenceCounter = 0; c.mSilenceCounter = 0;
mChannelList.push_back(c); mChannelList.push_back(c);
} }
} }
AgcFilter::~AgcFilter() {} AgcFilter::~AgcFilter()
void AgcFilter::process(void* pcm, int length)
{ {
for (size_t i = 0; i < mChannelList.size(); i++) }
processChannel((short*)pcm, length / (sizeof(short) * mChannelList.size()), i);
void AgcFilter::process(void *pcm, int length)
{
for (size_t i=0; i<mChannelList.size(); i++)
processChannel((short*)pcm, length / (sizeof(short) * mChannelList.size()), i);
} }
void AgcFilter::processChannel(short* pcm, int nrOfSamples, int channelIndex) void AgcFilter::processChannel(short* pcm, int nrOfSamples, int channelIndex)
{ {
int i; int i;
for (i = 0; i < nrOfSamples; i++) for(i=0; i<nrOfSamples; i++)
{
long gain_new;
int sample;
int sampleIndex = mChannelList.size() * i + channelIndex;
Channel& channel = mChannelList[channelIndex];
/* get the abs of buffer[i] */
sample = pcm[sampleIndex];
sample = (sample < 0 ? -(sample):sample);
if(sample > (int)channel.mSampleMax)
{ {
long gain_new; /* update the max */
int sample; channel.mSampleMax = (unsigned int)sample;
int sampleIndex = mChannelList.size() * i + channelIndex;
Channel& channel = mChannelList[channelIndex];
/* get the abs of buffer[i] */
sample = pcm[sampleIndex];
sample = (sample < 0 ? -(sample) : sample);
if (sample > (int)channel.mSampleMax)
{
/* update the max */
channel.mSampleMax = (unsigned int)sample;
}
channel.mCounter++;
/* Will we get an overflow with the current gain factor? */
if (((sample * channel.mIgain) >> 16) > channel.mIpeak)
{
/* Yes: Calculate new gain. */
channel.mIgain = ((channel.mIpeak / channel.mSampleMax) * 62259) >> 16;
channel.mSilenceCounter = 0;
pcm[sampleIndex] = (short)((pcm[sampleIndex] * channel.mIgain) >> 16);
continue;
}
/* Calculate new gain factor 10x per second */
if (channel.mCounter >= AUDIO_SAMPLERATE / 10)
{
if (channel.mSampleMax > AUDIO_SAMPLERATE / 10) /* speaking? */
{
gain_new = ((channel.mIpeak / channel.mSampleMax) * 62259) >> 16;
if (channel.mSilenceCounter > 40) /* pause -> speaking */
channel.mIgain += (gain_new - channel.mIgain) >> 2;
else
channel.mIgain += (gain_new - channel.mIgain) / 20;
channel.mSilenceCounter = 0;
}
else /* silence */
{
channel.mSilenceCounter++;
/* silence > 2 seconds: reduce gain */
if ((channel.mIgain > 65536) && (channel.mSilenceCounter >= 20))
channel.mIgain = (channel.mIgain * 62259) >> 16;
}
channel.mCounter = 0;
channel.mSampleMax = 1;
}
pcm[sampleIndex] = (short)((pcm[sampleIndex] * channel.mIgain) >> 16);
} }
channel.mCounter ++;
/* Will we get an overflow with the current gain factor? */
if (((sample * channel.mIgain) >> 16) > channel.mIpeak)
{
/* Yes: Calculate new gain. */
channel.mIgain = ((channel.mIpeak / channel.mSampleMax) * 62259) >> 16;
channel.mSilenceCounter = 0;
pcm[sampleIndex] = (short) ((pcm[sampleIndex] * channel.mIgain) >> 16);
continue;
}
/* Calculate new gain factor 10x per second */
if (channel.mCounter >= AUDIO_SAMPLERATE / 10)
{
if (channel.mSampleMax > AUDIO_SAMPLERATE / 10) /* speaking? */
{
gain_new = ((channel.mIpeak / channel.mSampleMax) * 62259) >> 16;
if (channel.mSilenceCounter > 40) /* pause -> speaking */
channel.mIgain += (gain_new - channel.mIgain) >> 2;
else
channel.mIgain += (gain_new - channel.mIgain) / 20;
channel.mSilenceCounter = 0;
}
else /* silence */
{
channel.mSilenceCounter++;
/* silence > 2 seconds: reduce gain */
if ((channel.mIgain > 65536) && (channel.mSilenceCounter >= 20))
channel.mIgain = (channel.mIgain * 62259) >> 16;
}
channel.mCounter = 0;
channel.mSampleMax = 1;
}
pcm[sampleIndex] = (short) ((pcm[sampleIndex] * channel.mIgain) >> 16);
}
} }
// --- AecFilter --- // --- AecFilter ---
#ifdef USE_SPEEX_AEC #ifdef USE_SPEEX_AEC
#include "speex/speex_echo.h" # include "speex/speex_echo.h"
#include "Audio_Interface.h" #include "Audio_Interface.h"
#if !defined(TARGET_WIN) #if !defined(TARGET_WIN)
#include <alloca.h> # include <alloca.h>
#endif #endif
#endif #endif
#ifdef USE_WEBRTC_AEC #ifdef USE_WEBRTC_AEC
#include "aec/echo_cancellation.h" # include "aec/echo_cancellation.h"
#endif #endif
#ifdef USE_WEBRTC_AEC #ifdef USE_WEBRTC_AEC
static void CheckWRACode(unsigned errorcode) static void CheckWRACode(unsigned errorcode)
{ {
if (errorcode) if (errorcode)
throw Exception(ERR_WEBRTC, errorcode); throw Exception(ERR_WEBRTC, errorcode);
} }
#endif #endif
AecFilter::AecFilter(int tailTime, int frameTime, int rate) : mCtx(nullptr), mFrameTime(frameTime), mRate(rate) AecFilter::AecFilter(int tailTime, int frameTime, int rate)
:mCtx(nullptr), mFrameTime(frameTime), mRate(rate)
{ {
#ifdef USE_SPEEX_AEC #ifdef USE_SPEEX_AEC
if (AUDIO_CHANNELS == 2) if (AUDIO_CHANNELS == 2)
mCtx = speex_echo_state_init_mc(frameTime * (mRate / 1000), tailTime * (mRate / 1000), AUDIO_CHANNELS, mCtx = speex_echo_state_init_mc(frameTime * (mRate / 1000), tailTime * (mRate / 1000), AUDIO_CHANNELS, AUDIO_CHANNELS );
AUDIO_CHANNELS); else
else mCtx = speex_echo_state_init(frameTime * (mRate / 1000), tailTime * (mRate / 1000));
mCtx = speex_echo_state_init(frameTime * (mRate / 1000), tailTime * (mRate / 1000)); int tmp = rate;
int tmp = rate; speex_echo_ctl((SpeexEchoState*)mCtx, SPEEX_ECHO_SET_SAMPLING_RATE, &tmp);
speex_echo_ctl((SpeexEchoState*)mCtx, SPEEX_ECHO_SET_SAMPLING_RATE, &tmp);
#endif #endif
#ifdef USE_WEBRTC_AEC #ifdef USE_WEBRTC_AEC
CheckWRACode(WebRtcAec_Create(&mCtx)); CheckWRACode(WebRtcAec_Create(&mCtx));
CheckWRACode(WebRtcAec_Init(mCtx, rate, rate)); CheckWRACode(WebRtcAec_Init(mCtx, rate, rate));
#endif #endif
} }
AecFilter::~AecFilter() AecFilter::~AecFilter()
{ {
#ifdef USE_SPEEX_AEC #ifdef USE_SPEEX_AEC
if (mCtx) if (mCtx)
{ {
// speex_echo_state_destroy((SpeexEchoState*)mCtx); //speex_echo_state_destroy((SpeexEchoState*)mCtx);
mCtx = nullptr; mCtx = nullptr;
} }
#endif #endif
#ifdef USE_WEBRTC_AEC #ifdef USE_WEBRTC_AEC
CheckWRACode(WebRtcAec_Free(mCtx)); CheckWRACode(WebRtcAec_Free(mCtx));
mCtx = NULL; mCtx = NULL;
#endif #endif
} }
void AecFilter::fromMic(void* data) void AecFilter::fromMic(void *data)
{ {
#ifdef USE_SPEEX_AEC #ifdef USE_SPEEX_AEC
short* output = (short*)alloca(Format().sizeFromTime(AUDIO_MIC_BUFFER_LENGTH)); short* output = (short*)alloca(Format().sizeFromTime(AUDIO_MIC_BUFFER_LENGTH));
speex_echo_capture((SpeexEchoState*)mCtx, (short*)data, (short*)output); speex_echo_capture((SpeexEchoState*)mCtx, (short*)data, (short*)output);
memmove(data, output, AUDIO_MIC_BUFFER_SIZE); memmove(data, output, AUDIO_MIC_BUFFER_SIZE);
#endif #endif
#ifdef USE_WEBRTC_AEC #ifdef USE_WEBRTC_AEC
short* inputframe = (short*)ALLOCA(framesize); short* inputframe = (short*)ALLOCA(framesize);
memcpy(inputframe, (char*)data + framesize * i, framesize); memcpy(inputframe, (char*)data+framesize*i, framesize);
CheckWRACode(WebRtcAec_Process(mCtx, (short*)inputframe, NULL, (short*)data + framesize / 2 * i, NULL, CheckWRACode(WebRtcAec_Process(mCtx, (short*)inputframe, NULL, (short*)data+framesize/2*i, NULL, mFrameTime * mRate / 1000, 0,0));
mFrameTime * mRate / 1000, 0, 0));
#endif #endif
} }
void AecFilter::toSpeaker(void* data) void AecFilter::toSpeaker(void *data)
{ {
#ifdef USE_SPEEX_AEC #ifdef USE_SPEEX_AEC
speex_echo_playback((SpeexEchoState*)mCtx, (short*)data); speex_echo_playback((SpeexEchoState*)mCtx, (short*)data);
#endif #endif
#ifdef USE_WEBRTC_AEC #ifdef USE_WEBRTC_AEC
CheckWRACode(WebRtcAec_BufferFarend(mCtx, (short*)data, length / 2 / AUDIO_CHANNELS)); CheckWRACode(WebRtcAec_BufferFarend(mCtx, (short*)data, length / 2 / AUDIO_CHANNELS));
#endif #endif
} }
int AecFilter::frametime() int AecFilter::frametime()
{ {
return mFrameTime; return mFrameTime;
} }
DenoiseFilter::DenoiseFilter(int rate) : mRate(rate) DenoiseFilter::DenoiseFilter(int rate)
:mRate(rate)
{ {
mCtx = speex_preprocess_state_init(mRate / 100, mRate); mCtx = speex_preprocess_state_init(mRate/100, mRate);
} }
DenoiseFilter::~DenoiseFilter() DenoiseFilter::~DenoiseFilter()
{ {
if (mCtx) if (mCtx)
speex_preprocess_state_destroy((SpeexPreprocessState*)mCtx); speex_preprocess_state_destroy((SpeexPreprocessState*)mCtx);
} }
void DenoiseFilter::fromMic(void* data, int timelength) void DenoiseFilter::fromMic(void* data, int timelength)
{ {
assert(timelength % 10 == 0); assert(timelength % 10 == 0);
// Process by 10-ms blocks // Process by 10-ms blocks
spx_int16_t* in = (spx_int16_t*)data; spx_int16_t* in = (spx_int16_t*)data;
for (int blockIndex = 0; blockIndex < timelength / 10; blockIndex++) for (int blockIndex=0; blockIndex<timelength/10; blockIndex++)
{ {
spx_int16_t* block = in + blockIndex * (mRate / 100) * AUDIO_CHANNELS; spx_int16_t* block = in + blockIndex * (mRate / 100) * AUDIO_CHANNELS;
speex_preprocess_run((SpeexPreprocessState*)mCtx, block); speex_preprocess_run((SpeexPreprocessState*)mCtx, block);
} }
} }
int DenoiseFilter::rate() int DenoiseFilter::rate()
{ {
return mRate; return mRate;
} }
+33 -34
View File
@@ -5,66 +5,65 @@
#ifndef __AUDIO_QUALITY_H #ifndef __AUDIO_QUALITY_H
#define __AUDIO_QUALITY_H #define __AUDIO_QUALITY_H
#include "../engine_config.h" #include "../config.h"
#include "../helper/HL_Sync.h" #include "../helper/HL_Sync.h"
#include <vector> #include <vector>
namespace Audio namespace Audio
{ {
class AgcFilter class AgcFilter
{ {
protected: protected:
struct Channel struct Channel
{ {
unsigned int mSampleMax; unsigned int mSampleMax;
int mCounter; int mCounter;
long mIgain; long mIgain;
int mIpeak; int mIpeak;
int mSilenceCounter; int mSilenceCounter;
}; };
std::vector<Channel> mChannelList; std::vector<Channel> mChannelList;
void processChannel(short* pcm, int nrOfSamples, int channelIndex); void processChannel(short* pcm, int nrOfSamples, int channelIndex);
public:
public:
AgcFilter(int channels); AgcFilter(int channels);
~AgcFilter(); ~AgcFilter();
void process(void* pcm, int length); void process(void* pcm, int length);
}; };
class AecFilter class AecFilter
{ {
public: public:
AecFilter(int tailTime, int frameTime, int rate); AecFilter(int tailTime, int frameTime, int rate);
~AecFilter(); ~AecFilter();
// These methods accept input block with timelength "frameTime" used in constructor. // These methods accept input block with timelength "frameTime" used in constructor.
void toSpeaker(void* data); void toSpeaker(void* data);
void fromMic(void* data); void fromMic(void* data);
int frametime(); int frametime();
protected: protected:
void* mCtx; /// The echo canceller context's pointer. void* mCtx; /// The echo canceller context's pointer.
Mutex mGuard; /// Mutex to protect this instance. Mutex mGuard; /// Mutex to protect this instance.
int mFrameTime; /// Duration of single audio frame (in milliseconds) int mFrameTime; /// Duration of single audio frame (in milliseconds)
int mRate; int mRate;
}; };
class DenoiseFilter class DenoiseFilter
{ {
public: public:
DenoiseFilter(int rate); DenoiseFilter(int rate);
~DenoiseFilter(); ~DenoiseFilter();
void fromMic(void* data, int timelength); void fromMic(void* data, int timelength);
int rate(); int rate();
protected: protected:
Mutex mGuard; /// Mutex to protect this instance. Mutex mGuard; /// Mutex to protect this instance.
void* mCtx; /// The denoiser context pointer. void* mCtx; /// The denoiser context pointer.
int mRate; /// Duration of single audio frame (in milliseconds) int mRate; /// Duration of single audio frame (in milliseconds)
}; };
} // namespace Audio }
#endif #endif
+58 -54
View File
@@ -3,7 +3,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "../engine_config.h" #include "../config.h"
#include "Audio_Resampler.h" #include "Audio_Resampler.h"
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
@@ -17,7 +17,10 @@ namespace Audio
{ {
SpeexResampler::SpeexResampler() {} SpeexResampler::SpeexResampler()
:mContext(NULL), mErrorCode(0), mSourceRate(0), mDestRate(0), mLastSample(0)
{
}
void SpeexResampler::start(int channels, int sourceRate, int destRate) void SpeexResampler::start(int channels, int sourceRate, int destRate)
{ {
@@ -34,8 +37,8 @@ void SpeexResampler::start(int channels, int sourceRate, int destRate)
if (sourceRate != destRate) if (sourceRate != destRate)
{ {
// Defer context creation until first request // Defer context creation until first request
// mContext = speex_resampler_init(channels, sourceRate, destRate, AUDIO_RESAMPLER_QUALITY, &mErrorCode); //mContext = speex_resampler_init(channels, sourceRate, destRate, AUDIO_RESAMPLER_QUALITY, &mErrorCode);
// assert(mContext != NULL); //assert(mContext != NULL);
} }
} }
@@ -48,35 +51,28 @@ void SpeexResampler::stop()
} }
} }
bool SpeexResampler::isOpened() const
{
return mContext != nullptr;
}
SpeexResampler::~SpeexResampler() SpeexResampler::~SpeexResampler()
{ {
stop(); stop();
} }
size_t SpeexResampler::processBuffer(const void* src, size_t sourceLength, size_t& sourceProcessed, void* dest, size_t SpeexResampler::processBuffer(const void* src, size_t sourceLength, size_t& sourceProcessed,
size_t destCapacity) void* dest, size_t destCapacity)
{ {
assert(mSourceRate != 0 && mDestRate != 0); assert(mSourceRate != 0 && mDestRate != 0);
if (mDestRate == mSourceRate) if (mDestRate == mSourceRate)
{ {
// Pass-through, but never write past the caller's buffer: clamp to its
// capacity instead of trusting sourceLength (which is caller/file driven).
assert(destCapacity >= sourceLength); assert(destCapacity >= sourceLength);
size_t copied = std::min(sourceLength, destCapacity); memcpy(dest, src, sourceLength);
memcpy(dest, src, copied); sourceProcessed = sourceLength;
sourceProcessed = copied; return sourceLength;
return copied;
} }
if (!mContext) if (!mContext)
{ {
mContext = speex_resampler_init(mChannels, mSourceRate, mDestRate, AUDIO_RESAMPLER_QUALITY, &mErrorCode); mContext = speex_resampler_init(mChannels, mSourceRate, mDestRate,
AUDIO_RESAMPLER_QUALITY, &mErrorCode);
if (!mContext) if (!mContext)
return 0; return 0;
} }
@@ -101,9 +97,11 @@ size_t SpeexResampler::processBuffer(const void* src, size_t sourceLength, size_
unsigned inLen = sourceLength / (sizeof(short) * mChannels); unsigned inLen = sourceLength / (sizeof(short) * mChannels);
outLen /= sizeof(short) * mChannels; outLen /= sizeof(short) * mChannels;
assert(mContext != NULL); assert(mContext != NULL);
spx_uint32_t in_len = static_cast<spx_uint32_t>(inLen), out_len = static_cast<spx_uint32_t>(outLen); spx_uint32_t in_len = static_cast<spx_uint32_t>(inLen),
out_len = static_cast<spx_uint32_t>(outLen);
int speexCode = speex_resampler_process_interleaved_int((SpeexResamplerState*)mContext, (spx_int16_t*)src, &in_len, int speexCode = speex_resampler_process_interleaved_int((SpeexResamplerState *)mContext,
(spx_int16_t*)src, &in_len,
(spx_int16_t*)dest, &out_len); (spx_int16_t*)dest, &out_len);
assert(speexCode == RESAMPLER_ERR_SUCCESS); assert(speexCode == RESAMPLER_ERR_SUCCESS);
@@ -115,25 +113,24 @@ size_t SpeexResampler::processBuffer(const void* src, size_t sourceLength, size_
return outLen * sizeof(short) * mChannels; return outLen * sizeof(short) * mChannels;
} }
int SpeexResampler::sourceRate() const int SpeexResampler::sourceRate()
{ {
return mSourceRate; return mSourceRate;
} }
int SpeexResampler::destRate() const int SpeexResampler::destRate()
{ {
return mDestRate; return mDestRate;
} }
size_t SpeexResampler::getDestLength(size_t sourceLen) const size_t SpeexResampler::getDestLength(size_t sourceLen)
{ {
return size_t(sourceLen * (float(mDestRate) / mSourceRate) + 0.5f); return size_t(sourceLen * (float(mDestRate) / mSourceRate) + 0.5f) / 2 * 2;
} }
size_t SpeexResampler::getSourceLength(size_t destLen) const size_t SpeexResampler::getSourceLength(size_t destLen)
{ {
// Here we want to get 'destLen' number of samples return size_t(destLen * (float(mSourceRate) / mDestRate) + 0.5f) / 2 * 2;
return size_t(destLen * (float(mSourceRate) / mDestRate) + 0.5f);
} }
// Returns instance + speex resampler size in bytes // Returns instance + speex resampler size in bytes
@@ -143,27 +140,27 @@ size_t SpeexResampler::getSize() const
} }
// -------------------------- ChannelConverter -------------------- // -------------------------- ChannelConverter --------------------
int ChannelConverter::stereoToMono(const void* source, int sourceLength, void* dest, int destLength) int ChannelConverter::stereoToMono(const void *source, int sourceLength, void *dest, int destLength)
{ {
assert(destLength == sourceLength / 2); assert(destLength == sourceLength / 2);
const short* input = (const short*)source; const short* input = (const short*)source;
short* output = (short*)dest; short* output = (short*)dest;
for (int sampleIndex = 0; sampleIndex < destLength / 2; sampleIndex++) for (int sampleIndex = 0; sampleIndex < destLength/2; sampleIndex++)
{ {
output[sampleIndex] = (input[sampleIndex * 2] + input[sampleIndex * 2 + 1]) >> 1; output[sampleIndex] = (input[sampleIndex*2] + input[sampleIndex*2+1]) >> 1;
} }
return sourceLength / 2; return sourceLength / 2;
} }
int ChannelConverter::monoToStereo(const void* source, int sourceLength, void* dest, int destLength) int ChannelConverter::monoToStereo(const void *source, int sourceLength, void *dest, int destLength)
{ {
assert(destLength == sourceLength * 2); assert (destLength == sourceLength * 2);
const short* input = (const short*)source; const short* input = (const short*)source;
short* output = (short*)dest; short* output = (short*)dest;
// Convert starting from the end of buffer to allow inplace conversion // Convert starting from the end of buffer to allow inplace conversion
for (int sampleIndex = sourceLength / 2 - 1; sampleIndex >= 0; sampleIndex--) for (int sampleIndex = sourceLength/2 - 1; sampleIndex >= 0; sampleIndex--)
{ {
output[2 * sampleIndex] = output[2 * sampleIndex + 1] = input[sampleIndex]; output[2*sampleIndex] = output[2*sampleIndex+1] = input[sampleIndex];
} }
return sourceLength * 2; return sourceLength * 2;
} }
@@ -180,14 +177,13 @@ Resampler48kTo16k::~Resampler48kTo16k()
WebRtcSpl_ResetResample48khzTo16khz(&mContext); WebRtcSpl_ResetResample48khzTo16khz(&mContext);
} }
int Resampler48kTo16k::process(const void* source, int sourceLen, void* dest, int destLen) int Resampler48kTo16k::process(const void *source, int sourceLen, void *dest, int destLen)
{ {
const short* input = (const short*)source; const short* input = (const short*)source; int inputLen = sourceLen / 2;
int inputLen = sourceLen / 2; short* output = (short*)dest; //int outputCapacity = destLen / 2;
short* output = (short*)dest; // int outputCapacity = destLen / 2;
assert(inputLen % 480 == 0); assert(inputLen % 480 == 0);
int frames = inputLen / 480; int frames = inputLen / 480;
for (int i = 0; i < frames; i++) for (int i=0; i<frames; i++)
WebRtcSpl_Resample48khzTo16khz(input + i * 480, output + i * 160, &mContext, mTemp); WebRtcSpl_Resample48khzTo16khz(input + i * 480, output + i * 160, &mContext, mTemp);
return sourceLen / 3; return sourceLen / 3;
@@ -204,14 +200,13 @@ Resampler16kto48k::~Resampler16kto48k()
WebRtcSpl_ResetResample16khzTo48khz(&mContext); WebRtcSpl_ResetResample16khzTo48khz(&mContext);
} }
int Resampler16kto48k::process(const void* source, int sourceLen, void* dest, int destLen) int Resampler16kto48k::process(const void *source, int sourceLen, void *dest, int destLen)
{ {
const WebRtc_Word16* input = (const WebRtc_Word16*)source; const WebRtc_Word16* input = (const WebRtc_Word16*)source; int inputLen = sourceLen / 2;
int inputLen = sourceLen / 2; WebRtc_Word16* output = (WebRtc_Word16*)dest; //int outputCapacity = destLen / 2;
WebRtc_Word16* output = (WebRtc_Word16*)dest; // int outputCapacity = destLen / 2;
assert(inputLen % 160 == 0); assert(inputLen % 160 == 0);
int frames = inputLen / 160; int frames = inputLen / 160;
for (int i = 0; i < frames; i++) for (int i=0; i<frames; i++)
WebRtcSpl_Resample16khzTo48khz(input + i * 160, output + i * 480, &mContext, mTemp); WebRtcSpl_Resample16khzTo48khz(input + i * 160, output + i * 480, &mContext, mTemp);
return sourceLen * 3; return sourceLen * 3;
@@ -220,12 +215,18 @@ int Resampler16kto48k::process(const void* source, int sourceLen, void* dest, in
#endif #endif
// ---------------- UniversalResampler ------------------- // ---------------- UniversalResampler -------------------
UniversalResampler::UniversalResampler() {} UniversalResampler::UniversalResampler()
{
UniversalResampler::~UniversalResampler() {} }
size_t UniversalResampler::resample(int sourceRate, const void* sourceBuffer, size_t sourceLength, UniversalResampler::~UniversalResampler()
size_t& sourceProcessed, int destRate, void* destBuffer, size_t destCapacity) {
}
size_t UniversalResampler::resample(int sourceRate, const void *sourceBuffer, size_t sourceLength,
size_t& sourceProcessed, int destRate, void *destBuffer, size_t destCapacity)
{ {
assert(destBuffer && sourceBuffer); assert(destBuffer && sourceBuffer);
size_t result; size_t result;
@@ -244,7 +245,10 @@ size_t UniversalResampler::resample(int sourceRate, const void* sourceBuffer, si
return result; return result;
} }
void UniversalResampler::preload() {} void UniversalResampler::preload()
{
}
size_t UniversalResampler::getDestLength(int sourceRate, int destRate, size_t sourceLength) size_t UniversalResampler::getDestLength(int sourceRate, int destRate, size_t sourceLength)
{ {
@@ -266,10 +270,10 @@ PResampler UniversalResampler::findResampler(int sourceRate, int destRate)
{ {
assert(sourceRate != destRate); assert(sourceRate != destRate);
ResamplerMap::iterator resamplerIter = mResamplerMap.find(RatePair(sourceRate, destRate)); ResamplerMap::iterator resamplerIter = mResamplerMap.find(RatePair(sourceRate, destRate));
PResampler r; PResampler r;
if (resamplerIter == mResamplerMap.end()) if (resamplerIter == mResamplerMap.end())
{ {
r = std::make_shared<Resampler>(); r = PResampler(new Resampler());
r->start(AUDIO_CHANNELS, sourceRate, destRate); r->start(AUDIO_CHANNELS, sourceRate, destRate);
mResamplerMap[RatePair(sourceRate, destRate)] = r; mResamplerMap[RatePair(sourceRate, destRate)] = r;
} }
@@ -278,4 +282,4 @@ PResampler UniversalResampler::findResampler(int sourceRate, int destRate)
return r; return r;
} }
} // namespace Audio } // end of namespace
+73 -74
View File
@@ -7,7 +7,7 @@
#define __AUDIO_RESAMPLER_H #define __AUDIO_RESAMPLER_H
#ifdef USE_WEBRTC_RESAMPLER #ifdef USE_WEBRTC_RESAMPLER
#include "signal_processing_library/signal_processing_library.h" # include "signal_processing_library/signal_processing_library.h"
#endif #endif
#include <vector> #include <vector>
@@ -16,90 +16,89 @@
namespace Audio namespace Audio
{ {
class SpeexResampler class SpeexResampler
{ {
public: public:
SpeexResampler(); SpeexResampler();
~SpeexResampler(); ~SpeexResampler();
void start(int channels, int sourceRate, int destRate); void start(int channels, int sourceRate, int destRate);
void stop(); void stop();
bool isOpened() const; size_t processBuffer(const void* source, size_t sourceLength, size_t& sourceProcessed,
void* dest, size_t destCapacity);
int sourceRate();
int destRate();
size_t getDestLength(size_t sourceLen);
size_t getSourceLength(size_t destLen);
size_t processBuffer(const void* source, size_t sourceLength, size_t& sourceProcessed, void* dest, // Returns instance + speex encoder size in bytes
size_t destCapacity); size_t getSize() const;
int sourceRate() const;
int destRate() const;
size_t getDestLength(size_t sourceLen) const;
size_t getSourceLength(size_t destLen) const;
// Returns instance + speex encoder size in bytes protected:
size_t getSize() const; void* mContext;
int mErrorCode;
int mSourceRate,
mDestRate,
mChannels;
short mLastSample;
};
protected: typedef SpeexResampler Resampler;
void* mContext = nullptr; typedef std::shared_ptr<Resampler> PResampler;
int mErrorCode = 0;
int mSourceRate = 0, mDestRate = 0, mChannels = 0;
short mLastSample = 0;
};
typedef SpeexResampler Resampler; class ChannelConverter
typedef std::shared_ptr<Resampler> PResampler; {
public:
static int stereoToMono(const void* source, int sourceLength, void* dest, int destLength);
static int monoToStereo(const void* source, int sourceLength, void* dest, int destLength);
};
class ChannelConverter // Operates with AUDIO_CHANNELS number of channels
{ class UniversalResampler
public: {
static int stereoToMono(const void* source, int sourceLength, void* dest, int destLength); public:
static int monoToStereo(const void* source, int sourceLength, void* dest, int destLength); UniversalResampler();
}; ~UniversalResampler();
// Operates with AUDIO_CHANNELS number of channels size_t resample(int sourceRate, const void* sourceBuffer, size_t sourceLength, size_t& sourceProcessed,
class UniversalResampler int destRate, void* destBuffer, size_t destCapacity);
{ size_t getDestLength(int sourceRate, int destRate, size_t sourceLength);
public: size_t getSourceLength(int sourceRate, int destRate, size_t destLength);
UniversalResampler();
~UniversalResampler();
size_t resample(int sourceRate, const void* sourceBuffer, size_t sourceLength, size_t& sourceProcessed, protected:
int destRate, void* destBuffer, size_t destCapacity); typedef std::pair<int, int> RatePair;
size_t getDestLength(int sourceRate, int destRate, size_t sourceLength); typedef std::map<RatePair, PResampler> ResamplerMap;
size_t getSourceLength(int sourceRate, int destRate, size_t destLength); ResamplerMap mResamplerMap;
PResampler findResampler(int sourceRate, int destRate);
protected: void preload();
typedef std::pair<int, int> RatePair; };
typedef std::map<RatePair, PResampler> ResamplerMap;
ResamplerMap mResamplerMap;
PResampler findResampler(int sourceRate, int destRate);
void preload(); #ifdef USE_WEBRTC_RESAMPLER
}; // n*10 milliseconds buffers required!
class Resampler48kTo16k
{
public:
Resampler48kTo16k();
~Resampler48kTo16k();
int process(const void* source, int sourceLen, void* dest, int destLen);
protected:
WebRtc_Word32 mTemp[496];
WebRtcSpl_State48khzTo16khz mContext;
};
#ifdef USE_WEBRTC_RESAMPLER class Resampler16kto48k
// n*10 milliseconds buffers required! {
class Resampler48kTo16k public:
{ Resampler16kto48k();
public: ~Resampler16kto48k();
Resampler48kTo16k(); int process(const void* source, int sourceLen, void* dest, int destLen);
~Resampler48kTo16k();
int process(const void* source, int sourceLen, void* dest, int destLen);
protected: protected:
WebRtc_Word32 mTemp[496]; WebRtc_Word32 mTemp[336];
WebRtcSpl_State48khzTo16khz mContext; WebRtcSpl_State16khzTo48khz mContext;
}; };
#endif
class Resampler16kto48k } // end of namespace
{
public:
Resampler16kto48k();
~Resampler16kto48k();
int process(const void* source, int sourceLen, void* dest, int destLen);
protected:
WebRtc_Word32 mTemp[336];
WebRtcSpl_State16khzTo48khz mContext;
};
#endif
} // namespace Audio
#endif #endif
+259 -297
View File
@@ -7,426 +7,388 @@
#include "helper/HL_Exception.h" #include "helper/HL_Exception.h"
#include "helper/HL_String.h" #include "helper/HL_String.h"
#include "helper/HL_Log.h" #include "helper/HL_Log.h"
#include "../engine_config.h" #include "../config.h"
#include <memory.h> #include <memory.h>
#include <assert.h>
#ifndef WORD #ifndef WORD
#define WORD unsigned short # define WORD unsigned short
#endif #endif
#ifndef DWORD #ifndef DWORD
#define DWORD unsigned int # define DWORD unsigned int
#endif #endif
typedef struct typedef struct {
{ WORD wFormatTag;
WORD wFormatTag; WORD nChannels;
WORD nChannels; DWORD nSamplesPerSec;
DWORD nSamplesPerSec; DWORD nAvgBytesPerSec;
DWORD nAvgBytesPerSec; WORD nBlockAlign;
WORD nBlockAlign; WORD wBitsPerSample;
WORD wBitsPerSample; WORD cbSize;
WORD cbSize; }
} WaveFormatEx; WaveFormatEx;
#define WAVE_FORMAT_PCM 1 #define WAVE_FORMAT_PCM 1
#define LOG_SUBSYSTEM "audio" #define LOG_SUBSYSTEM "WavFileReader"
#define LOCK std::unique_lock<std::recursive_mutex> lock(mFileMtx); #define LOCK std::unique_lock<std::recursive_mutex> lock(mFileMtx);
using namespace Audio; using namespace Audio;
// ---------------------- WavFileReader ------------------------- // ---------------------- WavFileReader -------------------------
WavFileReader::WavFileReader() : mSamplerate(0), mLastError(0), mChannels(0), mBits(0), mDataLength(0) WavFileReader::WavFileReader()
:mHandle(nullptr), mRate(0), mLastError(0)
{ {
mDataOffset = 0; mDataOffset = 0;
} }
WavFileReader::~WavFileReader() {} WavFileReader::~WavFileReader()
{
}
#define THROW_READERROR throw Exception(ERR_WAVFILE_FAILED); #define THROW_READERROR throw Exception(ERR_WAVFILE_FAILED);
std::string WavFileReader::readChunk() std::string WavFileReader::readChunk()
{ {
char name[5] = {0}; char name[5];
readBuffer(name, 4); if (fread(name, 1, 4, mHandle) != 4)
THROW_READERROR;
std::string result = name; name[4] = 0;
uint32_t size = 0; std::string result = name;
readBuffer(&size, 4); unsigned size;
if (fread(&size, 4, 1, mHandle) != 1)
THROW_READERROR;
if (result == "data") if (result == "fact")
mDataLength = size; fread(&mDataLength, 4, 1, mHandle);
else else
// Skip the chunk body; RIFF chunks are word-aligned, so odd sizes carry a pad byte if (result != "data")
mInput->seekg(std::streamoff(size + (size & 1)), std::ios_base::cur); fseek(mHandle, size, SEEK_CUR);
else
mDataLength = size;
return result; return result;
} }
void WavFileReader::readBuffer(void* buffer, size_t sz) bool WavFileReader::open(const std::tstring& filename)
{ {
auto p = mInput->tellg(); LOCK;
mInput->read(reinterpret_cast<char*>(buffer), sz); try
if (mInput->tellg() - p != sz) {
throw Exception(ERR_WAVFILE_FAILED); #ifdef WIN32
} mHandle = _wfopen(filename.c_str(), L"rb");
#else
size_t WavFileReader::tryReadBuffer(void* buffer, size_t sz) mHandle = fopen(StringHelper::makeUtf8(filename).c_str(), "rb");
{ #endif
auto p = mInput->tellg(); if (NULL == mHandle)
mInput->read(reinterpret_cast<char*>(buffer), sz);
return mInput->tellg() - p;
}
bool WavFileReader::open(const std::filesystem::path& p)
{
LOCK;
try
{ {
mPath = p;
mInput = std::make_unique<std::ifstream>(p, std::ios::binary | std::ios::in);
if (!mInput->is_open())
{
#if defined(TARGET_ANDROID) || defined(TARGET_LINUX) || defined(TARGET_OSX) #if defined(TARGET_ANDROID) || defined(TARGET_LINUX) || defined(TARGET_OSX)
mLastError = errno; mLastError = errno;
#endif #endif
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
mLastError = GetLastError(); mLastError = GetLastError();
#endif #endif
return false; return false;
}
mLastError = 0;
// Read the .WAV header
char riff[4];
readBuffer(riff, sizeof riff);
if (!(riff[0] == 'R' && riff[1] == 'I' && riff[2] == 'F' && riff[3] == 'F'))
THROW_READERROR;
// Read the file size
uint32_t filesize = 0;
readBuffer(&filesize, sizeof(filesize));
char wavefmt[9] = {0};
readBuffer(wavefmt, 8);
if (strcmp(wavefmt, "WAVEfmt ") != 0)
THROW_READERROR;
uint32_t fmtSize = 0;
readBuffer(&fmtSize, sizeof(fmtSize));
auto fmtStart = mInput->tellg();
uint16_t formattag = 0;
readBuffer(&formattag, sizeof(formattag));
if (formattag != 1 /*WAVE_FORMAT_PCM*/)
THROW_READERROR;
mChannels = 0;
readBuffer(&mChannels, sizeof(mChannels));
mSamplerate = 0;
readBuffer(&mSamplerate, sizeof(mSamplerate));
uint32_t avgbytespersec = 0;
readBuffer(&avgbytespersec, sizeof(avgbytespersec));
uint16_t blockalign = 0;
readBuffer(&blockalign, sizeof(blockalign));
mBits = 0;
readBuffer(&mBits, sizeof(mBits));
// Only 16-bit PCM is supported: the read path feeds the data
// directly into a 16-bit resampler.
if (mBits != 16)
THROW_READERROR;
// The read path is mono-only: it divides byte counts by AUDIO_CHANNELS
// and starts the resampler with AUDIO_CHANNELS. Reject anything else -
// a multi-channel file would make read() copy more source bytes than the
// caller's (mono-sized) output buffer can hold. Also reject a zero rate,
// which would divide by zero in readRaw() / poison the resampler ratio.
if (mChannels != AUDIO_CHANNELS || mSamplerate == 0)
THROW_READERROR;
// Look for the chunk 'data'
mInput->seekg(fmtStart + std::streampos(fmtSize));
mDataLength = 0;
while (readChunk() != "data")
;
mDataOffset = mInput->tellg();
mResampler.start(AUDIO_CHANNELS, mSamplerate, AUDIO_SAMPLERATE);
} }
catch (...) mLastError = 0;
{
mInput.reset(); // Read the .WAV header
mLastError = static_cast<unsigned>(-1); char riff[4];
} if (fread(riff, 4, 1, mHandle) < 1)
return isOpened(); THROW_READERROR;
if (!(riff[0] == 'R' && riff[1] == 'I' && riff[2] == 'F' && riff[3] == 'F'))
THROW_READERROR;
// Read the file size
unsigned int filesize = 0;
if (fread(&filesize, 4, 1, mHandle) < 1)
THROW_READERROR;
char wavefmt[9];
if (fread(wavefmt, 8, 1, mHandle) < 1)
THROW_READERROR;
wavefmt[8] = 0;
if (strcmp(wavefmt, "WAVEfmt ") != 0)
THROW_READERROR;
unsigned fmtSize = 0;
if (fread(&fmtSize, 4, 1, mHandle) < 1)
THROW_READERROR;
unsigned fmtStart = ftell(mHandle);
unsigned short formattag = 0;
if (fread(&formattag, 2, 1, mHandle) < 1)
THROW_READERROR;
if (formattag != 1/*WAVE_FORMAT_PCM*/)
THROW_READERROR;
mChannels = 0;
if (fread(&mChannels, 2, 1, mHandle) < 1)
THROW_READERROR;
mRate = 0;
if (fread(&mRate, 4, 1, mHandle) < 1)
THROW_READERROR;
unsigned int avgbytespersec = 0;
if (fread(&avgbytespersec, 4, 1, mHandle) < 1)
THROW_READERROR;
unsigned short blockalign = 0;
if (fread(&blockalign, 2, 1, mHandle) < 1)
THROW_READERROR;
mBits = 0;
if (fread(&mBits, 2, 1, mHandle) < 1)
THROW_READERROR;
if (mBits !=8 && mBits != 16)
THROW_READERROR;
// Read the "chunk"
fseek(mHandle, fmtStart + fmtSize, SEEK_SET);
//unsigned pos = ftell(mHandle);
mDataLength = 0;
while (readChunk() != "data")
;
mFileName = filename;
mDataOffset = ftell(mHandle);
mResampler.start(AUDIO_CHANNELS, mRate, AUDIO_SAMPLERATE);
}
catch(...)
{
fclose(mHandle); mHandle = nullptr;
mLastError = static_cast<unsigned>(-1);
}
return isOpened();
} }
void WavFileReader::close() void WavFileReader::close()
{ {
LOCK; LOCK;
mInput.reset();
if (nullptr != mHandle)
fclose(mHandle);
mHandle = nullptr;
} }
int WavFileReader::samplerate() const int WavFileReader::rate() const
{ {
return mSamplerate; return mRate;
} }
int WavFileReader::channels() const unsigned WavFileReader::read(void* buffer, unsigned bytes)
{ {
return mChannels; return read((short*)buffer, bytes / (AUDIO_CHANNELS * 2)) * AUDIO_CHANNELS * 2;
} }
size_t WavFileReader::read(void* buffer, size_t bytes) unsigned WavFileReader::read(short* buffer, unsigned samples)
{ {
return read((short*)buffer, bytes / (AUDIO_CHANNELS * 2)) * AUDIO_CHANNELS * 2; LOCK;
}
size_t WavFileReader::readRaw(void* buffer, size_t bytes) if (!mHandle)
{ return 0;
return readRaw((short*)buffer, bytes / channels() / sizeof(short)) * channels() * sizeof(short);
}
size_t WavFileReader::read(short* buffer, size_t samples) // Get number of samples that must be read from source file
{ int requiredBytes = mResampler.getSourceLength(samples) * mChannels * mBits / 8;
LOCK; void* temp = alloca(requiredBytes);
memset(temp, 0, requiredBytes);
if (!mInput) // Find required size of input buffer
return 0; if (mDataLength)
{
unsigned filePosition = ftell(mHandle);
// Get number of samples that must be read from source file // Check how much data we can read
size_t requiredBytes = mResampler.getSourceLength(samples) * mChannels * mBits / 8; unsigned fileAvailable = mDataLength + mDataOffset - filePosition;
bool useHeap = requiredBytes > sizeof mTempBuffer; requiredBytes = (int)fileAvailable < requiredBytes ? (int)fileAvailable : requiredBytes;
void* temp; }
if (useHeap)
temp = malloc(requiredBytes);
else
temp = mTempBuffer;
memset(temp, 0, requiredBytes); /*int readSamples = */fread(temp, 1, requiredBytes, mHandle);// / mChannels / (mBits / 8);
size_t processedBytes = 0;
size_t result = mResampler.processBuffer(temp, requiredBytes, processedBytes,
buffer, samples * 2 * AUDIO_CHANNELS);
// Find required size of input buffer return result / 2 / AUDIO_CHANNELS;
if (mDataLength)
{
auto filePosition = mInput->tellg();
// Check how much data we can read
std::streamoff dataEnd = std::streamoff(mDataLength) + mDataOffset;
size_t fileAvailable = filePosition < dataEnd ? size_t(dataEnd - filePosition) : 0;
requiredBytes = fileAvailable < requiredBytes ? fileAvailable : requiredBytes;
}
size_t readBytes = tryReadBuffer(temp, requiredBytes);
size_t processedBytes = 0;
size_t result = mResampler.processBuffer(temp, readBytes, processedBytes, buffer, samples * 2 * AUDIO_CHANNELS);
if (useHeap)
free(temp);
return result / 2 / AUDIO_CHANNELS;
}
size_t WavFileReader::readRaw(short* buffer, size_t samples)
{
LOCK;
if (!mInput)
return 0;
// Get number of samples that must be read from source file
size_t requiredBytes = samples * channels() * sizeof(short);
// Find required size of input buffer
if (mDataLength)
{
auto filePosition = mInput->tellg();
// Check how much data we can read
std::streamoff dataEnd = std::streamoff(mDataLength) + mDataOffset;
size_t fileAvailable = filePosition < dataEnd ? size_t(dataEnd - filePosition) : 0;
requiredBytes = fileAvailable < requiredBytes ? fileAvailable : requiredBytes;
}
size_t readBytes = tryReadBuffer(buffer, requiredBytes);
return readBytes / channels() / sizeof(short);
} }
bool WavFileReader::isOpened() bool WavFileReader::isOpened()
{ {
LOCK; LOCK;
if (!mInput)
return false; return (mHandle != 0);
return mInput->is_open();
} }
void WavFileReader::rewind() void WavFileReader::rewind()
{ {
LOCK; LOCK;
if (mInput)
mInput->seekg(mDataOffset); if (mHandle)
fseek(mHandle, mDataOffset, SEEK_SET);
} }
std::filesystem::path WavFileReader::path() const std::tstring WavFileReader::filename() const
{ {
LOCK; LOCK;
return mPath;
return mFileName;
} }
size_t WavFileReader::size() const unsigned WavFileReader::size() const
{ {
LOCK; LOCK;
return mDataLength;
return mDataLength;
} }
unsigned WavFileReader::lastError() const unsigned WavFileReader::lastError() const
{ {
return mLastError; return mLastError;
} }
// ------------------------- WavFileWriter ------------------------- // ------------------------- WavFileWriter -------------------------
#define LOG_SUBSYTEM "WavFileWriter" #define LOG_SUBSYTEM "WavFileWriter"
#define BITS_PER_CHANNEL 16 #define BITS_PER_CHANNEL 16
WavFileWriter::WavFileWriter() : mLengthOffset(0), mSamplerate(AUDIO_SAMPLERATE), mChannels(1), mWritten(0) {} WavFileWriter::WavFileWriter()
:mHandle(nullptr), mLengthOffset(0), mRate(AUDIO_SAMPLERATE), mChannels(1), mWritten(0)
{
}
WavFileWriter::~WavFileWriter() WavFileWriter::~WavFileWriter()
{ {
close(); close();
} }
void WavFileWriter::checkWriteResult(int result) void WavFileWriter::checkWriteResult(int result)
{ {
if (result < 1) if (result < 1)
throw Exception(ERR_WAVFILE_FAILED, errno); throw Exception(ERR_WAVFILE_FAILED, errno);
} }
void WavFileWriter::writeBuffer(const void* buffer, size_t sz) bool WavFileWriter::open(const std::tstring& filename, int rate, int channels)
{ {
if (!mOutput) LOCK;
return;
auto p = mOutput->tellp(); close();
mOutput->write(reinterpret_cast<const char*>(buffer), sz);
if (mOutput->tellp() - p != sz)
throw Exception(ERR_WAVFILE_FAILED);
}
bool WavFileWriter::open(const std::filesystem::path& p, int samplerate, int channels) mRate = rate;
{ mChannels = channels;
LOCK;
close();
mSamplerate = samplerate;
mChannels = channels;
mOutput = std::make_unique<std::ofstream>(p, std::ios::binary | std::ios::trunc); #ifdef WIN32
if (!mOutput->is_open()) mHandle = _wfopen(filename.c_str(), L"wb");
{ #else
int errorcode = errno; mHandle = fopen(StringHelper::makeUtf8(filename).c_str(), "wb");
ICELogError(<< "Failed to create .wav file: filename = " << p << " , error = " << errorcode); #endif
mOutput.reset(); if (nullptr == mHandle)
return false; {
} ICELogError(<< "Failed to create .wav file: filename = " << StringHelper::makeUtf8(filename) << " , error = " << errno);
return false;
}
// Write the .WAV header // Write the .WAV header
const char* riff = "RIFF"; const char* riff = "RIFF";
writeBuffer(riff, 4); checkWriteResult( fwrite(riff, 4, 1, mHandle) );
// Write the file size // Write the file size
uint32_t filesize = 0; unsigned int filesize = 0;
writeBuffer(&filesize, sizeof filesize); checkWriteResult( fwrite(&filesize, 4, 1, mHandle) );
const char* wavefmt = "WAVEfmt "; const char* wavefmt = "WAVEfmt ";
writeBuffer(wavefmt, 8); checkWriteResult( fwrite(wavefmt, 8, 1, mHandle) );
// Set the format description // Set the format description
uint32_t dwFmtSize = 16; /*= 16L*/ DWORD dwFmtSize = 16; /*= 16L*/;
; checkWriteResult( fwrite(&dwFmtSize, sizeof(dwFmtSize), 1, mHandle) );
writeBuffer(&dwFmtSize, sizeof(dwFmtSize));
WaveFormatEx format; WaveFormatEx format;
format.wFormatTag = WAVE_FORMAT_PCM; format.wFormatTag = WAVE_FORMAT_PCM;
writeBuffer(&format.wFormatTag, sizeof(format.wFormatTag)); checkWriteResult( fwrite(&format.wFormatTag, sizeof(format.wFormatTag), 1, mHandle) );
format.nChannels = mChannels; format.nChannels = mChannels;
writeBuffer(&format.nChannels, sizeof(format.nChannels)); checkWriteResult( fwrite(&format.nChannels, sizeof(format.nChannels), 1, mHandle) );
format.nSamplesPerSec = mSamplerate; format.nSamplesPerSec = mRate;
writeBuffer(&format.nSamplesPerSec, sizeof(format.nSamplesPerSec)); checkWriteResult( fwrite(&format.nSamplesPerSec, sizeof(format.nSamplesPerSec), 1, mHandle) );
format.nAvgBytesPerSec = mSamplerate * 2 * mChannels; format.nAvgBytesPerSec = mRate * 2 * mChannels;
writeBuffer(&format.nAvgBytesPerSec, sizeof(format.nAvgBytesPerSec)); checkWriteResult( fwrite(&format.nAvgBytesPerSec, sizeof(format.nAvgBytesPerSec), 1, mHandle) );
format.nBlockAlign = 2 * mChannels; format.nBlockAlign = 2 * mChannels;
writeBuffer(&format.nBlockAlign, sizeof(format.nBlockAlign)); checkWriteResult( fwrite(&format.nBlockAlign, sizeof(format.nBlockAlign), 1, mHandle) );
format.wBitsPerSample = BITS_PER_CHANNEL; format.wBitsPerSample = BITS_PER_CHANNEL;
writeBuffer(&format.wBitsPerSample, sizeof(format.wBitsPerSample)); checkWriteResult( fwrite(&format.wBitsPerSample, sizeof(format.wBitsPerSample), 1, mHandle) );
const char* data = "data"; const char* data = "data";
writeBuffer(data, 4); checkWriteResult( fwrite(data, 4, 1, mHandle));
mPath = p; mFileName = filename;
mWritten = 0; mWritten = 0;
mLengthOffset = mOutput->tellp(); mLengthOffset = ftell(mHandle);
writeBuffer(&mWritten, sizeof mWritten); checkWriteResult( fwrite(&mWritten, 4, 1, mHandle) );
return isOpened(); return isOpened();
} }
void WavFileWriter::close() void WavFileWriter::close()
{ {
LOCK; LOCK;
mOutput.reset();
if (mHandle)
{
fclose(mHandle);
mHandle = nullptr;
}
} }
size_t WavFileWriter::write(const void* buffer, size_t bytes) size_t WavFileWriter::write(const void* buffer, size_t bytes)
{ {
LOCK; LOCK;
if (!mOutput) if (!mHandle)
return 0; return 0;
// Seek the end of file - here new data will be written // Seek the end of file
mOutput->seekp(0, std::ios_base::end); fseek(mHandle, 0, SEEK_END);
mWritten += bytes; mWritten += bytes;
// Write the data // Write the data
writeBuffer(buffer, bytes); fwrite(buffer, bytes, 1, mHandle);
// Write file length // Write file length
mOutput->seekp(4, std::ios_base::beg); fseek(mHandle, 4, SEEK_SET);
uint32_t fl = mWritten + 36; int32_t fl = mWritten + 36;
writeBuffer(&fl, sizeof(fl)); fwrite(&fl, sizeof(fl), 1, mHandle);
// Write data length // Write data length
mOutput->seekp(mLengthOffset, std::ios_base::beg); fseek(mHandle, static_cast<long>(mLengthOffset), SEEK_SET);
writeBuffer(&mWritten, sizeof(mWritten)); checkWriteResult( fwrite(&mWritten, 4, 1, mHandle) );
return bytes; return bytes;
} }
bool WavFileWriter::isOpened() const bool WavFileWriter::isOpened()
{ {
LOCK; LOCK;
return mOutput && mOutput->is_open();
return (mHandle != nullptr);
} }
std::filesystem::path WavFileWriter::path() const std::tstring WavFileWriter::filename()
{ {
LOCK; LOCK;
return mPath;
return mFileName;
} }
+47 -57
View File
@@ -12,85 +12,75 @@
#include <string> #include <string>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <filesystem>
#include <fstream>
namespace Audio namespace Audio
{ {
class WavFileReader class WavFileReader
{ {
protected: protected:
uint16_t mChannels = 0; FILE* mHandle;
uint16_t mBits = 0; short mChannels;
int mSamplerate = 0; short mBits;
std::filesystem::path mPath; int mRate;
mutable std::recursive_mutex mFileMtx; std::tstring mFileName;
size_t mDataOffset = 0; mutable std::recursive_mutex mFileMtx;
size_t mDataLength = 0; unsigned mDataOffset;
Resampler mResampler; unsigned mDataLength;
unsigned mLastError = 0; Resampler mResampler;
std::unique_ptr<std::ifstream> mInput; unsigned mLastError;
uint8_t mTempBuffer[16384];
std::string readChunk(); std::string readChunk();
void readBuffer(void* buffer, size_t sz); // This raises an exception if sz bytes are not read public:
size_t tryReadBuffer(void* buffer, size_t sz); // This doesn't raise an exception
public:
WavFileReader(); WavFileReader();
~WavFileReader(); ~WavFileReader();
bool open(const std::filesystem::path& p); bool open(const std::tstring& filename);
void close(); void close();
bool isOpened(); bool isOpened();
void rewind(); void rewind();
int samplerate() const; int rate() const;
int channels() const;
// This method returns number of read bytes // This method returns number of read bytes
size_t read(void* buffer, size_t bytes); unsigned read(void* buffer, unsigned bytes);
size_t readRaw(void* buffer, size_t bytes);
// This method returns number of read samples // This method returns number of read samples
size_t read(short* buffer, size_t samples); unsigned read(short* buffer, unsigned samples);
size_t readRaw(short* buffer, size_t samples); std::tstring filename() const;
unsigned size() const;
std::filesystem::path path() const; unsigned lastError() const;
size_t size() const; };
unsigned lastError() const; typedef std::shared_ptr<WavFileReader> PWavFileReader;
};
typedef std::shared_ptr<WavFileReader> PWavFileReader; class WavFileWriter
{
protected:
FILE* mHandle; /// Handle of audio file.
std::tstring mFileName; /// Path to requested audio file.
std::recursive_mutex mFileMtx; /// Mutex to protect this instance.
int mWritten; /// Amount of written data (in bytes)
int mLengthOffset; /// Position of length field.
int mRate,
mChannels;
class WavFileWriter void checkWriteResult(int result);
{
protected:
std::unique_ptr<std::ofstream> mOutput; /// Handle of audio file.
std::filesystem::path mPath; /// Path to requested audio file.
mutable std::recursive_mutex mFileMtx; /// Mutex to protect this instance.
size_t mWritten = 0; /// Amount of written data (in bytes)
size_t mLengthOffset = 0; /// Position of length field.
int mSamplerate = 0, mChannels = 0;
void checkWriteResult(int result); public:
void writeBuffer(const void* buffer, size_t sz);
public:
WavFileWriter(); WavFileWriter();
~WavFileWriter(); ~WavFileWriter();
bool open(const std::filesystem::path& p, int samplerate, int channels); bool open(const std::tstring& filename, int rate, int channels);
void close(); void close();
bool isOpened() const; bool isOpened();
size_t write(const void* buffer, size_t bytes); size_t write(const void* buffer, size_t bytes);
std::filesystem::path path() const; std::tstring filename();
}; };
typedef std::shared_ptr<WavFileWriter> PWavFileWriter; typedef std::shared_ptr<WavFileWriter> PWavFileWriter;
} // namespace Audio }
#endif #endif
+334 -333
View File
@@ -15,540 +15,541 @@ using namespace Audio;
WmmeInputDevice::Buffer::Buffer() WmmeInputDevice::Buffer::Buffer()
{ {
// Do not use WAVEHDR allocated on stack! // Do not use WAVEHDR allocated on stack!
mHeaderHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, sizeof WAVEHDR); mHeaderHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, sizeof WAVEHDR);
if (!mHeaderHandle) if (!mHeaderHandle)
throw Exception(ERR_WMME_FAILED, GetLastError()); throw Exception(ERR_WMME_FAILED, GetLastError());
mHeader = (WAVEHDR*)GlobalLock(mHeaderHandle); mHeader = (WAVEHDR*)GlobalLock(mHeaderHandle);
mDataHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, AUDIO_MIC_BUFFER_SIZE); mDataHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, AUDIO_MIC_BUFFER_SIZE);
if (!mDataHandle) if (!mDataHandle)
throw Exception(ERR_WMME_FAILED, GetLastError()); throw Exception(ERR_WMME_FAILED, GetLastError());
mData = GlobalLock(mDataHandle); mData = GlobalLock(mDataHandle);
memset(mHeader, 0, sizeof *mHeader); memset(mHeader, 0, sizeof *mHeader);
mHeader->dwBufferLength = AUDIO_MIC_BUFFER_SIZE; mHeader->dwBufferLength = AUDIO_MIC_BUFFER_SIZE;
mHeader->dwFlags = 0; mHeader->dwFlags = 0;
mHeader->lpData = (LPSTR)mData; mHeader->lpData = (LPSTR)mData;
} }
WmmeInputDevice::Buffer::~Buffer() WmmeInputDevice::Buffer::~Buffer()
{ {
if (mDataHandle) if (mDataHandle)
{ {
GlobalUnlock(mDataHandle); GlobalUnlock(mDataHandle);
GlobalFree(mDataHandle); GlobalFree(mDataHandle);
} }
if (mHeaderHandle) if (mHeaderHandle)
{ {
GlobalUnlock(mHeaderHandle); GlobalUnlock(mHeaderHandle);
GlobalFree(mHeaderHandle); GlobalFree(mHeaderHandle);
} }
} }
bool WmmeInputDevice::Buffer::prepare(HWAVEIN device) bool WmmeInputDevice::Buffer::prepare(HWAVEIN device)
{ {
MMRESULT resCode = MMSYSERR_NOERROR; MMRESULT resCode = MMSYSERR_NOERROR;
mHeader->dwFlags = 0; mHeader->dwFlags = 0;
mHeader->dwBufferLength = AUDIO_MIC_BUFFER_SIZE; mHeader->dwBufferLength = AUDIO_MIC_BUFFER_SIZE;
mHeader->lpData = (LPSTR)mData; mHeader->lpData = (LPSTR)mData;
resCode = waveInPrepareHeader(device, mHeader, sizeof *mHeader); resCode = waveInPrepareHeader(device, mHeader, sizeof *mHeader);
// if (resCode != MMSYSERR_NOERROR) //if (resCode != MMSYSERR_NOERROR)
// LogCritical("Audio", << "Failed to prepare source header. Error code " << resCode << "."); // LogCritical("Audio", << "Failed to prepare source header. Error code " << resCode << ".");
return resCode == MMSYSERR_NOERROR; return resCode == MMSYSERR_NOERROR;
} }
bool WmmeInputDevice::Buffer::unprepare(HWAVEIN device) bool WmmeInputDevice::Buffer::unprepare(HWAVEIN device)
{ {
if (mHeader->dwFlags & WHDR_PREPARED) if (mHeader->dwFlags & WHDR_PREPARED)
{ {
MMRESULT resCode = waveInUnprepareHeader(device, mHeader, sizeof *mHeader); MMRESULT resCode = waveInUnprepareHeader(device, mHeader, sizeof *mHeader);
// if (resCode != MMSYSERR_NOERROR) //if (resCode != MMSYSERR_NOERROR)
// LogCritical("Audio", << "Failed to unprepare source header. Error code " << resCode << "."); // LogCritical("Audio", << "Failed to unprepare source header. Error code " << resCode << ".");
return resCode == MMSYSERR_NOERROR; return resCode == MMSYSERR_NOERROR;
} }
return true; return true;
} }
bool WmmeInputDevice::Buffer::isFinished() bool WmmeInputDevice::Buffer::isFinished()
{ {
return (mHeader->dwFlags & WHDR_DONE) != 0; return (mHeader->dwFlags & WHDR_DONE) != 0;
} }
bool WmmeInputDevice::Buffer::addToDevice(HWAVEIN device) bool WmmeInputDevice::Buffer::addToDevice(HWAVEIN device)
{ {
MMRESULT resCode = waveInAddBuffer(device, mHeader, sizeof(*mHeader)); MMRESULT resCode = waveInAddBuffer(device, mHeader, sizeof(*mHeader));
// if (resCode != MMSYSERR_NOERROR) //if (resCode != MMSYSERR_NOERROR)
// LogCritical("Audio", << "Failed to add buffer to source audio device. Error code is " << resCode << "."); // LogCritical("Audio", << "Failed to add buffer to source audio device. Error code is " << resCode << ".");
return resCode == MMSYSERR_NOERROR; return resCode == MMSYSERR_NOERROR;
} }
void* WmmeInputDevice::Buffer::data() void* WmmeInputDevice::Buffer::data()
{ {
return mData; return mData;
} }
WmmeInputDevice::WmmeInputDevice(int deviceId) WmmeInputDevice::WmmeInputDevice(int deviceId)
: mDevHandle(NULL), mDoneSignal(INVALID_HANDLE_VALUE), mFakeMode(false), mBufferIndex(0), mDeviceIndex(deviceId), :mDevHandle(NULL), mDoneSignal(INVALID_HANDLE_VALUE), mFakeMode(false),
mThreadHandle(0) mBufferIndex(0), mDeviceIndex(deviceId), mThreadHandle(0)
{ {
mDoneSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); mDoneSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL);
mShutdownSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); mShutdownSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL);
mRefCount = 0; mRefCount = 0;
} }
WmmeInputDevice::~WmmeInputDevice() WmmeInputDevice::~WmmeInputDevice()
{ {
close(); close();
::CloseHandle(mDoneSignal); ::CloseHandle(mDoneSignal);
::CloseHandle(mShutdownSignal); ::CloseHandle(mShutdownSignal);
} }
bool WmmeInputDevice::fakeMode() bool WmmeInputDevice::fakeMode()
{ {
return mFakeMode; return mFakeMode;
} }
void CALLBACK WmmeInputDevice::callbackProc(HWAVEIN hwi, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, void CALLBACK WmmeInputDevice::callbackProc(HWAVEIN hwi, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
DWORD_PTR dwParam2)
{ {
WmmeInputDevice* impl; WmmeInputDevice* impl;
switch (uMsg) switch(uMsg)
{ {
case WIM_DATA: case WIM_DATA:
impl = (WmmeInputDevice*)dwInstance; impl = (WmmeInputDevice*)dwInstance;
SetEvent(impl->mDoneSignal); SetEvent(impl->mDoneSignal);
break; break;
case WIM_CLOSE: case WIM_CLOSE:
break; break;
case WIM_OPEN: case WIM_OPEN:
break; break;
} }
} }
void WmmeInputDevice::openDevice() void WmmeInputDevice::openDevice()
{ {
// Build WAVEFORMATEX structure // Build WAVEFORMATEX structure
WAVEFORMATEX wfx; WAVEFORMATEX wfx;
memset(&wfx, 0, sizeof(wfx)); memset(&wfx, 0, sizeof(wfx));
wfx.wFormatTag = WAVE_FORMAT_PCM; wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nChannels = AUDIO_CHANNELS; wfx.nChannels = AUDIO_CHANNELS;
wfx.nSamplesPerSec = AUDIO_SAMPLERATE; wfx.nSamplesPerSec = AUDIO_SAMPLERATE;
wfx.wBitsPerSample = 16; wfx.wBitsPerSample = 16;
wfx.cbSize = 0; wfx.cbSize = 0;
wfx.nBlockAlign = wfx.wBitsPerSample * wfx.nChannels / 8; wfx.nBlockAlign = wfx.wBitsPerSample * wfx.nChannels / 8;
wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec; wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;
// Open wavein // Open wavein
MMRESULT mmres = MMRESULT mmres = waveInOpen(&mDevHandle, mDeviceIndex, &wfx, (DWORD_PTR)callbackProc, (DWORD_PTR)this, CALLBACK_FUNCTION);
waveInOpen(&mDevHandle, mDeviceIndex, &wfx, (DWORD_PTR)callbackProc, (DWORD_PTR)this, CALLBACK_FUNCTION); if (mmres != MMSYSERR_NOERROR)
if (mmres != MMSYSERR_NOERROR) {
{ mFakeMode = true;
mFakeMode = true; return;
return; }
} else
else mFakeMode = false;
mFakeMode = false;
// Create the buffers for running // Create the buffers for running
mBufferIndex = 0; mBufferIndex = 0;
for (int i = 0; i < AUDIO_MIC_BUFFER_COUNT; i++) for (int i=0; i<AUDIO_MIC_BUFFER_COUNT; i++)
mBufferList[i].prepare(mDevHandle); mBufferList[i].prepare(mDevHandle);
for (int i = 0; i < AUDIO_MIC_BUFFER_COUNT; i++) for (int i=0; i<AUDIO_MIC_BUFFER_COUNT; i++)
mBufferList[i].addToDevice(mDevHandle); mBufferList[i].addToDevice(mDevHandle);
/*mmres = */ waveInStart(mDevHandle); /*mmres = */waveInStart(mDevHandle);
} }
bool WmmeInputDevice::open() bool WmmeInputDevice::open()
{ {
Lock lock(mGuard); Lock lock(mGuard);
mRefCount++; mRefCount++;
if (mRefCount > 1) if (mRefCount > 1)
return true;
mThreadHandle = (HANDLE)_beginthread(&threadProc, 0, this);
return true; return true;
mThreadHandle = (HANDLE)_beginthread(&threadProc, 0, this);
return true;
} }
void WmmeInputDevice::closeDevice() void WmmeInputDevice::closeDevice()
{ {
// Stop device // Stop device
if (mDevHandle) if (mDevHandle)
{ {
MMRESULT mmres = MMSYSERR_NOERROR; MMRESULT mmres = MMSYSERR_NOERROR;
waveInReset(mDevHandle); waveInReset(mDevHandle);
waveInStop(mDevHandle); waveInStop(mDevHandle);
} }
// Close buffers // Close buffers
for (int i = 0; i < AUDIO_MIC_BUFFER_COUNT; i++) for (int i=0; i<AUDIO_MIC_BUFFER_COUNT; i++)
mBufferList[i].unprepare(mDevHandle); mBufferList[i].unprepare(mDevHandle);
// Close device // Close device
if (mDevHandle) if (mDevHandle)
{ {
waveInClose(mDevHandle); waveInClose(mDevHandle);
mDevHandle = NULL; mDevHandle = NULL;
} }
} }
void WmmeInputDevice::close() void WmmeInputDevice::close()
{ {
Lock l(mGuard); Lock l(mGuard);
mRefCount--; mRefCount--;
if (mRefCount != 0) if (mRefCount != 0)
return; return;
// Set shutdown signal // Set shutdown signal
if (!mThreadHandle) if (!mThreadHandle)
return; return;
::SetEvent(mShutdownSignal);
::WaitForSingleObject(mThreadHandle, INFINITE);
mThreadHandle = 0;
::SetEvent(mShutdownSignal);
::WaitForSingleObject(mThreadHandle, INFINITE);
mThreadHandle = 0;
} }
bool WmmeInputDevice::tryReadBuffer(void* buffer) bool WmmeInputDevice::tryReadBuffer(void* buffer)
{ {
Buffer& devBuffer = mBufferList[mBufferIndex]; Buffer& devBuffer = mBufferList[mBufferIndex];
if (!devBuffer.isFinished()) if (!devBuffer.isFinished())
return false; return false;
memcpy(buffer, devBuffer.data(), AUDIO_MIC_BUFFER_SIZE); memcpy(buffer, devBuffer.data(), AUDIO_MIC_BUFFER_SIZE);
devBuffer.unprepare(mDevHandle); devBuffer.unprepare(mDevHandle);
devBuffer.prepare(mDevHandle); devBuffer.prepare(mDevHandle);
if (!devBuffer.addToDevice(mDevHandle)) if (!devBuffer.addToDevice(mDevHandle))
setFakeMode(true); setFakeMode(true);
else else
{ {
} }
mBufferIndex = (mBufferIndex + 1) % AUDIO_MIC_BUFFER_COUNT; mBufferIndex = (mBufferIndex + 1) % AUDIO_MIC_BUFFER_COUNT;
return true; return true;
} }
void WmmeInputDevice::setFakeMode(bool fakeMode) void WmmeInputDevice::setFakeMode(bool fakeMode)
{ {
mFakeMode = fakeMode; mFakeMode = fakeMode;
} }
int WmmeInputDevice::readBuffer(void* buffer) int WmmeInputDevice::readBuffer(void* buffer)
{ {
// Lock lock(mGuard); //Lock lock(mGuard);
if (mRefCount <= 0 || mFakeMode) if (mRefCount <= 0 || mFakeMode)
return 0; return 0;
// Check for finished buffer // Check for finished buffer
while (!tryReadBuffer(buffer)) while (!tryReadBuffer(buffer))
WaitForSingleObject(mDoneSignal, 50); WaitForSingleObject(mDoneSignal, 50);
return AUDIO_MIC_BUFFER_SIZE; return AUDIO_MIC_BUFFER_SIZE;
} }
HWAVEIN WmmeInputDevice::handle() HWAVEIN WmmeInputDevice::handle()
{ {
Lock lock(mGuard); Lock lock(mGuard);
return mDevHandle; return mDevHandle;
} }
void WmmeInputDevice::threadProc(void* arg) void WmmeInputDevice::threadProc(void* arg)
{ {
WmmeInputDevice* impl = (WmmeInputDevice*)arg; WmmeInputDevice* impl = (WmmeInputDevice*)arg;
impl->openDevice(); impl->openDevice();
void* buffer = _alloca(AUDIO_MIC_BUFFER_SIZE); void* buffer = _alloca(AUDIO_MIC_BUFFER_SIZE);
DWORD waitResult = 0; DWORD waitResult = 0;
HANDLE waitArray[2] = {impl->mDoneSignal, impl->mShutdownSignal}; HANDLE waitArray[2] = {impl->mDoneSignal, impl->mShutdownSignal};
DWORD wr; DWORD wr;
do do
{
wr = ::WaitForMultipleObjects(2, waitArray, FALSE, INFINITE);
if (wr == WAIT_OBJECT_0)
{ {
wr = ::WaitForMultipleObjects(2, waitArray, FALSE, INFINITE); impl->readBuffer(buffer);
if (impl->connection())
impl->connection()->onMicData(Format(), buffer, AUDIO_MIC_BUFFER_SIZE);
}
} while (wr == WAIT_OBJECT_0);
if (wr == WAIT_OBJECT_0) impl->closeDevice();
{
impl->readBuffer(buffer);
if (impl->connection())
impl->connection()->onMicData(Format(), buffer, AUDIO_MIC_BUFFER_SIZE);
}
} while (wr == WAIT_OBJECT_0);
impl->closeDevice();
} }
// --- WmmeOutputDevice --- // --- WmmeOutputDevice ---
WmmeOutputDevice::Buffer::Buffer() : mHeaderHandle(NULL), mDataHandle(NULL), mData(NULL), mHeader(NULL) WmmeOutputDevice::Buffer::Buffer()
:mHeaderHandle(NULL), mDataHandle(NULL), mData(NULL), mHeader(NULL)
{ {
mHeaderHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, AUDIO_SPK_BUFFER_SIZE); mHeaderHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, AUDIO_SPK_BUFFER_SIZE);
if (!mHeaderHandle) if (!mHeaderHandle)
throw Exception(ERR_NOMEM); throw Exception(ERR_NOMEM);
mDataHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, AUDIO_SPK_BUFFER_SIZE); mDataHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, AUDIO_SPK_BUFFER_SIZE);
if (!mDataHandle) if (!mDataHandle)
throw Exception(ERR_NOMEM); throw Exception(ERR_NOMEM);
mHeader = (WAVEHDR*)GlobalLock(mHeaderHandle); mHeader = (WAVEHDR*)GlobalLock(mHeaderHandle);
mData = GlobalLock(mDataHandle); mData = GlobalLock(mDataHandle);
memset(mHeader, 0, sizeof *mHeader); memset(mHeader, 0, sizeof *mHeader);
mHeader->dwBufferLength = AUDIO_SPK_BUFFER_SIZE; mHeader->dwBufferLength = AUDIO_SPK_BUFFER_SIZE;
mHeader->lpData = (LPSTR)mData; mHeader->lpData = (LPSTR)mData;
} }
WmmeOutputDevice::Buffer::~Buffer() WmmeOutputDevice::Buffer::~Buffer()
{ {
if (mHeaderHandle) if (mHeaderHandle)
{ {
GlobalUnlock(mHeaderHandle); GlobalUnlock(mHeaderHandle);
GlobalFree(mHeaderHandle); GlobalFree(mHeaderHandle);
} }
if (mDataHandle) if (mDataHandle)
{ {
GlobalUnlock(mDataHandle); GlobalUnlock(mDataHandle);
GlobalFree(mDataHandle); GlobalFree(mDataHandle);
} }
} }
bool WmmeOutputDevice::Buffer::prepare(HWAVEOUT device) bool WmmeOutputDevice::Buffer::prepare(HWAVEOUT device)
{ {
MMRESULT result; MMRESULT result;
result = ::waveOutPrepareHeader(device, mHeader, sizeof *mHeader); result = ::waveOutPrepareHeader(device, mHeader, sizeof *mHeader);
return result == MMSYSERR_NOERROR; return result == MMSYSERR_NOERROR;
} }
bool WmmeOutputDevice::Buffer::unprepare(HWAVEOUT device) bool WmmeOutputDevice::Buffer::unprepare(HWAVEOUT device)
{ {
MMRESULT result; MMRESULT result;
result = ::waveOutUnprepareHeader(device, mHeader, sizeof *mHeader); result = ::waveOutUnprepareHeader(device, mHeader, sizeof *mHeader);
return result == MMSYSERR_NOERROR; return result == MMSYSERR_NOERROR;
} }
bool WmmeOutputDevice::Buffer::write(HWAVEOUT device) bool WmmeOutputDevice::Buffer::write(HWAVEOUT device)
{ {
MMRESULT result; MMRESULT result;
result = ::waveOutWrite(device, mHeader, sizeof *mHeader); result = ::waveOutWrite(device, mHeader, sizeof *mHeader);
return result == MMSYSERR_NOERROR; return result == MMSYSERR_NOERROR;
} }
WmmeOutputDevice::WmmeOutputDevice(int index) WmmeOutputDevice::WmmeOutputDevice(int index)
: mDevice(NULL), mDeviceIndex(index), mPlayedTime(0), mPlayedCount(0), mBufferIndex(0), mThreadHandle(NULL), :mDevice(NULL), mDeviceIndex(index), mPlayedTime(0), mPlayedCount(0), mBufferIndex(0), mThreadHandle(NULL),
mFailed(false), mShutdownMarker(false) mFailed(false), mShutdownMarker(false)
{ {
mDoneSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); mDoneSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL);
mShutdownSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); mShutdownSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL);
} }
WmmeOutputDevice::~WmmeOutputDevice() WmmeOutputDevice::~WmmeOutputDevice()
{ {
close(); close();
// Destroy used signals // Destroy used signals
CloseHandle(mDoneSignal); CloseHandle(mDoneSignal); CloseHandle(mShutdownSignal);
CloseHandle(mShutdownSignal);
} }
bool WmmeOutputDevice::open() bool WmmeOutputDevice::open()
{ {
// Start thread // Start thread
mThreadHandle = (HANDLE)_beginthread(&threadProc, 0, this); mThreadHandle = (HANDLE)_beginthread(&threadProc, 0, this);
return true; return true;
} }
void WmmeOutputDevice::close() void WmmeOutputDevice::close()
{ {
// Tell the thread to exit // Tell the thread to exit
SetEvent(mShutdownSignal); SetEvent(mShutdownSignal);
mShutdownMarker = true; mShutdownMarker = true;
// Wait for thread // Wait for thread
if (mThreadHandle) if (mThreadHandle)
WaitForSingleObject(mThreadHandle, INFINITE); WaitForSingleObject(mThreadHandle, INFINITE);
mThreadHandle = 0; mThreadHandle = 0;
} }
void WmmeOutputDevice::openDevice() void WmmeOutputDevice::openDevice()
{ {
mClosing = false; mClosing = false;
MMRESULT mmres = 0; MMRESULT mmres = 0;
WAVEFORMATEX wfx; WAVEFORMATEX wfx;
memset(&wfx, 0, sizeof(wfx)); memset(&wfx, 0, sizeof(wfx));
wfx.wFormatTag = 0x0001; wfx.wFormatTag = 0x0001;
wfx.nChannels = AUDIO_CHANNELS; wfx.nChannels = AUDIO_CHANNELS;
wfx.nSamplesPerSec = AUDIO_SAMPLERATE; wfx.nSamplesPerSec = AUDIO_SAMPLERATE;
wfx.wBitsPerSample = 16; wfx.wBitsPerSample = 16;
wfx.cbSize = 0; wfx.cbSize = 0;
wfx.nBlockAlign = wfx.wBitsPerSample * wfx.nChannels / 8; wfx.nBlockAlign = wfx.wBitsPerSample * wfx.nChannels / 8;
wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec; wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;
mmres = waveOutOpen(&mDevice, mDeviceIndex, &wfx, (DWORD_PTR)&callbackProc, (DWORD_PTR)this, CALLBACK_FUNCTION); mmres = waveOutOpen(&mDevice, mDeviceIndex, &wfx, (DWORD_PTR)&callbackProc, (DWORD_PTR)this, CALLBACK_FUNCTION);
if (mmres != MMSYSERR_NOERROR) if (mmres != MMSYSERR_NOERROR)
throw Exception(ERR_WMME_FAILED, mmres); throw Exception(ERR_WMME_FAILED, mmres);
// Prebuffer silence // Prebuffer silence
for (unsigned i = 0; i < AUDIO_SPK_BUFFER_COUNT; i++) for (unsigned i=0; i<AUDIO_SPK_BUFFER_COUNT; i++)
{ {
// bool dumb = false; //bool dumb = false;
// mCallback(mBufferList[i].mData, SPK_BUFFER_SIZE, dumb, dumb); //mCallback(mBufferList[i].mData, SPK_BUFFER_SIZE, dumb, dumb);
memset(mBufferList[i].mData, 0, AUDIO_SPK_BUFFER_SIZE); memset(mBufferList[i].mData, 0, AUDIO_SPK_BUFFER_SIZE);
mBufferList[i].prepare(mDevice); mBufferList[i].prepare(mDevice);
mBufferList[i].write(mDevice); mBufferList[i].write(mDevice);
} }
} }
void WmmeOutputDevice::closeDevice() void WmmeOutputDevice::closeDevice()
{ {
Lock l(mGuard); Lock l(mGuard);
mClosing = true; mClosing = true;
bool finished = false; bool finished = false;
while (!finished) while (!finished)
{ {
WaitForSingleObject(mDoneSignal, 10); WaitForSingleObject(mDoneSignal, 10);
finished = areBuffersFinished(); finished = areBuffersFinished();
} }
if (mDevice) if (mDevice)
{ {
waveOutReset(mDevice); waveOutReset(mDevice);
waveOutClose(mDevice); waveOutClose(mDevice);
} }
mDevice = NULL; mDevice = NULL;
} }
bool WmmeOutputDevice::areBuffersFinished() bool WmmeOutputDevice::areBuffersFinished()
{ {
Lock l(mGuard); Lock l(mGuard);
bool result = true; bool result = true;
for (unsigned i = 0; i < AUDIO_SPK_BUFFER_COUNT && result; i++) for (unsigned i=0; i<AUDIO_SPK_BUFFER_COUNT && result; i++)
{
bool finished = mBufferList[i].mHeader->dwFlags & WHDR_DONE ||
!mBufferList[i].mHeader->dwFlags;
if (finished)
{ {
bool finished = mBufferList[i].mHeader->dwFlags & WHDR_DONE || !mBufferList[i].mHeader->dwFlags; /* if (mBufferList[i].mHeader->dwFlags & WHDR_PREPARED)
if (finished) mBufferList[i].Unprepare(mDevice); */
{
/* if (mBufferList[i].mHeader->dwFlags & WHDR_PREPARED)
mBufferList[i].Unprepare(mDevice); */
}
result &= finished;
} }
result &= finished;
}
return result; return result;
} }
void WmmeOutputDevice::threadProc(void* arg) void WmmeOutputDevice::threadProc(void* arg)
{ {
WmmeOutputDevice* impl = (WmmeOutputDevice*)arg; WmmeOutputDevice* impl = (WmmeOutputDevice*)arg;
impl->openDevice(); impl->openDevice();
DWORD waitResult = 0; DWORD waitResult = 0;
HANDLE waitArray[2] = {impl->mDoneSignal, impl->mShutdownSignal}; HANDLE waitArray[2] = {impl->mDoneSignal, impl->mShutdownSignal};
unsigned index, i; unsigned index, i;
unsigned exitCount = 0; unsigned exitCount = 0;
bool exitSignal = false; bool exitSignal = false;
do do
{
// Poll for exit signal
if (!exitSignal)
exitSignal = impl->mShutdownMarker;
// Wait for played buffer
WaitForSingleObject(impl->mDoneSignal, 500);
// Iterate buffers to find played
for (i=0; i<AUDIO_SPK_BUFFER_COUNT; i++)
{ {
// Poll for exit signal index = (impl->mBufferIndex + i) % AUDIO_SPK_BUFFER_COUNT;
if (!exitSignal) Buffer& buffer = impl->mBufferList[index];
exitSignal = impl->mShutdownMarker; if (!(buffer.mHeader->dwFlags & WHDR_DONE))
break;
// Wait for played buffer buffer.unprepare(impl->mDevice);
WaitForSingleObject(impl->mDoneSignal, 500); if (!exitSignal)
{
bool useAEC = true;
if (impl->connection())
impl->connection()->onSpkData(Format(), buffer.mData, AUDIO_SPK_BUFFER_SIZE);
else
memset(buffer.mData, 0, AUDIO_SPK_BUFFER_SIZE);
// Iterate buffers to find played buffer.prepare(impl->mDevice);
for (i = 0; i < AUDIO_SPK_BUFFER_COUNT; i++) buffer.write(impl->mDevice);
{ }
index = (impl->mBufferIndex + i) % AUDIO_SPK_BUFFER_COUNT; else
Buffer& buffer = impl->mBufferList[index]; exitCount++;
if (!(buffer.mHeader->dwFlags & WHDR_DONE)) }
break; impl->mBufferIndex = (impl->mBufferIndex + i) % AUDIO_SPK_BUFFER_COUNT;
}
buffer.unprepare(impl->mDevice); while (!exitSignal || exitCount < AUDIO_SPK_BUFFER_COUNT);
if (!exitSignal) impl->closeDevice();
{
bool useAEC = true;
if (impl->connection())
impl->connection()->onSpkData(Format(), buffer.mData, AUDIO_SPK_BUFFER_SIZE);
else
memset(buffer.mData, 0, AUDIO_SPK_BUFFER_SIZE);
buffer.prepare(impl->mDevice);
buffer.write(impl->mDevice);
}
else
exitCount++;
}
impl->mBufferIndex = (impl->mBufferIndex + i) % AUDIO_SPK_BUFFER_COUNT;
} while (!exitSignal || exitCount < AUDIO_SPK_BUFFER_COUNT);
impl->closeDevice();
} }
HWAVEOUT WmmeOutputDevice::handle() HWAVEOUT WmmeOutputDevice::handle()
{ {
return mDevice; return mDevice;
} }
unsigned WmmeOutputDevice::playedTime() unsigned WmmeOutputDevice::playedTime()
{ {
if (!mDevice) if (!mDevice)
return 0; return 0;
unsigned result = 0; unsigned result = 0;
MMTIME mmt; MMTIME mmt;
memset(&mmt, 0, sizeof(mmt)); memset(&mmt, 0, sizeof(mmt));
mmt.wType = TIME_SAMPLES; mmt.wType = TIME_SAMPLES;
MMRESULT rescode = waveOutGetPosition(mDevice, &mmt, sizeof(mmt)); MMRESULT rescode = waveOutGetPosition(mDevice, &mmt, sizeof(mmt));
if (rescode != MMSYSERR_NOERROR || mmt.wType != TIME_SAMPLES) if (rescode != MMSYSERR_NOERROR || mmt.wType != TIME_SAMPLES)
closeDevice(); closeDevice();
else
{
if (mmt.u.ms < mPlayedTime)
result = 0;
else else
{ {
if (mmt.u.ms < mPlayedTime) result = mmt.u.ms - mPlayedTime;
result = 0; mPlayedTime = mmt.u.ms - result % 8;
else
{
result = mmt.u.ms - mPlayedTime;
mPlayedTime = mmt.u.ms - result % 8;
}
} }
}
return result / 8; return result / 8;
} }
void WmmeOutputDevice::setFakeMode(bool fakemode) void WmmeOutputDevice::setFakeMode(bool fakemode)
{ {
closeDevice(); closeDevice();
} }
bool WmmeOutputDevice::fakeMode() bool WmmeOutputDevice::fakeMode()
{ {
return mFailed; return mFailed;
} }
bool WmmeOutputDevice::closing() bool WmmeOutputDevice::closing()
{ {
return mClosing; return mClosing;
} }
void CALLBACK WmmeOutputDevice::callbackProc(HWAVEOUT hwo, UINT msg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, void CALLBACK WmmeOutputDevice::callbackProc(HWAVEOUT hwo, UINT msg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
DWORD_PTR dwParam2)
{ {
WmmeOutputDevice* impl; WmmeOutputDevice* impl;
if (msg == WOM_DONE) if (msg == WOM_DONE)
{ {
impl = (WmmeOutputDevice*)dwInstance; impl = (WmmeOutputDevice*)dwInstance;
InterlockedIncrement(&impl->mPlayedCount); InterlockedIncrement(&impl->mPlayedCount);
SetEvent(impl->mDoneSignal); SetEvent(impl->mDoneSignal);
} }
} }
#endif #endif
+80 -80
View File
@@ -8,7 +8,7 @@
#ifdef TARGET_WIN #ifdef TARGET_WIN
#include "../engine_config.h" #include "../config.h"
#include <winsock2.h> #include <winsock2.h>
#include <windows.h> #include <windows.h>
@@ -30,118 +30,118 @@
namespace Audio namespace Audio
{ {
class WmmeInputDevice : public InputDevice class WmmeInputDevice: public InputDevice
{ {
public: public:
WmmeInputDevice(int index); WmmeInputDevice(int index);
~WmmeInputDevice(); ~WmmeInputDevice();
bool open(); bool open();
void close(); void close();
bool fakeMode(); bool fakeMode();
void setFakeMode(bool fakeMode); void setFakeMode(bool fakeMode);
int readBuffer(void* buffer); int readBuffer(void* buffer);
HWAVEIN handle(); HWAVEIN handle();
protected: protected:
class Buffer class Buffer
{ {
public: public:
Buffer(); Buffer();
~Buffer(); ~Buffer();
bool prepare(HWAVEIN device); bool prepare(HWAVEIN device);
bool unprepare(HWAVEIN device); bool unprepare(HWAVEIN device);
bool isFinished(); bool isFinished();
bool addToDevice(HWAVEIN device); bool addToDevice(HWAVEIN device);
void* data(); void* data();
protected: protected:
HGLOBAL mDataHandle; HGLOBAL mDataHandle;
void* mData; void* mData;
HGLOBAL mHeaderHandle; HGLOBAL mHeaderHandle;
WAVEHDR* mHeader; WAVEHDR* mHeader;
}; };
Mutex mGuard; /// Mutex to protect this instance. Mutex mGuard; /// Mutex to protect this instance.
HWAVEIN mDevHandle; /// Handle of opened capture device. HWAVEIN mDevHandle; /// Handle of opened capture device.
HANDLE mThreadHandle; HANDLE mThreadHandle;
HANDLE mShutdownSignal; HANDLE mShutdownSignal;
HANDLE mDoneSignal; /// Event handle to signal about finished capture. HANDLE mDoneSignal; /// Event handle to signal about finished capture.
Buffer mBufferList[AUDIO_MIC_BUFFER_COUNT]; Buffer mBufferList[AUDIO_MIC_BUFFER_COUNT];
unsigned mBufferIndex; unsigned mBufferIndex;
int mDeviceIndex; /// Index of capture device. int mDeviceIndex; /// Index of capture device.
volatile bool mFakeMode; /// Marks if fake mode is active. volatile bool mFakeMode; /// Marks if fake mode is active.
int mRefCount; int mRefCount;
bool tryReadBuffer(void* buffer); bool tryReadBuffer(void* buffer);
void openDevice(); void openDevice();
void closeDevice(); void closeDevice();
static void CALLBACK callbackProc(HWAVEIN hwi, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, static void CALLBACK callbackProc(HWAVEIN hwi, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2);
DWORD_PTR dwParam2); static void threadProc(void* arg);
static void threadProc(void* arg); };
};
class WmmeOutputDevice : public OutputDevice class WmmeOutputDevice: public OutputDevice
{ {
public: public:
WmmeOutputDevice(int index); WmmeOutputDevice(int index);
~WmmeOutputDevice(); ~WmmeOutputDevice();
bool open(); bool open();
void close(); void close();
HWAVEOUT handle(); HWAVEOUT handle();
unsigned playedTime(); unsigned playedTime();
void setFakeMode(bool fakemode); void setFakeMode(bool fakemode);
bool fakeMode(); bool fakeMode();
bool closing(); bool closing();
protected: protected:
class Buffer class Buffer
{ {
friend class WmmeOutputDevice; friend class WmmeOutputDevice;
public: public:
Buffer(); Buffer();
~Buffer(); ~Buffer();
bool prepare(HWAVEOUT device); bool prepare(HWAVEOUT device);
bool unprepare(HWAVEOUT device); bool unprepare(HWAVEOUT device);
bool write(HWAVEOUT device); bool write(HWAVEOUT device);
protected: protected:
WAVEHDR* mHeader; WAVEHDR* mHeader;
void* mData; void* mData;
HGLOBAL mHeaderHandle; HGLOBAL mHeaderHandle;
HGLOBAL mDataHandle; HGLOBAL mDataHandle;
}; };
Mutex mGuard; /// Mutex to protect this instance Mutex mGuard; /// Mutex to protect this instance
int mDeviceIndex; int mDeviceIndex;
HWAVEOUT mDevice; /// Handle of opened audio device HWAVEOUT mDevice; /// Handle of opened audio device
Buffer mBufferList[AUDIO_SPK_BUFFER_COUNT]; Buffer mBufferList[AUDIO_SPK_BUFFER_COUNT];
unsigned mPlayedTime; /// Amount of played time in milliseconds unsigned mPlayedTime; /// Amount of played time in milliseconds
bool mClosing; bool mClosing;
HANDLE mDoneSignal, mShutdownSignal, mThreadHandle; HANDLE mDoneSignal,
volatile bool mShutdownMarker; mShutdownSignal,
mThreadHandle;
volatile bool mShutdownMarker;
volatile LONG mPlayedCount; volatile LONG mPlayedCount;
unsigned mBufferIndex; unsigned mBufferIndex;
bool mFailed; bool mFailed;
void openDevice(); void openDevice();
void closeDevice(); void closeDevice();
bool areBuffersFinished(); bool areBuffersFinished();
static void CALLBACK callbackProc(HWAVEOUT hwo, UINT msg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, static void CALLBACK callbackProc(HWAVEOUT hwo, UINT msg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2);
DWORD_PTR dwParam2); static void threadProc(void* arg);
static void threadProc(void* arg); };
};
} // namespace Audio
}
#endif #endif
+1
View File
@@ -1 +1,2 @@
#include "Audio_iOS.h" #include "Audio_iOS.h"
+20 -18
View File
@@ -1,36 +1,38 @@
#ifndef __AUDIO_IOS #ifndef __AUDIO_IOS
#define __AUDIO_IOS #define __AUDIO_IOS
class IosInputDevice : public InputDevice class IosInputDevice: public InputDevice
{ {
protected: protected:
public: public:
IosInputDevice(); IosInputDevice();
~IosInputDevice(); ~IosInputDevice();
void open();
void close(); void open();
void close();
}; };
class IosOutputDevice : public OutputDevice class IosOutputDevice: public OutputDevice
{ {
protected: protected:
public: public:
IosOutputDevice(); IosOutputDevice();
~IosOutputDevice(); ~IosOutputDevice();
enum enum
{ {
Receiver, Receiver,
Speaker, Speaker,
Bluetooth Bluetooth
}; };
int route(); int route();
void setRoute(int route); void setRoute(int route);
void open(); void open();
void close(); void close();
}; };
#endif #endif
+1 -16
View File
@@ -1,42 +1,27 @@
project (audio_lib) project (audio_lib)
# Rely on C++ 11 # Rely on C++ 11
set (CMAKE_CXX_STANDARD 20) set (CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_STANDARD_REQUIRED ON) set (CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set (AUDIOLIB_SOURCES set (AUDIOLIB_SOURCES
Audio_Resampler.cpp Audio_Resampler.cpp
Audio_Resampler.h
Audio_Quality.cpp Audio_Quality.cpp
Audio_Quality.h
Audio_Mixer.cpp Audio_Mixer.cpp
Audio_Mixer.h
Audio_Interface.cpp Audio_Interface.cpp
Audio_Interface.h
Audio_Helper.cpp Audio_Helper.cpp
Audio_Helper.h
Audio_DataWindow.cpp Audio_DataWindow.cpp
Audio_DataWindow.h
Audio_DevicePair.cpp Audio_DevicePair.cpp
Audio_DevicePair.h
Audio_Player.cpp Audio_Player.cpp
Audio_Player.h
Audio_Null.cpp Audio_Null.cpp
Audio_Null.h
Audio_CoreAudio.cpp Audio_CoreAudio.cpp
Audio_CoreAudio.h
Audio_DirectSound.cpp Audio_DirectSound.cpp
Audio_DirectSound.h
Audio_AndroidOboe.cpp
Audio_AndroidOboe.h
Audio_WavFile.cpp Audio_WavFile.cpp
Audio_WavFile.h
) )
add_library(audio_lib ${AUDIOLIB_SOURCES}) add_library(audio_lib ${AUDIOLIB_SOURCES})
set_property(TARGET audio_lib PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
## ##
target_include_directories(audio_lib target_include_directories(audio_lib
+113
View File
@@ -0,0 +1,113 @@
/* Copyright(C) 2007-2020 VoIP objects (voipobjects.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef __TOOLKIT_CONFIG_H
#define __TOOLKIT_CONFIG_H
#define USE_SPEEX_AEC
// TODO: test implementation with webrtc aec; be careful - it needs fixes!
//#define USE_WEBRTC_AEC
#define USER
#define AUDIO_SAMPLE_WIDTH 16
#define AUDIO_CHANNELS 1
// Samplerate must be 8 / 16 / 24 / 32 / 48 KHz
#define AUDIO_SAMPLERATE 8000
#define AUDIO_MIC_BUFFER_COUNT 16
#define AUDIO_MIC_BUFFER_LENGTH 10
#define AUDIO_MIC_BUFFER_SIZE (AUDIO_MIC_BUFFER_LENGTH * AUDIO_SAMPLERATE / 1000 * 2 * AUDIO_CHANNELS)
#define AUDIO_SPK_BUFFER_COUNT 16
#define AUDIO_SPK_BUFFER_LENGTH 10
#define AUDIO_SPK_BUFFER_SIZE (AUDIO_SPK_BUFFER_LENGTH * AUDIO_SAMPLERATE / 1000 * 2 * AUDIO_CHANNELS)
#define AUDIO_MIX_CHANNEL_COUNT 16
#define AUDIO_DEVICEPAIR_INPUTBUFFER 16384
// Avoid too high resampler quality - it can take many CPU and cause gaps in playing
#define AUDIO_RESAMPLER_QUALITY 1
#define AEC_FRAME_TIME 10
#define AEC_TAIL_TIME 160
// Defined these two lines to get dumping of audio input/output
//#define AUDIO_DUMPINPUT
//#define AUDIO_DUMPOUTPUT
#define UA_REGISTRATION_TIME 3600
#define UA_MEDIA_PORT_START 20000
#define UA_MEDIA_PORT_FINISH 30000
#define UA_MAX_UDP_PACKET_SIZE 576
#define UA_PUBLICATION_ID "314"
#define MT_SAMPLERATE AUDIO_SAMPLERATE
#define MT_MAXAUDIOFRAME 1440
#define MT_MAXRTPPACKET 1500
#define MT_DTMF_END_PACKETS 3
#define RTP_BUFFER_HIGH 24480
#define RTP_BUFFER_LOW 10
#define RTP_BUFFER_PREBUFFER 80
#define RTP_DECODED_CAPACITY 2048
#define DEFAULT_SUBSCRIPTION_TIME 1200
#define DEFAULT_SUBSCRIPTION_REFRESHTIME 500
#define PRESENCE_IN_REG_HEADER "PresenceInReg"
// Maximum UDP packet length
#define MAX_UDPPACKET_SIZE 65535
#define MAX_VALID_UDPPACKET_SIZE 2048
// AMR codec defines - it requires USE_AMR_CODEC defined
// #define USE_AMR_CODEC
#define MT_AMRNB_PAYLOADTYPE 112
#define MT_AMRNB_CODECNAME "amr"
#define MT_AMRNB_OCTET_PAYLOADTYPE 113
#define MT_AMRWB_PAYLOADTYPE 96
#define MT_AMRWB_CODECNAME "amr-wb"
#define MT_AMRWB_OCTET_PAYLOADTYPE 97
#define MT_GSMEFR_PAYLOADTYPE 126
#define MT_GSMEFR_CODECNAME "GERAN-EFR"
#define MT_EVS_PAYLOADTYPE 127
#define MT_EVS_CODECNAME "EVS"
// OPUS codec defines
// #define USE_OPUS_CODEC
#define MT_OPUS_CODEC_PT 106
// ILBC codec defines
#define MT_ILBC20_PAYLOADTYPE -1
#define MT_ILBC30_PAYLOADTYPE -1
// ISAC codec defines
#define MT_ISAC16K_PAYLOADTYPE -1
#define MT_ISAC32K_PAYLOADTYPE -1
// GSM HR payload type
#define MT_GSMHR_PAYLOADTYPE -1
// Mirror buffer capacity
#define MT_MIRROR_CAPACITY 32768
// Mirror buffer readiness threshold - 50 milliseconds
#define MT_MIRROR_PREBUFFER (MT_SAMPLERATE / 10)
#if defined(TARGET_OSX) || defined(TARGET_LINUX)
# define TEXT(X) X
#endif
// In milliseconds
#define MT_SEVANA_FRAME_TIME 680
#endif
File diff suppressed because it is too large Load Diff
+83 -84
View File
@@ -22,121 +22,120 @@
class UserAgent; class UserAgent;
class Session; class Session;
class Account : public resip::DnsResultSink class Account: public resip::DnsResultSink
{ {
friend class UserAgent; friend class UserAgent;
friend class NATDecorator; friend class NATDecorator;
public: public:
Account(PVariantMap config, UserAgent& agent); Account(PVariantMap config, UserAgent& agent);
~Account(); ~Account();
void start(); void start();
void stop(); void stop();
void refresh(); void refresh();
bool active(); bool active();
int id() const; int id() const;
enum class RegistrationState enum class RegistrationState
{ {
None, None,
Registering, Registering,
Reregistering, Reregistering,
Registered, Registered,
Unregistering Unregistering
}; };
RegistrationState registrationState(); RegistrationState registrationState();
/* Publishes new presence information */ /* Publishes new presence information */
void publishPresence(bool online, const std::string& content, int seconds = 600); void publishPresence(bool online, const std::string& content, int seconds = 600);
/* Stops publishing of presence */ /* Stops publishing of presence */
void stopPublish(); void stopPublish();
/* Starts observing on specified target / package */ /* Starts observing on specified target / package */
PClientObserver observe(const std::string& target, const std::string& package, void* tag); PClientObserver observe(const std::string& target, const std::string& package, void* tag);
/* Queues message to peer with specified mime type. Returns ID of message. */ /* Queues message to peer with specified mime type. Returns ID of message. */
int sendMsg(const std::string& peer, const void* ptr, unsigned length, const std::string& mime, void* tag); int sendMsg(const std::string& peer, const void* ptr, unsigned length, const std::string& mime, void* tag);
/* Returns name of account - <sip:user@domain> */ /* Returns name of account - <sip:user@domain> */
std::string name(); std::string name();
/* Updates account with configuration */ /* Updates account with configuration */
void setup(VariantMap& config); void setup(VariantMap& config);
/* Returns corresponding resiprocate profile */ /* Returns corresponding resiprocate profile */
std::shared_ptr<resip::UserProfile> getUserProfile() const { return mProfile; } resip::SharedPtr<resip::UserProfile> getUserProfile() const { return mProfile; }
typedef std::map<std::string, std::string> UserInfo; typedef std::map<std::string, std::string> UserInfo;
void setUserInfo(const UserInfo& info); void setUserInfo(const UserInfo& info);
UserInfo getUserInfo() const; UserInfo getUserInfo() const;
protected: protected:
PVariantMap mConfig; PVariantMap mConfig;
// Registration // Registration
ResipSession* mRegistration; ResipSession* mRegistration;
resip::ClientRegistrationHandle mRegistrationHandle; resip::ClientRegistrationHandle mRegistrationHandle;
resip::ClientPublicationHandle mPublication; resip::ClientPublicationHandle mPublication;
resip::TransportType mUsedTransport; resip::TransportType mUsedTransport;
RegistrationState mRegistrationState; RegistrationState mRegistrationState;
ice::NetworkAddress mExternalAddress; ice::NetworkAddress mExternalAddress;
std::shared_ptr<resip::UserProfile> mProfile; resip::SharedPtr<resip::UserProfile> mProfile;
UserAgent& mAgent; UserAgent& mAgent;
bool mPresenceOnline; bool mPresenceOnline;
std::string mPresenceContent; std::string mPresenceContent;
// Timer to refresh STUN server IP // Timer to refresh STUN server IP
ice::ICEScheduleTimer mRefreshStunServerIpTimer; ice::ICEScheduleTimer mRefreshStunServerIpTimer;
// Cached auth // Cached auth
resip::Auth mCachedAuth; resip::Auth mCachedAuth;
// Id of account // Id of account
int mId; int mId;
// User info about current state // User info about current state
UserInfo mUserInfo; UserInfo mUserInfo;
// List of client subscriptions sent from this account // List of client subscriptions sent from this account
typedef std::set<PClientObserver> ClientObserverSet; typedef std::set<PClientObserver> ClientObserverSet;
ClientObserverSet mClientObserverSet; ClientObserverSet mClientObserverSet;
void process(); void process();
// Method queries new stun server ip from dns (if stun server is specified as dns name) // Method queries new stun server ip from dns (if stun server is specified as dns name)
void queryStunServerIp(); void queryStunServerIp();
bool isResponsibleFor(const resip::NameAddr& addr); bool isResponsibleFor(const resip::NameAddr& addr);
enum class SecureScheme enum class SecureScheme
{ {
SipsAndTls, SipsAndTls,
SipsOnly, SipsOnly,
TlsOnly, TlsOnly,
Nothing Nothing
}; };
resip::NameAddr contact(SecureScheme ss = SecureScheme::SipsOnly); resip::NameAddr contact(SecureScheme ss = SecureScheme::SipsOnly);
// This method prepares configuration, creates ice stack and sets ownership to session // This method prepares configuration, creates ice stack and sets ownership to session
void prepareIceStack(Session* session, ice::AgentRole role); void prepareIceStack(Session* session, ice::AgentRole role);
void onSuccess(resip::ClientRegistrationHandle h, const resip::SipMessage& response); void onSuccess(resip::ClientRegistrationHandle h, const resip::SipMessage& response);
void onRemoved(resip::ClientRegistrationHandle h, const resip::SipMessage& response); void onRemoved(resip::ClientRegistrationHandle h, const resip::SipMessage& response);
void onFailure(resip::ClientRegistrationHandle, const resip::SipMessage& response); void onFailure(resip::ClientRegistrationHandle, const resip::SipMessage& response);
#pragma region DnsResultSink implementation #pragma region DnsResultSink implementation
void onDnsResult(const resip::DNSResult<resip::DnsHostRecord>&); void onDnsResult(const resip::DNSResult<resip::DnsHostRecord>&);
void onDnsResult(const resip::DNSResult<resip::DnsAAAARecord>&); void onDnsResult(const resip::DNSResult<resip::DnsAAAARecord>&);
void onDnsResult(const resip::DNSResult<resip::DnsSrvRecord>&); void onDnsResult(const resip::DNSResult<resip::DnsSrvRecord>&);
void onDnsResult(const resip::DNSResult<resip::DnsNaptrRecord>&); void onDnsResult(const resip::DNSResult<resip::DnsNaptrRecord>&);
void onDnsResult(const resip::DNSResult<resip::DnsCnameRecord>&); void onDnsResult(const resip::DNSResult<resip::DnsCnameRecord>&);
#pragma endregion #pragma endregion
static int generateId(); static int generateId();
static std::atomic_int IdGenerator; static resip::AtomicCounter IdGenerator;
}; };
typedef std::shared_ptr<Account> PAccount; typedef std::shared_ptr<Account> PAccount;
+63 -60
View File
@@ -14,10 +14,11 @@
#include "../helper/HL_Log.h" #include "../helper/HL_Log.h"
#include "../helper/HL_String.h" #include "../helper/HL_String.h"
#define LOG_SUBSYSTEM "engine" #define LOG_SUBSYSTEM "AudioProvider"
AudioProvider::AudioProvider(UserAgent& agent, MT::Terminal& terminal) AudioProvider::AudioProvider(UserAgent& agent, MT::Terminal& terminal)
: mUserAgent(agent), mTerminal(terminal), mState(0), mRemoteTelephoneCodec(0), mRemoteNoSdp(false) :mUserAgent(agent), mTerminal(terminal), mState(0),
mRemoteTelephoneCodec(0), mRemoteNoSdp(false)
{ {
mActive = mfActive; mActive = mfActive;
mRemoteState = msSendRecv; mRemoteState = msSendRecv;
@@ -25,11 +26,12 @@ AudioProvider::AudioProvider(UserAgent& agent, MT::Terminal& terminal)
if (mUserAgent.config().exists(CONFIG_CODEC_PRIORITY)) if (mUserAgent.config().exists(CONFIG_CODEC_PRIORITY))
mCodecPriority.setupFrom(mUserAgent.config()[CONFIG_CODEC_PRIORITY].asVMap()); mCodecPriority.setupFrom(mUserAgent.config()[CONFIG_CODEC_PRIORITY].asVMap());
mSrtpSuite = SRTP_NONE; mSrtpSuite = SRTP_NONE;
setStateImpl((int)StreamState::SipRecv | (int)StreamState::SipSend | (int)StreamState::Receiving | setStateImpl((int)StreamState::SipRecv | (int)StreamState::SipSend | (int)StreamState::Receiving | (int)StreamState::Sending);
(int)StreamState::Sending);
} }
AudioProvider::~AudioProvider() {} AudioProvider::~AudioProvider()
{
}
std::string AudioProvider::streamName() std::string AudioProvider::streamName()
{ {
@@ -45,7 +47,7 @@ std::string AudioProvider::streamProfile()
} }
// Sets destination IP address // Sets destination IP address
void AudioProvider::setDestinationAddress(const RtpPair<InternetAddress>& addr) void AudioProvider::setDestinationAddress(const RtpPair<InternetAddress>& addr)
{ {
if (!mActiveStream) if (!mActiveStream)
return; return;
@@ -53,7 +55,7 @@ void AudioProvider::setDestinationAddress(const RtpPair<InternetAddress>& addr)
mActiveStream->setDestination(addr); mActiveStream->setDestination(addr);
} }
void AudioProvider::configureMediaObserver(MT::Stream::MediaObserver* observer, void* userTag) void AudioProvider::configureMediaObserver(MT::Stream::MediaObserver *observer, void* userTag)
{ {
mMediaObserver = observer; mMediaObserver = observer;
mMediaObserverTag = userTag; mMediaObserverTag = userTag;
@@ -62,21 +64,20 @@ void AudioProvider::configureMediaObserver(MT::Stream::MediaObserver* observer,
} }
// Processes incoming data // Processes incoming data
void AudioProvider::processData(const PDatagramSocket& s, const void* dataBuffer, int dataSize, InternetAddress& source) void AudioProvider::processData(PDatagramSocket s, const void* dataBuffer, int dataSize, InternetAddress& source)
{ {
if (!mActiveStream) if (!mActiveStream)
return; return;
if (RtpHelper::isRtpOrRtcp(dataBuffer, dataSize)) if (RtpHelper::isRtpOrRtcp(dataBuffer, dataSize))
{ {
ICELogMedia(<< "Adding new data to stream processing"); ICELogMedia(<<"Adding new data to stream processing");
mActiveStream->dataArrived(s, dataBuffer, dataSize, source); mActiveStream->dataArrived(s, dataBuffer, dataSize, source);
} }
} }
// This method is called by user agent to send ICE packet from mediasocket // This method is called by user agent to send ICE packet from mediasocket
void AudioProvider::sendData(const PDatagramSocket& s, InternetAddress& destination, const void* buffer, void AudioProvider::sendData(PDatagramSocket s, InternetAddress& destination, const void* buffer, unsigned int size)
unsigned int size)
{ {
s->sendDatagram(destination, buffer, size); s->sendDatagram(destination, buffer, size);
} }
@@ -92,19 +93,18 @@ void AudioProvider::updateSdpOffer(resip::SdpContents::Session::Medium& sdp, Sdp
// Check if SRTP suite is found already or not // Check if SRTP suite is found already or not
if (mSrtpSuite == SRTP_NONE) if (mSrtpSuite == SRTP_NONE)
{ {
// RFC 4568 requires a unique tag per crypto attribute; use the suite id.
for (int suite = SRTP_AES_128_AUTH_80; suite <= SRTP_LAST; suite++) for (int suite = SRTP_AES_128_AUTH_80; suite <= SRTP_LAST; suite++)
sdp.addAttribute("crypto", resip::Data(createCryptoAttribute((SrtpSuite)suite, suite))); sdp.addAttribute("crypto", resip::Data(createCryptoAttribute((SrtpSuite)suite)));
} }
else else
// Answer/re-offer: echo the tag of the negotiated attribute. sdp.addAttribute("crypto", resip::Data(createCryptoAttribute(mSrtpSuite)));
sdp.addAttribute("crypto", resip::Data(createCryptoAttribute(mSrtpSuite, mSrtpTag)));
} }
#if defined(USE_RESIP_INTEGRATION)
// Use CodecListPriority mCodecPriority adapter to work with codec priorities // Use CodecListPriority mCodecPriority adapter to work with codec priorities
if (mAvailableCodecs.empty()) if (mAvailableCodecs.empty())
{ {
for (int i = 0; i < mCodecPriority.count(mTerminal.codeclist()); i++) for (int i=0; i<mCodecPriority.count(mTerminal.codeclist()); i++)
mCodecPriority.codecAt(mTerminal.codeclist(), i).updateSdp(sdp.codecs(), direction); mCodecPriority.codecAt(mTerminal.codeclist(), i).updateSdp(sdp.codecs(), direction);
sdp.addCodec(resip::SdpContents::Session::Codec::TelephoneEvent); sdp.addCodec(resip::SdpContents::Session::Codec::TelephoneEvent);
} }
@@ -114,42 +114,27 @@ void AudioProvider::updateSdpOffer(resip::SdpContents::Session::Medium& sdp, Sdp
if (mRemoteTelephoneCodec) if (mRemoteTelephoneCodec)
sdp.addCodec(resip::SdpContents::Session::Codec::TelephoneEvent); sdp.addCodec(resip::SdpContents::Session::Codec::TelephoneEvent);
} }
#endif
// Publish stream state // Publish stream state
const char* attr = nullptr; const char* attr = nullptr;
switch (mActive) switch (mActive)
{ {
case mfActive: case mfActive:
switch (mRemoteState) switch(mRemoteState)
{ {
case msSendonly: case msSendonly: attr = "recvonly"; break;
attr = "recvonly"; case msInactive: attr = "recvonly"; break;
break;
case msInactive:
attr = "recvonly";
break;
case msRecvonly:
case msSendRecv:
break; // Do nothing here
} }
break; break;
case mfPaused: case mfPaused:
switch (mRemoteState) switch (mRemoteState)
{ {
case msRecvonly: case msRecvonly: attr = "sendonly"; break;
attr = "sendonly"; case msSendonly: attr = "inactive"; break;
break; case msInactive: attr = "inactive"; break;
case msSendonly: case msSendRecv: attr = "sendonly"; break;
attr = "inactive";
break;
case msInactive:
attr = "inactive";
break;
case msSendRecv:
attr = "sendonly";
break;
} }
break; break;
} }
@@ -239,11 +224,13 @@ bool AudioProvider::processSdpOffer(const resip::SdpContents::Session::Medium& m
// Use CodecListPriority mCodecPriority to work with codec priorities // Use CodecListPriority mCodecPriority to work with codec priorities
int pt; int pt;
for (int localIndex = 0; localIndex < mCodecPriority.count(mTerminal.codeclist()); localIndex++) for (int localIndex=0; localIndex<mCodecPriority.count(mTerminal.codeclist()); localIndex++)
{ {
MT::Codec::Factory& factory = mCodecPriority.codecAt(mTerminal.codeclist(), localIndex); MT::Codec::Factory& factory = mCodecPriority.codecAt(mTerminal.codeclist(), localIndex);
#if defined(USE_RESIP_INTEGRATION)
if ((pt = factory.processSdp(media.codecs(), sdpDirection)) != -1) if ((pt = factory.processSdp(media.codecs(), sdpDirection)) != -1)
mAvailableCodecs.push_back(RemoteCodec(&factory, pt)); mAvailableCodecs.push_back(RemoteCodec(&factory, pt));
#endif
} }
if (!mAvailableCodecs.size()) if (!mAvailableCodecs.size())
@@ -254,19 +241,17 @@ bool AudioProvider::processSdpOffer(const resip::SdpContents::Session::Medium& m
{ {
// Find the most strong crypt suite // Find the most strong crypt suite
const std::list<resip::Data>& vl = media.getValues("crypto"); const std::list<resip::Data>& vl = media.getValues("crypto");
SrtpSuite ss = SRTP_NONE; SrtpSuite ss = SRTP_NONE;
ByteBuffer key; ByteBuffer key;
for (std::list<resip::Data>::const_iterator attrIter = vl.begin(); attrIter != vl.end(); attrIter++) for (std::list<resip::Data>::const_iterator attrIter = vl.begin(); attrIter != vl.end(); attrIter++)
{ {
const resip::Data& attr = *attrIter; const resip::Data& attr = *attrIter;
ByteBuffer tempkey; ByteBuffer tempkey;
int tag = 1; SrtpSuite suite = processCryptoAttribute(attr, tempkey);
SrtpSuite suite = processCryptoAttribute(attr, tempkey, &tag); if (suite > ss)
if (srtpSuiteStrength(suite) > srtpSuiteStrength(ss))
{ {
ss = suite; ss = suite;
mSrtpSuite = suite; mSrtpSuite = suite;
mSrtpTag = tag;
key = tempkey; key = tempkey;
} }
} }
@@ -311,30 +296,40 @@ MT::PStream AudioProvider::activeStream()
return mActiveStream; return mActiveStream;
} }
std::string AudioProvider::createCryptoAttribute(SrtpSuite suite, int tag) std::string AudioProvider::createCryptoAttribute(SrtpSuite suite)
{ {
if (!mActiveStream) if (!mActiveStream)
return ""; return "";
// Use tag 1 - it is ok, as we use only single crypto attribute
int srtpTag = 1;
// Print key to base64 string // Print key to base64 string
PByteBuffer keyBuffer = mActiveStream->srtp().outgoingKey(suite).first; PByteBuffer keyBuffer = mActiveStream->srtp().outgoingKey(suite).first;
if (!keyBuffer)
return "";
resip::Data d(keyBuffer->data(), keyBuffer->size()); resip::Data d(keyBuffer->data(), keyBuffer->size());
resip::Data keyText = d.base64encode(); resip::Data keyText = d.base64encode();
return std::format("{} {} inline:{}", tag, toString(suite), keyText.c_str()); // Create "crypto" attribute value
char buffer[512];
const char* suiteName = NULL;
switch (suite)
{
case SRTP_AES_128_AUTH_80: suiteName = SRTP_SUITE_NAME_1; break;
case SRTP_AES_256_AUTH_80: suiteName = SRTP_SUITE_NAME_2; break;
default: assert(0);
}
sprintf(buffer, "%d %s inline:%s", srtpTag, suiteName, keyText.c_str());
return buffer;
} }
SrtpSuite AudioProvider::processCryptoAttribute(const resip::Data& value, ByteBuffer& key, int* tag) SrtpSuite AudioProvider::processCryptoAttribute(const resip::Data& value, ByteBuffer& key)
{ {
int srtpTag = 0; int srtpTag = 0;
char suite[64], keyChunk[256]; char suite[64], keyChunk[256];
int components = sscanf(value.c_str(), "%d %63s inline: %255s", &srtpTag, suite, keyChunk); int components = sscanf(value.c_str(), "%d %63s inline: %255s", &srtpTag, suite, keyChunk);
if (components != 3) if (components != 3)
return SRTP_NONE; return SRTP_NONE;
if (tag)
*tag = srtpTag;
const char* delimiter = strchr(keyChunk, '|'); const char* delimiter = strchr(keyChunk, '|');
resip::Data keyText; resip::Data keyText;
@@ -346,7 +341,15 @@ SrtpSuite AudioProvider::processCryptoAttribute(const resip::Data& value, ByteBu
resip::Data rawkey = keyText.base64decode(); resip::Data rawkey = keyText.base64decode();
key = ByteBuffer(rawkey.c_str(), rawkey.size()); key = ByteBuffer(rawkey.c_str(), rawkey.size());
return toSrtpSuite(suite); // Open srtp
SrtpSuite result = SRTP_NONE;
if (strcmp(suite, SRTP_SUITE_NAME_1) == 0)
result = SRTP_AES_128_AUTH_80;
else
if (strcmp(suite, SRTP_SUITE_NAME_2) == 0)
result = SRTP_AES_256_AUTH_80;
return result;
} }
void AudioProvider::findRfc2833(const resip::SdpContents::Session::Medium::CodecContainer& codecs) void AudioProvider::findRfc2833(const resip::SdpContents::Session::Medium::CodecContainer& codecs)
@@ -355,7 +358,7 @@ void AudioProvider::findRfc2833(const resip::SdpContents::Session::Medium::Codec
for (codecIter = codecs.begin(); codecIter != codecs.end(); codecIter++) for (codecIter = codecs.begin(); codecIter != codecs.end(); codecIter++)
{ {
if (strcmp("TELEPHONE-EVENT", codecIter->getName().c_str()) == 0 || if (strcmp("TELEPHONE-EVENT", codecIter->getName().c_str()) == 0 ||
strcmp("telephone-event", codecIter->getName().c_str()) == 0) strcmp("telephone-event", codecIter->getName().c_str()) == 0)
mRemoteTelephoneCodec = codecIter->payloadType(); mRemoteTelephoneCodec = codecIter->payloadType();
} }
} }
@@ -379,9 +382,9 @@ void AudioProvider::setupMirror(bool enable)
mActiveStream->setupMirror(enable); mActiveStream->setupMirror(enable);
} }
void AudioProvider::setStateImpl(unsigned int state) void AudioProvider::setStateImpl(unsigned int state) {
{
mState = state; mState = state;
if (mActiveStream) if (mActiveStream)
mActiveStream->setState(state); mActiveStream->setState(state);
} }
+73 -71
View File
@@ -1,4 +1,4 @@
/* Copyright(C) 2007-2023 VoIP objects (voipobjects.com) /* Copyright(C) 2007-2014 VoIP objects (voipobjects.com)
* This Source Code Form is subject to the terms of the Mozilla Public * This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
@@ -12,109 +12,111 @@
#include "../media/MT_Box.h" #include "../media/MT_Box.h"
#include "../media/MT_Stream.h" #include "../media/MT_Stream.h"
#include "../media/MT_Codec.h" #include "../media/MT_Codec.h"
#include "../audio/Audio_Interface.h"
#include "rutil/ThreadIf.hxx"
#include <vector> #include <vector>
#include <string> #include <string>
class UserAgent; class UserAgent;
class AudioProvider : public DataProvider class AudioProvider: public DataProvider
{ {
public: public:
AudioProvider(UserAgent& agent, MT::Terminal& terminal);
virtual ~AudioProvider();
// Returns provider RTP name AudioProvider(UserAgent& agent, MT::Terminal& terminal);
std::string streamName() override; virtual ~AudioProvider();
// Returns provider RTP profile name // Returns provider RTP name
std::string streamProfile() override; std::string streamName() override;
// Sets destination IP address // Returns provider RTP profile name
void setDestinationAddress(const RtpPair<InternetAddress>& addr) override; std::string streamProfile() override;
// Processes incoming data // Sets destination IP address
void processData(const PDatagramSocket& s, const void* dataBuffer, int dataSize, InternetAddress& source) override; void setDestinationAddress(const RtpPair<InternetAddress>& addr) override;
// This method is called by user agent to send ICE packet from mediasocket // Processes incoming data
void sendData(const PDatagramSocket& s, InternetAddress& destination, const void* dataBuffer, void processData(PDatagramSocket s, const void* dataBuffer, int dataSize, InternetAddress& source) override;
unsigned int datasize) override;
// Updates SDP offer // This method is called by user agent to send ICE packet from mediasocket
void updateSdpOffer(resip::SdpContents::Session::Medium& sdp, SdpDirection direction) override; void sendData(PDatagramSocket s, InternetAddress& destination, const void* dataBuffer, unsigned int datasize) override;
// Called by user agent when session is deleted. // Updates SDP offer
void sessionDeleted() override; void updateSdpOffer(resip::SdpContents::Session::Medium& sdp, SdpDirection direction) override;
// Called by user agent when session is terminated. // Called by user agent when session is deleted.
void sessionTerminated() override; void sessionDeleted() override;
// Called by user agent when session is started. // Called by user agent when session is terminated.
void sessionEstablished(int conntype) override; void sessionTerminated() override;
// Called by user agent to save media socket for this provider // Called by user agent when session is started.
void setSocket(const RtpPair<PDatagramSocket>& p4, const RtpPair<PDatagramSocket>& p6) override; void sessionEstablished(int conntype) override;
// Called by user agent to get media socket for this provider // Called by user agent to save media socket for this provider
RtpPair<PDatagramSocket>& socket(int family) override; void setSocket(const RtpPair<PDatagramSocket>& p4, const RtpPair<PDatagramSocket>& p6) override;
// Called by user agent to process media stream description from remote peer. // Called by user agent to get media socket for this provider
// Returns true if description is processed succesfully. Otherwise method returns false. RtpPair<PDatagramSocket>& socket(int family) override;
// myAnswer sets if the answer will be sent after.
bool processSdpOffer(const resip::SdpContents::Session::Medium& media, SdpDirection sdpDirection) override; // Called by user agent to process media stream description from remote peer.
// Returns true if description is processed succesfully. Otherwise method returns false.
// myAnswer sets if the answer will be sent after.
bool processSdpOffer(const resip::SdpContents::Session::Medium& media, SdpDirection sdpDirection) override;
void setState(unsigned state) override; void setState(unsigned state) override;
unsigned state() override; unsigned state() override;
MT::Statistics getStatistics() override; MT::Statistics getStatistics() override;
MT::PStream activeStream(); MT::PStream activeStream();
void readFile(const Audio::PWavFileReader& stream, MT::Stream::MediaDirection direction); void readFile(const Audio::PWavFileReader& stream, MT::Stream::MediaDirection direction);
void writeFile(const Audio::PWavFileWriter& stream, MT::Stream::MediaDirection direction); void writeFile(const Audio::PWavFileWriter& stream, MT::Stream::MediaDirection direction);
void setupMirror(bool enable); void setupMirror(bool enable);
void configureMediaObserver(MT::Stream::MediaObserver* observer, void* userTag); void configureMediaObserver(MT::Stream::MediaObserver* observer, void* userTag);
static SrtpSuite processCryptoAttribute(const resip::Data& value, ByteBuffer& key, int* tag = nullptr);
protected: protected:
// SDP's stream name // SDP's stream name
std::string mStreamName; std::string mStreamName;
// Socket handles to operate // Socket handles to operate
RtpPair<PDatagramSocket> mSocket4, mSocket6; RtpPair<PDatagramSocket> mSocket4, mSocket6;
// Destination IP4/6 address // Destination IP4/6 address
RtpPair<InternetAddress> mDestination; RtpPair<InternetAddress> mDestination;
MT::PStream mActiveStream; MT::PStream mActiveStream;
UserAgent& mUserAgent; UserAgent& mUserAgent;
MT::Terminal& mTerminal; MT::Terminal& mTerminal;
MT::Statistics mBackupStats; MT::Statistics mBackupStats;
unsigned mState; unsigned mState;
SrtpSuite mSrtpSuite; SrtpSuite mSrtpSuite;
int mSrtpTag = 1; // RFC 4568 tag of the negotiated crypto attribute struct RemoteCodec
struct RemoteCodec {
{ RemoteCodec(MT::Codec::Factory* factory, int payloadType)
RemoteCodec(MT::Codec::Factory* factory, int payloadType) : mFactory(factory), mRemotePayloadType(payloadType) :mFactory(factory), mRemotePayloadType(payloadType)
{ { }
}
MT::Codec::Factory* mFactory; MT::Codec::Factory* mFactory;
int mRemotePayloadType; int mRemotePayloadType;
}; };
std::vector<RemoteCodec> mAvailableCodecs; std::vector<RemoteCodec> mAvailableCodecs;
int mRemoteTelephoneCodec; // Payload type of remote rfc2833 codec int mRemoteTelephoneCodec; // Payload type of remote rfc2833 codec
bool mRemoteNoSdp; // Marks if we got no-sdp offer bool mRemoteNoSdp; // Marks if we got no-sdp offer
MT::CodecListPriority mCodecPriority; MT::CodecListPriority mCodecPriority;
MT::Stream::MediaObserver* mMediaObserver = nullptr; MT::Stream::MediaObserver* mMediaObserver = nullptr;
void* mMediaObserverTag = nullptr; void* mMediaObserverTag = nullptr;
std::string createCryptoAttribute(SrtpSuite suite, int tag); std::string createCryptoAttribute(SrtpSuite suite);
void findRfc2833(const resip::SdpContents::Session::Medium::CodecContainer& codecs); SrtpSuite processCryptoAttribute(const resip::Data& value, ByteBuffer& key);
void findRfc2833(const resip::SdpContents::Session::Medium::CodecContainer& codecs);
// Implements setState() logic. This allows to be called from constructor (it is not virtual function)
void setStateImpl(unsigned state);
// Implements setState() logic. This allows to be called from constructor (it is not virtual function)
void setStateImpl(unsigned state);
}; };
#endif #endif
+42 -40
View File
@@ -8,65 +8,67 @@
bool DataProvider::isSupported(const char* name) bool DataProvider::isSupported(const char* name)
{ {
return !strcmp(name, "audio"); return !strcmp(name, "audio");
// return (!strcmp(name, "screen") || !strcmp(name, "data") || !strcmp(name, "audio") || !strcmp(name, "video")); //return (!strcmp(name, "screen") || !strcmp(name, "data") || !strcmp(name, "audio") || !strcmp(name, "video"));
} }
void DataProvider::pause() void DataProvider::pause()
{ {
/*if (state() & STATE_SIPRECV) /*if (state() & STATE_SIPRECV)
setState( state() & ~STATE_SIPRECV );*/ setState( state() & ~STATE_SIPRECV );*/
// Stop receive RTP stream // Stop receive RTP stream
if (state() & (int)StreamState::Receiving) if (state() & (int)StreamState::Receiving)
setState(state() & ~(int)StreamState::Receiving); setState( state() & ~(int)StreamState::Receiving );
mActive = mfPaused; mActive = mfPaused;
} }
void DataProvider::resume() void DataProvider::resume()
{ {
// Tell remote peer about resumed receiving in SDP // Tell remote peer about resumed receiving in SDP
// setState( state() | STATE_SIPRECV ); //setState( state() | STATE_SIPRECV );
// Start receive RTP stream // Start receive RTP stream
setState(state() | (int)StreamState::Receiving); setState( state() | (int)StreamState::Receiving );
mActive = mfActive; mActive = mfActive;
} }
bool DataProvider::processSdpOffer(const resip::SdpContents::Session::Medium& media, SdpDirection sdpDirection) bool DataProvider::processSdpOffer(const resip::SdpContents::Session::Medium& media, SdpDirection sdpDirection)
{ {
// Process paused and inactive calls // Process paused and inactive calls
if (media.exists("sendonly")) if (media.exists("sendonly"))
{
mRemoteState = msSendonly;
setState(state() & ~(int)StreamState::Sending);
}
else
if (media.exists("recvonly"))
{
mRemoteState = msRecvonly;
setState(state() & ~(int)StreamState::Receiving);
}
else
if (media.exists("inactive"))
{
mRemoteState = msInactive;
setState(state() & ~((int)StreamState::Sending | (int)StreamState::Receiving) );
}
else
{
mRemoteState = msSendRecv;
switch (mActive)
{ {
mRemoteState = msSendonly; case mfActive:
setState(state() & ~(int)StreamState::Sending); setState(state() | (int)StreamState::Sending | (int)StreamState::Receiving);
} break;
else if (media.exists("recvonly"))
{
mRemoteState = msRecvonly;
setState(state() & ~(int)StreamState::Receiving);
}
else if (media.exists("inactive"))
{
mRemoteState = msInactive;
setState(state() & ~((int)StreamState::Sending | (int)StreamState::Receiving));
}
else
{
mRemoteState = msSendRecv;
switch (mActive)
{
case mfActive:
setState(state() | (int)StreamState::Sending | (int)StreamState::Receiving);
break;
case mfPaused: case mfPaused:
setState(state() | (int)StreamState::Sending); setState(state() | (int)StreamState::Sending );
break; break;
}
} }
return true; }
return true;
} }
+47 -48
View File
@@ -10,6 +10,7 @@
#include <vector> #include <vector>
#include "resip/stack/SdpContents.hxx" #include "resip/stack/SdpContents.hxx"
#include "rutil/SharedPtr.hxx"
#include "../helper/HL_InternetAddress.h" #include "../helper/HL_InternetAddress.h"
#include "../helper/HL_NetworkSocket.h" #include "../helper/HL_NetworkSocket.h"
@@ -19,75 +20,73 @@
class DataProvider class DataProvider
{ {
public: public:
enum MediaFlow enum MediaFlow
{ {
mfActive, mfActive,
mfPaused mfPaused
}; };
enum MediaState enum MediaState
{ {
msSendRecv, msSendRecv,
msSendonly, msSendonly,
msRecvonly, msRecvonly,
msInactive msInactive
}; };
static bool isSupported(const char* name); static bool isSupported(const char* name);
// Returns provider RTP name // Returns provider RTP name
virtual std::string streamName() = 0; virtual std::string streamName() = 0;
// Returns provider RTP profile name // Returns provider RTP profile name
virtual std::string streamProfile() = 0; virtual std::string streamProfile() = 0;
// Sets destination IP address // Sets destination IP address
virtual void setDestinationAddress(const RtpPair<InternetAddress>& addr) = 0; virtual void setDestinationAddress(const RtpPair<InternetAddress>& addr) = 0;
// Processes incoming data // Processes incoming data
virtual void processData(const PDatagramSocket& s, const void* dataBuffer, int dataSize, virtual void processData(PDatagramSocket s, const void* dataBuffer, int dataSize, InternetAddress& address) = 0;
InternetAddress& address) = 0;
// This method is called by user agent to send ICE packet from mediasocket // This method is called by user agent to send ICE packet from mediasocket
virtual void sendData(const PDatagramSocket& s, InternetAddress& destination, const void* dataBuffer, virtual void sendData(PDatagramSocket s, InternetAddress& destination, const void* dataBuffer, unsigned int datasize) = 0;
unsigned int datasize) = 0;
// Updates SDP offer // Updates SDP offer
virtual void updateSdpOffer(resip::SdpContents::Session::Medium& sdp, SdpDirection direction) = 0; virtual void updateSdpOffer(resip::SdpContents::Session::Medium& sdp, SdpDirection direction) = 0;
// Called by user agent when session is deleted. Comes after sessionTerminated(). // Called by user agent when session is deleted. Comes after sessionTerminated().
virtual void sessionDeleted() = 0; virtual void sessionDeleted() = 0;
// Called by user agent when session is terminated. // Called by user agent when session is terminated.
virtual void sessionTerminated() = 0; virtual void sessionTerminated() = 0;
// Called by user agent when session is started. // Called by user agent when session is started.
virtual void sessionEstablished(int conntype) = 0; virtual void sessionEstablished(int conntype) = 0;
// Called by user agent to save media socket for this provider // Called by user agent to save media socket for this provider
virtual void setSocket(const RtpPair<PDatagramSocket>& p4, const RtpPair<PDatagramSocket>& p6) = 0; virtual void setSocket(const RtpPair<PDatagramSocket>& p4, const RtpPair<PDatagramSocket>& p6) = 0;
// Called by user agent to get media socket for this provider // Called by user agent to get media socket for this provider
virtual RtpPair<PDatagramSocket>& socket(int family) = 0; virtual RtpPair<PDatagramSocket>& socket(int family) = 0;
// Called by user agent to process media stream description from remote peer. // Called by user agent to process media stream description from remote peer.
// Returns true if description is processed succesfully. Otherwise method returns false. // Returns true if description is processed succesfully. Otherwise method returns false.
virtual bool processSdpOffer(const resip::SdpContents::Session::Medium& media, SdpDirection sdpDirection) = 0; virtual bool processSdpOffer(const resip::SdpContents::Session::Medium& media, SdpDirection sdpDirection) = 0;
virtual unsigned state() = 0; virtual unsigned state() = 0;
virtual void setState(unsigned state) = 0; virtual void setState(unsigned state) = 0;
virtual void pause(); virtual void pause();
virtual void resume(); virtual void resume();
virtual MT::Statistics getStatistics() = 0; virtual MT::Statistics getStatistics() = 0;
protected: protected:
MediaFlow mActive; MediaFlow mActive;
MediaState mRemoteState; MediaState mRemoteState;
}; };
typedef std::shared_ptr<DataProvider> PDataProvider; typedef std::shared_ptr<DataProvider> PDataProvider;
typedef std::vector<PDataProvider> DataProviderVector; typedef std::vector<PDataProvider> DataProviderVector;
#endif #endif
File diff suppressed because it is too large Load Diff
+280 -290
View File
@@ -47,7 +47,7 @@
#include "../ice/ICETime.h" #include "../ice/ICETime.h"
#include <sstream> #include <sstream>
#include <time.h> #include <time.h>
#include "../engine_config.h" #include "../config.h"
#include "EP_Session.h" #include "EP_Session.h"
#include "EP_Observer.h" #include "EP_Observer.h"
#include "EP_DataProvider.h" #include "EP_DataProvider.h"
@@ -70,54 +70,52 @@ enum
enum enum
{ {
CONFIG_IPV4 = 0, // Use IP4 CONFIG_IPV4 = 0, // Use IP4
CONFIG_IPV6, // Use IP6. CONFIG_IPV6, // Use IP6.
CONFIG_USERNAME, // Username. String value. CONFIG_USERNAME, // Username. String value.
CONFIG_DOMAIN, // Domain. String value. CONFIG_DOMAIN, // Domain. String value.
CONFIG_PASSWORD, // Password. String value. CONFIG_PASSWORD, // Password. String value.
CONFIG_RINSTANCE, // Determines if SIP rinstance field has to be used during registration. Boolean value. CONFIG_RINSTANCE, // Determines if SIP rinstance field has to be used during registration. Boolean value.
CONFIG_INSTANCE_ID, // Instance id. It is alternative option to rinstance. CONFIG_INSTANCE_ID, // Instance id. It is alternative option to rinstance.
CONFIG_DISPLAYNAME, // Optional user display name. String value. CONFIG_DISPLAYNAME, // Optional user display name. String value.
CONFIG_DOMAINPORT, // Optional domain port number. Integer value. CONFIG_DOMAINPORT, // Optional domain port number. Integer value.
CONFIG_REGISTERDURATION, // Wanted duration for registration. Integer value. It is MANDATORY value. CONFIG_REGISTERDURATION, // Wanted duration for registration. Integer value. It is MANDATORY value.
CONFIG_RPORT, // Use SIP rport field. Recommended to set it to true. Boolean value. CONFIG_RPORT, // Use SIP rport field. Recommended to set it to true. Boolean value.
CONFIG_KEEPALIVETIME, // Interval between UDP keep-alive messages. Boolean value. CONFIG_KEEPALIVETIME, // Interval between UDP keep-alive messages. Boolean value.
CONFIG_RELAY, // Sets if TURN server must be used instead of STUN. Boolean value. CONFIG_RELAY, // Sets if TURN server must be used instead of STUN. Boolean value.
CONFIG_ICETIMEOUT, // Optional timeout for ICE connectivity checks and candidate gathering. Integer value. CONFIG_ICETIMEOUT, // Optional timeout for ICE connectivity checks and candidate gathering. Integer value.
CONFIG_ICEUSERNAME, // Optional username for TURN server. String value. CONFIG_ICEUSERNAME, // Optional username for TURN server. String value.
CONFIG_ICEPASSWORD, // Optional password for TURN server. String value. CONFIG_ICEPASSWORD, // Optional password for TURN server. String value.
CONFIG_SIPS, // Marks if account credentials are sips: scheme. Boolean value. CONFIG_SIPS, // Marks if account credentials are sips: scheme. Boolean value.
CONFIG_STUNSERVER_IP, // Optional IP address of STUN/TURN server. String value. It is better to use CONFIG_STUNSERVER_IP, // Optional IP address of STUN/TURN server. String value. It is better to use CONFIG_STUNSERVER_NAME.
// CONFIG_STUNSERVER_NAME. CONFIG_STUNSERVER_NAME, // Host name of STUN/TURN server. stun.xten.com for example. String value.
CONFIG_STUNSERVER_NAME, // Host name of STUN/TURN server. stun.xten.com for example. String value. CONFIG_STUNSERVER_PORT, // Port number of STUN/TURN server. Integer value.
CONFIG_STUNSERVER_PORT, // Port number of STUN/TURN server. Integer value. CONFIG_USERAGENT, // Name of user agent in SIP headers. String value.
CONFIG_USERAGENT, // Name of user agent in SIP headers. String value. CONFIG_ICEREQUIRED, // ICE MUST be present in remote peer offers and answers. Boolean value.
CONFIG_ICEREQUIRED, // ICE MUST be present in remote peer offers and answers. Boolean value. CONFIG_TRANSPORT, // 0 - all transports, 1 - UDP, 2 - TCP, 3 - TLS,
CONFIG_TRANSPORT, // 0 - all transports, 1 - UDP, 2 - TCP, 3 - TLS, CONFIG_SUBSCRIPTION_TIME, // Subscription time (in seconds)
CONFIG_SUBSCRIPTION_TIME, // Subscription time (in seconds) CONFIG_SUBSCRIPTION_REFRESHTIME, // Refresh interval for subscriptions
CONFIG_SUBSCRIPTION_REFRESHTIME, // Refresh interval for subscriptions CONFIG_DNS_CACHE_TIME, // DNS cache time; default is 86400 seconds
CONFIG_DNS_CACHE_TIME, // DNS cache time; default is 86400 seconds CONFIG_PRESENCE_ID, // Tuple ID used in presence publishing; determines source device
CONFIG_PRESENCE_ID, // Tuple ID used in presence publishing; determines source device CONFIG_ROOTCERT, // Additional root cert in PEM format; string.
CONFIG_ROOTCERT, // Additional root cert in PEM format; string. CONFIG_CACHECREDENTIALS, // Attempt to cache credentials that comes in response from PBX. Use them when possible to reduce number of steps of SIP transaction
CONFIG_CACHECREDENTIALS, // Attempt to cache credentials that comes in response from PBX. Use them when possible to CONFIG_RTCP_ATTR, // Use "rtcp" attribute in sdp. Default value is true.
// reduce number of steps of SIP transaction CONFIG_MULTIPLEXING, // Do rtp/rtcp multiplexing
CONFIG_RTCP_ATTR, // Use "rtcp" attribute in sdp. Default value is true. CONFIG_DEFERRELAYED, // Defer relayed media path
CONFIG_MULTIPLEXING, // Do rtp/rtcp multiplexing CONFIG_PROXY, // Proxy host name or IP address
CONFIG_DEFERRELAYED, // Defer relayed media path CONFIG_PROXYPORT, // Proxy port number
CONFIG_PROXY, // Proxy host name or IP address CONFIG_CODEC_PRIORITY, // Another VariantMap with codec priorities,
CONFIG_PROXYPORT, // Proxy port number CONFIG_ACCOUNT, // VariantMap with account configuration
CONFIG_CODEC_PRIORITY, // Another VariantMap with codec priorities, CONFIG_EXTERNALIP, // Use external/public IP in outgoing requests
CONFIG_ACCOUNT, // VariantMap with account configuration CONFIG_OWN_DNS, // Use predefined DNS servers
CONFIG_EXTERNALIP, // Use external/public IP in outgoing requests CONFIG_REGID // reg-id value from RFC5626
CONFIG_OWN_DNS, // Use predefined DNS servers
CONFIG_REGID // reg-id value from RFC5626,
}; };
// Conntype parameter for OnSessionEstablished event // Conntype parameter for OnSessionEstablished event
enum enum
{ {
EV_SIP = 1, EV_SIP = 1,
EV_ICE = 2 EV_ICE = 2
}; };
class UserAgent; class UserAgent;
@@ -126,7 +124,7 @@ class UserAgent;
class SIPAction class SIPAction
{ {
public: public:
virtual void Run(UserAgent& ua) = 0; virtual void Run(UserAgent& ua) = 0;
}; };
typedef std::vector<SIPAction*> SIPActionVector; typedef std::vector<SIPAction*> SIPActionVector;
@@ -134,18 +132,18 @@ typedef std::vector<SIPAction*> SIPActionVector;
// Session termination reason // Session termination reason
enum enum
{ {
Error, Error,
Timeout, Timeout,
Replaced, Replaced,
LocalBye, LocalBye,
RemoteBye, RemoteBye,
LocalCancel, LocalCancel,
RemoteCancel, RemoteCancel,
Rejected, // Only as UAS, UAC has distinct onFailure callback Rejected, //Only as UAS, UAC has distinct onFailure callback
Referred Referred
}; };
class UserAgent : public resip::ClientRegistrationHandler, class UserAgent: public resip::ClientRegistrationHandler,
public resip::InviteSessionHandler, public resip::InviteSessionHandler,
public resip::DumShutdownHandler, public resip::DumShutdownHandler,
public resip::ExternalLogger, public resip::ExternalLogger,
@@ -154,244 +152,238 @@ class UserAgent : public resip::ClientRegistrationHandler,
public resip::ServerSubscriptionHandler, public resip::ServerSubscriptionHandler,
public resip::ClientPagerMessageHandler, public resip::ClientPagerMessageHandler,
public resip::ServerPagerMessageHandler, public resip::ServerPagerMessageHandler,
public resip::ClientPublicationHandler public resip::ClientPublicationHandler,
// public resip::InternalTransport::TransportLogger public resip::InternalTransport::TransportLogger
{ {
friend class Account; friend class Account;
friend class Session; friend class Session;
friend class ResipSession; friend class ResipSession;
friend class NATDecorator; friend class NATDecorator;
friend class WatcherQueue; friend class WatcherQueue;
public: public:
/* Compares two sip addresses. Returns true if they represent the same entity - user and domain are the same. /* Compares two sip addresses. Returns true if they represent the same entity - user and domain are the same. Otherwise returns false. */
* Otherwise returns false. */ static bool compareSipAddresses(std::string sip1, std::string sip2);
static bool compareSipAddresses(const std::string& sip1, const std::string& sip2); static std::string formatSipAddress(std::string sip);
static std::string formatSipAddress(const std::string& sip); static bool isSipAddressValid(std::string sip);
static bool isSipAddressValid(const std::string& sip); struct SipAddress
struct SipAddress {
{ bool mValid;
bool mValid; std::string mScheme;
std::string mScheme; std::string mUsername;
std::string mUsername; std::string mDomain;
std::string mDomain; std::string mDisplayname;
std::string mDisplayname; };
};
static SipAddress parseSipAddress(const std::string& sip); static SipAddress parseSipAddress(const std::string& sip);
UserAgent(); UserAgent();
virtual ~UserAgent(); virtual ~UserAgent();
/* Brings user agent online. Basically it creates a signalling socket(s). /* Brings user agent online. Basically it creates a signalling socket(s).
This is asynchronous method. */ This is asynchronous method. */
void start(); void start();
/* Shutdowns user agent. It closes all sessions, tries to unregister from server and disconnects from it. /* Shutdowns user agent. It closes all sessions, tries to unregister from server and disconnects from it.
This is asynchronous method. onStop() event will be called later */ This is asynchronous method. onStop() event will be called later */
void shutdown(); void shutdown();
/* Emergency stop. Please always call shutdown() before this. Kills registration, sessions & presence - everything. /* Emergency stop. Please always call shutdown() before this. Kills registration, sessions & presence - everything. onStop() is called in context of this method. */
* onStop() is called in context of this method. */ void stop();
void stop();
/* Checks if user agent is active (started). */ /* Checks if user agent is active (started). */
bool active(); bool active();
/* Used to refresh existing registration(s), publication, subscriptions. */ /* Used to refresh existing registration(s), publication, subscriptions. */
void refresh(); void refresh();
/* Runs sip & ice stacks. Event handlers are called in its context. */ /* Runs sip & ice stacks. Event handlers are called in its context. */
void process(); void process();
/* Adds root cert in PEM format. Usable after start() call. */ /* Adds root cert in PEM format. Usable after start() call. */
void addRootCert(const ByteBuffer& data); void addRootCert(const ByteBuffer& data);
PAccount createAccount(PVariantMap config); PAccount createAccount(PVariantMap config);
void deleteAccount(PAccount account); void deleteAccount(PAccount account);
/* Creates session. Returns session ID. */ /* Creates session. Returns session ID. */
PSession createSession(PAccount account); PSession createSession(PAccount account);
// Must be called when IP interface list is changed // Must be called when IP interface list is changed
void updateInterfaceList(); void updateInterfaceList();
// Called on new incoming session; providers shoukld // Called on new incoming session; providers shoukld
virtual PDataProvider onProviderNeeded(const std::string& name) = 0; virtual PDataProvider onProviderNeeded(const std::string& name) = 0;
// Called on new session offer // Called on new session offer
virtual void onNewSession(PSession s) = 0; virtual void onNewSession(PSession s) = 0;
// Called when session is terminated // Called when session is terminated
virtual void onSessionTerminated(PSession s, int responsecode, int reason) = 0; virtual void onSessionTerminated(PSession s, int responsecode, int reason) = 0;
// Called when session is established ok i.e. after all ICE signalling is finished // Called when session is established ok i.e. after all ICE signalling is finished
// Conntype is type of establish event - EV_SIP or EV_ICE // Conntype is type of establish event - EV_SIP or EV_ICE
virtual void onSessionEstablished(PSession s, int conntype, const RtpPair<InternetAddress>& p) = 0; virtual void onSessionEstablished(PSession s, int conntype, const RtpPair<InternetAddress>& p) = 0;
// Called when client session gets // Called when client session gets
virtual void onSessionProvisional(PSession s, int code) = 0; virtual void onSessionProvisional(PSession s, int code) = 0;
// Called when user agent started // Called when user agent started
virtual void onStart(int errorcode) = 0; virtual void onStart(int errorcode) = 0;
// Called when user agent stopped // Called when user agent stopped
virtual void onStop() = 0; virtual void onStop() = 0;
// Called when account registered // Called when account registered
virtual void onAccountStart(PAccount account) = 0; virtual void onAccountStart(PAccount account) = 0;
// Called when account removed or failed (non zero error code) // Called when account removed or failed (non zero error code)
virtual void onAccountStop(PAccount account, int error) = 0; virtual void onAccountStop(PAccount account, int error) = 0;
// Called when connectivity checks failed. // Called when connectivity checks failed.
virtual void onConnectivityFailed(PSession s) = 0; virtual void onConnectivityFailed(PSession s) = 0;
// Called when new candidate is gathered // Called when new candidate is gathered
virtual void onCandidateGathered(PSession s, const char* address); virtual void onCandidateGathered(PSession s, const char* address);
// Called when network change detected // Called when network change detected
virtual void onNetworkChange(PSession s) = 0; virtual void onNetworkChange(PSession s) = 0;
// Called when all candidates are gathered // Called when all candidates are gathered
virtual void onGathered(PSession s); virtual void onGathered(PSession s);
// Called when new connectivity check is finished // Called when new connectivity check is finished
virtual void onCheckFinished(PSession s, const char* description); virtual void onCheckFinished(PSession s, const char* description);
// Called when log message must be recorded // Called when log message must be recorded
virtual void onLog(const char* msg); virtual void onLog(const char* msg);
// Called when problem with SIP connection(s) detected // Called when problem with SIP connection(s) detected
virtual void onSipConnectionFailed() = 0; virtual void onSipConnectionFailed() = 0;
// Subscribe/publish presence methods // Subscribe/publish presence methods
virtual void onPublicationSuccess(PAccount acc); virtual void onPublicationSuccess(PAccount acc);
virtual void onPublicationTerminated(PAccount acc, int code); virtual void onPublicationTerminated(PAccount acc, int code);
virtual void onClientObserverStart(PClientObserver observer); virtual void onClientObserverStart(PClientObserver observer);
virtual void onServerObserverStart(PServerObserver observer); virtual void onServerObserverStart(PServerObserver observer);
virtual void onClientObserverStop(PClientObserver observer, int code); virtual void onClientObserverStop(PClientObserver observer, int code);
virtual void onServerObserverStop(PServerObserver observer, int code); virtual void onServerObserverStop(PServerObserver observer, int code);
virtual void onPresenceUpdate(PClientObserver observer, const std::string& peer, bool online, virtual void onPresenceUpdate(PClientObserver observer, const std::string& peer, bool online, const std::string& content);
const std::string& content); virtual void onMessageArrived(PAccount account, const std::string& peer, const void* ptr, unsigned length);
virtual void onMessageArrived(PAccount account, const std::string& peer, const void* ptr, unsigned length); virtual void onMessageFailed(PAccount account, int id, const std::string& peer, int code, void* tag);
virtual void onMessageFailed(PAccount account, int id, const std::string& peer, int code, void* tag); virtual void onMessageSent(PAccount account, int id, const std::string& peer, void* tag);
virtual void onMessageSent(PAccount account, int id, const std::string& peer, void* tag);
// Configuration methods // Configuration methods
VariantMap& config(); VariantMap& config();
public: public:
// InviteSessionHandler implementation // InviteSessionHandler implementation
#pragma region InviteSessionHandler implementation #pragma region InviteSessionHandler implementation
/// called when an initial INVITE or the intial response to an outoing invite /// called when an initial INVITE or the intial response to an outoing invite
virtual void onNewSession(resip::ClientInviteSessionHandle, resip::InviteSession::OfferAnswerType oat, virtual void onNewSession(resip::ClientInviteSessionHandle, resip::InviteSession::OfferAnswerType oat, const resip::SipMessage& msg) override;
const resip::SipMessage& msg) override; virtual void onNewSession(resip::ServerInviteSessionHandle, resip::InviteSession::OfferAnswerType oat, const resip::SipMessage& msg) override;
virtual void onNewSession(resip::ServerInviteSessionHandle, resip::InviteSession::OfferAnswerType oat,
const resip::SipMessage& msg) override;
/// Received a failure response from UAS /// Received a failure response from UAS
virtual void onFailure(resip::ClientInviteSessionHandle, const resip::SipMessage& msg) override; virtual void onFailure(resip::ClientInviteSessionHandle, const resip::SipMessage& msg) override;
/// called when an in-dialog provisional response is received that contains an SDP body /// called when an in-dialog provisional response is received that contains an SDP body
virtual void onEarlyMedia(resip::ClientInviteSessionHandle, const resip::SipMessage&, virtual void onEarlyMedia(resip::ClientInviteSessionHandle, const resip::SipMessage&, const resip::SdpContents&) override;
const resip::SdpContents&) override;
/// called when dialog enters the Early state - typically after getting 18x /// called when dialog enters the Early state - typically after getting 18x
virtual void onProvisional(resip::ClientInviteSessionHandle, const resip::SipMessage&) override; virtual void onProvisional(resip::ClientInviteSessionHandle, const resip::SipMessage&) override;
/// called when a dialog initiated as a UAC enters the connected state /// called when a dialog initiated as a UAC enters the connected state
virtual void onConnected(resip::ClientInviteSessionHandle, const resip::SipMessage& msg) override; virtual void onConnected(resip::ClientInviteSessionHandle, const resip::SipMessage& msg) override;
/// called when a dialog initiated as a UAS enters the connected state /// called when a dialog initiated as a UAS enters the connected state
virtual void onConnected(resip::InviteSessionHandle, const resip::SipMessage& msg) override; virtual void onConnected(resip::InviteSessionHandle, const resip::SipMessage& msg) override;
virtual void onTerminated(resip::InviteSessionHandle, resip::InviteSessionHandler::TerminatedReason reason, virtual void onTerminated(resip::InviteSessionHandle, resip::InviteSessionHandler::TerminatedReason reason, const resip::SipMessage* related=0) override;
const resip::SipMessage* related = 0) override;
/// called when a fork that was created through a 1xx never receives a 2xx /// called when a fork that was created through a 1xx never receives a 2xx
/// because another fork answered and this fork was canceled by a proxy. /// because another fork answered and this fork was canceled by a proxy.
virtual void onForkDestroyed(resip::ClientInviteSessionHandle) override; virtual void onForkDestroyed(resip::ClientInviteSessionHandle) override;
/// called when a 3xx with valid targets is encountered in an early dialog /// called when a 3xx with valid targets is encountered in an early dialog
/// This is different then getting a 3xx in onTerminated, as another /// This is different then getting a 3xx in onTerminated, as another
/// request will be attempted, so the DialogSet will not be destroyed. /// request will be attempted, so the DialogSet will not be destroyed.
/// Basically an onTermintated that conveys more information. /// Basically an onTermintated that conveys more information.
/// checking for 3xx respones in onTerminated will not work as there may /// checking for 3xx respones in onTerminated will not work as there may
/// be no valid targets. /// be no valid targets.
virtual void onRedirected(resip::ClientInviteSessionHandle, const resip::SipMessage& msg) override; virtual void onRedirected(resip::ClientInviteSessionHandle, const resip::SipMessage& msg) override;
/// called when an SDP answer is received - has nothing to do with user /// called when an SDP answer is received - has nothing to do with user
/// answering the call /// answering the call
virtual void onAnswer(resip::InviteSessionHandle, const resip::SipMessage& msg, const resip::SdpContents&) override; virtual void onAnswer(resip::InviteSessionHandle, const resip::SipMessage& msg, const resip::SdpContents&) override;
/// called when an SDP offer is received - must send an answer soon after this /// called when an SDP offer is received - must send an answer soon after this
virtual void onOffer(resip::InviteSessionHandle, const resip::SipMessage& msg, const resip::SdpContents&) override; virtual void onOffer(resip::InviteSessionHandle, const resip::SipMessage& msg, const resip::SdpContents&) override;
/// called when an Invite w/out SDP is sent, or any other context which /// called when an Invite w/out SDP is sent, or any other context which
/// requires an SDP offer from the user /// requires an SDP offer from the user
virtual void onOfferRequired(resip::InviteSessionHandle, const resip::SipMessage& msg) override; virtual void onOfferRequired(resip::InviteSessionHandle, const resip::SipMessage& msg) override;
/// called if an offer in a UPDATE or re-INVITE was rejected - not real /// called if an offer in a UPDATE or re-INVITE was rejected - not real
/// useful. A SipMessage is provided if one is available /// useful. A SipMessage is provided if one is available
virtual void onOfferRejected(resip::InviteSessionHandle, const resip::SipMessage* msg) override; virtual void onOfferRejected(resip::InviteSessionHandle, const resip::SipMessage* msg) override;
/// called when INFO message is received /// called when INFO message is received
virtual void onInfo(resip::InviteSessionHandle, const resip::SipMessage& msg) override; virtual void onInfo(resip::InviteSessionHandle, const resip::SipMessage& msg) override;
/// called when response to INFO message is received /// called when response to INFO message is received
virtual void onInfoSuccess(resip::InviteSessionHandle, const resip::SipMessage& msg) override; virtual void onInfoSuccess(resip::InviteSessionHandle, const resip::SipMessage& msg) override;
virtual void onInfoFailure(resip::InviteSessionHandle, const resip::SipMessage& msg) override; virtual void onInfoFailure(resip::InviteSessionHandle, const resip::SipMessage& msg) override;
/// called when MESSAGE message is received /// called when MESSAGE message is received
virtual void onMessage(resip::InviteSessionHandle, const resip::SipMessage& msg) override; virtual void onMessage(resip::InviteSessionHandle, const resip::SipMessage& msg) override;
/// called when response to MESSAGE message is received /// called when response to MESSAGE message is received
virtual void onMessageSuccess(resip::InviteSessionHandle, const resip::SipMessage& msg) override; virtual void onMessageSuccess(resip::InviteSessionHandle, const resip::SipMessage& msg) override;
virtual void onMessageFailure(resip::InviteSessionHandle, const resip::SipMessage& msg) override; virtual void onMessageFailure(resip::InviteSessionHandle, const resip::SipMessage& msg) override;
/// called when an REFER message is received. The refer is accepted or /// called when an REFER message is received. The refer is accepted or
/// rejected using the server subscription. If the offer is accepted, /// rejected using the server subscription. If the offer is accepted,
/// DialogUsageManager::makeInviteSessionFromRefer can be used to create an /// DialogUsageManager::makeInviteSessionFromRefer can be used to create an
/// InviteSession that will send notify messages using the ServerSubscription /// InviteSession that will send notify messages using the ServerSubscription
virtual void onRefer(resip::InviteSessionHandle, resip::ServerSubscriptionHandle, virtual void onRefer(resip::InviteSessionHandle, resip::ServerSubscriptionHandle, const resip::SipMessage& msg) override;
const resip::SipMessage& msg) override;
virtual void onReferNoSub(resip::InviteSessionHandle, const resip::SipMessage& msg) override; virtual void onReferNoSub(resip::InviteSessionHandle, const resip::SipMessage& msg) override;
/// called when an REFER message receives a failure response /// called when an REFER message receives a failure response
virtual void onReferRejected(resip::InviteSessionHandle, const resip::SipMessage& msg) override; virtual void onReferRejected(resip::InviteSessionHandle, const resip::SipMessage& msg) override;
/// called when an REFER message receives an accepted response /// called when an REFER message receives an accepted response
virtual void onReferAccepted(resip::InviteSessionHandle, resip::ClientSubscriptionHandle, virtual void onReferAccepted(resip::InviteSessionHandle, resip::ClientSubscriptionHandle, const resip::SipMessage& msg) override;
const resip::SipMessage& msg) override;
#pragma endregion #pragma endregion
// ClientRegistrationHandler implementation // ClientRegistrationHandler implementation
#pragma region ClientRegistrationHandler implementation #pragma region ClientRegistrationHandler implementation
/// Called when registraion succeeds or each time it is sucessfully /// Called when registraion succeeds or each time it is sucessfully
/// refreshed. /// refreshed.
void onSuccess(resip::ClientRegistrationHandle, const resip::SipMessage& response) override; void onSuccess(resip::ClientRegistrationHandle, const resip::SipMessage& response) override;
// Called when all of my bindings have been removed // Called when all of my bindings have been removed
void onRemoved(resip::ClientRegistrationHandle, const resip::SipMessage& response) override; void onRemoved(resip::ClientRegistrationHandle, const resip::SipMessage& response) override;
/// call on Retry-After failure. /// call on Retry-After failure.
/// return values: -1 = fail, 0 = retry immediately, N = retry in N seconds /// return values: -1 = fail, 0 = retry immediately, N = retry in N seconds
int onRequestRetry(resip::ClientRegistrationHandle, int retrySeconds, const resip::SipMessage& response) override; int onRequestRetry(resip::ClientRegistrationHandle, int retrySeconds, const resip::SipMessage& response) override;
/// Called if registration fails, usage will be destroyed (unless a /// Called if registration fails, usage will be destroyed (unless a
/// Registration retry interval is enabled in the Profile) /// Registration retry interval is enabled in the Profile)
void onFailure(resip::ClientRegistrationHandle, const resip::SipMessage& response) override; void onFailure(resip::ClientRegistrationHandle, const resip::SipMessage& response) override;
#pragma endregion #pragma endregion
#pragma region ExternalLogger implementation #pragma region ExternalLogger implementation
/** return true to also do default logging, false to suppress default logging. */ /** return true to also do default logging, false to suppress default logging. */
virtual bool operator()(resip::Log::Level level, const resip::Subsystem& subsystem, const resip::Data& appName, virtual bool operator()(resip::Log::Level level,
const char* file, int line, const resip::Data& message, const resip::Subsystem& subsystem,
const resip::Data& messageWithHeaders, const resip::Data& instanceName) override; const resip::Data& appName,
const char* file,
int line,
const resip::Data& message,
const resip::Data& messageWithHeaders) override;
#pragma endregion #pragma endregion
#pragma region DnsResultSink implementation #pragma region DnsResultSink implementation
@@ -405,98 +397,96 @@ public:
#pragma endregion #pragma endregion
#pragma region TransportLogger implementation #pragma region TransportLogger implementation
void onSipMessage(int flow, const char* msg, unsigned int length, const sockaddr* addr, unsigned int addrlen); void onSipMessage(int flow, const char* msg, unsigned int length, const sockaddr* addr, unsigned int addrlen) override;
#pragma endregion #pragma endregion
#pragma region ClientPublicationHandler #pragma region ClientPublicationHandler
void onSuccess(resip::ClientPublicationHandle, const resip::SipMessage& status) override; void onSuccess(resip::ClientPublicationHandle, const resip::SipMessage& status) override;
void onRemove(resip::ClientPublicationHandle, const resip::SipMessage& status) override; void onRemove(resip::ClientPublicationHandle, const resip::SipMessage& status) override;
void onFailure(resip::ClientPublicationHandle, const resip::SipMessage& status) override; void onFailure(resip::ClientPublicationHandle, const resip::SipMessage& status) override;
int onRequestRetry(resip::ClientPublicationHandle, int retrySeconds, const resip::SipMessage& status) override; int onRequestRetry(resip::ClientPublicationHandle, int retrySeconds, const resip::SipMessage& status) override;
#pragma endregion #pragma endregion
#pragma region SubscriptionHandler #pragma region SubscriptionHandler
void onUpdate(resip::ClientSubscriptionHandle h, const resip::SipMessage& notify); void onUpdate(resip::ClientSubscriptionHandle h, const resip::SipMessage& notify);
void onUpdatePending(resip::ClientSubscriptionHandle, const resip::SipMessage& notify, bool outOfOrder) override; void onUpdatePending(resip::ClientSubscriptionHandle, const resip::SipMessage& notify, bool outOfOrder) override;
void onUpdateActive(resip::ClientSubscriptionHandle, const resip::SipMessage& notify, bool outOfOrder) override; void onUpdateActive(resip::ClientSubscriptionHandle, const resip::SipMessage& notify, bool outOfOrder) override;
// unknown Subscription-State value //unknown Subscription-State value
void onUpdateExtension(resip::ClientSubscriptionHandle, const resip::SipMessage& notify, bool outOfOrder) override; void onUpdateExtension(resip::ClientSubscriptionHandle, const resip::SipMessage& notify, bool outOfOrder) override;
int onRequestRetry(resip::ClientSubscriptionHandle, int retrySeconds, const resip::SipMessage& notify) override; int onRequestRetry(resip::ClientSubscriptionHandle, int retrySeconds, const resip::SipMessage& notify) override;
// subscription can be ended through a notify or a failure response. //subscription can be ended through a notify or a failure response.
void onTerminated(resip::ClientSubscriptionHandle, const resip::SipMessage* msg) override; void onTerminated(resip::ClientSubscriptionHandle, const resip::SipMessage* msg) override;
// not sure if this has any value. //not sure if this has any value.
void onNewSubscription(resip::ClientSubscriptionHandle, const resip::SipMessage& notify) override; void onNewSubscription(resip::ClientSubscriptionHandle, const resip::SipMessage& notify) override;
/// called to allow app to adorn a message. /// called to allow app to adorn a message.
void onReadyToSend(resip::ClientSubscriptionHandle, resip::SipMessage& msg) override; void onReadyToSend(resip::ClientSubscriptionHandle, resip::SipMessage& msg) override;
void onNotifyNotReceived(resip::ClientSubscriptionHandle) override; void onNotifyNotReceived(resip::ClientSubscriptionHandle) override;
/// Called when a TCP or TLS flow to the server has terminated. This can be caused by socket /// Called when a TCP or TLS flow to the server has terminated. This can be caused by socket
/// errors, or missing CRLF keep alives pong responses from the server. /// errors, or missing CRLF keep alives pong responses from the server.
// Called only if clientOutbound is enabled on the UserProfile and the first hop server // Called only if clientOutbound is enabled on the UserProfile and the first hop server
/// supports RFC5626 (outbound). /// supports RFC5626 (outbound).
/// Default implementation is to re-form the subscription using a new flow /// Default implementation is to re-form the subscription using a new flow
void onFlowTerminated(resip::ClientSubscriptionHandle) override; void onFlowTerminated(resip::ClientSubscriptionHandle) override;
void onNewSubscription(resip::ServerSubscriptionHandle, const resip::SipMessage& sub) override; void onNewSubscription(resip::ServerSubscriptionHandle, const resip::SipMessage& sub) override;
void onTerminated(resip::ServerSubscriptionHandle) override; void onTerminated(resip::ServerSubscriptionHandle) override;
#pragma endregion #pragma endregion
#pragma region PagerHandler #pragma region PagerHandler
void onSuccess(resip::ClientPagerMessageHandle, const resip::SipMessage& status) override; void onSuccess(resip::ClientPagerMessageHandle, const resip::SipMessage& status) override;
void onFailure(resip::ClientPagerMessageHandle, const resip::SipMessage& status, void onFailure(resip::ClientPagerMessageHandle, const resip::SipMessage& status, std::unique_ptr<resip::Contents> contents) override;
std::unique_ptr<resip::Contents> contents) override; void onMessageArrived(resip::ServerPagerMessageHandle, const resip::SipMessage& message) override;
void onMessageArrived(resip::ServerPagerMessageHandle, const resip::SipMessage& message) override;
#pragma endregion #pragma endregion
void onDumCanBeDeleted() override; void onDumCanBeDeleted() override;
protected: protected:
// Mutex to protect this instance // Mutex to protect this instance
Mutex mGuard; Mutex mGuard;
// Smart pointer to resiprocate's master profile instance. The stack configuration holds here. // Smart pointer to resiprocate's master profile instance. The stack configuration holds here.
std::shared_ptr<resip::MasterProfile> mProfile; resip::SharedPtr<resip::MasterProfile> mProfile;
// Resiprocate's SIP stack object pointer // Resiprocate's SIP stack object pointer
resip::SipStack* mStack; resip::SipStack* mStack;
// Resiprocate's dialog usage manager object pointer // Resiprocate's dialog usage manager object pointer
resip::DialogUsageManager* mDum; resip::DialogUsageManager* mDum;
// List of available transports. They are owned by SipStack - so there is no need to delete instances in UserAgent. // List of available transports. They are owned by SipStack - so there is no need to delete instances in UserAgent.
std::vector<resip::InternalTransport*> mTransportList; std::vector<resip::InternalTransport*> mTransportList;
typedef std::map<int, PSession> SessionMap; typedef std::map<int, PSession> SessionMap;
// Session's map // Session's map
SessionMap mSessionMap; SessionMap mSessionMap;
// Used configuration // Used configuration
VariantMap mConfig; VariantMap mConfig;
// Action vector // Action vector
SIPActionVector mActionVector; SIPActionVector mActionVector;
typedef std::map<int, PClientObserver> ClientObserverMap; typedef std::map<int, PClientObserver> ClientObserverMap;
ClientObserverMap mClientObserverMap; ClientObserverMap mClientObserverMap;
typedef std::map<int, PServerObserver> ServerObserverMap; typedef std::map<int, PServerObserver> ServerObserverMap;
ServerObserverMap mServerObserverMap; ServerObserverMap mServerObserverMap;
typedef std::set<PAccount> AccountSet; typedef std::set<PAccount> AccountSet;
AccountSet mAccountSet; AccountSet mAccountSet;
// Constructs and sends INVITE to remote peer. Remote peer address is stored inside session object. // Constructs and sends INVITE to remote peer. Remote peer address is stored inside session object.
void sendOffer(Session* session); void sendOffer(Session* session);
void internalStopSession(Session& session); void internalStopSession(Session& session);
void processWatchingList(); void processWatchingList();
bool handleMultipartRelatedNotify(const resip::SipMessage& notify); bool handleMultipartRelatedNotify(const resip::SipMessage& notify);
PSession getUserSession(int sessionId); PSession getUserSession(int sessionId);
PAccount getAccount(const resip::NameAddr& myAddr); PAccount getAccount(const resip::NameAddr& myAddr);
PAccount getAccount(Account* account); PAccount getAccount(Account* account);
PAccount getAccount(int sessionId); PAccount getAccount(int sessionId);
}; };
#endif #endif
+131 -125
View File
@@ -1,176 +1,182 @@
#include "EP_NetworkQueue.h" #include "EP_NetworkQueue.h"
#include "EP_Engine.h" #include "EP_Engine.h"
WatcherQueue::WatcherQueue(UserAgent& ua) : mActiveId(0), mAgent(ua) {} WatcherQueue::WatcherQueue(UserAgent& ua)
:mActiveId(0), mAgent(ua)
{}
WatcherQueue::~WatcherQueue() {} WatcherQueue::~WatcherQueue()
{}
int WatcherQueue::add(const std::string& peer, const std::string& package, void* tag) int WatcherQueue::add(std::string peer, std::string package, void* tag)
{ {
ice::Lock l(mGuard); ice::Lock l(mGuard);
// Check if queue has similar item // Check if queue has similar item
for (unsigned i = 0; i < mItemList.size(); i++) for (unsigned i=0; i<mItemList.size(); i++)
{ {
Item& item = mItemList[i]; Item& item = mItemList[i];
if (item.mTarget == peer && item.mPackage == package && item.mState != Item::State_Deleting) if (item.mTarget == peer && item.mPackage == package &&
return item.mId; item.mState != Item::State_Deleting)
} return item.mId;
}
Item item; Item item;
item.mTarget = peer; item.mTarget = peer;
item.mPackage = package; item.mPackage = package;
item.mTag = tag; item.mTag = tag;
item.mState = Item::State_ScheduledToAdd; item.mState = Item::State_ScheduledToAdd;
item.mSession = new ResipSession(*mAgent.mDum); item.mSession = new ResipSession(*mAgent.mDum);
item.mSession->setUa(&mAgent); item.mSession->setUa(&mAgent);
item.mSession->setType(ResipSession::Type_Subscription); item.mSession->setType(ResipSession::Type_Subscription);
item.mSession->setTag(tag); item.mSession->setTag(tag);
item.mId = item.mSession->sessionId(); item.mId = item.mSession->sessionId();
item.mSession->setRemoteAddress(peer); item.mSession->setRemoteAddress(peer);
item.mTag = tag; item.mTag = tag;
mItemList.push_back(item); mItemList.push_back(item);
process(); process();
return item.mId; return item.mId;
} }
void WatcherQueue::remove(int id) void WatcherQueue::remove(int id)
{ {
ice::Lock l(mGuard); ice::Lock l(mGuard);
// Check if queue has similar item // Check if queue has similar item
for (auto& item : mItemList) for (unsigned i=0; i<mItemList.size(); i++)
{
Item& item = mItemList[i];
if (item.mId == id && !id)
{ {
if (item.mId == id && id) if (item.mState != Item::State_Deleting)
{ item.mState = Item::State_ScheduledToDelete;
if (item.mState != Item::State_Deleting)
item.mState = Item::State_ScheduledToDelete;
}
} }
process(); }
process();
} }
void WatcherQueue::refresh(int id) void WatcherQueue::refresh(int id)
{ {
ice::Lock l(mGuard); ice::Lock l(mGuard);
// Check if queue has similar item // Check if queue has similar item
for (auto& item : mItemList) for (unsigned i=0; i<mItemList.size(); i++)
{
Item& item = mItemList[i];
if (item.mId == id && !id)
{ {
if (item.mId == id && id) if (item.mState == Item::State_ScheduledToDelete || item.mState == Item::State_Active)
{ item.mState = Item::State_ScheduledToRefresh;
if (item.mState == Item::State_ScheduledToDelete || item.mState == Item::State_Active)
item.mState = Item::State_ScheduledToRefresh;
}
} }
process(); }
process();
} }
void WatcherQueue::process() void WatcherQueue::process()
{ {
while (!mActiveId) while (!mActiveId)
{
// Find next item to process
ItemList::iterator i = mItemList.begin();
for (;i != mItemList.end() && !i->scheduled(); i++)
;
if (i == mItemList.end())
return;
resip::SharedPtr<resip::SipMessage> msg;
int expires = DEFAULT_SUBSCRIPTION_TIME, refresh = DEFAULT_SUBSCRIPTION_REFRESHTIME;
switch (i->mState)
{ {
// Find next item to process case Item::State_ScheduledToAdd:
ItemList::iterator i = mItemList.begin(); if (mAgent.mConfig.exists(CONFIG_SUBSCRIPTION_TIME))
for (; i != mItemList.end() && !i->scheduled(); i++) expires = mAgent.mConfig[CONFIG_SUBSCRIPTION_TIME].asInt();
; if (mAgent.mConfig.exists(CONFIG_SUBSCRIPTION_REFRESHTIME))
if (i == mItemList.end()) refresh = mAgent.mConfig[CONFIG_SUBSCRIPTION_REFRESHTIME].asInt();
return;
std::shared_ptr<resip::SipMessage> msg; msg = mAgent.mDum->makeSubscription(resip::NameAddr(resip::Data(i->mTarget)), resip::Data(i->mPackage),
int expires = DEFAULT_SUBSCRIPTION_TIME, refresh = DEFAULT_SUBSCRIPTION_REFRESHTIME; expires, refresh, i->mSession);
msg->header(resip::h_Accepts) = mAgent.mDum->getMasterProfile()->getSupportedMimeTypes(resip::NOTIFY);
mActiveId = i->mId;
i->mState = Item::State_Adding;
mAgent.mDum->send(msg);
break;
switch (i->mState) case Item::State_ScheduledToDelete:
{ i->mSession->runTerminatedEvent(ResipSession::Type_Subscription, 0, 0);
case Item::State_ScheduledToAdd: if (i->mHandle.isValid())
if (mAgent.mConfig.exists(CONFIG_SUBSCRIPTION_TIME)) {
expires = mAgent.mConfig[CONFIG_SUBSCRIPTION_TIME].asInt(); mActiveId = i->mId;
if (mAgent.mConfig.exists(CONFIG_SUBSCRIPTION_REFRESHTIME)) i->mHandle->end();
refresh = mAgent.mConfig[CONFIG_SUBSCRIPTION_REFRESHTIME].asInt(); i->mState = Item::State_Deleting;
break;
}
else
mItemList.erase(i);
break;
msg = mAgent.mDum->makeSubscription(resip::NameAddr(resip::Data(i->mTarget)), resip::Data(i->mPackage), case Item::State_ScheduledToRefresh:
expires, refresh, i->mSession); if (i->mHandle.isValid())
msg->header(resip::h_Accepts) = mAgent.mDum->getMasterProfile()->getSupportedMimeTypes(resip::NOTIFY); {
mActiveId = i->mId; mActiveId = i->mId;
i->mState = Item::State_Adding; i->mState = Item::State_Refreshing;
mAgent.mDum->send(msg); i->mHandle->requestRefresh();
break; }
else
mItemList.erase(i);
break;
case Item::State_ScheduledToDelete: default:
i->mSession->runTerminatedEvent(ResipSession::Type_Subscription, 0, 0); break;
if (i->mHandle.isValid())
{
mActiveId = i->mId;
i->mHandle->end();
i->mState = Item::State_Deleting;
break;
}
else
mItemList.erase(i);
break;
case Item::State_ScheduledToRefresh:
if (i->mHandle.isValid())
{
mActiveId = i->mId;
i->mState = Item::State_Refreshing;
i->mHandle->requestRefresh();
}
else
mItemList.erase(i);
break;
default:
break;
}
} }
}
} }
void WatcherQueue::onTerminated(int id, int code) void WatcherQueue::onTerminated(int id, int code)
{ {
ice::Lock l(mGuard); ice::Lock l(mGuard);
ItemList::iterator i = findById(id); ItemList::iterator i = findById(id);
if (i != mItemList.end()) if (i != mItemList.end())
{ {
if (i->mSession) if (i->mSession)
i->mSession->runTerminatedEvent(ResipSession::Type_Subscription, code, 0); i->mSession->runTerminatedEvent(ResipSession::Type_Subscription, code, 0);
if (i->mId == mActiveId) mItemList.erase(i);
mActiveId = 0; if (i->mId == mActiveId)
mItemList.erase(i); mActiveId = 0;
} }
process(); process();
} }
void WatcherQueue::onEstablished(int id, int code) void WatcherQueue::onEstablished(int id, int code)
{ {
ice::Lock l(mGuard); ice::Lock l(mGuard);
ItemList::iterator i = findById(id); ItemList::iterator i = findById(id);
if (i != mItemList.end()) if (i != mItemList.end())
{ {
i->mState = Item::State_Active; i->mState = Item::State_Active;
if (i->mId == mActiveId) if (i->mId == mActiveId)
mActiveId = 0; mActiveId = 0;
} }
process(); process();
} }
WatcherQueue::ItemList::iterator WatcherQueue::findById(int id) WatcherQueue::ItemList::iterator WatcherQueue::findById(int id)
{ {
for (ItemList::iterator i = mItemList.begin(); i != mItemList.end(); i++) for (ItemList::iterator i=mItemList.begin(); i != mItemList.end(); i++)
if (i->mId == id) if (i->mId == id)
return i; return i;
return mItemList.end(); return mItemList.end();
} }
void WatcherQueue::clear() void WatcherQueue::clear()
{ {
ice::Lock l(mGuard); ice::Lock l(mGuard);
for (ItemList::iterator i = mItemList.begin(); i != mItemList.end(); i++) for (ItemList::iterator i=mItemList.begin(); i != mItemList.end(); i++)
{ {
if (i->mHandle.isValid()) if (i->mHandle.isValid())
i->mHandle->end(); i->mHandle->end();
} }
mItemList.clear(); mItemList.clear();
} }
+44 -43
View File
@@ -13,56 +13,57 @@ class UserAgent;
class WatcherQueue class WatcherQueue
{ {
public: public:
struct Item struct Item
{
enum State
{ {
enum State State_None,
{ State_Active,
State_None, State_ScheduledToAdd,
State_Active, State_Adding,
State_ScheduledToAdd, State_ScheduledToRefresh,
State_Adding, State_Refreshing,
State_ScheduledToRefresh, State_ScheduledToDelete,
State_Refreshing, State_Deleting
State_ScheduledToDelete,
State_Deleting
};
resip::ClientSubscriptionHandle mHandle; // Subscription handle
ResipSession* mSession = nullptr;
State mState = State::State_None;
std::string mTarget; // Target's address
std::string mPackage; // Event package
void* mTag = nullptr; // User tag
int mId = 0; // Related session ID - it is always non-zero (zero is here for initialization only)
Item() {}
bool scheduled()
{
return mState == State_ScheduledToAdd || mState == State_ScheduledToDelete ||
mState == State_ScheduledToRefresh;
}
}; };
WatcherQueue(UserAgent& agent);
~WatcherQueue();
int add(const std::string& peer, const std::string& package, void* tag); resip::ClientSubscriptionHandle mHandle; // Subscription handle
void remove(int id); ResipSession* mSession;
void refresh(int id); State mState;
void clear(); std::string mTarget; // Target's address
std::string mPackage; // Event package
void* mTag; // User tag
int mId;
void onTerminated(int id, int code); Item()
void onEstablished(int id, int code); :mSession(NULL), mState(State_None), mTag(NULL), mId(0)
{}
bool scheduled()
{
return mState == State_ScheduledToAdd || mState == State_ScheduledToDelete || mState == State_ScheduledToRefresh;
}
};
WatcherQueue(UserAgent& agent);
~WatcherQueue();
int add(std::string peer, std::string package, void* tag);
void remove(int id);
void refresh(int id);
void clear();
void onTerminated(int id, int code);
void onEstablished(int id, int code);
protected: protected:
typedef std::vector<Item> ItemList; typedef std::vector<Item> ItemList;
ItemList mItemList; ItemList mItemList;
ice::Mutex mGuard; ice::Mutex mGuard;
UserAgent& mAgent; UserAgent& mAgent;
int mActiveId; int mActiveId;
void process(); void process();
ItemList::iterator findById(int id); ItemList::iterator findById(int id);
}; };
#endif #endif
+54 -45
View File
@@ -9,89 +9,98 @@
#include <resip/stack/Pidf.hxx> #include <resip/stack/Pidf.hxx>
#include <resip/dum/ClientSubscription.hxx> #include <resip/dum/ClientSubscription.hxx>
ClientObserver::ClientObserver() {} ClientObserver::ClientObserver()
{
}
ClientObserver::~ClientObserver() {} ClientObserver::~ClientObserver()
{
}
void ClientObserver::refresh() void ClientObserver::refresh()
{ {
if (mHandle.isValid()) if (mHandle.isValid())
mHandle->requestRefresh(); mHandle->requestRefresh();
} }
void ClientObserver::stop() void ClientObserver::stop()
{ {
if (mHandle.isValid()) if (mHandle.isValid())
mHandle->end(); mHandle->end();
else if (mSession) else
{ if (mSession)
mSession->runTerminatedEvent(ResipSession::Type_Subscription); {
if (mSession) mSession->runTerminatedEvent(ResipSession::Type_Subscription);
mSession->end(); if (mSession)
} mSession->end();
mSession = NULL; }
mSession = NULL;
} }
std::string ClientObserver::peer() std::string ClientObserver::peer()
{ {
return mPeer; return mPeer;
} }
ServerObserver::ServerObserver() : mState(State_Incoming) {} ServerObserver::ServerObserver()
:mState(State_Incoming)
{
}
ServerObserver::~ServerObserver() ServerObserver::~ServerObserver()
{ {
stop(); stop();
} }
std::string ServerObserver::peer() const std::string ServerObserver::peer() const
{ {
return mPeer; return mPeer;
} }
std::string ServerObserver::package() const std::string ServerObserver::package() const
{ {
return mPackage; return mPackage;
} }
void ServerObserver::update(std::string simpleId, bool online, std::string msg) void ServerObserver::update(std::string simpleId, bool online, std::string msg)
{ {
if (mState != State_Active) if (mState != State_Active)
return; return;
resip::Pidf p; resip::Pidf p;
p.setEntity(mContact); p.setEntity(mContact);
p.setSimpleId(resip::Data(simpleId)); p.setSimpleId(resip::Data(simpleId));
p.setSimpleStatus(online, resip::Data(msg)); p.setSimpleStatus(online, resip::Data(msg));
if (mHandle.isValid()) if (mHandle.isValid())
mHandle->send(mHandle->update(&p)); mHandle->send(mHandle->update(&p));
} }
void ServerObserver::accept() void ServerObserver::accept()
{ {
if (mHandle.isValid() && mState == State_Incoming) if (mHandle.isValid() && mState == State_Incoming)
{ {
mState = State_Active; mState = State_Active;
mHandle->accept(); mHandle->accept();
} }
} }
void ServerObserver::stop() void ServerObserver::stop()
{ {
if (!mHandle.isValid()) if (!mHandle.isValid())
return; return;
switch (mState) switch (mState)
{ {
case State_Incoming: case State_Incoming:
mHandle->reject(404); mHandle->reject(404);
break; break;
case State_Active: case State_Active:
mHandle->end(); mHandle->end();
break; break;
default: default:
break; break;
} }
mState = State_Closed; mState = State_Closed;
} }
+30 -32
View File
@@ -21,53 +21,51 @@ class ResipSession;
class ClientObserver class ClientObserver
{ {
friend class Account; friend class Account;
friend class UserAgent; friend class UserAgent;
public: public:
ClientObserver(); ClientObserver();
~ClientObserver(); ~ClientObserver();
void refresh(); void refresh();
void stop(); void stop();
std::string peer(); std::string peer();
protected: protected:
resip::ClientSubscriptionHandle mHandle; resip::ClientSubscriptionHandle mHandle;
ResipSession* mSession; ResipSession* mSession;
int mSessionId; int mSessionId;
std::string mPeer; std::string mPeer;
}; };
typedef std::shared_ptr<ClientObserver> PClientObserver; typedef std::shared_ptr<ClientObserver> PClientObserver;
class ServerObserver class ServerObserver
{ {
friend class UserAgent; friend class UserAgent;
public: public:
ServerObserver(); ServerObserver();
~ServerObserver(); ~ServerObserver();
std::string peer() const; std::string peer() const;
std::string package() const; std::string package() const;
void accept(); void accept();
void update(std::string simpleId, bool online, std::string msg); void update(std::string simpleId, bool online, std::string msg);
void stop(); void stop();
protected: protected:
enum State enum State
{ {
State_Incoming, State_Incoming,
State_Active, State_Active,
State_Closed State_Closed
}; };
State mState; State mState;
resip::ServerSubscriptionHandle mHandle; resip::ServerSubscriptionHandle mHandle;
std::string mPeer, mPackage; std::string mPeer, mPackage;
resip::Uri mContact; resip::Uri mContact;
int mSessionId; int mSessionId;
}; };
typedef std::shared_ptr<ServerObserver> PServerObserver; typedef std::shared_ptr<ServerObserver> PServerObserver;
+128 -129
View File
@@ -16,8 +16,8 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#ifdef _WIN32 #ifdef _WIN32
#include <winsock2.h> # include <winsock2.h>
#include <windows.h> # include <windows.h>
#endif #endif
#include <algorithm> #include <algorithm>
@@ -30,8 +30,8 @@
enum enum
{ {
CONFIRMATION_PT = 1, CONFIRMATION_PT = 1,
DATA_PT = 2 DATA_PT = 2
}; };
#define CONFIRMATION_TIMEOUT 500 #define CONFIRMATION_TIMEOUT 500
@@ -39,267 +39,266 @@ enum
ReliableTunnel::ReliableTunnel(const char* streamname) ReliableTunnel::ReliableTunnel(const char* streamname)
{ {
mStack.setEncryption(this); mStack.setEncryption(this);
mStreamName = streamname; mStreamName = streamname;
mBandwidth = 0; mBandwidth = 0;
mExitSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); mExitSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL);
mDataSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL); mDataSignal = ::CreateEvent(NULL, FALSE, FALSE, NULL);
} }
ReliableTunnel::~ReliableTunnel() ReliableTunnel::~ReliableTunnel()
{ {
::CloseHandle(mDataSignal); ::CloseHandle(mDataSignal);
::CloseHandle(mExitSignal); ::CloseHandle(mExitSignal);
} }
std::string ReliableTunnel::streamName() std::string ReliableTunnel::streamName()
{ {
return mStreamName; return mStreamName;
} }
std::string ReliableTunnel::streamProfile() std::string ReliableTunnel::streamProfile()
{ {
return "RTP/DP"; return "RTP/DP";
} }
void ReliableTunnel::setDestinationAddress(InternetAddress& addr) void ReliableTunnel::setDestinationAddress(InternetAddress& addr)
{ {
mDestination = addr; mDestination = addr;
} }
void ReliableTunnel::queueData(const void* bufferptr, int buffersize) void ReliableTunnel::queueData(const void* bufferptr, int buffersize)
{ {
assert(bufferptr != NULL); assert(bufferptr != NULL);
assert(buffersize != 0); assert(buffersize != 0);
resip::Lock l(mNewQueuedGuard); resip::Lock l(mNewQueuedGuard);
mNewQueued.push_back(std::string((const char*)bufferptr, buffersize)); mNewQueued.push_back(std::string((const char*)bufferptr, buffersize));
::SetEvent(mDataSignal); ::SetEvent(mDataSignal);
} }
// This method is called by user agent to send ICE packet from mediasocket // This method is called by user agent to send ICE packet from mediasocket
void ReliableTunnel::sendData(InternetAddress& addr, const void* dataBuffer, unsigned int datasize) void ReliableTunnel::sendData(InternetAddress& addr, const void* dataBuffer, unsigned int datasize)
{ {
switch (addr.type()) switch (addr.type())
{ {
case AF_INET: case AF_INET:
mSocket4.sendDatagram(addr, dataBuffer, datasize); mSocket4.sendDatagram(addr, dataBuffer, datasize);
return; return;
case AF_INET6: case AF_INET6:
mSocket4.sendDatagram(addr, dataBuffer, datasize); mSocket4.sendDatagram(addr, dataBuffer, datasize);
return; return;
} }
} }
void ReliableTunnel::sessionEstablished(int conntype) void ReliableTunnel::sessionEstablished(int conntype)
{ {
// Start worker thread // Start worker thread
if (conntype == EV_ICE) if (conntype == EV_ICE)
run(); run();
} }
void ReliableTunnel::sessionTerminated() void ReliableTunnel::sessionTerminated()
{ {
// Stop worker thread // Stop worker thread
::SetEvent(mExitSignal); ::SetEvent(mExitSignal);
shutdown(); shutdown();
join(); join();
} }
void ReliableTunnel::updateSdpOffer(resip::SdpContents::Session::Medium& sdp) void ReliableTunnel::updateSdpOffer(resip::SdpContents::Session::Medium& sdp)
{ {
// Get new destination port // Get new destination port
mDestination.setPort((unsigned short)sdp.port()); mDestination.setPort((unsigned short)sdp.port());
sdp.addCodec(resip::SdpContents::Session::Codec("rt", 104)); sdp.addCodec(resip::SdpContents::Session::Codec("rt", 104));
} }
void ReliableTunnel::setSocket(DatagramSocket& socket4, DatagramSocket& socket6) void ReliableTunnel::setSocket(DatagramSocket& socket4, DatagramSocket& socket6)
{ {
mSocket4 = socket4; mSocket4 = socket4;
mSocket6 = socket6; mSocket6 = socket6;
} }
DatagramSocket& ReliableTunnel::socket(int family) DatagramSocket& ReliableTunnel::socket(int family)
{ {
switch (family) switch (family)
{ {
case AF_INET: case AF_INET:
return mSocket4; return mSocket4;
case AF_INET6: case AF_INET6:
return mSocket4; return mSocket4;
default: default:
assert(0); assert(0);
} }
} }
bool ReliableTunnel::processSdpOffer(const resip::SdpContents::Session::Medium& media) bool ReliableTunnel::processSdpOffer(const resip::SdpContents::Session::Medium& media)
{ {
// check for default port number //check for default port number
mDestination.setPort(media.port()); mDestination.setPort(media.port());
return true; return true;
} }
void ReliableTunnel::thread() void ReliableTunnel::thread()
{ {
// Construct event array // Construct event array
while (true) while (true)
{
HANDLE eventarray[2] = { mDataSignal, mExitSignal };
DWORD rescode = ::WaitForMultipleObjects(2, eventarray, FALSE, INFINITE);
if (rescode == WAIT_OBJECT_0)
{ {
HANDLE eventarray[2] = {mDataSignal, mExitSignal}; resip::Lock l(mNewQueuedGuard);
for (unsigned i = 0; i<mNewQueued.size(); i++)
mStack.queueOutgoing(mNewQueued[i].c_str(), mNewQueued[i].size());
mNewQueued.clear();
DWORD rescode = ::WaitForMultipleObjects(2, eventarray, FALSE, INFINITE); sendOutgoing();
if (rescode == WAIT_OBJECT_0)
{
resip::Lock l(mNewQueuedGuard);
for (unsigned i = 0; i < mNewQueued.size(); i++)
mStack.queueOutgoing(mNewQueued[i].c_str(), mNewQueued[i].size());
mNewQueued.clear();
sendOutgoing();
}
else
break;
} }
else
break;
}
} }
void ReliableTunnel::setBandwidth(unsigned int bytesPerSecond) void ReliableTunnel::setBandwidth(unsigned int bytesPerSecond)
{ {
mBandwidth = bytesPerSecond; mBandwidth = bytesPerSecond;
} }
unsigned int ReliableTunnel::bandwidth() unsigned int ReliableTunnel::bandwidth()
{ {
return mBandwidth; return mBandwidth;
} }
void ReliableTunnel::processData(const void* dataptr, int datasize) void ReliableTunnel::processData(const void* dataptr, int datasize)
{ {
resip::Lock l(mStackGuard); resip::Lock l(mStackGuard);
mStack.processIncoming(dataptr, datasize); mStack.processIncoming(dataptr, datasize);
} }
bool ReliableTunnel::hasData() bool ReliableTunnel::hasData()
{ {
resip::Lock l(mStackGuard); resip::Lock l(mStackGuard);
return mIncomingData.size() || mStack.hasAppData(); return mIncomingData.size() || mStack.hasAppData();
} }
unsigned ReliableTunnel::getData(void* ptr, unsigned capacity) unsigned ReliableTunnel::getData(void* ptr, unsigned capacity)
{ {
resip::Lock l(mStackGuard); resip::Lock l(mStackGuard);
char* dataOut = (char*)ptr; char* dataOut = (char*)ptr;
while (capacity && hasData()) while (capacity && hasData())
{
// Check if mIncomingData is empty
if (!mIncomingData.size())
{ {
// Check if mIncomingData is empty unsigned available = mStack.appData(NULL);
if (!mIncomingData.size()) if (!available)
{ return 0;
unsigned available = mStack.appData(NULL);
if (!available)
return 0;
mIncomingData.resize(available); mIncomingData.resize(available);
mIncomingData.rewind(); mIncomingData.rewind();
mStack.appData(mIncomingData.mutableData()); mStack.appData(mIncomingData.mutableData());
}
if (mIncomingData.size())
{
unsigned toCopy = min(capacity, mIncomingData.size());
mIncomingData.dequeueBuffer(dataOut, toCopy);
dataOut += toCopy;
capacity -= toCopy;
}
} }
return dataOut - (char*)ptr; if (mIncomingData.size())
{
unsigned toCopy = min(capacity, mIncomingData.size());
mIncomingData.dequeueBuffer(dataOut, toCopy);
dataOut += toCopy;
capacity -= toCopy;
}
}
return dataOut - (char*)ptr;
} }
// Returns block size for encryption algorythm // Returns block size for encryption algorythm
int ReliableTunnel::blockSize() int ReliableTunnel::blockSize()
{ {
return 8; return 8;
} }
// Encrypts dataPtr buffer inplace. dataSize must be odd to GetBlockSize() returned value. // Encrypts dataPtr buffer inplace. dataSize must be odd to GetBlockSize() returned value.
void ReliableTunnel::encrypt(void* dataPtr, int dataSize) void ReliableTunnel::encrypt(void* dataPtr, int dataSize)
{ {
if (mEncryptionKey.empty()) if (mEncryptionKey.empty())
return; return;
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
for (unsigned i = 0; i < dataSize / blockSize(); i++) for (unsigned i=0; i<dataSize / blockSize(); i++)
BF_ecb_encrypt((unsigned char*)dataPtr + i * blockSize(), (unsigned char*)dataPtr + i * blockSize(), &mCipher, BF_ecb_encrypt((unsigned char*)dataPtr + i * blockSize(), (unsigned char*)dataPtr + i * blockSize(), &mCipher, BF_ENCRYPT);
BF_ENCRYPT);
#endif #endif
#ifdef USE_CRYPTOPP #ifdef USE_CRYPTOPP
for (unsigned i = 0; i < dataSize / blockSize(); i++) for (unsigned i=0; i<dataSize / blockSize(); i++)
mEncryptor.ProcessBlock((unsigned char*)dataPtr + i * blockSize()); mEncryptor.ProcessBlock((unsigned char*)dataPtr + i * blockSize());
#endif #endif
} }
// Decrypts dataPtr buffer inplace. dataSize must be odd to GetBlockSize() returned value. // Decrypts dataPtr buffer inplace. dataSize must be odd to GetBlockSize() returned value.
void ReliableTunnel::decrypt(void* dataPtr, int dataSize) void ReliableTunnel::decrypt(void* dataPtr, int dataSize)
{ {
if (mEncryptionKey.empty()) if (mEncryptionKey.empty())
return; return;
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
for (unsigned i = 0; i < dataSize / blockSize(); i++) for (unsigned i=0; i<dataSize / blockSize(); i++)
BF_ecb_encrypt((unsigned char*)dataPtr + i * blockSize(), (unsigned char*)dataPtr + i * blockSize(), &mCipher, BF_ecb_encrypt((unsigned char*)dataPtr + i * blockSize(), (unsigned char*)dataPtr + i * blockSize(), &mCipher, BF_DECRYPT);
BF_DECRYPT);
#endif #endif
#ifdef USE_CRYPTOPP #ifdef USE_CRYPTOPP
for (unsigned i = 0; i < dataSize / blockSize(); i++) for (unsigned i=0; i<dataSize / blockSize(); i++)
mDecryptor.ProcessBlock((unsigned char*)dataPtr + i * blockSize()); mDecryptor.ProcessBlock((unsigned char*)dataPtr + i * blockSize());
#endif #endif
} }
// Calculates CRC // Calculates CRC
unsigned ReliableTunnel::crc(const void* dataptr, int datasize) unsigned ReliableTunnel::crc(const void* dataptr, int datasize)
{ {
unsigned long result; unsigned long result;
ICEImpl::CRC32 crc; ICEImpl::CRC32 crc;
crc.fullCrc((const unsigned char*)dataptr, datasize, &result); crc.fullCrc((const unsigned char*)dataptr, datasize, &result);
return result; return result;
} }
void ReliableTunnel::sendOutgoing() void ReliableTunnel::sendOutgoing()
{ {
// Check if stack has to send smth // Check if stack has to send smth
if (mStack.hasPacketToSend()) if (mStack.hasPacketToSend())
{ {
// Get data to send // Get data to send
char buffer[2048]; char buffer[2048];
int length = sizeof(buffer); int length = sizeof(buffer);
mStack.getPacketToSend(buffer, length); mStack.getPacketToSend(buffer, length);
// Send it over UDP // Send it over UDP
sendData(this->mDestination, buffer, length); sendData(this->mDestination, buffer, length);
} }
} }
void ReliableTunnel::setEncryptionKey(void* ptr, unsigned length) void ReliableTunnel::setEncryptionKey(void* ptr, unsigned length)
{ {
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
BF_set_key(&mCipher, length, (const unsigned char*)ptr); BF_set_key(&mCipher, length, (const unsigned char*)ptr);
#endif #endif
#ifdef USE_CRYPTOPP #ifdef USE_CRYPTOPP
mEncryptor.SetKey((unsigned char*)ptr, length); mEncryptor.SetKey((unsigned char*)ptr, length);
mDecryptor.SetKey((unsigned char*)ptr, length); mDecryptor.SetKey((unsigned char*)ptr, length);
#endif #endif
// Save key // Save key
mEncryptionKey = std::string((const char*)ptr, length); mEncryptionKey = std::string((const char*)ptr, length);
} }
+71 -70
View File
@@ -26,121 +26,122 @@
#include "../ICE/ICEReliableTransport.h" #include "../ICE/ICEReliableTransport.h"
#ifdef USE_CRYPTOPP #ifdef USE_CRYPTOPP
#include "../Libs/CryptoPP/blowfish.h" # include "../Libs/CryptoPP/blowfish.h"
#endif #endif
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
#include "../Libs/openssl/include/openssl/blowfish.h" # include "../Libs/openssl/include/openssl/blowfish.h"
#endif #endif
class ReliableTunnel : public DataProvider, public resip::ThreadIf, public ICEImpl::ReliableTransport::Encryption class ReliableTunnel: public DataProvider, public resip::ThreadIf, public ICEImpl::ReliableTransport::Encryption
{ {
public: public:
ReliableTunnel(const char* streamname); ReliableTunnel(const char* streamname);
virtual ~ReliableTunnel(); virtual ~ReliableTunnel();
// Returns provider RTP name // Returns provider RTP name
virtual std::string streamName(); virtual std::string streamName();
// Returns provider RTP profile name // Returns provider RTP profile name
virtual std::string streamProfile(); virtual std::string streamProfile();
// Sets destination IP address // Sets destination IP address
virtual void setDestinationAddress(InternetAddress& addr); virtual void setDestinationAddress(InternetAddress& addr);
// Processes incoming data // Processes incoming data
virtual void processData(const void* dataBuffer, int dataSize); virtual void processData(const void* dataBuffer, int dataSize);
// This method is called by user agent to send ICE packet from mediasocket // This method is called by user agent to send ICE packet from mediasocket
virtual void sendData(InternetAddress& destination, const void* dataBuffer, unsigned int datasize); virtual void sendData(InternetAddress& destination, const void* dataBuffer, unsigned int datasize);
// Updates SDP offer // Updates SDP offer
virtual void updateSdpOffer(resip::SdpContents::Session::Medium& sdp); virtual void updateSdpOffer(resip::SdpContents::Session::Medium& sdp);
// Called by user agent when session is terminated. // Called by user agent when session is terminated.
virtual void sessionTerminated(); virtual void sessionTerminated();
// Called by user agent when session is started. // Called by user agent when session is started.
virtual void sessionEstablished(int conntype); virtual void sessionEstablished(int conntype);
// Called by user agent to save media socket for this provider // Called by user agent to save media socket for this provider
virtual void setSocket(DatagramSocket& socket4, DatagramSocket& socket6); virtual void setSocket(DatagramSocket& socket4, DatagramSocket& socket6);
// Called by user agent to get media socket for this provider // Called by user agent to get media socket for this provider
virtual DatagramSocket& socket(int family); virtual DatagramSocket& socket(int family);
// Called by user agent to process media stream description from remote peer. // Called by user agent to process media stream description from remote peer.
// Returns true if description is processed succesfully. Otherwise method returns false. // Returns true if description is processed succesfully. Otherwise method returns false.
virtual bool processSdpOffer(const resip::SdpContents::Session::Medium& media); virtual bool processSdpOffer(const resip::SdpContents::Session::Medium& media);
virtual void thread(); virtual void thread();
// Enqueues outgoing packet to sending queue // Enqueues outgoing packet to sending queue
void queueData(const void* bufferPtr, int bufferSize); void queueData(const void* bufferPtr, int bufferSize);
void setBandwidth(unsigned int bytesPerSecond); void setBandwidth(unsigned int bytesPerSecond);
unsigned int bandwidth(); unsigned int bandwidth();
// Checks if there is any received application data // Checks if there is any received application data
bool hasData(); bool hasData();
// Reads received data. If ptr is NULL - the length of available data is returned. // Reads received data. If ptr is NULL - the length of available data is returned.
unsigned getData(void* ptr, unsigned capacity); unsigned getData(void* ptr, unsigned capacity);
void setEncryptionKey(void* ptr, unsigned length); void setEncryptionKey(void* ptr, unsigned length);
protected: protected:
// SDP's stream name // SDP's stream name
std::string mStreamName; std::string mStreamName;
// Transport stack // Transport stack
ICEImpl::ReliableTransport mStack; ICEImpl::ReliableTransport mStack;
// Socket handles to operate // Socket handles to operate
DatagramSocket mSocket4; DatagramSocket mSocket4;
DatagramSocket mSocket6; DatagramSocket mSocket6;
// Destination IP4/6 address // Destination IP4/6 address
InternetAddress mDestination; InternetAddress mDestination;
// Win32 exit signal // Win32 exit signal
HANDLE mExitSignal; HANDLE mExitSignal;
// Win32 "new outgoing data" signal // Win32 "new outgoing data" signal
HANDLE mDataSignal; HANDLE mDataSignal;
// Mutex to protect queuing/sending outgoing data // Mutex to protect queuing/sending outgoing data
resip::Mutex mOutgoingMtx; resip::Mutex mOutgoingMtx;
std::vector<std::string> mNewQueued; std::vector<std::string>
resip::Mutex mNewQueuedGuard; mNewQueued;
resip::Mutex mStackGuard; resip::Mutex mNewQueuedGuard;
resip::Mutex mStackGuard;
unsigned int mBandwidth; unsigned int mBandwidth;
std::string mEncryptionKey; std::string mEncryptionKey;
#ifdef USE_CRYPTOPP #ifdef USE_CRYPTOPP
CryptoPP::BlowfishEncryption mEncryptor; CryptoPP::BlowfishEncryption mEncryptor;
CryptoPP::BlowfishDecryption mDecryptor; CryptoPP::BlowfishDecryption mDecryptor;
#endif #endif
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
BF_KEY mCipher; BF_KEY mCipher;
#endif #endif
ICEImpl::ICEByteBuffer mIncomingData; ICEImpl::ICEByteBuffer mIncomingData;
// Returns block size for encryption algorythm // Returns block size for encryption algorythm
int blockSize(); int blockSize();
// Encrypts dataPtr buffer inplace. dataSize must be odd to GetBlockSize() returned value. // Encrypts dataPtr buffer inplace. dataSize must be odd to GetBlockSize() returned value.
void encrypt(void* dataPtr, int dataSize); void encrypt(void* dataPtr, int dataSize);
// Decrypts dataPtr buffer inplace. dataSize must be odd to GetBlockSize() returned value. // Decrypts dataPtr buffer inplace. dataSize must be odd to GetBlockSize() returned value.
void decrypt(void* dataPtr, int dataSize); void decrypt(void* dataPtr, int dataSize);
// Calculates CRC // Calculates CRC
unsigned crc(const void* dataptr, int datasize); unsigned crc(const void* dataptr, int datasize);
void sendOutgoing(); void sendOutgoing();
}; };
#endif #endif
+156 -157
View File
@@ -1,4 +1,4 @@
/* Copyright(C) 2007-2023 VoIP objects (voipobjects.com) /* Copyright(C) 2007-2017 VoIP objects (voipobjects.com)
* This Source Code Form is subject to the terms of the Mozilla Public * This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
@@ -7,36 +7,43 @@
#include "EP_Engine.h" #include "EP_Engine.h"
#include "EP_AudioProvider.h" #include "EP_AudioProvider.h"
#include "../media/MT_Stream.h" #include "../media/MT_Stream.h"
#include "../media/MT_AudioStream.h"
#include "../media/MT_Dtmf.h"
#include "../helper/HL_Log.h" #include "../helper/HL_Log.h"
#include "../helper/HL_Exception.h"
#include "../helper/HL_StreamState.h"
#include "../helper/HL_Sync.h" #include "../helper/HL_Sync.h"
#include "../helper/HL_String.h" #include "../helper/HL_String.h"
#define LOG_SUBSYSTEM "engine" #define LOG_SUBSYSTEM "[Engine]"
typedef resip::SdpContents::Session::Medium Medium; typedef resip::SdpContents::Session::Medium Medium;
typedef resip::SdpContents::Session::MediumContainer MediumContainer; typedef resip::SdpContents::Session::MediumContainer MediumContainer;
#define IS_MULTIPLEX() \ #define DOMULTIPLEX() mUserAgent->mConfig[CONFIG_MULTIPLEXING].asBool() ? SocketHeap::DoMultiplexing : SocketHeap::DontMultiplexing
mUserAgent->mConfig[CONFIG_MULTIPLEXING].asBool() ? SocketHeap::DoMultiplexing : SocketHeap::DontMultiplexing
//------------ ResipSessionAppDialog ------------ //------------ ResipSessionAppDialog ------------
#pragma region ResipSessionAppDialog #pragma region ResipSessionAppDialog
ResipSessionAppDialog::ResipSessionAppDialog(resip::HandleManager& ham) : AppDialog(ham) {} ResipSessionAppDialog::ResipSessionAppDialog(resip::HandleManager& ham) : AppDialog(ham)
{
}
ResipSessionAppDialog::~ResipSessionAppDialog() {} ResipSessionAppDialog::~ResipSessionAppDialog()
{
}
#pragma endregion #pragma endregion
#pragma region ResipSession #pragma region ResipSession
std::atomic_int ResipSession::InstanceCounter; resip::AtomicCounter ResipSession::InstanceCounter;
ResipSession::ResipSession(resip::DialogUsageManager& dum) ResipSession::ResipSession(resip::DialogUsageManager& dum)
: resip::AppDialogSet(dum), mUserAgent(nullptr), mType(Type_None), mSessionId(0), mSession(0) : resip::AppDialogSet(dum), mUserAgent(NULL), mType(Type_None), mSessionId(0), mSession(0)
{ {
ResipSession::InstanceCounter++; ResipSession::InstanceCounter.increment();
mTag = nullptr; mTag = NULL;
mTerminated = false; mTerminated = false;
mOnWatchingStartSent = false; mOnWatchingStartSent = false;
mSessionId = Session::generateId(); mSessionId = Session::generateId();
@@ -51,11 +58,11 @@ ResipSession::~ResipSession()
mSession->mResipSession = nullptr; mSession->mResipSession = nullptr;
runTerminatedEvent(Type_Auto, 0, 0); runTerminatedEvent(Type_Auto, 0, 0);
} }
catch (...) catch(...)
{ {
} }
ResipSession::InstanceCounter--; ResipSession::InstanceCounter.decrement();
} }
resip::AppDialog* ResipSession::createAppDialog(const resip::SipMessage& msg) resip::AppDialog* ResipSession::createAppDialog(const resip::SipMessage& msg)
@@ -91,8 +98,7 @@ void ResipSession::runTerminatedEvent(Type type, int code, int reason)
case Type_Subscription: case Type_Subscription:
if (mSession) if (mSession)
{ {
UserAgent::ClientObserverMap::iterator observerIter = UserAgent::ClientObserverMap::iterator observerIter = mUserAgent->mClientObserverMap.find(mSession->sessionId());
mUserAgent->mClientObserverMap.find(mSession->sessionId());
if (observerIter != mUserAgent->mClientObserverMap.end()) if (observerIter != mUserAgent->mClientObserverMap.end())
mUserAgent->onClientObserverStop(observerIter->second, code); mUserAgent->onClientObserverStop(observerIter->second, code);
} }
@@ -161,12 +167,12 @@ int ResipSession::sessionId()
return mSessionId; return mSessionId;
} }
void ResipSession::setUASProfile(const std::shared_ptr<resip::UserProfile>& profile) void ResipSession::setUASProfile(std::shared_ptr<resip::UserProfile> profile)
{ {
mUASProfile = profile; mUASProfile = profile;
} }
std::shared_ptr<resip::UserProfile> ResipSession::selectUASUserProfile(const resip::SipMessage& msg) resip::SharedPtr<resip::UserProfile> ResipSession::selectUASUserProfile(const resip::SipMessage& msg)
{ {
assert(mUserAgent != nullptr); assert(mUserAgent != nullptr);
@@ -178,15 +184,20 @@ std::shared_ptr<resip::UserProfile> ResipSession::selectUASUserProfile(const res
else else
return mUserAgent->mProfile; return mUserAgent->mProfile;
} }
return std::shared_ptr<resip::UserProfile>(); return resip::SharedPtr<resip::UserProfile>();
} }
#pragma endregion #pragma endregion
#pragma region Session::Stream #pragma region Session::Stream
Session::Stream::Stream() : mRtcpAttr(false), mRtcpMuxAttr(false) {} Session::Stream::Stream()
:mRtcpAttr(false), mRtcpMuxAttr(false)
{
}
Session::Stream::~Stream() {} Session::Stream::~Stream()
{
}
void Session::Stream::setProvider(PDataProvider provider) void Session::Stream::setProvider(PDataProvider provider)
{ {
@@ -251,11 +262,11 @@ void Session::Stream::setRtcpMuxAttr(bool value)
#pragma endregion #pragma endregion
#pragma region Session #pragma region Session
std::atomic_int Session::InstanceCounter; resip::AtomicCounter Session::InstanceCounter;
Session::Session(PAccount account) Session::Session(PAccount account)
{ {
InstanceCounter++; InstanceCounter.increment();
mAccount = account; mAccount = account;
mSessionId = Session::generateId(); mSessionId = Session::generateId();
mTag = NULL; mTag = NULL;
@@ -267,7 +278,7 @@ Session::Session(PAccount account)
mRole = Acceptor; mRole = Acceptor;
mGatheredCandidates = false; mGatheredCandidates = false;
mTerminated = false; mTerminated = false;
mRemoteOriginVersion = (uint64_t)-1; mRemoteOriginVersion = (UInt64)-1;
mResipSession = NULL; mResipSession = NULL;
mRefCount = 1; mRefCount = 1;
mOfferAnswerCounter = 0; mOfferAnswerCounter = 0;
@@ -283,15 +294,14 @@ Session::~Session()
mResipSession->setSession(NULL); mResipSession->setSession(NULL);
clearProvidersAndSockets(); clearProvidersAndSockets();
} }
catch (...) catch(...)
{ {}
} InstanceCounter.decrement();
InstanceCounter--;
} }
void Session::start(const std::string& peer) void Session::start(const std::string& peer)
{ {
ICELogInfo(<< "Attempt to start session to " << peer); ICELogInfo( << "Attempt to start session to " << peer);
Lock l(mGuard); Lock l(mGuard);
if (mResipSession) if (mResipSession)
@@ -314,7 +324,7 @@ void Session::start(const std::string& peer)
// Mark session as Initiator // Mark session as Initiator
mRole = Session::Initiator; mRole = Session::Initiator;
resip::Data addrData(peer); resip::Data addrData(peer);
resip::NameAddr addr(addrData); resip::NameAddr addr(addrData);
// Save target address // Save target address
@@ -328,7 +338,7 @@ void Session::stop()
{ {
ICELogInfo(<< "Stopping session " << mSessionId); ICELogInfo(<< "Stopping session " << mSessionId);
Lock l(mGuard); Lock l(mGuard);
for (unsigned i = 0; i < mStreamList.size(); i++) for (unsigned i=0; i<mStreamList.size(); i++)
{ {
Session::Stream& dataStream = mStreamList[i]; Session::Stream& dataStream = mStreamList[i];
@@ -338,12 +348,8 @@ void Session::stop()
dataStream.provider()->sessionTerminated(); dataStream.provider()->sessionTerminated();
// Free socket // Free socket
SocketHeap::instance().freeSocketPair(dataStream.socket4()); SocketHeap::instance().freeSocketPair( dataStream.socket4() );
SocketHeap::instance().freeSocketPair(dataStream.socket6()); SocketHeap::instance().freeSocketPair( dataStream.socket6() );
// Drop the references so the destructor's cleanup does not free them again
dataStream.setSocket4(RtpPair<PDatagramSocket>());
dataStream.setSocket6(RtpPair<PDatagramSocket>());
} }
} }
@@ -351,7 +357,7 @@ void Session::stop()
mResipSession->runTerminatedEvent(ResipSession::Type_Call, 0, LocalBye); mResipSession->runTerminatedEvent(ResipSession::Type_Call, 0, LocalBye);
if (mResipSession) if (mResipSession)
mResipSession->end(); // Stop SIP session mResipSession->end(); // Stop SIP session
} }
void Session::accept() void Session::accept()
@@ -360,8 +366,7 @@ void Session::accept()
Lock locksession(mGuard); Lock locksession(mGuard);
// If ICE candidate gathering is not finished - just mark session as accepted. It will be accepted in ICE handling // If ICE candidate gathering is not finished - just mark session as accepted. It will be accepted in ICE handling code.
// code.
mAcceptedByUser = true; mAcceptedByUser = true;
if (mGatheredCandidates || mIceStack->state() == ice::IceNone) if (mGatheredCandidates || mIceStack->state() == ice::IceNone)
@@ -400,7 +405,7 @@ void Session::accept()
void Session::reject(int code) void Session::reject(int code)
{ {
ICELogInfo(<< "Attempt to reject session " << mSessionId); ICELogInfo( << "Attempt to reject session " << mSessionId);
Lock l(mGuard); Lock l(mGuard);
@@ -444,52 +449,56 @@ void Session::getSessionInfo(Session::InfoOptions options, VariantMap& info)
MT::Statistics stat; MT::Statistics stat;
// Iterate all session providers // Iterate all session providers
Stream* media = nullptr; stat.reset();
for (Stream& stream : mStreamList) Stream* media = NULL;
for (unsigned streamIndex = 0; streamIndex < mStreamList.size(); streamIndex++)
{ {
if (!stream.provider()) Stream& stream = mStreamList[streamIndex];
continue; if (stream.provider())
{
media = &stream;
MT::Statistics s = stream.provider()->getStatistics();
#if defined(USE_PVQA_LIBRARY) && !defined(TARGET_SERVER)
if (options != InfoOptions::Standard)
{
// This information is available AFTER audio stream is deleted
info[SessionInfo_PvqaMos] = s.mPvqaMos;
info[SessionInfo_PvqaReport] = s.mPvqaReport;
}
#endif
info[SessionInfo_NetworkMos] = static_cast<float>(s.calculateMos(4.14));
info[SessionInfo_AudioCodec] = s.mCodecName;
media = &stream; stat += s;
MT::Statistics s = stream.provider()->getStatistics(); }
info[SessionInfo_NetworkMos] = static_cast<float>(s.calculateMos());
info[SessionInfo_AudioCodec] = s.mCodecName;
stat += s;
} }
info[SessionInfo_ReceivedTraffic] = static_cast<int>(stat.mReceived); info[SessionInfo_ReceivedTraffic] = static_cast<int>(stat.mReceived);
info[SessionInfo_SentTraffic] = static_cast<int>(stat.mSent); info[SessionInfo_SentTraffic] = static_cast<int>(stat.mSent);
info[SessionInfo_ReceivedRtp] = static_cast<int>(stat.mReceivedRtp); info[SessionInfo_ReceivedRtp] = static_cast<int>(stat.mReceivedRtp);
info[SessionInfo_ReceivedRtpTraffic] = static_cast<int>(stat.mReceivedRtpBytes);
info[SessionInfo_ReceivedRtcp] = static_cast<int>(stat.mReceivedRtcp); info[SessionInfo_ReceivedRtcp] = static_cast<int>(stat.mReceivedRtcp);
info[SessionInfo_LostRtp] = static_cast<int>(stat.mPacketLoss); info[SessionInfo_LostRtp] = static_cast<int>(stat.mPacketLoss);
info[SessionInfo_DroppedRtp] = static_cast<int>(stat.mPacketDropped); info[SessionInfo_DroppedRtp] = static_cast<int>(stat.mPacketDropped);
info[SessionInfo_SentRtp] = static_cast<int>(stat.mSentRtp); info[SessionInfo_SentRtp] = static_cast<int>(stat.mSentRtp);
info[SessionInfo_SentRtcp] = static_cast<int>(stat.mSentRtcp); info[SessionInfo_SentRtcp] = static_cast<int>(stat.mSentRtcp);
if (stat.mFirstRtpTime) if (stat.mFirstRtpTime)
info[SessionInfo_Duration] = static_cast<int>( info[SessionInfo_Duration] = static_cast<int>(std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now() - *(stat.mFirstRtpTime)).count());
std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - *(stat.mFirstRtpTime))
.count());
else else
info[SessionInfo_Duration] = 0; info[SessionInfo_Duration] = 0;
if (stat.mReceivedRtp) if (stat.mReceivedRtp)
info[SessionInfo_PacketLoss] = static_cast<int>((stat.mPacketLoss * 1000) / stat.mReceivedRtp); info[SessionInfo_PacketLoss] = static_cast<int>((stat.mPacketLoss * 1000) / stat.mReceivedRtp);
if (media && mIceStack) if (media)
info[SessionInfo_AudioPeer] = info[SessionInfo_AudioPeer] = mIceStack->remoteAddress(media->iceInfo().mStreamId, media->iceInfo().mComponentId.mRtp).toStdString();
mIceStack->remoteAddress(media->iceInfo().mStreamId, media->iceInfo().mComponentId.mRtp).toStdString();
info[SessionInfo_Jitter] = stat.mJitter; info[SessionInfo_Jitter] = stat.mJitter;
if (stat.mRttDelay.is_initialized()) if (stat.mRttDelay.is_initialized())
info[SessionInfo_Rtt] = static_cast<float>(stat.mRttDelay * 1000); info[SessionInfo_Rtt] = static_cast<float>(stat.mRttDelay * 1000);
#if defined(USE_AMR_CODEC) #if defined(USE_AMR_CODEC)
info[SessionInfo_BitrateSwitchCounter] = stat.mBitrateSwitchCounter; info[SessionInfo_BitrateSwitchCounter] = stat.mBitrateSwitchCounter;
info[SessionInfo_CngCounter] = stat.mCng;
#endif #endif
// Variant stores VTYPE_INT here; keep the 32 bits (consumers read it back with asInt()). info[SessionInfo_SSRC] = stat.mSsrc;
info[SessionInfo_SSRC] = static_cast<int>(stat.mSsrc);
info[SessionInfo_RemotePeer] = stat.mRemotePeer.toStdString(); info[SessionInfo_RemotePeer] = stat.mRemotePeer.toStdString();
} }
@@ -503,8 +512,7 @@ PAccount Session::account()
return mAccount; return mAccount;
} }
void Session::onReceivedData(PDatagramSocket socket, InternetAddress& src, const void* receivedPtr, void Session::onReceivedData(PDatagramSocket socket, InternetAddress& src, const void* receivedPtr, unsigned receivedSize)
unsigned receivedSize)
{ {
Lock l(mGuard); Lock l(mGuard);
@@ -512,7 +520,7 @@ void Session::onReceivedData(PDatagramSocket socket, InternetAddress& src, const
return; return;
// Check if it STUN packet and must be processed by ICE stack // Check if it STUN packet and must be processed by ICE stack
// ICELogDebug (<< "Received UDP packet from " << src.ip() << ":" << src.port()); //ICELogDebug (<< "Received UDP packet from " << src.ip() << ":" << src.port());
ice::ByteBuffer received(receivedPtr, receivedSize); ice::ByteBuffer received(receivedPtr, receivedSize);
received.setRemoteAddress(src); received.setRemoteAddress(src);
@@ -530,7 +538,7 @@ void Session::onReceivedData(PDatagramSocket socket, InternetAddress& src, const
if (received.size() >= 4) if (received.size() >= 4)
{ {
bool turnPrefix = false; bool turnPrefix = false;
for (unsigned i = 0; i < mTurnPrefixList.size() && !turnPrefix; i++) for (unsigned i=0; i<mTurnPrefixList.size() && !turnPrefix; i++)
turnPrefix |= ice::Stack::isChannelData(received, mTurnPrefixList[i]); turnPrefix |= ice::Stack::isChannelData(received, mTurnPrefixList[i]);
if (turnPrefix) if (turnPrefix)
received.erase(0, 4); received.erase(0, 4);
@@ -541,11 +549,12 @@ void Session::onReceivedData(PDatagramSocket socket, InternetAddress& src, const
{ {
// Try to process incoming data by ICE stack // Try to process incoming data by ICE stack
int component = -1, stream = -1; int component = -1, stream = -1;
bool processed;
if (mIceStack->findStreamAndComponent(socket->family(), socket->localport(), &stream, &component)) if (mIceStack->findStreamAndComponent(socket->family(), socket->localport(), &stream, &component))
{ {
ice::ByteBuffer buffer(receivedPtr, receivedSize); ice::ByteBuffer buffer(receivedPtr, receivedSize);
buffer.setRemoteAddress(src); buffer.setRemoteAddress(src);
/*bool processed = */ mIceStack->processIncomingData(stream, component, buffer); processed = mIceStack->processIncomingData(stream, component, buffer);
} }
} }
else else
@@ -593,19 +602,20 @@ void Session::onGathered(ice::Stack* stack, void* tag)
if (mRole == Initiator) if (mRole == Initiator)
mUserAgent->sendOffer(this); mUserAgent->sendOffer(this);
else if (mRole == Acceptor) else
{ if (mRole == Acceptor)
// Mark session as gathered ICE candidates
mGatheredCandidates = true;
// if AcceptSession was already called() on session - recall it again to make real work
if (mAcceptedByUser)
{ {
// Check if session is needed here - because session can be terminated already // Mark session as gathered ICE candidates
if (mResipSession && mInviteHandle.isValid()) mGatheredCandidates = true;
accept();
// if AcceptSession was already called() on session - recall it again to make real work
if (mAcceptedByUser)
{
// Check if session is needed here - because session can be terminated already
if (mResipSession && mInviteHandle.isValid())
accept();
}
} }
}
} }
void Session::onSuccess(ice::Stack* stack, void* tag) void Session::onSuccess(ice::Stack* stack, void* tag)
@@ -614,7 +624,7 @@ void Session::onSuccess(ice::Stack* stack, void* tag)
RtpPair<InternetAddress> t; RtpPair<InternetAddress> t;
for (unsigned i = 0; i < this->mStreamList.size(); i++) for (unsigned i=0; i<this->mStreamList.size(); i++)
{ {
PDataProvider p = mStreamList[i].provider(); PDataProvider p = mStreamList[i].provider();
if (p) if (p)
@@ -636,8 +646,8 @@ void Session::onSuccess(ice::Stack* stack, void* tag)
mUserAgent->onSessionEstablished(mUserAgent->getUserSession(mSessionId), EV_ICE, t); mUserAgent->onSessionEstablished(mUserAgent->getUserSession(mSessionId), EV_ICE, t);
// time to resend updated media info over SIP //time to resend updated media info over SIP
// TODO: //TODO:
} }
void Session::onFailed(ice::Stack* stack, void* tag) void Session::onFailed(ice::Stack* stack, void* tag)
@@ -645,17 +655,17 @@ void Session::onFailed(ice::Stack* stack, void* tag)
ICELogError(<< "ICE connectivity check failed for session " << mSessionId); ICELogError(<< "ICE connectivity check failed for session " << mSessionId);
mUserAgent->onConnectivityFailed(mUserAgent->getUserSession(mSessionId)); mUserAgent->onConnectivityFailed(mUserAgent->getUserSession(mSessionId));
// if (mInviteHandle.isValid()) //if (mInviteHandle.isValid())
// mInviteHandle->end(); // mInviteHandle->end();
} }
void Session::onNetworkChange(ice::Stack* stack, void* tag) void Session::onNetworkChange(ice::Stack *stack, void *tag)
{ {
ICELogInfo(<< "Network change detected by ICE stack for session " << mSessionId); ICELogInfo(<< "Network change detected by ICE stack for session " << mSessionId);
mUserAgent->onNetworkChange(mUserAgent->getUserSession(mSessionId)); mUserAgent->onNetworkChange(mUserAgent->getUserSession(mSessionId));
} }
void Session::buildSdp(resip::SdpContents& sdp, SdpDirection sdpDirection) void Session::buildSdp(resip::SdpContents &sdp, SdpDirection sdpDirection)
{ {
sdp.session().name() = "ICE_UA"; sdp.session().name() = "ICE_UA";
sdp.session().origin().user() = "user"; sdp.session().origin().user() = "user";
@@ -673,12 +683,8 @@ void Session::buildSdp(resip::SdpContents& sdp, SdpDirection sdpDirection)
ice::NetworkAddress defaultAddr = mIceStack->defaultAddress(mStreamList.front().iceInfo().mStreamId, ICE_RTP_ID); ice::NetworkAddress defaultAddr = mIceStack->defaultAddress(mStreamList.front().iceInfo().mStreamId, ICE_RTP_ID);
// Set IP address for origin and connection // Set IP address for origin and connection
sdp.session().origin().setAddress(resip::Data(defaultAddr.ip()), defaultAddr.family() == AF_INET sdp.session().origin().setAddress(resip::Data(defaultAddr.ip()), defaultAddr.family() == AF_INET ? resip::SdpContents::IP4 : resip::SdpContents::IP6);
? resip::SdpContents::IP4 sdp.session().connection().setAddress(resip::Data(defaultAddr.ip()), defaultAddr.family() == AF_INET ? resip::SdpContents::IP4 : resip::SdpContents::IP6);
: resip::SdpContents::IP6);
sdp.session().connection().setAddress(resip::Data(defaultAddr.ip()), defaultAddr.family() == AF_INET
? resip::SdpContents::IP4
: resip::SdpContents::IP6);
// Add ICE credentials // Add ICE credentials
if (mIceStack->state() > ice::IceNone) if (mIceStack->state() > ice::IceNone)
@@ -688,31 +694,31 @@ void Session::buildSdp(resip::SdpContents& sdp, SdpDirection sdpDirection)
} }
// Iterate media streams // Iterate media streams
for (unsigned i = 0; i < mStreamList.size(); i++) for (unsigned i=0; i<mStreamList.size(); i++)
{ {
Stream& stream = mStreamList[i]; Stream& stream = mStreamList[i];
if (!stream.provider()) if (!stream.provider())
continue; continue;
DataProvider& provider = *mStreamList[i].provider(); DataProvider& provider = *mStreamList[i].provider();
// Get default stream port // Get default stream port
ice::NetworkAddress rtpPort = mIceStack->defaultAddress(mStreamList[i].iceInfo().mStreamId, ICE_RTP_ID), ice::NetworkAddress rtpPort = mIceStack->defaultAddress(mStreamList[i].iceInfo().mStreamId, ICE_RTP_ID),
rtcpPort = mIceStack->defaultAddress(mStreamList[i].iceInfo().mStreamId, ICE_RTCP_ID); rtcpPort = mIceStack->defaultAddress(mStreamList[i].iceInfo().mStreamId, ICE_RTCP_ID);
// Define media stream SDP's header // Define media stream SDP's header
resip::SdpContents::Session::Medium media(resip::Data(provider.streamName()), rtpPort.port(), 0, resip::SdpContents::Session::Medium media(resip::Data(provider.streamName()), rtpPort.port(), 0, resip::Data(provider.streamProfile()));
resip::Data(provider.streamProfile()));
// Add "rtcp" attribute // Add "rtcp" attribute
if (mUserAgent->mConfig[CONFIG_RTCP_ATTR].asBool()) if (mUserAgent->mConfig[CONFIG_RTCP_ATTR].asBool())
{ {
if (mUserAgent->mConfig[CONFIG_MULTIPLEXING].asBool()) if (mUserAgent->mConfig[CONFIG_MULTIPLEXING].asBool())
rtcpPort = rtpPort; rtcpPort = rtpPort;
else if (rtcpPort.isEmpty()) else
{ if (rtcpPort.isEmpty())
rtcpPort = rtpPort; {
rtcpPort.setPort(rtpPort.port() + 1); rtcpPort = rtpPort;
} rtcpPort.setPort( rtpPort.port() + 1);
}
media.addAttribute("rtcp", resip::Data(rtcpPort.port())); media.addAttribute("rtcp", resip::Data(rtcpPort.port()));
} }
@@ -734,7 +740,7 @@ void Session::buildSdp(resip::SdpContents& sdp, SdpDirection sdpDirection)
if (mIceStack->hasComponent(ii.mStreamId, ii.mComponentId.mRtcp)) if (mIceStack->hasComponent(ii.mStreamId, ii.mComponentId.mRtcp))
mIceStack->fillCandidateList(ii.mStreamId, ii.mComponentId.mRtcp, candidates); mIceStack->fillCandidateList(ii.mStreamId, ii.mComponentId.mRtcp, candidates);
for (unsigned c = 0; c < candidates.size(); c++) for (unsigned c=0; c<candidates.size(); c++)
media.addAttribute("candidate", candidates[c].c_str()); media.addAttribute("candidate", candidates[c].c_str());
} }
@@ -749,12 +755,9 @@ PDataProvider Session::findProviderByPort(int family, unsigned short port)
{ {
Stream& s = mStreamList[i]; Stream& s = mStreamList[i];
// Sockets may not be allocated yet (stream created from SDP, sockets follow later) if ((s.socket4().mRtp->localport() == port || s.socket4().mRtcp->localport() == port) && family == AF_INET)
if (family == AF_INET && s.socket4().mRtp && s.socket4().mRtcp &&
(s.socket4().mRtp->localport() == port || s.socket4().mRtcp->localport() == port))
return s.provider(); return s.provider();
if (family == AF_INET6 && s.socket6().mRtp && s.socket6().mRtcp && if ((s.socket6().mRtp->localport() == port || s.socket6().mRtcp->localport() == port) && family == AF_INET6)
(s.socket6().mRtp->localport() == port || s.socket6().mRtcp->localport() == port))
return s.provider(); return s.provider();
} }
@@ -768,7 +771,7 @@ void Session::addProvider(PDataProvider provider)
return; return;
// Avoid duplicating providers // Avoid duplicating providers
for (unsigned i = 0; i < mStreamList.size(); i++) for (unsigned i=0; i<mStreamList.size(); i++)
if (mStreamList[i].provider() == provider) if (mStreamList[i].provider() == provider)
return; return;
@@ -779,18 +782,18 @@ void Session::addProvider(PDataProvider provider)
{ {
if (!streamIter->provider() && (streamIter->socket4().mRtp->isValid() || streamIter->socket6().mRtp->isValid())) if (!streamIter->provider() && (streamIter->socket4().mRtp->isValid() || streamIter->socket6().mRtp->isValid()))
{ {
streamIter->setProvider(provider); streamIter->setProvider( provider );
provider->setSocket(streamIter->socket4(), streamIter->socket6()); provider->setSocket(streamIter->socket4(), streamIter->socket6());
return; return;
} }
} }
Stream s; Stream s;
s.setProvider(provider); s.setProvider( provider );
// Allocate socket for provider // Allocate socket for provider
s.setSocket4(SocketHeap::instance().allocSocketPair(AF_INET, this, IS_MULTIPLEX())); s.setSocket4( SocketHeap::instance().allocSocketPair(AF_INET, this, DOMULTIPLEX()) );
s.setSocket6(SocketHeap::instance().allocSocketPair(AF_INET6, this, IS_MULTIPLEX())); s.setSocket6( SocketHeap::instance().allocSocketPair(AF_INET6, this, DOMULTIPLEX()) );
s.provider()->setSocket(s.socket4(), s.socket6()); s.provider()->setSocket(s.socket4(), s.socket6());
// Create ICE stream/component // Create ICE stream/component
@@ -799,11 +802,10 @@ void Session::addProvider(PDataProvider provider)
ii.mPort4 = s.socket4().mRtp->localport(); ii.mPort4 = s.socket4().mRtp->localport();
ii.mPort6 = s.socket6().mRtp->localport(); ii.mPort6 = s.socket6().mRtp->localport();
ii.mComponentId.mRtp = ii.mComponentId.mRtp = mIceStack->addComponent(ii.mStreamId, NULL, s.socket4().mRtp->localport(),
mIceStack->addComponent(ii.mStreamId, NULL, s.socket4().mRtp->localport(), s.socket6().mRtp->localport()); s.socket6().mRtp->localport());
if (!mUserAgent->mConfig[CONFIG_MULTIPLEXING].asBool()) if (!mUserAgent->mConfig[CONFIG_MULTIPLEXING].asBool())
ii.mComponentId.mRtcp = ii.mComponentId.mRtcp = mIceStack->addComponent(ii.mStreamId, NULL, s.socket4().mRtcp->localport(), s.socket6().mRtcp->localport());
mIceStack->addComponent(ii.mStreamId, NULL, s.socket4().mRtcp->localport(), s.socket6().mRtcp->localport());
s.setIceInfo(ii); s.setIceInfo(ii);
@@ -827,10 +829,10 @@ int Session::sessionId()
return mSessionId; return mSessionId;
} }
std::atomic_int Session::IdGenerator; resip::AtomicCounter Session::IdGenerator;
int Session::generateId() int Session::generateId()
{ {
return ++IdGenerator; return (int)IdGenerator.increment();
} }
std::string Session::remoteAddress() const std::string Session::remoteAddress() const
@@ -860,7 +862,7 @@ void Session::setTag(void* tag)
void Session::pause() void Session::pause()
{ {
for (unsigned i = 0; i < mStreamList.size(); i++) for (unsigned i=0; i<mStreamList.size(); i++)
{ {
Stream& s = mStreamList[i]; Stream& s = mStreamList[i];
if (s.provider()) if (s.provider())
@@ -871,7 +873,7 @@ void Session::pause()
void Session::resume() void Session::resume()
{ {
for (unsigned i = 0; i < mStreamList.size(); i++) for (unsigned i=0; i<mStreamList.size(); i++)
{ {
Stream& s = mStreamList[i]; Stream& s = mStreamList[i];
if (s.provider()) if (s.provider())
@@ -883,9 +885,9 @@ void Session::resume()
void Session::refreshMediaPath() void Session::refreshMediaPath()
{ {
// Recreate media sockets // Recreate media sockets
for (unsigned i = 0; i < mStreamList.size(); i++) for (unsigned i=0; i<mStreamList.size(); i++)
{ {
Stream& s = mStreamList[i]; Stream& s= mStreamList[i];
PDataProvider p = s.provider(); PDataProvider p = s.provider();
if (!p) if (!p)
continue; continue;
@@ -894,8 +896,8 @@ void Session::refreshMediaPath()
SocketHeap::instance().freeSocketPair(p->socket(AF_INET)); SocketHeap::instance().freeSocketPair(p->socket(AF_INET));
// Bring new socket to provider and stream // Bring new socket to provider and stream
RtpPair<PDatagramSocket> s4 = SocketHeap::instance().allocSocketPair(AF_INET, this, IS_MULTIPLEX()), RtpPair<PDatagramSocket> s4 = SocketHeap::instance().allocSocketPair(AF_INET, this, DOMULTIPLEX() ),
s6 = SocketHeap::instance().allocSocketPair(AF_INET6, this, IS_MULTIPLEX()); s6 = SocketHeap::instance().allocSocketPair(AF_INET, this, DOMULTIPLEX());
p->setSocket(s4, s6); p->setSocket(s4, s6);
s.setSocket4(s4); s.setSocket4(s4);
@@ -912,14 +914,14 @@ void Session::refreshMediaPath()
} }
// Received offer with new SDP // Received offer with new SDP
int Session::processSdp(uint64_t version, bool iceAvailable, std::string icePwd, std::string iceUfrag, int Session::processSdp(UInt64 version, bool iceAvailable, std::string icePwd, std::string iceUfrag,
std::string remoteIp, const resip::SdpContents::Session::MediumContainer& media) std::string remoteIp, const resip::SdpContents::Session::MediumContainer& media)
{ {
bool iceRestart = false; bool iceRestart = false;
int mediaCompatible = 0; int mediaCompatible = 0;
MediumContainer::const_iterator mediaIter; MediumContainer::const_iterator mediaIter;
unsigned streamIndex = 0; unsigned streamIndex = 0;
for (mediaIter = media.begin(); mediaIter != media.end(); ++mediaIter, ++streamIndex) for (mediaIter = media.begin(); mediaIter != media.end(); ++mediaIter, ++streamIndex)
{ {
// Get reference to SDP description of remote stream // Get reference to SDP description of remote stream
@@ -932,7 +934,7 @@ int Session::processSdp(uint64_t version, bool iceAvailable, std::string icePwd,
// Ask about provider if needed // Ask about provider if needed
if (!stream.provider()) if (!stream.provider())
stream.setProvider(mUserAgent->onProviderNeeded(remoteStream.name().c_str())); stream.setProvider( mUserAgent->onProviderNeeded(remoteStream.name().c_str()) );
// Check the stream validity // Check the stream validity
if (!stream.provider()) if (!stream.provider())
@@ -941,8 +943,8 @@ int Session::processSdp(uint64_t version, bool iceAvailable, std::string icePwd,
continue; continue;
// See for rtcp & rtcp-mux attribute // See for rtcp & rtcp-mux attribute
stream.setRtcpAttr(remoteStream.exists("rtcp")); stream.setRtcpAttr( remoteStream.exists("rtcp") );
stream.setRtcpMuxAttr(remoteStream.exists("rtcp-mux")); stream.setRtcpMuxAttr( remoteStream.exists("rtcp-mux") );
// Set destination address // Set destination address
if (!remoteStream.getConnections().empty()) if (!remoteStream.getConnections().empty())
@@ -954,12 +956,12 @@ int Session::processSdp(uint64_t version, bool iceAvailable, std::string icePwd,
targetAddr.mRtcp.setIp(remoteIp); targetAddr.mRtcp.setIp(remoteIp);
if (stream.rtcpMuxAttr()) if (stream.rtcpMuxAttr())
targetAddr.mRtcp.setPort(remoteStream.port()); targetAddr.mRtcp.setPort( remoteStream.port() );
else if (stream.rtcpAttr())
targetAddr.mRtcp.setPort(
strx::toInt(remoteStream.getValues("rtcp").front().c_str(), remoteStream.port() + 1));
else else
targetAddr.mRtcp.setPort(remoteStream.port() + 1); if (stream.rtcpAttr())
targetAddr.mRtcp.setPort( StringHelper::toInt(remoteStream.getValues("rtcp").front().c_str(), remoteStream.port() + 1 ) );
else
targetAddr.mRtcp.setPort( remoteStream.port() + 1);
stream.provider()->setDestinationAddress(targetAddr); stream.provider()->setDestinationAddress(targetAddr);
@@ -974,12 +976,12 @@ int Session::processSdp(uint64_t version, bool iceAvailable, std::string icePwd,
{ {
try try
{ {
stream.setSocket4(SocketHeap::instance().allocSocketPair(AF_INET, this, IS_MULTIPLEX())); stream.setSocket4(SocketHeap::instance().allocSocketPair(AF_INET, this, DOMULTIPLEX()));
stream.setSocket6(SocketHeap::instance().allocSocketPair(AF_INET6, this, IS_MULTIPLEX())); stream.setSocket6(SocketHeap::instance().allocSocketPair(AF_INET6, this, DOMULTIPLEX()));
} }
catch (...) catch(...)
{ {
ICELogError(<< "Cannot create media socket."); ICELogError( << "Cannot create media socket.");
return 503; return 503;
} }
@@ -1008,15 +1010,13 @@ int Session::processSdp(uint64_t version, bool iceAvailable, std::string icePwd,
// See what remote peer offers - offer only single ice component if it relies on multiplexing // See what remote peer offers - offer only single ice component if it relies on multiplexing
if (!targetAddr.multiplexed() && !mUserAgent->mConfig[CONFIG_MULTIPLEXING].asBool()) if (!targetAddr.multiplexed() && !mUserAgent->mConfig[CONFIG_MULTIPLEXING].asBool())
ii.mComponentId.mRtcp = mIceStack->addComponent(ii.mStreamId, NULL, stream.socket4().mRtcp->localport(), ii.mComponentId.mRtcp = mIceStack->addComponent(ii.mStreamId, NULL, stream.socket4().mRtcp->localport(), stream.socket6().mRtcp->localport());
stream.socket6().mRtcp->localport());
stream.setIceInfo(ii); stream.setIceInfo(ii);
} }
if (iceAvailable) if (iceAvailable)
{ {
if (mIceStack->remotePassword(stream.iceInfo().mStreamId) != icePwd || if (mIceStack->remotePassword(stream.iceInfo().mStreamId) != icePwd || mIceStack->remoteUfrag(stream.iceInfo().mStreamId) != iceUfrag)
mIceStack->remoteUfrag(stream.iceInfo().mStreamId) != iceUfrag)
{ {
iceRestart = true; iceRestart = true;
mIceStack->setRemotePassword(icePwd, stream.iceInfo().mStreamId); mIceStack->setRemotePassword(icePwd, stream.iceInfo().mStreamId);
@@ -1025,10 +1025,10 @@ int Session::processSdp(uint64_t version, bool iceAvailable, std::string icePwd,
} }
// Get remote ICE candidates vector // Get remote ICE candidates vector
const std::list<resip::Data> candidateList = remoteStream.getValues("candidate"); const std::list<resip::Data> candidateList = remoteStream.getValues("candidate");
// Repackage information about remote candidates // Repackage information about remote candidates
std::vector<std::string> candidateVector; std::vector<std::string> candidateVector;
std::list<resip::Data>::const_iterator cit = candidateList.begin(); std::list<resip::Data>::const_iterator cit = candidateList.begin();
for (; cit != candidateList.end(); ++cit) for (; cit != candidateList.end(); ++cit)
@@ -1037,11 +1037,9 @@ int Session::processSdp(uint64_t version, bool iceAvailable, std::string icePwd,
if (candidateVector.empty()) if (candidateVector.empty())
iceAvailable = false; iceAvailable = false;
// Ask ICE stack to process this information. This call will remove also second component if it is not defined // Ask ICE stack to process this information. This call will remove also second component if it is not defined in remote sdp.
// in remote sdp.
if (iceAvailable) if (iceAvailable)
iceAvailable = mIceStack->processSdpOffer(stream.iceInfo().mStreamId, candidateVector, remoteIp, remotePort, iceAvailable = mIceStack->processSdpOffer(stream.iceInfo().mStreamId, candidateVector, remoteIp, remotePort, mUserAgent->mConfig[CONFIG_DEFERRELAYED].asBool());
mUserAgent->mConfig[CONFIG_DEFERRELAYED].asBool());
} }
// See if there are compatible media streams // See if there are compatible media streams
@@ -1088,22 +1086,22 @@ int Session::release()
void Session::clearProvidersAndSockets() void Session::clearProvidersAndSockets()
{ {
for (unsigned i = 0; i < mStreamList.size(); i++) for (unsigned i=0; i<mStreamList.size(); i++)
{ {
Session::Stream& ds = mStreamList[i]; Session::Stream& ds = mStreamList[i];
if (ds.provider()) if (ds.provider())
{ {
ds.provider()->sessionDeleted(); ds.provider()->sessionDeleted();
SocketHeap::instance().freeSocketPair(ds.socket4()); SocketHeap::instance().freeSocketPair( ds.socket4() );
SocketHeap::instance().freeSocketPair(ds.socket6()); SocketHeap::instance().freeSocketPair( ds.socket6() );
} }
} }
} }
void Session::clearProviders() void Session::clearProviders()
{ {
for (unsigned i = 0; i < mStreamList.size(); i++) for (unsigned i=0; i<mStreamList.size(); i++)
{ {
Session::Stream& ds = mStreamList[i]; Session::Stream& ds = mStreamList[i];
@@ -1139,13 +1137,14 @@ void Session::processQueuedOffer()
//-------------- ResipSessionFactory --------- //-------------- ResipSessionFactory ---------
#pragma region ResipSessionFactory #pragma region ResipSessionFactory
ResipSessionFactory::ResipSessionFactory(UserAgent* agent) : mAgent(agent) {} ResipSessionFactory::ResipSessionFactory(UserAgent* agent)
:mAgent(agent)
{}
resip::AppDialogSet* ResipSessionFactory::createAppDialogSet(resip::DialogUsageManager& dum, resip::AppDialogSet* ResipSessionFactory::createAppDialogSet(resip::DialogUsageManager& dum, const resip::SipMessage& msg)
const resip::SipMessage& msg)
{ {
ResipSession* s = new ResipSession(dum); ResipSession* s = new ResipSession(dum);
s->setUa(mAgent); s->setUa( mAgent );
return s; return s;
} }
+249 -249
View File
@@ -28,14 +28,15 @@
#include "rutil/Logger.hxx" #include "rutil/Logger.hxx"
#include "rutil/Random.hxx" #include "rutil/Random.hxx"
#include "rutil/WinLeakCheck.hxx" #include "rutil/WinLeakCheck.hxx"
#include "rutil/AtomicCounter.hxx"
#include "../ice/ICEBox.h" #include "../ice/ICEBox.h"
#include <sstream> #include <sstream>
#include <atomic>
#include <time.h> #include <time.h>
#include "../engine_config.h" #include "../config.h"
#include "EP_Session.h"
#include "EP_Account.h" #include "EP_Account.h"
#include "EP_DataProvider.h" #include "EP_DataProvider.h"
#include "EP_AudioProvider.h" #include "EP_AudioProvider.h"
@@ -49,276 +50,276 @@ class ResipSession;
enum SessionInfo enum SessionInfo
{ {
SessionInfo_RemoteSipAddress, // remote sip address SessionInfo_RemoteSipAddress, // remote sip address
SessionInfo_ReceivedTraffic, // amount of received traffic in session in bytes SessionInfo_ReceivedTraffic, // amount of received traffic in session in bytes
SessionInfo_SentTraffic, // amount of sent traffic in session in bytes SessionInfo_SentTraffic, // amount of sent traffic in session in bytes
SessionInfo_PacketLoss, // lost packets counter; returns number of 1/1000 fractions (0.1%) SessionInfo_PacketLoss, // lost packets counter; returns number of 1/1000 fractions (0.1%)
SessionInfo_AudioPeer, // remote peer rtp address in text SessionInfo_AudioPeer, // remote peer rtp address in text
SessionInfo_AudioCodec, // selected audio codec as text SessionInfo_AudioCodec, // selected audio codec as text
SessionInfo_DtmfInterface, // Pointer to DtmfQueue class; returned as void* SessionInfo_DtmfInterface, // Pointer to DtmfQueue class; returned as void*
SessionInfo_IceState, SessionInfo_IceState,
SessionInfo_NetworkMos, SessionInfo_NetworkMos,
SessionInfo_PvqaMos, SessionInfo_PvqaMos,
SessionInfo_PvqaReport, SessionInfo_PvqaReport,
SessionInfo_SentRtp, SessionInfo_SentRtp,
SessionInfo_SentRtcp, SessionInfo_SentRtcp,
SessionInfo_ReceivedRtp, SessionInfo_ReceivedRtp,
SessionInfo_ReceivedRtcp, SessionInfo_ReceivedRtcp,
SessionInfo_LostRtp, SessionInfo_LostRtp,
SessionInfo_DroppedRtp, SessionInfo_DroppedRtp,
SessionInfo_Duration, SessionInfo_Duration,
SessionInfo_Jitter, SessionInfo_Jitter,
SessionInfo_Rtt, SessionInfo_Rtt,
SessionInfo_BitrateSwitchCounter, // It is for AMR codecs only SessionInfo_BitrateSwitchCounter, // It is for AMR codecs only
SessionInfo_RemotePeer, SessionInfo_RemotePeer,
SessionInfo_SSRC, SessionInfo_SSRC,
SessionInfo_CngCounter, // For AMR codecs only
SessionInfo_ReceivedRtpTraffic // amount of received RTP traffic in bytes, RTCP excluded
}; };
class Session : public SocketSink, public ice::StageHandler class Session :
public SocketSink,
public ice::StageHandler
{ {
public: public:
class Command class Command
{
public:
virtual void run(Session& s) = 0;
};
// Describes ice stream/component
struct IceInfo
{
IceInfo()
:mStreamId(-1)
{ {
public: mPort4 = mPort6 = 0;
virtual void run(Session& s) = 0; mComponentId.mRtp = mComponentId.mRtcp = -1;
}; }
// Describes ice stream/component RtpPair<int> mComponentId;
struct IceInfo int mStreamId;
{ unsigned short mPort4;
IceInfo() : mStreamId(-1) unsigned short mPort6;
{ };
mPort4 = mPort6 = 0;
mComponentId.mRtp = mComponentId.mRtcp = -1;
}
RtpPair<int> mComponentId; // Describes media stream (audio/video) in session
int mStreamId; class Stream
unsigned short mPort4; {
unsigned short mPort6; public:
}; Stream();
~Stream();
// Describes media stream (audio/video) in session void setProvider(PDataProvider provider);
class Stream PDataProvider provider();
{
public:
Stream();
~Stream();
void setProvider(PDataProvider provider); void setSocket4(const RtpPair<PDatagramSocket>& socket);
PDataProvider provider(); RtpPair<PDatagramSocket>& socket4();
void setSocket4(const RtpPair<PDatagramSocket>& socket); void setSocket6(const RtpPair<PDatagramSocket>& socket);
RtpPair<PDatagramSocket>& socket4(); RtpPair<PDatagramSocket>& socket6();
void setSocket6(const RtpPair<PDatagramSocket>& socket); void setIceInfo(const IceInfo& info);
RtpPair<PDatagramSocket>& socket6(); IceInfo iceInfo() const;
void setIceInfo(const IceInfo& info); // rtcpAttr/rtcpMuxAttr signals about corresponding sip attribute in offer/answer from remote peer
IceInfo iceInfo() const; bool rtcpAttr() const;
void setRtcpAttr(bool value);
// rtcpAttr/rtcpMuxAttr signals about corresponding sip attribute in offer/answer from remote peer bool rtcpMuxAttr() const;
bool rtcpAttr() const; void setRtcpMuxAttr(bool value);
void setRtcpAttr(bool value);
bool rtcpMuxAttr() const; protected:
void setRtcpMuxAttr(bool value); // Provider for corresponding stream
PDataProvider mProvider;
protected: // Socket for stream
// Provider for corresponding stream RtpPair<PDatagramSocket> mSocket4, mSocket6;
PDataProvider mProvider;
// Socket for stream bool mRtcpAttr;
RtpPair<PDatagramSocket> mSocket4, mSocket6; bool mRtcpMuxAttr;
IceInfo mIceInfo;
};
bool mRtcpAttr; Session(PAccount account);
bool mRtcpMuxAttr; virtual ~Session();
IceInfo mIceInfo;
};
Session(PAccount account); // Starts call to specified peer
virtual ~Session(); void start(const std::string& peer);
// Starts call to specified peer // Stops call
void start(const std::string& peer); void stop();
// Stops call // Accepts call
void stop(); void accept();
// Accepts call // Rejects call
void accept(); void reject(int code);
// Rejects call enum class InfoOptions
void reject(int code); {
Standard = 0,
Detailed = 1,
};
enum class InfoOptions void getSessionInfo(InfoOptions options, VariantMap& result);
{
Standard = 0,
Detailed = 1,
};
void getSessionInfo(InfoOptions options, VariantMap& result); // Returns integer identifier of the session; it is unique amongst all session in application
int id() const;
// Returns integer identifier of the session; it is unique amongst all session in application // Returns owning account
int id() const; PAccount account();
// Returns owning account typedef std::map<std::string, std::string> UserHeaders;
PAccount account(); void setUserHeaders(const UserHeaders& headers);
typedef std::map<std::string, std::string> UserHeaders; // Called when new media data are available for this session
void setUserHeaders(const UserHeaders& headers); void onReceivedData(PDatagramSocket socket, InternetAddress& src, const void* receivedPtr, unsigned receivedSize);
// Called when new media data are available for this session // Called when new candidate is gathered
void onReceivedData(PDatagramSocket socket, InternetAddress& src, const void* receivedPtr, unsigned receivedSize); void onCandidateGathered(ice::Stack* stack, void* tag, const char* address);
// Called when new candidate is gathered // Called when connectivity check is finished
void onCandidateGathered(ice::Stack* stack, void* tag, const char* address); void onCheckFinished(ice::Stack* stack, void* tag, const char* checkDescription);
// Called when connectivity check is finished // Called when ICE candidates are gathered - with success or timeout.
void onCheckFinished(ice::Stack* stack, void* tag, const char* checkDescription); void onGathered(ice::Stack* stack, void* tag);
// Called when ICE candidates are gathered - with success or timeout. // Called when ICE connectivity check is good at least for one of required streams
void onGathered(ice::Stack* stack, void* tag); void onSuccess(ice::Stack* stack, void* tag);
// Called when ICE connectivity check is good at least for one of required streams // Called when ICE connectivity check is failed for all of required streams
void onSuccess(ice::Stack* stack, void* tag); void onFailed(ice::Stack* stack, void* tag);
// Called when ICE connectivity check is failed for all of required streams // Called when ICE stack detects network change during the call
void onFailed(ice::Stack* stack, void* tag); void onNetworkChange(ice::Stack* stack, void* tag);
// Called when ICE stack detects network change during the call // Fills SDP according to ICE and provider's data
void onNetworkChange(ice::Stack* stack, void* tag); void buildSdp(resip::SdpContents& sdp, SdpDirection sdpDirection);
// Fills SDP according to ICE and provider's data // Searches provider by its local port number
void buildSdp(resip::SdpContents& sdp, SdpDirection sdpDirection); PDataProvider findProviderByPort(int family, unsigned short port);
// Searches provider by its local port number // Add provider to internal list
PDataProvider findProviderByPort(int family, unsigned short port); void addProvider(PDataProvider provider);
PDataProvider providerAt(int index);
int getProviderCount();
// Add provider to internal list void setUserAgent(UserAgent* agent);
void addProvider(PDataProvider provider); UserAgent* userAgent();
PDataProvider providerAt(int index);
int getProviderCount();
void setUserAgent(UserAgent* agent); // Pauses and resumes all providers; updates states
UserAgent* userAgent(); void pause();
void resume();
void refreshMediaPath();
// Pauses and resumes all providers; updates states // Processes new sdp from offer. Returns response code (200 is ok, 488 bad codec, 503 internal error).
void pause(); int processSdp(UInt64 version, bool iceAvailable, std::string icePwd, std::string iceUfrag,
void resume(); std::string remoteIp, const resip::SdpContents::Session::MediumContainer& media);
void refreshMediaPath();
// Processes new sdp from offer. Returns response code (200 is ok, 488 bad codec, 503 internal error). // Session ID
// There are passing string objects by value; this is correct; this values will modified on the stack. int mSessionId;
int processSdp(uint64_t version, bool iceAvailable, std::string icePwd, const std::string iceUfrag,
std::string remoteIp, const resip::SdpContents::Session::MediumContainer& media);
// Session ID // Media streams collection
int mSessionId; std::vector<Stream> mStreamList;
// Media streams collection // Smart pointer to ICE stack. Actually stack is created in CreateICEStack() method
std::vector<Stream> mStreamList; resip::SharedPtr<ice::Stack> mIceStack;
// Smart pointer to ICE stack. Actually stack is created in CreateICEStack() method // Pointer to owner user agent instance
std::shared_ptr<ice::Stack> mIceStack; UserAgent* mUserAgent;
// Pointer to owner user agent instance // Remote peer SIP address
UserAgent* mUserAgent; resip::NameAddr mRemotePeer;
// Remote peer SIP address // Mutex to protect this instance
resip::NameAddr mRemotePeer; Mutex mGuard;
// Mutex to protect this instance // SDP's origin version for sending
Mutex mGuard; int mOriginVersion;
UInt64 mRemoteOriginVersion;
// SDP's origin version for sending // SDP's session version
int mOriginVersion; int mSessionVersion;
uint64_t mRemoteOriginVersion;
// SDP's session version // Marks if this session does not need OnNewSession event
int mSessionVersion; bool mAcceptedByEngine;
bool mAcceptedByUser;
// Marks if this session does not need OnNewSession event // Invite session handle
bool mAcceptedByEngine; resip::InviteSessionHandle mInviteHandle;
bool mAcceptedByUser;
// Invite session handle // Dialog set object pointer
resip::InviteSessionHandle mInviteHandle; ResipSession* mResipSession;
// Dialog set object pointer // Reference counter
ResipSession* mResipSession; int mRefCount;
// Reference counter enum
int mRefCount; {
Initiator = 1,
Acceptor = 2
};
enum // Specifies session role - caller (Initiator) or callee (Acceptor)
{ volatile int mRole;
Initiator = 1,
Acceptor = 2
};
// Specifies session role - caller (Initiator) or callee (Acceptor) // Marks if candidates are gather already
volatile int mRole; volatile bool mGatheredCandidates;
// Marks if candidates are gather already // Marks if OnTerminated event was called already on session
volatile bool mGatheredCandidates; volatile bool mTerminated;
// Marks if OnTerminated event was called already on session // User friend remote peer's sip address
volatile bool mTerminated; std::string mRemoteAddress;
// User friend remote peer's sip address // Application specific data
std::string mRemoteAddress; void* mTag;
// Application specific data // Used to count number of transistions to Connected state and avoid multiple onEstablished events.
void* mTag; int mOfferAnswerCounter;
// Used to count number of transistions to Connected state and avoid multiple onEstablished events. // List of turn prefixes related to sessioj
int mOfferAnswerCounter; std::vector<int> mTurnPrefixList;
// List of turn prefixes related to sessioj // True if user agent has to send offer
std::vector<int> mTurnPrefixList; bool mHasToSendOffer;
// True if user agent has to send offer // True if user agent has to enqueue offer after ice gather finished
bool mHasToSendOffer; bool mSendOfferUpdateAfterIceGather;
// True if user agent has to enqueue offer after ice gather finished // Related sip account
bool mSendOfferUpdateAfterIceGather; PAccount mAccount;
// Related sip account // User headers for INVITE transaction
PAccount mAccount; UserHeaders mUserHeaders;
// User headers for INVITE transaction std::string remoteAddress() const;
UserHeaders mUserHeaders; void setRemoteAddress(const std::string& address);
std::string remoteAddress() const; void* tag();
void setRemoteAddress(const std::string& address); void setTag(void* tag);
int sessionId();
int increaseSdpVersion();
int addRef();
int release();
void* tag(); // Deletes providers and media sockets
void setTag(void* tag); void clearProvidersAndSockets();
int sessionId();
int increaseSdpVersion();
int addRef();
int release();
// Deletes providers and media sockets // Deletes providers
void clearProvidersAndSockets(); void clearProviders();
// Deletes providers // Helper method to find audio provider for active sip stream
void clearProviders(); AudioProvider* findProviderForActiveAudio();
// Helper method to find audio provider for active sip stream void processCommandList();
AudioProvider* findProviderForActiveAudio(); void addCommand(Command* cmd);
void enqueueOffer();
void processCommandList(); void processQueuedOffer();
void addCommand(Command* cmd); static int generateId();
void enqueueOffer(); static resip::AtomicCounter IdGenerator;
void processQueuedOffer(); static resip::AtomicCounter InstanceCounter;
static int generateId();
static std::atomic_int IdGenerator;
static std::atomic_int InstanceCounter;
}; };
typedef std::shared_ptr<Session> PSession; typedef std::shared_ptr<Session> PSession;
@@ -335,77 +336,76 @@ typedef std::shared_ptr<Session> PSession;
class ResipSessionAppDialog : public resip::AppDialog class ResipSessionAppDialog : public resip::AppDialog
{ {
public: public:
ResipSessionAppDialog(resip::HandleManager& ham); ResipSessionAppDialog(resip::HandleManager& ham);
virtual ~ResipSessionAppDialog(); virtual ~ResipSessionAppDialog();
}; };
class ResipSession : public resip::AppDialogSet class ResipSession: public resip::AppDialogSet
{ {
friend class UserAgent; friend class UserAgent;
friend class Account; friend class Account;
public: public:
enum Type enum Type
{ {
Type_None, Type_None,
Type_Registration, Type_Registration,
Type_Subscription, Type_Subscription,
Type_Call, Type_Call,
Type_Auto Type_Auto
}; };
static std::atomic_int InstanceCounter; static resip::AtomicCounter InstanceCounter;
ResipSession(resip::DialogUsageManager& dum); ResipSession(resip::DialogUsageManager& dum);
virtual ~ResipSession(); virtual ~ResipSession();
virtual resip::AppDialog* createAppDialog(const resip::SipMessage& msg); virtual resip::AppDialog* createAppDialog(const resip::SipMessage& msg);
virtual std::shared_ptr<resip::UserProfile> selectUASUserProfile(const resip::SipMessage& msg); virtual resip::SharedPtr<resip::UserProfile> selectUASUserProfile(const resip::SipMessage& msg);
void setType(Type type); void setType(Type type);
Type type(); Type type();
Session* session(); Session* session();
void setSession(Session* session); void setSession(Session* session);
UserAgent* ua(); UserAgent* ua();
void setUa(UserAgent* ua); void setUa(UserAgent* ua);
// Used for subscriptions/messages // Used for subscriptions/messages
int sessionId(); int sessionId();
// Used for subscriptions/messages // Used for subscriptions/messages
void* tag() const; void* tag() const;
void setTag(void* tag); void setTag(void* tag);
// Used for subscriptions/messages // Used for subscriptions/messages
std::string remoteAddress() const; std::string remoteAddress() const;
void setRemoteAddress(std::string address); void setRemoteAddress(std::string address);
void runTerminatedEvent(Type type, int code = 0, int reason = 0); void runTerminatedEvent(Type type, int code = 0, int reason = 0);
void setUASProfile(const std::shared_ptr<resip::UserProfile>& profile); void setUASProfile(std::shared_ptr<resip::UserProfile> profile);
protected: protected:
bool mTerminated; bool mTerminated;
UserAgent* mUserAgent; UserAgent* mUserAgent;
Type mType; Type mType;
Session* mSession; Session* mSession;
int mSessionId; int mSessionId;
std::string mRemoteAddress; std::string mRemoteAddress;
void* mTag; void* mTag;
bool mOnWatchingStartSent; bool mOnWatchingStartSent;
std::shared_ptr<resip::UserProfile> mUASProfile; std::shared_ptr<resip::UserProfile> mUASProfile;
}; };
class ResipSessionFactory : public resip::AppDialogSetFactory class ResipSessionFactory : public resip::AppDialogSetFactory
{ {
public: public:
ResipSessionFactory(UserAgent* agent); ResipSessionFactory(UserAgent* agent);
virtual resip::AppDialogSet* createAppDialogSet(resip::DialogUsageManager& dum, const resip::SipMessage& msg); virtual resip::AppDialogSet* createAppDialogSet(resip::DialogUsageManager& dum, const resip::SipMessage& msg);
protected: protected:
UserAgent* mAgent; UserAgent* mAgent;
}; };
#endif #endif
-119
View File
@@ -1,119 +0,0 @@
/* Copyright(C) 2007-2023 VoIP objects (voipobjects.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef __TOOLKIT_CONFIG_H
#define __TOOLKIT_CONFIG_H
#define USE_SPEEX_AEC
// TODO: test implementation with webrtc aec; be careful - it needs fixes!
// #define USE_WEBRTC_AEC
#define USER
#define AUDIO_SAMPLE_WIDTH 16
#define AUDIO_CHANNELS 1
// Samplerate must be 8 / 16 / 24 / 32 / 48 KHz
#define AUDIO_SAMPLERATE 48000
#define AUDIO_MIC_BUFFER_COUNT 16
#define AUDIO_MIC_BUFFER_LENGTH 10
#define AUDIO_MIC_BUFFER_SIZE (AUDIO_MIC_BUFFER_LENGTH * AUDIO_SAMPLERATE / 1000 * 2 * AUDIO_CHANNELS)
#define AUDIO_SPK_BUFFER_COUNT 16
#define AUDIO_SPK_BUFFER_LENGTH 10
#define AUDIO_SPK_BUFFER_SIZE (AUDIO_SPK_BUFFER_LENGTH * AUDIO_SAMPLERATE / 1000 * 2 * AUDIO_CHANNELS)
#define AUDIO_MIX_CHANNEL_COUNT 16
#define AUDIO_DEVICEPAIR_INPUTBUFFER 16384
// Resampler quality on the Speex 0..10 scale. Quality 1 upsampling (e.g. G.711 8k -> 48k)
// adds audible aliasing and hurts PVQA/AQuA scores; 7 is a good fidelity/CPU balance for a
// single mono stream on modern ARM64. Raise toward 10 for max fidelity if CPU allows.
#define AUDIO_RESAMPLER_QUALITY 7
#define AEC_FRAME_TIME 10
#define AEC_TAIL_TIME 160
// Defined these two lines to get dumping of audio input/output
// #define AUDIO_DUMPINPUT
// #define AUDIO_DUMPOUTPUT
#define UA_REGISTRATION_TIME 3600
#define UA_MEDIA_PORT_START 20000
#define UA_MEDIA_PORT_FINISH 30000
#define UA_MAX_UDP_PACKET_SIZE 576
#define UA_PUBLICATION_ID "314"
#define MT_SAMPLERATE AUDIO_SAMPLERATE
#define MT_MAXAUDIOFRAME 1440
#define MT_MAXRTPPACKET 1500
#define MT_DTMF_END_PACKETS 3
// Milliseconds before
#define RTP_BUFFER_HIGH (2000)
#define RTP_BUFFER_LOW (0)
#define RTP_BUFFER_PREBUFFER (100)
#define RTP_DECODED_CAPACITY 2048
#define DEFAULT_SUBSCRIPTION_TIME 1200
#define DEFAULT_SUBSCRIPTION_REFRESHTIME 500
#define PRESENCE_IN_REG_HEADER "PresenceInReg"
// Maximum UDP packet length
#define MAX_UDPPACKET_SIZE 65535
#define MAX_VALID_UDPPACKET_SIZE 2048
// AMR codec defines - it requires USE_AMR_CODEC defined
// #define USE_AMR_CODEC
#define MT_AMRNB_PAYLOADTYPE 112
#define MT_AMRNB_CODECNAME "amr"
#define MT_AMRNB_OCTET_PAYLOADTYPE 113
#define MT_AMRWB_PAYLOADTYPE 96
#define MT_AMRWB_CODECNAME "amr-wb"
#define MT_AMRWB_OCTET_PAYLOADTYPE 97
#define MT_GSMEFR_PAYLOADTYPE 126
#define MT_GSMEFR_CODECNAME "GERAN-EFR"
#define MT_EVS_PAYLOADTYPE 127
#define MT_EVS_CODECNAME "EVS"
// OPUS codec defines
#define MT_OPUS_CODEC_PT 106
// ILBC codec defines
#define MT_ILBC20_PAYLOADTYPE -1
#define MT_ILBC30_PAYLOADTYPE -1
// ISAC codec defines
#define MT_ISAC16K_PAYLOADTYPE -1
#define MT_ISAC32K_PAYLOADTYPE -1
// GSM HR payload type
#define MT_GSMHR_PAYLOADTYPE -1
// Mirror buffer capacity
#define MT_MIRROR_CAPACITY 32768
// Mirror buffer readiness threshold - 50 milliseconds
#define MT_MIRROR_PREBUFFER (MT_SAMPLERATE / 10)
#if defined(TARGET_OSX) || defined(TARGET_LINUX)
#define TEXT(X) X
#endif
// In milliseconds
#define MT_SEVANA_FRAME_TIME 680
// Number of samples
#define MT_MAX_DECODEBUFFER 32768
#endif
+4 -7
View File
@@ -1,20 +1,17 @@
cmake_minimum_required (VERSION 3.15)
project (helper_lib) project (helper_lib)
# Rely on C++ 11 # Rely on C++ 11
set (CMAKE_CXX_STANDARD 20) set (CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_STANDARD_REQUIRED ON) set (CMAKE_CXX_STANDARD_REQUIRED ON)
set (USE_NULL_UUID OFF CACHE BOOL "When enabled linking to libuuid is avoided")
set (CMAKE_POSITION_INDEPENDENT_CODE ON) set (CMAKE_POSITION_INDEPENDENT_CODE ON)
file (GLOB HELPER_LIB_SOURCES "*.cpp" "*.h") file (GLOB HELPER_LIB_SOURCES "*.cpp" "*.h")
add_library(helper_lib ${HELPER_LIB_SOURCES}) add_library(helper_lib ${HELPER_LIB_SOURCES})
set_property(TARGET helper_lib PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
# Private include directories # Private include directories
target_include_directories(helper_lib PUBLIC ../../libs/ ../../engine ../ .) target_include_directories(helper_lib PRIVATE ../../libs/ ../../engine ../)
target_compile_definitions(helper_lib PRIVATE -D_CRT_SECURE_NO_WARNINGS -D_UNICODE) target_compile_definitions(helper_lib PRIVATE -D_CRT_SECURE_NO_WARNINGS -D_UNICODE)
if (TARGET_LINUX)
target_link_libraries (helper_lib PUBLIC uuid)
endif()
+8 -2
View File
@@ -5,6 +5,12 @@
#include "HL_AsyncCommand.h" #include "HL_AsyncCommand.h"
AsyncCommand::AsyncCommand() {} AsyncCommand::AsyncCommand()
{
AsyncCommand::~AsyncCommand() {} }
AsyncCommand::~AsyncCommand()
{
}
+4 -4
View File
@@ -9,11 +9,11 @@
class AsyncCommand class AsyncCommand
{ {
public: public:
AsyncCommand(); AsyncCommand();
virtual ~AsyncCommand(); virtual ~AsyncCommand();
virtual void run(void* environment) = 0; virtual void run(void* environment) = 0;
virtual bool finished() = 0; virtual bool finished() = 0;
}; };
#endif // HL_ASYNCCOMMAND_H #endif // HL_ASYNCCOMMAND_H
+191 -239
View File
@@ -4,301 +4,253 @@
#include <string> #include <string>
const char kBase64Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" const char kBase64Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyz"
"0123456789+/"; "0123456789+/";
class Base64 class Base64 {
{ public:
public: static bool Encode(const std::string &in, std::string *out) {
static bool Encode(const std::string& in, std::string* out) int i = 0, j = 0;
{ size_t enc_len = 0;
int i = 0, j = 0; unsigned char a3[3];
size_t enc_len = 0; unsigned char a4[4];
unsigned char a3[3];
unsigned char a4[4];
out->resize(EncodedLength(in)); out->resize(EncodedLength(in));
int input_len = in.size(); int input_len = in.size();
std::string::const_iterator input = in.begin(); std::string::const_iterator input = in.begin();
while (input_len--) while (input_len--) {
{ a3[i++] = *(input++);
a3[i++] = *(input++); if (i == 3) {
if (i == 3) a3_to_a4(a4, a3);
{
a3_to_a4(a4, a3);
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++) {
{ (*out)[enc_len++] = kBase64Alphabet[a4[i]];
(*out)[enc_len++] = kBase64Alphabet[a4[i]];
}
i = 0;
}
} }
if (i) i = 0;
{ }
for (j = i; j < 3; j++) }
{
a3[j] = '\0';
}
a3_to_a4(a4, a3); if (i) {
for (j = i; j < 3; j++) {
a3[j] = '\0';
}
for (j = 0; j < i + 1; j++) a3_to_a4(a4, a3);
{
(*out)[enc_len++] = kBase64Alphabet[a4[j]];
}
while ((i++ < 3)) for (j = 0; j < i + 1; j++) {
{ (*out)[enc_len++] = kBase64Alphabet[a4[j]];
(*out)[enc_len++] = '='; }
}
while ((i++ < 3)) {
(*out)[enc_len++] = '=';
}
}
return (enc_len == out->size());
}
static bool Encode(const char *input, size_t input_length, char *out, size_t out_length) {
int i = 0, j = 0;
char *out_begin = out;
unsigned char a3[3];
unsigned char a4[4];
size_t encoded_length = EncodedLength(input_length);
if (out_length < encoded_length) return false;
while (input_length--) {
a3[i++] = *input++;
if (i == 3) {
a3_to_a4(a4, a3);
for (i = 0; i < 4; i++) {
*out++ = kBase64Alphabet[a4[i]];
} }
return (enc_len == out->size()); i = 0;
}
} }
static bool Encode(const char* input, size_t input_length, char* out, size_t out_length) if (i) {
{ for (j = i; j < 3; j++) {
int i = 0, j = 0; a3[j] = '\0';
char* out_begin = out; }
unsigned char a3[3];
unsigned char a4[4];
size_t encoded_length = EncodedLength(input_length); a3_to_a4(a4, a3);
if (out_length < encoded_length) for (j = 0; j < i + 1; j++) {
return false; *out++ = kBase64Alphabet[a4[j]];
}
while (input_length--) while ((i++ < 3)) {
{ *out++ = '=';
a3[i++] = *input++; }
if (i == 3) }
{
a3_to_a4(a4, a3);
for (i = 0; i < 4; i++) return (out == (out_begin + encoded_length));
{ }
*out++ = kBase64Alphabet[a4[i]];
}
i = 0; static bool Decode(const std::string &in, std::string *out) {
} int i = 0, j = 0;
size_t dec_len = 0;
unsigned char a3[3];
unsigned char a4[4];
int input_len = in.size();
std::string::const_iterator input = in.begin();
out->resize(DecodedLength(in));
while (input_len--) {
if (*input == '=') {
break;
}
a4[i++] = *(input++);
if (i == 4) {
for (i = 0; i <4; i++) {
a4[i] = b64_lookup(a4[i]);
} }
if (i) a4_to_a3(a3,a4);
{
for (j = i; j < 3; j++)
{
a3[j] = '\0';
}
a3_to_a4(a4, a3); for (i = 0; i < 3; i++) {
(*out)[dec_len++] = a3[i];
for (j = 0; j < i + 1; j++)
{
*out++ = kBase64Alphabet[a4[j]];
}
while ((i++ < 3))
{
*out++ = '=';
}
} }
return (out == (out_begin + encoded_length)); i = 0;
}
} }
static bool Decode(const std::string& in, std::string* out) if (i) {
{ for (j = i; j < 4; j++) {
int i = 0, j = 0; a4[j] = '\0';
size_t dec_len = 0; }
unsigned char a3[3];
unsigned char a4[4];
int input_len = in.size(); for (j = 0; j < 4; j++) {
std::string::const_iterator input = in.begin(); a4[j] = b64_lookup(a4[j]);
}
out->resize(DecodedLength(in)); a4_to_a3(a3,a4);
while (input_len--) for (j = 0; j < i - 1; j++) {
{ (*out)[dec_len++] = a3[j];
if (*input == '=') }
{ }
break;
}
a4[i++] = *(input++); return (dec_len == out->size());
if (i == 4) }
{
for (i = 0; i < 4; i++)
{
a4[i] = b64_lookup(a4[i]);
}
a4_to_a3(a3, a4); static bool Decode(const char *input, size_t input_length, char *out, size_t out_length) {
int i = 0, j = 0;
char *out_begin = out;
unsigned char a3[3];
unsigned char a4[4];
for (i = 0; i < 3; i++) size_t decoded_length = DecodedLength(input, input_length);
{
(*out)[dec_len++] = a3[i];
}
i = 0; if (out_length < decoded_length) return false;
}
while (input_length--) {
if (*input == '=') {
break;
}
a4[i++] = *(input++);
if (i == 4) {
for (i = 0; i <4; i++) {
a4[i] = b64_lookup(a4[i]);
} }
if (i) a4_to_a3(a3,a4);
{
for (j = i; j < 4; j++)
{
a4[j] = '\0';
}
for (j = 0; j < 4; j++) for (i = 0; i < 3; i++) {
{ *out++ = a3[i];
a4[j] = b64_lookup(a4[j]);
}
a4_to_a3(a3, a4);
for (j = 0; j < i - 1; j++)
{
(*out)[dec_len++] = a3[j];
}
} }
return (dec_len == out->size()); i = 0;
}
} }
static bool Decode(const char* input, size_t input_length, char* out, size_t out_length) if (i) {
{ for (j = i; j < 4; j++) {
int i = 0, j = 0; a4[j] = '\0';
char* out_begin = out; }
unsigned char a3[3];
unsigned char a4[4];
size_t decoded_length = DecodedLength(input, input_length); for (j = 0; j < 4; j++) {
a4[j] = b64_lookup(a4[j]);
}
if (out_length < decoded_length) a4_to_a3(a3,a4);
return false;
while (input_length--) for (j = 0; j < i - 1; j++) {
{ *out++ = a3[j];
if (*input == '=') }
{
break;
}
a4[i++] = *(input++);
if (i == 4)
{
for (i = 0; i < 4; i++)
{
a4[i] = b64_lookup(a4[i]);
}
a4_to_a3(a3, a4);
for (i = 0; i < 3; i++)
{
*out++ = a3[i];
}
i = 0;
}
}
if (i)
{
for (j = i; j < 4; j++)
{
a4[j] = '\0';
}
for (j = 0; j < 4; j++)
{
a4[j] = b64_lookup(a4[j]);
}
a4_to_a3(a3, a4);
for (j = 0; j < i - 1; j++)
{
*out++ = a3[j];
}
}
return (out == (out_begin + decoded_length));
} }
static int DecodedLength(const char* in, size_t in_length) return (out == (out_begin + decoded_length));
{ }
int numEq = 0;
const char* in_end = in + in_length; static int DecodedLength(const char *in, size_t in_length) {
while (*--in_end == '=') int numEq = 0;
++numEq;
return ((6 * in_length) / 8) - numEq; const char *in_end = in + in_length;
while (*--in_end == '=') ++numEq;
return ((6 * in_length) / 8) - numEq;
}
static int DecodedLength(const std::string &in) {
int numEq = 0;
int n = in.size();
for (std::string::const_reverse_iterator it = in.rbegin(); *it == '='; ++it) {
++numEq;
} }
static int DecodedLength(const std::string& in) return ((6 * n) / 8) - numEq;
{ }
int numEq = 0;
int n = in.size();
for (std::string::const_reverse_iterator it = in.rbegin(); *it == '='; ++it) inline static int EncodedLength(size_t length) {
{ return (length + 2 - ((length + 2) % 3)) / 3 * 4;
++numEq; }
}
return ((6 * n) / 8) - numEq; inline static int EncodedLength(const std::string &in) {
} return EncodedLength(in.length());
}
inline static int EncodedLength(size_t length) { return (length + 2 - ((length + 2) % 3)) / 3 * 4; } inline static void StripPadding(std::string *in) {
while (!in->empty() && *(in->rbegin()) == '=') in->resize(in->size() - 1);
}
inline static int EncodedLength(const std::string& in) { return EncodedLength(in.length()); } private:
static inline void a3_to_a4(unsigned char * a4, unsigned char * a3) {
a4[0] = (a3[0] & 0xfc) >> 2;
a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4);
a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6);
a4[3] = (a3[2] & 0x3f);
}
inline static void StripPadding(std::string* in) static inline void a4_to_a3(unsigned char * a3, unsigned char * a4) {
{ a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4);
while (!in->empty() && *(in->rbegin()) == '=') a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2);
in->resize(in->size() - 1); a3[2] = ((a4[2] & 0x3) << 6) + a4[3];
} }
private: static inline unsigned char b64_lookup(unsigned char c) {
static inline void a3_to_a4(unsigned char* a4, unsigned char* a3) if(c >='A' && c <='Z') return c - 'A';
{ if(c >='a' && c <='z') return c - 71;
a4[0] = (a3[0] & 0xfc) >> 2; if(c >='0' && c <='9') return c + 4;
a4[1] = ((a3[0] & 0x03) << 4) + ((a3[1] & 0xf0) >> 4); if(c == '+') return 62;
a4[2] = ((a3[1] & 0x0f) << 2) + ((a3[2] & 0xc0) >> 6); if(c == '/') return 63;
a4[3] = (a3[2] & 0x3f); return 255;
} }
static inline void a4_to_a3(unsigned char* a3, unsigned char* a4)
{
a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4);
a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2);
a3[2] = ((a4[2] & 0x3) << 6) + a4[3];
}
static inline unsigned char b64_lookup(unsigned char c)
{
if (c >= 'A' && c <= 'Z')
return c - 'A';
if (c >= 'a' && c <= 'z')
return c - 71;
if (c >= '0' && c <= '9')
return c + 4;
if (c == '+')
return 62;
if (c == '/')
return 63;
return 255;
}
}; };
#endif // HL_BASE64_H #endif // HL_BASE64_H
+4 -4
View File
@@ -8,10 +8,10 @@
#include "ice/ICEByteBuffer.h" #include "ice/ICEByteBuffer.h"
typedef ice::ByteBuffer ByteBuffer; typedef ice::ByteBuffer ByteBuffer;
typedef ice::PByteBuffer PByteBuffer; typedef ice::PByteBuffer PByteBuffer;
typedef ice::BitReader BitReader; typedef ice::BitReader BitReader;
typedef ice::BitWriter BitWriter; typedef ice::BitWriter BitWriter;
typedef ice::BufferReader BufferReader; typedef ice::BufferReader BufferReader;
typedef ice::BufferWriter BufferWriter; typedef ice::BufferWriter BufferWriter;
File diff suppressed because it is too large Load Diff
+84 -79
View File
@@ -14,65 +14,64 @@
BOOL WINAPI CrashReporter::Callback(LPVOID arg) BOOL WINAPI CrashReporter::Callback(LPVOID arg)
{ {
return TRUE; return TRUE;
} }
typedef int(__stdcall* CrInstallProc)(__in PCR_INSTALL_INFOW pInfo); typedef int(__stdcall *CrInstallProc)(__in PCR_INSTALL_INFOW pInfo);
static CrInstallProc CrInstall = nullptr; static CrInstallProc CrInstall = nullptr;
typedef int(__stdcall* CrUninstallProc)(); typedef int(__stdcall *CrUninstallProc)();
static CrUninstallProc CrUninstall = nullptr; static CrUninstallProc CrUninstall = nullptr;
typedef int(__stdcall* CrInstallIntoCurrentThreadProc)(DWORD dwFlags); typedef int(__stdcall *CrInstallIntoCurrentThreadProc)(DWORD dwFlags);
static CrInstallIntoCurrentThreadProc CrInstallIntoCurrentThread = nullptr; static CrInstallIntoCurrentThreadProc CrInstallIntoCurrentThread = nullptr;
typedef int(__stdcall* CrUninstallFromCurrentThreadProc)(); typedef int(__stdcall *CrUninstallFromCurrentThreadProc)();
static CrUninstallFromCurrentThreadProc CrUninstallFromCurrentThread = nullptr; static CrUninstallFromCurrentThreadProc CrUninstallFromCurrentThread = nullptr;
typedef int(__stdcall* CrGetLastErrorMsgProc)(LPWSTR buffer, UINT size); typedef int(__stdcall *CrGetLastErrorMsgProc)(LPWSTR buffer, UINT size);
static CrGetLastErrorMsgProc CrGetLastErrorMsg = nullptr; static CrGetLastErrorMsgProc CrGetLastErrorMsg = nullptr;
static HMODULE CrLibraryHandle = NULL; static HMODULE CrLibraryHandle = NULL;
#endif #endif
void CrashReporter::init(const std::string& appname, const std::string& version, const std::string& url) void CrashReporter::init(const std::string& appname, const std::string& version, const std::string& url)
{ {
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
#if defined(CRASHRPT_DYNAMIC) #if defined(CRASHRPT_DYNAMIC)
// Check if DLL functions are here // Check if DLL functions are here
if (CrLibraryHandle) if (CrLibraryHandle)
return; // Library is loaded already - so initialized already return; // Library is loaded already - so initialized already
CrLibraryHandle = ::LoadLibrary(TEXT("crashrpt.dll")); CrLibraryHandle = ::LoadLibrary(TEXT("crashrpt.dll"));
if (!CrLibraryHandle) if (!CrLibraryHandle)
return; // No logging here - initialization happens on very first stages, no chance to log anything return; // No logging here - initialization happens on very first stages, no chance to log anything
CrInstall = (CrInstallProc)::GetProcAddress(CrLibraryHandle, "crInstallW"); CrInstall = (CrInstallProc)::GetProcAddress(CrLibraryHandle, "crInstallW");
CrUninstall = (CrUninstallProc)::GetProcAddress(CrLibraryHandle, "crUninstall"); CrUninstall = (CrUninstallProc)::GetProcAddress(CrLibraryHandle, "crUninstall");
CrInstallIntoCurrentThread = CrInstallIntoCurrentThread = (CrInstallIntoCurrentThreadProc)::GetProcAddress(CrLibraryHandle, "crInstallToCurrentThread2");
(CrInstallIntoCurrentThreadProc)::GetProcAddress(CrLibraryHandle, "crInstallToCurrentThread2"); CrUninstallFromCurrentThread = (CrUninstallFromCurrentThreadProc)::GetProcAddress(CrLibraryHandle, "crUninstallFromCurrentThread");
CrUninstallFromCurrentThread = CrGetLastErrorMsg = (CrGetLastErrorMsgProc)::GetProcAddress(CrLibraryHandle, "crGetLastErrorMsgW");
(CrUninstallFromCurrentThreadProc)::GetProcAddress(CrLibraryHandle, "crUninstallFromCurrentThread");
CrGetLastErrorMsg = (CrGetLastErrorMsgProc)::GetProcAddress(CrLibraryHandle, "crGetLastErrorMsgW");
#else #else
CrInstall = &crInstallW; CrInstall = &crInstallW;
CrUninstall = &crUninstall; CrUninstall = &crUninstall;
CrInstallIntoCurrentThread = &crInstallToCurrentThread2; CrInstallIntoCurrentThread = &crInstallToCurrentThread2;
CrUninstallFromCurrentThread = &crUninstallFromCurrentThread; CrUninstallFromCurrentThread = &crUninstallFromCurrentThread;
CrGetLastErrorMsg = &crGetLastErrorMsgW; CrGetLastErrorMsg = &crGetLastErrorMsgW;
#endif #endif
if (!isLoaded()) if (!isLoaded())
return; return;
CR_INSTALL_INFO info; CR_INSTALL_INFO info;
memset(&info, 0, sizeof(CR_INSTALL_INFO)); memset(&info, 0, sizeof(CR_INSTALL_INFO));
info.cb = sizeof(CR_INSTALL_INFO); info.cb = sizeof(CR_INSTALL_INFO);
struct struct
{ {
std::wstring appname, version, url; std::wstring appname, version, url;
} unicode; } unicode;
unicode.appname = StringHelper::makeTstring(appname), unicode.version = StringHelper::makeTstring(version), unicode.appname = StringHelper::makeTstring(appname),
unicode.version = StringHelper::makeTstring(version),
unicode.url = StringHelper::makeTstring(url); unicode.url = StringHelper::makeTstring(url);
if (unicode.appname.empty()) if (unicode.appname.empty())
@@ -84,105 +83,111 @@ void CrashReporter::init(const std::string& appname, const std::string& version,
if (unicode.version.empty()) if (unicode.version.empty())
unicode.version = L"General version"; unicode.version = L"General version";
info.pszAppName = unicode.appname.c_str(); info.pszAppName = unicode.appname.c_str();
info.pszAppVersion = unicode.version.c_str(); info.pszAppVersion = unicode.version.c_str();
info.pszEmailSubject = TEXT("Crash report"); info.pszEmailSubject = TEXT("Crash report");
// info.pszEmailTo = L"amegyeri@minerva-soft.com"; //info.pszEmailTo = L"amegyeri@minerva-soft.com";
// info.pszUrl = L"http://ftp.minerva-soft.com/crashlog/crashrpt.php"; //info.pszUrl = L"http://ftp.minerva-soft.com/crashlog/crashrpt.php";
// info.pszUrl = L"http://sip.crypttalk.com/crashlog/crashrpt.php"; //info.pszUrl = L"http://sip.crypttalk.com/crashlog/crashrpt.php";
info.pszUrl = unicode.url.c_str(); info.pszUrl = unicode.url.c_str();
info.pfnCrashCallback = Callback; info.pfnCrashCallback = Callback;
info.uPriorities[CR_HTTP] = 1; info.uPriorities[CR_HTTP] = 1;
info.uPriorities[CR_SMTP] = CR_NEGATIVE_PRIORITY; info.uPriorities[CR_SMTP] = CR_NEGATIVE_PRIORITY;
info.uPriorities[CR_SMAPI] = CR_NEGATIVE_PRIORITY; info.uPriorities[CR_SMAPI] = CR_NEGATIVE_PRIORITY;
info.dwFlags = 0; info.dwFlags = 0;
info.pszCrashSenderPath = TEXT("."); info.pszCrashSenderPath = TEXT(".");
int nResult = CrInstall(&info); int nResult = CrInstall(&info);
if (nResult) if (nResult)
{ {
wchar_t errorMsg[512] = L""; wchar_t errorMsg[512] = L"";
CrGetLastErrorMsg(errorMsg, 512); CrGetLastErrorMsg(errorMsg, 512);
OutputDebugStringW(errorMsg); OutputDebugStringW(errorMsg);
// LogCritical("Core", << "Failed to install CrashReporter with code " << nResult << " and message " << //LogCritical("Core", << "Failed to install CrashReporter with code " << nResult << " and message " << errorMsg);
// errorMsg); }
}
#endif #endif
} }
void CrashReporter::free() void CrashReporter::free()
{ {
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
if (isLoaded()) if (isLoaded())
{ {
CrUninstall(); CrUninstall();
CrInstall = nullptr; CrInstall = nullptr;
CrUninstall = nullptr; CrUninstall = nullptr;
CrInstallIntoCurrentThread = nullptr; CrInstallIntoCurrentThread = nullptr;
CrUninstallFromCurrentThread = nullptr; CrUninstallFromCurrentThread = nullptr;
CrGetLastErrorMsg = nullptr; CrGetLastErrorMsg = nullptr;
#if defined(CRASHRPT_DYNAMIC) #if defined(CRASHRPT_DYNAMIC)
::FreeLibrary(CrLibraryHandle); ::FreeLibrary(CrLibraryHandle); CrLibraryHandle = NULL;
CrLibraryHandle = NULL;
#endif #endif
} }
#endif #endif
} }
bool CrashReporter::isLoaded() bool CrashReporter::isLoaded()
{ {
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
return !(!CrInstall || !CrUninstall || !CrGetLastErrorMsg || !CrInstallIntoCurrentThread || return !(!CrInstall || !CrUninstall || !CrGetLastErrorMsg ||
!CrUninstallFromCurrentThread); !CrInstallIntoCurrentThread || !CrUninstallFromCurrentThread);
#else #else
return false; return false;
#endif #endif
} }
void CrashReporter::initThread() void CrashReporter::initThread()
{ {
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
if (isLoaded()) if (isLoaded())
CrInstallIntoCurrentThread(0); CrInstallIntoCurrentThread(0);
#endif #endif
} }
void CrashReporter::freeThread() void CrashReporter::freeThread()
{ {
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
if (isLoaded()) if (isLoaded())
CrUninstallFromCurrentThread(); CrUninstallFromCurrentThread();
#endif #endif
} }
CrashReporterThreadPoint::CrashReporterThreadPoint() CrashReporterThreadPoint::CrashReporterThreadPoint()
{ {
CrashReporter::initThread(); CrashReporter::initThread();
} }
CrashReporterThreadPoint::~CrashReporterThreadPoint() CrashReporterThreadPoint::~CrashReporterThreadPoint()
{ {
CrashReporter::freeThread(); CrashReporter::freeThread();
} }
CrashReporterGuard::CrashReporterGuard() CrashReporterGuard::CrashReporterGuard()
{ {
CrashReporter::init("generic"); CrashReporter::init("generic");
} }
CrashReporterGuard::~CrashReporterGuard() CrashReporterGuard::~CrashReporterGuard()
{ {
CrashReporter::free(); CrashReporter::free();
} }
#else #else
CrashReporterThreadPoint::CrashReporterThreadPoint() {} CrashReporterThreadPoint::CrashReporterThreadPoint()
{
}
CrashReporterThreadPoint::~CrashReporterThreadPoint() {} CrashReporterThreadPoint::~CrashReporterThreadPoint()
{
}
CrashReporterGuard::CrashReporterGuard() {} CrashReporterGuard::CrashReporterGuard()
{
}
CrashReporterGuard::~CrashReporterGuard() {} CrashReporterGuard::~CrashReporterGuard()
{
}
#endif #endif
+19 -20
View File
@@ -4,8 +4,8 @@
#include <string> #include <string>
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
#include <WinSock2.h> # include <WinSock2.h>
#include <Windows.h> # include <Windows.h>
#endif #endif
// Helper class to translate SEH exceptions to C++ - sometimes it is needed // Helper class to translate SEH exceptions to C++ - sometimes it is needed
@@ -14,36 +14,35 @@
class SE_Exception class SE_Exception
{ {
private: private:
unsigned int nSE; unsigned int nSE;
public: public:
SE_Exception() {} SE_Exception() {}
SE_Exception(unsigned int n) : nSE(n) {} SE_Exception(unsigned int n) : nSE(n) {}
~SE_Exception() {} ~SE_Exception() {}
unsigned int getSeNumber() { return nSE; } unsigned int getSeNumber() { return nSE; }
}; };
extern void SEHToCpp(unsigned int, EXCEPTION_POINTERS*); extern void SEHToCpp(unsigned int, EXCEPTION_POINTERS*);
// Although better way is to have _set_se_translator set - in our case we do not call it. // Although better way is to have _set_se_translator set - in our case we do not call it.
// The cause is usage of CrashRpt libraries - it gives better control on exception reporting. // The cause is usage of CrashRpt libraries - it gives better control on exception reporting.
#define SET_SEH_TO_CPP # define SET_SEH_TO_CPP
//_set_se_translator(&SEHToCpp) //_set_se_translator(&SEHToCpp)
#else #else
#define SET_SEH_TO_CPP # define SET_SEH_TO_CPP
#endif #endif
class CrashReporter class CrashReporter
{ {
public: public:
static void init(const std::string& appname, const std::string& version = "", const std::string& url = ""); static void init(const std::string& appname, const std::string& version = "", const std::string& url = "");
static void free(); static void free();
static void initThread(); static void initThread();
static void freeThread(); static void freeThread();
static bool isLoaded(); static bool isLoaded();
#ifdef TARGET_WIN #ifdef TARGET_WIN
static BOOL WINAPI Callback(LPVOID /*lpvState*/); static BOOL WINAPI Callback(LPVOID /*lpvState*/);
#endif #endif
}; };
@@ -51,15 +50,15 @@ public:
class CrashReporterThreadPoint class CrashReporterThreadPoint
{ {
public: public:
CrashReporterThreadPoint(); CrashReporterThreadPoint();
~CrashReporterThreadPoint(); ~CrashReporterThreadPoint();
}; };
class CrashReporterGuard class CrashReporterGuard
{ {
public: public:
CrashReporterGuard(); CrashReporterGuard();
~CrashReporterGuard(); ~CrashReporterGuard();
}; };
#endif #endif
+15 -12
View File
@@ -2,25 +2,28 @@
#include "HL_String.h" #include "HL_String.h"
// --------- CsvFile ---------------- // --------- CsvFile ----------------
CsvReader::CsvReader(std::istream& stream) : mInputStream(stream) {} CsvReader::CsvReader(std::istream& stream)
:mInputStream(stream)
{}
CsvReader::~CsvReader() {} CsvReader::~CsvReader()
{}
std::istream& CsvReader::stream() const std::istream& CsvReader::stream() const
{ {
return mInputStream; return mInputStream;
} }
bool CsvReader::readLine(std::vector<std::string>& cells) bool CsvReader::readLine(std::vector<std::string>& cells)
{ {
cells.clear(); cells.clear();
std::string line; std::string line;
if (!std::getline(mInputStream, line)) if (!std::getline(mInputStream, line))
return false; return false;
strx::trim(line); StringHelper::trim(line);
if (line.empty()) if (line.empty())
return false; return false;
strx::split(line, cells, ",;"); StringHelper::split(line, cells, ",;");
return true; return true;
} }
+6 -6
View File
@@ -8,16 +8,16 @@
class CsvReader class CsvReader
{ {
public: public:
CsvReader(std::istream& stream); CsvReader(std::istream& stream);
~CsvReader(); ~CsvReader();
void setStream(std::istream& input); void setStream(std::istream& input);
std::istream& stream() const; std::istream& stream() const;
bool readLine(std::vector<std::string>& cells); bool readLine(std::vector<std::string>& cells);
protected: protected:
std::istream& mInputStream; std::istream& mInputStream;
}; };
#endif #endif
+56 -44
View File
@@ -10,64 +10,76 @@
#include <memory.h> #include <memory.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <cstdio>
enum enum
{ {
ERR_MEDIA_SOCKET_FAILED = 1, // Failed to create media socket ERR_MEDIA_SOCKET_FAILED = 1, // Failed to create media socket
ERR_CANNOT_FIND_SESSION = 2, // Cannot find session ERR_CANNOT_FIND_SESSION = 2, // Cannot find session
ERR_NO_CREDENTIALS = 3, // No credentials to configure instance ERR_NO_CREDENTIALS = 3, // No credentials to configure instance
ERR_BAD_VARIANT_TYPE = 4, // Bad variant type conversion ERR_BAD_VARIANT_TYPE = 4, // Bad variant type conversion
ERR_RINSTANCE = 5, ERR_RINSTANCE = 5,
ERR_SRTP = 6, // libsrtp error ERR_SRTP = 6, // libsrtp error
ERR_WEBRTC = 7, // webrtc error ERR_WEBRTC = 7, // webrtc error
ERR_NOMEM = 8, // no more memory ERR_NOMEM = 8, // no more memory
ERR_WMME_FAILED = 9, // WMME error ERR_WMME_FAILED = 9, // WMME error
ERR_QPC = 10, // QueryPerformanceCounter failed ERR_QPC = 10, // QueryPerformanceCounter failed
ERR_BAD_PARAM = 11, // Bad parameter ERR_BAD_PARAM = 11, // Bad parameter
ERR_NET_FAILED = 12, // Call to OS network subsystem failed ERR_NET_FAILED = 12, // Call to OS network subsystem failed
ERR_NOT_IMPLEMENTED = 13, // Not implemented in this build ERR_NOT_IMPLEMENTED = 13, // Not implemented in this build
ERR_MIXER_OVERFLOW = 14, // No more available channels in audio mixer ERR_MIXER_OVERFLOW = 14, // No more available channels in audio mixer
ERR_WAVFILE_FAILED = 15, // Error with .wav file ERR_WAVFILE_FAILED = 15, // Error with .wav file
ERR_DSOUND = 16, // DSound error ERR_DSOUND = 16, // DSound error
ERR_COREAUDIO = 17, // CoreAudio error ERR_COREAUDIO = 17, // CoreAudio error
ERR_CREATEWINDOW = 18, // CreateWindow failed ERR_CREATEWINDOW = 18, // CreateWindow failed
ERR_REGISTERNOTIFICATION = 19, // RegisterDeviceNotification failed ERR_REGISTERNOTIFICATION = 19, // RegisterDeviceNotification failed
ERR_PCAP = 20, // Smth bad with libpcap ERR_PCAP = 20, // Smth bad with libpcap
ERR_CACHE_FAILED = 21, // Failed to open cache directory ERR_CACHE_FAILED = 21, // Failed to open cache directory
ERR_FILENOTOPEN = 22, // Cannot open the file ERR_FILENOTOPEN = 22, // Cannot open the file
ERR_OPENSLES = 23 // OpenSL ES failed. Subcode has actual error code. ERR_OPENSLES = 23 // OpenSL ES failed. Subcode has actual error code.
}; };
class Exception : public std::exception class Exception: public std::exception
{ {
public: public:
Exception(int code, int subcode = 0) : mCode(code), mSubcode(subcode) Exception(int code, int subcode = 0)
{ :mCode(code), mSubcode(subcode)
std::snprintf(mMessage, sizeof(mMessage), "%d-%d", code, subcode); {
} sprintf(mMessage, "%d-%d", code, subcode);
}
Exception(int code, const char* message) Exception(int code, const char* message)
{ {
if (message) if (message)
strncpy(mMessage, message, (sizeof mMessage) - 1); strncpy(mMessage, message, (sizeof mMessage) - 1 );
} }
Exception(const Exception& src) : mCode(src.mCode), mSubcode(src.mSubcode) Exception(const Exception& src)
{ :mCode(src.mCode), mSubcode(src.mSubcode)
memcpy(mMessage, src.mMessage, sizeof mMessage); {
} memcpy(mMessage, src.mMessage, sizeof mMessage);
}
~Exception() {} ~Exception()
{ }
int code() const { return mCode; } int code() const
{
return mCode;
}
int subcode() const { return mSubcode; } int subcode() const
{
return mSubcode;
}
const char* what() const noexcept { return mMessage; } const char* what() const noexcept
{
return mMessage;
}
protected: protected:
int mCode = 0, mSubcode = 0; int mCode, mSubcode;
char mMessage[256] = {0}; char mMessage[256];
}; };
#endif #endif
+22 -54
View File
@@ -1,10 +1,10 @@
#include "HL_File.h" #include "HL_File.h"
#include <fstream> #include <fstream>
#if defined(TARGET_LINUX) || defined(TARGET_OSX) || defined(TARGET_ANDROID) #if defined(TARGET_LINUX) || defined(TARGET_OSX)
#include <unistd.h> # include <unistd.h>
#include <sys/statvfs.h> # include <sys/statvfs.h>
#include <memory.h> # include <memory.h>
#endif #endif
bool FileHelper::exists(const std::string& s) bool FileHelper::exists(const std::string& s)
@@ -32,24 +32,23 @@ void FileHelper::remove(const char* s)
::remove(s); ::remove(s);
} }
// std::string FileHelper::gettempname() std::string FileHelper::gettempname()
// { {
// #if defined(TARGET_LINUX) || defined(TARGET_ANDROID) #if defined(TARGET_LINUX)
// char template_filename[L_tmpnam] = "rtphone_XXXXXXX.tmp"; char template_filename[L_tmpnam] = "rtphone_XXXXXXX.tmp";
// int code = mkstemp(template_filename); mkstemp(template_filename);
return template_filename;
#elif defined(TARGET_WIN)
char buffer[L_tmpnam];
tmpnam(buffer);
// return template_filename; return buffer;
// #elif defined(TARGET_WIN) #elif defined(TARGET_OSX)
// char buffer[L_tmpnam]; char template_filename[L_tmpnam] = "rtphone_XXXXXXX.tmp";
// tmpnam(buffer); mktemp(template_filename);
return template_filename;
// return buffer; #endif
// #elif defined(TARGET_OSX) }
// char template_filename[L_tmpnam] = "rtphone_XXXXXXX.tmp";
// mktemp(template_filename);
// return template_filename;
// #endif
// }
bool FileHelper::isAbsolute(const std::string& s) bool FileHelper::isAbsolute(const std::string& s)
{ {
@@ -65,7 +64,7 @@ std::string FileHelper::getCurrentDir()
return std::string(); return std::string();
#endif #endif
#if defined(TARGET_LINUX) || defined(TARGET_OSX) || defined(TARGET_ANDROID) #if defined(TARGET_LINUX) || defined(TARGET_OSX)
char buf[512]; char buf[512];
if (getcwd(buf, sizeof buf) != nullptr) if (getcwd(buf, sizeof buf) != nullptr)
return buf; return buf;
@@ -102,8 +101,7 @@ size_t FileHelper::getFreespace(const std::string& path)
size_t r = static_cast<size_t>(-1); size_t r = static_cast<size_t>(-1);
#if defined(TARGET_LINUX) #if defined(TARGET_LINUX)
struct statvfs stats; struct statvfs stats; memset(&stats, 0, sizeof stats);
memset(&stats, 0, sizeof stats);
int retcode = statvfs(path.c_str(), &stats); int retcode = statvfs(path.c_str(), &stats);
if (retcode == 0) if (retcode == 0)
@@ -111,33 +109,3 @@ size_t FileHelper::getFreespace(const std::string& path)
#endif #endif
return r; return r;
} }
std::string FileHelper::expandUserHome(const std::string& path)
{
if (path.empty() || path[0] != '~')
return path; // No expansion needed
const char* home_dir = nullptr;
#ifdef TARGET_WIN
home_dir = std::getenv("USERPROFILE");
if (!home_dir)
{
home_dir = std::getenv("HOMEDRIVE");
const char* homepath = std::getenv("HOMEPATH");
if (home_dir && homepath)
{
std::string fullpath(home_dir);
fullpath += homepath;
return fullpath + path.substr(1);
}
}
#else
home_dir = std::getenv("HOME");
#endif
if (!home_dir)
throw std::runtime_error("Unable to determine the home directory");
return std::string(home_dir) + path.substr(1);
}
+7 -8
View File
@@ -6,14 +6,14 @@
class FileHelper class FileHelper
{ {
public: public:
static bool exists(const std::string& s); static bool exists(const std::string& s);
static bool exists(const char* s); static bool exists(const char* s);
static void remove(const std::string& s); static void remove(const std::string& s);
static void remove(const char* s); static void remove(const char* s);
// static std::string gettempname(); static std::string gettempname();
static bool isAbsolute(const std::string& s); static bool isAbsolute(const std::string& s);
static std::string getCurrentDir(); static std::string getCurrentDir();
@@ -22,8 +22,7 @@ public:
// Returns free space on volume for path // Returns free space on volume for path
// Works for Linux only. For other systems (size_t)-1 is returned (for errors too) // Works for Linux only. For other systems (size_t)-1 is returned (for errors too)
static size_t getFreespace(const std::string& path); static size_t getFreespace(const std::string& path);
static std::string expandUserHome(const std::string& path);
}; };
#endif #endif
+25 -60
View File
@@ -7,13 +7,13 @@ static const uint32_t HEPID2 = 0x021002;
static const uint32_t HEPID3 = 0x48455033; static const uint32_t HEPID3 = 0x48455033;
bool Packet::parseV3(const ByteBuffer& packet) bool Packet::parseV3(const ByteBuffer& packet)
{ {
if (packet.size() < 30) if (packet.size() < 30)
return false; return false;
BufferReader r(packet); BufferReader r(packet);
char signature[4]; char signature[4];
r.readBuffer(signature, 4); r.readBuffer(signature, 4);
if (signature[0] != 'H' || signature[1] != 'E' || signature[2] != 'P' || signature[3] != '3') if (signature[0] != 'H' || signature[1] != 'E' || signature[2] != 'P' || signature[3] != '3')
@@ -24,12 +24,12 @@ bool Packet::parseV3(const ByteBuffer& packet)
l -= 6; l -= 6;
InternetAddress sourceAddr4, destAddr4, sourceAddr6, destAddr6; InternetAddress sourceAddr4, destAddr4, sourceAddr6, destAddr6;
uint16_t sourcePort = 0, destPort = 0; uint16_t sourcePort = 0, destPort = 0;
while (r.count() < packet.size()) while (r.count() < packet.size())
{ {
mVendorId = (VendorId)r.readUShort(); mVendorId = (VendorId)r.readUShort();
ChunkType chunkType = (ChunkType)r.readUShort(); ChunkType chunkType = (ChunkType)r.readUShort();
int chunkLength = r.readUShort(); int chunkLength = r.readUShort();
switch (chunkType) switch (chunkType)
{ {
@@ -102,16 +102,18 @@ bool Packet::parseV3(const ByteBuffer& packet)
if (!sourceAddr4.isEmpty()) if (!sourceAddr4.isEmpty())
mSourceAddress = sourceAddr4; mSourceAddress = sourceAddr4;
else if (!sourceAddr6.isEmpty()) else
mSourceAddress = sourceAddr6; if (!sourceAddr6.isEmpty())
mSourceAddress = sourceAddr6;
if (!mSourceAddress.isEmpty()) if (!mSourceAddress.isEmpty())
mSourceAddress.setPort(sourcePort); mSourceAddress.setPort(sourcePort);
if (!destAddr4.isEmpty()) if (!destAddr4.isEmpty())
mDestinationAddress = destAddr4; mDestinationAddress = destAddr4;
else if (!destAddr6.isEmpty()) else
mDestinationAddress = destAddr6; if (!destAddr6.isEmpty())
mDestinationAddress = destAddr6;
if (!mDestinationAddress.isEmpty()) if (!mDestinationAddress.isEmpty())
mDestinationAddress.setPort(destPort); mDestinationAddress.setPort(destPort);
@@ -119,7 +121,7 @@ bool Packet::parseV3(const ByteBuffer& packet)
return true; return true;
} }
bool Packet::parseV2(const ByteBuffer& packet) bool Packet::parseV2(const ByteBuffer &packet)
{ {
if (packet.size() < 31) if (packet.size() < 31)
return false; return false;
@@ -146,53 +148,16 @@ bool Packet::parseV2(const ByteBuffer& packet)
return true; return true;
} }
#define WRITE_CHUNK_UCHAR(T, V) \ #define WRITE_CHUNK_UCHAR(T, V) {w.writeUShort((uint16_t)mVendorId); w.writeUShort((uint16_t)T); w.writeUShort(1); w.writeUChar((uint8_t)V);}
{ \ #define WRITE_CHUNK_USHORT(T, V) {w.writeUShort((uint16_t)mVendorId); w.writeUShort((uint16_t)T); w.writeUShort(2); w.writeUShort((uint16_t)V);}
w.writeUShort((uint16_t)mVendorId); \ #define WRITE_CHUNK_UINT(T, V) {w.writeUShort((uint16_t)mVendorId); w.writeUShort((uint16_t)T); w.writeUShort(4); w.writeUInt((uint32_t)V);}
w.writeUShort((uint16_t)T); \ #define WRITE_CHUNK_IP4(T, V) {w.writeUShort((uint16_t)mVendorId); w.writeUShort((uint16_t)T); w.writeUShort(4); w.writeIp(V);}
w.writeUShort(1); \ #define WRITE_CHUNK_IP6(T, V) {w.writeUShort((uint16_t)mVendorId); w.writeUShort((uint16_t)T); w.writeUShort(8); w.writeIp(V);}
w.writeUChar((uint8_t)V); \ #define WRITE_CHUNK_BUFFER(T, V) {w.writeUShort((uint16_t)mVendorId); w.writeUShort((uint16_t)T); w.writeUShort(8); w.writeBuffer(V.data(), V.size());}
}
#define WRITE_CHUNK_USHORT(T, V) \
{ \
w.writeUShort((uint16_t)mVendorId); \
w.writeUShort((uint16_t)T); \
w.writeUShort(2); \
w.writeUShort((uint16_t)V); \
}
#define WRITE_CHUNK_UINT(T, V) \
{ \
w.writeUShort((uint16_t)mVendorId); \
w.writeUShort((uint16_t)T); \
w.writeUShort(4); \
w.writeUInt((uint32_t)V); \
}
#define WRITE_CHUNK_IP4(T, V) \
{ \
w.writeUShort((uint16_t)mVendorId); \
w.writeUShort((uint16_t)T); \
w.writeUShort(4); \
w.writeIp(V); \
}
#define WRITE_CHUNK_IP6(T, V) \
{ \
w.writeUShort((uint16_t)mVendorId); \
w.writeUShort((uint16_t)T); \
w.writeUShort(8); \
w.writeIp(V); \
}
#define WRITE_CHUNK_BUFFER(T, V) \
{ \
w.writeUShort((uint16_t)mVendorId); \
w.writeUShort((uint16_t)T); \
w.writeUShort(8); \
w.writeBuffer(V.data(), V.size()); \
}
ByteBuffer Packet::buildV3() ByteBuffer Packet::buildV3()
{ {
ByteBuffer r; ByteBuffer r; r.resize(mBody.size() + 512);
r.resize(mBody.size() + 512);
BufferWriter w(r); BufferWriter w(r);
// Signature // Signature
@@ -209,8 +174,9 @@ ByteBuffer Packet::buildV3()
{ {
if (mSourceAddress.isV4()) if (mSourceAddress.isV4())
WRITE_CHUNK_IP4(ChunkType::IP4SourceAddress, mSourceAddress) WRITE_CHUNK_IP4(ChunkType::IP4SourceAddress, mSourceAddress)
else if (mSourceAddress.isV6()) else
WRITE_CHUNK_IP6(ChunkType::IP6SourceAddress, mSourceAddress); if (mSourceAddress.isV6())
WRITE_CHUNK_IP6(ChunkType::IP6SourceAddress, mSourceAddress);
WRITE_CHUNK_USHORT(ChunkType::SourcePort, mSourceAddress.port()); WRITE_CHUNK_USHORT(ChunkType::SourcePort, mSourceAddress.port());
} }
@@ -220,8 +186,9 @@ ByteBuffer Packet::buildV3()
{ {
if (mDestinationAddress.isV4()) if (mDestinationAddress.isV4())
WRITE_CHUNK_IP4(ChunkType::IP4DestinationAddress, mDestinationAddress) WRITE_CHUNK_IP4(ChunkType::IP4DestinationAddress, mDestinationAddress)
else if (mDestinationAddress.isV6()) else
WRITE_CHUNK_IP6(ChunkType::IP6DestinationAddress, mDestinationAddress); if (mDestinationAddress.isV6())
WRITE_CHUNK_IP6(ChunkType::IP6DestinationAddress, mDestinationAddress);
WRITE_CHUNK_USHORT(ChunkType::DestinationPort, mDestinationAddress.port()); WRITE_CHUNK_USHORT(ChunkType::DestinationPort, mDestinationAddress.port());
} }
@@ -249,9 +216,7 @@ ByteBuffer Packet::buildV3()
r.resize(w.offset()); r.resize(w.offset());
w.rewind(); w.rewind(); w.skip(4); w.writeUShort((uint16_t)r.size());
w.skip(4);
w.writeUShort((uint16_t)r.size());
return r; return r;
} }
+31 -27
View File
@@ -6,8 +6,8 @@
namespace HEP namespace HEP
{ {
enum class ChunkType enum class ChunkType
{ {
None = 0, None = 0,
IPProtocolFamily, IPProtocolFamily,
IPProtocolID, IPProtocolID,
@@ -19,17 +19,17 @@ enum class ChunkType
DestinationPort, DestinationPort,
Timestamp, Timestamp,
TimestampMicro, TimestampMicro,
ProtocolType, // Maps to Protocol Types below ProtocolType, // Maps to Protocol Types below
CaptureAgentID, CaptureAgentID,
KeepAliveTimer, KeepAliveTimer,
AuthenticationKey, AuthenticationKey,
PacketPayload, PacketPayload,
CompressedPayload, CompressedPayload,
InternalC InternalC
}; };
enum class VendorId enum class VendorId
{ {
None, None,
FreeSwitch, FreeSwitch,
Kamailio, Kamailio,
@@ -37,10 +37,10 @@ enum class VendorId
Asterisk, Asterisk,
Homer, Homer,
SipXecs SipXecs
}; };
enum class ProtocolId enum class ProtocolId
{ {
Reserved = 0, Reserved = 0,
SIP, SIP,
XMPP, XMPP,
@@ -54,28 +54,32 @@ enum class ProtocolId
IAX, IAX,
H322, H322,
H321 H321
}; };
struct Packet struct Packet
{ {
bool parseV3(const ByteBuffer& packet); bool parseV3(const ByteBuffer& packet);
bool parseV2(const ByteBuffer& packet); bool parseV2(const ByteBuffer& packet);
ByteBuffer buildV3(); ByteBuffer buildV3();
uint8_t mIpProtocolFamily, mIpProtocolId; uint8_t
mIpProtocolFamily,
mIpProtocolId;
InternetAddress mSourceAddress, mDestinationAddress; InternetAddress
mSourceAddress,
mDestinationAddress;
timeval mTimestamp; timeval mTimestamp;
ProtocolId mProtocolType; ProtocolId mProtocolType;
uint16_t mCaptureAgentId; uint16_t mCaptureAgentId;
uint16_t mKeepAliveTimer; uint16_t mKeepAliveTimer;
ByteBuffer mAuthenticateKey; ByteBuffer mAuthenticateKey;
ByteBuffer mBody; ByteBuffer mBody;
VendorId mVendorId; VendorId mVendorId;
uint32_t mBodyOffset = 0; uint32_t mBodyOffset = 0;
}; };
} // namespace HEP }
#endif #endif
File diff suppressed because it is too large Load Diff
+24 -23
View File
@@ -6,33 +6,34 @@
class IuUP class IuUP
{ {
public: public:
enum class PduType enum class PduType
{ {
DataWithCrc = 0, DataWithCrc = 0,
DataNoCrc = 1, DataNoCrc = 1,
ControlProc = 14 ControlProc = 14
}; };
struct Frame struct Frame
{ {
PduType mPduType; PduType mPduType;
uint8_t mFrameNumber; uint8_t mFrameNumber;
uint8_t mFqc; uint8_t mFqc;
uint8_t mRfci; uint8_t mRfci;
uint8_t mHeaderCrc; uint8_t mHeaderCrc;
bool mHeaderCrcOk; bool mHeaderCrcOk;
uint16_t mPayloadCrc; uint16_t mPayloadCrc;
bool mPayloadCrcOk; bool mPayloadCrcOk;
const uint8_t* mPayload; const uint8_t* mPayload;
uint16_t mPayloadSize; uint16_t mPayloadSize;
}; };
/* Default value is false */ /* Default value is false */
static bool TwoBytePseudoheader; static bool TwoBytePseudoheader;
static bool parse(const uint8_t* packet, int size, Frame& result); static bool parse(const uint8_t* packet, int size, Frame& result);
static bool parse2(const uint8_t* packet, int size, Frame& result); static bool parse2(const uint8_t* packet, int size, Frame& result);
}; };
#endif // HL_IUUP_H #endif // HL_IUUP_H
+1
View File
@@ -2,3 +2,4 @@
* This Source Code Form is subject to the terms of the Mozilla Public * This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+4 -4
View File
@@ -11,13 +11,13 @@
using ice::GLogger; using ice::GLogger;
using ice::LogLock; using ice::LogLock;
using ice::LL_CRITICAL;
using ice::LL_DEBUG;
using ice::LL_ERROR;
using ice::LL_INFO;
using ice::LL_MEDIA; using ice::LL_MEDIA;
using ice::LL_DEBUG;
using ice::LL_INFO;
using ice::LL_CRITICAL;
using ice::LL_NONE; using ice::LL_NONE;
using ice::LL_SPECIAL; using ice::LL_SPECIAL;
using ice::LL_ERROR;
using ice::LogLevelHelper; using ice::LogLevelHelper;
#endif #endif
+84 -97
View File
@@ -3,44 +3,23 @@
#include "HL_NetworkFrame.h" #include "HL_NetworkFrame.h"
#include "HL_InternetAddress.h" #include "HL_InternetAddress.h"
#define ETHERTYPE_MPLS_UC (0x8847) #define ETHERTYPE_MPLS_UC (0x8847)
#define ETHERTYPE_MPLS_MC (0x8848) #define ETHERTYPE_MPLS_MC (0x8848)
#define ETHERTYPE_IPV6 (0x86dd) #define ETHERTYPE_IPV6 (0x86dd)
#define ETHERTYPE_IP (0x0800) #define ETHERTYPE_IP (0x0800)
#define MPLS_STACK_MASK (0x00000100) #define MPLS_STACK_MASK (0x00000100)
#define MPLS_STACK_SHIFT (8) #define MPLS_STACK_SHIFT (8)
NetworkFrame::Payload NetworkFrame::GetUdpPayloadForRaw(const Packet& data) NetworkFrame::PacketData NetworkFrame::GetUdpPayloadForEthernet(NetworkFrame::PacketData& packet, InternetAddress& source, InternetAddress& destination)
{ {
const Ip4Header* ip4 = reinterpret_cast<const Ip4Header*>(data.mData); PacketData result(packet);
if (ip4->mProtocol != IPPROTO_UDP && ip4->mProtocol != 0) const EthernetHeader* ethernet = reinterpret_cast<const EthernetHeader*>(packet.mData);
return Payload();
switch (ip4->version())
{
case 4:
return GetUdpPayloadForIp4(data);
case 6:
return GetUdpPayloadForIp6(data);
default:
return Payload();
}
}
NetworkFrame::Payload NetworkFrame::GetUdpPayloadForEthernet(const Packet& data)
{
Packet result(data);
const EthernetHeader* ethernet = reinterpret_cast<const EthernetHeader*>(data.mData);
// Skip ethernet header // Skip ethernet header
result.mData += sizeof(EthernetHeader); packet.mData += sizeof(EthernetHeader);
result.mLength -= sizeof(EthernetHeader); packet.mLength -= sizeof(EthernetHeader);
// See if there is Vlan header // See if there is Vlan header
uint16_t proto = 0; uint16_t proto = 0;
@@ -49,11 +28,12 @@ NetworkFrame::Payload NetworkFrame::GetUdpPayloadForEthernet(const Packet& data)
// Skip 1 or more VLAN headers // Skip 1 or more VLAN headers
do do
{ {
const VlanHeader* vlan = reinterpret_cast<const VlanHeader*>(result.mData); const VlanHeader* vlan = reinterpret_cast<const VlanHeader*>(packet.mData);
result.mData += sizeof(VlanHeader); packet.mData += sizeof(VlanHeader);
result.mLength -= sizeof(VlanHeader); packet.mLength -= sizeof(VlanHeader);
proto = ntohs(vlan->mData); proto = ntohs(vlan->mData);
} while (proto == 0x8100); }
while (proto == 0x8100);
} }
// Skip MPLS headers // Skip MPLS headers
@@ -62,11 +42,11 @@ NetworkFrame::Payload NetworkFrame::GetUdpPayloadForEthernet(const Packet& data)
case ETHERTYPE_MPLS_UC: case ETHERTYPE_MPLS_UC:
case ETHERTYPE_MPLS_MC: case ETHERTYPE_MPLS_MC:
// Parse MPLS here until marker "bottom of mpls stack" // Parse MPLS here until marker "bottom of mpls stack"
for (bool bottomOfStack = false; !bottomOfStack; for(bool bottomOfStack = false; !bottomOfStack;
bottomOfStack = ((ntohl(*(uint32_t*)(result.mData - 4)) & MPLS_STACK_MASK) >> MPLS_STACK_SHIFT) != 0) bottomOfStack = ((ntohl(*(uint32_t*)(packet.mData - 4)) & MPLS_STACK_MASK) >> MPLS_STACK_SHIFT) != 0)
{ {
result.mData += 4; packet.mData += 4;
result.mLength -= 4; packet.mLength -=4;
} }
break; break;
@@ -79,86 +59,86 @@ NetworkFrame::Payload NetworkFrame::GetUdpPayloadForEthernet(const Packet& data)
break; break;
} }
const Ip4Header* ip4 = reinterpret_cast<const Ip4Header*>(result.mData); const Ip4Header* ip4 = reinterpret_cast<const Ip4Header*>(packet.mData);
if (ip4->mProtocol != IPPROTO_UDP && ip4->mProtocol != 0) if (ip4->mProtocol != IPPROTO_UDP && ip4->mProtocol != 0)
return Payload(); return PacketData();
switch (ip4->version()) switch (ip4->version())
{ {
case 4: case 4:
return GetUdpPayloadForIp4(result); return GetUdpPayloadForIp4(packet, source, destination);
case 6: case 6:
return GetUdpPayloadForIp6(result); return GetUdpPayloadForIp6(packet, source, destination);
default: default:
return Payload(); return PacketData();
} }
} }
NetworkFrame::Payload NetworkFrame::GetUdpPayloadForSLL(const Packet& data) NetworkFrame::PacketData NetworkFrame::GetUdpPayloadForSLL(NetworkFrame::PacketData& packet, InternetAddress& source, InternetAddress& destination)
{ {
Packet result(data); PacketData result(packet);
if (result.mLength < 16) if (packet.mLength < 16)
return Payload(); return PacketData();
const LinuxSllHeader* sll = reinterpret_cast<const LinuxSllHeader*>(result.mData); const LinuxSllHeader* sll = reinterpret_cast<const LinuxSllHeader*>(packet.mData);
result.mData += sizeof(LinuxSllHeader); packet.mData += sizeof(LinuxSllHeader);
result.mLength -= sizeof(LinuxSllHeader); packet.mLength -= sizeof(LinuxSllHeader);
switch (ntohs(sll->mProtocolType)) switch (ntohs(sll->mProtocolType))
{ {
case 0x0800: case 0x0800:
return GetUdpPayloadForIp4(result); return GetUdpPayloadForIp4(packet, source, destination);
case 0x86DD: case 0x86DD:
return GetUdpPayloadForIp6(result); return GetUdpPayloadForIp6(packet, source, destination);
default: default:
return Payload(); return PacketData();
} }
} }
NetworkFrame::Payload NetworkFrame::GetUdpPayloadForLoopback(const Packet& data) NetworkFrame::PacketData NetworkFrame::GetUdpPayloadForLoopback(NetworkFrame::PacketData& packet, InternetAddress& source, InternetAddress& destination)
{ {
Packet result(data); PacketData result(packet);
if (result.mLength < 16) if (packet.mLength < 16)
return Payload(); return PacketData();
struct LoopbackHeader struct LoopbackHeader
{ {
uint32_t mProtocolType; uint32_t mProtocolType;
}; };
const LoopbackHeader* lh = reinterpret_cast<const LoopbackHeader*>(result.mData); const LoopbackHeader* lh = reinterpret_cast<const LoopbackHeader*>(packet.mData);
result.mData += sizeof(LoopbackHeader); packet.mData += sizeof(LoopbackHeader);
result.mLength -= sizeof(LoopbackHeader); packet.mLength -= sizeof(LoopbackHeader);
switch (lh->mProtocolType) switch (lh->mProtocolType)
{ {
case AF_INET: case AF_INET:
return GetUdpPayloadForIp4(result); return GetUdpPayloadForIp4(packet, source, destination);
case AF_INET6: case AF_INET6:
return GetUdpPayloadForIp6(result); return GetUdpPayloadForIp6(packet, source, destination);
default: default:
return Payload(); return PacketData();
} }
} }
NetworkFrame::Payload NetworkFrame::GetUdpPayloadForIp4(const Packet& data) NetworkFrame::PacketData NetworkFrame::GetUdpPayloadForIp4(NetworkFrame::PacketData& packet, InternetAddress& source, InternetAddress& destination)
{ {
Packet result(data); PacketData result(packet);
const Ip4Header* ip4 = reinterpret_cast<const Ip4Header*>(data.mData); const Ip4Header* ip4 = reinterpret_cast<const Ip4Header*>(packet.mData);
if (ip4->mProtocol != IPPROTO_UDP && ip4->mProtocol != 0) if (ip4->mProtocol != IPPROTO_UDP && ip4->mProtocol != 0)
return Payload(); return PacketData(nullptr, 0);
result.mData += ip4->headerLength(); result.mData += ip4->headerLength();
result.mLength -= ip4->headerLength(); result.mLength -= ip4->headerLength();
@@ -172,62 +152,69 @@ NetworkFrame::Payload NetworkFrame::GetUdpPayloadForIp4(const Packet& data)
if (length - sizeof(UdpHeader) < (size_t)result.mLength) if (length - sizeof(UdpHeader) < (size_t)result.mLength)
result.mLength = length - sizeof(UdpHeader); result.mLength = length - sizeof(UdpHeader);
InternetAddress addr_source; source.setIp(ip4->mSource);
addr_source.setIp(ip4->mSource); source.setPort(ntohs(udp->mSourcePort));
addr_source.setPort(ntohs(udp->mSourcePort));
InternetAddress addr_dest; destination.setIp(ip4->mDestination);
addr_dest.setIp(ip4->mDestination); destination.setPort(ntohs(udp->mDestinationPort));
addr_dest.setPort(ntohs(udp->mDestinationPort));
return {.data = result, .source = addr_source, .dest = addr_dest}; return result;
} }
struct Ip6Header struct Ip6Header
{ {
#if __BYTE_ORDER == __LITTLE_ENDIAN #if __BYTE_ORDER == __LITTLE_ENDIAN
uint8_t traffic_class_hi : 4, version : 4; uint8_t traffic_class_hi:4,
uint8_t flow_label_hi : 4, traffic_class_lo : 4; version:4;
uint16_t flow_label_lo; uint8_t flow_label_hi:4,
traffic_class_lo:4;
uint16_t flow_label_lo;
#elif __BYTE_ORDER == __BIG_ENDIAN #elif __BYTE_ORDER == __BIG_ENDIAN
uint8_t version : 4, traffic_class_hi : 4; uint8_t version:4,
uint8_t traffic_class_lo : 4, flow_label_hi : 4; traffic_class_hi:4;
uint16_t flow_label_lo; uint8_t traffic_class_lo:4,
flow_label_hi:4;
uint16_t flow_label_lo;
#else #else
#error "Please fix endianness defines" # error "Please fix endianness defines"
#endif #endif
uint16_t payload_len; uint16_t payload_len;
uint8_t next_header; uint8_t next_header;
uint8_t hop_limit; uint8_t hop_limit;
struct in6_addr src_ip; struct in6_addr src_ip;
struct in6_addr dst_ip; struct in6_addr dst_ip;
}; };
NetworkFrame::Payload NetworkFrame::GetUdpPayloadForIp6(const Packet& data) NetworkFrame::PacketData NetworkFrame::GetUdpPayloadForIp6(NetworkFrame::PacketData& packet, InternetAddress& source, InternetAddress& destination)
{ {
Packet result(data); PacketData result(packet);
const Ip6Header* ip6 = reinterpret_cast<const Ip6Header*>(result.mData); const Ip6Header* ip6 = reinterpret_cast<const Ip6Header*>(packet.mData);
/*if (ip6->mProtocol != IPPROTO_UDP && ip4->mProtocol != 0) /*if (ip6->mProtocol != IPPROTO_UDP && ip4->mProtocol != 0)
return PacketData(nullptr, 0); return PacketData(nullptr, 0);
*/ */
result.mData += sizeof(Ip6Header); result.mData += sizeof(Ip6Header);
result.mLength -= sizeof(Ip6Header); result.mLength -= sizeof(Ip6Header);
// std::cout << sizeof(Ip6Header) << std::endl; //std::cout << sizeof(Ip6Header) << std::endl;
const UdpHeader* udp = reinterpret_cast<const UdpHeader*>(result.mData); const UdpHeader* udp = reinterpret_cast<const UdpHeader*>(result.mData);
result.mData += sizeof(UdpHeader); result.mData += sizeof(UdpHeader);
result.mLength -= sizeof(UdpHeader); result.mLength -= sizeof(UdpHeader);
InternetAddress addr_source; /*
addr_source.setIp(ip6->src_ip); if (result.mLength != ntohs(udp->mDatagramLength))
addr_source.setPort(ntohs(udp->mSourcePort)); return PacketData(nullptr, 0);
*/
InternetAddress addr_dest; source.setIp(ip6->src_ip);
addr_dest.setIp(ip6->dst_ip); source.setPort(ntohs(udp->mSourcePort));
addr_dest.setPort(ntohs(udp->mDestinationPort)); //std::cout << source.toStdString() << " - ";
return {.data = result, .source = addr_source, .dest = addr_dest}; destination.setIp(ip6->dst_ip);
destination.setPort(ntohs(udp->mDestinationPort));
//std::cout << destination.toStdString() << std::endl;
return result;
} }
+58 -56
View File
@@ -7,44 +7,38 @@
class NetworkFrame class NetworkFrame
{ {
public: public:
struct Packet struct PacketData
{ {
const uint8_t* mData; const uint8_t* mData;
size_t mLength; size_t mLength;
Packet(const uint8_t* data, size_t length) : mData(data), mLength(length) {} PacketData(const uint8_t* data, size_t length)
:mData(data), mLength(length)
{}
Packet() : mData(nullptr), mLength(0) {} PacketData()
:mData(nullptr), mLength(0)
bool is_empty() const { return mData == nullptr || mLength == 0; } {}
}; };
struct Payload static PacketData GetUdpPayloadForEthernet(PacketData& packet, InternetAddress& source, InternetAddress& destination);
{ static PacketData GetUdpPayloadForIp4(PacketData& packet, InternetAddress& source, InternetAddress& destination);
Packet data; static PacketData GetUdpPayloadForIp6(PacketData& packet, InternetAddress& source, InternetAddress& destination);
InternetAddress source; static PacketData GetUdpPayloadForSLL(PacketData& packet, InternetAddress& source, InternetAddress& destination);
InternetAddress dest; static PacketData GetUdpPayloadForLoopback(PacketData& packet, InternetAddress& source, InternetAddress& destination);
};
static Payload GetUdpPayloadForEthernet(const Packet& data);
static Payload GetUdpPayloadForIp4(const Packet& data);
static Payload GetUdpPayloadForIp6(const Packet& data);
static Payload GetUdpPayloadForSLL(const Packet& data);
static Payload GetUdpPayloadForLoopback(const Packet& data);
static Payload GetUdpPayloadForRaw(const Packet& data);
struct EthernetHeader struct EthernetHeader
{ {
/* Ethernet addresses are 6 bytes */ /* Ethernet addresses are 6 bytes */
static const int AddressLength = 6; static const int AddressLength = 6;
uint8_t mEtherDHost[AddressLength]; /* Destination host address */ uint8_t mEtherDHost[AddressLength]; /* Destination host address */
uint8_t mEtherSHost[AddressLength]; /* Source host address */ uint8_t mEtherSHost[AddressLength]; /* Source host address */
uint16_t mEtherType; /* IP? ARP? RARP? etc */ uint16_t mEtherType; /* IP? ARP? RARP? etc */
}; };
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
struct /*__attribute__((packed))*/ LinuxSllHeader struct /*__attribute__((packed))*/ LinuxSllHeader
#else #else
struct __attribute__((packed)) LinuxSllHeader struct __attribute__((packed)) LinuxSllHeader
#endif #endif
{ {
@@ -63,48 +57,55 @@ public:
struct Ip4Header struct Ip4Header
{ {
uint8_t mVhl; /* version << 4 | header length >> 2 */ uint8_t mVhl; /* version << 4 | header length >> 2 */
uint8_t mTos; /* type of service */ uint8_t mTos; /* type of service */
uint16_t mLen; /* total length */ uint16_t mLen; /* total length */
uint16_t mId; /* identification */ uint16_t mId; /* identification */
uint16_t mOffset; /* fragment offset field */ uint16_t mOffset; /* fragment offset field */
#define IP_RF 0x8000 /* reserved fragment flag */ #define IP_RF 0x8000 /* reserved fragment flag */
#define IP_DF 0x4000 /* dont fragment flag */ #define IP_DF 0x4000 /* dont fragment flag */
#define IP_MF 0x2000 /* more fragments flag */ #define IP_MF 0x2000 /* more fragments flag */
#define IP_OFFMASK 0x1fff /* mask for fragmenting bits */ #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */
uint8_t mTtl; /* time to live */ uint8_t mTtl; /* time to live */
uint8_t mProtocol; /* protocol */ uint8_t mProtocol; /* protocol */
uint16_t mChecksum; /* checksum */ uint16_t mChecksum; /* checksum */
in_addr mSource, mDestination; /* source and dest address */ in_addr mSource,
mDestination; /* source and dest address */
int headerLength() const { return (mVhl & 0x0f) * 4; } int headerLength() const
{
return (mVhl & 0x0f) * 4;
}
int version() const { return mVhl >> 4; } int version() const
{
return mVhl >> 4;
}
const in_addr& source4() const { return mSource; } const in_addr& source4() const { return mSource; }
const in_addr& dest4() const { return mDestination; } const in_addr& dest4() const { return mDestination; }
const in6_addr& source6() const { return (const in6_addr&)mSource; } const in6_addr& source6() const { return (const in6_addr&)mSource; }
const in6_addr& dest6() const { return (const in6_addr&)mDestination; } const in6_addr& dest6() const { return (const in6_addr&)mDestination; }
}; };
struct UdpHeader struct UdpHeader
{ {
uint16_t mSourcePort; /* source port */ uint16_t mSourcePort; /* source port */
uint16_t mDestinationPort; uint16_t mDestinationPort;
uint16_t mDatagramLength; /* datagram length */ uint16_t mDatagramLength; /* datagram length */
uint16_t mDatagramChecksum; /* datagram checksum */ uint16_t mDatagramChecksum; /* datagram checksum */
}; };
struct TcpHeader struct TcpHeader
{ {
uint16_t mSourcePort; /* source port */ uint16_t mSourcePort; /* source port */
uint16_t mDestinationPort; /* destination port */ uint16_t mDestinationPort; /* destination port */
uint32_t mSeqNo; /* sequence number */ uint32_t mSeqNo; /* sequence number */
uint32_t mAckNo; /* acknowledgement number */ uint32_t mAckNo; /* acknowledgement number */
uint32_t mDataOffset; /* data offset, rsvd */ uint32_t mDataOffset; /* data offset, rsvd */
#define TH_OFF(th) (((th)->th_offx2 & 0xf0) >> 4) #define TH_OFF(th) (((th)->th_offx2 & 0xf0) >> 4)
uint8_t mFlags; uint8_t mFlags;
#define TH_FIN 0x01 #define TH_FIN 0x01
#define TH_SYN 0x02 #define TH_SYN 0x02
#define TH_RST 0x04 #define TH_RST 0x04
@@ -113,10 +114,11 @@ public:
#define TH_URG 0x20 #define TH_URG 0x20
#define TH_ECE 0x40 #define TH_ECE 0x40
#define TH_CWR 0x80 #define TH_CWR 0x80
#define TH_FLAGS (TH_FIN | TH_SYN | TH_RST | TH_ACK | TH_URG | TH_ECE | TH_CWR) #define TH_FLAGS (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)
uint16_t mWindow; /* window */ uint16_t mWindow; /* window */
uint16_t mChecksum; /* checksum */ uint16_t mChecksum; /* checksum */
uint16_t mUrgentPointer; /* urgent pointer */ uint16_t mUrgentPointer; /* urgent pointer */
}; };
}; };
#endif #endif
+85 -109
View File
@@ -1,206 +1,182 @@
/* Copyright(C) 2007-2026 VoIP objects (voipobjects.com) /* Copyright(C) 2007-2017 VoIP objects (voipobjects.com)
* This Source Code Form is subject to the terms of the Mozilla Public * This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#if defined(TARGET_LINUX) || defined(TARGET_ANDROID) #if defined(TARGET_LINUX) || defined(TARGET_ANDROID)
#include <asm/ioctls.h> # include <asm/ioctls.h>
#endif #endif
#include "../engine_config.h" #include "../config.h"
#include "HL_NetworkSocket.h" #include "HL_NetworkSocket.h"
#include "HL_Log.h"
#if defined(TARGET_OSX) || defined(TARGET_LINUX) #if defined(TARGET_OSX) || defined(TARGET_LINUX)
#include <fcntl.h> # include <fcntl.h>
#endif #endif
#if !defined(TARGET_WIN) #if !defined(TARGET_WIN)
#include <unistd.h> # include <unistd.h>
#endif #endif
#include <assert.h> #include <assert.h>
#define LOG_SUBSYSTEM "network" DatagramSocket::DatagramSocket()
:mFamily(AF_INET), mHandle(INVALID_SOCKET), mLocalPort(0)
{
DatagramSocket::DatagramSocket() : mFamily(AF_INET), mHandle(INVALID_SOCKET), mLocalPort(0) {} }
DatagramSocket::~DatagramSocket() DatagramSocket::~DatagramSocket()
{ {
internalClose(); closeSocket();
} }
void DatagramSocket::open(int family) void DatagramSocket::open(int family)
{ {
if (mHandle != INVALID_SOCKET || mFamily != family) if (mHandle != INVALID_SOCKET || mFamily != family)
closeSocket(); closeSocket();
assert(family == AF_INET || family == AF_INET6); assert(family == AF_INET || family == AF_INET6);
mFamily = family; mFamily = family;
mHandle = ::socket(mFamily, SOCK_DGRAM, IPPROTO_UDP); mHandle = ::socket(mFamily, SOCK_DGRAM, IPPROTO_UDP);
if (mHandle != INVALID_SOCKET) if (mHandle != INVALID_SOCKET)
{
sockaddr_in addr4; sockaddr_in6 addr6;
memset(&addr4, 0, sizeof(addr4)); memset(&addr6, 0, sizeof(addr6));
socklen_t l = mFamily == AF_INET ? sizeof(addr4) : sizeof(addr6);
int retcode = getsockname(mHandle, (mFamily == AF_INET ? (sockaddr*)&addr4 : (sockaddr*)&addr6), &l);
if (!retcode)
{ {
sockaddr_in addr4; mLocalPort = ntohs(mFamily == AF_INET ? addr4.sin_port : addr6.sin6_port);
sockaddr_in6 addr6;
memset(&addr4, 0, sizeof(addr4));
memset(&addr6, 0, sizeof(addr6));
socklen_t l = mFamily == AF_INET ? sizeof(addr4) : sizeof(addr6);
int retcode = getsockname(mHandle, (mFamily == AF_INET ? (sockaddr*)&addr4 : (sockaddr*)&addr6), &l);
if (!retcode)
{
mLocalPort = ntohs(mFamily == AF_INET ? addr4.sin_port : addr6.sin6_port);
}
} }
}
} }
int DatagramSocket::localport() int DatagramSocket::localport()
{ {
return mLocalPort; return mLocalPort;
} }
void DatagramSocket::sendDatagram(InternetAddress& dest, const void* packetData, unsigned int packetSize) void DatagramSocket::sendDatagram(InternetAddress &dest, const void *packetData, unsigned int packetSize)
{ {
if (mHandle == INVALID_SOCKET) if (mHandle == INVALID_SOCKET)
return; return;
/*int sent = */ ::sendto(mHandle, (const char*)packetData, packetSize, 0, dest.genericsockaddr(), int sent = ::sendto(mHandle, (const char*)packetData, packetSize, 0, dest.genericsockaddr(), dest.sockaddrLen());
dest.sockaddrLen());
} }
unsigned DatagramSocket::recvDatagram(InternetAddress& src, void* packetBuffer, unsigned packetCapacity) unsigned DatagramSocket::recvDatagram(InternetAddress &src, void *packetBuffer, unsigned packetCapacity)
{ {
if (mHandle == INVALID_SOCKET) if (mHandle == INVALID_SOCKET)
return 0;
sockaddr* addr = nullptr;
socklen_t addrLen = 0;
sockaddr_in addr_4 = {AF_INET, 0, {0}, {0}};
sockaddr_in6 addr_6 = {AF_INET6, 0, 0, {0}, 0};
switch (mFamily)
{
case AF_INET:
addr = (sockaddr*)&addr_4;
addrLen = sizeof(addr_4);
break;
case AF_INET6:
addr = (sockaddr*)&addr_6;
addrLen = sizeof(addr_6);
break;
default:
assert(0);
}
if (!addr || !addrLen)
return 0;
int received = ::recvfrom(mHandle, (char*)packetBuffer, packetCapacity, 0, addr, &addrLen);
if (received > 0)
{
src = InternetAddress((sockaddr&)*addr, addrLen);
return received;
}
return 0; return 0;
}
void DatagramSocket::internalClose() sockaddr_in sourceaddr;
{
if (mHandle != INVALID_SOCKET)
{
#ifdef WIN32 #ifdef WIN32
::closesocket(mHandle); int addrlen = sizeof(sourceaddr);
#else #else
close(mHandle); socklen_t addrlen = sizeof(sourceaddr);
#endif #endif
mHandle = INVALID_SOCKET; int received = ::recvfrom(mHandle, (char*)packetBuffer, packetCapacity, 0, (sockaddr*)&sourceaddr, &addrlen);
} if (received > 0)
{
src = InternetAddress((sockaddr&)sourceaddr, addrlen);
return received;
}
else
return 0;
} }
void DatagramSocket::closeSocket() void DatagramSocket::closeSocket()
{ {
internalClose(); if (mHandle != INVALID_SOCKET)
{
#ifdef WIN32
::closesocket(mHandle);
#else
close(mHandle);
#endif
mHandle = INVALID_SOCKET;
}
} }
bool DatagramSocket::isValid() const bool DatagramSocket::isValid() const
{ {
return mHandle != INVALID_SOCKET; return mHandle != INVALID_SOCKET;
} }
int DatagramSocket::family() const int DatagramSocket::family() const
{ {
return mFamily; return mFamily;
} }
bool DatagramSocket::setBlocking(bool blocking) bool DatagramSocket::setBlocking(bool blocking)
{ {
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
unsigned long mode = blocking ? 0 : 1; unsigned long mode = blocking ? 0 : 1;
return (ioctlsocket(mHandle, FIONBIO, &mode) == 0) ? true : false; return (ioctlsocket(mHandle, FIONBIO, &mode) == 0) ? true : false;
#endif #endif
#if defined(TARGET_OSX) || defined(TARGET_LINUX) #if defined(TARGET_OSX) || defined(TARGET_LINUX)
int flags = fcntl(mHandle, F_GETFL, 0); int flags = fcntl(mHandle, F_GETFL, 0);
if (flags < 0) if (flags < 0)
return false; return false;
flags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK); flags = blocking ? (flags&~O_NONBLOCK) : (flags|O_NONBLOCK);
return (fcntl(mHandle, F_SETFL, flags) == 0) ? true : false; return (fcntl(mHandle, F_SETFL, flags) == 0) ? true : false;
#endif #endif
#if defined(TARGET_ANDROID) #if defined(TARGET_ANDROID)
unsigned long mode = blocking ? 0 : 1; unsigned long mode = blocking ? 0 : 1;
return (ioctl(mHandle, FIONBIO, &mode) == 0) ? true : false; return (ioctl(mHandle, FIONBIO, &mode) == 0) ? true : false;
#endif #endif
return false; return false;
} }
SOCKET DatagramSocket::socket() const SOCKET DatagramSocket::socket() const
{ {
return mHandle; return mHandle;
} }
DatagramAgreggator::DatagramAgreggator() DatagramAgreggator::DatagramAgreggator()
{ {
FD_ZERO(&mReadSet); FD_ZERO(&mReadSet);
mMaxHandle = 0; mMaxHandle = 0;
} }
DatagramAgreggator::~DatagramAgreggator() {} DatagramAgreggator::~DatagramAgreggator()
{
}
void DatagramAgreggator::addSocket(PDatagramSocket socket) void DatagramAgreggator::addSocket(PDatagramSocket socket)
{ {
if (socket->mHandle == INVALID_SOCKET) if (socket->mHandle == INVALID_SOCKET)
return; return;
if (mSocketVector.size() >= 62) FD_SET(socket->mHandle, &mReadSet);
{ if (socket->mHandle > mMaxHandle)
ICELogError(<< "fd_set overflow; too much sockets"); mMaxHandle = socket->mHandle;
return;
}
FD_SET(socket->mHandle, &mReadSet); mSocketVector.push_back(socket);
if (socket->mHandle > mMaxHandle)
mMaxHandle = socket->mHandle;
mSocketVector.push_back(socket);
} }
unsigned DatagramAgreggator::count() unsigned DatagramAgreggator::count()
{ {
return mSocketVector.size(); return mSocketVector.size();
} }
bool DatagramAgreggator::hasDataAtIndex(unsigned index) bool DatagramAgreggator::hasDataAtIndex(unsigned index)
{ {
PDatagramSocket socket = mSocketVector[index]; PDatagramSocket socket = mSocketVector[index];
return FD_ISSET(socket->mHandle, &mReadSet); return (FD_ISSET(socket->mHandle, &mReadSet) != 0);
} }
PDatagramSocket DatagramAgreggator::socketAt(unsigned index) PDatagramSocket DatagramAgreggator::socketAt(unsigned index)
{ {
return mSocketVector[index]; return mSocketVector[index];
} }
bool DatagramAgreggator::waitForData(std::chrono::milliseconds timeout) bool DatagramAgreggator::waitForData(unsigned milliseconds)
{ {
timeval tv; timeval tv;
tv.tv_sec = timeout.count() / 1000; tv.tv_sec = milliseconds / 1000;
tv.tv_usec = (timeout.count() % 1000) * 1000; tv.tv_usec = (milliseconds % 1000) * 1000;
int rescode = ::select(mMaxHandle + 1, &mReadSet, nullptr, nullptr, &tv); int rescode = ::select(mMaxHandle, &mReadSet, NULL, NULL, &tv);
return rescode > 0; return rescode > 0;
} }
+30 -33
View File
@@ -1,4 +1,4 @@
/* Copyright(C) 2007-2026 VoIP objects (voipobjects.com) /* Copyright(C) 2007-2014 VoIP objects (voipobjects.com)
* This Source Code Form is subject to the terms of the Mozilla Public * This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
@@ -9,61 +9,58 @@
#include "HL_InternetAddress.h" #include "HL_InternetAddress.h"
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <chrono>
class NetworkSocket class NetworkSocket
{ {
public: public:
virtual int localport() = 0; virtual int localport() = 0;
}; };
class DatagramSocket class DatagramSocket
{ {
friend class SocketHeap; friend class SocketHeap;
friend class DatagramAgreggator; friend class DatagramAgreggator;
public: public:
DatagramSocket(); DatagramSocket();
virtual ~DatagramSocket(); virtual ~DatagramSocket();
virtual int localport(); virtual int localport();
virtual void sendDatagram(InternetAddress& dest, const void* packetData, unsigned packetSize); virtual void sendDatagram(InternetAddress& dest, const void* packetData, unsigned packetSize);
virtual unsigned recvDatagram(InternetAddress& src, void* packetBuffer, unsigned packetCapacity); virtual unsigned recvDatagram(InternetAddress& src, void* packetBuffer, unsigned packetCapacity);
virtual void closeSocket(); virtual void closeSocket();
virtual bool isValid() const; virtual bool isValid() const;
virtual int family() const; virtual int family() const;
virtual bool setBlocking(bool blocking); virtual bool setBlocking(bool blocking);
virtual SOCKET socket() const; virtual SOCKET socket() const;
virtual void open(int family);
virtual void open(int family);
protected: protected:
int mFamily; int mFamily;
SOCKET mHandle; SOCKET mHandle;
int mLocalPort; int mLocalPort;
void internalClose();
}; };
typedef std::shared_ptr<DatagramSocket> PDatagramSocket; typedef std::shared_ptr<DatagramSocket> PDatagramSocket;
class DatagramAgreggator class DatagramAgreggator
{ {
public: public:
DatagramAgreggator(); DatagramAgreggator();
~DatagramAgreggator(); ~DatagramAgreggator();
void addSocket(PDatagramSocket socket); void addSocket(PDatagramSocket socket);
unsigned count(); unsigned count();
bool hasDataAtIndex(unsigned index); bool hasDataAtIndex(unsigned index);
PDatagramSocket socketAt(unsigned index); PDatagramSocket socketAt(unsigned index);
bool waitForData(std::chrono::milliseconds timeout); bool waitForData(unsigned milliseconds);
protected: protected:
typedef std::vector<PDatagramSocket> SocketList; typedef std::vector<PDatagramSocket> SocketList;
SocketList mSocketVector; SocketList mSocketVector;
fd_set mReadSet; fd_set mReadSet;
SOCKET mMaxHandle; SOCKET mMaxHandle;
}; };
#endif #endif
File diff suppressed because it is too large Load Diff
+60 -60
View File
@@ -10,79 +10,79 @@
#include <windows.h> #include <windows.h>
#if defined(USE_MINIDUMP) #if defined(USE_MINIDUMP)
#include <DbgHelp.h> # include <DbgHelp.h>
#endif #endif
int winVersion() int winVersion()
{ {
DWORD dwVersion = 0; DWORD dwVersion = 0;
DWORD dwMajorVersion = 0; DWORD dwMajorVersion = 0;
DWORD dwMinorVersion = 0; DWORD dwMinorVersion = 0;
DWORD dwBuild = 0; DWORD dwBuild = 0;
dwVersion = GetVersion(); dwVersion = GetVersion();
// Get the Windows version. // Get the Windows version.
dwMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion))); dwMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion)));
dwMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion))); dwMinorVersion = (DWORD)(HIBYTE(LOWORD(dwVersion)));
// Get the build number. // Get the build number.
if (dwVersion < 0x80000000) if (dwVersion < 0x80000000)
dwBuild = (DWORD)(HIWORD(dwVersion)); dwBuild = (DWORD)(HIWORD(dwVersion));
if (dwMajorVersion == 5) if (dwMajorVersion == 5)
return Win_Xp; return Win_Xp;
if (dwMinorVersion == 1) if (dwMinorVersion == 1)
return Win_Seven; return Win_Seven;
else else
return Win_Vista; return Win_Vista;
} }
// ----------------- CrashMiniDump ----------------- // ----------------- CrashMiniDump -----------------
#if defined(USE_MINIDUMP) #if defined(USE_MINIDUMP)
static LONG WINAPI MyExceptionHandler(EXCEPTION_POINTERS* ExceptionInfo) static LONG WINAPI MyExceptionHandler(EXCEPTION_POINTERS* ExceptionInfo)
{ {
// Open the file // Open the file
HANDLE hFile = HANDLE hFile = CreateFile( L"MiniDump.dmp", GENERIC_READ | GENERIC_WRITE,
CreateFile(L"MiniDump.dmp", GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
if ((hFile != NULL) && (hFile != INVALID_HANDLE_VALUE)) if( ( hFile != NULL ) && ( hFile != INVALID_HANDLE_VALUE ) )
{ {
// Create the minidump // Create the minidump
MINIDUMP_EXCEPTION_INFORMATION mdei; MINIDUMP_EXCEPTION_INFORMATION mdei;
mdei.ThreadId = GetCurrentThreadId(); mdei.ThreadId = GetCurrentThreadId();
mdei.ExceptionPointers = ExceptionInfo; mdei.ExceptionPointers = ExceptionInfo;
mdei.ClientPointers = FALSE; mdei.ClientPointers = FALSE;
MINIDUMP_TYPE mdt = MiniDumpWithFullMemory; MINIDUMP_TYPE mdt = MiniDumpWithFullMemory;
BOOL rv = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, mdt, BOOL rv = MiniDumpWriteDump( GetCurrentProcess(), GetCurrentProcessId(),
(ExceptionInfo != 0) ? &mdei : 0, 0, 0); hFile, mdt, (ExceptionInfo != 0) ? &mdei : 0, 0, 0 );
// Close the file // Close the file
CloseHandle(hFile); CloseHandle( hFile );
} }
else else
{ {
} }
return EXCEPTION_CONTINUE_SEARCH; return EXCEPTION_CONTINUE_SEARCH;
} }
static LPTOP_LEVEL_EXCEPTION_FILTER OldExceptionHandler = nullptr; static LPTOP_LEVEL_EXCEPTION_FILTER OldExceptionHandler = nullptr;
void CrashMiniDump::registerHandler() void CrashMiniDump::registerHandler()
{ {
OldExceptionHandler = ::SetUnhandledExceptionFilter(&MyExceptionHandler); OldExceptionHandler = ::SetUnhandledExceptionFilter(&MyExceptionHandler);
} }
void CrashMiniDump::unregisterHandler() void CrashMiniDump::unregisterHandler()
{ {
::SetUnhandledExceptionFilter(nullptr); ::SetUnhandledExceptionFilter(nullptr);
} }
#endif #endif
@@ -124,29 +124,29 @@ int _kbhit()
ioctl(STDIN, FIONREAD, &bytesWaiting); ioctl(STDIN, FIONREAD, &bytesWaiting);
return bytesWaiting;*/ return bytesWaiting;*/
static const int STDIN_FILENO = 0; static const int STDIN_FILENO = 0;
struct termios oldt, newt; struct termios oldt, newt;
int ch; int ch;
int oldf; int oldf;
tcgetattr(STDIN_FILENO, &oldt); tcgetattr(STDIN_FILENO, &oldt);
newt = oldt; newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO); newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt); tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0); oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK); fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar(); ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt); tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf); fcntl(STDIN_FILENO, F_SETFL, oldf);
if (ch != EOF) if(ch != EOF)
{ {
ungetc(ch, stdin); ungetc(ch, stdin);
return 1; return 1;
} }
return 0; return 0;
} }
#endif #endif
+9 -9
View File
@@ -10,11 +10,11 @@
enum enum
{ {
Win_Xp = 0, Win_Xp = 0,
Win_Vista = 1, Win_Vista = 1,
Win_Seven = 2, Win_Seven = 2,
Win_Eight = 3, Win_Eight = 3,
Win_Ten = 4 Win_Ten = 4
}; };
extern int winVersion(); extern int winVersion();
@@ -22,8 +22,8 @@ extern int winVersion();
class CrashMiniDump class CrashMiniDump
{ {
public: public:
static void registerHandler(); static void registerHandler();
static void unregisterHandler(); static void unregisterHandler();
}; };
extern void writeMiniDump(); extern void writeMiniDump();
@@ -41,7 +41,7 @@ int iosVersion();
#include <sys/select.h> #include <sys/select.h>
#include <termios.h> #include <termios.h>
#if defined(TARGET_LINUX) #if defined(TARGET_LINUX)
// # include <stropts.h> //# include <stropts.h>
#endif #endif
extern int _kbhit(); extern int _kbhit();
@@ -49,6 +49,6 @@ extern int _kbhit();
#endif #endif
#if defined(TARGET_WIN) #if defined(TARGET_WIN)
#include <conio.h> # include <conio.h>
#endif #endif
#endif #endif
+28 -26
View File
@@ -5,49 +5,51 @@
#include "HL_Pointer.h" #include "HL_Pointer.h"
UsageCounter::UsageCounter() {} UsageCounter::UsageCounter()
{}
UsageCounter::~UsageCounter() {} UsageCounter::~UsageCounter()
{}
int UsageCounter::obtain(int usageId) int UsageCounter::obtain(int usageId)
{ {
Lock l(mGuard); Lock l(mGuard);
UsageMap::iterator usageIter = mUsage.find(usageId); UsageMap::iterator usageIter = mUsage.find(usageId);
if (usageIter != mUsage.end()) if (usageIter != mUsage.end())
usageIter->second = usageIter->second + 1; usageIter->second = usageIter->second + 1;
else else
mUsage[usageId] = 1; mUsage[usageId] = 1;
return usageCount(); return usageCount();
} }
int UsageCounter::release(int usageId) int UsageCounter::release(int usageId)
{ {
Lock l(mGuard); Lock l(mGuard);
UsageMap::iterator usageIter = mUsage.find(usageId); UsageMap::iterator usageIter = mUsage.find(usageId);
if (usageIter == mUsage.end()) if (usageIter == mUsage.end())
return usageCount();
usageIter->second = usageIter->second - 1;
if (!usageIter->second)
mUsage.erase(usageIter);
return usageCount(); return usageCount();
usageIter->second = usageIter->second - 1;
if (!usageIter->second)
mUsage.erase(usageIter);
return usageCount();
} }
int UsageCounter::usageCount() int UsageCounter::usageCount()
{ {
Lock l(mGuard); Lock l(mGuard);
UsageMap::const_iterator usageIter; UsageMap::const_iterator usageIter;
int result = 0; int result = 0;
for (usageIter = mUsage.begin(); usageIter != mUsage.end(); usageIter++) for (usageIter = mUsage.begin(); usageIter != mUsage.end(); usageIter++)
result += usageIter->second; result += usageIter->second;
return result; return result;
} }
void UsageCounter::clear() void UsageCounter::clear()
{ {
Lock l(mGuard); Lock l(mGuard);
mUsage.clear(); mUsage.clear();
} }
+9 -9
View File
@@ -13,17 +13,17 @@
class UsageCounter class UsageCounter
{ {
public: public:
UsageCounter(); UsageCounter();
~UsageCounter(); ~UsageCounter();
int obtain(int usageId); int obtain(int usageId);
int release(int usageId); int release(int usageId);
int usageCount(); int usageCount();
void clear(); void clear();
protected: protected:
typedef std::map<int, int> UsageMap; typedef std::map<int, int> UsageMap;
UsageMap mUsage; UsageMap mUsage;
Mutex mGuard; Mutex mGuard;
}; };
#endif #endif
-399
View File
@@ -1,399 +0,0 @@
#pragma once
/// @file
/// @brief A thread-local fixed-block memory pool plus a std-conforming Allocator wrapper, used to
/// pool the small, short-lived, per-RTP-packet shared_ptr nodes produced by std::allocate_shared
/// (e.g. allocate_shared<jrtplib::RTPPacket> on the capture path and allocate_shared<RtpBuffer::
/// Packet> in the jitter buffer). Those objects are fixed-size and churn at the packet rate, so a
/// pool removes them from the general allocator's hot path.
///
/// Pooling is active by default. Define HL_RTP_POOL=0 at compile time to make hl::PoolAllocator a
/// transparent passthrough to the global allocator (i.e. allocate_shared behaves like make_shared)
/// for A/B benchmarking without touching the call sites.
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <mutex>
#include <new>
#include <vector>
#ifndef HL_RTP_POOL
#define HL_RTP_POOL 1
#endif
namespace hl
{
#if HL_RTP_POOL
/// @class FixedBlockPool
/// A process-wide, fixed-block pool with a lock-free thread-local fast path: uniform 256-byte
/// blocks carved from 64 KB chunks, an intrusive thread-local free list, and a per-block header
/// tag so deallocate() is O(1) and lock-free for any block (and can tell pooled blocks from the
/// global-allocator fallback used for oversized requests) regardless of the freeing thread.
///
/// CROSS-THREAD RECLAIM (2026-07-26) — fixes an unbounded capacity ratchet. A uniform block size
/// makes a block allocated on one thread safe to free on another, but "safe" is not "reclaimed".
/// With a purely thread-local free list the block joins the FREEING thread's list, so a
/// producer/consumer split strands it: vq-core allocates RTPPacket / jitter-buffer nodes on the
/// capture threads and frees them on the interval/reap side, so the capture threads' lists were
/// perpetually empty and refill() carved a fresh chunk every BlocksPerChunk allocations, forever.
/// Observed on vqmonitor (the only box with >1 capture thread): capacityBlocks() 256 -> 11.8M
/// = 2.7 GiB over 4 days at ~25 MiB/h, while activeBlocks() drained to 0 every idle interval.
///
/// The fix is a process-wide depot of free-block batches. A thread whose free list grows past
/// ThreadCacheHighWater hands a batch down to the depot; a thread that runs dry takes a batch
/// from the depot before carving a new chunk; a thread flushes its remainder to the depot on
/// exit. The depot is touched once per BlocksPerChunk operations and the hysteresis between
/// "empty" and the high-water keeps it off the per-packet path, so the fast path stays lock-free.
/// capacityBlocks() now plateaus at the true concurrent high-water instead of ratcheting.
class FixedBlockPool
{
public:
/// Usable bytes handed back to the caller from a pooled block. Comfortably covers the
/// shared_ptr nodes we pool (control block + RTPPacket / RtpBuffer::Packet, ~90-130 bytes).
static constexpr std::size_t PayloadSize = 240;
static constexpr std::size_t BlocksPerChunk = 256;
/// Free blocks a thread keeps to itself before handing a BlocksPerChunk batch to the depot.
/// Must exceed BlocksPerChunk so a thread that churns around the boundary does not bounce
/// on the depot mutex; 2x leaves a full batch in hand after every release.
static constexpr std::size_t ThreadCacheHighWater = 2 * BlocksPerChunk;
static void* allocate(std::size_t size)
{
if (size > PayloadSize)
{
uint8_t* raw = static_cast<uint8_t*>(::operator new(size + HeaderSize));
tagOf(raw) = TagGlobal;
s_activeGlobal.fetch_add(1, std::memory_order_relaxed);
return raw + HeaderSize;
}
ThreadCache& tc = cache();
if (tc.head == nullptr)
{
// Reclaim from the depot before asking the OS for more memory. Only when the
// depot is dry as well has the pool genuinely outgrown its current capacity.
std::size_t taken = 0;
tc.head = depot().takeBatch(taken);
if (tc.head == nullptr)
{
tc.head = registry().refill();
taken = BlocksPerChunk;
}
tc.count = taken;
}
uint8_t* block = static_cast<uint8_t*>(tc.head);
tc.head = nextOf(block);
--tc.count;
s_activeBlocks.fetch_add(1, std::memory_order_relaxed);
return block + HeaderSize;
}
static void deallocate(void* ptr) noexcept
{
if (ptr == nullptr)
return;
uint8_t* block = static_cast<uint8_t*>(ptr) - HeaderSize;
if (tagOf(block) == TagPool)
{
ThreadCache& tc = cache();
nextOf(block) = tc.head;
tc.head = block;
++tc.count;
s_activeBlocks.fetch_sub(1, std::memory_order_relaxed);
// The one line that closes the ratchet: surplus goes back to the depot, where
// the thread that actually allocates can reach it.
if (tc.count >= ThreadCacheHighWater)
releaseBatch(tc, BlocksPerChunk);
}
else
{
::operator delete(static_cast<void*>(block));
s_activeGlobal.fetch_sub(1, std::memory_order_relaxed);
}
}
/// @name Diagnostics (relaxed gauges — not synchronization)
/// @{
/// Blocks currently handed out from the pool = pooled allocate() minus
/// pooled deallocate(). Unlike capacityBlocks() (the chunk high-water,
/// which only ever grows) this DROPS when objects are freed, so a rising
/// activeBlocks() is a genuine leak of referenced objects rather than a
/// traffic peak that merely carved extra chunks. Covers every pooled
/// shared_ptr node (allocate_shared<RTPPacket> + jitter-buffer packets).
static std::int64_t activeBlocks() noexcept { return s_activeBlocks.load(std::memory_order_relaxed); }
/// Live oversized allocations that overflowed to ::operator new.
static std::int64_t activeGlobalAllocations() noexcept { return s_activeGlobal.load(std::memory_order_relaxed); }
/// Total blocks ever carved (chunks x BlocksPerChunk) = high-water capacity.
/// With cross-thread reclaim this PLATEAUS once the pool has covered the
/// concurrent high-water; sustained linear growth means reclaim is not working.
static std::int64_t capacityBlocks() noexcept
{
return s_chunks.load(std::memory_order_relaxed) * static_cast<std::int64_t>(BlocksPerChunk);
}
/// Free blocks parked in the process-wide depot, i.e. reclaimed from a freeing
/// thread and available to any allocating thread. Healthy steady state is a
/// non-zero, bounded value: it is the surplus that used to be stranded.
static std::int64_t depotBlocks() noexcept { return s_depotBlocks.load(std::memory_order_relaxed); }
/// Approximate live pooled payload bytes (excludes per-block header).
static std::int64_t activeBytes() noexcept { return activeBlocks() * static_cast<std::int64_t>(PayloadSize); }
/// @}
private:
inline static std::atomic<std::int64_t> s_activeBlocks{0};
inline static std::atomic<std::int64_t> s_activeGlobal{0};
inline static std::atomic<std::int64_t> s_chunks{0};
inline static std::atomic<std::int64_t> s_depotBlocks{0};
static constexpr std::size_t HeaderSize =
alignof(std::max_align_t) >= sizeof(uint64_t) ? alignof(std::max_align_t) : sizeof(uint64_t);
static constexpr std::size_t BlockSize = HeaderSize + PayloadSize;
static constexpr uint64_t TagPool = 0x504F4F4C52545008ULL; // "POOLRTP\b"
static constexpr uint64_t TagGlobal = 0x474C4F42524C0808ULL; // "GLOBRL\b\b"
/// A free block's payload is dead space, so the list links live there. Slot 0 chains
/// blocks within a list/batch; slots 1-2 are used only on a batch's head block, to chain
/// batches inside the depot and to carry the batch length (batches from a thread-exit
/// flush are shorter than BlocksPerChunk).
static_assert(PayloadSize >= 2 * sizeof(void*) + sizeof(std::size_t),
"pooled block payload must hold the free-list, depot and length slots");
static uint64_t& tagOf(void* block) noexcept { return *reinterpret_cast<uint64_t*>(block); }
static void*& nextOf(void* block) noexcept
{
return *reinterpret_cast<void**>(static_cast<uint8_t*>(block) + HeaderSize);
}
static void*& batchNextOf(void* block) noexcept
{
return *reinterpret_cast<void**>(static_cast<uint8_t*>(block) + HeaderSize + sizeof(void*));
}
static std::size_t& batchLenOf(void* block) noexcept
{
return *reinterpret_cast<std::size_t*>(static_cast<uint8_t*>(block) + HeaderSize + 2 * sizeof(void*));
}
/// Per-thread free list. The destructor hands the remainder to the depot so a thread that
/// exits does not strand its blocks — the same ratchet as the cross-thread case, just
/// triggered once per thread instead of continuously.
struct ThreadCache
{
void* head = nullptr;
std::size_t count = 0;
~ThreadCache()
{
while (count > 0 && head != nullptr)
releaseBatch(*this, count);
}
};
static ThreadCache& cache() noexcept
{
static thread_local ThreadCache tc;
return tc;
}
/// Detach the first `blocks` entries of the thread list and park them in the depot.
static void releaseBatch(ThreadCache& tc, std::size_t blocks) noexcept
{
if (blocks == 0 || tc.head == nullptr)
return;
void* batchHead = tc.head;
void* batchTail = batchHead;
std::size_t length = 1;
for (; length < blocks && nextOf(batchTail) != nullptr; ++length)
batchTail = nextOf(batchTail);
tc.head = nextOf(batchTail);
tc.count -= length;
nextOf(batchTail) = nullptr;
batchLenOf(batchHead) = length;
depot().giveBatch(batchHead, length);
}
/// Process-wide store of reclaimed batches. Intrusive (the links live in the free blocks
/// themselves) so neither path can allocate or throw — deallocate() is noexcept. The mutex
/// is taken once per BlocksPerChunk operations, not per packet.
class Depot
{
public:
void* takeBatch(std::size_t& lengthOut) noexcept
{
std::lock_guard<std::mutex> lock(m_Mutex);
void* batch = m_Head;
if (batch == nullptr)
{
lengthOut = 0;
return nullptr;
}
m_Head = batchNextOf(batch);
lengthOut = batchLenOf(batch);
batchNextOf(batch) = nullptr;
s_depotBlocks.fetch_sub(static_cast<std::int64_t>(lengthOut), std::memory_order_relaxed);
return batch;
}
void giveBatch(void* batch, std::size_t length) noexcept
{
std::lock_guard<std::mutex> lock(m_Mutex);
batchNextOf(batch) = m_Head;
m_Head = batch;
s_depotBlocks.fetch_add(static_cast<std::int64_t>(length), std::memory_order_relaxed);
}
private:
std::mutex m_Mutex;
void* m_Head = nullptr;
};
class ChunkRegistry
{
public:
void* refill()
{
const std::size_t chunkBytes = BlockSize * BlocksPerChunk;
uint8_t* chunk = static_cast<uint8_t*>(::operator new(chunkBytes));
s_chunks.fetch_add(1, std::memory_order_relaxed);
{
std::lock_guard<std::mutex> lock(m_Mutex);
m_Chunks.push_back(chunk);
}
void* list = nullptr;
for (std::size_t i = 0; i < BlocksPerChunk; ++i)
{
uint8_t* block = chunk + i * BlockSize;
tagOf(block) = TagPool;
nextOf(block) = list;
list = block;
}
return list;
}
private:
std::mutex m_Mutex;
std::vector<uint8_t*> m_Chunks;
};
/// Both singletons are deliberately immortal (leaked at exit, reclaimed by the OS).
/// A thread_local ThreadCache is destroyed at thread exit — for the main thread that is
/// during static destruction — and its destructor touches the depot, so a depot with a
/// destructor could be used after being destroyed. The same applies to the chunk memory:
/// any pooled object outliving the registry would free into deleted chunks. Never
/// destroying them removes both hazards; the process is exiting either way.
static Depot& depot() noexcept
{
static Depot* instance = new Depot();
return *instance;
}
static ChunkRegistry& registry()
{
static ChunkRegistry* instance = new ChunkRegistry();
return *instance;
}
};
#endif // HL_RTP_POOL
/// Mode-independent accessors for the pool diagnostics, so callers need not know
/// whether HL_RTP_POOL is compiled in. When pooling is disabled they return -1
/// ("not applicable") since allocate_shared then behaves like make_shared.
#if HL_RTP_POOL
inline std::int64_t poolActiveBlocks() noexcept
{
return FixedBlockPool::activeBlocks();
}
inline std::int64_t poolCapacityBlocks() noexcept
{
return FixedBlockPool::capacityBlocks();
}
inline std::int64_t poolDepotBlocks() noexcept
{
return FixedBlockPool::depotBlocks();
}
inline std::int64_t poolActiveGlobal() noexcept
{
return FixedBlockPool::activeGlobalAllocations();
}
inline std::int64_t poolActiveBytes() noexcept
{
return FixedBlockPool::activeBytes();
}
#else
inline std::int64_t poolActiveBlocks() noexcept
{
return -1;
}
inline std::int64_t poolCapacityBlocks() noexcept
{
return -1;
}
inline std::int64_t poolDepotBlocks() noexcept
{
return -1;
}
inline std::int64_t poolActiveGlobal() noexcept
{
return -1;
}
inline std::int64_t poolActiveBytes() noexcept
{
return -1;
}
#endif
/// @class PoolAllocator
/// A stateless, std-conforming Allocator suitable for std::allocate_shared. When HL_RTP_POOL is
/// enabled it serves single-node allocations from FixedBlockPool; otherwise (and for any request
/// that does not fit a pooled block) it delegates to the global allocator, matching make_shared.
template<class T>
struct PoolAllocator
{
using value_type = T;
PoolAllocator() noexcept = default;
template<class U>
PoolAllocator(const PoolAllocator<U>&) noexcept
{
}
T* allocate(std::size_t n)
{
#if HL_RTP_POOL
return static_cast<T*>(FixedBlockPool::allocate(n * sizeof(T)));
#else
return static_cast<T*>(::operator new(n * sizeof(T)));
#endif
}
void deallocate(T* p, std::size_t /*n*/) noexcept
{
#if HL_RTP_POOL
FixedBlockPool::deallocate(p);
#else
::operator delete(static_cast<void*>(p));
#endif
}
template<class U>
bool operator==(const PoolAllocator<U>&) const noexcept
{
return true;
}
template<class U>
bool operator!=(const PoolAllocator<U>&) const noexcept
{
return false;
}
};
} // namespace hl

Some files were not shown because too many files have changed in this diff Show More