V
V
Volodymyr Palamar2020-04-04 02:15:28
Java
Volodymyr Palamar, 2020-04-04 02:15:28

How to make ffmpeg work with AudioTrack?

there is such a code for playing an audio signal that came from a udp socket

public int SAMPLE_RATE = 44100;
    public int CHANNEL = AudioFormat.CHANNEL_OUT_MONO;
    public int bfsze = AudioTrack.getMinBufferSize(SAMPLE_RATE,CHANNEL,AudioFormat.ENCODING_PCM_16BIT);





public void startSpeakers() {
        // Creates the thread for receiving and playing back audio
        if (bfsze == AudioTrack.ERROR || bfsze == AudioTrack.ERROR_BAD_VALUE){
            //bfsze = SAMPLE_RATE*2;
        }
        if(!speakers) {
            Toast.makeText(getApplicationContext(),"ok start "+bfsze,Toast.LENGTH_SHORT).show();
            speakers = true;
            Thread receiveThread = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        // Create an instance of AudioTrack, used for playing back audio
                        Log.i(LOG_TAG, "Receive thread started. Thread id: " + Thread.currentThread().getId());
                        
                         AudioTrack track = new AudioTrack(AudioManager.STREAM_MUSIC, SAMPLE_RATE, CHANNEL,
                                                          AudioFormat.ENCODING_PCM_16BIT, bfsze, AudioTrack.MODE_STREAM);
                        track.play();
                        try {
                            // Define a socket to receive the audio
                            DatagramSocket socket = new DatagramSocket(port);
                            
                            byte[] buf = new byte[bfsze];
                            while(speakers) {
                                // Play back the audio received from packets
                                DatagramPacket packet = new DatagramPacket(buf, bfsze);
                                socket.receive(packet);
                                Log.i(LOG_TAG, "Packet received: " + packet.getLength());
                        
                                
                                track.write(packet.getData(), 0, bfsze);
                            }
                            // Stop playing back and release resources
                            socket.disconnect();
                            socket.close();
                            track.stop();
                            track.flush();
                            track.release();
                            speakers = false;
                            return;
                        }
                        catch(SocketException e) {

                            Log.e(LOG_TAG, "SocketException: " + e.toString());
                            speakers = false;
                        }
                        catch(IOException e) {

                            Log.e(LOG_TAG, "IOException: " + e.toString());
                            speakers = false;
                        }
                    }
                });
            receiveThread.start();
        }
  }


But it all works only in tandem with this
public void startMic() {
        // Creates the thread for capturing and transmitting audio
        mic = true;
        try
        {
            address = InetAddress.getByName("192.168.8.101");
        }
        catch (UnknownHostException e)
        {}
        Thread thread = new Thread(new Runnable() {

                @Override
                public void run() {
                    // Create an instance of the AudioRecord class
                    Log.i(LOG_TAG, "Send thread started. Thread id: " + Thread.currentThread().getId());
                    AudioRecord audioRecorder = new AudioRecord (MediaRecorder.AudioSource.VOICE_COMMUNICATION, SAMPLE_RATE,
                                                                 AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT, 
                                                                 AudioRecord.getMinBufferSize(SAMPLE_RATE, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT)*10);
                    int bytes_read = 0;
                    int bytes_sent = 0;
                    byte[] buf = new byte[bfsze];
                    try {
                        // Create a socket and start recording
                        Log.i(LOG_TAG, "Packet destination: " + address.toString());
                        DatagramSocket socket = new DatagramSocket();
                        //socket.setBroadcast(true);
                        audioRecorder.startRecording();
                        while(mic) {
                            // Capture audio from the mic and transmit it
                            bytes_read = audioRecorder.read(buf, 0, bfsze);
                            DatagramPacket packet = new DatagramPacket(buf, bytes_read, address, port);
                            socket.send(packet);
                            bytes_sent += bytes_read;
                            Log.i(LOG_TAG, "Total bytes sent: " + bytes_sent);
                            Thread.sleep(20, 0);
                            
                        }
                        // Stop recording and release resources
                        audioRecorder.stop();
                        audioRecorder.release();
                        socket.disconnect();
                        socket.close();
                        mic = false;
                        return;
                    }
                    
                    catch(InterruptedException e) {

                        Log.e(LOG_TAG, "InterruptedException: " + e.toString());
                        mic = false;
                    }
                    catch(SocketException e) {

                        Log.e(LOG_TAG, "SocketException: " + e.toString());
                        mic = false;
                    }
                    catch(UnknownHostException e) {

                        Log.e(LOG_TAG, "UnknownHostException: " + e.toString());
                        mic = false;
                    }
                    catch(IOException e) {

                        Log.e(LOG_TAG, "IOException: " + e.toString());
                        mic = false;
                    }
                }
            });
        thread.start();
    }


The sound that the program recorded and transmitted is the same, but on another device it plays normally.
I tried
ffmpeg -y  -i test.mp3  -acodec pcm_s16le -f s16le -ac 2 -ar 44100 udp://192.168.8.101:8079

But the output is some kind of alien music

Tried with Python
with open("test.pcm","rb") as f:
    b=f.read(14144) #14144 это размер буфера
    while 1:
        sock.send(b,(192.168.8.101,8079))
        b=f.read(14144)

This helped a little at first, the norms and then everything is in a bunch

Who knows what the problem is ??

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Volodymyr Palamar, 2020-04-04
@GORNOSTAY25

4.4.2020
the only working option and it somehow intermittently forwards, we can assume that this is via udp

#!/usr/bin/python
import socket,time
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
with open(0,'rb') as f:
    b = f.read(14144)
    while b:
        sock.sendto(b, ('192.168.8.100', 8079))
        time.sleep(0.079)
        b = f.read(14144)

ffmpeg -i file.mp3 -f s16le -ac 1 -ar 44100 - | ./pytoudp.py

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question