Created
July 9, 2020 16:14
-
-
Save shannah/6340312195dd6bbfe0bb4842ff79845a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2014 The Chromium Embedded Framework Authors. All rights | |
// reserved. Use of this source code is governed by a BSD-style license that | |
// can be found in the LICENSE file. | |
package com.codename1.impl.javase.cef; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import org.cef.callback.CefCallback; | |
import org.cef.handler.CefResourceHandlerAdapter; | |
import org.cef.misc.IntRef; | |
import org.cef.misc.StringRef; | |
import org.cef.network.CefRequest; | |
import org.cef.network.CefResponse; | |
/** | |
* The example for the second scheme with domain handling is a more | |
* complex example and is taken from the parent project CEF. Please | |
* see CEF: "cefclient/scheme_test.cpp" for futher details | |
*/ | |
public class ClientSchemeHandler extends CefResourceHandlerAdapter { | |
public static final String scheme = "client"; | |
public static final String domain = "tests"; | |
private byte[] data_; | |
private String mime_type_; | |
private int offset_ = 0; | |
public ClientSchemeHandler() { | |
super(); | |
} | |
@Override | |
public synchronized boolean processRequest(CefRequest request, CefCallback callback) { | |
boolean handled = false; | |
String url = request.getURL(); | |
// Using https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3 downloaded locally | |
// as a test file | |
File f = new File("/Users/shannah/tmp/test.mp3"); | |
try (FileInputStream fis = new FileInputStream(f)){ | |
data_ = new byte[(int)f.length()]; | |
fis.read(data_); | |
mime_type_ = "audio/mp3"; | |
handled = true; | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
if (handled) { | |
// Indicate the headers are available. | |
callback.Continue(); | |
return true; | |
} | |
return false; | |
} | |
@Override | |
public void getResponseHeaders( | |
CefResponse response, IntRef response_length, StringRef redirectUrl) { | |
response.setMimeType(mime_type_); | |
response.setStatus(200); | |
// Set the resulting response length | |
response_length.set(data_.length); | |
System.out.println("Response length: "+data_.length); | |
} | |
@Override | |
public synchronized boolean readResponse( | |
byte[] data_out, int bytes_to_read, IntRef bytes_read, CefCallback callback) { | |
boolean has_data = false; | |
if (offset_ < data_.length) { | |
// Copy the next block of data into the buffer. | |
int transfer_size = Math.min(bytes_to_read, (data_.length - offset_)); | |
System.arraycopy(data_, offset_, data_out, 0, transfer_size); | |
offset_ += transfer_size; | |
System.out.println("Read "+transfer_size+". Offset now "+offset_); | |
bytes_read.set(transfer_size); | |
has_data = true; | |
} else { | |
offset_ = 0; | |
bytes_read.set(0); | |
} | |
return has_data; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[java] [java] Response length: 764176 | |
[java] [java] Read 65536. Offset now 65536 | |
[java] [java] Read 65536. Offset now 131072 | |
[java] [java] In shouldNavigate callback with url: {"duration":27252} | |
[java] [java] In shouldNavigate callback with url: {"state":"playing"} | |
[java] [java] In shouldNavigate callback with url: {"time":32} | |
[java] [java] In shouldNavigate callback with url: {"time":189} | |
[java] [java] In shouldNavigate callback with url: {"time":441} | |
[java] [java] In shouldNavigate callback with url: {"time":701} | |
[java] [java] In shouldNavigate callback with url: {"time":933} | |
[java] [java] In shouldNavigate callback with url: {"time":1183} | |
[java] [java] In shouldNavigate callback with url: {"time":1434} | |
[java] [java] In shouldNavigate callback with url: {"time":1686} | |
[java] [java] In shouldNavigate callback with url: {"time":1943} | |
[java] [java] In shouldNavigate callback with url: {"time":2134} | |
[java] [java] In shouldNavigate callback with url: {"time":2134} | |
... | |
[java] [java] Exception in thread "AWT-AppKit" [0709/085805.325138:ERROR:batching_media_log.cc(38)] MediaEvent: {"error":"FFmpegDemuxer: data source error"} | |
[java] [java] [0709/085805.327419:ERROR:batching_media_log.cc(35)] MediaEvent: {"pipeline_error":9} | |
[java] [java] In shouldNavigate callback with url: {"error":"PIPELINE_ERROR_READ: FFmpegDemuxer: data source error. Code=2"} | |
[java] [java] In shouldNavigate callback with url: {"time":2197} | |
[java] [java] In shouldNavigate callback with url: {"state":"paused"} | |
[java] [java] [EDT] 0:0:44,524 - PIPELINE_ERROR_READ: FFmpegDemuxer: data source error. Code=2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<body> | |
<audio src="client://tests/streams/1" controls autoplay/> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment