Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rom1v committed Jun 9, 2020
0 parents commit 00d8471
Show file tree
Hide file tree
Showing 40 changed files with 1,171 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.iml
.gradle
/local.properties
/.idea/
.DS_Store
/build
/dist
/captures
.externalNativeBuild
.cxx
46 changes: 46 additions & 0 deletions BUILD.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Build

## Debug

This project is an Android application with some shell scripts to execute on the
computer. Therefore, just use `gradle` as usual:

```
./gradlew assembleDebug
```

(or build from _Android Studio_)

To run it:

```bash
./run
./run <serial> # if several devices are connected
```

_Since building is very fast, `./run` also executes `./gradlew assembleDebug` to
always run an up-to-date version._


## Release

To build and install a release, you need to generate a signed APK.

For that purpose, first generate a _keystore_:

```bash
# generate sndcpy.keystore file
keytool -genkey -v -keystore sndcpy.keystore -alias sndcpy \
-keyalg RSA -keysize 2048 -validity 30000
```

Then, add these lines (and adapt) in `~/.gradle/gradle.properties`:

```bash
SNDCPY_STORE_FILE=/path/to/your/sndcpy.keystore
SNDCPY_STORE_PASSWORD=the_keystore_password
SNDCPY_KEY_ALIAS=sndcpy
SNDCPY_KEY_PASSWORD=the_key_password
```

Then, execute `./release.sh`. It will generate a release in `dist/`.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Romain Vimont

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
119 changes: 119 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# sndcpy

This tool forwards audio from an Android 10 device to the computer. It does not
require any _root_ access. It works on _GNU/Linux_, _Windows_ and _macOS_.

The purpose is to enable [audio forwarding][issue14] while mirroring with
[scrcpy]. However, it can be used independently.

[issue14]: https://github.com/Genymobile/scrcpy/issues/14
[scrcpy]: https://github.com/Genymobile/scrcpy


## Requirements

- The Android device requires at least Android 10.
- [VLC] must be installed on the computer.

[vlc]: https://www.videolan.org/


## Get the app

Download the latest release:

- [`sndcpy-v1.0.zip`][release]
_SHA256: TODO_
- [`sndcpy-with-adb-windows-v1.0.zip`][release-adb]
_SHA256: TODO_

_On Windows, for simplicity, take the second archive, which also contains
`adb`._

[release]: https://github.com/rom1v/sndcpy/releases/download/v1.0/sndcpy-v1.0.zip
[release-adb]: https://github.com/rom1v/sndcpy/releases/download/v1.0/sndcpy-with-adb-windows-v1.0.zip

Alternatively, you could [build the app][BUILD].

[BUILD]: BUILD.md

## Run the app

Plug an Android 10 device with USB debugging enabled, and execute:

```bash
./sndcpy
```

If several devices are connected (listed by `adb devices`):

```bash
./sndcpy <serial> # replace <serial> by the device serial
```

_(omit `./` on Windows)_

It will install the app on the device, and request permission to start audio
capture:

![screenshot](assets/request.png)

Once you clicked on _START NOW_, press _Enter_ in the console to start playing
on the computer. Press `Ctrl`+`c` in the terminal to stop (except on Windows,
just disconnect the device or stop capture from the device notifications).

VLC may print this error message once:

```
main stream error: connection error: Connection refused
```

It is "expected", just ignore it.

The sound continues to be played on the device. The volume can be adjusted
independently on the device and on the computer.

## Uninstall

To uninstall the app from the device:

```bash
adb uninstall com.rom1v.sndcpy
```

## Apps restrictions

`sndcpy` may only forward audio from apps which do not [prevent audio
capture][allow]. The rules are detailed in [§capture policy][rules]:

> - By default, apps that target versions up to and including to Android 9.0 do
> not permit playback capture. To enable it, include
> `android:allowAudioPlaybackCapture="true"` in the app's `manifest.xml` file.
> - By default, apps that target Android 10 (API level 29) or higher allow their
> audio to be captured. To disable playback capture, include
> `android:allowAudioPlaybackCapture="false"` in the app's `manifest.xml`
> file.
So some apps might need to be updated to support audio capture.

[allow]: https://developer.android.com/guide/topics/media/playback-capture#allowing_playback_capture
[rules]: https://developer.android.com/guide/topics/media/playback-capture#capture_policy

## Audio delay

This is just a proof-of-concept, so it's far from perfect.

For example, jitter may cause VLC to automatically increase its buffering,
causing an unacceptable delay:

```
main input error: ES_OUT_SET_(GROUP_)PCR is called too late (pts_delay increased to 377 ms)
```

In that case, just restart it.

## Blog post

- [Audio forwarding on Android 10][blogpost]

[blogpost]: https://blog.rom1v.com/2020/06/audio-forwarding-on-android-10/
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
36 changes: 36 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"

defaultConfig {
applicationId "com.rom1v.sndcpy"
minSdkVersion 29
targetSdkVersion 29
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

if (project.hasProperty("SNDCPY_STORE_FILE")) {
android.signingConfigs {
release {
// to be defined in ~/.gradle/gradle.properties
storeFile file(RELEASE_STORE_FILE)
storePassword RELEASE_STORE_PASSWORD
keyAlias RELEASE_KEY_ALIAS
keyPassword RELEASE_KEY_PASSWORD
}
}
android.buildTypes.release.signingConfig = android.signingConfigs.release
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
29 changes: 29 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rom1v.sndcpy">

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round">
<activity
android:name="com.rom1v.sndcpy.MainActivity"
android:theme="@style/Theme.Transparent"
android:showWhenLocked="true"
android:turnScreenOn="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service
android:name="com.rom1v.sndcpy.RecordService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="mediaProjection" />
</application>
</manifest>
36 changes: 36 additions & 0 deletions app/src/main/java/com/rom1v/sndcpy/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.rom1v.sndcpy;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.projection.MediaProjectionManager;
import android.os.Bundle;

public class MainActivity extends Activity {

private static final int REQUEST_CODE_PERMISSION_AUDIO = 1;
private static final int REQUEST_CODE_START_CAPTURE = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
String[] permissions = {Manifest.permission.RECORD_AUDIO};
requestPermissions(permissions, REQUEST_CODE_PERMISSION_AUDIO);
}

MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(MEDIA_PROJECTION_SERVICE);
Intent intent = mediaProjectionManager.createScreenCaptureIntent();
startActivityForResult(intent, REQUEST_CODE_START_CAPTURE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_START_CAPTURE && resultCode == Activity.RESULT_OK) {
RecordService.start(this, data);
}
finish();
}
}
Loading

0 comments on commit 00d8471

Please sign in to comment.