After an IP camera is connected to LinkVisual, you must create an HTTP web server. A web client obtains a live streaming URL from the web server. This topic describes how to obtain a live streaming URL.
You can use different methods to create an HTTP web server. In this example, an HTTP web server is created by using Netty. The live streaming URL is obtained from the web server. You can also create an HTTP web server based on your business requirements.
Prerequisites
An IP camera is connected to LinkVisual. For more information, see Connect the device to IoT Platform.
Development environment
In this example, the development environment consists of the following components:
Operating system: Windows 10 (64-bit)
Java Development Kit (JDK): JDK 8
Integrated development environment (IDE): IntelliJ IDEA Community Edition
Procedure
Open IntelliJ IDEA and create a sample project named Demo.
Add the Maven dependencies to the pom.xml file of the project.
Maven coordinates of LinkVisual SDK for Java:
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-linkvisual</artifactId> <version>1.5.14</version> </dependency>
Maven coordinates of the core Alibaba Cloud SDK for Java:
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.3</version> </dependency>
Maven coordinates of the Netty framework:
<dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.29.Final</version> </dependency>
Create the SampleHttpServer.java file in the src/main/java directory, and then enter the following code:
import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.linkvisual.model.v20180120.QueryLiveStreamingRequest; import com.aliyuncs.linkvisual.model.v20180120.QueryLiveStreamingResponse; import com.aliyuncs.profile.DefaultProfile; import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.Unpooled; import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.codec.http.*; import com.aliyuncs.profile.IClientProfile; import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; /** * SampleHttpServer */ public class SampleHttpServer { private static String LIVE_PATH = "/live/stream"; private static String RESPONSE_SUCCESS_CODE = "200"; private static IAcsClient client = null; private int port; public SampleHttpServer(int port) { this.port = port; } class HttpServerHandler extends ChannelInboundHandlerAdapter { @Override public void channelReadComplete(ChannelHandlerContext ctx) { ctx.flush(); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { if (msg instanceof HttpRequest) { QueryStringDecoder decoder = new QueryStringDecoder(((HttpRequest)msg).uri()); if (decoder.path().equals(LIVE_PATH)) { // Obtain parameters from the HTTP request, such as the device ID, instance ID, and streaming protocol. Note: In this example, the exception handling logic is not specified. String iotId = decoder.parameters().get("iotId").get(0); String scheme = decoder.parameters().get("scheme").get(0); String instanceId = decoder.parameters().get("instanceId").get(0); // Call the API operation to obtain the live streaming URL. QueryLiveStreamingRequest queryLiveStreamingRequest = new QueryLiveStreamingRequest(); queryLiveStreamingRequest.setIotId(iotId); queryLiveStreamingRequest.setIotInstanceId(instanceId); queryLiveStreamingRequest.setScheme(scheme); QueryLiveStreamingResponse queryLiveStreamingResponse = null; try { queryLiveStreamingResponse = client.getAcsResponse(queryLiveStreamingRequest); } catch (Exception e) { // Handle exceptions. } if (queryLiveStreamingResponse != null && queryLiveStreamingResponse.getCode().equals(RESPONSE_SUCCESS_CODE)) { // Return the live streaming URL. FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(queryLiveStreamingResponse.getData().getPath().getBytes())); response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain"); response.headers().set(HttpHeaderNames.CONTENT_LENGTH, queryLiveStreamingResponse.getData().getPath().length()); response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN, "*"); response.headers().set(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true"); ctx.write(response); } else { // Handle exceptions. } } else { FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.BAD_REQUEST, Unpooled.EMPTY_BUFFER); ctx.write(response); } } } } class HttpServerInitializer extends ChannelInitializer<SocketChannel> { @Override public void initChannel(SocketChannel ch) { ChannelPipeline p = ch.pipeline(); p.addLast(new HttpServerCodec()); p.addLast(new HttpServerHandler()); } } public void start() { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.option(ChannelOption.SO_BACKLOG, 1024); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new HttpServerInitializer()); try { Channel ch = b.bind(port).sync().channel(); ch.closeFuture().sync(); } catch (InterruptedException e) {} } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } public static void main(String[] args) { SampleHttpServer httpServer = new SampleHttpServer(8082); // The AccessKey pair of an Alibaba Cloud account can be used to call all API operations. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. Keep your AccessKey pair confidential. // We recommend that you do not include your AccessKey pair in materials that are easily accessible to others, such as the project code. Otherwise, your AccessKey pair may be leaked and resources in your account become insecure. // In this example, the AccessKey pair is stored in environment variables. String accessKeyID = System.getenv("CC_AK_ENV"); String accessKeySecret = System.getenv("CC_SK_ENV"); String regionId = "ap-northeast-1"; // The ID of the region in which your device resides. You do not need to modify this parameter. String domain = "linkvisual.ap-northeast-1.aliyuncs.com"; // The endpoint. You do not need to modify this parameter. String productCode = "Linkvisual"; // The code of the product. You do not need to modify this parameter. try { DefaultProfile.addEndpoint(regionId, regionId, productCode, domain); IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyID, accessKeySecret); client = new DefaultAcsClient(profile); } catch (Exception e) { } // Enable the HTTP server. httpServer.start(); } }
The following table describes how to obtain the AccessKey pair.
Parameter
Example
Description
accessKeyID
LTAI4GFGQvKuqHJhFa******
Log on to the IoT Platform console, move the pointer over the profile picture, and then click AccessKey Management to obtain the AccessKey ID and AccessKey secret.
NoteIf you use a RAM user, you must attach the AliyunIOTFullAccess policy to the RAM user. This policy allows the RAM user to manage IoT Platform resources. If you do not attach the policy to the RAM user, the connection to IoT Platform fails. For more information about how to grant permissions to a RAM user, see Access IoT Platform as a RAM user.
accessKeySecret
iMS8ZhCDdfJbCMeA005sieKe******
Run the code to obtain the live streaming URL and enable the HTTP server.