通过阅读本文,您可以查看合流代码的示例。

示例说明

本文以Java语言为例进行介绍,获取其他语言SDK,请参见视频直播SDK

SDK引入

  1. 引入阿里云核心SDK和视频直播SDK。此处版本仅供参考,获取最新的版本请参见视频直播SDK
    1. 引入阿里云核心SDK。
      <dependencies>
          <dependency>    
              <groupId>com.aliyun</groupId>    
              <artifactId>aliyun-java-sdk-core</artifactId>    
              <version>4.5.17</version>
          </dependency>
      </dependencies>
    2. 引入视频直播SDK。
      <dependencies>
          <dependency>
              <groupId>com.aliyun</groupId>
              <artifactId>aliyun-java-sdk-live</artifactId>
              <version>3.9.4</version>
          </dependency>
      </dependencies>
  2. 初始化IAcsClient实例。

    视频直播SDK通过IAcsClient实例完成OpenAPI的调用。因此,在发起调用前需要先初始化IAcsClient实例。

    public void init() throws ClientException {
         DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
         client = new DefaultAcsClient(profile);
     }
  3. 查看合流服务示例代码。
    • 创建合流任务(使用预设布局)
      public void createDefault() throws ClientException {
          CreateMixStreamRequest createMixStreamRequest = new CreateMixStreamRequest();
          // 设置拉流域名(直播播流域名,即用该域名观看合流后的内容)
          createMixStreamRequest.setDomainName("your domain name");
          // 设置预设的layoutId,以"MixStreamLayout-2-1"为例
          createMixStreamRequest.setLayoutId("MixStreamLayout-2-1"); 
      
          // 设置输出配置
          JSONObject outputConfig = new JSONObject();
          outputConfig.put("AppName", "your app name"); // AppName  
          outputConfig.put("StreamName", "your stream name"); // StreamName
          outputConfig.put("MixStreamTemplate", "lp_ld"); // 合流输出规格模板
          String outputConfigStr = outputConfig.toJSONString();
          createMixStreamRequest.setOutputConfig(outputConfigStr);
      
          // 初始化输入流
          JSONArray inputStreamList = new JSONArray();
      
          // 构建输入流A
          JSONObject inputStreamA = new JSONObject();
          inputStreamA.put("LayoutChildId", 1); // 布局id,注意:一定要从1开始,并且所有id要连续
          inputStreamA.put("ResourceType", "url"); // 资源类型: url 或 live
          inputStreamA.put("ResourceValue", "your resource value"); // 资源地址
      
          inputStreamList.add(inputStream);
      
          // 构建输入流B(方法同A)
          JSONObject inputStreamB = new JSONObject();
          inputStreamB.put("LayoutChildId", 2);
          inputStreamB.put("ResourceType", "url");
          inputStreamB.put("ResourceValue", "your resource value");
      
          inputStreamList.add(inputStreamB);
      
          // 设置输入流
          String inputStreamListStr =  inputStreamList.toJSONString();  
          createMixStreamRequest.setInputStreamList(inputStreamListStr);
      
          // 发起openapi请求
          CreateMixStreamResponse createMixStreamResponse = client.getAcsResponse(createMixStreamRequest);
      
          // 打印合流id
          String mixStreamId = createMixStreamResponse.getMixStreamId();
          System.out.println(mixStreamId);
      }
    • 创建合流任务(自定义布局)
      public void createMixStream() throws ClientException {
          CreateMixStreamRequest createMixStreamRequest = new CreateMixStreamRequest();
          // 设置拉流域名(直播播流域名,即用该域名观看合流后的内容)
          createMixStreamRequest.setDomainName("your domain name");
          // 设置布局id,自定义布局填写"USERDEFINED"
          createMixStreamRequest.setLayoutId("USERDEFINED"); 
      
          // 设置输出配置
          JSONObject outputConfig = new JSONObject();
          outputConfig.put("AppName", "your app name"); // AppName  
          outputConfig.put("StreamName", "your stream name"); // StreamName
          outputConfig.put("MixStreamTemplate", "lp_ld"); // 合流输出规格模板
          String outputConfigStr = outputConfig.toJSONString();
          createMixStreamRequest.setOutputConfig(outputConfigStr);
      
          // 初始化输入流
          JSONArray inputStreamList = new JSONArray();
      
          // 构建输入流A
          JSONObject inputStreamA = new JSONObject();
          JSONObject layoutConfigA = new JSONObject(); // 自定义布局配置设置
          Float[] fillPositionNormalizedA = new Float[]{0f, 0f}; // 位置坐标
          Float[] fillSizeNormalizedA = new Float[]{1f, 1f}; // 大小归一化值
          layoutConfigA.put("FillMode", "fit");
          layoutConfigA.put("PositionRefer", "topLeft"); 
          layoutConfigA.put("FillPositionNormalized", fillPositionNormalizedA);
          layoutConfigA.put("FillSizeNormalized", fillSizeNormalizedA);
      
          inputStreamA.put("LayoutChildId", 1); // 布局id,注意:一定要从1开始,并且所有id要连续
          inputStreamA.put("ResourceType", "url"); // 资源类型: url 或 live
          inputStreamA.put("ResourceValue", "your resource value"); // 资源地址
          inputStreamA.put("LayoutConfig", layoutConfigA); // 设置自定义的布局配置
          inputStreamList.add(inputStreamA);
      
          // 构建输入流B(方法同A)
          JSONObject inputStreamB = new JSONObject();
          JSONObject layoutConfigB = new JSONObject(); 
          Float[] fillPositionNormalizedB = new Float[]{0.7f, 0.03f};
          Float[] fillSizeNormalizedB = new Float[]{0.25f, 0.25f};
          layoutConfigB.put("FillMode", "fit");
          layoutConfigB.put("PositionRefer", "topLeft"); 
          layoutConfigB.put("FillPositionNormalized", fillPositionNormalizedB);
          layoutConfigB.put("FillSizeNormalized", fillSizeNormalizedB);
      
          inputStreamB.put("LayoutChildId", 2);
          inputStreamB.put("ResourceType", "url");
          inputStreamB.put("ResourceValue", "your resource value");
          inputStreamB.put("LayoutConfig", layoutConfigB);
          inputStreamList.add(inputStreamB);
      
          // 设置输入流
          String inputStreamListStr =  inputStreamList.toJSONString();  
          createMixStreamRequest.setInputStreamList(inputStreamListStr);
      
          // 发起openapi请求
          CreateMixStreamResponse createMixStreamResponse = client.getAcsResponse(createMixStreamRequest);
      
          // 打印合流id
          String mixStreamId = createMixStreamResponse.getMixStreamId();
          System.out.println(mixStreamId);
      }
    • 更新合流任务
      public void updateMixStream() throws ClientException {
          UpdateMixStreamRequest updateMixStreamRequest = new UpdateMixStreamRequest();
          // 设置拉流域名(直播播流域名,即用该域名观看合流后的内容)
          updateMixStreamRequest.setDomainName("your domain name");
          // 设置布局id,自定义布局填写"USERDEFINED"
          updateMixStreamRequest.setLayoutId("USERDEFINED");
          // 设置要更新的合流id,即之前创建过的合流任务
          updateMixStreamRequest.setMixStreamId("your mixstreamId");
      
          // 初始化输入流
          JSONArray inputStreamList = new JSONArray();
      
          // 构建输入流A
          JSONObject inputStreamA = new JSONObject();
          JSONObject layoutConfigA = new JSONObject(); // 自定义布局配置设置
          Float[] fillPositionNormalizedA = new Float[]{0f, 0f}; // 位置坐标
          Float[] fillSizeNormalizedA = new Float[]{1f, 1f}; // 大小归一化值
          layoutConfigA.put("FillMode", "fit");
          layoutConfigA.put("PositionRefer", "topLeft"); 
          layoutConfigA.put("FillPositionNormalized", fillPositionNormalizedA);
          layoutConfigA.put("FillSizeNormalized", fillSizeNormalizedA);
      
          inputStreamA.put("LayoutChildId", 1); // 布局id,注意:一定要从1开始,并且所有id要连续
          inputStreamA.put("ResourceType", "url"); // 资源类型: url 或 live
          inputStreamA.put("ResourceValue", "your resource value"); // 资源地址
          inputStreamA.put("LayoutConfig", layoutConfigA); // 设置自定义的布局配置
          inputStreamList.add(inputStreamA);
      
          // 构建输入流B(方法同A)
          JSONObject inputStreamB = new JSONObject();
          JSONObject layoutConfigB = new JSONObject(); 
          Float[] fillPositionNormalizedB = new Float[]{0.5f, 0.5f};
          Float[] fillSizeNormalizedB = new Float[]{0.3f, 0.3f};
          layoutConfigB.put("FillMode", "fit");
          layoutConfigB.put("PositionRefer", "topLeft"); 
          layoutConfigB.put("FillPositionNormalized", fillPositionNormalizedB);
          layoutConfigB.put("FillSizeNormalized", fillSizeNormalizedB);
      
          inputStreamB.put("LayoutChildId", 2);
          inputStreamB.put("ResourceType", "url");
          inputStreamB.put("ResourceValue", "your resource value");
          inputStreamB.put("LayoutConfig", layoutConfigB);
          inputStreamList.add(inputStreamB);
      
          // 设置输入流
          String inputStreamListStr =  inputStreamList.toJSONString();
          updateMixStreamRequest.setInputStreamList(inputStreamListStr);
      
          // 发起openapi请求
          UpdateMixStreamResponse updateMixStreamResponse = client.getAcsResponse(updateMixStreamRequest);
          String mixStreamId = updateMixStreamResponse.getMixStreamId();
      
          // 打印合流id
          System.out.println(mixStreamId);
      }
    • 查询合流任务
      public void describeMixStreamList() throws ClientException {
          DescribeMixStreamListRequest describeMixStreamListRequest = new DescribeMixStreamListRequest();
          // 设置拉流域名(直播播流域名,即用该域名观看合流后的内容)
          describeMixStreamListRequest.setDomainName("your domain name");
          // 设置appName
          describeMixStreamListRequest.setAppName("your app name");
          // 设置查询开始时间
          describeMixStreamListRequest.setStartTime("2020-12-03T14:00:00Z");
          // 设置查询结束时间
          describeMixStreamListRequest.setEndTime("2020-12-05T14:00:00Z");
          
          // 发起openapi请求
          DescribeMixStreamListResponse describeMixStreamListResponse = client.getAcsResponse(describeMixStreamListRequest);
          
          // 获取合流列表
          List<MixStreamInfo> result = describeMixStreamListResponse.getMixStreamList();
          
          // 打印合流id
          if (result != null && !result.isEmpty()) {
              for (MixStreamInfo mixStreamInfo : result) {
                  String mixStreamId = mixStreamInfo.getMixstreamId();
                  System.out.println(mixStreamId);
              }
          }
      }
    • 删除合流任务
      public void deleteMixStream() throws ClientException {
          DeleteMixStreamRequest deleteMixStreamRequest = new DeleteMixStreamRequest();
          // 设置拉流域名(直播播流域名,此前用该域名观看合流后的内容)
          deleteMixStreamRequest.setDomainName("your domain name");
          // 设置appName
          deleteMixStreamRequest.setAppName("your app name");
          // 设置流名
          deleteMixStreamRequest.setStreamName("your stream name");
          // 设置要删除的合流id
          deleteMixStreamRequest.setMixStreamId("your mixstreamId");
      
          // 发起openapi请求
          DeleteMixStreamResponse deleteMixStreamResponse = client.getAcsResponse(deleteMixStreamRequest);
          String mixStreamId = deleteMixStreamResponse.getMixStreamId();
      
          // 打印合流id
          System.out.println(mixStreamId);
      }

更新合流任务

public void updateMixStream() throws ClientException {
    UpdateMixStreamRequest updateMixStreamRequest = new UpdateMixStreamRequest();
    // 设置拉流域名(直播播流域名,即用该域名观看合流后的内容)
    updateMixStreamRequest.setDomainName("your domain name");
    // 设置布局id,自定义布局填写"USERDEFINED"
    updateMixStreamRequest.setLayoutId("USERDEFINED");
    // 设置要更新的合流id,即之前创建过的合流任务
    updateMixStreamRequest.setMixStreamId("your mixstreamId");

    // 初始化输入流
    JSONArray inputStreamList = new JSONArray();

    // 构建输入流A
    JSONObject inputStreamA = new JSONObject();
    JSONObject layoutConfigA = new JSONObject(); // 自定义布局配置设置
    Float[] fillPositionNormalizedA = new Float[]{0f, 0f}; // 位置坐标
    Float[] fillSizeNormalizedA = new Float[]{1f, 1f}; // 大小归一化值
    layoutConfigA.put("FillMode", "fit");
    layoutConfigA.put("PositionRefer", "topLeft"); 
    layoutConfigA.put("FillPositionNormalized", fillPositionNormalizedA);
    layoutConfigA.put("FillSizeNormalized", fillSizeNormalizedA);

    inputStreamA.put("LayoutChildId", 1); // 布局id,注意:一定要从1开始,并且所有id要连续
    inputStreamA.put("ResourceType", "url"); // 资源类型: url 或 live
    inputStreamA.put("ResourceValue", "your resource value"); // 资源地址
    inputStreamA.put("LayoutConfig", layoutConfigA); // 设置自定义的布局配置
    inputStreamList.add(inputStreamA);

    // 构建输入流B(方法同A)
    JSONObject inputStreamB = new JSONObject();
    JSONObject layoutConfigB = new JSONObject(); 
    Float[] fillPositionNormalizedB = new Float[]{0.5f, 0.5f};
    Float[] fillSizeNormalizedB = new Float[]{0.3f, 0.3f};
    layoutConfigB.put("FillMode", "fit");
    layoutConfigB.put("PositionRefer", "topLeft"); 
    layoutConfigB.put("FillPositionNormalized", fillPositionNormalizedB);
    layoutConfigB.put("FillSizeNormalized", fillSizeNormalizedB);

    inputStreamB.put("LayoutChildId", 2);
    inputStreamB.put("ResourceType", "url");
    inputStreamB.put("ResourceValue", "your resource value");
    inputStreamB.put("LayoutConfig", layoutConfigB);
    inputStreamList.add(inputStreamB);

    // 设置输入流
    String inputStreamListStr =  inputStreamList.toJSONString();
    updateMixStreamRequest.setInputStreamList(inputStreamListStr);

    // 发起openapi请求
    UpdateMixStreamResponse updateMixStreamResponse = client.getAcsResponse(updateMixStreamRequest);
    String mixStreamId = updateMixStreamResponse.getMixStreamId();

    // 打印合流id
    System.out.println(mixStreamId);
}

查询合流任务

public void describeMixStreamList() throws ClientException {
    DescribeMixStreamListRequest describeMixStreamListRequest = new DescribeMixStreamListRequest();
    // 设置拉流域名(直播播流域名,即用该域名观看合流后的内容)
    describeMixStreamListRequest.setDomainName("your domain name");
    // 设置appName
    describeMixStreamListRequest.setAppName("your app name");
    // 设置查询开始时间
    describeMixStreamListRequest.setStartTime("2020-12-03T14:00:00Z");
    // 设置查询结束时间
    describeMixStreamListRequest.setEndTime("2020-12-05T14:00:00Z");
    
    // 发起openapi请求
    DescribeMixStreamListResponse describeMixStreamListResponse = client.getAcsResponse(describeMixStreamListRequest);
    
    // 获取合流列表
    List<MixStreamInfo> result = describeMixStreamListResponse.getMixStreamList();
    
    // 打印合流id
    if (result != null && !result.isEmpty()) {
        for (MixStreamInfo mixStreamInfo : result) {
            String mixStreamId = mixStreamInfo.getMixstreamId();
            System.out.println(mixStreamId);
        }
    }
}

删除合流任务

public void deleteMixStream() throws ClientException {
    DeleteMixStreamRequest deleteMixStreamRequest = new DeleteMixStreamRequest();
    // 设置拉流域名(直播播流域名,此前用该域名观看合流后的内容)
    deleteMixStreamRequest.setDomainName("your domain name");
    // 设置appName
    deleteMixStreamRequest.setAppName("your app name");
    // 设置流名
    deleteMixStreamRequest.setStreamName("your stream name");
    // 设置要删除的合流id
    deleteMixStreamRequest.setMixStreamId("your mixstreamId");

    // 发起openapi请求
    DeleteMixStreamResponse deleteMixStreamResponse = client.getAcsResponse(deleteMixStreamRequest);
    String mixStreamId = deleteMixStreamResponse.getMixStreamId();

    // 打印合流id
    System.out.println(mixStreamId);
}