【问题标题】:Exception Copying Stream from Payload从有效负载复制流的异常
【发布时间】:2018-02-27 17:38:39
【问题描述】:

我很难修复这些东西。设置是,我实现了 Camera.PreviewCallback 对象的 onPreviewFrame 方法,以逐帧获取要发送到连接设备的数据字节。

  Camera.PreviewCallback previewCallback= new Camera.PreviewCallback() {
    @Override
    public void onPreviewFrame(byte[] bytes, Camera camera) {
        Log.e(TAG, "onPreviewFrame: BYTE SIZE: "+bytes.toString() );
        framesCallback.getCurrentData(bytes);
    }
};

回调是 framesCallback.getCurrentData(bytes); 。我在 CameraActivity 中实现了回调,代码如下:

/**
 * INITIALIZE THE REALTIME CALLBACK FOR GETTING RAW DATA OF SURFACE(IMAGE) TO BE SEND AS PAYLOAD
 */
private void initFramesToBeSendCallback() {
    surfaceHolder.setFramesToBeRetrieveListener(new CustomSurfaceHolder.OnCaptureFramesToBeSendCallback() {
        @Override
        public void getCurrentData(byte[] bytes) {
            if (isRecording) {
                Log.e(TAG, "initFramesToBeSendCallback RECORDING, will send data!");
                is= new ByteArrayInputStream(bytes);
                sendFramesOfImages(is);
            } else {
                Log.e(TAG, "initFramesToBeSendCallback NOT RECORDING");
            }

        }
    });
}

由于它更新并发送每一帧字节,我使用方法:

 is= new ByteArrayInputStream(bytes);
            sendFramesOfImages(is);

读取每个字节并将其保存在 InputStream 中。 sendFramesOfImages(is); 方法发送实时帧数据。这是 Google Nearby Connections 发送流数据负载,参数是我创建的输入流:

 private void sendFramesOfImages(InputStream is) {
    Nearby.Connections.sendPayload(client, SERVICE_ID, Payload.fromStream(is))
            .setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(@NonNull Status status) {
                    if (status.isSuccess()) {
                        Log.e(TAG, "onResult: STREAM DATA SUCCESS");
                    }
                }
            });
}

当触发数据发送的Button被点击时。它显示错误日志

02-28 04:22:53.015 12814-12898/shaveyourneck.syn.app.com.shaveyourneck W/NearbyConnections: Unable to deliver status for Payload -5402869508027937544
                                                                                        java.io.IOException: write failed: EPIPE (Broken pipe)
                                                                                            at libcore.io.IoBridge.write(IoBridge.java:542)
                                                                                            at java.io.FileOutputStream.write(FileOutputStream.java:186)
                                                                                            at java.io.FileOutputStream.write(FileOutputStream.java:191)
                                                                                            at com.google.android.gms.internal.zzcom.zza(Unknown Source)
                                                                                            at com.google.android.gms.internal.zzcom.zza(Unknown Source)
                                                                                            at com.google.android.gms.internal.zzcon.run(Unknown Source)
                                                                                            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                                            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                                            at java.lang.Thread.run(Thread.java:818)
                                                                                         Caused by: android.system.ErrnoException: write failed: EPIPE (Broken pipe)
                                                                                            at libcore.io.Posix.writeBytes(Native Method)
                                                                                            at libcore.io.Posix.write(Posix.java:223)
                                                                                            at libcore.io.BlockGuardOs.write(BlockGuardOs.java:313)
                                                                                            at libcore.io.IoBridge.write(IoBridge.java:537)
                                                                                            at java.io.FileOutputStream.write(FileOutputStream.java:186) 
                                                                                            at java.io.FileOutputStream.write(FileOutputStream.java:191) 
                                                                                            at com.google.android.gms.internal.zzcom.zza(Unknown Source) 
                                                                                            at com.google.android.gms.internal.zzcom.zza(Unknown Source) 
                                                                                            at com.google.android.gms.internal.zzcon.run(Unknown Source) 
                                                                                            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                                                            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

而且我无法发送所需的数据。请问这方面有什么帮助吗?任何帮助将不胜感激。

【问题讨论】:

    标签: java android stream inputstream google-nearby


    【解决方案1】:

    您的 sendPayload 调用中存在错误。它应该是sendPayload(GoogleApiClient, remoteEndpointId, Payload),但您将 SERVICE_ID 作为 remoteEndpointId 传递。

    除此之外,您正在为每一帧数据创建一个新流。我建议制作一个流来不断地泵送数据。效率会更高。

    ParcelFileDescriptor[] payloadPipe = ParcelFileDescriptor.createPipe();
    
    // Send the remote side one half of the payload pipe.
    Nearby.getConnectionsClient(context).sendPayload(endpointId, Payload.fromStream(payloadPipe[0]));
    
    // Use the other half of the payload pipe to pump data through.
    surfaceHolder.setFramesToBeRetrieveListener(new CustomSurfaceHolder.OnCaptureFramesToBeSendCallback() {
            @Override
            public void getCurrentData(byte[] bytes) {
                if (isRecording) {
                    payloadPipe[1].write(bytes);
                }
            }
        });
    

    【讨论】:

      猜你喜欢
      • 2018-02-16
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-09
      • 2019-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多