devopvoid / webrtc-java

WebRTC for desktop platforms running Java
Apache License 2.0
264 stars 63 forks source link

Video cannot be obtained from the SRS service #132

Open skill7899 opened 11 months ago

skill7899 commented 11 months ago

Describe the bug A clear and concise description of what the bug is.

To Reproduce Steps to reproduce the behavior:

https://ossrs.io/lts/en-us/docs/v5/doc/getting-started run

CANDIDATE="your localip" docker run --rm -it -p 1935:1935 -p 1985:1985 -p 8080:8080 \ --env CANDIDATE=$CANDIDATE -p 8000:8000/udp \ registry.cn-hangzhou.aliyuncs.com/ossrs/srs:5 ./objs/srs -c conf/rtc.conf

i start the video from web, http://localhost:8080/players/rtc_publisher.html?schema=http web push video to srs, webrtc-java connect to srs and pull the video,but fail。nothing get it

/*
 * Copyright 2019 Alex Andres
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.example.webrtc;

import dev.onvoid.webrtc.*;
import dev.onvoid.webrtc.media.MediaStreamTrack;
import dev.onvoid.webrtc.media.MediaStreamTrackMuteListener;
import dev.onvoid.webrtc.media.audio.AudioOptions;
import dev.onvoid.webrtc.media.audio.AudioTrackSink;
import dev.onvoid.webrtc.media.audio.AudioTrackSource;
import dev.onvoid.webrtc.media.audio.AudioTrack;
import dev.onvoid.webrtc.media.video.VideoDeviceSource;
import dev.onvoid.webrtc.media.video.VideoFrame;
import dev.onvoid.webrtc.media.video.VideoTrack;
import dev.onvoid.webrtc.media.video.VideoTrackSink;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static dev.onvoid.webrtc.RTCRtcpMuxPolicy.NEGOTIATE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

class RTCPeerConnectionTests {

    protected PeerConnectionFactory factory;

    private RTCPeerConnection peerConnection;

    private RestTemplate restTemplate;

    void init() {

    }

    void dispose() {
        peerConnection.close();
    }

    public static void main(String[] args) {
        new RTCPeerConnectionTests().configuration();
    }
    void  configuration() {
        restTemplate = new RestTemplate();
        factory = new PeerConnectionFactory();
        RTCConfiguration config = new RTCConfiguration();
        PeerConnectionObserver observer = candidate -> {};

        peerConnection = factory.createPeerConnection(config, observer);
                addRecvOnlyAudioTransceiver();
        addRecvOnlyVideoTransceiver();

        Data data = new Data();
        RTCOfferOptions rtcOfferOptions = new RTCOfferOptions();
        //rtcOfferOptions.iceRestart =  true;
        peerConnection.createOffer(rtcOfferOptions, new CreateSessionDescriptionObserver() {
            @Override
            public void onSuccess(RTCSessionDescription description) {
                peerConnection.setLocalDescription(description, new SetSessionDescriptionObserver() {
                    @Override
                    public void onSuccess() {
                        System.out.println("成功");

                    }

                    @Override
                    public void onFailure(String error) {
                        System.out.println("失败:" + error);
                    }
                });
                data.setApi("http://localhost:1985/rtc/v1/play/");
                data.setSdp(description.sdp.toString());
                data.setStreamurl("webrtc://localhost/live/livestream");
                data.setTid("5705199");
                Map answer = restTemplate.postForObject("http://localhost:1985/rtc/v1/play/", data, Map.class);

                peerConnection.setRemoteDescription(new RTCSessionDescription(RTCSdpType.ANSWER, answer.get("sdp").toString()), new SetSessionDescriptionObserver() {
                    @Override
                    public void onSuccess() {
                        System.out.println("成功接上");

                    }

                    @Override
                    public void onFailure(String error) {
                        System.out.println("失败了:" + error);
                    }
                });
            }

            @Override
            public void onFailure(String error) {
                System.out.println("失败" + error);
            }
        });

        try {
            Thread.sleep(1000000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        peerConnection.close();
    }
    @Setter
    @Getter
    public static class Data{
        String api;
        String streamurl;
        String sdp;
        String tid;
        String clientip;
    }

    void addTrack() {
        AudioTrackSource audioSource = factory.createAudioSource(new AudioOptions());
        AudioTrack audioTrack = factory.createAudioTrack("audioTrack", audioSource);
        audioTrack.addSink(new AudioTrackSink() {
            @Override
            public void onData(byte[] data, int bitsPerSample, int sampleRate, int channels, int frames) {
                System.out.println(data);
            }
        });
        VideoDeviceSource videoSource = new VideoDeviceSource();
        VideoTrack videoTrack = factory.createVideoTrack("videoTrack", videoSource);
        videoTrack.addSink(new VideoTrackSink() {
            @Override
            public void onVideoFrame(VideoFrame frame) {
                System.out.println(frame.timestampNs);
            }
        });
        List<String> streamIds = new ArrayList<>();
        streamIds.add("stream-0");

        RTCRtpSender audioSender = peerConnection.addTrack(audioTrack, streamIds);
        RTCRtpSender videoSender = peerConnection.addTrack(videoTrack, streamIds);

        RTCRtpSender[] senders = peerConnection.getSenders();
        RTCRtpReceiver[] receivers = peerConnection.getReceivers();
        for (RTCRtpReceiver receiver : receivers) {
            receiver.getTrack().addTrackMuteListener(new MediaStreamTrackMuteListener() {
                @Override
                public void onTrackMute(MediaStreamTrack track, boolean muted) {
                    System.out.println(1111);
                }
            });
        }

    }
    void addRecvOnlyVideoTransceiver() {
        VideoDeviceSource videoSource = new VideoDeviceSource();
        VideoTrack track = factory.createVideoTrack("videoTrack", videoSource);
        track.addSink(new VideoTrackSink() {
            @Override
            public void onVideoFrame(VideoFrame frame) {
                System.out.println(1111);
            }
        });
        RTCRtpTransceiverInit init = new RTCRtpTransceiverInit();
        init.direction = RTCRtpTransceiverDirection.RECV_ONLY;

        RTCRtpTransceiver transceiver = peerConnection.addTransceiver(track, init);
        transceiver.getReceiver().getTrack().addTrackMuteListener(new MediaStreamTrackMuteListener() {
            @Override
            public void onTrackMute(MediaStreamTrack track, boolean muted) {
                System.out.println(1111);
            }
        });
    }

    void addRecvOnlyAudioTransceiver() {
        AudioTrackSource audioSource = factory.createAudioSource(new AudioOptions());
        AudioTrack track = factory.createAudioTrack("audioTrack", audioSource);
        track.addSink(new AudioTrackSink() {
            @Override
            public void onData(byte[] data, int bitsPerSample, int sampleRate, int channels, int frames) {
                System.out.println(data);
            }
        });
        RTCRtpTransceiverInit init = new RTCRtpTransceiverInit();
        init.direction = RTCRtpTransceiverDirection.RECV_ONLY;

        RTCRtpTransceiver transceiver = peerConnection.addTransceiver(track, init);

    }

    void addAudioTransceiver() {
        AudioTrackSource audioSource = factory.createAudioSource(new AudioOptions());
        AudioTrack track = factory.createAudioTrack("audioTrack", audioSource);
        track.addSink(new AudioTrackSink() {
            @Override
            public void onData(byte[] data, int bitsPerSample, int sampleRate, int channels, int frames) {
                System.out.println(1111);
            }
        });
        RTCRtpTransceiver transceiver = peerConnection.addTransceiver(track, new RTCRtpTransceiverInit());

        assertNotNull(transceiver);

        RTCRtpTransceiver[] transceivers = peerConnection.getTransceivers();

        assertNotNull(transceivers);
        assertEquals(1, transceivers.length);
        assertEquals(transceiver.getSender().getTrack().getId(), transceivers[0].getSender().getTrack().getId());
        assertEquals(RTCRtpTransceiverDirection.SEND_RECV, transceiver.getDirection());
    }

}

Expected behavior A clear and concise description of what you expected to happen.

i can get the video frame

Screenshots If applicable, add screenshots to help explain your problem.

image

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

skill7899 commented 10 months ago

@devopvoid help

skill7899 commented 10 months ago

The same situation as this issue https://github.com/devopvoid/webrtc-java/issues/34