• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import rpc from "@ohos.rpc"
17import avSession from '@ohos.multimedia.avsession';
18
19export default {
20    onStart() {
21        console.info('AVSessionServer: onStart')
22    },
23    onStop() {
24        console.info('AVSessionServer: onStop')
25    },
26    onCommand(want, startId) {
27        console.info('AVSessionServer: onCommand, want: ' + JSON.stringify(want) + ', startId: ' + startId)
28    },
29    onConnect(want) {
30        console.info('AVSessionServer: service onConnect called.')
31        return new Stub("rpcTestAbility")
32    },
33    onDisconnect(want) {
34        console.info('AVSessionServer: service onDisConnect called.')
35    },
36    onReconnect(want) {
37        console.info('AVSessionServer: service onReConnect called.')
38    }
39}
40
41const CODE_CAST_AUDIO = 1;
42let descriptor;
43let controller;
44
45class Stub extends rpc.RemoteObject {
46    constructor(descriptor) {
47        super(descriptor);
48    }
49
50    async onRemoteMessageRequest(code, data, reply, option) {
51        try {
52            console.info(`AVSessionServer: onRemoteRequest: ${code}`);
53            switch (code) {
54                case CODE_CAST_AUDIO:
55                {
56                    console.info('AVSessionServer:case  CODE_CAST_AUDIO');
57                    await avSession.getAllSessionDescriptors().then((descriptors) => {
58                        console.info(descriptors.length);
59                        console.info('AVSessionServer: Get descriptors Successfully');
60                        if (descriptors.length === 0) {
61                            console.info('AVSessionServer: Get descriptors : Fail');
62                        }
63                        descriptor = descriptors[0];
64                    }).catch((err) => {
65                        console.info(`AVSessionServer: ${err.message}`);
66                        return false;
67                    });
68
69                    await avSession.createController(descriptor.sessionId).then((data) => {
70                        console.info('AVSessionServer: Create controller Successfully');
71                        controller = data;
72                    }).catch((err) => {
73                        console.info(`AVSessionServer: ${err.message}`);
74                        return false;
75                    });
76
77                    await controller.getAVMetadata().then((data) => {
78                        console.info(data.assetId);
79                        console.info(data.artist);
80                        console.info('AVSessionServer: Get Metadata');
81                        if (data.assetId === '121278' && data.artist === 'Eminem') {
82                            console.info(`AVSessionServer: Get Metadata is ${data}`);
83                            let writeResult = reply.writeString('case 1 get successfully');
84                            console.info(`AVSessionServer writeString result is ${writeResult}`);
85                            return true;
86                        }
87                    }).catch((err) => {
88                        console.info(`AVSessionServer: ${err.message}`);
89                        return false;
90                    });
91
92                }
93                default:
94                    console.error(`AVSessionServer: default case code is ${code}`);
95                    return super.onRemoteMessageRequest(code, data, reply, option);
96            }
97        } catch (error) {
98            console.info(`AVSessionServer: onRemoteRequest: ${error.message}`);
99        }
100        return false;
101    }
102}